Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Call contract inside another contarct in solidity version 0.5.2?

I'm using solidity version 0.5.2

pragma solidity ^0.5.2;

contract CampaignFactory{
address[] public deployedCampaigns;

function createCampaign(uint minimum) public{
    address newCampaign  = new Campaign(minimum,msg.sender);  //Error 
//here!!!
    deployedCampaigns.push(newCampaign);
} 

function getDeployedCampaigns() public view returns(address[] memory){
    return deployedCampaigns;
}
}

I'm getting the error while assigning calling the Campaign contract inside CampaignFactory contract

TypeError: Type contract Campaign is not implicitly convertible to expected 
type address.        
address newCampaign  = new Campaign(minimum,msg.sender);

I have another contract called Campaign which i want to access inside CampaignFactory.

contract Campaign{
//some variable declarations and some codes here......

and I have the constructor as below

constructor (uint minimum,address creator) public{
    manager=creator;
    minimumContribution=minimum;

}
like image 640
Kartik ganiga Avatar asked Jan 26 '23 20:01

Kartik ganiga


1 Answers

You can just cast it:

address newCampaign = address(new Campaign(minimum,msg.sender));

Or better yet, stop using address and use the more specific type Campaign:

pragma solidity ^0.5.2;

contract CampaignFactory{
    Campaign[] public deployedCampaigns;

    function createCampaign(uint minimum) public {
        Campaign newCampaign = new Campaign(minimum, msg.sender);
        deployedCampaigns.push(newCampaign);
    } 

    function getDeployedCampaigns() public view returns(Campaign[] memory) {
        return deployedCampaigns;
    }
}
like image 116
user94559 Avatar answered Feb 13 '23 08:02

user94559