Skip to main content

Integration Guide

Creating a Limit Order

To create a limit order programmatically, follow these steps:

  1. Approve Token Spending: User must approve the LimitOrderEscrow contract to spend the input token.
  2. Call createOrder: Submit the order with all required parameters.
  3. Monitor Order Status: Track order execution through events and view functions.

Example: Creating a SELL Order

// Approve token spending
await tokenContract.approve(limitOrderEscrow.address, amountIn);

// Create order
await limitOrderEscrow.createOrder(
WETH_ADDRESS, // tokenIn (volatile token)
USDC_ADDRESS, // tokenOut (stable token)
ethers.parseEther("1"), // amountIn (1 WETH)
ethers.parseUnits("3000", 6), // minAmountOut (3000 USDC)
ethers.parseUnits("3100", 18),// limitPrice (normalized to 18 decimals)
deadline, // Unix timestamp
PartialFillMode.Split5, // Allow 5 partial fills
OrderType.SELL // SELL order
);

Executing Orders (Bot Integration)

Authorized bots can execute orders when market conditions are favorable. The execution process includes comprehensive validation:

  1. Order Status Check: Ensure order is active or partially filled and not expired.
  2. Price Validation: Verify current market price meets the order’s limit price criteria.
  3. Slippage Protection: Confirm output amount meets minimum requirements.
  4. Partial Fill Logic: Calculate appropriate fill amount based on order’s fill mode.

Best Practices for Bots

  • Price Monitoring: Implement efficient price feeds to detect execution opportunities quickly.
  • Gas Optimization: Batch multiple order executions when profitable to amortize gas costs.
  • Profitability Analysis: Calculate gas costs vs bot fees to ensure executions are economically viable.