Skip to content

Commit bb4362e

Browse files
authored
Merge pull request #5 from musitdev/main
Yet another order book.
2 parents 713939b + 029422e commit bb4362e

File tree

10 files changed

+1556
-0
lines changed

10 files changed

+1556
-0
lines changed

1-exchanges/orderbook/Cargo.toml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "orderbook"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.3.0" }
8+
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.3.0" }
9+
10+
[dev-dependencies]
11+
radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.3.0" }
12+
13+
[profile.release]
14+
opt-level = 's' # Optimize for size.
15+
lto = true # Enable Link Time Optimization.
16+
codegen-units = 1 # Reduce number of codegen units to increase optimizations.
17+
panic = 'abort' # Abort on panic.
18+
19+
[lib]
20+
crate-type = ["cdylib", "lib"]

1-exchanges/orderbook/README.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Order book
2+
This is a Serum Dex inspired Order Book.
3+
Trade are done on a market.
4+
A market allow the transfer of assert between a quote and a base. Quote and Base can be any token. They are declared during the market creation using the `instantiate_market` method.
5+
6+
## Trading using order
7+
Trader push Bid(Buy base using quote) or Ask(sell base to quote) order to the Dex.
8+
Pushed order depending on their type are matched for all or part of its amount depending on the opposite side order found.
9+
For bid order if a ask price with less than the limit price is already present in the orderbook, it's matched and fund are transferred.
10+
For ask order if a bid price is greater or equals to the limit price is found, it's matched.
11+
Bid order define a maximum limit price and Ask order a minimum price to match the order.
12+
13+
## Trader Vault
14+
To push order, a trader must create a trader order vault with the `create_openorders` method.
15+
This method create a quote and base vault to store asset transferred between order.
16+
To get back the asset, the trader can use the `withdraw` method.
17+
18+
## Push order
19+
To push a bid order, use the method `bid_order` with the max limit price, amount to transfer, the order type, add quote asset to use for the transfer.
20+
The amount of quote asset must be equals or more that the limit price * with the amount to be sure that there is enough quote for the transfer.
21+
The provided quote are locked and can't be withdrew while the order is pending.
22+
To push a ask order, use `ask_order` with the min acceptable price, amount to transfer, the order type, add base asset to use for the transfer.
23+
24+
With the transfer method, the badge created with `create_openorders` must be provided to identify the orders owner.
25+
26+
After the order is pushed, the call return the order id that can be use to cancel it.
27+
28+
Order type can be:
29+
* limit: 0 when the order is push the maximum amount of quote is transferred depending on the available opposite order in the book. If some amount can't be matched, the remaining is added to the order book.
30+
* Immediate or Cancel: 1 same as limit but if a remaining amount can't be matched, it's cancelled.
31+
* Post: 2 the order is not matched an immediately added to the order book. It can be useful to decrease the fee (see Fee management).
32+
33+
## Withdraw
34+
To get back all transferred asset from badge owner vault. Locked quote for pending bid order can be retrieve without cancelling the orders.
35+
36+
## Cancel
37+
The method `cancel_order`, cancel a pending order with its id. Matched order can be cancelled. Quote locked by pending bid order are unlocked. Cancelled asset are return to the vault associated to the badge provided with the method call.
38+
39+
## Fee
40+
Fee are withdraw from transferred asset to the market vault. When the market is created the quote and base vault is created to store fee taken from matched orders.
41+
Order added that match other order in the book are call taker order and order that are matched from the book are call maker order.
42+
Maker and taker order has different fee and often maker order are less.
43+
In this example, taker order is 10% and maker order 5%.
44+
45+
# Test
46+
To test at the root of the project use the cmd: `resim publish .` and `cargo test`
47+
48+
Transaction manifest are in progress.

1-exchanges/orderbook/create.rtm

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CALL_METHOD Address("02b1613251d3afc21a8c50be7b3a6e41240dfeef1f6c756f75ecab") "create_openorders";
2+
CALL_METHOD_WITH_ALL_RESOURCES Address("0293c502780e23621475989d707cd8128e4506362e5fed6ac0c00a") "deposit_batch";

1-exchanges/orderbook/inst.rtm

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CLONE_BUCKET_REF BucketRef(1u32) BucketRef("badge1");
2+
CALL_METHOD Address("0293c502780e23621475989d707cd8128e4506362e5fed6ac0c00a") "withdraw" Decimal("1000") Address("030000000000000000000000000000000000000000000000000004") BucketRef("badge1");
3+
TAKE_FROM_WORKTOP Decimal("1000") Address("030000000000000000000000000000000000000000000000000004") Bucket("bucket1");
4+
CLONE_BUCKET_REF BucketRef(1u32) BucketRef("badge2");
5+
CALL_METHOD Address("0293c502780e23621475989d707cd8128e4506362e5fed6ac0c00a") "withdraw" Decimal("1000") Address("030000000000000000000000000000000000000000000000000004") BucketRef("badge2");
6+
TAKE_FROM_WORKTOP Decimal("1000") Address("030000000000000000000000000000000000000000000000000004") Bucket("bucket2");
7+
CALL_FUNCTION Address("0178834db3770a171ba038b512d96c7ad3b60898bb49f4360719f6") "Market" "instantiate_market" Bucket("bucket1") Bucket("bucket2") "testmarket";
8+
CALL_METHOD_WITH_ALL_RESOURCES Address("0293c502780e23621475989d707cd8128e4506362e5fed6ac0c00a") "deposit_batch";

0 commit comments

Comments
 (0)