合约应用主要介绍编写应用相关内容合约部署使用方式参看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

  1. {
  2. "module_name": "native", // native or wasm
  3. "contract_name": "eleccert", // contract name
  4. "method_name": "save", // invoke or query
  5. "args": {
  6. "owner": "aaa", // user name
  7. "filehash": "存证文件的hash值",
  8. "timestamp": "存证的timestamp"
  9. }
  10. }

query

./xchain-cli native query -a ‘args内容’ —method query -H localhost:37101 eleccert

  1. {
  2. "module_name": "native", // native or wasm
  3. "contract_name": "eleccert", // contract name
  4. "method_name": "query", // invoke or query
  5. "args": {
  6. "owner": "aaa", // user name
  7. "filehash": "文件hash值"
  8. }
  9. }
  10. \\ output
  11. {
  12. "filehash": "文件hash值",
  13. "timestamp": "文件存入timestamp"
  14. }

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

  1. {
  2. "module_name": "native", // native或wasm
  3. "contract_name": "erc721", // contract name
  4. "method_name": "initialize", // initialize or query or invoke
  5. "args": {
  6. "from": "dudu", // userName
  7. "supply": "1,2" // token_ids
  8. }
  9. }

invoke

./xchain-cli native invoke -a ‘args内容’ —method invoke -H localhost:37101 erc721

  1. {
  2. "module_name": "native", // native或wasm
  3. "contract_name": "erc721", // contract name
  4. "method_name": "invoke", // initialize or query or invoke
  5. "args": {
  6. "action": "transfer", // action name
  7. "from": "dudu", // usera
  8. "to": "chengcheng", // userb
  9. "token_id": "1" // token_ids
  10. }
  11. }
  12. {
  13. "module_name": "native", // native或wasm
  14. "contract_name": "erc721", // contract name
  15. "method_name": "invoke", // initialize or query or invoke
  16. "args": {
  17. "action": "transferFrom", // action name
  18. "from": "dudu", // userA
  19. "caller": "chengcheng", // userB
  20. "to": "miaomiao", // userC
  21. "token_id": "1" // token_ids
  22. }
  23. }
  24. {
  25. "module_name": "native", // native或wasm
  26. "contract_name": "erc721", // contract name
  27. "method_name": "invoke", // initialize or query or invoke
  28. "args": {
  29. "action": "approve", // action name
  30. "from": "dudu", // userA
  31. "to": "chengcheng", // userB
  32. "token_id": "1" // token_ids
  33. }
  34. }

query

./xchain-cli native query -a ‘args内容’ —method query -H localhost:37101 erc721

  1. {
  2. "module_name": "native", // native或wasm
  3. "contract_name": "erc721", // contract name
  4. "method_name": "query", // initialize or query or invoke
  5. "args": {
  6. "action": "balanceOf", // action name
  7. "from": "dudu" // userA
  8. }
  9. }
  10. {
  11. "module_name": "native", // native或wasm
  12. "contract_name": "erc721", // contract name
  13. "method_name": "query", // initialize or query or invoke
  14. "args": {
  15. "action": "totalSupply" // action name
  16. }
  17. }
  18. {
  19. "module_name": "native", // native或wasm
  20. "contract_name": "erc721", // contract name
  21. "method_name": "query", // initialize or query or invoke
  22. "args": {
  23. "action": "approvalOf", // action name
  24. "from": "dudu", // userA
  25. "to": "chengcheng" // userB
  26. }
  27. }

4.3 智能合约sdk

systemAPI

  1. PutObject
  2. 输入:kv
  3. 输出:true/false
  4. GetObject
  5. 输入:k
  6. 输出:v
  7. DeleteObject
  8. 输入:kv
  9. 输出:true/false