Transferring TRC-10 in smart contracts
Transfer TRC-10 tokens from inside a Solidity contract using the trcToken type and the transferToken built-in.
Prerequisites
Since the Odyssey 3.2 release, smart contracts can transfer TRC-10 tokens directly through TRON-specific Solidity built-ins. Unlike TRC-20 — which is a contract-level standard implemented in user-deployed contracts — TRC-10 is a protocol-level token, so transferring it from inside a contract uses dedicated language extensions rather than calling another contract's transfer function.
A TRON-specific compiler is requiredThe
trcTokentype,transferToken,msg.tokenid, andtokenBalanceare TRON extensions to Solidity — they cannot be compiled by a generic Solidity compiler such assolcor Remix. Compile such contracts with the TRON solc (tron-solc) bundled with TronBox or TronIDE.
Example: TRC-10 transfer in a contract
pragma solidity 0.5.10;
contract transferTokenContract {
constructor() payable public {}
function() payable external {}
function transferTokenTest(address payable toAddress, uint256 tokenValue, trcToken id) payable public {
toAddress.transferToken(tokenValue, id);
}
function msgTokenValueAndTokenIdTest() public payable returns (trcToken, uint256) {
trcToken id = msg.tokenid;
uint256 value = msg.tokenvalue;
return (id, value);
}
function getTokenBalanceTest(address accountAddress) payable public returns (uint256) {
trcToken id = 1000001;
return accountAddress.tokenBalance(id);
}
}TRON-specific TRC-10 built-ins
trcToken type
trcToken typeOdyssey 3.2 introduced the trcToken type, which represents a TRC-10 token ID in transfer operations.
trcToken id = 1000001;address.transferToken(value, tokenId)
address.transferToken(value, tokenId)Send a TRC-10 token from a contract to an address. The recipient must be payable.
address.transferToken(uint256 tokenValue, trcToken tokenId)address.tokenBalance(tokenId)
address.tokenBalance(tokenId)Query the TRC-10 token balance of an address. Returns the balance as a uint256.
address.tokenBalance(trcToken) returns (uint256 tokenAmount)msg.tokenvalue and msg.tokenid
msg.tokenvalue and msg.tokenidWithin a function called with attached TRC-10 tokens, msg.tokenvalue exposes the amount and msg.tokenid exposes the token ID.
Related resources
- TRC-10 — TRC-10 standard overview
- TRC-20 — fungible token standard for new projects (recommended)
- Writing & compiling Solidity — Solidity basics on TRON
Updated 7 days ago