CLOB Contracts
The CLOB contracts implement a Central Limit Order Book for the MuchFi protocol, enabling efficient order matching and trade execution.Contract Addresses
CLOBRouter
CLOBPoolManager
CLOBBalanceManager
CLOBRouter
The main entry point for CLOB trading operations.Order Placement
| Function | Description |
|---|---|
placeOrder(pool, price, quantity, side, user) | Place a limit order |
placeOrderWithDeposit(pool, price, quantity, side, user) | Place order with token deposit |
placeOrderWithDepositETH(pool, price, quantity, side) | Place order with ETH deposit (payable) |
placeMarketOrder(pool, quantity, side, user, minAmountOut) | Place a market order |
placeMarketOrderWithDeposit(pool, quantity, side, user, minAmountOut) | Market order with deposit |
placeMarketOrderWithDepositETH(pool, quantity, side, minAmountOut) | Market order with ETH (payable) |
placeBatchOrders(orders, atomic) | Place multiple orders in batch |
Order Cancellation
| Function | Description |
|---|---|
cancelOrder(pool, orderId) | Cancel a single order |
cancelBatchOrders(pool, orderIds) | Cancel multiple orders |
Swap Functions
| Function | Description |
|---|---|
swap(srcCurrency, dstCurrency, srcAmount, minDstAmount, maxHops, user) | Execute token swap |
swapETHForTokens(dstCurrency, minDstAmount, maxHops) | Swap ETH for tokens (payable) |
swapTokensForETH(srcCurrency, srcAmount, minETHOut, maxHops) | Swap tokens for ETH |
Quote Functions
| Function | Returns | Description |
|---|---|---|
getQuote(baseCurrency, quoteCurrency, orderType, side, quantity, price) | QuoteResult | Get quote for order |
getQuoteByQuoteAmount(baseCurrency, quoteCurrency, side, quoteAmount) | QuoteResult | Get quote by quote amount |
getBatchQuote(orders) | QuoteResult[] | Get quotes for batch orders |
expectedAmountOut- Expected output amounttotalQuoteAmount- Total quote currency amountexecutionPrice- Execution priceavgExecutionPrice- Average execution pricepriceImpact- Price impact percentagefee- Trading feeexecutable- Whether order can executemessage- Status message
Order Information
| Function | Returns | Description |
|---|---|---|
getOrder(baseCurrency, quoteCurrency, orderId) | Order | Get order details |
getBestPrice(baseCurrency, quoteCurrency, side) | PriceVolume | Get best bid/ask price |
getNextBestPrices(pool, side, price, count) | PriceVolume[] | Get next best prices |
getOrderQueue(baseCurrency, quoteCurrency, side, price) | (orderCount, totalVolume) | Get order queue info |
calculateOrderAmounts(pool, price, quantity, side, orderType) | (orderAmount, quoteAmount) | Calculate order amounts |
View Functions
| Function | Returns | Description |
|---|---|---|
getWETH() | address | Get WETH contract address |
getMaxBatchSize() | uint256 | Get maximum batch size |
owner() | address | Contract owner |
Events
| Event | Description |
|---|---|
BatchOrderPlaced(orderId, batchIndex, success, message) | Batch order placed |
BatchOrderCancelled(orderId, batchIndex, success, message) | Batch order cancelled |
Initialized(version) | Contract initialized |
OwnershipTransferred(previousOwner, newOwner) | Ownership transferred |
Errors
| Error | Description |
|---|---|
FillOrKillNotFulfilled(filledAmount, requestedAmount) | FOK order not fully filled |
InsufficientBalance(requiredDeposit, userBalance) | Insufficient balance |
InvalidPrice(price) | Invalid price |
InvalidPriceIncrement | Price not on valid tick |
InvalidQuantity | Invalid quantity |
InvalidQuantityIncrement | Quantity not on valid increment |
MaxOrdersExceeded(totalOrders, maxOrders) | Max orders exceeded |
OrderNotFound | Order not found |
OrderTooLarge(amount, maxAmount) | Order exceeds max size |
OrderTooSmall(amount, minAmount) | Order below min size |
PostOnlyWouldTake | Post-only order would cross |
SlippageExceeded(requestedPrice, limitPrice) | Slippage exceeded |
SlippageTooHigh(received, minReceived) | Slippage too high |
TradingPaused | Trading is paused |
UnauthorizedCancellation | Not authorized to cancel |
CLOBBalanceManager
Manages user balances and fund locking for CLOB trading.Deposit Functions
| Function | Description |
|---|---|
deposit(currency, amount, sender, user) | Deposit tokens to account |
depositAndLock(currency, amount, user, orderBook) | Deposit and lock for trading |
Withdrawal Functions
| Function | Description |
|---|---|
withdraw(currency, amount) | Withdraw tokens |
withdraw(currency, amount, user) | Withdraw tokens for user |
Lock Functions
| Function | Description |
|---|---|
lock(user, currency, amount) | Lock funds for order |
lock(user, currency, amount, orderBook) | Lock funds for specific orderbook |
unlock(user, currency, amount) | Unlock funds after cancellation |
Transfer Functions
| Function | Description |
|---|---|
transferFrom(sender, receiver, currency, amount) | Transfer between accounts |
transferLockedFrom(sender, receiver, currency, amount) | Transfer locked funds |
transferOut(sender, receiver, currency, amount) | Transfer out of system |
View Functions
| Function | Returns | Description |
|---|---|---|
getBalance(user, currency) | uint256 | Get user balance |
getLockedBalance(user, operator, currency) | uint256 | Get locked balance |
feeMaker() | uint256 | Maker fee rate |
feeTaker() | uint256 | Taker fee rate |
feeReceiver() | address | Fee receiver address |
getFeeUnit() | uint256 | Fee unit denominator |
Events
| Event | Description |
|---|---|
Deposit(user, id, amount) | Funds deposited |
Withdrawal(user, id, amount) | Funds withdrawn |
Lock(user, id, amount) | Funds locked |
Unlock(user, id, amount) | Funds unlocked |
TransferFrom(operator, sender, receiver, id, amount, feeAmount) | Internal transfer |
TransferLockedFrom(operator, sender, receiver, id, amount, feeAmount) | Locked transfer |
Errors
| Error | Description |
|---|---|
InsufficientBalance(user, id, want, have) | Insufficient balance |
UnauthorizedCaller(caller) | Unauthorized caller |
UnauthorizedOperator(operator) | Unauthorized operator |
ZeroAmount | Zero amount not allowed |
TransferError(user, currency, amount) | Transfer failed |
NativeTransferFailed | Native token transfer failed |
OrderBook
Individual order book contract for each trading pair.Order Functions
| Function | Description |
|---|---|
placeOrder(price, quantity, side, user, timeInForce) | Place limit order |
placeMarketOrder(quantity, side, user) | Place market order |
cancelOrder(orderId, user) | Cancel order |
View Functions
| Function | Returns | Description |
|---|---|---|
getOrder(orderId) | Order | Get order details |
getBestPrice(side) | PriceVolume | Get best price for side |
getNextBestPrices(side, price, count) | PriceVolume[] | Get next prices |
getOrderQueue(side, price) | (orderCount, totalVolume) | Get queue at price |
getTradingRules() | TradingRules | Get trading rules |
user- Order ownerid- Order IDquantity- Order quantityfilled- Filled amountprice- Order priceexpiry- Expiration timestampstatus- Order statusorderType- Order typeside- Buy/Sell side
minTradeAmount- Minimum trade amountminAmountMovement- Minimum amount incrementminPriceMovement- Minimum price tickminOrderSize- Minimum order size
Enums
Side:0= Buy1= Sell
0= Limit1= Market
0= GTC (Good Till Cancel)1= IOC (Immediate or Cancel)2= FOK (Fill or Kill)3= PostOnly
0= Open1= PartiallyFilled2= Filled3= Cancelled
Events
| Event | Description |
|---|---|
OrderPlaced(orderId, user, side, price, quantity, expiry, isMarketOrder, status) | Order placed |
OrderCancelled(orderId, user, timestamp, status) | Order cancelled |
OrderMatched(user, buyOrderId, sellOrderId, side, timestamp, executionPrice, executedQuantity) | Orders matched |
UpdateOrder(orderId, timestamp, filled, status) | Order updated |