3

When deployed as a single source file is the address stored in variable creator = TokenCreator(msg.sender) within function OwnedToken referring to the address of the contract TokenCreator or the address of an external account?

contract OwnedToken {

TokenCreator creator;
address owner;
bytes32 name;

function OwnedToken(bytes32 _name) {
    owner = msg.sender;

    creator = TokenCreator(msg.sender);
    name = _name;
}
function changeName(bytes32 newName) {

    if (msg.sender == creator) name = newName;
}
function transfer(address newOwner) {
    if (msg.sender != owner) return;

    if (creator.isTokenTransferOK(owner, newOwner))
        owner = newOwner;
}


contract TokenCreator {
    function createToken(bytes32 name)
       returns (OwnedToken tokenAddress)
    {

        return new OwnedToken(name);
    }
    function changeName(OwnedToken tokenAddress, bytes32 name) {

        tokenAddress.changeName(name);
    }
    function isTokenTransferOK(
        address currentOwner,
        address newOwner
    ) returns (bool ok) {
        address tokenAddress = msg.sender;
        return (sha3(newOwner) & 0xff) == (bytes20(tokenAddress) & 0xff);
    }
}
3

In creator = TokenCreator(msg.sender);

creator has the address of msg.sender.

msg.sender is the address of the account that creates OwnedToken. There are 2 cases:

  • an external account that creates OwnedToken

OR

  • a contract, like TokenCreator, that creates an OwnedToken (via new OwnedToken(...)).

When deployed as a single source file, only the last contract is instantiated on the blockchain, an instance of TokenCreator which gets an address, call it X.

When a transaction is later sent to X invoking createToken, this is when OwnedToken is instantiated on the blockchain and creator in that OwnedToken will be X.

Note that X is different from the external account that is used to deploy the single source file. And the external account invoking createToken can be the same or different as the one used to deploy the single source file.

1

from the Solidity docs, here is an example of a contract that replicates another:

contract ReplicatorB {

    address creator;
    uint blockCreatedOn;

    function Replicator() 
    {
        creator = msg.sender;
       // next = new ReplicatorA();    // Replicator B can't instantiate A because it doesn't yet know about A
                         // At the time of this writing (Sept 2015), It's impossible to create cyclical relationships
                         // either with self-replicating contracts or A-B-A-B 
        blockCreatedOn = block.number;
    }

  function getBlockCreatedOn() constant returns (uint)
  {
    return blockCreatedOn;
  }

    /**********
     Standard kill() function to recover funds 
     **********/

    function kill()
    { 
        if (msg.sender == creator)
        {
            suicide(creator);  // kills this contract and sends remaining funds back to creator
        }
    }
}

contract ReplicatorA {

    address creator;
  address baddress;
  uint blockCreatedOn;

    function Replicator() 
    {
        creator = msg.sender;
        baddress = new ReplicatorB();    // This works just fine because A already knows about B
        blockCreatedOn = block.number;
    }

  function getBAddress() constant returns (address)
  {
    return baddress;
  }

  function getBlockCreatedOn() constant returns (uint)
  {
    return blockCreatedOn;
  }

    /**********
     Standard kill() function to recover funds 
     **********/

    function kill()
    { 
        if (msg.sender == creator)
        {
            suicide(creator);  // kills this contract and sends remaining funds back to creator
        }
    }
}

github

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.