Skip to content

Commit be3c2ad

Browse files
committed
cleanup
1 parent 69fc9cf commit be3c2ad

File tree

2 files changed

+30
-46
lines changed
  • noir-projects/noir-contracts/contracts/app/orderbook_contract/src/test

2 files changed

+30
-46
lines changed

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Orderbook, test::utils::setup_orderbook_with_tokens};
1+
use crate::{Orderbook, test::utils::setup};
22
use aztec::{
33
protocol_types::{address::AztecAddress, traits::FromField},
44
test::helpers::authwit::add_private_authwit_from_call_interface,
@@ -13,8 +13,7 @@ global FULFILL_ORDER_AUTHWIT_NONCE: Field = 2;
1313

1414
#[test]
1515
unconstrained fn create_order_happy_path() {
16-
let (mut env, orderbook_address, token0_address, token1_address, minter, maker) =
17-
setup_orderbook_with_tokens(true);
16+
let (mut env, orderbook_address, token0_address, token1_address, minter, maker) = setup(true);
1817

1918
let token0 = Token::at(token0_address);
2019
let orderbook = Orderbook::at(orderbook_address);
@@ -46,8 +45,7 @@ unconstrained fn create_order_happy_path() {
4645

4746
#[test]
4847
unconstrained fn full_flow() {
49-
let (mut env, orderbook_address, token0_address, token1_address, minter, maker) =
50-
setup_orderbook_with_tokens(true);
48+
let (mut env, orderbook_address, token0_address, token1_address, minter, maker) = setup(true);
5149

5250
let token0 = Token::at(token0_address);
5351
let token1 = Token::at(token1_address);
@@ -118,8 +116,7 @@ unconstrained fn full_flow() {
118116

119117
#[test(should_fail_with = "ZERO_BID_AMOUNT")]
120118
unconstrained fn create_order_zero_bid_amount() {
121-
let (mut env, orderbook_address, token0_address, token1_address, _, maker) =
122-
setup_orderbook_with_tokens(false);
119+
let (mut env, orderbook_address, token0_address, token1_address, _, maker) = setup(false);
123120

124121
let orderbook = Orderbook::at(orderbook_address);
125122

@@ -139,8 +136,7 @@ unconstrained fn create_order_zero_bid_amount() {
139136

140137
#[test(should_fail_with = "ZERO_ASK_AMOUNT")]
141138
unconstrained fn create_order_zero_ask_amount() {
142-
let (mut env, orderbook_address, token0_address, token1_address, _, maker) =
143-
setup_orderbook_with_tokens(false);
139+
let (mut env, orderbook_address, token0_address, token1_address, _, maker) = setup(false);
144140

145141
let orderbook = Orderbook::at(orderbook_address);
146142

@@ -160,8 +156,7 @@ unconstrained fn create_order_zero_ask_amount() {
160156

161157
#[test(should_fail_with = "BID_TOKEN_IS_INVALID")]
162158
unconstrained fn create_order_invalid_bid_token() {
163-
let (mut env, orderbook_address, _token0_address, token1_address, _, maker) =
164-
setup_orderbook_with_tokens(false);
159+
let (mut env, orderbook_address, _token0_address, token1_address, _, maker) = setup(false);
165160

166161
let orderbook = Orderbook::at(orderbook_address);
167162

@@ -181,8 +176,7 @@ unconstrained fn create_order_invalid_bid_token() {
181176

182177
#[test(should_fail_with = "ASK_TOKEN_IS_INVALID")]
183178
unconstrained fn create_order_invalid_ask_token() {
184-
let (mut env, orderbook_address, token0_address, _token1_address, _, maker) =
185-
setup_orderbook_with_tokens(false);
179+
let (mut env, orderbook_address, token0_address, _token1_address, _, maker) = setup(false);
186180

187181
let orderbook = Orderbook::at(orderbook_address);
188182

@@ -202,8 +196,7 @@ unconstrained fn create_order_invalid_ask_token() {
202196

203197
#[test(should_fail_with = "SAME_TOKEN_TRADE")]
204198
unconstrained fn create_order_same_tokens() {
205-
let (mut env, orderbook_address, token0_address, _token1_address, _, maker) =
206-
setup_orderbook_with_tokens(false);
199+
let (mut env, orderbook_address, token0_address, _token1_address, _, maker) = setup(false);
207200

208201
let orderbook = Orderbook::at(orderbook_address);
209202

noir-projects/noir-contracts/contracts/app/orderbook_contract/src/test/utils.nr

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,27 @@ use aztec::{
44
};
55
use token::Token;
66

7-
// Utility function to setup two token contracts
7+
// Utility function to setup orderbook with two tokens
88
// TODO(#16560): Make it possible to return a contract instance directly from setup func
9-
unconstrained fn setup_tokens(
10-
env: &mut TestEnvironment,
11-
admin: AztecAddress,
12-
) -> (AztecAddress, AztecAddress) {
13-
// Deploy first token contract
9+
pub(crate) unconstrained fn setup(
10+
with_account_contracts: bool,
11+
) -> (TestEnvironment, AztecAddress, AztecAddress, AztecAddress, AztecAddress, AztecAddress) {
12+
let mut env = TestEnvironment::new();
13+
14+
// Setup accounts
15+
let admin = if with_account_contracts {
16+
env.create_contract_account()
17+
} else {
18+
env.create_light_account()
19+
};
20+
21+
let maker = if with_account_contracts {
22+
env.create_contract_account()
23+
} else {
24+
env.create_light_account()
25+
};
26+
27+
// Deploy tokens
1428
let token0_initializer = Token::interface().constructor(
1529
admin,
1630
"Token00000000000000000000000000",
@@ -20,7 +34,6 @@ unconstrained fn setup_tokens(
2034
let token0_address =
2135
env.deploy("@token_contract/Token").with_public_initializer(admin, token0_initializer);
2236

23-
// Deploy second token contract
2437
let token1_initializer = Token::interface().constructor(
2538
admin,
2639
"Token11111111111111111111111111",
@@ -30,35 +43,13 @@ unconstrained fn setup_tokens(
3043
let token1_address =
3144
env.deploy("@token_contract/Token").with_public_initializer(admin, token1_initializer);
3245

33-
(token0_address, token1_address)
34-
}
35-
36-
// Utility function to setup orderbook with two tokens
37-
pub(crate) unconstrained fn setup_orderbook_with_tokens(
38-
with_account_contracts: bool,
39-
) -> (TestEnvironment, AztecAddress, AztecAddress, AztecAddress, AztecAddress, AztecAddress) {
40-
let mut env = TestEnvironment::new();
41-
let admin = if with_account_contracts {
42-
env.create_contract_account()
43-
} else {
44-
env.create_light_account()
45-
};
46-
47-
let maker = if with_account_contracts {
48-
env.create_contract_account()
49-
} else {
50-
env.create_light_account()
51-
};
52-
53-
let (token0_address, token1_address) = setup_tokens(&mut env, admin);
54-
5546
// Deploy orderbook contract
5647
let orderbook_initializer = Orderbook::interface().constructor(token0_address, token1_address);
5748
let orderbook_address =
5849
env.deploy("Orderbook").with_public_initializer(admin, orderbook_initializer);
5950

60-
// Admin has minter role and since where this function is used we care about minter role and not about admin role
61-
// we name the return value a minter.
51+
// The admin has the minter role. Since we only care about the minting capability in the tests, we return the admin
52+
// address as 'minter' rather than 'admin'.
6253
let minter = admin;
6354

6455
(env, orderbook_address, token0_address, token1_address, minter, maker)

0 commit comments

Comments
 (0)