TRC-721合约交互

1.代币名称查询

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

const TronWeb = require('tronweb')

const HttpProvider = TronWeb.providers.HttpProvider;
const fullNode = new HttpProvider("https://nile.trongrid.io");
const solidityNode = new HttpProvider("https://nile.trongrid.io");
const eventServer = new HttpProvider("https://nile.trongrid.io");
const privateKey = "your private key";
const tronWeb = new TronWeb(fullNode,solidityNode,eventServer,privateKey);

async function trc721_name() {
    const trc721ContractAddress = "TRio4FwnDvtYN2ogss6Qm7Hn2EaTLwWMNs";//contract address

    try {
        let contract = await tronWeb.contract().at(trc721ContractAddress);
        //Use call to execute a pure or view smart contract method.
        // These methods do not modify the blockchain, do not cost anything to execute and are also not broadcasted to the network.
        let trc721name = await contract.name().call();
        console.log('name: ', trc721name);
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}
trc721_name()

返回结果:

name:  TRC721TEST  

2.代币符号查询

调用TRC721合约的symbol()函数获取代币符号。

const TronWeb = require('tronweb')

const HttpProvider = TronWeb.providers.HttpProvider;
const fullNode = new HttpProvider("https://nile.trongrid.io");
const solidityNode = new HttpProvider("https://nile.trongrid.io");
const eventServer = new HttpProvider("https://nile.trongrid.io");
const privateKey = "your private key";
const tronWeb = new TronWeb(fullNode,solidityNode,eventServer,privateKey);

async function trc721_symbol() {
    const trc721ContractAddress = "TRio4FwnDvtYN2ogss6Qm7Hn2EaTLwWMNs";//contract address

    try {
        let contract = await tronWeb.contract().at(trc721ContractAddress);
        //Use call to execute a pure or view smart contract method.
        // These methods do not modify the blockchain, do not cost anything to execute and are also not broadcasted to the network.
        let trc721symbol = await contract.symbol().call();
        console.log('symbol: ', trc721symbol);
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}
trc721_symbol()

返回结果:

symbol:  TEST

3.代币余额查询

调用TRC721合约的balanceOf()函数获取指定账户的代币余额。

const TronWeb = require('tronweb')

const HttpProvider = TronWeb.providers.HttpProvider;
const fullNode = new HttpProvider("https://nile.trongrid.io");
const solidityNode = new HttpProvider("https://nile.trongrid.io");
const eventServer = new HttpProvider("https://nile.trongrid.io");
const privateKey = "your private key";
const tronWeb = new TronWeb(fullNode,solidityNode,eventServer,privateKey);

async function trc721_balanceOf() {
    const trc721ContractAddress = "TRio4FwnDvtYN2ogss6Qm7Hn2EaTLwWMNs";//contract address
    var address = "TA1g2WQiXbU5GnYBTJ5Cp22dvSjT3ug9uK";

    try {
        let contract = await tronWeb.contract().at(trc721ContractAddress);
        //Use call to execute a pure or view smart contract method.
        // These methods do not modify the blockchain, do not cost anything to execute and are also not broadcasted to the network.
        let result = await contract.balanceOf(address).call();
        console.log('balance: ', tronWeb.toDecimal(result));
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}
trc721_balanceOf()

返回结果:

balance:  1

4.NFT转账

调用TRC721合约的transferFrom()函数进行NFT转账

const TronWeb = require('tronweb')

const HttpProvider = TronWeb.providers.HttpProvider;
const fullNode = new HttpProvider("https://nile.trongrid.io");
const solidityNode = new HttpProvider("https://nile.trongrid.io");
const eventServer = new HttpProvider("https://nile.trongrid.io");
const privateKey = "your private key";
const tronWeb = new TronWeb(fullNode,solidityNode,eventServer,privateKey);

async function trc721_transferFrom() {
    const trc721ContractAddress = "TRio4FwnDvtYN2ogss6Qm7Hn2EaTLwWMNs";//contract address

    try {
        let contract = await tronWeb.contract().at(trc721ContractAddress);
        //Use send to execute a non-pure or modify smart contract method on a given smart contract that modify or change values on the blockchain.
        // These methods consume resources(bandwidth and energy) to perform as the changes need to be broadcasted out to the network.
        await contract.transferFrom(
            "TA1g2WQiXbU5GnYBTJ5Cp22dvSjT3ug9uK", //address _from
            "TM8vRhebJD7zeoBLWAnr9SrYrhWNrHjBgC", //address _to
            666 //uint256 tokenId
        ).send({
            feeLimit: 100000000
        }).then(output => {console.log('- transferFrom hash:', output, '\n');});
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}
trc721_transferFrom()

返回结果:

- transferFrom hash: 9f4d10713cb0406adb7c729013b941d35597afeeba56faf2a1bc7647fc0a92bd

注:上述结果为转账交易hash,可以通过Nile tronscan浏览器查询交易详情

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

调用TRC721合约的approve()函数授权NFT使用权给其他地址

const TronWeb = require('tronweb')

const HttpProvider = TronWeb.providers.HttpProvider;
const fullNode = new HttpProvider("https://nile.trongrid.io");
const solidityNode = new HttpProvider("https://nile.trongrid.io");
const eventServer = new HttpProvider("https://nile.trongrid.io");
const privateKey = "your private key";
const tronWeb = new TronWeb(fullNode,solidityNode,eventServer,privateKey);

async function trc721_approve() {
    const trc721ContractAddress = "TRio4FwnDvtYN2ogss6Qm7Hn2EaTLwWMNs";//contract address

    try {
        let contract = await tronWeb.contract().at(trc721ContractAddress);
        //Use send to execute a non-pure or modify smart contract method on a given smart contract that modify or change values on the blockchain.
        // These methods consume resources(bandwidth and energy) to perform as the changes need to be broadcasted out to the network.
        await contract.approve(
            "TA1g2WQiXbU5GnYBTJ5Cp22dvSjT3ug9uK", //address _spender
            666 //uint256 tokenId
        ).send({
            feeLimit: 100000000
        }).then(output => {console.log('- approve hash:', output, '\n');});
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}
trc721_approve()

返回结果:

- approve hash: d7cb1451ed962667f3e24323655dadd8c650ee80d171ea5e44ea97b97eaa3118 

注:上述结果为授权交易hash,可以通过Nile tronscan浏览器查询交易详情

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

(1).调用TRC721合约的balanceOf(address _owner)方法查询上述地址持有NFT个数

const TronWeb = require('tronweb')

const HttpProvider = TronWeb.providers.HttpProvider;
const fullNode = new HttpProvider("https://nile.trongrid.io");
const solidityNode = new HttpProvider("https://nile.trongrid.io");
const eventServer = new HttpProvider("https://nile.trongrid.io");
const privateKey = "your private key";
const tronWeb = new TronWeb(fullNode,solidityNode,eventServer,privateKey);

async function trc721_balanceOf() {
    const trc721ContractAddress = "TRio4FwnDvtYN2ogss6Qm7Hn2EaTLwWMNs";//contract address
    var address = "TM8vRhebJD7zeoBLWAnr9SrYrhWNrHjBgC";

    try {
        let contract = await tronWeb.contract().at(trc721ContractAddress);
        //Use call to execute a pure or view smart contract method.
        // These methods do not modify the blockchain, do not cost anything to execute and are also not broadcasted to the network.
        let result = await contract.balanceOf(address).call();
        console.log('result: ', tronWeb.toDecimal(result));
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}
trc721_balanceOf()

返回结果:

result:  2
注:返回结果说明上述查询地址持数量为2。

(2).调用TRC721合约的tokenOfOwnerByIndex(address _owner, uint256 _index) 方法遍历所有token_id

因上述地址持有的NFT数量为2,故查询index分别为0和1。

const TronWeb = require('tronweb')

const HttpProvider = TronWeb.providers.HttpProvider;
const fullNode = new HttpProvider("https://nile.trongrid.io");
const solidityNode = new HttpProvider("https://nile.trongrid.io");
const eventServer = new HttpProvider("https://nile.trongrid.io");
const privateKey = "your private key";
const tronWeb = new TronWeb(fullNode,solidityNode,eventServer,privateKey);

async function trc721_tokenOfOwnerByIndex(index) {
    const trc721ContractAddress = "TRio4FwnDvtYN2ogss6Qm7Hn2EaTLwWMNs";//contract address

    try {
        let contract = await tronWeb.contract().at(trc721ContractAddress);
        //Use call to execute a pure or view smart contract method.
        // These methods do not modify the blockchain, do not cost anything to execute and are also not broadcasted to the network.
        let token_id = await contract.tokenOfOwnerByIndex(
            "TM8vRhebJD7zeoBLWAnr9SrYrhWNrHjBgC", // address owner
            index //uint256 index
            ).call();
        console.log('token_id: ', tronWeb.toDecimal(token_id));
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}
trc721_tokenOfOwnerByIndex(0)
trc721_tokenOfOwnerByIndex(1)

返回结果:

token_id:  666
token_id:  555

(3).调用TRC721合约的tokenURI(uint256 _tokenId) 方法查询每一个NFT的详细信息

const TronWeb = require('tronweb')

const HttpProvider = TronWeb.providers.HttpProvider;
const fullNode = new HttpProvider("https://nile.trongrid.io");
const solidityNode = new HttpProvider("https://nile.trongrid.io");
const eventServer = new HttpProvider("https://nile.trongrid.io");
const privateKey = "your private key";
const tronWeb = new TronWeb(fullNode,solidityNode,eventServer,privateKey);

async function trc721_tokenURI(tokenid) {
    const trc721ContractAddress = "TRio4FwnDvtYN2ogss6Qm7Hn2EaTLwWMNs";//contract address

    try {
        let contract = await tronWeb.contract().at(trc721ContractAddress);
        //Use call to execute a pure or view smart contract method.
        // These methods do not modify the blockchain, do not cost anything to execute and are also not broadcasted to the network.
        let tokenURI = await contract.tokenOfOwnerByIndex(
            tokenid //uint256 tokenid
            ).call();
        console.log(tokenid + ' tokenURI:', tokenURI);
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}
trc721_tokenURI(666)
trc721_tokenURI(555)

返回结果:

666 tokenURI:  https://gateway.btfs.io/btfs/QmWq4cp588QD8tzrSxvPs2bGikDdKyA35BT3iysBcP1jFD
555 tokenURI: https://gateway.btfs.io/btfs/QmWq4cp588QD8tzrSxvPs2bGikDdKyA35BT3iysBcP1jFD