This efficient order book utilises the BokkyPooBahsRedBlackTreeLibrary
library for sorting prices allowing O(log n)
for tree segment insertion, traversal, and deletion. It supports batch orders and batch cancelling, ERC2981
royalties, and a dev and burn fee on each trade.
It is kept gas efficient by packing data in many areas:
- Four orders (
uint24
quantity +uint40
order id) into a 256bit word giving a 4x improvement compared to using 1 storage slot per order - When taking from the order book no tokens/nfts are transferred. Instead the orderId is stored in a claimable array
- The tokens claimable are packed with 3 orders per storage slot
The order book is kept healthy by requiring a minimum quantity that can be added - partial quantities can still be taken from the order book. Cancelling orders shifts all entries at that price level to remove gaps.
Constraints:
- The order quantity to be added to the book is limited to ~16mil
- The maximum number of orders in the book that can ever be added is limited to 1 trillion
- The maximum number of orders that can be added to a specific price level in its lifetime is 16 billion
- Cannot change tick for a tokenId after it's been set
While this order book was created for ERC1155
NFTs it could be adapted for ERC20
tokens.
Potential improvements:
- TokenInfo could use uint64 for both members and packed 2 in an array if tokenIds are sequential and limited
- Range delete of the red-black tree using split/join
- When cancelling an order some of the shifting logic can be improved to move some orders in segments in 1 go.
To start copy the .env.sample
file to .env
and fill in PRIVATE_KEY
at a minimum (starts with 0x
).
yarn install
# To compile the contracts
yarn compile
# To run the tests
yarn test
# To get code coverage
yarn coverage
# To deploy all contracts
yarn deploy --network <network>
yarn deploy --network fantom_testnet
# Export abi
yarn abi
# To fork or open a node connection
yarn fork
yarn fork --fork <rpc_url>
yarn fork --fork https://rpc.ftm.tools
# To impersonate an account on a forked or local blockchain for debugging
yarn impersonate