Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: AMM #8644

Closed
wants to merge 67 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
cb72ad4
feat: DEX draft
benesjan Sep 19, 2024
3750f6a
WIP
benesjan Sep 20, 2024
e5d9f81
WIP
benesjan Sep 20, 2024
2109f23
WIP
benesjan Sep 20, 2024
3c6e17a
WIP
benesjan Sep 20, 2024
1d29890
WIP
benesjan Sep 20, 2024
408f0c9
WIP
benesjan Sep 20, 2024
6a65781
WIP
benesjan Sep 20, 2024
873894e
WIP
benesjan Sep 23, 2024
687906d
WIP
benesjan Sep 23, 2024
f510b6b
WIP
benesjan Sep 23, 2024
0245ab3
WIP
benesjan Sep 23, 2024
0c0d17a
locks
benesjan Sep 23, 2024
70d6d42
WIP
benesjan Sep 23, 2024
f7916b6
WIP
benesjan Sep 23, 2024
998ae1a
WIP
benesjan Sep 23, 2024
ba8afcf
WIP
benesjan Sep 23, 2024
7c39579
Nuking unnecessary Reserves optimization
benesjan Sep 23, 2024
59348ed
WIP
benesjan Sep 24, 2024
98c212e
WIP
benesjan Sep 24, 2024
766fef7
WIP
benesjan Sep 24, 2024
0d08d41
cleanup
benesjan Sep 24, 2024
4edb48a
WIP
benesjan Sep 24, 2024
cb6bf20
WIP
benesjan Sep 24, 2024
9279f0b
fmt
benesjan Sep 24, 2024
f2d9677
commenting out code
benesjan Sep 24, 2024
0e74791
Revert "commenting out code"
benesjan Sep 24, 2024
a550c06
WIP
benesjan Sep 24, 2024
df5da33
WIP
benesjan Oct 7, 2024
63ce890
WIP
benesjan Oct 7, 2024
5376993
WIP
benesjan Oct 7, 2024
91a6457
WIP
benesjan Oct 9, 2024
8b9a0e2
WIP
benesjan Oct 9, 2024
93f172a
WIP
benesjan Oct 9, 2024
deb3a5c
WIP
benesjan Oct 9, 2024
42b9ebd
WIP
benesjan Oct 9, 2024
fff6530
WIP
benesjan Oct 9, 2024
458094a
WIP
benesjan Oct 9, 2024
3bf8443
WIP
benesjan Oct 10, 2024
d8b8d20
WIP
benesjan Oct 10, 2024
6db3508
WIP
benesjan Oct 10, 2024
17a4efe
DEX --> AMM
benesjan Oct 10, 2024
5ee64b7
Apply suggestions from code review
benesjan Oct 28, 2024
9e0eaf5
linking issue
benesjan Oct 28, 2024
59b0025
improved re-entrancy guard comments
benesjan Oct 28, 2024
dad820b
U128 instead of u64
benesjan Oct 28, 2024
f7525fd
fix
benesjan Oct 28, 2024
844ce57
clarifying re-entrancy comment
benesjan Oct 28, 2024
6f9db4a
get_amounts_to_add
benesjan Oct 28, 2024
a569a19
various improvements
benesjan Oct 28, 2024
6973d4c
fix
benesjan Oct 28, 2024
a0cbd37
no sqrt in init liquidity
benesjan Oct 28, 2024
ba4f1bd
fixes
benesjan Oct 30, 2024
ff9a39e
fix: compilation issues
benesjan Oct 30, 2024
1a8223a
WIP
benesjan Oct 31, 2024
c9194c1
WIP
benesjan Oct 31, 2024
4441cbe
fix
benesjan Oct 31, 2024
47ee850
WIP
benesjan Oct 31, 2024
038e7d0
fixes
benesjan Oct 31, 2024
51088f4
WIP
benesjan Oct 31, 2024
eb33795
WIP
benesjan Oct 31, 2024
854f7d6
optimization
benesjan Oct 31, 2024
552fb5e
WIP
benesjan Oct 31, 2024
1810b51
fixes after rebase
benesjan Nov 13, 2024
ee0c86d
fix
benesjan Nov 13, 2024
f55c4c2
WIP on an AMM
benesjan Nov 13, 2024
a404b58
WIP
benesjan Nov 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions noir-projects/noir-contracts/Nargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [
"contracts/amm_contract",
"contracts/app_subscription_contract",
"contracts/auth_contract",
"contracts/auth_registry_contract",
Expand Down
11 changes: 11 additions & 0 deletions noir-projects/noir-contracts/contracts/amm_contract/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "amm_contract"
authors = [""]
compiler_version = ">=0.25.0"
type = "contract"

[dependencies]
aztec = { path = "../../../aztec-nr/aztec" }
token = { path = "../token_contract" }
uint_note = { path = "../../../aztec-nr/uint-note" }
authwit = { path = "../../../aztec-nr/authwit" }
61 changes: 61 additions & 0 deletions noir-projects/noir-contracts/contracts/amm_contract/src/lib.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/// Given some amount of an asset and pair reserves, returns an equivalent amount of the other asset.
/// copy of https://github.com/Uniswap/v2-periphery/blob/0335e8f7e1bd1e8d8329fd300aea2ef2f36dd19f/contracts/libraries/UniswapV2Library.sol#L36
fn get_quote(amountA: U128, reserveA: U128, reserveB: U128) -> U128 {
assert(amountA > U128::zero(), "INSUFFICIENT_AMOUNT");
assert((reserveA > U128::zero()) & (reserveB > U128::zero()), "INSUFFICIENT_LIQUIDITY");
(amountA * reserveB) / reserveA
benesjan marked this conversation as resolved.
Show resolved Hide resolved
}

/// Given an input amount of an asset and pair reserves, returns the maximum output amount of the other asset.
/// copy of https://github.com/Uniswap/v2-periphery/blob/0335e8f7e1bd1e8d8329fd300aea2ef2f36dd19f/contracts/libraries/UniswapV2Library.sol#L43
pub fn get_amount_out(amount_in: U128, reserve_in: U128, reserve_out: U128) -> U128 {
assert(amount_in > U128::zero(), "INSUFFICIENT_INPUT_AMOUNT");
assert((reserve_in > U128::zero()) & (reserve_out > U128::zero()), "INSUFFICIENT_LIQUIDITY");
let amount_in_with_fee = amount_in * U128::from_integer(997);
let numerator = amount_in_with_fee * reserve_out;
let denominator = reserve_in * U128::from_integer(1000) + amount_in_with_fee;
numerator / denominator
}

/// Given an output amount of an asset and pair reserves, returns a required input amount of the other asset.
/// copy of https://github.com/Uniswap/v2-periphery/blob/0335e8f7e1bd1e8d8329fd300aea2ef2f36dd19f/contracts/libraries/UniswapV2Library.sol#L53
pub fn get_amount_in(amount_out: U128, reserve_in: U128, reserve_out: U128) -> U128 {
assert(amount_out > U128::zero(), "INSUFFICIENT_OUTPUT_AMOUNT");
assert((reserve_in > U128::zero()) & (reserve_out > U128::zero()), "INSUFFICIENT_LIQUIDITY");
let numerator = reserve_in * amount_out * U128::from_integer(1000);
let denominator = (reserve_out - amount_out) * U128::from_integer(997);
(numerator / denominator) + U128::from_integer(1)
}

/// Given the desired amounts and reserves of token0 and token1 returns the optimal amount of token0 and token1 to be added to the pool.
pub fn get_amounts_to_add(
amount0_desired: U128,
amount1_desired: U128,
amount0_min: U128,
amount1_min: U128,
reserve0: U128,
reserve1: U128,
) -> (U128, U128) {
let mut amount0 = amount0_desired;
let mut amount1 = amount1_desired;
if ((reserve0 != U128::zero()) | (reserve1 != U128::zero())) {
// First calculate the optimal amount of token1 based on the desired amount of token0.
let amount1_optimal = get_quote(amount0_desired, reserve0, reserve1);
if (amount1_optimal <= amount1_desired) {
// Revert if the optimal amount of token1 is less than the desired amount of token1.
assert(amount1_optimal >= amount1_min, "INSUFFICIENT_1_AMOUNT");
amount0 = amount0_desired;
amount1 = amount1_optimal;
} else {
// We got more amount of token1 than desired so we try repeating the process but this time by quoting
// based on token1.
let amount0_optimal = get_quote(amount1_desired, reserve1, reserve0);
assert(amount0_optimal <= amount0_desired);
assert(amount0_optimal >= amount0_min, "INSUFFICIENT_0_AMOUNT");
amount0 = amount0_optimal;
amount1 = amount1_desired;
}
}

(amount0, amount1)
}
Loading
Loading