# External Swap Router

## External Router Migration

The `CrocSwapRouter` contract exposes an ABI compatible `swap()` function that works with the upgraded swap callpath. This option will add approximately 60,000 gas to swap calls compared to direct swap calls.

The contract is deployed at

Ethereum: `0x533E164ded63f4c55E83E1f409BDf2BaC5278035`

Scroll: `0xfB5f26851E03449A0403Ca945eBB4201415fd1fc`

### Solidity Example

```solidity
// Ethereum
address router = 0x533E164ded63f4c55E83E1f409BDf2BaC5278035;
CrocSwapRouter(router).swap(
	base, 
  quote,
  poolIdx, 
  isBuy, 
  inBaseQty, 
  qty, 
  tip,
  limitPrice, 
  minOut,
  settleFlags);

//Scroll
address router = 0xfB5f26851E03449A0403Ca945eBB4201415fd1fc;
CrocSwapRouter(router).swap(
	base, 
  quote,
  poolIdx, 
  isBuy, 
  inBaseQty, 
  qty, 
  tip,
  limitPrice, 
  minOut,
  settleFlags);
```

### Javascript Example

The below example uses an Ethers provider to send a swap to the router contract:

```jsx
const { ethers } = require('ethers');

const provider = new ethers.providers.JsonRpcProvider(...);

const routerAbi = ... // See below

const etherRouterAddr = "0x533E164ded63f4c55E83E1f409BDf2BaC5278035"
const scrollRouterAddr = "0xfB5f26851E03449A0403Ca945eBB4201415fd1fc"

const contract = new ethers.Contract(etherRouterAddr, routerAbi, provider);

contract.swap(
	baseAddress, 
  quoteAddress,
  420, 
  isBuy, 
  inBaseQty, 
  qty, 
  0,
  limitPrice, 
  minOut,
  0);
```

#### ABI

For an ABI to call swap on the contract, you can use the below JSON

```jsx
[
    {
      "inputs": [
        {
          "internalType": "address",
          "name": "base",
          "type": "address"
        },
        {
          "internalType": "address",
          "name": "quote",
          "type": "address"
        },
        {
          "internalType": "uint256",
          "name": "poolIdx",
          "type": "uint256"
        },
        {
          "internalType": "bool",
          "name": "isBuy",
          "type": "bool"
        },
        {
          "internalType": "bool",
          "name": "inBaseQty",
          "type": "bool"
        },
        {
          "internalType": "uint128",
          "name": "qty",
          "type": "uint128"
        },
        {
          "internalType": "uint16",
          "name": "tip",
          "type": "uint16"
        },
        {
          "internalType": "uint128",
          "name": "limitPrice",
          "type": "uint128"
        },
        {
          "internalType": "uint128",
          "name": "minOut",
          "type": "uint128"
        },
        {
          "internalType": "uint8",
          "name": "reserveFlags",
          "type": "uint8"
        }
      ],
      "name": "swap",
      "outputs": [
        {
          "internalType": "int128",
          "name": "baseFlow",
          "type": "int128"
        },
        {
          "internalType": "int128",
          "name": "quoteFlow",
          "type": "int128"
        }
      ],
      "stateMutability": "payable",
      "type": "function"
    },
  ]
```

### Notes

Users will need to approve any tokens being sent to the `CrocSwapRouter` contract address

Swapping to/from dex balance surplus collateral is not available through this contract.


---

# 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://docs.ambient.finance/developers/dex-contract-interface/swaps/hot-path-swap-migration/external-swap-router.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.
