# How to Swap

You will need the Stargate [router interface](/stargate/interfaces/evm-solidity-interfaces/istargaterouter.sol.md).

To perform a cross chain `swap()` in solidity, here is an example:

{% hint style="info" %}
For the native gas fee required for `swap()` you need to call `quoteLayerZero()` on the Router.sol contract to get the amount you should send as msg.value.&#x20;
{% endhint %}

Here is the swap() interface:

<pre><code>function swap(
<strong>  uint16 _dstChainId,
</strong>  uint256 _srcPoolId,
  uint256 _dstPoolId,
  address payable _refundAddress,
  uint256 _amountLD,
  uint256 _minAmountLD,       
  lzTxObj memory _lzTxParams,
  bytes calldata _to,
  bytes calldata _payload
) external payable override nonReentrant {
</code></pre>

First determine the swap fee, then call swap() to transfer the asset to the destination chain.

```javascript
// perform a Stargate swap() in a solidity smart contract function
// the msg.value is the "fee" that Stargate needs to pay for the cross chain message
IStargateRouter(routerAddress).swap{value:msg.value}(
    10006,                           // send to Fuji (use LayerZero chainId)
    1,                               // source pool id
    1,                               // dest pool id                 
    msg.sender,                      // refund adddress. extra gas (if any) is returned to this address
    _amountLD,                       // quantity to swap in LD, (local decimals)
    _minAmountLD,                    // the min qty you would accept in LD (local decimals)
    IStargateRouter.lzTxObj(0, 0, "0x")  // 0 additional gasLimit increase, 0 airdrop, at 0x address
    abi.encodePacked(msg.sender),    // the address to send the tokens to on the destination
    bytes("")                        // bytes param, if you wish to send additional payload you can abi.encode() them here
);
```

To perform a `swap()` using ethers via a frontend, use the abi and call `swap()` on the Router contract instance:

```javascript
let tx = await router.swap(
    2,                     // destination chainId
    1,                     // source poolId
    1,                     // destination poolId
    yourAddress,           // refund address. extra gas (if any) is returned to this address
    _amountLD,             // quantity to swap in LD (local decimals) 
    _minAmountLD,          // the min qty you would accept in LD (local decimals)
    { dstGasForCall: 0, dstNativeAmount: 0, dstNativeAddr: "0x" },
    yourAddress,           // the address to send the tokens to on the destination
    "",                    // payload
    {value: fee}           // "fee" is the native gas to pay for the cross chain message fee. see 
);
```

TESTNET ONLY: For the `fee` parameter, if you wish to simply guess the fee, you can do something like this, as Stargate will `refund` the caller the additional gas amount if they send too much.

```javascript
let messageFee = ethers.utils.parseEther('0.025');     // send 0.02 eth converter to wei
await stargateRouter.swap(..., {value: messageFee});  // swap asset across chains
```

See how to [get the cross chain message fee](/stargate/developers/cross-chain-swap-fee.md) to pay enough native gas when calling `swap()`


---

# Agent Instructions: 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:

```
GET https://stargateprotocol.gitbook.io/stargate/developers/how-to-swap.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
