Skip to content

Commit 70694ae

Browse files
authored
Merge pull request #149 from dcernahoschi/concentrated_liq_dex
Initial commit for mojitoswap
2 parents 2d18f5e + 728b6fa commit 70694ae

File tree

9 files changed

+2379
-0
lines changed

9 files changed

+2379
-0
lines changed
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
max_width = 140

7-defi-devpost/mojitoswap/Cargo.toml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[package]
2+
name = "mojitoswap"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "db963f1"}
8+
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "db963f1"}
9+
lazy_static = "1.4.0"
10+
11+
[dev-dependencies]
12+
radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "db963f1"}
13+
transaction = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "db963f1"}
14+
scrypto-unit = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "db963f1"}
15+
regex = "1"
16+
17+
[profile.release]
18+
opt-level = 's' # Optimize for size.
19+
lto = true # Enable Link Time Optimization.
20+
codegen-units = 1 # Reduce number of codegen units to increase optimizations.
21+
panic = 'abort' # Abort on panic.
22+
strip = "debuginfo" # Strip debug info.
23+
overflow-checks = true # Panic in the case of an overflow.
24+
25+
[lib]
26+
crate-type = ["cdylib", "lib"]

7-defi-devpost/mojitoswap/README.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# MojitoSwap
2+
3+
MojitoSwap is a concentrated liquidity pool inspired by the [Uniswap v3 whitepaper](https://uniswap.org/whitepaper-v3.pdf). This whitepaper explains quite well how concentrated liquidity pools work. Thus it represents a prerequiste to read before diving into the MojitoSwap implementation. Implementation details are provided though as comments in the code.
4+
5+
## Contents
6+
For now, the MojitoSwap repository contains only the scrypto blueprint implementation, without the Oracle functionality described in the whitepaper. The blueprint represents a pool of 2 fungible resources and provides the following operations:
7+
- Add a new liquidity position to the pool
8+
- Remove a liquidity position a LP holds
9+
- Add more liquidity to a position a LP already holds
10+
- Collect the fees for a position a LP holds
11+
- Add position fees to liquidity
12+
- Add positions acting as limit orders (implicit)
13+
14+
## How to run and test the component
15+
The repository contains an utility that allows to define test scenarios as integration tests. These tests scenarios create transaction manifests that are against the component. For example, a test scenario that can be created (tests/pool.rs file) looks as following:
16+
17+
/**
18+
* Limit order & multiple range swap.
19+
*
20+
* Given a pool with fee=0, sqrt_price=1 and a position=[10000 MOJ + 10000 USDT, -1000, 1000]
21+
*
22+
* Test that if:
23+
* - an account adds a position=[1000 MOJ, 199, 200], this can act as limit order saying: sell 1000MOJ at price 1.02.
24+
* The range [199, 200] is corresponding to sqrt_price range [1.009999163397141405, 1.010049662092875426] which
25+
* aproximates a price of 1.02
26+
* - the price moves past the position range
27+
* - the account holding the limit order position remove it
28+
*
29+
* Then the account gets the expected amount of USDT: ~1020 USDT
30+
*/
31+
#[test]
32+
fn scenario_6() {
33+
let mut context = Context::new(Decimal::zero(), Decimal::one());
34+
let account = context.new_account_with_moj_and_usdt(dec!("20000"), dec!("20000"));
35+
context.add_position(&account, dec!("10000"), dec!("10000"), -1000, 1000);
36+
37+
let account2 = context.new_account_with_moj_and_usdt(dec!("20000"), dec!("20000"));
38+
context.add_position(&account2, dec!("1000"), Decimal::zero(), 199, 200);
39+
40+
context.swap_usdt_for_moj(&account, dec!("8000"), dec!("7750.081465536550594191"));
41+
context.remove_pos(&account2, Decimal::zero(), dec!("1020.149313703371480602"));
42+
}
43+
44+
After the test scenario is created, it can be run as a scrypto/cargo test from the project directory:
45+
46+
scrypto test - scenario_6 -- --nocapture
47+
48+
Look in tests/pool.rs for more scenarios and documentation on the test utility methods.
49+
50+
The component also displays a detailed debug log for each executed transaction. It has also some unit tests for the math computations with explanations of the formulas.
51+
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use std::collections::BTreeSet;
2+
use std::ops::Bound::*;
3+
4+
pub fn previous_elem<T: Ord>(tree: &BTreeSet<T>, val: T) -> Option<&T> {
5+
tree.range((Unbounded, Excluded(val))).next_back()
6+
}
7+
8+
pub fn next_elem<T: Ord>(tree: &BTreeSet<T>, val: T) -> Option<&T> {
9+
tree.range((Excluded(val), Unbounded)).next()
10+
}
11+
12+
#[cfg(test)]
13+
mod tests {
14+
use super::*;
15+
#[test]
16+
fn previous_elem_retrieval() {
17+
let mut btree_set = BTreeSet::new();
18+
19+
btree_set.insert(32);
20+
btree_set.insert(40);
21+
btree_set.insert(18);
22+
23+
assert_eq!(
24+
previous_elem(&btree_set, 39),
25+
Option::from(&32)
26+
);
27+
assert_eq!(previous_elem(&btree_set, 18), None);
28+
assert_eq!(
29+
previous_elem(&btree_set, 44),
30+
Option::from(&40)
31+
);
32+
assert_eq!(
33+
previous_elem(&btree_set, 32),
34+
Option::from(&18)
35+
);
36+
}
37+
38+
#[test]
39+
fn next_elem_retrieval() {
40+
let mut btree_set = BTreeSet::new();
41+
42+
btree_set.insert(32);
43+
btree_set.insert(40);
44+
btree_set.insert(18);
45+
46+
assert_eq!(next_elem(&btree_set, 21), Option::from(&32));
47+
assert_eq!(next_elem(&btree_set, 32), Option::from(&40));
48+
assert_eq!(next_elem(&btree_set, 15), Option::from(&18));
49+
assert_eq!(next_elem(&btree_set, 40), None);
50+
}
51+
}

7-defi-devpost/mojitoswap/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[macro_use]
2+
extern crate lazy_static;
3+
4+
mod btree_set_ext;
5+
pub mod pool;
6+
pub mod tick_math;
7+
pub mod pool_math;

0 commit comments

Comments
 (0)