OApp and OFT come packaged with methods you can implement or call directly in order to receive a quote for how much native gas your message will cost to send to the destination chain.
Both the
OApp and OFT implementations for estimating fees require some knowledge of how _options work. We recommend reviewing the OApp or OFT Quickstart and Message Options guides first to better understand _options usage.OApp Fee Estimation
To estimate how much gas a message will cost to be sent and received, you will need to implement aquote function to return an estimate from the Endpoint contract to use as a recommended msg.value.
_quote can be returned in either the native gas token or in LzToken, supporting both payment methods.
In general, this quote will be accurate as the same function is used by the Endpoint when pricing an _lzSend call:
Remember that to send a message, a
msg.sender will be paying the source chain, the selected DVNs to deliver the message, and the destination chain to execute the transaction.OFT Fee Estimation
To estimate how much gas an OFT transfer will cost, call thequoteSend function to return an estimate from the Endpoint contract.
Best Practices for Fee Estimation
Testing and Validation
- Test quotes thoroughly on each target chain before deployment
- Compare estimated vs actual costs in your testing environment
- Account for gas price volatility by including appropriate buffers
Implementation Considerations
- Cache quotes when possible to reduce onchain calls
- Handle quote failures gracefully with proper error handling
- Provide clear fee breakdowns to users in your interface
- Update quotes regularly for accurate pricing in volatile markets
User Experience
- Display total costs upfront before transaction confirmation
- Show fee components separately (source gas, DVN fees, destination gas)
- Offer gas limit recommendations based on operation complexity
- Provide fee optimization suggestions for frequent operations