0

I've copied the code from here. In the example below, minter's address is kept in the contract:

minter = msg.sender;

In the comments, it's said:

"This is the constructor whose code is run only when the contract is created."

Therefore, minter's address is assigned only once.

Question: When is a contract created?

Is it created when it's called, or when it's deployed (sent to the network)?

     pragma solidity ^0.4.0;

    contract Coin {
    // The keyword "public" makes those variables
    // readable from outside.
    address public minter;
    mapping (address => uint) public balances;

    // Events allow light clients to react on
    // changes efficiently.
    event Sent(address from, address to, uint amount);

    // **** This is the constructor whose code is
    // run only when the contract is created.
    function Coin() {
        minter = msg.sender;
    }

    function mint(address receiver, uint amount) {
        if (msg.sender != minter) return;
        balances[receiver] += amount;
    }

    function send(address receiver, uint amount) {
        if (balances[msg.sender] < amount) return;
        balances[msg.sender] -= amount;
        balances[receiver] += amount;
        Sent(msg.sender, receiver, amount);
    }
}

Also, after the contract is sent to the network, can someone make the contract like:

 Coin co=new Coin();

and deploy it to the network? In this case, would co have a different address than the Coin contract?

3

The contract is created once it is deployed. (in your example, msg.sender is referenced, which is the address of the account that deployed the contract - sent a message to a special zero-address. This is described in the section 4.3 of the yellow-paper where the field to of transaction is explained)

Let me try to answer the second question: somebody can create a new instance of the Coin, but this contract would have a different address (as stated here, The address of the new account is defined as being the rightmost 160 bits of the Keccak hash of the RLP encoding of the structure containing only the sender and the nonce.)

So, the code of the contract would be the same, but everything else (address and state) different.

  • "The contract is created once it is deployed." What happens if the constructor had an argument? For example, function Coin(uint256 initBalance) - what value would it use for initBalance in the Coin constructor? – cogitoergosum Oct 31 '17 at 15:53

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.