Solidity

Solidity Contract Creation Basics

Solidity Contract Creation Basics Main Tips

  • Solidity’s contracts are very similar to classes in other object-oriented programming languages.
  • Contracts contain state variables, which store their persistent data, and functions, which contains block of executable code, that can modify the state variables.
  • If you were to call a function from another contract, it would use an EVM function call, which switches the context, making state variables inaccessible.

Solidity Contract Creation Basics

You can created contracts “from outside” using Ethereum transactions or from inside of Solidity contracts.

IDEs, for example, Remix, make the process of screation seamless with UI elements.

Programatically creating contracts on Ethereum is done best by using the web3.js JavaScript API . As of now it provides a method named web3.eth.Contract for facilitating contract creation.

Once a contract has been created, its constructor (A function, which has a name identical to the contract) gets executed once. You may or may not have a constructor. Only a single constructor is allowed, meaning overloading does not get support.

Internally, arguments of the constructor are passed as ABI encoded after the contract code itself, however, if you use web3.js you do not have to worry about this.

When a contract intends to create another contract, the binary and the source code of the contract that was created must be known to the creator. Which means the cyclic creation dependencies are not possible.

Example

pragma solidity ^0.4.0;

contract OwnToken {
    // The contract type TokenGenerator is defined below.
    // So long you do not use it to create a new contract, 
    // you can reference it.
    TokenGenerator creator;
    address owner;
    bytes32 name;

    // The constructor below is used to register the
    // assigned name and the creator.
    function OwnToken(bytes32 _name) {
        // State variables get accessed using their name
        // instead of, for example, this.owner. This applies
        // to functions and even more so in constructors,
        // as they can only be called this way, that is, "internally",
        // since the contract is non-existent for now.
        owner = msg.sender;
        // We perform an type conversion explicitly from 'address'
        // to TokenGenerator, assuming that the type of
        // the contract that is calling is TokenGenerator, there is
        // no actual way of checking that.
        creator = TokenGenerator(msg.sender);
        name = _name;
    }

    function nameChange(bytes32 nameNew) {
        // The only one capable of altering the name is the creator --
        // the comparison can be done since contracts
        // can be implicitly converted to addresses.
        if (msg.sender == address(creator))
            name = nameNew;
    }

    function transfer(address ownerNew) {
        // The current owner only may transfer the token.
        if (msg.sender != owner) return;
        // Also we would like to ask the creator whether the transfer
        // is fine or not. Keep in mind that this calls a function of the
        // contract that is only below. If the call happens to fail (e.g.
        // due to an out-of-gas exception), the execution here will stop
        // right away.
        if (creator.isTokenTransferSuccess(owner, ownerNew))
            owner = ownerNew;
    }
}

contract TokenGenerator {
    function generateToken(bytes32 name)
       returns (OwnToken addressToken)
    {
        // A new token is created and returned to its address.
        // From the side of JavaScript, the return type is simply
        // "address", since this type is the closest available in
        // the ABI.
        return new OwnToken(name);
    }

    function nameChange(OwnToken addressToken, bytes32 name) {
        // "addressToken" has the external type is "address".
        addressToken.nameChange(name);
    }

    function isTokenTransferSuccess(
        address currentOwner,
        address ownerNew
    ) returns (bool ok) {
        // Check an arbitrary condition.
        address addressToken = msg.sender;
        return (keccak256(ownerNew) & 0xff) == (bytes20(addressToken) & 0xff);
    }
}

 

Try on Remix Try live on Hosting

Read previous post:
Solidity Error Handling

Solidity Error Handling Main Tips Solidity utilizes exceptions that can revert state for error handling, undoing all changes that have been...

Close