1 unstable release
0.1.0 | Feb 8, 2025 |
---|
#985 in Magic Beans
128 downloads per month
10KB
127 lines
Protobook
Protobook is a Solana smart contract for issuing public, timebound orders to buy a given token at a fixed price. It allows anyone to securely execute a one-to-many swap with willing public liquidity, thereby serving as a generic and permissionless orderbook protocol.
Accounts
Order
– An order is a public, timebound offer to buy a given token at a fixed price.Receipt
– A receipt tracks a deposit to fill an order.
Instructions
Cancel
– Cancels an order immediately.Close
– Closes an order account.Collect
– Collects tokens from an expired order.Fill
– Fills an order.Open
– Opens an order.Redeem
– Redeems a receipt for an expired order.
How it works
A user can open an order by specifying the token they want to buy, the amount they want to buy, the token they want to sell, the amount they want to sell, and then locking the tokens they wish to sell in an escrow vault. Any user then can fill (or partially fill) the order by depositing the requested tokens into the escrow vault and receiving a receipt to track the deposit.
When an order expires, its owner can collect either the tokens they wished to buy if the order was filled, or reclaim their original deposit if the order was not filled. During collection, an optional fee can be sent to a fee collector if the order was filled. Receipt holders can redeem their receipts to receive either the tokens offered if the order was filled, or their original deposit if the order was not filled. Once all tokens have been withdrawn from the escrow vaults, the order account can be closed and rent returned to its owner.
Discussion
Protobook assumes all order matching happens offchain. It provides only a system for issuing and managing swap orders. These orders can represent one-off OTC swaps between private parties or one-to-many swaps on a public exchange. An orderbook UI and trading bots can be readibly be built on the Protobook data structure with full support for limit orders and immediate order cancellation. In short, Protobook puts the orders onchain and builds the "book" offchain.
A book is simply an index of all open orders for a given market. To create a book, for example, one could filter for all Protobook orders with SOL and USDC mints. Then simply filter out the expired orders, index the orders into price ranges, and display the orders as a book to visualize the bid/ask spread and market depth. For orders batched into the same price range, an exchange can choose to fill orders with the best prices first. By assuming that all order matching can happen offchain, Protobook greatly reduces transaction complexity and cost compared to existing orderbook protocols on Solana.
Get started
Compile your program:
steel build
Run unit and integration tests:
steel test
Dependencies
~16–24MB
~357K SLoC