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