SmartContract

Smart contract deployment

The drep smart contract is fully compatible with the Ethereum smart contract, and supports standards like erc20, erc721 and operations like smart contract deployment, calling, searching and so on. You can use tools such as remix and truffle to write and compile smart contracts. Here we take remix as an example to instruct how to deploy and call smart contracts on the drep chain.

  1. Environment preparation

    There is no need for installing any plug-ins to set an environment for the contract, you could start remix (http://remix.ethereum.org/) directly in any browser. There are many examples of token contracts, here is an example of a simplest executable token contract:

    ``` pragma solidity 0.6.6; contract MyToken { / This creates an array with all balances / mapping (address => uint256) public balanceOf;

    /* Initializes contract with initial supply tokens to the creator of the contract */
    function Abstract(uint256 initialSupply) public {
        balanceOf[msg.sender] = initialSupply;
        // Give the creator all initial tokens
    }
    
    /* Send coins */
    function transfer(address _to, uint256 _value) public {
        require(balanceOf[msg.sender] >= _value);
        // Check if the sender has enough
        require(balanceOf[_to] + _value >= balanceOf[_to]);
        // Check for overflows
        balanceOf[msg.sender] -= _value;
        // Subtract from the sender
        balanceOf[_to] += _value;
        // Add the same to the recipient
    }
    

    }

    ```

    This MyToken contract is really simple and runs only two operations

    Create tokens: create a specified number of tokens when initiating a contract, and the token belongs to Drep account which initiated the contract

    Transfer tokens: transfer a specified number of tokens to a specified Drep account

    You can refer to the Ethereum ERC20 Token User Manual to find a complete token contract.

  2. Using remix to write and compile contracts

    < 1 Click on the upper right corner + add a new contract with any name and “.sol” as extension. The new contract added here is named: MyToken.sol.

    < 2 Copy the contract content above to MyToken.sol and use remix to compile. This step will happen automatically in default and the compilation should succeed if there is no red error prompt on the screen.

  3. Contract deployment on drep mainnet

    You can deploy and call contract on the DEPLOY & RUN TRANSACTIONS panel in remix. Select MyToken.sol and click the Deploy button to conduct the contract deployment. In the log box, select the input item and copy the hexadecimal contract content, like 0x608060405234801561001057600080fd5b50610320806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c806351be7b341461004657806370a0823114610074578063a9059cbb146100cc575b600080fd5b6100726004803603602081101561005c57600080fd5b810190808035906020019092919050505061011a565b005b6100b66004803603602081101561008a57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610160565b6040518082815260200191505060405180910390f35b610118600480360360408110156100e257600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610178565b005b806000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555050565b60006020528060005260406000206000915090505481565b806000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410156101c357600080fd5b6000808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205401101561024e57600080fd5b806000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540392505081905550806000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540192505081905550505056fea26469706673582212200b75180354e36422b04317b9fe31cce3fa39db8a0d34988a262173b9f0ac007664736f6c63430006060033 Call the api of drep at this moment and smart contract will be deployed by account_createCode on drep mainnet, the process is as below:

    curl -H "Content-Type: application/json" -X post --data '{"jsonrpc":"2.0","method":"account_createCode","params":["the account address of the contract initiator ","hexadecimal contract "],"id":3}' http://127.0.0.1:15645

  4. Contract calling

    The calling of transfer function: to call this function, you only need to input the destination address and amount in the transfer calling box in the Deployed Contracts column to generate a hexadecimal code, and use the code to call drep’s api interface. For example, he destination address is 0x3ebcbe7cb440dd8c52940a2963472380afbb56c5, 100 coins to transfer, and the hexadecimal code generated will be:0xa9059cbb0000000000000000000000003ebcbe7cb440dd8c52940a2963472380afbb56c50000000000000000000000000000000000000000000000000000000000000064

    The calling process will be: :curl -H "Content-Type: application/json" -X post --data '{"jsonrpc":"2.0","method":"account_executeContract","params":["0xec61c03f719a5c214f60719c3f36bb362a202125","0xecfb51e10aa4c146bf6c12eee090339c99841efc","0xa9059cbb0000000000000000000000003ebcbe7cb440dd8c52940a2963472380afbb56c50000000000000000000000000000000000000000000000000000000000000064","0x110","0x30000"],"id":3}' http://127.0.0.1:15645