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

0x29BF83fF2bb9E0c5C273b0B118C465D6c602C173

CLOBPoolManager

0x9C6B7292a52ea1Ca2A19B45E041d8653A4aD534f

CLOBBalanceManager

0xE75E8b4861D9cC5Da3bbF6C7f280A8E163dD8554

CLOBRouter

The main entry point for CLOB trading operations.

Order Placement

FunctionDescription
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

FunctionDescription
cancelOrder(pool, orderId)Cancel a single order
cancelBatchOrders(pool, orderIds)Cancel multiple orders

Swap Functions

FunctionDescription
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

FunctionReturnsDescription
getQuote(baseCurrency, quoteCurrency, orderType, side, quantity, price)QuoteResultGet quote for order
getQuoteByQuoteAmount(baseCurrency, quoteCurrency, side, quoteAmount)QuoteResultGet quote by quote amount
getBatchQuote(orders)QuoteResult[]Get quotes for batch orders
QuoteResult struct:
  • expectedAmountOut - Expected output amount
  • totalQuoteAmount - Total quote currency amount
  • executionPrice - Execution price
  • avgExecutionPrice - Average execution price
  • priceImpact - Price impact percentage
  • fee - Trading fee
  • executable - Whether order can execute
  • message - Status message

Order Information

FunctionReturnsDescription
getOrder(baseCurrency, quoteCurrency, orderId)OrderGet order details
getBestPrice(baseCurrency, quoteCurrency, side)PriceVolumeGet 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

FunctionReturnsDescription
getWETH()addressGet WETH contract address
getMaxBatchSize()uint256Get maximum batch size
owner()addressContract owner

Events

EventDescription
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

ErrorDescription
FillOrKillNotFulfilled(filledAmount, requestedAmount)FOK order not fully filled
InsufficientBalance(requiredDeposit, userBalance)Insufficient balance
InvalidPrice(price)Invalid price
InvalidPriceIncrementPrice not on valid tick
InvalidQuantityInvalid quantity
InvalidQuantityIncrementQuantity not on valid increment
MaxOrdersExceeded(totalOrders, maxOrders)Max orders exceeded
OrderNotFoundOrder not found
OrderTooLarge(amount, maxAmount)Order exceeds max size
OrderTooSmall(amount, minAmount)Order below min size
PostOnlyWouldTakePost-only order would cross
SlippageExceeded(requestedPrice, limitPrice)Slippage exceeded
SlippageTooHigh(received, minReceived)Slippage too high
TradingPausedTrading is paused
UnauthorizedCancellationNot authorized to cancel

CLOBBalanceManager

Manages user balances and fund locking for CLOB trading.

Deposit Functions

FunctionDescription
deposit(currency, amount, sender, user)Deposit tokens to account
depositAndLock(currency, amount, user, orderBook)Deposit and lock for trading

Withdrawal Functions

FunctionDescription
withdraw(currency, amount)Withdraw tokens
withdraw(currency, amount, user)Withdraw tokens for user

Lock Functions

FunctionDescription
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

FunctionDescription
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

FunctionReturnsDescription
getBalance(user, currency)uint256Get user balance
getLockedBalance(user, operator, currency)uint256Get locked balance
feeMaker()uint256Maker fee rate
feeTaker()uint256Taker fee rate
feeReceiver()addressFee receiver address
getFeeUnit()uint256Fee unit denominator

Events

EventDescription
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

ErrorDescription
InsufficientBalance(user, id, want, have)Insufficient balance
UnauthorizedCaller(caller)Unauthorized caller
UnauthorizedOperator(operator)Unauthorized operator
ZeroAmountZero amount not allowed
TransferError(user, currency, amount)Transfer failed
NativeTransferFailedNative token transfer failed

OrderBook

Individual order book contract for each trading pair.

Order Functions

FunctionDescription
placeOrder(price, quantity, side, user, timeInForce)Place limit order
placeMarketOrder(quantity, side, user)Place market order
cancelOrder(orderId, user)Cancel order

View Functions

FunctionReturnsDescription
getOrder(orderId)OrderGet order details
getBestPrice(side)PriceVolumeGet best price for side
getNextBestPrices(side, price, count)PriceVolume[]Get next prices
getOrderQueue(side, price)(orderCount, totalVolume)Get queue at price
getTradingRules()TradingRulesGet trading rules
Order struct:
  • user - Order owner
  • id - Order ID
  • quantity - Order quantity
  • filled - Filled amount
  • price - Order price
  • expiry - Expiration timestamp
  • status - Order status
  • orderType - Order type
  • side - Buy/Sell side
TradingRules struct:
  • minTradeAmount - Minimum trade amount
  • minAmountMovement - Minimum amount increment
  • minPriceMovement - Minimum price tick
  • minOrderSize - Minimum order size

Enums

Side:
  • 0 = Buy
  • 1 = Sell
OrderType:
  • 0 = Limit
  • 1 = Market
TimeInForce:
  • 0 = GTC (Good Till Cancel)
  • 1 = IOC (Immediate or Cancel)
  • 2 = FOK (Fill or Kill)
  • 3 = PostOnly
Status:
  • 0 = Open
  • 1 = PartiallyFilled
  • 2 = Filled
  • 3 = Cancelled

Events

EventDescription
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