> ## Documentation Index
> Fetch the complete documentation index at: https://docs.layerzero.network/llms.txt
> Use this file to discover all available pages before exploring further.

# Estimating Gas Fees

> Configure Estimating Gas Fees for your LayerZero application. Set up DVNs, executors, and pathway settings for crosschain messaging. LayerZero enables...

Both [`OApp`](../oapp/overview) and [`OFT`](../oft/quickstart) 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.

<Tip>
  For a comprehensive overview of how LayerZero's transaction pricing works, see [Transaction Pricing Model](../../../concepts/protocol/transaction-pricing). This page focuses on the technical implementation of fee estimation.
</Tip>

<Info>
  Both the `OApp` and `OFT` implementations for estimating fees require some knowledge of how `_options` work. We recommend reviewing the [**OApp**](../oapp/overview) or [**OFT Quickstart**](../oft/quickstart) and [**Message Options**](/v2/developers/evm/configuration/options) guides first to better understand `_options` usage.
</Info>

[](../../../concepts/message-ordering#unordered-delivery)

## OApp Fee Estimation

To estimate how much gas a message will cost to be sent and received, you will need to implement a `quote` function to return an estimate from the Endpoint contract to use as a recommended `msg.value`.

```solidity wrap theme={null}
function quote(
    uint32 _dstEid, // destination endpoint id
    bytes memory payload, // message payload being sent
    bytes memory _options, // your message execution options
    bool memory _payInLzToken // boolean for which token to return fee in
) public view returns (uint256 nativeFee, uint256 zroFee) {
    return _quote(_dstEid, payload, _options, _payInLzToken);
}
```

The `_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:

```solidity wrap theme={null}
// How the _quote function works.
// This function is already defined in your OApp contract.
/// @dev the generic quote interface to interact with the LayerZero EndpointV2.quote()
function _quote(
    uint32 _dstEid,
    bytes memory _message,
    bytes memory _options,
    bool _payInLzToken
) internal view virtual returns (MessagingFee memory fee) {
    return
        endpoint.quote(
            MessagingParams(_dstEid, _getPeerOrRevert(_dstEid), _message, _options, _payInLzToken),
            address(this)
        );
}
```

<Tip>
  Make sure that the arguments passed into the `_quote` function identically match the parameters used in the `lzSend` function. If parameters mismatch, you may run into errors as your `msg.value` will not match the actual gas quote.
</Tip>

<br />

<Note>
  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.
</Note>

## OFT Fee Estimation

To estimate how much gas an OFT transfer will cost, call the `quoteSend` function to return an estimate from the Endpoint contract.

```solidity wrap theme={null}
// @dev Requests a nativeFee/lzTokenFee quote for sending the corresponding msg crosschain through the layerZero Endpoint
function quoteSend(
    SendParam calldata _sendParam, // send parameters struct
    bytes calldata _extraOptions, // extra message options
    bool _payInLzToken, // bool for payment in native gas or LzToken
    bytes calldata _composeMsg, // data for composed message
    bytes calldata _oftCmd // data for custom OFT behaviours
)
    public
    view
    virtual
    returns (
        MessagingFee memory msgFee, // fee struct for native or LzToken
        OFTLimit memory oftLimit,
        OFTReceipt memory oftReceipt,
        OFTFeeDetail[] memory oftFeeDetails // @dev unused in the default implementation, future proofs complex fees inside of an oft send
    )
{
    (oftLimit, oftReceipt) = quoteOFT(_sendParam);

    (bytes memory message, bytes memory options) = _buildMsgAndOptions(
        _sendParam,
        _extraOptions,
        _composeMsg,
        oftReceipt.amountCreditLD
    );

    msgFee = _quote(_sendParam.dstEid, message, options, _payInLzToken);
}
```

## 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
