Sizing a TRON Swap’s Energy Limit
A swap caller should set its Energy limit above the simulation estimate; for example, a 150,000-Energy estimate with a 20% buffer calls for a 180,000-Energy budget. The estimate is a snapshot of a particular call against a particular contract state, not a promise that the mined transaction will use exactly that amount. For an integrator, the job is to reproduce the intended call, budget for plausible changes, and avoid signing stale transactions.
- Simulate the exact swap call, including its route and arguments.
- Convert the Energy budget into fee_limit using the current network price.
- Refresh the estimate when inputs, state, or the route changes.
- Check both the caller’s available resources and the contract’s Energy sharing rules.
Estimate the call you intend to send
Use the exact router contract, method, token path, input amount, recipient, and other arguments that will go into the transaction. A quote for one route does not estimate another route: a router may call different pools, and each contract path can perform different work. If your app chooses between routes, simulate the selected route after that choice.
Before signing, simulate the call through a node’s wallet/triggerconstantcontract endpoint. Send the caller address, contract address, function selector, and ABI-encoded parameters; include any call value if the method uses it. The response’s energy_used is useful for most contracts. The TRON API Reference also describes wallet/estimateenergy, which returns energy_required and may help with some unusual contracts, but a node may not have that endpoint enabled. Check the result for success before using either estimate.
A simulation creates no on-chain transaction and consumes no account resources, but it still reflects the node’s view of contract state at that moment. That matters when a pool is active: another swap can change reserves between your simulation and execution. An estimate of TRON Energy costs is useful for budgeting the call, but it cannot lock a route’s state or guarantee the eventual execution cost.
Turn the estimate into a caller budget
Pick a buffer based on how much the call can vary. For a stable, well-tested route, a 10–20% margin may be a reasonable starting point; for a route with uncertain pool calls or rapidly changing state, measure live calls and choose a larger margin from observed variation. These percentages are implementation examples, not protocol rules. A huge blanket margin can hide a poor estimate and raise the maximum the caller authorizes.
For a simple calculation, suppose simulation returns 150,000 Energy and you choose a 20% margin. Set the caller budget to 180,000 Energy. Then convert that budget to fee_limit: multiply by the current getEnergyFee value in sun per Energy. At 100 sun per Energy, for example, 180,000 Energy gives a fee_limit of 18,000,000 sun, or 18 TRX. The TRON Developer Hub documents both the conversion and the chain parameter; query the live value instead of hard-coding it.
fee_limit is denominated in sun and caps the Energy the caller can cover, whether from staked Energy or TRX burned for a shortfall. It is not a fixed fee or a promise that the whole amount will be charged. The TRON Energy budget can be partly met by the contract deployer if the contract is configured to share Energy, so check that contract’s settings before assuming the caller must fund the full execution. A higher fee_limit does not itself make the transaction more likely to be included.
Refresh estimates when the call can change
Re-simulate when the selected route, token amount, recipient, or encoded parameters change. Also refresh shortly before signing if the pool state may have moved since the quote. A router call can execute successfully with a different Energy use than its earlier simulation because contract state changed; the swap’s minimum-output or slippage checks govern the token result, while the Energy budget governs whether execution can finish.
Dynamic Energy can also affect the amount. TRON’s Dynamic Energy Model can add a contract-specific factor to base Energy, and that factor can change over time. If the same call’s Energy use moves more than your buffer allows, inspect the contract’s Energy factor and compare several recent simulations rather than raising the limit blindly. Query getEnergyFee again when converting to sun, since a change in price affects the required fee_limit even if the Energy estimate stays the same.
Diagnose a failed swap from its receipt
If a mined call fails with OUT_OF_ENERGY, the caller-side budget or the combined caller and deployer contribution was insufficient for execution to finish. Retrieve the solidified transaction receipt and inspect its result and consumed resources. Compare the failed call’s inputs and route with the simulation request; if they match, increase the buffer based on the measured gap or investigate a state-dependent branch that the simulation did not capture.
Also verify the units and the cap. fee_limit is in sun, so passing an Energy count directly is a unit error. The network’s maximum fee_limit can change; read getMaxFeeLimit rather than assuming a value. If the required budget approaches that ceiling, the better fix is usually to simplify the route or split work where the contract design permits it, rather than allowing an unbounded caller exposure.
For each swap, the implementation flow is:
- Choose the route and build the exact contract call.
- Simulate it; confirm success and record the returned Energy estimate.
- Add a margin justified by observed variation, then convert with the current Energy price.
- Check the contract’s sharing settings, the caller’s available resources, and the current maximum.
- Sign promptly; if the route or parameters change, simulate again.