OnchainLobSpot
Overview
The OnchainLobSpot
class is designed for interacting with the Onchain LOB Spot API. It provides methods for retrieving market information, subscribing to market updates, placing orders, managing user orders and fills, and more.
Common contract transaction parameters
The following parameters are common across contract transaction methods:
gasLimit
: (optional) Transaction gas limit. If not provided, the value is estimated viaeth_estimateGas
call.maxFeePerGas
: (optional) The maximum fee per unit of gas willing to be paid for the transaction. Calculated asbaseFeePerGas + maxPriorityFeePerGas
. If not provided, obtained viaeth_maxPriorityFeePerGas
call.maxPriorityFeePerGas
: (optional) The maximum price of the consumed gas to be included as a tip to the validator. If not provided, obtained viaeth_maxPriorityFeePerGas
call.nonce
: (optional) Transaction nonce (counter). If not provided, obtained viaeth_getTransactionCount
call.
approveTokens
Approves the specified amount of tokens for the corresponding market contract.
market
: The market identifier.token
: The token to be approved.amount
: The amount of tokens to approve. Ifbigint
is provided, then the token's contract unit is used. IfBigNumber
is provided, then the scaled unit with the token's decimals is used.
depositTokens
Deposits the specified amount of tokens to the corresponding market contract.
market
: The market identifier.token
: The token to be deposited.amount
: The amount of tokens to deposit. The Onchain LOB scaled units are used.
withdrawTokens
Withdraws the specified amount of tokens or all tokens from the corresponding market contract.
market
: The market identifier.baseTokenAmount
: The amount of base tokens to withdraw. Ifbigint
is provided, then the token's contract unit is used. IfBigNumber
is provided, then the scaled unit with the token's decimals is used. Optional ifwithdrawAll
is true.quoteTokenAmount
: The amount of quote tokens to withdraw. Ifbigint
is provided, then the token's contract unit is used. IfBigNumber
is provided, then the scaled unit with the token's decimals is used. Optional ifwithdrawAll
is true.withdrawAll
: A flag indicating whether to withdraw all tokens. If true,baseTokenAmount
andquoteTokenAmount
are ignored.
setClaimableStatus
Sets the claimable status for the corresponding market contract.
market
: The market identifier.status
: The claimable status to be set.
placeOrder
Places a new order in the corresponding market contract. It can place limit or market order and use native token if market supports it.
market
: The market identifier.type
: The type of the order (limit, limit_post_only, ioc or market_execution).side
: The order side (buy or sell).size
: The size of the order.price
: The price of the order.transferExecutedTokens
: Whether to transfer executed tokens automatically.maxCommission
: The upper bound of commission to pay.nativeTokenToSend
: The amount of native token to send.useNativeToken
: Use native token for the transaction instead of the wrapped token.
Type parameter value
There are four types of order execution behavior:
limit
: place in the order book or execute and place with the specified price,limit_post_only
: place or cancel the order,ioc
: execute order with the indicated price or better, the remainder is cancelled,market_execution
: execute the full order size, price parameter is omitted.
placeOrderWithPermit
Places a new order with a permit in the corresponding market contract if the token supports ERC20Permit interface.
market
: The market identifier.type
: The type of the order (limit, limit_post_only, ioc or market_execution).side
: The order side (buy or sell).size
: The size of the order.price
: The price of the order.permit
: The quantity of tokens to permit for the order. Ussually the same value as the approve value.transferExecutedTokens
: Whether to transfer executed tokens automatically.maxCommission
: The upper bound of commission to pay.
This method allows placing an order with a permit, which is useful for tokens that support permit functionality, enabling gasless approvals.
placeMarketOrderWithTargetValue
Places a market order with a target value of the quote token in the corresponding market contract.
market
: The market identifier.type
: The type of the order (ioc or market_execution).side
: The order side (buy or sell).price
: The price of the order.size
: The quote token value to spend.targetValue
: The quote token value to spend.maxCommission
: The upper bound of commission to pay.nativeTokenToSend
: The amount of native token to send. Use native token for the transaction instead of the wrapped token.
This method allows placing a market order by specifying the target value of the quote token, which is useful for executing orders based on a specific value rather than base token quantity.
placeMarketOrderWithTargetValueWithPermit
Places a market order with a target value of the quote token and a permit in the corresponding market contract.
market
: The market identifier.type
: The type of the order (ioc or market_execution).side
: The order side (buy or sell).price
: The price of the order.size
: The quote token value to spend.permit
: The quantity of tokens to permit for the order. Usually the same value as the approve value.maxCommission
: The upper bound of commission to pay.transferExecutedTokens
: Whether to transfer executed tokens automatically.
The same action as in placeMarketOrderWithTargetValue
but it uses a token that supports permit.
claimOrder
Claims an order or fully cancels it in the corresponding market contract.
market
: The market identifier.orderId
: The unique identifier of the order to be claimed.transferExecutedTokens
: Whether to transfer executed tokens automatically.
changeOrder
Changes an existing order in the corresponding market contract.
market
: The market identifier.orderId
: The unique identifier of the order to be changed.newSize
: The new size of the order.newPrice
: The new price of the order.type
: The type of the order (e.g., limit, limit_post_only).transferExecutedTokens
: Whether to transfer executed tokens automatically.maxCommission
: The upper bound of commission to pay.
batchPlaceOrder
Places multiple orders in the corresponding market contract.
market
: The market identifier.type
: The type of the orders (limit, limit_post_only).orderParams
: Orders to place:side
: the side of the order (ask or bid).size
: the size of the order.price
: the price of the order.
transferExecutedTokens
: Whether to transfer executed tokens automatically.
This method does not support sending native token. Approve the wrapped token that is specified in the market object.
batchClaim
Claims or cancels specified orders.
market
: The market identifier.orderParams
: Orders to place:orderId
: the id of the order to claim or cancel.address
: the owner of the order.
onlyClaim
: Whether to claim or cancel orders (true
to claim).
batchChangeOrder
Changes multiple orders in the corresponding market contract.
market
: The market identifier.type
: The type of the orders (limit, limit_post_only).orderParams
: Orders to change:orderId
: the id of the order.newSize
: the new size of the order.newPrice
: the new price of the order.
transferExecutedTokens
: Whether to transfer executed tokens automatically.
This method cancels existing orders and places new ones. The new orders will each have new order ids.
getMarket
Retrieves the market information for the specified market.
market
: The market identifier.
getMarkets
Retrieves the markets.
market
: Optional market identifier to filter results.
getTokens
Retrieves the tokens.
token
: Optional token identifier to filter results.
getOrderbook
Retrieves the orderbook for the specified market.
market
: The market identifier.aggregation
: Optional level of price aggregation.limit
: Optional limit on the number of orders to retrieve.
getOrders
Retrieves the orders for the specified market.
market
: The market identifier.user
: The user's address.limit
: Optional limit on the number of orders to retrieve.status
: Optional filter for order status.
getOrderHistory
Retrieves the order history for the specified market.
market
: The market identifier.user
: The user's address.limit
: Optional limit on the number of orders to retrieve.
getTrades
Retrieves the trades for the specified market.
market
: The market identifier.limit
: Optional limit on the number of trades to retrieve.
getFills
Retrieves the fills for the specified market.
market
: The market identifier.user
: The user's address.limit
: Optional limit on the number of fills to retrieve.
getCandles
Retrieves the candles for the specified market and resolution.
market
: The market identifier.resolution
: The time resolution for the candles.fromTime
: Optional start time for the candle data.toTime
: Optional end time for the candle data.
events
The OnchainLobSpot.events
property defines various events that you can listen to for real-time updates. These events include:
marketUpdated
: Triggered when there is an update in the market.orderbookUpdated
: Triggered when there is an update in the order book.tradesUpdated
: Triggered when a new trade occurs.userFillUpdated
: Triggered when a fill is updated.userOrderUpdated
: Triggered when an order is updated.userOrderHistoryUpdated
: Triggered when an order history is updated.candleUpdated
: Triggered when new candle data is available.allMarketsUpdated
: Triggered when there is an update across any market.subscriptionError
: Triggered when there is an error related to a subscription.
You can add event listeners to these events to handle real-time data as it comes in. Events start coming after ypu subscribe to them with certain methods
OnchainLobSpot Subscription Methods
The OnchainLobSpot class also includes methods for subscribing to and unsubscribing from various market updates, such as subscribeToMarket
, unsubscribeFromMarket
, subscribeToOrderbook
, unsubscribeFromOrderbook
, etc. These methods interact with the WebSocket API to provide real-time updates.
Market Subscriptions
subscribeToMarket
Subscribes to updates for a specific market.
unsubscribeFromMarket
Unsubscribes from updates for a specific market.
subscribeToAllMarkets
Subscribes to updates for all markets.
unsubscribeFromAllMarkets
Unsubscribes from updates for all markets.
Orderbook Subscriptions
subscribeToOrderbook
Subscribes to orderbook updates for a specific market.
unsubscribeFromOrderbook
Unsubscribes from orderbook updates for a specific market.
Trade Subscriptions
subscribeToTrades
Subscribes to trade updates for a specific market.
unsubscribeFromTrades
Unsubscribes from trade updates for a specific market.
User Order Subscriptions
subscribeToUserOrders
Subscribes to user order updates for a specific market and user.
unsubscribeFromUserOrders
Unsubscribes from user order updates for a specific market and user.
User Order History Subscriptions
subscribeToUserOrderHistory
Subscribes to user order history updates for a specific market and user.
unsubscribeFromUserOrderHistory
Unsubscribes from user order history updates for a specific market and user.
User Fill Subscriptions
subscribeToUserFills
Subscribes to user fill updates for a specific market and user.
unsubscribeFromUserFills
Unsubscribes from user fill updates for a specific market and user.
Candle Subscriptions
subscribeToCandles
Subscribes to candle updates for a specific market and resolution.
unsubscribeFromCandles
Unsubscribes from candle updates for a specific market and resolution.
Examples
Post a new order (view with HTTP API)
Post a new order (view with WebSockets)
getOrderbook
Returns snapshot of the orderbook.
getOrders
Returns user's orders.
getOrderHistory
Returns user's order history.
getTrades
Returns last trades.
getFills
Returns user's fills.
getMarkets
Returns market data.
Last updated