2. 数字资产交易

代码样例参看:contractsdk/go/example/erc721.go

或使用超级链XuperOS,其已发布丰富的合约模板,涵盖溯源、存证、积分、去中心化等多行业模板。 点击了解

2.1. ERC721简介

ERC721是数字资产合约,交易的商品是非同质性商品。其中,每一份资产,也就是token_id都是独一无二的类似收藏品交易。

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

    • pproveAll: userA将自己的所有收藏品token_id的售卖权限授予userB

  • 通过query方法,执行不同的查询功能

    • balanceOf: userA的所有收藏品的数量

    • totalSupply: 交易池中所有的收藏品的数量

    • approvalOf: userA授权给userB的收藏品的数量

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. }