Home指南API 参考手册
指南API 参考手册社区Discord博客FAQ漏洞赏金公告中心English(英文版)Log In
指南

TRC-20 合约交互

本文以 Shasta 测试网中的 USDT 合约为示例,分别展示如何通过 Node HTTP API、TronWeb 和 Wallet-cli 与其 TRC-20 接口进行交互操作。

name

调用 TRC-20 合约的 name() 函数获取通证的名称。

HTTP API :

// Node HTTP API: /wallet/triggerconstantcontract
// Description: Trigger the constant of the smart contract, the transaction is off the blockchain

curl -X POST  https://api.shasta.trongrid.io/wallet/triggerconstantcontract -d '{
"contract_address":"TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs",
"function_selector":"name()",
"owner_address":"TPnBjYQEMo4Yd4866KCzXdi4a169KGd63n",
"visible":true
}'

TronWeb 示例:

const { TronWeb } = require('tronweb');

const tronWeb = new TronWeb({
  fullHost: 'https://api.shasta.trongrid.io',
  headers: { 'TRON-PRO-API-KEY': 'your trongrid api key' },
  privateKey: 'your private key'
});

async function triggerSmartContract() {
    const trc20ContractAddress = "TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs";//contract address
    const abi = [...]; // Please replace it with the token's abi.
    try {
        let instance = await tronWeb.contract(abi, trc20ContractAddress);
        //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 instance.name().call();
        console.log('result: ', result);
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}

Wallet-cli 示例:

TriggerConstantContract TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs name() # false

**用法:**TriggerConstantContract [ownerAddress] [contractAddress] [method] [args] [isHex]
参数说明:

  • ownerAddress: 调用者地址
  • contractAdress:TRC20合约地址
  • method: 合约函数
  • args:函数参数,如果没有参数用#占位
  • isHex: 命令参数的地址是否是16进制格式

symbol

调用 TRC-20 合约的 symbol函数获取代币的符号。

HTTP API:

demo: 查询USDT通证的符号
/wallet/triggerconstantcontract

curl -X POST  https://api.shasta.trongrid.io/wallet/triggerconstantcontract -d '{
"contract_address":"TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs",
"function_selector":"symbol()",
"owner_address":"TPnBjYQEMo4Yd4866KCzXdi4a169KGd63n",
"visible":true
}'

TronWeb 示例:

const { TronWeb } = require('tronweb');

const tronWeb = new TronWeb({
  fullHost: 'https://api.shasta.trongrid.io',
  headers: { 'TRON-PRO-API-KEY': 'your trongrid api key' },
  privateKey: 'your private key'
});

async function triggerSmartContract() {
    const trc20ContractAddress = "TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs";//contract address
    const abi = [...]; // Please replace it with the token's abi.
    try {
        let instance = await tronWeb.contract(abi, trc20ContractAddress);
        //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 instance.symbol().call();
        console.log('result: ', result);
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}

Wallet-cli 示例:

TriggerConstantContract TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs symbol() # false

**用法:**TriggerConstantContract [ownerAddress] [contractAddress] [method] [args] [isHex]
参数说明:

  • ownerAddress: 调用者地址
  • contractAdress:TRC20合约地址
  • method: 合约函数
  • args:函数参数,如果没有参数用#占位
  • isHex: 命令参数的地址是否是16进制格式

decimals

调用TRC-20合约的decimals函数获取代币的精度。

HTTP API::

demo: 查询USDT通证的精度

curl -X POST  https://api.shasta.trongrid.io/wallet/triggerconstantcontract -d '{
"contract_address":"TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs",
"function_selector":"decimals()",
"owner_address":"TPnBjYQEMo4Yd4866KCzXdi4a169KGd63n",
"visible":true
}'

TronWeb 示例:

const { TronWeb } = require('tronweb');

const tronWeb = new TronWeb({
  fullHost: 'https://api.shasta.trongrid.io',
  headers: { 'TRON-PRO-API-KEY': 'your trongrid api key' },
  privateKey: 'your private key'
});

async function triggerSmartContract() {
    const trc20ContractAddress = "TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs";//contract address
    const abi = [...]; // Please replace it with the token's abi.
    try {
        let instance = await tronWeb.contract(abi, trc20ContractAddress);
        //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 instance.decimals().call();
        console.log('result: ', result);
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}

Wallet-cli 示例:

TriggerConstantContract TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs decimals() # false

**用法:**TriggerConstantContract [ownerAddress] [contractAddress] [method] [args] [isHex]
参数说明:

  • ownerAddress: 调用者地址
  • contractAdress:TRC20合约地址
  • method: 合约函数
  • args:函数参数,如果没有参数用#占位
  • isHex: 命令参数的地址是否是16进制格式

totalSupply

调用TRC20合约的totalSupply函数获取代币的总供应量。

HTTP API :

demo:查询USDT通证的发行总量

curl -X POST  https://api.shasta.trongrid.io/wallet/triggerconstantcontract -d '{
"contract_address":"TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs",
"function_selector":"totalSupply()",
"owner_address":"TPnBjYQEMo4Yd4866KCzXdi4a169KGd63n",
"visible":true
}'

TronWeb 示例:

const { TronWeb } = require('tronweb');

const tronWeb = new TronWeb({
  fullHost: 'https://api.shasta.trongrid.io',
  headers: { 'TRON-PRO-API-KEY': 'your trongrid api key' },
  privateKey: 'your private key'
});

async function triggerSmartContract() {
    const trc20ContractAddress = "TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs";//contract address
    const abi = [...]; // Please replace it with the token's abi.
    try {
        let instance = await tronWeb.contract(abi, trc20ContractAddress);
        //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 instance.totalSupply().call();
        console.log('result: ', result);
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}

Wallet-cli 示例:

TriggerConstantContract TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs totalSupply() # false

**用法:**TriggerConstantContract [ownerAddress] [contractAddress] [method] [args] [isHex]
参数说明:

  • ownerAddress: 调用者地址
  • contractAdress:TRC20合约地址
  • method: 合约函数
  • args:函数参数,如果没有参数用#占位
  • isHex: 命令参数的地址是否是16进制格式

balanceOf

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

HTTP API :

demo: 查询某个地址拥有的USDT通证数量

curl -X POST  https://api.shasta.trongrid.io/wallet/triggerconstantcontract -d '{
"contract_address":"TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs",
"function_selector":"balanceOf(address)",
"parameter":"000000000000000000000041977C20977F412C2A1AA4EF3D49FEE5EC4C31CDFB",
"owner_address":"TPnBjYQEMo4Yd4866KCzXdi4a169KGd63n",
"visible":true
}'

TronWeb 示例:

const { TronWeb } = require('tronweb');

const tronWeb = new TronWeb({
  fullHost: 'https://api.shasta.trongrid.io',
  headers: { 'TRON-PRO-API-KEY': 'your trongrid api key' },
  privateKey: 'your private key'
});

async function triggerSmartContract() {
    const trc20ContractAddress = "TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs";//contract address
    const abi = [...]; // Please replace it with the token's abi.
    try 
    {
        let instance = await tronWeb.contract(abi, trc20ContractAddress);
        //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 address = "TM2TmqauSEiRf16CyFgzHV2BVxBejY9iyR";
     	let result = await instance.balanceOf(address).call();
        console.log('result: ', result);
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}

Wallet-cli示例:

TriggerConstantContract TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs balanceOf(address) "TM2TmqauSEiRf16CyFgzHV2BVxBejY9iyR" false

**用法:**TriggerConstantContract [ownerAddress] [contractAddress] [method] [args] [isHex]
参数说明:

  • ownerAddress: 调用者地址
  • contractAdress:TRC20合约地址
  • method: 合约函数
  • args:函数参数,如果没有参数用#占位
  • isHex: 命令参数的地址是否是16进制格式

transfer

调用TRC20合约的transfer函数进行代币转账。

HTTP API :

demo: 向某个账号地址转账USDT通证资产
wallet/triggersmartcontract

curl -X POST  https://api.shasta.trongrid.io/wallet/triggersmartcontract -d '{
"contract_address":"TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs",
"function_selector":"transfer(address,uint256)",
"parameter":"00000000000000000000004115208EF33A926919ED270E2FA61367B2DA3753DA0000000000000000000000000000000000000000000000000000000000000032",
"fee_limit":100000000,
"call_value":0,
"owner_address":"TPnBjYQEMo4Yd4866KCzXdi4a169KGd63n",
"visible":true
}'

parameter参数是对transfer(address,uint256)中的address和uint256进行编码,具体参考参数编码和解码文档
提示:调用完该HTTP API,还需要调用签名,和广播的API。

Tronweb示例:

const { TronWeb } = require('tronweb');

const tronWeb = new TronWeb({
  fullHost: 'https://api.shasta.trongrid.io',
  headers: { 'TRON-PRO-API-KEY': 'your trongrid api key' },
  privateKey: 'your private key'
});

async function triggerSmartContract() {
    const trc20ContractAddress = "TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs";//contract address
    const abi = [...]; // Please replace it with the token's abi.
    try {
        let instance = await tronWeb.contract(abi, trc20ContractAddress);
        //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.
        let result = await instance.transfer(
                    "TWbcHNCYzqAGbrQteKnseKJdxfzBHyTfuh", // to address
                    1000000   // amount
                ).send({
                              feeLimit:100_000_000,
                              callValue:0,
                              shouldPollResponse:true
                          });
        console.log('result: ', result);
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}

Wallet-cli示例:

TriggerContract TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs transfer(address,uint256) "TWbcHNCYzqAGbrQteKnseKJdxfzBHyTfuh",1000000 false 100000000 0 0 #

用法:
TriggerContract [ownerAddress] [contractAddress] [method] [args] [isHex] [fee_limit] [value] [token_value] [token_id]
参数说明:

  • ownerAddress: 调用者地址
  • contractAdress:TRC20合约地址
  • method: 合约函数
  • args:函数参数,如果没有参数用#占位
  • isHex: 命令参数的地址是否是16进制格式
  • fee_limit: 本次调用合约最大的trx消耗,单位是sun
  • value: 调用合约的同时向合约转账TRX 数量。
  • token_value: 调用合约的同时向合约转账TRC10代币数量。
  • token_id:调用合约的同时向合约转账TRC10代币ID。

交易确认:
根据getTransactionInfoById 查询转账TRC-20是否成功。

approve

调用TRC-20合约的approve函数给一个地址授权一部分额度供其转账。

HTTP API :

demo:向某个
wallet/triggersmartcontract

curl -X POST  https://api.shasta.trongrid.io/wallet/triggersmartcontract -d '{
"contract_address":"TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs",
"function_selector":"approve(address,uint256)",
"parameter":"0000000000000000000000410FB357921DFB0E32CBC9D1B30F09AAD13017F2CD0000000000000000000000000000000000000000000000000000000000000064",
"fee_limit":100000000,
"call_value":0,
"owner_address":"TPnBjYQEMo4Yd4866KCzXdi4a169KGd63n",
"visible":true
}'

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

Tronweb示例:

const { TronWeb } = require('tronweb');

const tronWeb = new TronWeb({
  fullHost: 'https://api.shasta.trongrid.io',
  headers: { 'TRON-PRO-API-KEY': 'your trongrid api key' },
  privateKey: 'your private key'
});

async function triggerSmartContract() {
    const trc20ContractAddress = "TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs";//contract address
    const abi = [...]; // Please replace it with the token's abi
    
    //User A allows user B to use 10USDT of A: A calls approve (B,10)
                 
    try {
        let instance = await tronWeb.contract(abi, trc20ContractAddress);
        //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.
        let result = await instance.approve(
                    "TWbcHNCYzqAGbrQteKnseKJdxfzBHyTfuh", // address _spender
                    10000000   // amount
                ).send({
                              feeLimit:100_000_000,
                              callValue:0,
                              shouldPollResponse:true
                          });
        console.log('result: ', result);
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}

Wallet-cli示例:

TriggerContract TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs  approve(address,uint256) "TWbcHNCYzqAGbrQteKnseKJdxfzBHyTfuh",10000000 false 100000000 0 0 #

用法:
TriggerContract [ownerAddress] [contractAddress] [method] [args] [isHex] [fee_limit] [value] [token_value] [token_id]
参数说明:

  • ownerAddress: 调用者地址
  • contractAdress:TRC20合约地址
  • method: 合约函数
  • args:函数参数,如果没有参数用#占位
  • isHex: 命令参数的地址是否是16进制格式
  • fee_limit: 本次调用合约最大的trx消耗,单位是sun
  • value: 调用合约的同时向合约转账TRX额度
  • token_value: 调用合约的同时向合约转账TRC10代币额度
  • token_id:调用合约的同时向合约转账TRC10代币ID

交易确认 完成上述步骤后,请通过 getTransactionInfoById 接口查询并确认该 TRC-20 转账是否成功。

transferFrom

被授权(approve函数)的地址调用TRC20合约的transferFrom函数从授权账户中使用代币。

HTTP API :

curl -X POST  https://api.shasta.trongrid.io/wallet/triggersmartcontract -d '{
"contract_address":"TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs",
"function_selector":"transferFrom(address,address,uint256)",
"parameter":"00000000000000000000004109669733965A37BA3582E70CCC5302F8D254675D0000000000000000000000410FB357921DFB0E32CBC9D1B30F09AAD13017F2CD0000000000000000000000000000000000000000000000000000000000000032",
"fee_limit":100000000,
"call_value":0,
"owner_address":"TPnBjYQEMo4Yd4866KCzXdi4a169KGd63n",
"visible":true
}'

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

Tronweb示例:

const { TronWeb } = require('tronweb');

const tronWeb = new TronWeb({
  fullHost: 'https://api.shasta.trongrid.io',
  headers: { 'TRON-PRO-API-KEY': 'your trongrid api key' },
  privateKey: 'your private key'
});

async function triggerSmartContract() {
    const trc20ContractAddress = "TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs";//contract address
    const abi = [...]; // Please replace it with the token's abi
    
    // Address B transfers 10 USDT from address A to C: B calls transferFrom (A, C, 10)
                 
    try {
        let instance = await tronWeb.contract(abi, trc20ContractAddress);
        //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.
        let result = await instance.transferFrom(
                    "TApuyuazZnGgxvbNbaGcrUijEFn1oidsAH", //address _from
                    "TBQDyqoJ2ZJHTRDsrGQasyqBm4nUVLbWee", //address _to
                    10000000   // amount
                ).send({
                              feeLimit:100_000_000,
                              callValue:0,
                              shouldPollResponse:true
                          });
        console.log('result: ', result);
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}

Wallet-cli示例:

TriggerContract TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs  transferFrom(address,address,uint256) "TApuyuazZnGgxvbNbaGcrUijEFn1oidsAH","TBQDyqoJ2ZJHTRDsrGQasyqBm4nUVLbWee",10000000, false 100000000 0 0 #

用法:
TriggerContract [ownerAddress] [contractAddress] [method] [args] [isHex] [fee_limit] [value] [token_value] [token_id]
参数说明:

  • ownerAddress: 调用者地址
  • contractAdress:TRC20合约地址
  • method: 合约函数
  • args:函数参数,如果没有参数用#占位
  • isHex: 命令参数的地址是否是16进制格式
  • fee_limit: 本次调用合约最大的trx消耗,单位是sun
  • value: 调用合约的同时向合约转账TRX额度
  • token_value: 调用合约的同时向合约转账TRC10代币额度
  • token_id:调用合约的同时向合约转账TRC10代币ID

交易确认 完成上述步骤后,请通过 getTransactionInfoById 接口查询并确认该 TRC-20 转账是否成功。

allowance

被授权(approve函数)的地址调用TRC20合约的allowance函数查询授权账户中自己当前的可用额度。

HTTP API :

/wallet/triggerconstantcontract

curl -X POST  https://api.shasta.trongrid.io/wallet/triggerconstantcontract -d '{
"contract_address":"TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs",
"function_selector":"allowance(address,address)",
"parameter":"00000000000000000000004109669733965A37BA3582E70CCC5302F8D254675D000000000000000000000041A245B99ECB47B18C6A90ED1D51100C5A9F0641A7",
"owner_address":"TPnBjYQEMo4Yd4866KCzXdi4a169KGd63n",
"visible":true
}'

Tronweb示例:

const { TronWeb } = require('tronweb');

const tronWeb = new TronWeb({
  fullHost: 'https://api.shasta.trongrid.io',
  headers: { 'TRON-PRO-API-KEY': 'your trongrid api key' },
  privateKey: 'your private key'
});

async function triggerSmartContract() {
    const trc20ContractAddress = "TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs";//contract address
    const abi = [...]; // Please replace it with the token's abi
    
    //Query the USDT balance that Account A can use for Account B: Account B calls allowance (A, B)
                 
    try {
        let instance = await tronWeb.contract(abi, trc20ContractAddress);
        // 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 instance.allowance(
                    "TApuyuazZnGgxvbNbaGcrUijEFn1oidsAH", //address _owner
                    "TBQDyqoJ2ZJHTRDsrGQasyqBm4nUVLbWee", //address _spender
                ).call();
        console.log('result: ', result);
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}

Wallet-cli示例:

TriggerConstantContract TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs allowance(address,address) "TApuyuazZnGgxvbNbaGcrUijEFn1oidsAH","TBQDyqoJ2ZJHTRDsrGQasyqBm4nUVLbWee" false

用法:
TriggerContract [ownerAddress] [contractAddress] [method] [args] [isHex]
参数说明:

  • ownerAddress: 调用者地址
  • contractAdress:TRC20合约地址
  • method: 合约函数
  • args:函数参数,如果没有参数用#占位
  • isHex: 命令参数的地址是否是16进制格式