Skip to content

Commit ae80a49

Browse files
committed
final touches
1 parent b3f3398 commit ae80a49

File tree

1 file changed

+15
-20
lines changed
  • noir-projects/noir-contracts/contracts/app/amm_contract/src/test

1 file changed

+15
-20
lines changed

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

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ global AUTHWIT_NONCE: Field = 1;
99
global DEFAULT_AMOUNT_0_MIN: u128 = 0;
1010
global DEFAULT_AMOUNT_1_MIN: u128 = 0;
1111

12+
// Note: Ideally this test would be split into 2 separate test cases - one for order creation and one for order
13+
// fulfillment, with the second test running on the state from the first test. Since this feature is not currently
14+
// supported, combining them into a single comprehensive test is the best approach.
1215
#[test]
13-
unconstrained fn add_liquidity_twice_and_remove_liquidity_happy_path() {
16+
unconstrained fn add_liquidity_twice_and_remove_liquidity() {
1417
let (mut env, amm_address, token0_address, token1_address, liquidity_token_address, minter) =
1518
setup();
1619

@@ -21,7 +24,7 @@ unconstrained fn add_liquidity_twice_and_remove_liquidity_happy_path() {
2124
let token1 = Token::at(token1_address);
2225
let liquidity_token = Token::at(liquidity_token_address);
2326

24-
// First add initial liquidity from minter
27+
// ADDING INITIAL LIQUIDITY
2528
let initial_amount0 = 1000 as u128;
2629
let initial_amount1 = 2000 as u128;
2730

@@ -38,12 +41,13 @@ unconstrained fn add_liquidity_twice_and_remove_liquidity_happy_path() {
3841
DEFAULT_AMOUNT_1_MIN,
3942
);
4043

44+
// We verify the initial liquidity token supply is as expected as we will need the value later on to calculate
45+
// the expected tokens received.
4146
let initial_liquidity_token_supply = env.view_public(liquidity_token.total_supply());
4247
assert_eq(initial_liquidity_token_supply, AMM::MINIMUM_LIQUIDITY + AMM::INITIAL_LIQUIDITY);
4348

44-
// Add liquidity from liquidity provider 2 with excess token1
49+
// ADDING LIQUIDITY AGAIN
4550
let amount0_max = initial_amount0 / 2;
46-
4751
// The amount of token1 that is expected to be deposited.
4852
let expected_amount_1_in = initial_amount1 / 2;
4953
// We intentionally set the amount1_max to be higher than the liquidity ration to check that we get a refund.
@@ -89,7 +93,7 @@ unconstrained fn add_liquidity_twice_and_remove_liquidity_happy_path() {
8993
expected_liquidity_tokens,
9094
);
9195

92-
// Now remove half the liquidity from liquidity provider 1's position
96+
// REMOVING LIQUIDITY
9397
let liquidity_to_remove = AMM::INITIAL_LIQUIDITY / 2;
9498
let amount0_min = 400 as u128;
9599
let amount1_min = 800 as u128;
@@ -127,7 +131,7 @@ unconstrained fn add_liquidity_twice_and_remove_liquidity_happy_path() {
127131
}
128132

129133
#[test]
130-
unconstrained fn swap_exact_tokens_for_tokens_happy_path() {
134+
unconstrained fn swap_exact_tokens_for_tokens() {
131135
let (mut env, amm_address, token0_address, token1_address, _liquidity_token_address, minter) =
132136
setup();
133137

@@ -177,15 +181,14 @@ unconstrained fn swap_exact_tokens_for_tokens_happy_path() {
177181
),
178182
);
179183

180-
// Verify swap occurred
184+
// Verify swap occurred - all of input tokens should be spent and hence the swapper should have 0 token0 balance.
181185
assert_eq(env.simulate_utility(token0.balance_of_private(swapper)), 0);
182186
// The exact amount out depends on the AMM formula, but should be > amount_out_min
183-
let swapper_token1_balance = env.simulate_utility(token1.balance_of_private(swapper));
184-
assert(swapper_token1_balance >= amount_out_min);
187+
assert(env.simulate_utility(token1.balance_of_private(swapper)) >= amount_out_min);
185188
}
186189

187190
#[test]
188-
unconstrained fn swap_tokens_for_exact_tokens_happy_path() {
191+
unconstrained fn swap_tokens_for_exact_tokens() {
189192
let (mut env, amm_address, token0_address, token1_address, _liquidity_token_address, minter) =
190193
setup();
191194

@@ -247,8 +250,6 @@ unconstrained fn swap_tokens_for_exact_tokens_happy_path() {
247250
assert(swapper_token0_balance < amount_in_max);
248251
}
249252

250-
// Unhappy path tests for add_liquidity
251-
252253
#[test(should_fail_with = "INCORRECT_TOKEN0_LIMITS")]
253254
unconstrained fn add_liquidity_incorrect_token0_limits() {
254255
let (mut env, amm_address, _token0_address, _token1_address, _liquidity_token_address, _minter)
@@ -327,8 +328,6 @@ unconstrained fn add_liquidity_zero_amount1_max() {
327328
);
328329
}
329330

330-
// Unhappy path tests for swap_exact_tokens_for_tokens
331-
332331
#[test(should_fail_with = "TOKEN_IN_IS_INVALID")]
333332
unconstrained fn swap_exact_tokens_invalid_token_in() {
334333
let (mut env, amm_address, _token0_address, token1_address, _liquidity_token_address, _minter)
@@ -400,8 +399,6 @@ unconstrained fn swap_exact_tokens_same_tokens() {
400399
);
401400
}
402401

403-
// Unhappy path tests for swap_tokens_for_exact_tokens
404-
405402
#[test(should_fail_with = "TOKEN_IN_IS_INVALID")]
406403
unconstrained fn swap_tokens_for_exact_invalid_token_in() {
407404
let (mut env, amm_address, _token0_address, token1_address, _liquidity_token_address, _minter)
@@ -473,8 +470,6 @@ unconstrained fn swap_tokens_for_exact_same_tokens() {
473470
);
474471
}
475472

476-
// Edge case tests
477-
478473
#[test(should_fail_with = "INSUFFICIENT_LIQUIDITY_MINTED")]
479474
unconstrained fn add_liquidity_insufficient_liquidity_minted() {
480475
let (mut env, amm_address, token0_address, token1_address, _liquidity_token_address, minter) =
@@ -543,7 +538,7 @@ unconstrained fn remove_liquidity_insufficient_amount0() {
543538
);
544539

545540
// Try to remove liquidity with unrealistic minimum expectations
546-
let liquidity_to_remove = 49500 as u128; // Half of 99000
541+
let liquidity_to_remove = AMM::INITIAL_LIQUIDITY / 2;
547542
let amount0_min = 10000 as u128; // Unrealistically high minimum
548543
let amount1_min = 800 as u128;
549544

@@ -583,7 +578,7 @@ unconstrained fn remove_liquidity_insufficient_amount1() {
583578
);
584579

585580
// Try to remove liquidity with unrealistic minimum expectations
586-
let liquidity_to_remove = 49500 as u128; // Half of 99000
581+
let liquidity_to_remove = AMM::INITIAL_LIQUIDITY / 2;
587582
let amount0_min = 400 as u128;
588583
let amount1_min = 20000 as u128; // Unrealistically high minimum
589584

0 commit comments

Comments
 (0)