Handle Errors
Handle errors, manage fees, and dispose of sensitive data in gas-free Tron wallets.
This guide covers how to handle gas-free transfer errors and handle native TRX transaction errors, plus best practices for fee management and memory cleanup.
Handle Gas-Free Transfer Errors
Gas-free transfers can fail for reasons including exceeded fee limits or insufficient token balances. Wrap calls to account.transfer() in try/catch blocks:
try {
const result = await account.transfer({
token: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', // USDT
recipient: 'TLyqzVGLV1srkB7dToTAEqgDSfPtXRJZYH',
amount: 1000000
})
console.log('Transfer successful:', result.hash)
console.log('Fee paid:', result.fee, 'token units')
} catch (error) {
console.error('Transfer failed:', error.message)
if (error.message.includes('exceeds the transfer max fee')) {
console.log('Transfer cancelled: fee too high')
} else if (error.message.toLowerCase().includes('insufficient')) {
console.log('Please add more TRC20 tokens to your wallet')
}
}Handle Native TRX Transaction Errors
Native TRX sends via account.sendTransaction() can fail for standard reasons:
try {
const result = await account.sendTransaction({
to: 'TLyqzVGLV1srkB7dToTAEqgDSfPtXRJZYH',
value: 1000000
})
console.log('Transaction hash:', result.hash)
} catch (error) {
if (error.message.toLowerCase().includes('insufficient')) {
console.error('Not enough TRX to complete transaction')
} else {
console.error('Transaction failed:', error.message)
}
}Best Practices
Manage Fee Limits
Set transferMaxFee when creating the wallet or per-transfer to prevent gas-free transfers from exceeding a maximum cost. You can retrieve current network rates using wallet.getFeeRates():
const feeRates = await wallet.getFeeRates()
console.log('Normal fee rate:', feeRates.normal, 'sun')
console.log('Fast fee rate:', feeRates.fast, 'sun')Dispose of Sensitive Data
Call dispose() on accounts and wallet managers to clear private keys and sensitive data from memory when they are no longer needed:
account.dispose()
wallet.dispose()Always call dispose() in a finally block or cleanup handler to ensure sensitive data is cleared even if an error occurs.