TRC-20合约交互

下面以Shasta测试网上的USDT通证为例,介绍如何同TRC-20合约进行交互。

以下工具可能会帮助您更好理解:
在tronscan上查看示例USDT通证
编码转换工具

说明:
由于合约中的一些方法是constant,这类方法在调用的时候,是可以通过triggerconstantcontract来进行的,这样就不用广播这笔交易,可以直接获得结果。
当然前提是你请求的节点是支持constant调用的(节点的配置项supportConstant需要设置为true)。

name

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

HTTP API :

demo: 查询USDT通证资产的名称
/wallet/triggerconstantcontract

curl -X POST  https://127.0.0.1:8090/wallet/triggerconstantcontract -d '{
"contract_address":"419E62BE7F4F103C36507CB2A753418791B1CDC182",
"function_selector":"name()",
"owner_address":"41977C20977F412C2A1AA4EF3D49FEE5EC4C31CDFB"
}'

返回结果:
{"result":{"result":true},"constant_result":["000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000045553445400000000000000000000000000000000000000000000000000000000"],"transaction":{"ret":[{}],"visible":false,"txID":"7e2e68a039ac4ffb250412c3a8eb556f25c0e5d2b823a0f50f69fb6560be26f7","raw_data":{"contract":[{"parameter":{"value":{"data":"06fdde03","owner_address":"41977c20977f412c2a1aa4ef3d49fee5ec4c31cdfb","contract_address":"419e62be7f4f103c36507cb2a753418791b1cdc182"},"type_url":"type.googleapis.com/protocol.TriggerSmartContract"},"type":"TriggerSmartContract"}],"ref_block_bytes":"616c","ref_block_hash":"5206423380cc41fd","expiration":1590388398000,"timestamp":1590388339236},"raw_data_hex":"0a02616c22085206423380cc41fd40b0afa4d4a42e5a6d081f12690a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412340a1541977c20977f412c2a1aa4ef3d49fee5ec4c31cdfb1215419e62be7f4f103c36507cb2a753418791b1cdc182220406fdde0370a4e4a0d4a42e"}}

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

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

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

tronweb 示例:

const TronWeb = require('tronweb')

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

async function triggerSmartContract() {
    const trc20ContractAddress = "TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK";//contract address

    try {
        let contract = await tronWeb.contract().at(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 contract.name().call();
        console.log('result: ', result);
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}

Wallet-cli示例:

TriggerConstantContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK name() # false

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

symbol

调用TRC20合约的symbol函数获取代币的符号。

HTTP API :

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

curl -X POST  https://127.0.0.1:8090/wallet/triggerconstantcontract -d '{
"contract_address":"419E62BE7F4F103C36507CB2A753418791B1CDC182",
"function_selector":"symbol()",
"owner_address":"41977C20977F412C2A1AA4EF3D49FEE5EC4C31CDFB"
}'

返回结果:
{"result":{"result":true},"constant_result":["000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000055553445424000000000000000000000000000000000000000000000000000000"],"transaction":{"ret":[{}],"visible":false,"txID":"cd149b5c5c726786601467240adb5b66ee78f8b001136bd4ea2d91e84261d10d","raw_data":{"contract":[{"parameter":{"value":{"data":"95d89b41","owner_address":"41977c20977f412c2a1aa4ef3d49fee5ec4c31cdfb","contract_address":"419e62be7f4f103c36507cb2a753418791b1cdc182"},"type_url":"type.googleapis.com/protocol.TriggerSmartContract"},"type":"TriggerSmartContract"}],"ref_block_bytes":"6273","ref_block_hash":"0ac4e00c0b259e6d","expiration":1590389253000,"timestamp":1590389195937},"raw_data_hex":"0a02627322080ac4e00c0b259e6d4088c7d8d4a42e5a6d081f12690a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412340a1541977c20977f412c2a1aa4ef3d49fee5ec4c31cdfb1215419e62be7f4f103c36507cb2a753418791b1cdc182220495d89b4170a189d5d4a42e"}}

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

Tronweb示例:

const TronWeb = require('tronweb')

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

async function triggerSmartContract() {
    const trc20ContractAddress = "TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK";//contract address

    try {
        let contract = await tronWeb.contract().at(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 contract.symbol().call();
        console.log('result: ', result);
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}

Wallet-cli示例:

TriggerConstantContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK symbol() # false

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

decimals

调用TRC20合约的decimals函数获取代币的精度。

HTTP API :

demo: 查询USDT通证的精度
/wallet/triggerconstantcontract
curl -X POST  https://127.0.0.1:8090/wallet/triggerconstantcontract -d '{
"contract_address":"419E62BE7F4F103C36507CB2A753418791B1CDC182",
"function_selector":"decimals()",
"owner_address":"41977C20977F412C2A1AA4EF3D49FEE5EC4C31CDFB"
}'

返回结果:
{"result":{"result":true},"constant_result":["0000000000000000000000000000000000000000000000000000000000000006"],"transaction":{"ret":[{}],"visible":false,"txID":"18ee58a895c7b9ca2fdb4956e19482e1c023424ee5d3c3b79841cbd07af46a68","raw_data":{"contract":[{"parameter":{"value":{"data":"313ce567","owner_address":"41977c20977f412c2a1aa4ef3d49fee5ec4c31cdfb","contract_address":"419e62be7f4f103c36507cb2a753418791b1cdc182"},"type_url":"type.googleapis.com/protocol.TriggerSmartContract"},"type":"TriggerSmartContract"}],"ref_block_bytes":"5a8e","ref_block_hash":"696524d781f4bb88","expiration":1590382704000,"timestamp":1590382644808},"raw_data_hex":"0a025a8e2208696524d781f4bb884080ebc8d1a42e5a6d081f12690a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412340a1541977c20977f412c2a1aa4ef3d49fee5ec4c31cdfb1215419e62be7f4f103c36507cb2a753418791b1cdc1822204313ce56770c89cc5d1a42e"}}

返回结果中constant_result就是USDT通证的精度,以hex string的格式进行返回,将它进行转换就是6。

Tronweb示例:

const TronWeb = require('tronweb')

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

async function triggerSmartContract() {
    const trc20ContractAddress = "TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK";//contract address

    try {
        let contract = await tronWeb.contract().at(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 contract.decimals().call();
        console.log('result: ', result);
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}

Wallet-cli示例:

TriggerConstantContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK decimals() # false

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

totalSupply

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

HTTP API :

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

curl -X POST  https://127.0.0.1:8090/wallet/triggerconstantcontract -d '{
"contract_address":"419E62BE7F4F103C36507CB2A753418791B1CDC182",
"function_selector":"totalSupply()",
"owner_address":"41977C20977F412C2A1AA4EF3D49FEE5EC4C31CDFB"
}'

返回结果:
{"result":{"result":true},"constant_result":["000000000000000000000000000000000000000000000000000000e8d4a51000"],"transaction":{"ret":[{}],"visible":false,"txID":"397d14fa396c2e3365391a09cf7c91a386a80a9490e55cd46eea2bc3bfe82ebe","raw_data":{"contract":[{"parameter":{"value":{"data":"18160ddd","owner_address":"41977c20977f412c2a1aa4ef3d49fee5ec4c31cdfb","contract_address":"419e62be7f4f103c36507cb2a753418791b1cdc182"},"type_url":"type.googleapis.com/protocol.TriggerSmartContract"},"type":"TriggerSmartContract"}],"ref_block_bytes":"5629","ref_block_hash":"2c34e5774dd06d40","expiration":1590379059000,"timestamp":1590379001737},"raw_data_hex":"0a02562922082c34e5774dd06d4040b8aeeacfa42e5a6d081f12690a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412340a1541977c20977f412c2a1aa4ef3d49fee5ec4c31cdfb1215419e62be7f4f103c36507cb2a753418791b1cdc182220418160ddd7089efe6cfa42e"}}

返回结果中constant_result就是USDT总供应量,以hex string的格式进行返回,将它进行转换就是1000000000000。

Tronweb示例:

const TronWeb = require('tronweb')

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

async function triggerSmartContract() {
    const trc20ContractAddress = "TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK";//contract address

    try {
        let contract = await tronWeb.contract().at(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 contract.totalSupply().call();
        console.log('result: ', result);
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}

Wallet-cli示例:

TriggerConstantContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK totalSupply() # false

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

balanceOf

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

HTTP API :

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

curl -X POST https://127.0.0.1:8090/wallet/triggerconstantcontract -d '{
"contract_address":"419E62BE7F4F103C36507CB2A753418791B1CDC182",
"function_selector":"balanceOf(address)",
"parameter":"000000000000000000000041977C20977F412C2A1AA4EF3D49FEE5EC4C31CDFB",
"owner_address":"41977C20977F412C2A1AA4EF3D49FEE5EC4C31CDFB"
}'
参数说明:
contract_address是合约的地址的hexstring格式
owner_address是触发合约方法的地址的hexstring格式
function_selector是要触发的合约方法
parameter是合约方法要传入的参数,本例中应该传入的是address。由于波场的地址结构是地址前缀"41"+20字节地址,传地址参数的时候要求是32字节的,所以前面用"0"进行补齐。

返回结果:
{"result":{"result":true},"constant_result":["0000000000000000000000000000000000000000000000000000000430e1b700"],"transaction":{"ret":[{}],"visible":false,"txID":"7f47212aed2fdab232195feece54fc302bde2ce379e92ffd0d0e95206ce7a3bb","raw_data":{"contract":[{"parameter":{"value":{"data":"70a08231000000000000000000000041977c20977f412c2a1aa4ef3d49fee5ec4c31cdfb","owner_address":"41977c20977f412c2a1aa4ef3d49fee5ec4c31cdfb","contract_address":"419e62be7f4f103c36507cb2a753418791b1cdc182"},"type_url":"type.googleapis.com/protocol.TriggerSmartContract"},"type":"TriggerSmartContract"}],"ref_block_bytes":"5ab1","ref_block_hash":"f24f075df912f43e","expiration":1590382815000,"timestamp":1590382762536},"raw_data_hex":"0a025ab12208f24f075df912f43e4098cecfd1a42e5a8e01081f1289010a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412540a1541977c20977f412c2a1aa4ef3d49fee5ec4c31cdfb1215419e62be7f4f103c36507cb2a753418791b1cdc182222470a08231000000000000000000000041977c20977f412c2a1aa4ef3d49fee5ec4c31cdfb70a8b4ccd1a42e"}}

返回结果中constant_result就是地址41977c20977f412c2a1aa4ef3d49fee5ec4c31cdfb拥有的USDT通证数量,以hex string的格式进行返回,将它进行转换就是17999968000

Tronweb示例:

const TronWeb = require('tronweb')

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

async function triggerSmartContract() {
    const trc20ContractAddress = "TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK";//contract address
    var address = "TM2TmqauSEiRf16CyFgzHV2BVxBe...";

    try {
        let contract = await tronWeb.contract().at(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 contract.balanceOf(address).call();
        console.log('result: ', result);
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}

Wallet-cli示例:

TriggerConstantContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK 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://127.0.0.1:8090/wallet/triggersmartcontract -d '{
"contract_address":"419E62BE7F4F103C36507CB2A753418791B1CDC182",
"function_selector":"transfer(address,uint256)",
"parameter":"00000000000000000000004115208EF33A926919ED270E2FA61367B2DA3753DA0000000000000000000000000000000000000000000000000000000000000032",
"fee_limit":100000000,
"call_value":0,
"owner_address":"41977C20977F412C2A1AA4EF3D49FEE5EC4C31CDFB"
}'

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

Tronweb示例:

const TronWeb = require('tronweb')

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

async function triggerSmartContract() {
    const trc20ContractAddress = "TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK";//contract address
    var address = "TM2TmqauSEiRf16CyFgzHV2BVxBe...";

    try {
        let contract = await tronWeb.contract().at(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 contract.transfer(
            "TVDGp...", //address _to
            1000000   //amount
        ).send({
            feeLimit: 1000000
        }).then(output => {console.log('- Output:', output, '\n');});
        console.log('result: ', result);
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}

Wallet-cli示例:

TriggerContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK  transfer(address,uint256) "TBQDyqoJ2ZJHTRDsrGQasyqBm4nUVLbWee",100 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额度。本例不需要发送TRX资产,故设置为0.
token_value: 调用合约的同时向合约转账TRC10代币额度。本例不需要发送TRC10资产,故设置为0。
token_id:调用合约的同时向合约转账TRC10代币ID。本例不需要发送TRC10资产,故用参数#进行占位。

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

approve

调用TRC20合约的approve函数授权代币使用权给其他地址

HTTP API :

demo:向某个
wallet/triggersmartcontract

curl -X POST https://127.0.0.1:8090/wallet/triggersmartcontract -d '{
"contract_address":"419E62BE7F4F103C36507CB2A753418791B1CDC182",
"function_selector":"approve(address,uint256)",
"parameter":"0000000000000000000000410FB357921DFB0E32CBC9D1B30F09AAD13017F2CD0000000000000000000000000000000000000000000000000000000000000064",
"fee_limit":100000000,
"call_value":0,
"owner_address":"41977C20977F412C2A1AA4EF3D49FEE5EC4C31CDFB"
}'

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

Tronweb示例:

const TronWeb = require('tronweb')

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

async function triggerSmartContract() {
    //User A allows user B to use 10USDT of A: A calls approve (B,10)
    const trc20ContractAddress = "TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK";//contract address

    try {
        let contract = await tronWeb.contract().at(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.
        await contract.approve(
            "TA1g2WQiXbU...", //address _spender
            10000000 //amount
        ).send({
            feeLimit: 100000000
        }).then(output => {console.log('- Output:', output, '\n');});
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}

Wallet-cli示例:

TriggerContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK  approve(address,uint256) "TBQDyqoJ2ZJHTRDsrGQasyqBm4nUVLbWee",100 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,如果不需要转账TRC10用#占位

transferFrom

调用TRC20合约的transferFrom函数从他们账户中转账代币,需要配合approve方法使用。

HTTP API :

wallet/triggersmartcontract
demo: curl -X POST https://127.0.0.1:8090/wallet/triggersmartcontract -d '{
"contract_address":"419E62BE7F4F103C36507CB2A753418791B1CDC182",
"function_selector":"transferFrom(address,address,uint256)",
"parameter":"00000000000000000000004109669733965A37BA3582E70CCC5302F8D254675D0000000000000000000000410FB357921DFB0E32CBC9D1B30F09AAD13017F2CD0000000000000000000000000000000000000000000000000000000000000032",
"fee_limit":100000000,
"call_value":0,
"owner_address":"41977C20977F412C2A1AA4EF3D49FEE5EC4C31CDFB"
}'

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

Tronweb示例:

const TronWeb = require('tronweb')

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

async function triggerSmartContract() {
    // Address B transfers 10 USDT from address A to C: B calls transferFrom (A, C, 10)
    const trc20ContractAddress = "TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK";//contract address

    try {
        let contract = await tronWeb.contract().at(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.
        await contract.transferFrom(
            "TM2TmqauSEiRf16CyFgzHV2BVxBej...", //address _from
            "TVDGpn4hCSzJ5nkHPLetk8KQBtwaT...", //address _to
            100000 //amount
        ).send({
            feeLimit: 10000000
        }).then(output => {console.log('- Output:', output, '\n');});
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}

Wallet-cli示例:

TriggerContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK  transferFrom(address,address,uint256) "TApuyuazZnGgxvbNbaGcrUijEFn1oidsAH","TBQDyqoJ2ZJHTRDsrGQasyqBm4nUVLbWee",50 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,如果不需要转账TRC10用#占位

allowance

调用TRC20合约的allowance函数查询可供第三方转账的查询账户的通证余额。

HTTP API :

/wallet/triggerconstantcontract
demo: curl -X POST https://127.0.0.1:8090/wallet/triggersmartcontract -d '{
"contract_address":"419E62BE7F4F103C36507CB2A753418791B1CDC182",
"function_selector":"allowance(address,address)",
"parameter":"00000000000000000000004109669733965A37BA3582E70CCC5302F8D254675D000000000000000000000041A245B99ECB47B18C6A90ED1D51100C5A9F0641A7",
"owner_address":"41977C20977F412C2A1AA4EF3D49FEE5EC4C31CDFB"
}'

Tronweb示例:

const TronWeb = require('tronweb')

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

async function triggerSmartContract() {
    //Query the USDT balance that Account A can use for Account B: Account B calls allowance (A, B)
    const trc20ContractAddress = "TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK";//contract address

    try {
        let contract = await tronWeb.contract().at(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.
        const value = await contract.allowance(
            "TM2TmqauSEiRf16CyFgzHV2BVxBejY9...", //address _owner
            "TA1g2WQiXbU5GnYBTJ5Cp22dvSjT3ug..." //address _spender
        ).call();
        console.log('- Output:', value, '\n');
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}

Wallet-cli示例:

TriggerConstantContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK allowance(address,address) "TApuyuazZnGgxvbNbaGcrUijEFn1oidsAH","TQmDzierQxEFJm1dT5YXnTXqVAfdN9HtXj" false

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