合约应用主要介绍编写应用相关内容合约部署使用方式参看3.4 智能合约开发
4.1 合约范例1:电子存证合约
代码样例参看:contractsdk/go/example/eleccert.go
4.1.1 电子存证合约简介
电子存证应用主要是通过区块链解决的存证中的信任问题,而存证合约只需做简单的读写操作即可
4.1.2 电子存证合约具备的读写操作
- 通过invoke方法,put存证到区块链
- 通过query方法,get存证
4.1.3调用json文件示例
invoke
./xchain-cli native invoke -a ‘下面json中args字段的内容’ —method save -H localhost:37101 eleccert
{
"module_name": "native", // native or wasm
"contract_name": "eleccert", // contract name
"method_name": "save", // invoke or query
"args": {
"owner": "aaa", // user name
"filehash": "存证文件的hash值",
"timestamp": "存证的timestamp"
}
}
query
./xchain-cli native query -a ‘args内容’ —method query -H localhost:37101 eleccert
{
"module_name": "native", // native or wasm
"contract_name": "eleccert", // contract name
"method_name": "query", // invoke or query
"args": {
"owner": "aaa", // user name
"filehash": "文件hash值"
}
}
\\ output
{
"filehash": "文件hash值",
"timestamp": "文件存入timestamp"
}
4.2 合约范例2:数字资产交易
代码样例参看:contractsdk/go/example/erc721.go
4.2.1 ERC721简介
ERC721是数字资产合约,交易的商品是非同质性商品其中,每一份资产,也就是token_id都是独一无二的类似收藏品交易
4.2.2 ERC721具备哪些功能
- 通过initialize方法,向交易池注入自己的token_id
- 注意token_id必须是全局唯一
- 通过invoke方法,执行不同的交易功能
- transfer: userA将自己的某个收藏品token_id转给userB
- approve: userA将自己的某个收藏品token_id的售卖权限授予userB
- transferFrom: userB替userA将赋予权限的收藏品token_id卖给userC
- approveAll: userA将自己的所有收藏品token_id的售卖权限授予userB
- 通过query方法,执行不同的查询功能
- balanceOf: userA的所有收藏品的数量
- totalSupply: 交易池中所有的收藏品的数量
- approvalOf: userA授权给userB的收藏品的数量
4.2.3调用json文件示例
initialize
./xchain-cli wasm invoke -a ‘下面json中args字段的内容’ —method initialize -H localhost:37101 erc721
{
"module_name": "native", // native或wasm
"contract_name": "erc721", // contract name
"method_name": "initialize", // initialize or query or invoke
"args": {
"from": "dudu", // userName
"supply": "1,2" // token_ids
}
}
invoke
./xchain-cli native invoke -a ‘args内容’ —method invoke -H localhost:37101 erc721
{
"module_name": "native", // native或wasm
"contract_name": "erc721", // contract name
"method_name": "invoke", // initialize or query or invoke
"args": {
"action": "transfer", // action name
"from": "dudu", // usera
"to": "chengcheng", // userb
"token_id": "1" // token_ids
}
}
{
"module_name": "native", // native或wasm
"contract_name": "erc721", // contract name
"method_name": "invoke", // initialize or query or invoke
"args": {
"action": "transferFrom", // action name
"from": "dudu", // userA
"caller": "chengcheng", // userB
"to": "miaomiao", // userC
"token_id": "1" // token_ids
}
}
{
"module_name": "native", // native或wasm
"contract_name": "erc721", // contract name
"method_name": "invoke", // initialize or query or invoke
"args": {
"action": "approve", // action name
"from": "dudu", // userA
"to": "chengcheng", // userB
"token_id": "1" // token_ids
}
}
query
./xchain-cli native query -a ‘args内容’ —method query -H localhost:37101 erc721
{
"module_name": "native", // native或wasm
"contract_name": "erc721", // contract name
"method_name": "query", // initialize or query or invoke
"args": {
"action": "balanceOf", // action name
"from": "dudu" // userA
}
}
{
"module_name": "native", // native或wasm
"contract_name": "erc721", // contract name
"method_name": "query", // initialize or query or invoke
"args": {
"action": "totalSupply" // action name
}
}
{
"module_name": "native", // native或wasm
"contract_name": "erc721", // contract name
"method_name": "query", // initialize or query or invoke
"args": {
"action": "approvalOf", // action name
"from": "dudu", // userA
"to": "chengcheng" // userB
}
}
4.3 智能合约sdk
systemAPI
PutObject
输入:k,v
输出:true/false
GetObject
输入:k
输出:v
DeleteObject
输入:k,v
输出:true/false