CrystalRouter
The router contract is used as a user-friendly endpoint to interact with Crystal's markets.
Functions
gov
gov
function gov() external view returns (address gov)
Returns the privileged party in charge of deploying new markets.
Return Values
gov
address
Current governance address
WETH
WETH
function WETH() external view returns (address WETH)
Returns the address of the ERC-20 wrapper of the native token.
Return Values
WETH
address
Address of the wrapped native token
ETH
ETH
function ETH() external view returns (address ETH)
Returns 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE, the placeholder address for the network's native token.
Return Values
ETH
address
Placeholder address for the network's native token
getMarket
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
tokenA
address
The address of one of the tokens in the market
tokenB
address
The address of the other token
Return Values
market
address
The address of the market for tokenA and tokenB, if deployed
refToAddress
refToAddress
function refToAddress(string calldata refCode) external view returns (address refAddress)
Returns the address of the owner of a given referral code.
Parameters
refCode
string
The referral code
Return Values
refAddress
address
The address of the referral code
addressToRef
addressToRef
function addressToRef(address refAddress) external view returns (string memory refCode)
Returns the referral code if created of a given address.
Parameters
refAddress
address
The address
Return Values
refCode
string
The referral code of the address
getAmountsOut
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
amountIn
uint256
Input token amount
path
address[] memory
A list of token addresses, with the input token first and output token last
Return Values
amounts
uint256[] memory
Token amounts at each step along the swap route
getAmountsIn
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
amountOut
uint256
Output token amount
path
address[] memory
A list of token addresses, with the input token first and output token last
Return Values
amounts
uint256[] memory
Token amounts at each step along the swap route
changeGov
changeGov
function changeGov(address _gov) external
Changes the governance address granted the privilege of creating new markets.
Parameters
_gov
address
New governance address
deploy
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
_quoteAsset
address
Quote token address
_baseAsset
address
Base token address
_fee
uint256
Fee percentage, calculated by (100000 - _fee) / 1000 = fee%
_minSize
uint256
Minimum limit order size in base token terms
_maxPrice
uint256
Maximum price
Return Values
market
address
Address of newly deployed market
swapExactETHForTokens
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
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
amounts
uint256[] memory
Token amounts at each step along the swap route
swapExactTokensForETH
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
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
amounts
uint256[] memory
Token amounts at each step along the swap route
swapExactTokensForTokens
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
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
amounts
uint256[] memory
Token amounts at each step along the swap route.
swapETHForExactTokens
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
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
amounts
uint256[] memory
Token amounts at each step along the swap route.
swapTokensForExactETH
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
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
amounts
uint256[] memory
Token amounts at each step along the swap route.
swapTokensForExactTokens
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
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
amounts
uint256[] memory
Token amounts at each step along the swap route.
swap
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
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
amountIn
uint256
Executed input amount
amountOut
uint256
Executed output amount
id
uint256
Limit order id if applicable
limitOrder
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
tokenIn
address
Token to swap from
tokenOut
address
Token to swap to
price
uint256
Limit price
size
uint256
Input token amount
Return Values
id
uint256
Limit order id
cancelOrder
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
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
size
uint256
Order size at cancel in terms of quote token multiplied by scale factor
claimFees
claimFees
function claimFees(address[] calldata markets) external
Batch claim accumulated referral fees and rebates from multiple markets.
Parameters
markets
address[] calldata
List of market addresses to claim fees from
setReferral
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
code
string calldata
Referral code
multiBatchOrders
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
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
returnData
uint256[][] memory
List of output amounts for market orders, ids for limit orders, and sizes at cancel for order cancellations
Events
OrderBookCreated
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
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