0

The example in the docs for truffle .sol tests has these lines:

MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin());

and

MetaCoin meta = new MetaCoin();

I've had a good read through the docs and don't understand why the MetaCoin type shows up so often. Wouldn't it be easier to access the deployed address with:

var meta = DeployedAddresses.MetaCoin(); ?

1

You're creating a state variable meta cast as Type MetaCoin. The compiler understands this Type by seeing the .sol code for MetaCoin, so meta gets all the methods of a MetaCoin contract.

You would accomplish that much with:

MetaCoin meta;

To be meaningful, it needs an address. Maybe it's already published, so a contract that wants to talk to it could get situated using an address passed to the constuctor.

   function MyContract(address metaAddress) {
      meta = MetaCoin(metaAddress);
   }

This is allowed, because meta is a MetaCoin - both sides are cast as the same Type.

Or, maybe you want to deploy a new MetaCoin each time:

function newMetaCoin() returns(address newContract)
    meta = new MetaCoin();
    return(meta);
}

The return works because contracts are directly convertable to Type address. It's shorthand for return(address(meta));

Hope it helps.

0

MetaCoin is simply what the truffle framework uses for demo purposes. Note how the contract is defined under the truffle-webpack-demo repository.

In order for you to interact with any of the functions defined in that contract (i.e. sendCoin), you need to instantiate a MetaCoin "object" that points to the deployed address by doing MetaCoin(DeployedAddresses.MetaCoin()). Simply doing DeployedAddresses.MetaCoin() will give you the address the contract is deployed to but won't give you the ability to make any calls or transactions.

  • So MetaCoin as it appears before the "meta" variable is like a modified "var"? I'm understanding it to mean "var in the context of the metacoin contract". – Pryce Turner Aug 24 '17 at 4:44

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.