Exchange SDK (CLOB)
CLOB order operations, fee management, and order book queries
The ExchangeSDK handles CLOB (Central Limit Order Book) order operations, fee management, and order book queries.
Pair Management
getPair
Get the exchange contract address for a token pair.
Signature:
async getPair(tokenA: string, tokenB: string): Promise<string>Parameters:
tokenA: string- First token addresstokenB: string- Second token address
Returns: Promise<string> - Exchange contract address
createPair
Create a new trading pair (if it doesn't exist).
Signature:
async createPair(tokenA: string, tokenB: string): Promise<string>Parameters:
tokenA: string- First token addresstokenB: string- Second token address
Returns: Promise<string> - Exchange contract address
Order Placement
placeSellOrder
Place a sell limit order.
Signature:
async placeSellOrder(
main: string,
sellPrice: bigint,
sellAmount: bigint,
tokenA: string,
tokenB: string
): Promise<ContractTransactionResponse>Parameters:
main: string- Main account addresssellPrice: bigint- Sell pricesellAmount: bigint- Amount to selltokenA: string- Token to selltokenB: string- Token to receive
Returns: Promise<ContractTransactionResponse>
Token Approval Requirement:
Before placing sell orders, you must approve the Exchange contract to spend your tokens:
// Get the exchange/pair address
const pairAddress = await exchangeSDK.getPair(tokenA, tokenB);
// Approve tokenA for the exchange
const tokenContract = new ethers.Contract(tokenA, TokenABI, signer);
const amount = ethers.parseEther("1000"); // Amount to approve
await tokenContract.approve(pairAddress, amount);
// Verify approval
const allowance = await tokenContract.allowance(userAddress, pairAddress);
if (allowance < sellAmount) {
throw new Error("Insufficient token approval");
}The SDK will throw an error "Not enough approval" if the allowance is insufficient.
placeBuyOrder
Place a buy limit order.
Signature:
async placeBuyOrder(
main: string,
buyPrice: bigint,
buyAmount: bigint,
tokenA: string,
tokenB: string
): Promise<ContractTransactionResponse>Parameters:
main: string- Main account addressbuyPrice: bigint- Buy pricebuyAmount: bigint- Amount to buytokenA: string- Token to buytokenB: string- Token to spend
Returns: Promise<ContractTransactionResponse>
Token Approval Requirement:
Before placing buy orders, you must approve the Exchange contract to spend your tokens:
// Get the exchange/pair address
const pairAddress = await exchangeSDK.getPair(tokenA, tokenB);
// Approve tokenB for the exchange
const tokenContract = new ethers.Contract(tokenB, TokenABI, signer);
const amount = ethers.parseEther("1000"); // Amount to approve
await tokenContract.approve(pairAddress, amount);
// Verify approval
const allowance = await tokenContract.allowance(userAddress, pairAddress);
if (allowance < buyAmount) {
throw new Error("Insufficient token approval");
}The SDK will throw an error "Not enough approval" if the allowance is insufficient.
placeSellOrderTo
Place a sell order with a specified recipient.
Signature:
async placeSellOrderTo(
main: string,
sellPrice: bigint,
sellAmount: bigint,
to: string,
tokenA: string,
tokenB: string
): Promise<ContractTransactionResponse>Parameters:
main: string- Main account addresssellPrice: bigint- Sell pricesellAmount: bigint- Amount to sellto: string- Recipient addresstokenA: string- Token to selltokenB: string- Token to receive
Returns: Promise<ContractTransactionResponse>
placeBuyOrderTo
Place a buy order with a specified recipient.
Signature:
async placeBuyOrderTo(
main: string,
buyPrice: bigint,
buyAmount: bigint,
to: string,
tokenA: string,
tokenB: string
): Promise<ContractTransactionResponse>Parameters:
main: string- Main account addressbuyPrice: bigint- Buy pricebuyAmount: bigint- Amount to buyto: string- Recipient addresstokenA: string- Token to buytokenB: string- Token to spend
Returns: Promise<ContractTransactionResponse>
placeSellOrderViaSubaccount
Place a sell order via a subaccount.
Signature:
async placeSellOrderViaSubaccount(
main: string,
sellPrice: bigint,
sellAmount: bigint,
tokenA: string,
tokenB: string
): Promise<ContractTransactionResponse>Parameters:
main: string- Main account addresssellPrice: bigint- Sell pricesellAmount: bigint- Amount to selltokenA: string- Token to selltokenB: string- Token to receive
Returns: Promise<ContractTransactionResponse>
placeBuyOrderViaSubaccount
Place a buy order via a subaccount.
Signature:
async placeBuyOrderViaSubaccount(
main: string,
buyPrice: bigint,
buyAmount: bigint,
tokenA: string,
tokenB: string
): Promise<ContractTransactionResponse>Parameters:
main: string- Main account addressbuyPrice: bigint- Buy pricebuyAmount: bigint- Amount to buytokenA: string- Token to buytokenB: string- Token to spend
Returns: Promise<ContractTransactionResponse>
Order Cancellation
deleteSellOrder
Cancel a sell order.
Signature:
async deleteSellOrder(
tokenA: string,
tokenB: string,
price: bigint,
orderId: string
): Promise<ContractTransactionResponse>Parameters:
tokenA: string- First token addresstokenB: string- Second token addressprice: bigint- Order priceorderId: string- Order ID to cancel
Returns: Promise<ContractTransactionResponse>
deleteBuyOrder
Cancel a buy order.
Signature:
async deleteBuyOrder(
tokenA: string,
tokenB: string,
price: bigint,
orderId: string
): Promise<ContractTransactionResponse>Parameters:
tokenA: string- First token addresstokenB: string- Second token addressprice: bigint- Order priceorderId: string- Order ID to cancel
Returns: Promise<ContractTransactionResponse>
deleteSellOrderForMain
Cancel a sell order for a specific main account (via subaccount).
Signature:
async deleteSellOrderForMain(
tokenA: string,
tokenB: string,
main: string,
price: bigint,
orderId: string
): Promise<ContractTransactionResponse>Parameters:
tokenA: string- First token addresstokenB: string- Second token addressmain: string- Main account addressprice: bigint- Order priceorderId: string- Order ID to cancel
Returns: Promise<ContractTransactionResponse>
deleteBuyOrderForMain
Cancel a buy order for a specific main account (via subaccount).
Signature:
async deleteBuyOrderForMain(
tokenA: string,
tokenB: string,
main: string,
price: bigint,
orderId: string
): Promise<ContractTransactionResponse>Parameters:
tokenA: string- First token addresstokenB: string- Second token addressmain: string- Main account addressprice: bigint- Order priceorderId: string- Order ID to cancel
Returns: Promise<ContractTransactionResponse>
Fee Management
getBuyerFeePercent
Get the buyer fee percentage for an exchange.
Signature:
async getBuyerFeePercent(exchangeAddress: string): Promise<bigint>Parameters:
exchangeAddress: string- Exchange contract address
Returns: Promise<bigint> - Fee percentage
getSellerFeePercent
Get the seller fee percentage for an exchange.
Signature:
async getSellerFeePercent(exchangeAddress: string): Promise<bigint>Parameters:
exchangeAddress: string- Exchange contract address
Returns: Promise<bigint> - Fee percentage
updateBuyerFeePercent
Update the buyer fee percentage (admin only).
Signature:
async updateBuyerFeePercent(
exchangeAddress: string,
newFeePercent: bigint
): Promise<ContractTransactionResponse>Parameters:
exchangeAddress: string- Exchange contract addressnewFeePercent: bigint- New fee percentage
Returns: Promise<ContractTransactionResponse>
updateSellerFeePercent
Update the seller fee percentage (admin only).
Signature:
async updateSellerFeePercent(
exchangeAddress: string,
newFeePercent: bigint
): Promise<ContractTransactionResponse>Parameters:
exchangeAddress: string- Exchange contract addressnewFeePercent: bigint- New fee percentage
Returns: Promise<ContractTransactionResponse>
getFeeReceiver
Get the fee receiver address for an exchange.
Signature:
async getFeeReceiver(exchangeAddress: string): Promise<string>Parameters:
exchangeAddress: string- Exchange contract address
Returns: Promise<string> - Fee receiver address
updateFeeReceiver
Update the fee receiver address (admin only).
Signature:
async updateFeeReceiver(exchangeAddress: string, newFeeReceiver: string): Promise<any>Parameters:
exchangeAddress: string- Exchange contract addressnewFeeReceiver: string- New fee receiver address
Returns: Promise<any> - Transaction receipt
Volume Management
getMinimumSellVolume
Get the minimum sell volume for an exchange.
Signature:
async getMinimumSellVolume(exchangeAddress: string): Promise<bigint>Parameters:
exchangeAddress: string- Exchange contract address
Returns: Promise<bigint> - Minimum sell volume
getMinimumBuyVolume
Get the minimum buy volume for an exchange.
Signature:
async getMinimumBuyVolume(exchangeAddress: string): Promise<bigint>Parameters:
exchangeAddress: string- Exchange contract address
Returns: Promise<bigint> - Minimum buy volume
updateMinimumSellVolume
Update the minimum sell volume (admin only).
Signature:
async updateMinimumSellVolume(
exchangeAddress: string,
newMinimumSellVolume: bigint
): Promise<ContractTransactionResponse>Parameters:
exchangeAddress: string- Exchange contract addressnewMinimumSellVolume: bigint- New minimum sell volume
Returns: Promise<ContractTransactionResponse>
updateMinimumBuyVolume
Update the minimum buy volume (admin only).
Signature:
async updateMinimumBuyVolume(
exchangeAddress: string,
newMinimumBuyVolume: bigint
): Promise<ContractTransactionResponse>Parameters:
exchangeAddress: string- Exchange contract addressnewMinimumBuyVolume: bigint- New minimum buy volume
Returns: Promise<ContractTransactionResponse>
Price Management
getPriceStep
Get the price step (tick size) for an exchange.
Signature:
async getPriceStep(exchangeAddress: string): Promise<bigint>Parameters:
exchangeAddress: string- Exchange contract address
Returns: Promise<bigint> - Price step value
setPriceStep
Set the price step (tick size) for an exchange (admin only).
Signature:
async setPriceStep(exchangeAddress: string, priceStep: bigint): Promise<ContractTransactionResponse>Parameters:
exchangeAddress: string- Exchange contract addresspriceStep: bigint- New price step value
Returns: Promise<ContractTransactionResponse>
Order Book Queries
getLimitedOrders
Get a limited number of orders from the order book.
Signature:
async getLimitedOrders(
tokenA: string,
tokenB: string,
maxOrders: number,
isBuyOrders: boolean
): Promise<{orders: any[], prices: string[]}>Parameters:
tokenA: string- First token addresstokenB: string- Second token addressmaxOrders: number- Maximum number of orders to fetchisBuyOrders: boolean- true for buy orders, false for sell orders
Returns: Promise<{orders: any[], prices: string[]}>
getOutputAmount
Get the output amount for a given input amount and token.
Signature:
async getOutputAmount(
tokenA: string,
tokenB: string,
inputToken: string,
inputAmount: bigint
): Promise<{outputAmount: string, price: string}>Parameters:
tokenA: string- First token addresstokenB: string- Second token addressinputToken: string- Input token addressinputAmount: bigint- Input amount
Returns: Promise<{outputAmount: string, price: string}>
getActiveBuyOrders
Get all active buy orders for the connected wallet.
Signature:
async getActiveBuyOrders(tokenA: string, tokenB: string): Promise<any[]>Parameters:
tokenA: string- First token addresstokenB: string- Second token address
Returns: Promise<any[]> - Array of buy order objects
getActiveSellOrders
Get all active sell orders for the connected wallet.
Signature:
async getActiveSellOrders(tokenA: string, tokenB: string): Promise<any[]>Parameters:
tokenA: string- First token addresstokenB: string- Second token address
Returns: Promise<any[]> - Array of sell order objects
getActiveBuyOrdersForUser
Get all active buy orders for a specific user.
Signature:
async getActiveBuyOrdersForUser(tokenA: string, tokenB: string, user: string): Promise<any[]>Parameters:
tokenA: string- First token addresstokenB: string- Second token addressuser: string- User address
Returns: Promise<any[]> - Array of buy order objects
getActiveSellOrdersForUser
Get all active sell orders for a specific user.
Signature:
async getActiveSellOrdersForUser(tokenA: string, tokenB: string, user: string): Promise<any[]>Parameters:
tokenA: string- First token addresstokenB: string- Second token addressuser: string- User address
Returns: Promise<any[]> - Array of sell order objects
balanceOf
Get token balances for the connected wallet.
Signature:
async balanceOf(tokenAAddress: string, tokenBAddress: string): Promise<{ tokenA: bigint; tokenB: bigint }>Parameters:
tokenAAddress: string- First token addresstokenBAddress: string- Second token address
Returns: Promise<{ tokenA: bigint; tokenB: bigint }>

