4

In this tutorial: https://dappsforbeginners.wordpress.com/tutorials/interactions-between-contracts/

they talk about interactions between contracts. However, the metacoin contract appears to be in the same source file as the contract referencing it. What if I wanted to reference a contract that was already deployed? What would the identifier be?

if we have

contract caller{
    metaCoin m = metaCoin(   ...address... )
}

then this will give a compiler error, since type metaCoin is unknown. How do I do this? i.e. reference contracts already on chain

3

When coinCaller is used, it takes as a parameter coinContractAddress, which is the address of the metaCoin contract that's already been deployed. So your coinCaller contract doesn't need that information when you compile it; All it needs is the function signatures of any methods on that contract that it's going to call.

The clean way to do this if you know what contract you'll be calling is to just keep all of it together and "import" the other contracts you're using. It doesn't matter if it's already been deployed - as long as the function signatures of the contract you're including are the same as the function signatures of the contract that's already deployed, it will work.

If including the original source code isn't practical, an easy way to work around it is just to make a stub contract above your own contract containing the function signature of the function you want to call. So if your contract looks like this:

contract coinCaller{
    function sendCoin(address coinContractAddress, address receiver, uint amount){
        metaCoin m = metaCoin(coinContractAddress);
        m.sendToken(receiver, amount);
    }
}

...you need the constructor (which is there automatically as in this case it doesn't take any arguments) and the sendToken method. So just add this to the top of your solidity file:

contract metaCoin { 
    function sendToken(address receiver, uint amount) returns(bool successful){
        // Nothing goes here, just leave it empty
    }
}

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.