CrystalRouter

The router contract is used as a user-friendly endpoint to interact with Crystal's markets.

Functions

gov

function gov() external view returns (address gov)

Returns the privileged party in charge of deploying new markets.

Return Values

Name
Type
Description

gov

address

Current governance address

WETH

function WETH() external view returns (address WETH)

Returns the address of the ERC-20 wrapper of the native token.

Return Values

Name
Type
Description

WETH

address

Address of the wrapped native token

ETH

function ETH() external view returns (address ETH)

Returns 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE, the placeholder address for the network's native token.

Return Values

Name
Type
Description

ETH

address

Placeholder address for the network's native token

getMarket

function getMarket(address tokenA, address tokenB) external view returns (address market)

Looks up and returns the address of the market corresponding to the given pair of token addresses if deployed.

Parameters

Name
Type
Description

tokenA

address

The address of one of the tokens in the market

tokenB

address

The address of the other token

Return Values

Name
Type
Description

market

address

The address of the market for tokenA and tokenB, if deployed

refToAddress

function refToAddress(string calldata refCode) external view returns (address refAddress)

Returns the address of the owner of a given referral code.

Parameters

Name
Type
Description

refCode

string

The referral code

Return Values

Name
Type
Description

refAddress

address

The address of the referral code

addressToRef

function addressToRef(address refAddress) external view returns (string memory refCode)

Returns the referral code if created of a given address.

Parameters

Name
Type
Description

refAddress

address

The address

Return Values

Name
Type
Description

refCode

string

The referral code of the address

getAmountsOut

function getAmountsOut(uint256 amountIn, address[] memory path) external view returns (uint256[] memory amounts)

Calculates the expected output amount given an input amount and path. Will revert if any consecutive pair of tokens in path does not have a market, or if path has less than 2 values.

Parameters

Name
Type
Description

amountIn

uint256

Input token amount

path

address[] memory

A list of token addresses, with the input token first and output token last

Return Values

Name
Type
Description

amounts

uint256[] memory

Token amounts at each step along the swap route

getAmountsIn

function getAmountsIn(uint256 amountOut, address[] memory path) public view returns (uint256[] memory amounts)

Calculates the required input amount given a desired output amount and path. Will revert if any consecutive pair of tokens in path does not have a market, if path has less than 2 values, or if there is insufficient liquidity in the markets to obtain the desired output amount.

Parameters

Name
Type
Description

amountOut

uint256

Output token amount

path

address[] memory

A list of token addresses, with the input token first and output token last

Return Values

Name
Type
Description

amounts

uint256[] memory

Token amounts at each step along the swap route

changeGov

function changeGov(address _gov) external

Changes the governance address granted the privilege of creating new markets.

Parameters

Name
Type
Description

_gov

address

New governance address

deploy

function deploy(address _quoteAsset, address _baseAsset, uint256 _fee, uint256 _scaleFactor, uint256 _minSize, uint256 _maxPrice) external returns (address market)

Deploys a new market with specified parameters and adds the market to the router's market registry.

Parameters

Name
Type
Description

_quoteAsset

address

Quote token address

_baseAsset

address

Base token address

_fee

uint256

Fee percentage, calculated by (100000 - _fee) / 1000 = fee%

_scaleFactor

uint256

_minSize

uint256

Minimum limit order size in base token terms

_maxPrice

uint256

Maximum price

Return Values

Name
Type
Description

market

address

Address of newly deployed market

swapExactETHForTokens

function swapExactETHForTokens(uint256 amountOutMin, address[] memory path, address to, uint256 deadline, address referrer) external payable returns (uint256[] memory amounts)

Swap the transaction value amount of native token for at least amountOutMin tokens along the route specified by path. Reverts if the path is invalid or if output amount is below the minimum.

Parameters

Name
Type
Description

amountOutMin

uint256

Minimum output token amount

path

address[] memory

A list of token addresses, with the input token first and output token last

to

address

The address to send the output to

deadline

uint256

Timestamp after which transaction will revert

referrer

address

Referrer address

Return Values

Name
Type
Description

amounts

uint256[] memory

Token amounts at each step along the swap route

swapExactTokensForETH

function swapExactTokensForETH(uint256 amountIn, uint256 amountOutMin, address[] memory path, address to, uint256 deadline, address referrer) external returns (uint256[] memory amounts)

Swap amountIn tokens for at least amountOutMin of the native token along the route specified by path. Reverts if the path is invalid or if output amount is below the minimum.

Parameters

Name
Type
Description

amountIn

uint256

Input token amount

amountOutMin

uint256

Minimum output token amount

path

address[] memory

A list of token addresses, with the input token first and output token last

to

address

The address to send the output to

deadline

uint256

Timestamp after which transaction will revert

referrer

address

Referrer address

Return Values

Name
Type
Description

amounts

uint256[] memory

Token amounts at each step along the swap route

swapExactTokensForTokens

function swapExactTokensForTokens(uint256 amountIn, uint256 amountOutMin, address[] memory path, address to, uint256 deadline, address referrer) external returns (uint256[] memory amounts)

Swap amountIn tokens for at least amountOutMin tokens along the route specified by path. Reverts if the path is invalid or if output amount is below the minimum.

Parameters

Name
Type
Description

amountIn

uint256

Input token amount

amountOutMin

uint256

Minimum output token amount

path

address[] memory

A list of token addresses, with the input token first and output token last

to

address

The address to send the output to

deadline

uint256

Timestamp after which transaction will revert

referrer

address

Referrer address

Return Values

Name
Type
Description

amounts

uint256[] memory

Token amounts at each step along the swap route.

swapETHForExactTokens

function swapETHForExactTokens(uint256 amountOut, address[] memory path, address to, uint256 deadline, address referrer) external payable returns (uint256[] memory amounts)

Swap the native token for exactly amountOut tokens along the route specified by path. The maximum input amount is determined by the transaction value. Reverts if the path is invalid or if input amount is above the maximum.

Parameters

Name
Type
Description

amountOut

uint256

Output token amount

path

address[] memory

A list of token addresses, with the input token first and output token last

to

address

The address to send the output to

deadline

uint256

Timestamp after which transaction will revert

referrer

address

Referrer address

Return Values

Name
Type
Description

amounts

uint256[] memory

Token amounts at each step along the swap route.

swapTokensForExactETH

function swapTokensForExactETH(uint256 amountOut, uint256 amountInMax, address[] memory path, address to, uint256 deadline, address referrer) external returns (uint256[] memory amounts)

Swap any token for exactly amountOut of the native token along the route specified by path. Reverts if the path is invalid or if input amount is above the maximum.

Parameters

Name
Type
Description

amountOut

uint256

Output token amount

amountInMax

uint256

Maximum input token amount

path

address[] memory

A list of token addresses, with the input token first and output token last

to

address

The address to send the output to

deadline

uint256

Timestamp after which transaction will revert

referrer

address

Referrer address

Return Values

Name
Type
Description

amounts

uint256[] memory

Token amounts at each step along the swap route.

swapTokensForExactTokens

function swapTokensForExactTokens(uint256 amountOut, uint256 amountInMax, address[] memory path, address to, uint256 deadline, address referrer) external returns (uint256[] memory amounts)

Swap any token for exactly amountOut tokens along the route specified by path. Reverts if the path is invalid or if input amount is above the maximum.

Parameters

Name
Type
Description

amountOut

uint256

Output token amount

amountInMax

uint256

Maximum input token amount

path

address[] memory

A list of token addresses, with the input token first and output token last

to

address

The address to send the output to

deadline

uint256

Timestamp after which transaction will revert

referrer

address

Referrer address

Return Values

Name
Type
Description

amounts

uint256[] memory

Token amounts at each step along the swap route.

swap

function swap(bool exactInput, address tokenIn, address tokenOut, uint256 orderType, uint256 size, uint256 worstPrice, uint256 deadline, address referrer) external payable returns (uint256 amountIn, uint256 amountOut, uint256 id)

Swap from any token to any other token across a singular market supporting various order types. Reverts if there is no market between tokenIn and tokenOut.

Parameters

Name
Type
Description

exactInput

bool

Whether the input or output size is specified

tokenIn

address

Token to swap from

tokenOut

address

Token to swap to

orderType

uint256

Order type (0 = partial fill, 1 = complete fill, 2 = market to limit)

size

uint256

Token amount to swap from/to depending on exactInput

worstPrice

uint256

Worst acceptable price

deadline

uint256

Timestamp after which transaction will revert

referrer

address

Referrer address

Return Values

Name
Type
Description

amountIn

uint256

Executed input amount

amountOut

uint256

Executed output amount

id

uint256

Limit order id if applicable

limitOrder

function limitOrder(address tokenIn, address tokenOut, uint256 price, uint256 size) external payable returns (uint256 id)

Place a post-only limit order with native token support. Reverts if the order fails.

Parameters

Name
Type
Description

tokenIn

address

Token to swap from

tokenOut

address

Token to swap to

price

uint256

Limit price

size

uint256

Input token amount

Return Values

Name
Type
Description

id

uint256

Limit order id

cancelOrder

function cancelOrder(address tokenIn, address tokenOut, uint256 price, uint256 id) external

Cancel an existing limit order with native token support. Reverts if the order is already filled or canceled.

Parameters

Name
Type
Description

tokenIn

address

Token to swap from

tokenOut

address

Token to swap to

price

uint256

Price of order to cancel

id

uint256

Id of order to cancel

Return Values

Name
Type
Description

size

uint256

Order size at cancel in terms of quote token multiplied by scale factor

claimFees

function claimFees(address[] calldata markets) external

Batch claim accumulated referral fees and rebates from multiple markets.

Parameters

Name
Type
Description

markets

address[] calldata

List of market addresses to claim fees from

setReferral

function setReferral(string calldata code) external

Update the referral mapping to set code as the caller's referral code. Each address can have one referral code at a time, and this function reverts if the code is already taken. Referral codes are always stored in lowercase.

Parameters

Name
Type
Description

code

string calldata

Referral code

multiBatchOrders

function multiBatchOrders(address[] calldata markets, uint256[][] calldata action, uint256[][] calldata price, uint256[][] calldata param1, address[][] calldata param2) external payable returns (uint256[][] memory returnData)

Executes multiple actions across multiple markets in a single transaction. Individual actions fail silently, returning 0 as the returnData value upon failure.

Parameters

Name
Type
Description

markets

address[] calldata

List of market addresses to perform actions on

action

uint256[][] calldata

List of actions to perform for each market previously listed (0 = cancel, 1 = limit buy, 2 = limit sell, 3 = market buy, 4 = market sell

price

uint256[][] calldata

The price level to place/cancel limit orders, or the worst acceptable price for market orders

param1

address[][] calldata

The size for market/limit orders or the id of the order to cancel

param2

address[][] calldata

The address to transfer tokens from for market/limit orders, or the market to transfer tokens to for order cancelations; must be either the router address or msg.sender

Return Values

Name
Type
Description

returnData

uint256[][] memory

List of output amounts for market orders, ids for limit orders, and sizes at cancel for order cancellations

Events

OrderBookCreated

event OrderBookCreated(address indexed quoteAsset, address indexed baseAsset, address market, uint256 fee, uint256 scaleFactor, uint256 minSize, uint256 maxPrice);

Emitted whenever a new market is created. A newly created market essentially replaces a previous market if there already exist one with the same assets.

Parameters

Name
Type
Description

quoteAsset

address indexed

The address of the asset price is denominated in

baseAsset

address indexed

The address of the other asset in the market

market

address

The address of the market

fee

uint256

The initial fee of the market

scaleFactor

uint256

The power of ten quote asset amounts are multiplied by to allow for trading at decimal prices

minSize

uint256

The minimum size of a valid limit order denominated in units of the quote asset

maxPrice

uint256

The initial maximum price supported by the market

Last updated