VRC-721 合约交互

1 代币名称查询

调用VRC721合约的name()函数获取代币名称。

HTTP:

curl -X POST https://vtest.infragrid.v.network/wallet/triggersmartcontract -d '{
           "contract_address":"468c921721ababd66313981e1ad49b19c4e799f24d",
           "function_selector":"name()",
           "owner_address":"461fafb1e96dfe4f609e2259bfaf8c77b60c535b93"
 }'

返回结果:

{
    "result": {
        "result": true
    },
    "constant_result": [
        "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000f596f757220546f6b656e204e616d650000000000000000000000000000000000"
    ],
    "transaction": {
        "ret": [
            {}
        ],
        "visible": false,
        "txID": "418a8fe76cb888e06c68cbe6a7c52b3b6f9c009877e16a2a87183868c5cbb1b0",
        "raw_data": {
            "contract": [
                {
                    "parameter": {
                        "value": {
                            "data": "06fdde03",
                            "owner_address": "461fafb1e96dfe4f609e2259bfaf8c77b60c535b93",
                            "contract_address": "468c921721ababd66313981e1ad49b19c4e799f24d"
                        },
                        "type_url": "type.googleapis.com/protocol.TriggerSmartContract"
                    },
                    "type": "TriggerSmartContract"
                }
            ],
            "ref_block_bytes": "2d6d",
            "ref_block_hash": "08e5816e980173a0",
            "expiration": 1615822557000,
            "fee_limit": 400000000,
            "timestamp": 1615822500321
        },
        "raw_data_hex": "0a022d6d220808e5816e980173a040c89e9eb4832f5a6d081f12690a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412340a15411fafb1e96dfe4f609e2259bfaf8c77b60c535b931215418c921721ababd66313981e1ad49b19c4e799f24d220406fdde0370e1e39ab4832f90018088debe01"
    }
}

返回结果中constant_result就是代币的名称,以hex string的格式进行返回。

此处为大家详细解释一下,由于返回值为string类型,虚拟机中string被认为是一个变长类型,它的数据包含长度和实际值两部分,因此上面的返回值在解析是需要拆成三部分(每32字节进行一次拆分):
0000000000000000000000000000000000000000000000000000000000000020 代表指针
000000000000000000000000000000000000000000000000000000000000000 F代表长度
596F757220546F6b656e204e616d650000000000000000000000000000000000 代表实际值

解析为语义就是该string数据是从第32字节起(16进制的20代表十进制32)开始读取,长度为15个字节,实际数据是"596F757220546F6b656e204e616d65", 再将其转换为字符串形式就是"Your Token Name"。

2 代币符号查询

curl -X POST  https://vtest.infragrid.v.network/wallet/triggersmartcontract -d '{
           "contract_address":"468c921721ababd66313981e1ad49b19c4e799f24d",
           "function_selector":"symbol()",
           "owner_address":"461fafb1e96dfe4f609e2259bfaf8c77b60c535b93"
  }'

返回结果中constant_result就是YTN,以hex string的格式进行返回。
解析过程和上面调用的name()方法类似,请参考name()的详细解释说明。

3 余额查询

curl -X POST https://vtest.infragrid.v.network/wallet/triggersmartcontract  -d '{
    "contract_address":"469E62BE7F4F103C36507CB2A753418791B1CDC182",
		"function_selector":"balanceOf(address)",
		"parameter":"000000000000000000000041977C20977F412C2A1AA4EF3D49FEE5EC4C31CDFB",
		"owner_address":"46977C20977F412C2A1AA4EF3D49FEE5EC4C31CDFB"
 }'

参数说明:
contract_address是合约的地址的hexstring格式
owner_address是触发合约方法的地址的hexstring格式
function_selector是要触发的合约方法
Parameter是合约方法要传入的参数,本例中应该传入的是address。由于Vision的地址结构是地址前缀"46"+20字节
地址 ,传地址参数的时候要求是32字节的,所以前面用"0"进行补齐。

4 NFT转账

curl -X POST https://vtest.infragrid.v.network/wallet/triggersmartcontract  -d '{
"contract_address":"469E62BE7F4F103C36507CB2A753418791B1CDC182",
"fee_limit": 400000000,
"function_selector":"transferFrom(address,address,uint256)",
"parameter":"0000000000000000000000001fafb1e96dfe4f609e2259bfaf8c77b60c535b9300000000000000000000000021ae4e504e68a75521221163faae1acd01deb3160000000000000000000000000000000000000000000000000000000000000001",
"call_value":0,
"owner_address":"469E62BE7F4F103C36507CB2A753418791B1CDC182"
 }'

parameter参数是对transfer(address,uint256)中的address和uint256进行编码,具体参考参数编码和解码文档

📘

提示

调用完该HTTP API,还需要调用签名和广播的API。

5 授权NFT的控制权给其他地址

curl -X POST https://vtest.infragrid.v.network/wallet/triggersmartcontract  -d '{
"contract_address":"469E62BE7F4F103C36507CB2A753418791B1CDC182",
"fee_limit": 400000000,
"function_selector":"approve(address,uint256)",
"parameter":"000000000000000000000000173ebb4f23dbdc69f31065d7f8d2dacab32e004f0000000000000000000000000000000000000000000000000000000000000001",
"call_value":0,
"owner_address":"469E62BE7F4F103C36507CB2A753418791B1CDC182"
 }'

parameter参数是对approve(address,uint256)中的address和uint256进行编码,具体参考参数编码和解码文档

📘

提示

调用完该HTTP API,还需要调用签名和广播的API。

6 查询一个地址下的某个VRC-721合约的所有NFT信息

  • 首先调用VRC721合约的balanceOf(address _owner)方法查询一个地址持有NFT个数
  • 然后调用tokenOfOwnerByIndex(address _owner, uint256 _index) 遍历所有token_id
  • 调用tokenURI(uint256 _tokenId) 查询每一个NFT的详细信息