> For the complete documentation index, see [llms.txt](https://stargateprotocol.gitbook.io/stargate/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://stargateprotocol.gitbook.io/stargate/developers/cross-chain-swap-fee.md).

# Cross Chain Swap Fee

Use `quoteLayerZeroFee()` to get the fee required to call `swap()`. The fee ensures the cross chain message is paid for.

```java
// Router.sol method to get the value for swap()
function quoteLayerZeroFee(
    uint16 _dstChainId,
    uint8 _functionType,
    bytes calldata _toAddress,
    bytes calldata _transferAndCallPayload,
    Router.lzTxObj memory _lzTxParams
) external view override returns (uint256, uint256)
```

For the `uint8 _functionType` argument use `1` for `swap()`s.  Here is an [explanation](/stargate/developers/function-types.md) of the [other function types](/stargate/developers/function-types.md).

Estimate the fee for the message cost of the `swap()` using offchain code like this:<br>

```javascript
let quoteData = await router.quoteLayerZeroFee(
    dstChainId,                 // destination chainId
    functionType,               // function type: see Bridge.sol for all types
    toAddress,                  // destination of tokens
    "0x",                       // payload, using abi.encode()
    ({
        dstGasForCall: 0,       // extra gas, if calling smart contract,
        dstNativeAmount: 0,     // amount of dust dropped in destination wallet 
        dstNativeAddr: taskArgs.dstNativeAddr // destination wallet for dust
    })
)
```

`quoteLayerZeroFee()` estimates the message fee and returns an amount of wei in source gas token. Use this as the `{ value: xxxx }` passed to the actual `swap()` method when you perform the swap.

Note:  quoteLayerZeroFee() returns a 2-value tuple:

```javascript
// the message fee is the first value in the tuple.
let feeWei = quoteData[0]
```

Use `feeWei` to call swap():

```javascript
// ethersjs example: call swap() with the feeWei value from quoteLayerZeroFee
// execute a Stargate swap on the Router.sol contract
await router.swap(
    dstChainId,
    srcPoolId,
    dstPoolId,
    payable(refundAddress),
    qty,
    qtyMin,
    { dstGasForCall: 0, dstNativeAmount: 0, dstNativeAddr: "0x" },   // lzTxObj
    toAddress, 
    "0x", // no payload
    { value: feeWei }  // <------ feeWei from quoteData[0] from quoteLayerZeroFee()   
)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://stargateprotocol.gitbook.io/stargate/developers/cross-chain-swap-fee.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
