Skip to content

Commit b3f3398

Browse files
committed
removed ugly constants
1 parent 6030670 commit b3f3398

File tree

2 files changed

+24
-10
lines changed
  • noir-projects/noir-contracts/contracts/app/amm_contract/src

2 files changed

+24
-10
lines changed

noir-projects/noir-contracts/contracts/app/amm_contract/src/main.nr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ pub contract AMM {
5555

5656
/// Amount of liquidity which gets locked when liquidity is provided for the first time. Its purpose is to prevent
5757
/// the pool from ever emptying which could lead to undefined behavior.
58-
global MINIMUM_LIQUIDITY: u128 = 1000;
58+
pub global MINIMUM_LIQUIDITY: u128 = 1000;
5959
/// We set it to 99 times the minimum liquidity. That way the first LP gets 99% of the value of their deposit.
60-
global INITIAL_LIQUIDITY: u128 = 99000;
60+
pub global INITIAL_LIQUIDITY: u128 = 99000;
6161

6262
// TODO(#9480): Either deploy the liquidity contract in the constructor or verify it that it corresponds to what
6363
// this contract expects (i.e. that the AMM has permission to mint and burn).

noir-projects/noir-contracts/contracts/app/amm_contract/src/test/test.nr

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ unconstrained fn add_liquidity_twice_and_remove_liquidity_happy_path() {
3838
DEFAULT_AMOUNT_1_MIN,
3939
);
4040

41+
let initial_liquidity_token_supply = env.view_public(liquidity_token.total_supply());
42+
assert_eq(initial_liquidity_token_supply, AMM::MINIMUM_LIQUIDITY + AMM::INITIAL_LIQUIDITY);
43+
4144
// Add liquidity from liquidity provider 2 with excess token1
4245
let amount0_max = initial_amount0 / 2;
4346

@@ -79,14 +82,15 @@ unconstrained fn add_liquidity_twice_and_remove_liquidity_happy_path() {
7982
);
8083

8184
// Check liquidity provider 2 received liquidity tokens proportional to their contribution
82-
// Expected: min((500 * 100000) / 1000, (1000 * 100000) / 2000) = 50000
85+
let expected_liquidity_tokens =
86+
(amount0_max * initial_liquidity_token_supply) / initial_amount0;
8387
assert_eq(
8488
env.simulate_utility(liquidity_token.balance_of_private(liquidity_provider_2)),
85-
50000,
89+
expected_liquidity_tokens,
8690
);
8791

8892
// Now remove half the liquidity from liquidity provider 1's position
89-
let liquidity_to_remove = 49500 as u128; // Half of 99000
93+
let liquidity_to_remove = AMM::INITIAL_LIQUIDITY / 2;
9094
let amount0_min = 400 as u128;
9195
let amount1_min = 800 as u128;
9296

@@ -101,15 +105,25 @@ unconstrained fn add_liquidity_twice_and_remove_liquidity_happy_path() {
101105
);
102106

103107
// Verify liquidity provider 1 got tokens back (proportional to liquidity burned)
104-
// 49500/100000 of 1000 = 495, 49500/100000 of 2000 = 990
105-
assert_eq(env.simulate_utility(token0.balance_of_private(liquidity_provider_1)), 495);
106-
assert_eq(env.simulate_utility(token1.balance_of_private(liquidity_provider_1)), 990);
108+
let expected_token0_back =
109+
(liquidity_to_remove * initial_amount0) / initial_liquidity_token_supply;
110+
let expected_token1_back =
111+
(liquidity_to_remove * initial_amount1) / initial_liquidity_token_supply;
112+
assert_eq(
113+
env.simulate_utility(token0.balance_of_private(liquidity_provider_1)),
114+
expected_token0_back,
115+
);
116+
assert_eq(
117+
env.simulate_utility(token1.balance_of_private(liquidity_provider_1)),
118+
expected_token1_back,
119+
);
107120

108121
// Check remaining liquidity tokens
109122
assert_eq(
110123
env.simulate_utility(liquidity_token.balance_of_private(liquidity_provider_1)),
111-
49500,
112-
); // Other half
124+
// The expected remaining liquidity is the other half of the initial liquidity.
125+
AMM::INITIAL_LIQUIDITY / 2,
126+
);
113127
}
114128

115129
#[test]

0 commit comments

Comments
 (0)