TRC-20#
TRC?-20 is a technical standard used for smart contracts on the TRON blockchain for implementing tokens with the TRON Virtual Machine (TVM). It is fully compatible with ERC?-20.
TRC-20 standard contract template
https://github.com/zyumingfit/TRC20-Contract-Template
Protocol Interface#
TRC-20 contract standard#
TRC-20 is a set of contract standards for the issuance of token assets, contracts written in compliance with this standard are considered to be a TRC-20 contract. When wallets and exchanges are docking the assets of the TRC-20 contract, from this set of standards, you can know which functions and events the contract defines, so as to facilitate the docking.
Optional Items Token Name
string public name = "TRONEuropeRewardCoin";
Token Abbreviation
string public symbol = "TERC";
Token Precision(Decimals)
uint8 public decimals = 6;
Required Items
contract TRC20 {
function totalSupply() constant returns (uint theTotalSupply);
function balanceOf(address _owner) constant returns (uint balance);
function transfer(address _to, uint _value) returns (bool success);
function transferFrom(address _from, address _to, uint _value) returns (bool success);
function approve(address _spender, uint _value) returns (bool success);
function allowance(address _owner, address _spender) constant returns (uint remaining);
event Transfer(address indexed _from, address indexed _to, uint _value);
event Approval(address indexed _owner, address indexed _spender, uint _value);
}
totalSupply()
This function returns the total supply of the token.
balanceOf()
This function returns the token balance of the specific account.
transfer()
This function is used to transfer a number of tokens to a specific address.
approve()
This function is used to authorize the third party (like a DAPP smart contract) to transfer the token from the token owner’s account.
transferFrom()
This function is used to allow the third party to transfer the token from an owner account to a receiver account. The owner account must be approved to be called by the third party.
allowance()
This function is used to query the remaining amount of tokens the third party can transfer.
Event Functions When the token is successfully transferred, the contract will trigger a Transfer Event.
event Transfer(address indexed _from, address indexed _to, uint256 _value)
When approval() is successfully called, the contract will trigger an Approval Event.
event Approval(address indexed _owner, address indexed _spender, uint256 _value)
Contract Example#
https://github.com/zyumingfit/TRC20-Contract-Template
Issuing TRC-20 tokens tutorial#
1.Install TronLink Chrome plugin
Address: TronLink
2.Prepare an account for issue token
There are three ways to create an account, import an account, and link a hardware wallet. You need to ensure that there are more than 10 TRX in the account.
3.Prepare the TRC20 contract code
Trc20 contract template: code
Modify the Token.sol file to define the token name, token symbol, precision, and totalsupply
4.Deploy TRC20 contract
Deploy with tronscan: deployment tool
* Link wallet
-
Upload contract code
-
Compile the contract
Please select 0.5.10 version compiler
The following prompt appears, indicating successful compilation
- Deployment contract
Please note that you must choose the Token contract, because Token is the main contract
Click Confirm to deploy, the tronlink signature dialog box will pop up, click to accept to sign,when successful deployment, please get the contract address, and record the contract address.
5.Add tokens to Tronlink
On the asset management page, fill in the contract address obtained after successful deployment in the add token input box, the contract just deployed will pop up, click the switch button, and add the token to tronlink. After the addition is successful, the transfer can be carried out.
You can also search the contract homepage on tronscan
6.Verify TRC20 contracts
Verify with Tronacan: Validation tool
- Enter contract information including contract address,contract name, compiler version, License, optimization history and Runs.
Contract address is the address recorded while deploying the contract.
Contract name refers to the name of the main contract deployed. In the example above, the name is "Token".
Compiler version is 0.5.10
You may select None for License
Optimization history is Yes and Runs is 0 by default.
Click Upload contract file(s) for validation.
- Upload contract code
Check I am not a robot (Note: Google authentication is required for this step. Users in Mainland China may have to use VPN).
Click Verify and publish, you will be directed to the contract information page once validated successfully.
- Contract successfully validated
Contract information page will show successful validation.
7.Record TRC20 Token
Record with Tronacan: Record tool
- Select token type
Select the TRC20 token and click Yes.
- Enter TRC20 token information
Enter the basic information, contract information and social media information of the token. Fields with "*" are required information. The information you entered must match that of the TRC20 contract. Please note that record must be logged in with the deployer address.
Enter all information required for the TRC20 token, click Next.
- Confirm token information
Check if token information is right. Click I’m not a robot, and then click Submit (Note: Google authentication is required for this step. Users in Mainland China may have to use VPN).
You will see a popup dialog to confirm token issuance. Click Confirm and you will see another popup from Tronlink asking for your signature. Click Accept to sign the message.
- Token successfully recorded
TRC-20 Contract Interaction#
Take the USDT contract on Shasta test net as an example,Use Tronweb and wallet-cli to call the TRC-20 interface of the contract, respectively.
Some related links:
Find the USDT on Tronscan
Code conversion tool
We can use the triggersmartcontract
function to call constant functions in the contract to get the result directly without broadcasting.
Please set supportConstant = true
in your node config.
name#
Call the name function to get the name of the token.
HTTP API :
HTTP
/wallet/triggerconstantcontract
Description: Trigger the constant of the smart contract, the transaction is off the blockchain
demo: curl -X POST https://127.0.0.1:8090/wallet/triggersmartcontract -d '{
"contract_address":"419E62BE7F4F103C36507CB2A753418791B1CDC182",
"function_selector":"name()",
"owner_address":"41977C20977F412C2A1AA4EF3D49FEE5EC4C31CDFB"
}'
Tronweb Example:
JavaScript
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 Example:
wallet-cli command
TriggerConstantContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK name() # false Usage : TriggerConstantContract [ownerAddress] [contractAddress] [method] [args] [isHex] Parameter Description: ownerAddress: calller address contractAdress:TRC20 contract address method: contract function args:function parameters,If there is no parameter,use # placeholder isHex: whether the address of the command parameter is in hex format
symbol#
Call the symbol function to get the symbol of the token.
HTTP API :
HTTP
/wallet/triggerconstantcontract
Description: Trigger the constant of the smart contract, the transaction is off the blockchain
demo: curl -X POST https://127.0.0.1:8090/wallet/triggersmartcontract -d '{
"contract_address":"419E62BE7F4F103C36507CB2A753418791B1CDC182",
"function_selector":"symbol()",
"owner_address":"41977C20977F412C2A1AA4EF3D49FEE5EC4C31CDFB"
}'
Tronweb Example:
JavaScript
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 Example:
wallet-cli command
TriggerConstantContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK symbol() # false
Usage : TriggerConstantContract [ownerAddress] [contractAddress] [method] [args] [isHex]
Parameter Description:
ownerAddress: calller address
contractAdress:TRC20 contract address
method: contract function
args:function parameters,If there is no parameter,use # placeholder
isHex: whether the address of the command parameter is in hex format
decimals#
Call the decimals function to get the precision of the token.
HTTP API :
HTTP
/wallet/triggerconstantcontract
Description: Trigger the constant of the smart contract, the transaction is off the blockchain
demo: curl -X POST https://127.0.0.1:8090/wallet/triggersmartcontract -d '{
"contract_address":"419E62BE7F4F103C36507CB2A753418791B1CDC182",
"function_selector":"decimals()",
"owner_address":"41977C20977F412C2A1AA4EF3D49FEE5EC4C31CDFB"
}'
Tronweb Example:
JavaScript
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 Example: wallet-cli command
TriggerConstantContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK decimals() # false
Usage : TriggerConstantContract [ownerAddress] [contractAddress] [method] [args] [isHex]
Parameter Description:
ownerAddress: calller address
contractAdress:TRC20 contract address
method: contract function
args:function parameters,If there is no parameter,use # placeholder
isHex: whether the address of the command parameter is in hex format
totalSupply#
Call the totalSupply function to get the total supply of the token.
HTTP API :
HTTP
/wallet/triggerconstantcontract
Description: Trigger the constant of the smart contract, the transaction is off the blockchain
demo: curl -X POST https://127.0.0.1:8090/wallet/triggersmartcontract -d '{
"contract_address":"419E62BE7F4F103C36507CB2A753418791B1CDC182",
"function_selector":"totalSupply()",
"owner_address":"41977C20977F412C2A1AA4EF3D49FEE5EC4C31CDFB"
}'
Tronweb Example:
JavaScript
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:
wallet-cli command
TriggerConstantContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK totalSupply() # false
Usage : TriggerConstantContract [ownerAddress] [contractAddress] [method] [args] [isHex]
Parameter Description:
ownerAddress: calller address
contractAdress:TRC20 contract address
method: contract function
args:function parameters,If there is no parameter,use # placeholder
isHex: whether the address of the command parameter is in hex format
balanceOf#
Call the balanceOf function to get the token balance of the specified account.
HTTP API :
HTTP
/wallet/triggerconstantcontract
Description: Trigger the constant of the smart contract, the transaction is off the blockchain
demo: curl -X POST https://127.0.0.1:8090/wallet/triggersmartcontract -d '{
"contract_address":"419E62BE7F4F103C36507CB2A753418791B1CDC182",
"function_selector":"balanceOf(address)",
"parameter":"000000000000000000000041977C20977F412C2A1AA4EF3D49FEE5EC4C31CDFB",
"owner_address":"41977C20977F412C2A1AA4EF3D49FEE5EC4C31CDFB"
}'
Tronweb Example:
JavaScript
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 Example:
wallet-cli command
TriggerConstantContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK balanceOf(address) "TM2TmqauSEiRf16CyFgzHV2BVxBejY9iyR" false
Usage : TriggerConstantContract [ownerAddress] [contractAddress] [method] [args] [isHex]
Parameter Description:
ownerAddress: calller address
contractAdress:TRC20 contract address
method: contract function
args:function parameters,If there is no parameter,use # placeholder
isHex: whether the address of the command parameter is in hex format
transfer Call transfer function for token transfer
HTTP API :
HTTP
wallet/triggersmartcontract
Description: Trigger smart contract
demo: 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"
}'
The parameter is to encode address and uint256 in transfer (address,uint256), please refer to the parameter encoding and decoding document Note: After calling this HTTP API, you also need to call the signature and broadcast APIs.
Tronweb Example:
JavaScript
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 Example:
wallet-cli command
TriggerContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK transfer(address,uint256) "TBQDyqoJ2ZJHTRDsrGQasyqBm4nUVLbWee",100 false 100000000 0 0 #
Usage :
TriggerContract [ownerAddress] [contractAddress] [method] [args] [isHex] [fee_limit] [value] [token_value] [token_id]
Parameter Description:
ownerAddress: calller address
contractAdress:TRC20 contract address
method: contract function
args:function parameters,If there is no parameter,use # placeholder
isHex: whether the address of the command parameter is in hex format
fee_limit: the maximum trx consumption of this calling, the unit is sun
value: the amount of TRX that transfered to the contract while calling the contract, the unit is sun
token_value: the amount of TRC10 asset that transfered to the contract while calling the contract
token_id:the TRC10 asset ID that transfered to the contract while calling the contract
Transaction confirmation: Check whether the transfer of TRC20 was successful according to result of getTransactionInfoById.
approve Call the approve function to authorize token use rights to other addresses
HTTP API :
HTTP
wallet/triggersmartcontract
Description: Trigger smart contract
demo: 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"
}'
Note: After calling this HTTP API, you also need to call the signature and broadcast APIs.
Tronweb Example:
JavaScript
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 Example:
wallet-cli command
TriggerContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK approve(address,uint256) "TBQDyqoJ2ZJHTRDsrGQasyqBm4nUVLbWee",100 false 100000000 0 0 #
Usage :
TriggerContract [ownerAddress] [contractAddress] [method] [args] [isHex] [fee_limit] [value] [token_value] [token_id]
Parameter Description:
ownerAddress: calller address
contractAdress:TRC20 contract address
method: contract function
args:function parameters,If there is no parameter,use # placeholder
isHex: whether the address of the command parameter is in hex format
fee_limit: the maximum trx consumption of this calling, the unit is sun
value: the amount of TRX that transfered to the contract while calling the contract, the unit is sun
token_value: the amount of TRC10 asset that transfered to the contract while calling the contract
token_id:the TRC10 asset ID that transfered to the contract while calling the contract
Transaction confirmation: Check whether the transfer of TRC20 was successful according to result of getTransactionInfoById.
transferFrom Calling the transferFrom function to transfer tokens from other people's accounts, needs to be used in conjunction with the approve method.
HTTP API :
HTTP
wallet/triggersmartcontract
Description: Trigger smart contract
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"
}'
Note: After calling this HTTP API, you also need to call the signature and broadcast APIs.
Tronweb Example:
JavaScript
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 Example:
wallet-cli command
TriggerContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK transferFrom(address,address,uint256) "TApuyuazZnGgxvbNbaGcrUijEFn1oidsAH","TBQDyqoJ2ZJHTRDsrGQasyqBm4nUVLbWee",50 false 100000000 0 0 #
Usage :
TriggerContract [ownerAddress] [contractAddress] [method] [args] [isHex] [fee_limit] [value] [token_value] [token_id]
Parameter Description:
ownerAddress: calller address
contractAdress:TRC20 contract address
method: contract function
args:function parameters,If there is no parameter,use # placeholder
isHex: whether the address of the command parameter is in hex format
fee_limit: the maximum trx consumption of this calling, the unit is sun
value: the amount of TRX that transfered to the contract while calling the contract, the unit is sun
token_value: the amount of TRC10 asset that transfered to the contract while calling the contract
token_id:the TRC10 asset ID that transfered to the contract while calling the contract
Transaction confirmation: Check whether the transfer of TRC20 was successful according to result of getTransactionInfoById.
allowance Call the allowance function to query the token balance of the query account available for third-party transfers.
HTTP API :
HTTP
/wallet/triggerconstantcontract
Description: Trigger the constant of the smart contract, the transaction is off the blockchain
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 Example:
JavaScript
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 Example: wallet-cli command
TriggerConstantContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK allowance(address,address) "TApuyuazZnGgxvbNbaGcrUijEFn1oidsAH","TQmDzierQxEFJm1dT5YXnTXqVAfdN9HtXj" false
Usage : TriggerConstantContract [ownerAddress] [contractAddress] [method] [args] [isHex]
Parameter Description:
ownerAddress: calller address
contractAdress:TRC20 contract address
method: contract function
args:function parameters,If there is no parameter,use # placeholder
isHex: whether the address of the command parameter is in hex format
TRC-20 Event Listener#
Use Tronweb's watch method to listen to events emitted by the smart contract method. You can set a callback function to handle the event. When the event occurs, the callback function will be triggered. The following example listens to the transfer event of the USDT-TRC20 contract of the MainNet.
Tronweb Example:
JavaScript
const trc20ContractAddress = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"; //mainnet USDT contract
let contract = await tronWeb.contract().at(trc20ContractAddress);
//contract.[eventname].watch(callback) enventname is the name of the event of the contract
await contract && contract.Transfer().watch((err, event) => {
if(err)
return console.error('Error with "Message" event:', err);
console.group('New event received');
console.log('- Contract Address:', event.contract);
console.log('- Event Name:', event.name);
console.log('- Transaction:', event.transaction);
console.log('- Block number:', event.block);
console.log('- Result:', event.result, '\n');
console.groupEnd();
});
Get TRC-20 transaction history#
SUGGEST EDITS TRC-20 transaction history Get the transaction history for a specific TRC-20 in a specific account.
API documents references
Get Transaction history Get TRC-20 transaction info by account address Shell
curl --request GET \
--url 'https://api.trongrid.io/v1/accounts/TJmmqjb1DK9TTZbQXzRQ2AuA94z4gKAPFh/transactions/trc20?limit=100&contract_address=TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'
Parameters:
version:The latest version v1.
address: account address,in Base58 or Hex.
only_confirmed: true|false. if false, returns both confirmed & unconfirmed transactions; if no parameters, returns both confirmed & unconfirmed transactions. CAN NOT be used with only_unconfirmed.
only_unconfirmed: true|false. if false,returns both confirmed & unconfirmed transactions; if no parameters, returns both confirmed & unconfirmed transactions. CAN NOT be used with only_confirmed.
limit:transactions per page,default is 20, maximum is 200.
fingerprint:The fingerprint of the last transaction returned on the previous page
. When using this, other parameters and filters should remain unchanged.
contract_address:TRC20 contract address, Base58 or Hex.
//Example
//Get transactions related to TRC20 USDT on the address TJmmqjb1DK9TTZbQXzRQ2AuA94z4gKAPFh
curl --request GET \
--url ' https://api.trongrid.io/v1/accounts/TJmmqjb1DK9TTZbQXzRQ2AuA94z4gKAPFh/transactions/trc20?limit=20&contract_address=TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'