TRON
On the FMZ Quant Trading Platform, you can use the exchange.IO() function to call gRPC methods and smart contracts on the TRON blockchain, enabling you to write related strategy code.
Web3 Exchange Object Configuration
Configuration method is similar to Ethereum exchange object configuration, ChainType needs to be selected as TRON. RPC Address defaults to: grpc.trongrid.io:50051, which is the official TRON node address.
Register ABI
TRC20 contract ABI is registered by default, with an underlying mechanism that automatically retrieves contract ABI based on contract address. Manual ABI registration is typically not required, only needed when certain contract ABIs cannot be automatically retrieved.
The method for registering ABI is the same as Ethereum, for example:
javascript
// USDT contract address: TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t
let abi = `[{"constant":true,"inputs":[{"name":"who","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]`
// Register balanceOf method
exchange.IO("abi", "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t", abi)
Calling TRON RPC Methods
Use the exchange.IO() function to call TRON RPC methods. For methods requiring signatures, the underlying layer has automatically encapsulated the signing operations. The following lists commonly used methods. For other methods, please refer to the official TRON project documentation.
- GetAccountjavascriptexchange.IO("api", "tron", "GetAccount", "TKCG...") // "TKCG..." is the TRON wallet address, this function returns the account information for the "TKCG..." address.
- GetAccountResourcejavascriptexchange.IO("api", "tron", "GetAccountResource", "TKCG...") // Get resources for the specified wallet address, including energy and bandwidth.
- GetContractABIjavascriptexchange.IO("api", "tron", "GetContractABI", "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t") // Get USDT TRC20 contract ABI
- GetAssetIssueByNamejavascriptexchange.IO("api", "tron", "GetAssetIssueByName", "TRX") // Get token asset information by token name
- GetNowBlockjavascriptexchange.IO("api", "tron", "GetNowBlock") // Get current block information
- GetBlockByNumjavascriptexchange.IO("api", "tron", "GetBlockByNum", 70624300)
- GetTransactionByIDjavascriptexchange.IO("api", "tron", "GetTransactionByID", "05a8fae2cd1cbf36b61d12e219588d25b4826436f055f93388a96e620ec3f3f2") // Get Transaction by transaction hash
- TRC20ContractBalancejavascriptexchange.IO("api", "tron", "TRC20ContractBalance", "TKCG...", "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t") // Get wallet USDT balance, note that the returned data is not precision-processed, for example, returned data: ```6890251``` means ```6.890251 USDT```.
- TriggerConstantContractjavascriptfunction main() { let ret = exchange.IO("api", "tron", "TriggerConstantContract", "", "TSUUVjysXV8YqHytSNjfkNXnnB49QDvZpx", "token0()", "") // Call the token0() method of the smart contract, TriggerConstantContract is used to call read-only methods let data = exchange.IO("decode", "address", Encode("raw", "raw", "hex", ret["constant_result"][0])) // Decode data return data // data: 0x891cdb91d149f23b1a45d9c5ca78a88d0cb44c18 }
- TRC20Calljavascriptfunction main() { let ret = exchange.IO("api", "tron", "TRC20Call", "", "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t", "0x06fdde03", true, 0) // Use TRC20Call to call read-only method 0x06fdde03 let data = Encode("raw", "raw", "hex", ret.constant_result[0]) return exchange.IO("api", "tron", "ParseTRC20StringProperty", data) // Tether USD }
- Transferjavascriptexchange.IO("api", "tron", "Transfer", "TWTbn...", "TKCG...", 1000000) // Use Transfer method to transfer TRX, from "TWTbn..." to "TKCG...", 1000000 equals 1 TRX.
Encoding/Decoding
When the exchange object is set to Web3 and TRON is selected, encoding/decoding operations remain consistent with Ethereum's Web3 exchange object.
- encode:javascriptlet ret = exchange.IO("encode", "address", "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t") Log(ret) // ret: 000000000000000000000000a614f803b6fd780986a42c78ec9c7f77e6ded13c , encoding the TRON address of USDT token.
- decode:javascriptlet data = "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000a5465746865722055534400000000000000000000000000000000000000000000" let ret = exchange.IO("decode", "string", data) Log(ret) // ret: Tether USD , similar to the functionality of ParseTRC20StringProperty
- encodePacked:javascriptlet ret = exchange.IO("encodePacked", "address", "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t") Log(ret) // ret: a614f803b6fd780986a42c78ec9c7f77e6ded13c
Support for Private Key Switching
The switching method is consistent with Web3 Ethereum exchange objects.
Calling Smart Contract Methods
Calling smart contract methods on TRON is basically the same as on Ethereum. Here is a specific example demonstrating how to:
-
Call smart contract methods, calling multiple contract methods in a single request:
javascriptfunction main() { let usdtAddress = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t" // token usdt contract address let data1 = exchange.IO("encode", usdtAddress, "name") // call function: name let data2 = exchange.IO("encode", usdtAddress, "decimals") // call function: decimals let data3 = exchange.IO("encode", usdtAddress, "balanceOf", "TKCG...") // call function: balanceOf var data = [] data.push([usdtAddress, data1]) data.push([usdtAddress, data2]) data.push([usdtAddress, data3]) exchange.IO("abi", "TGXuuKAb4bnrn137u39EKbYzKNXvdCes98", `[{"inputs":[{"components":[{"internalType":"address","name":"target","type":"address"},{"internalType":"bytes","name":"callData","type":"bytes"}],"internalType":"struct TronMulticall.Call[]","name":"calls","type":"tuple[]"}],"name":"aggregate","outputs":[{"internalType":"uint256","name":"blockNumber","type":"uint256"},{"internalType":"bytes[]","name":"returnData","type":"bytes[]"}],"stateMutability":"view","type":"function"}]`) let ret = exchange.IO("api", "TGXuuKAb4bnrn137u39EKbYzKNXvdCes98", "aggregate", data) Log("name:", exchange.IO("decode", "string", ret["returnData"][0])) Log("decimals:", exchange.IO("decode", "uint8", ret["returnData"][1])) Log("balanceOf:", exchange.IO("decode", "uint256", ret["returnData"][2])) }Output:
logInfo balanceOf: 6890251 Info decimals: 6 Info name: Tether USD
Other Function Calls
-
Get wallet address configured for exchange object
Same usage as Ethereum. -
Switch blockchain RPC node
Same usage as Ethereum. -
Calculate hash value
javascriptlet algo = "sign" // algo: algorithm/method to use let inputFormat = "string" // inputFormat: input format let outputFormat = "hex" // outputFormat: output format let data = "txHash" // txHash: specific hash value let signature = exchange.IO("hash", algo, inputFormat, outputFormat, data) // returns signature dataWhen the algo parameter is set to
"sign", it is used for calculating signatures; when set to other algorithm parameters (e.g., "sha256"), the function is equivalent to theEncode()function.