Router

Code

Router.cairo

Address

Router is deployed at:

Starknet Alpha Testnet: 0x02bcc885342ebbcbcd170ae6cafa8a4bed22bb993479f49806e72d96af94c965

Starknet Alpha Mainnet: 0x041fd22b238fa21cfcf5dd45a8548974d8263b3a531a60388411c5e230f97023

View Functions

factory

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

Returns Factory address.

sort_tokens​

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

Sort tokens by address.

quote​

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.

get_amount_out​

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​

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​

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 each pair of token addresses in the path in turn, and using these to call get_amount_out.

  • Useful for calculating optimal token amounts before calling swap.

get_amounts_in​

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 each pair of token addresses in the path in turn, and using these to call get_amount_in.

  • Useful for calculating optimal token amounts before calling swap.

State-Changing Functions

addLiquidity​

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

​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​

​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​

​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

%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

[
    {
        "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"
    }
]

Last updated