This repository contains a decentralized order book implementation written in Clarity for the Stacks blockchain. The smart contract enables secure, transparent peer-to-peer trading of assets with on-chain order matching and settlement.
- Limit orders: Place buy or sell orders at a specific price
- Market orders: Execute trades at the best available price
- Order cancellation: Users can cancel their open orders
- Efficient matching algorithm: Optimized for gas consumption
- Transparent and decentralized: All trades are settled on-chain
The main contract file is contracts/order-book.clar
. It implements the core functionality of the order book, including:
- Order placement
- Order matching
- Order cancellation
- Market price calculation
To interact with this smart contract, you'll need:
- Clarinet: A Clarity runtime packaged as a command-line tool
- Stacks Wallet: To interact with the Stacks blockchain
-
Clone this repository:
git clone https://github.com/walterthesmart/OrderBook.git cd OrderBook
-
Install Clarinet following the official documentation.
-
Initialize the Clarinet project (if not already done):
clarinet init
-
Deploy the contract to the Stacks testnet or mainnet using Clarinet.
To place a limit order, call the place-limit-order
function with the following parameters:
side
: "buy" or "sell"price
: The price per unitamount
: The amount of the asset to trade
Example:
(contract-call? .order-book place-limit-order "buy" u100000000 u5)
To cancel an existing order, use the cancel-order
function with the order ID:
(contract-call? .order-book cancel-order u1)
To view the current state of the order book, you can call various read-only functions such as get-buy-orders
and get-sell-orders
.
Run the included test suite using Clarinet:
clarinet test
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
This smart contract is provided as-is. Users should perform their own security audits before using it in a production environment.
For questions or feedback, please open an issue on this GitHub repository.