Ants block chain BaaS platform application development guide (C): from a simple contract start

Could IDE inlet

The new version of Cloud IDE configuration requires a certificate has been removed, developers may begin after the opening of the block chain directly develop intelligent contracts. In this section, we will use Could IDE to write contracts, compiling and debugging work.
If the chain is experience, boot boot screen in the novice, find the contract chain experience card, click on the commissioning contract .
Ants block chain BaaS platform application development guide (C): from a simple contract start

If you are hosting a formal chain or chain block chain innovation contest, through contract management > New Project or edit an existing project into the Cloud IDE.
Ants block chain BaaS platform application development guide (C): from a simple contract start

From a simple contract start

Select the target chain

Before compiling deployment contract, to specify good to be deployed and deployed with the chain account:
In the right column, click the environment configuration :
Ants block chain BaaS platform application development guide (C): from a simple contract start

In the present embodiment, the selected contract experience chain :
Ants block chain BaaS platform application development guide (C): from a simple contract start

Default Account Account temporarily deployed first with the platform provides Test001 and corresponding private key;

In compiling the deployment process, developers must choose the correct corresponding chain.
Accounts can be deployed using the default accounts, the late developers if there is a demand, you can configure the account to deploy contracts according to their own needs.

Click OK.

Compile contract

In the open Could IDE interface, create a new contract documents (main.sol).
Ants block chain BaaS platform application development guide (C): from a simple contract start

Fill in the middle of the following editor:

pragma solidity ^0.4.0;

contract SimpleStorage {
    uint storedData;

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

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

Cloud IDE in the right-hand column, click compile .
Then compile the results can be seen in the lower left corner (lower region in FIG ×××), comprising Warning and Error. In the right column, a contract can be seen compiled byte code (bytecode The) and contractual interface description (the ABI) (FIG green region of the lower).
Ants block chain BaaS platform application development guide (C): from a simple contract start

点击保存按钮,可以把当前合约文件保存到合约工程中。

注意,这里只是把合约文件保存到工程文件中,并不是保存到区块链上。下一节会介绍如何通过Cloud IDE把编译好到合约部署到链上。

部署合约

当合约编译完毕,点击字节码右上角的部署合约
Ants block chain BaaS platform application development guide (C): from a simple contract start

给要部署的合约实例一个名称:
Ants block chain BaaS platform application development guide (C): from a simple contract start

合约每成功部署一次,在链上就存在一个实例。
同一条链上的合约实例不得重名。

部署成功后,应该在右边栏看到如下信息:合约ID部署交易的交易Hash合约暴露的可调用接口
Ants block chain BaaS platform application development guide (C): from a simple contract start

调用合约

对于上面的样例代码,我们做了非常简单的三件事情:

  • 声明了一个类型为Uint的Stroage变量storedData,该变量在部署的时候会被初始化为0,并保存在区块链上;
  • 暴露了一个设置该变量的接口set(uint256)
  • 暴露了一个获取该变量的接口get()

先点击调用合约调用set接口,在弹出的输入参数窗口,输入一个非负数,点击确定。
调用完成后,可以看到如下结果:
Ants block chain BaaS platform application development guide (C): from a simple contract start

同样的,再调用get方法去获取storedData值,调用成功后,可以在output字段中见到我们设置到值,具体结果参考下面:
Ants block chain BaaS platform application development guide (C): from a simple contract start

至此,我们完整的编译、部署和调用了一个智能合约。这个合约永久的存储在我们的目标链上,合约名或合约ID是该合约实例的访问接口,任何人都可以通过Cloud IDE或其他SDK来接入链并调用该合约所暴露的方法。

进阶

更新合约

Currently Could IDE does not support the contract update. After deployment, if you need to modify the contract, then the only changes, recompile and redeploy into a new instance of the contract, the contract name and contract ID will be updated.

If there is a demand, developers can update the interface to the contract by the SDK, as detailed here and here .

Connected to a contract already deployed

Developers often need to test calls have been deployed contracts currently Could IDE project file is not saved in the name of contract after contract deployment, ID, ABI and bytecode. Therefore, to achieve this requirement, the following steps:

  1. Open Could IDE, configure chain environment.
  2. Deployed Code contract before opening.
  3. Compiling a contract, the contract to generate bytecode and ABI interface descriptions . ( Key point : the production and the chain contract exactly the same ABI interface specification )
  4. Click on the top right corner ABI interface has been deployed contract button.
    Ants block chain BaaS platform application development guide (C): from a simple contract start

  5. Enter deployed before the contract name or contract the Identity .
    Ants block chain BaaS platform application development guide (C): from a simple contract start

  6. Once connected, you can see the method of contract, and may continue to call:
    Ants block chain BaaS platform application development guide (C): from a simple contract start

When connected to a deployed contracts, Cloud IDE interfaces are not going to do any checking, developers should ensure that the resulting contract ABI and deployed entirely, or it may result in a call to fail.
Note: Once the project file inside the contract code logic changes, you can not continue to link the instances deployed before, otherwise they will call error. If the code is updated, the proposed deployment of a new instance for debugging.

summary

Cloud IDE is a very convenient contract management, debugging and deployment tools. Developers need to pay more familiar with and understand the use of each function, which is the follow-up development very helpful.

Guess you like

Origin blog.51cto.com/14321927/2418709