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
3
4
5
6
7
8
9
{
    "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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{
    "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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
    "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
    }
}