# Router

## Code

[Router.cairo](https://github.com/jediswaplabs/JediSwap/blob/main/contracts/Router.cairo)

## Address

Router is deployed at:

Starknet Alpha Testnet: [0x02bcc885342ebbcbcd170ae6cafa8a4bed22bb993479f49806e72d96af94c965](https://testnet.starkscan.co/contract/0x02bcc885342ebbcbcd170ae6cafa8a4bed22bb993479f49806e72d96af94c965)

Starknet Alpha Mainnet: [0x041fd22b238fa21cfcf5dd45a8548974d8263b3a531a60388411c5e230f97023](https://starkscan.co/contract/0x041fd22b238fa21cfcf5dd45a8548974d8263b3a531a60388411c5e230f97023)

## View Functions

### factory

```js
func factory() -> (address: felt):
```

Returns  [Factory](/for-developers/jediswap-v1/smart-contract-reference/factory.md) address.

### sort\_tokens​

```js
func sort_tokens(tokenA: felt, tokenB: felt) -> (token0: felt, token1: felt):
```

Sort tokens by address.

### quote​

```js
func quote(amountA: Uint256, reserveA: Uint256, reserveB: Uint256) -> (amountB: Uint256):
```

Given some asset amount and reserves, returns an amount of the other asset representing equivalent value.

* Useful for calculating optimal token amounts before calling [mint](/for-developers/jediswap-v1/smart-contract-reference/pair.md#mint-1).

### get\_amount\_out​

```js
func get_amount_out(amountIn: Uint256, reserveIn: Uint256, reserveOut: Uint256) -> (amountOut: Uint256):
```

Given an input asset amount, returns the maximum output amount of the other asset (accounting for fees) given reserves.

### get\_amount\_in​

```js
func get_amount_in(amountOut: Uint256, reserveIn: Uint256, reserveOut: Uint256) -> (amountIn: Uint256):
```

Returns the minimum input asset amount required to buy the given output asset amount (accounting for fees) given reserves.

### get\_amounts\_out​

```js
func get_amounts_out(amountIn: Uint256, path_len: felt, path: felt*) -> (amounts_len: felt, amounts: Uint256*):
```

Given an input asset amount and an array of token addresses, calculates all subsequent maximum output token amounts by calling [get\_reserves](/for-developers/jediswap-v1/smart-contract-reference/pair.md#get_reserves) for each pair of token addresses in the path in turn, and using these to call [ get\_amount\_out](#get_amount_out).

* Useful for calculating optimal token amounts before calling [swap](/for-developers/jediswap-v1/smart-contract-reference/pair.md#swap-1).

### get\_amounts\_in​

```js
func get_amounts_in(amountOut: Uint256, path_len: felt, path: felt*) -> (amounts_len: felt, amounts: Uint256*):
```

Given an output asset amount and an array of token addresses, calculates all preceding minimum input token amounts by calling [get\_reserves](/for-developers/jediswap-v1/smart-contract-reference/pair.md#get_reserves) for each pair of token addresses in the path in turn, and using these to call [get\_amount\_in](#get_amount_in).

* Useful for calculating optimal token amounts before calling [swap](/for-developers/jediswap-v1/smart-contract-reference/pair.md#swap-1).

## State-Changing Functions

### addLiquidity​

```js
func add_liquidity(tokenA: felt, tokenB: felt, amountADesired: Uint256,            amountBDesired: Uint256, amountAMin: Uint256, amountBMin: Uint256, to: felt, deadline: felt) -> (amountA: Uint256, amountB: Uint256, liquidity: Uint256):
```

Adds liquidity to an ERC-20⇄ERC-20 pool.

* To cover all possible scenarios,� *�calle* r should have already given the router an allowance of at least amountADesired/amountBDesired on tokenA/tokenB.
* Always adds assets at the ideal ratio, according to the price when the transaction is executed.

### remove\_liquidity

```js
​func remove_liquidity(tokenA: felt, tokenB: felt, liquidity: Uint256, amountAMin: Uint256, amountBMin: Uint256, to: felt, deadline: felt) -> (amountA: Uint256, amountB: Uint256):
```

Removes liquidity from an ERC-20⇄ERC-20 pool.

* *caller*  should have already given the router an allowance of at least liquidity on the pool.

### swap\_exact\_tokens\_for\_tokens​

```js
​func swap_exact_tokens_for_tokens(amountIn: Uint256, amountOutMin: Uint256, path_len: felt, path: felt*, to: felt, deadline: felt) -> (amounts_len: felt, amounts: Uint256*):
```

Swaps an exact amount of input tokens for as many output tokens as possible, along the route determined by the path. The first element of path is the input token, the last is the output token, and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist).

* *caller*  should have already given the router an allowance of at least amountIn on the input token.

### swap\_tokens\_for\_exact\_tokens​

```js
​func swap_tokens_for_exact_tokens(amountOut: Uint256, amountInMax: Uint256, path_len: felt, path: felt*, to: felt, deadline: felt) -> (amounts_len: felt, amounts: Uint256*):
```

Receive an exact amount of output tokens for as few input tokens as possible, along the route determined by the path. The first element of path is the input token, the last is the output token, and any intermediate elements represent intermediate tokens to trade through (if, for example, a direct pair does not exist).

* *caller*  should have already given the router an allowance of at least amountInMax on the input token.

## Interface

```js
%lang starknet

from starkware.cairo.common.uint256 import Uint256

@contract_interface
namespace IRouter:
    func factory() -> (address: felt):
    end

    func sort_tokens(tokenA: felt, tokenB: felt) -> (token0: felt, token1: felt):
    end

    func quote(amountA: Uint256, reserveA: Uint256, reserveB: Uint256) -> (amountB: Uint256):
    end

    ​func get_amount_out(amountIn: Uint256, reserveIn: Uint256, reserveOut: Uint256) -> (amountOut: Uint256):
    end

    ​func get_amount_in(amountOut: Uint256, reserveIn: Uint256, reserveOut: Uint256) -> (amountIn: Uint256):
    end

    func get_amounts_out(amountIn: Uint256, path_len: felt, path: felt*) -> (amounts_len: felt, amounts: Uint256*):
    end

    func get_amounts_in(amountOut: Uint256, path_len: felt, path: felt*) -> (amounts_len: felt, amounts: Uint256*):
    end

    func add_liquidity(tokenA: felt, tokenB: felt, amountADesired: Uint256,            amountBDesired: Uint256, amountAMin: Uint256, amountBMin: Uint256, to: felt, deadline: felt) -> (amountA: Uint256, amountB: Uint256, liquidity: Uint256):
    end

    ​func remove_liquidity(tokenA: felt, tokenB: felt, liquidity: Uint256, amountAMin: Uint256, amountBMin: Uint256, to: felt, deadline: felt) -> (amountA: Uint256, amountB: Uint256):
    end

    ​func swap_exact_tokens_for_tokens(amountIn: Uint256, amountOutMin: Uint256, path_len: felt, path: felt*, to: felt, deadline: felt) -> (amounts_len: felt, amounts: Uint256*):
    end

    ​func swap_tokens_for_exact_tokens(amountOut: Uint256, amountInMax: Uint256, path_len: felt, path: felt*, to: felt, deadline: felt) -> (amounts_len: felt, amounts: Uint256*):
    end
end
```

## ABI

```js
[
    {
        "members": [
            {
                "name": "low",
                "offset": 0,
                "type": "felt"
            },
            {
                "name": "high",
                "offset": 1,
                "type": "felt"
            }
        ],
        "name": "Uint256",
        "size": 2,
        "type": "struct"
    },
    {
        "inputs": [
            {
                "name": "factory",
                "type": "felt"
            }
        ],
        "name": "constructor",
        "outputs": [],
        "type": "constructor"
    },
    {
        "inputs": [],
        "name": "factory",
        "outputs": [
            {
                "name": "address",
                "type": "felt"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [
            {
                "name": "tokenA",
                "type": "felt"
            },
            {
                "name": "tokenB",
                "type": "felt"
            }
        ],
        "name": "sort_tokens",
        "outputs": [
            {
                "name": "token0",
                "type": "felt"
            },
            {
                "name": "token1",
                "type": "felt"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [
            {
                "name": "amountA",
                "type": "Uint256"
            },
            {
                "name": "reserveA",
                "type": "Uint256"
            },
            {
                "name": "reserveB",
                "type": "Uint256"
            }
        ],
        "name": "quote",
        "outputs": [
            {
                "name": "amountB",
                "type": "Uint256"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [
            {
                "name": "amountIn",
                "type": "Uint256"
            },
            {
                "name": "reserveIn",
                "type": "Uint256"
            },
            {
                "name": "reserveOut",
                "type": "Uint256"
            }
        ],
        "name": "get_amount_out",
        "outputs": [
            {
                "name": "amountOut",
                "type": "Uint256"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [
            {
                "name": "amountOut",
                "type": "Uint256"
            },
            {
                "name": "reserveIn",
                "type": "Uint256"
            },
            {
                "name": "reserveOut",
                "type": "Uint256"
            }
        ],
        "name": "get_amount_in",
        "outputs": [
            {
                "name": "amountIn",
                "type": "Uint256"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [
            {
                "name": "amountIn",
                "type": "Uint256"
            },
            {
                "name": "path_len",
                "type": "felt"
            },
            {
                "name": "path",
                "type": "felt*"
            }
        ],
        "name": "get_amounts_out",
        "outputs": [
            {
                "name": "amounts_len",
                "type": "felt"
            },
            {
                "name": "amounts",
                "type": "Uint256*"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [
            {
                "name": "amountOut",
                "type": "Uint256"
            },
            {
                "name": "path_len",
                "type": "felt"
            },
            {
                "name": "path",
                "type": "felt*"
            }
        ],
        "name": "get_amounts_in",
        "outputs": [
            {
                "name": "amounts_len",
                "type": "felt"
            },
            {
                "name": "amounts",
                "type": "Uint256*"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [
            {
                "name": "tokenA",
                "type": "felt"
            },
            {
                "name": "tokenB",
                "type": "felt"
            },
            {
                "name": "amountADesired",
                "type": "Uint256"
            },
            {
                "name": "amountBDesired",
                "type": "Uint256"
            },
            {
                "name": "amountAMin",
                "type": "Uint256"
            },
            {
                "name": "amountBMin",
                "type": "Uint256"
            },
            {
                "name": "to",
                "type": "felt"
            },
            {
                "name": "deadline",
                "type": "felt"
            }
        ],
        "name": "add_liquidity",
        "outputs": [
            {
                "name": "amountA",
                "type": "Uint256"
            },
            {
                "name": "amountB",
                "type": "Uint256"
            },
            {
                "name": "liquidity",
                "type": "Uint256"
            }
        ],
        "type": "function"
    },
    {
        "inputs": [
            {
                "name": "tokenA",
                "type": "felt"
            },
            {
                "name": "tokenB",
                "type": "felt"
            },
            {
                "name": "liquidity",
                "type": "Uint256"
            },
            {
                "name": "amountAMin",
                "type": "Uint256"
            },
            {
                "name": "amountBMin",
                "type": "Uint256"
            },
            {
                "name": "to",
                "type": "felt"
            },
            {
                "name": "deadline",
                "type": "felt"
            }
        ],
        "name": "remove_liquidity",
        "outputs": [
            {
                "name": "amountA",
                "type": "Uint256"
            },
            {
                "name": "amountB",
                "type": "Uint256"
            }
        ],
        "type": "function"
    },
    {
        "inputs": [
            {
                "name": "amountIn",
                "type": "Uint256"
            },
            {
                "name": "amountOutMin",
                "type": "Uint256"
            },
            {
                "name": "path_len",
                "type": "felt"
            },
            {
                "name": "path",
                "type": "felt*"
            },
            {
                "name": "to",
                "type": "felt"
            },
            {
                "name": "deadline",
                "type": "felt"
            }
        ],
        "name": "swap_exact_tokens_for_tokens",
        "outputs": [
            {
                "name": "amounts_len",
                "type": "felt"
            },
            {
                "name": "amounts",
                "type": "Uint256*"
            }
        ],
        "type": "function"
    },
    {
        "inputs": [
            {
                "name": "amountOut",
                "type": "Uint256"
            },
            {
                "name": "amountInMax",
                "type": "Uint256"
            },
            {
                "name": "path_len",
                "type": "felt"
            },
            {
                "name": "path",
                "type": "felt*"
            },
            {
                "name": "to",
                "type": "felt"
            },
            {
                "name": "deadline",
                "type": "felt"
            }
        ],
        "name": "swap_tokens_for_exact_tokens",
        "outputs": [
            {
                "name": "amounts_len",
                "type": "felt"
            },
            {
                "name": "amounts",
                "type": "Uint256*"
            }
        ],
        "type": "function"
    }
]

```


---

# 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.jediswap.xyz/for-developers/jediswap-v1/smart-contract-reference/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.
