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?
2 Answers
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);
}
}
-
This returns the amount right? What I was asking for is returning the list of addresses. How about returning the list of all the addresses that currently have a balance > 0? Oct 14, 2017 at 6:40
-
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
-
1whether 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/…– manuhaloNov 14, 2017 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– llzNov 15, 2017 at 4:29