How To Develop and Deploy Contracts

Edit contract

You can write contracts based on any editor that supports solidity. The following will be based on remix online editor as an example for explanation. Be careful to use a compiler that is consistent with the contract version.

For convenience of description, we use the following simple contract example:

pragma solidity ^0.4.24;

contract SimpleStorage {
    uint storedData;

    function set(uint x) public {
        storedData = x;
    }

    function get() public view returns (uint) {
        return storedData;
    }
}

(Note: Use SCDO to deploy contracts, the recommended solidity version is 0.4.24-0.4.26)

Compile contract

After the contract is written, you can use the compile function of remix to compile the contract and check for possible syntax errors in the contract.

In the remix interface, click [Run] on the right, Environment select JavaScript VM, you can see that there is a default account, gas limit.

Below is the contract name SimpleStorage we edited. Click Deploy to test the deployment of the contract and get the bytecode.

There is a log window at the bottom of the remix middle panel, showing the result of deploying the contract. Expand the corresponding log content, you can see the status, transaction hash and other information. The input content is the bytecode corresponding to the contract.

The bytecode corresponding to the above example contract is:

0x608060405234801561001057600080fd5b5060df8061001f6000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146078575b600080fd5b348015605957600080fd5b5060766004803603810190808035906020019092919050505060a0565b005b348015608357600080fd5b50608a60aa565b6040518082815260200191505060405180910390f35b8060008190555050565b600080549050905600a165627a7a723058208df15a24cad7025968f08e277ea08a607527ac4c4dc9d8ef22c5f1149ea8160d0029

Deploy contract

Once you have the bytecode, you can use the SCDO client tool to deploy the contract.

You can find the software package under the corresponding platform in [SCDO release].

Note that deploying a contract is the same as sending a transaction. You can run the corresponding SCDO node locally or send the transaction to other SCDO nodes.

Local test environment

To ensure that the contract submitted to the SCDO mainnet is available, it is recommended to run [SCDO private chain node] locally,and deploy contract and call it based on the private chain.

Mainnet environment

After the local test environment is passed, you can deploy the contract to the SCDO mainnet by following the steps of deploying the contract on the private chain.

Deploy the contract through the client tool

  • Prepare an SCDO account and ensure that there is enough balance to deploy the contract. Here we use account 1S011ff44ec45b0019d4c8c2d6d63c73a563bff271 of shard 1, and save the account keystore file

  • Deploy the contract using the sendtx command

./client sendtx --amount 0 --from 1S017f6215c30f1505e0d42769c5472b410d6bf961.keystore --shard 1 --payload 0x608060405234801561001057600080fd5b5060ec8061001f6000396000f3fe6080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146085575b600080fd5b348015605957600080fd5b50608360048036036020811015606e57600080fd5b810190808035906020019092919050505060ad565b005b348015609057600080fd5b50609760b7565b6040518082815260200191505060405180910390f35b8060008190555050565b6000805490509056fea165627a7a7230582045fa1cc38960a931a2b1cf070ffa20bf25683c97838b5f32de5f37a7d6f5568d0029 --gas 100000

The sendtx parameter amount is the transaction amount (in Wen), which can usually be set to 0 for contract deployment; from is the keystore file corresponding to the sending transaction account keyfile;

Payload is the contract bytecode; gas is the minimum gas value required for the transaction (the default is 20000). The execution result of the above command is as follows:

account: 1S011ff44ec45b0019d4c8c2d6d63c73a563bff271, transaction nonce: 0
transaction sent successfully
{
    "Hash": "0x80cb46ef3e3350e5c681397444f2b180624a9c1d534927a12e78cd689ef022fe",
    "Data": {
        "Type": 0,
        "From": "1S011ff44ec45b0019d4c8c2d6d63c73a563bff271",
        "To": "0S0000000000000000000000000000000000000000",
        "Amount": 0,
        "AccountNonce": 0,
        "GasPrice": 10,
        "GasLimit": 200000,
        "Timestamp": 0,
        "Payload": "0x608060405234801561001057600080fd5b5060ec8061001f6000396000f3fe6080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463fffffff16806360fe47b114604e5780636d4ce63c146085575b600080fd5b348015605957600080fd5b50608360048036036020811015606e57600080fd5b810190808035906020019092919050505060ad565b005b348015609057600080fd550609760b7565b6040518082815260200191505060405180910390f35b8060008190555050565b6000805490509056fea165627a7a7230582045fa1cc38960a931a2b1cf070ffa20bf25683c97838b5f32de5f37a7d6f5568d0029"
    },
    "Signature": {
        "Sig": "KHi0wZMIeH4SIDtD0hLvDJp++I3H6C7HpFz5H9AEDbIxUDFydVbif9jQtXd9zeDlNfOdCG2x2QwijGVqlFjItQE="
    }
}

View the contract deployment result according to the transaction Hash:

client getreceipt --hash 0x80cb46ef3e3350e5c681397444f2b180624a9c1d534927a12e78cd689ef022fe

Return result:

{
    "contract": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "failed": false,
    "poststate": "0x7a6b4d2f7b09e1bdaacb3185c2013f361ea9dc0b05a233ecd268188098878874",
    "result": "0x6080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146085575b600080fd5b348015605957600080fd5b50608360048036036020811015606e57600080fd5b810190808035906020019092919050505060ad565b005b348015609057600080fd5b50609760b7565b6040518082815260200191505060405180910390f35b8060008190555050565b6000805490509056fea165627a7a7230582045fa1cc38960a931a2b1cf070ffa20bf25683c97838b5f32de5f37a7d6f5568d0029",
    "totalFee": 837670,
    "txhash": "0x80cb46ef3e3350e5c681397444f2b180624a9c1d534927a12e78cd689ef022fe",
    "usedGas": 83767
}

The value of failed as false in the result indicates that the contract is successfully deployed, and the value of contract is the contract address

Call contract

Use sendTx to call the contract

Obtain method call bytecode through remix

To use the client sendtx command to call a contract, you need to provide the payload information of the contract call method, which can be obtained through remix. At the deployed contract at the bottom right of remix, click the small triangle on the left to see the variables and methods contained in the contract. This example contract contains set and get methods. Fill in the parameter values ​​on the right side of set and click set to implement function call. You can see the result of the call in the log window. Click [Details] to view the call details, where the value of input is the bytecode of the method call. This example calls set and sets the variable value to 21, as shown in the following figure:

Use sendtx to call the contract

The following command calls the set method in the above contract and sets the variable value to 21. The payload is the bytecode obtained from remix, from is the account that initiated the transaction, and to is the contract address

client sendtx --amount 0   --payload 0x60fe47b10000000000000000000000000000000000000000000000000000000000000015 --from 1S017f6215c30f1505e0d42769c5472b410d6bf961.keystore --to 0x47a99059219055cf8277d5d7dff933446edb0012

Result:

account: 1S017f6215c30f1505e0d42769c5472b410d6bf961, transaction nonce: 1
transaction sent successfully
{
    "Hash": "0x44925f095e067c00660769d2488ab44a6eb9cf3183767d343f3005e7d60e98d6",
    "Data": {
        "Type": 0,
        "From": "1S017f6215c30f1505e0d42769c5472b410d6bf961",
        "To": "0x47a99059219055cf8277d5d7dff933446edb0012",
        "Amount": 0,
        "AccountNonce": 1,
        "GasPrice": 10,
        "GasLimit": 200000,
        "Timestamp": 0,
        "Payload": "0x60fe47b10000000000000000000000000000000000000000000000000000000000000015"
    },
    "Signature": {
        "Sig": "m8tt0j10uDlhd06HO9M2iW0xQe5ZcX+i1N6D7IkbA91BG4/MMhsonTXUEleYyeKa+V02S96kzd2QhgyMkdBDZQA="
    }
}

By querying the receipt information, the result of the contract call can be obtained:

client getreceipt --hash 0x44925f095e067c00660769d2488ab44a6eb9cf3183767d343f3005e7d60e98d6

Result:

{
    "contract": "0x",
    "failed": false,
    "poststate": "0x3f5f6e4d2747b50a55b9f86952d7eb6cd42976db80a624a3f2472af8508029d0",
    "result": "0x",
    "totalFee": 416950,
    "txhash": "0x44925f095e067c00660769d2488ab44a6eb9cf3183767d343f3005e7d60e98d6",
    "usedGas": 41695
}

Note: The account calling the contract must be in the same shard as the contract address. SCDO currently does not support cross-shard contract calls

Use call to call contract methods

For methods that only get the value of a variable without changing the state of the contract, you can call it through call. Obtaining the payload information of the corresponding method is similar to the above-mentioned sendtx call contract method. For example, call the get method in the sample contract:

client call --payload 0x6d4ce63c --to 0x47a99059219055cf8277d5d7dff933446edb0012

The following results can be obtained:

{
    "contract": "0x",
    "failed": false,
    "poststate": "0x44f2eb71f9044e8ff3620fae7adf1581a0d1f96cbefbaa546b824de8079c5969",
    "result": "0x0000000000000000000000000000000000000000000000000000000000000015",
    "totalFee": 21696,
    "txhash": "0xbaf65d3280c940d78040535ac925905fdeaefeaf4d210dcc5f0a9d5cd76767d3",
    "usedGas": 21696
}

As you can see, the variable value displayed by result is 21, which was previously set by the set method.

Last updated