0

I have a Crowdsale smart contract taken from the example provided by Ethereum on their website. The Crowdsale contract contains the addresses of the people who participated in the Crowdsale. Is there any way for me to access those address from a different contract and how?

0

I have created a sample contract called CrowdSale with function GetContributors that returns contrbution amount upon passing address.

 contract CrowdSale {
    mapping (address=>uint) contributors;
    function GetContributors(address addr) returns(uint)
    {
    return contributors[addr];
    }
    }

Now Lets create another contract and In this contract we would call the CrowdSale contract by passing address in the constructor of another contract.

cscontract = CrowdSale(crowdsalecontract);

Code below :

     contract DifferentContract{
        CrowdSale  cscontract;
        function DifferentContract(address crowdsalecontract){
        cscontract = CrowdSale(crowdsalecontract);  
        }

        function GetContributorsFromDifferentContract(address addr) returns (uint)
        {
        return cscontract.GetContributors(addr); 
        }
}
0

I think what you are asking is whether or not ContractFoo can access data from ContractBar. In the case of a mapping of addresses, the answer is no. ContractFoo has to have a getter function that's public in order for ContractBar to access its mapping data.

mapping (address=>uint) somemapping; //not accessible because there is no getter

Variables on the other hand is accessible.

uint value;

See this fiddle: https://ethfiddle.com/3_3shuPETg

edit: I was wrong, as the commenter correctly pointed out, if you add public to the mapping like mapping (address=>unit) public somemapping the getter will be automatically created for you. Although...personally I'd rather just explicitly write the getters

  • 1
    whether a variable is accessible or not depends on its visibility: a mapping which is defined as public automatically implements a getter method and can thus be accessed by another contract. See solidity.readthedocs.io/en/develop/… – manuhalo Nov 14 '17 at 11:47
  • haha you're totally right! previously when i added public to a mapping, it just broke the contract, but I think think that was just my fault with the syntax. fiddle to show it working: ethfiddle.com/XvKNFZber4 – llz Nov 15 '17 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.