Integration Guide
Creating a Limit Order
To create a limit order programmatically, follow these steps:
- Approve Token Spending: User must approve the
LimitOrderEscrowcontract to spend the input token. - Call createOrder: Submit the order with all required parameters.
- 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:
- Order Status Check: Ensure order is active or partially filled and not expired.
- Price Validation: Verify current market price meets the order’s limit price criteria.
- Slippage Protection: Confirm output amount meets minimum requirements.
- 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.