formercrystal
All asset balances, markets, and trading functionality are accessible through a single central contract featuring swap endpoints across the orderbook, AMM, and launchpad.
Functions
highestBid
function highestBid() external view returns (uint256 highestBid)Returns the current highest bid.
Return Values
highestBid
uint256
Value of the highest bid.
lowestAsk
function lowestAsk() external view returns (uint256 lowestAsk)Returns the current lowest bid.
Return Values
lowestAsk
uint256
Value of the lowest ask.
latestUserId
function latestUserId() external view returns (uint256 latestUserId)Returns the most recently assigned user ID.
Return Values
latestUserId
uint256
Value of latestUserId
quoteAsset
Returns the contract address of the quote asset.
Return Values
quoteAsset
address
Address of the quote asset
baseAsset
Returns the contract address of the base asset.
Return Values
baseAsset
address
The address of the base asset
router
Returns the address of the router.
Return Values
containi
address
The address of the router contract associated with this contract.
feeAddress
Returns the address that receives the fee.
Return Values
feeAddress
address
The address designated to receive the contract fees.
fee
Returns the taker order fee rate. To convert to percentage, the formula is (100,000-fee)/1,000. For example, a fee value of 99,970 would result in a fee rate of (100,000-99,970)/1,000 = 0.03%.
Return Values
fee
uint256
The current fee
maxPrice
Returns the maximum price for orders.
Return Values
maxPrice
uint256
The maximum allowable price
scaleFactor
Returns the factor used to scale prices within the contract.
Return Values
scaleFactor
uint256
Multiplier or divisor used when scaling prices
minSize
Returns the minimum trade/order size.
Return Values
minSize
uint256
The minimum permissible quantity.
orders
Retrieves an Order based on specified price and orderId.
Parameters
price
uint256
The price level key
orderId
uint256
The order's unique identifier within price
Return Values
order
Order
Order struct containing information about the specified order
priceLevels
Returns the PriceLevel.
Parameters
price
uint256
Price key for the price level
Return Values
priceLevel
PriceLevel
PriceLevel struct containing information at the specified price
userIdToAddress
Returns the address mapped to userId.
Parameters
userID
uint256
The userID
Return Values
userIdToAddress
address
The address associated with the given userId
addressToUserId
Retrieves the user ID associated with a given address.
Parameters
user
address
The address for which to retrieve the user ID
Return Values
addressToUserId
uint256
The numeric user ID corresponding to user
accumulatedFeeQuote
Returns the accumulated quote-asset fees for a user.
Parameters
user
address
The address of the retrieved quote-asset fees
Return Values
accumulatedFeeQuote
uint256
Total acucmulated fee quote recorded for the user
accumulatedFeeBase
Returns the accumulated base-asset fees for a user.
Parameters
user
address
The address of the user
Return Values
accumulatedFeeBase
uint256
The total accumulated fee base recorded for the user
getPriceLevels
Retrieves a list of initialized price levels and their respective sizes.
Parameters
isAscending
boolean
If true, search for price levels in the space above startPrice, otherwise search below
startPrice
uint256
Start price, inclusive
distance
uint256
How many price increments to check
Return Values
_orders
bytes memory
Byte string containing 32 byte chunks, with the first 16 bytes the price and last 16 bytes the sum of all order sizes at the price level
getPriceLevelsFromMid
Retrieves a list of initialized price levels and their respective sizes starting from the bid-ask spread, searching both above and below.
Parameters
distance
uint256
How many price increments to check each way
Return Values
_highestBid
bytes memory
The price of the highest buy order
_lowestAsk
uint256
The price of the lowest sell order
buyOrders
bytes memory
Byte string representing all buy orders within distance of the bid-ask spread
sellOrders
bytes memory
Byte string representing all sell orders within distance of the bid-ask spread
getQuote
Calculates the expected input/output amount of a trade with worstPrice as the worst acceptable price.
Parameters
isBuy
bool
Buy order if true, otherwise sell order
isExactInput
bool
If true, input amount is specified, otherwise output amount is specified
size
uint256
Input/output size depending on isExactInput
worstPrice
uint256
Worst acceptable price at which order execution will terminate
Return Values
amountIn
uint256
Calculated input amount
amountOut
uint256
Calculated output amount
changeMaxPrice
Updates the maximum price of this market.
Parameters
newMaxPrice
uint256
The new maximum price
changeFee
Updates the taker fee rate of this market. The maximum possible taker fee is 5%.
Parameters
newFee
uint256
The new taker fee rate
changeGov
Updates the governance address of this market. The governance address is the fee recipient and can change the maximum price and fee rate of this market.
Parameters
newGov
address
The new governance address
marketOrder
Places a taker order.
Parameters
isBuy
bool
Buy order if true, otherwise sell order
isExactInput
bool
If true, input amount is specified, otherwise output amount is specified
isFromCaller
bool
If true, transfer tokens from the caller address, otherwise transfer tokens from msg.sender
isToCaller
bool
If true, transfer tokens to the caller address, otherwise transfer tokens to msg.sender
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
caller
address
Equal to msg.sender unless the caller is the router, in which this parameter would be the address that called the router
referrer
address
Referrer address
Return Values
amountIn
uint256
Executed input amount
amountOut
uint256
Executed output amount
id
uint256
Limit order id if applicable
limitOrder
Places a post-only maker order.
Parameters
isBuy
bool
Buy order if true, otherwise sell order
price
uint256
Limit price
size
uint256
Input token amount
from
address
Address to transfer tokens from, must be either caller or msg.sender
owner
address
Equal to msg.sender unless the caller is the router, in which this parameter would be the address that called the router
Return Values
id
uint256
Limit order id
cancelOrder
Cancels an existing limit order.
Parameters
price
uint256
Price of order to cancel
id
uint256
Id of order to cancel
to
address
Address to transfer remaining tokens to
owner
address
Equal to msg.sender unless the caller is the router, in which this parameter would be the address that called the router
Return Values
size
uint256
Order size at cancel in terms of quote token multiplied by scale factor
registerUser
Allows an address to place limit orders, required to be called once per market. The router takes care of this automatically.
Parameters
caller
address
Equal to msg.sender unless the caller is the router, in which this parameter would be the address that called the router
Return Values
_latestUserId
uint256
The user's id, only used internally
claimFees
Claim accumulated referral fees and rebates for address caller.
Parameters
caller
address
Equal to msg.sender unless the caller is the router, in which this parameter would be the address that called the router
batchOrders
Executes multiple actions in a single transaction. Individual actions fail silently, returning 0 as the returnData value upon failure.
Parameters
actions
address
List of actions to perform (0 = cancel, 1 = limit buy, 2 = limit sell, 3 = market buy, 4 = market sell)
prices
uint256[] calldata
The price level to place/cancel limit orders, or the worst acceptable price for market orders
param1
uint256[] calldata
The order 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
owner
address
Equal to msg.sender unless the caller is the router, in which this parameter would be the address that called the router
Return Values
returnData
uint256[] memory
List of output amounts for market orders, ids for limit orders, and sizes at cancel for order cancellations
Fallback
The fallback function is gas-optimized and intended for use by market makers and other high frequency automated trading strategies. Calldata is in 32 byte chunks, with the first byte being the action (0 = cancel, 1 = limit buy, 2 = limit sell, 3 = market buy, 4 = market sell), trailing 10 bytes of the first 16 bytes being the price level to place/cancel limit orders, or the worst acceptable price for market orders, and last 16 bytes being the order size for market/limit orders or the id of the order to cancel. Each 32 byte chunk represents an action, and msg.sender is used as all address parameters for each action.
Events
OrdersFilled
Emitted at the conclusion of a successful taker order.
Parameters
caller
address indexed
The address of the user who placed the trade.
amounts
uint256
The first 16 bytes represent the input amount of the trade denominated in the input token. The last 16 bytes represent the output amount of the trade denominated in the output token.
info
uint256
The first bit of this value represents whether it is a buy or sell order (1 is buy). The last 16 bytes represent the last filled price of the order.
filled
bytes
Each 32 byte chunk of this bytes array represents a filled or partially filled maker order. The first 10 bytes represent the price of the order, next 6 represent the id, and last 16 represent the updated size of the order following the trade. Price is in the form of quote amount * scale factor / base amount, while size is in the form of quote amount * scale factor.
OrdersUpdated
Emitted at the conclusion of a successful batch of limit order placement/cancelations.
Parameters
caller
address indexed
The address of the user who is placing/canceling trades.
orderData
bytes
Each 32 byte chunk of this bytes array represents a newly placed or canceled maker order. The first bit represents the action (3 = cancel buy, 2 = cancel sell, 1 = buy, 0 = sell). The first 10 bytes excluding the first bit represent the price of the order, next 6 represent the id, and last 16 represent the size of the order or the size of the order prior to cancellation. Price is in the form of quote amount * scale factor / base amount, while size is in the form of quote amount * scale factor.
Structs
Order
Last updated