简体   繁体   English

在Solidity版本0.5.2中,如何在另一个容器中调用合同?

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

I'm using solidity version 0.5.2 我正在使用Solidity版本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 在分配CampaignFactory合同内的调用Campaign合同时出现错误

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. 我还有另一个称为Campaign的合同,我想在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;

}

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 : 或者更好的是,停止使用address并使用更具体的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;
    }
}

To call an existing contract from another contract ,pass the contract address inside cast 要从另一个合同中调用现有合同,请在演员表中传递合同地址

pragma solidity ^0.5.1;

contract D {
    uint x;
    constructor (uint a) public  {
        x = a;
    }
    function getX() public view returns(uint a)
    {
        return x;
    }
}

contract C {
//DAddress : is the exsiting contract instance address after deployment
    function getValue(address DAddress) public view returns(uint a){
        D d =D(DAddress);
        a=d.getX();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM