Solidity 0.8.15
SPDX-License-Identifier: UNLICENSED
The LimitOrder smart contract provides functionality for placing limit orders within a decentralized exchange environment. It is specifically designed to work with the Uniswap V4 protocol, facilitating the execution of trades at predetermined prices, thus offering users a more traditional trading experience on a decentralized platform.
Please follow these steps, if you all ready have Foundry setup ignore steps 1 and 2
Step 1: Install Rust https://doc.rust-lang.org/book/ch01-01-installation.html
Step 2: Install Foundry https://book.getfoundry.sh/getting-started/installation#using-foundryup
Step 3: Build
Please run the command below to build your contracts
forge build
If you get a stack to deep error try running the command below
forge build --via-ir
Step 4: Test
Please run the command below to test your contracts, the given tests are examples please generate your own.
forge test
- LimitOrder State: Manages the state of limit orders and oversees their lifecycle.
- Order Submission: Enables users to submit limit orders with a specific price and duration.
- Order Execution: Handles the execution of orders when the market price matches the limit order price.
- Price Tracking: Monitors the market price to trigger the execution of limit orders.
- Order Management: Provides functions for creating, updating, and canceling limit orders.
- Order Queue: Organizes orders in a priority queue based on prices and timestamps.
- Leverages Uniswap V4-core libraries for price calculations and state management.
- Integrates with Uniswap V4 oracles for accurate, decentralized price feeds.
- OrderPlaced: Logs the details when a new limit order is placed.
- OrderExecuted: Logs the details when a limit order is executed.
- OrderCancelled: Logs the details when a limit order is cancelled.
- onlyOwner: Restricts certain functions to be called by the contract owner only.
- placeOrder: Allows a user to place a new limit order.
- cancelOrder: Enables a user to cancel an existing limit order.
- executeOrder: Executed by the contract when a market price meets a limit order price.
- Throws errors for conditions such as invalid order prices, insufficient balances, and unauthorized cancellations.
- Users must understand the mechanics of limit orders and the associated risks.
- Interacting with the contract typically requires a user interface that supports limit order functionality.
- To be deployed on an Ethereum-compatible blockchain with access to Uniswap V4 pools.
- Requires linking to a specific pool and setting appropriate permissions.
Within the Uniswap V4 Hooks
library, the LimitOrder contract leverages the following hooks where the flags are set to true
in the getHooksCalls
function:
afterInitialize
: Engaged after the pool is initialized, allowing the LimitOrder contract to perform setup actions post-initialization.afterSwap
: Engaged after a swap has occurred, enabling the LimitOrder contract to check for any limit orders that can be executed based on the new market price.