2

while reading the ethereum docs about contracts came across the example mentioned here. In the ownedcontract constructor there is a casting at statement#2

creator = TokenCreator(msg.sender);

where msg.sender is of type address.

At first glance, I thought it's a constructor call but in the explanation it is clearly mentioned that it is a type cast.

How is it a cast? what is happening in the above statement?

  1. To access a contract we need the address of the deployed contract right?

  2. How is the contract available in the first place?

  3. Will the contract be still available if they both are deployed seperately i.e, one after another?

2

To access a contract we need the address of the deployed contract right?

Right. However, in this example, the OwnedToken contract is actually created by an existing, already-deployed instance of a TokenCreator contract. See below where the TokenCreator contract calls new OwnedToken(name). When a contract creates another contract, the creator contract is the msg.sender of the created contract. Since it wants to later be able to call methods on its creator contract - specifically isTokenTransferOK() - it stores its address in its constructor.

How is the contract available in the first place?

See above: Somebody had already mined TokenCreator, which has a method that creates an instance of OwnedToken.

Will the contract be still available if they both are deployed seperately i.e, one after another?

Well, you could have created an OwnedToken separately rather than making a method in TokenCreator that could create contracts. But OwnedToken needs to know the address of TokenCreator for when it needs to call isTokenTransferOK(). The way it's currently written that's done by relying on the fact that the relevant TokenCreator was its msg.sender. If you create the contract yourself then your address will be msg.sender, so that wouldn't work. You'd need to write it slightly differently - for example by adding an extra parameter to the constructor allowing you to pass in the address of the relevant TokenCreator. (You'd probably also rename TokenCreator to something like TokenController, since it wouldn't be creating tokens any more...)

  • after going through it again is it not some king of cyclic? I mean Owned contact is creating TokenCreator in the constructor and again TokenCreator is creating an instance of OwnedToken again? – mahesh gupta Dec 20 '16 at 9:54

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.