Skip to content

Commit aad5a45

Browse files
committed
separating utils from tests
1 parent 0072e9c commit aad5a45

3 files changed

Lines changed: 100 additions & 90 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod test;
2+
pub(crate) mod utils;

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

Lines changed: 7 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,16 @@
1-
use crate::Orderbook;
1+
use crate::{
2+
Orderbook,
3+
test::utils::{
4+
check_private_balance, check_public_balance, mint_to_private, setup_orderbook_with_tokens,
5+
},
6+
};
27
use aztec::{
38
protocol_types::{address::AztecAddress, traits::FromField},
4-
test::helpers::{
5-
authwit::add_private_authwit_from_call_interface, test_environment::TestEnvironment,
6-
},
9+
test::helpers::authwit::add_private_authwit_from_call_interface,
710
};
811
use token::Token;
912
use uint_note::uint_note::PartialUintNote;
1013

11-
// Utility function to setup two token contracts
12-
unconstrained fn setup_tokens(
13-
env: &mut TestEnvironment,
14-
owner: AztecAddress,
15-
) -> (AztecAddress, AztecAddress) {
16-
// Deploy first token contract
17-
let token0_initializer = Token::interface().constructor(
18-
owner,
19-
"Token00000000000000000000000000",
20-
"TK00000000000000000000000000000",
21-
18,
22-
);
23-
let token0_address =
24-
env.deploy("@token_contract/Token").with_public_initializer(owner, token0_initializer);
25-
26-
// Deploy second token contract
27-
let token1_initializer = Token::interface().constructor(
28-
owner,
29-
"Token11111111111111111111111111",
30-
"TK11111111111111111111111111111",
31-
18,
32-
);
33-
let token1_address =
34-
env.deploy("@token_contract/Token").with_public_initializer(owner, token1_initializer);
35-
36-
(token0_address, token1_address)
37-
}
38-
39-
// Utility function to setup orderbook with two tokens
40-
unconstrained fn setup_orderbook_with_tokens(
41-
with_account_contracts: bool,
42-
) -> (TestEnvironment, AztecAddress, AztecAddress, AztecAddress, AztecAddress, AztecAddress) {
43-
let mut env = TestEnvironment::new();
44-
let owner = if with_account_contracts {
45-
env.create_contract_account()
46-
} else {
47-
env.create_light_account()
48-
};
49-
50-
let (token0_address, token1_address) = setup_tokens(&mut env, owner);
51-
52-
// Deploy orderbook contract
53-
let orderbook_initializer = Orderbook::interface().constructor(token0_address, token1_address);
54-
let orderbook_address =
55-
env.deploy("Orderbook").with_public_initializer(owner, orderbook_initializer);
56-
57-
(env, orderbook_address, token0_address, token1_address, owner, owner)
58-
}
59-
60-
// Utility function to mint tokens to private balance
61-
unconstrained fn mint_to_private(
62-
env: TestEnvironment,
63-
token_address: AztecAddress,
64-
owner: AztecAddress,
65-
to: AztecAddress,
66-
amount: u128,
67-
) {
68-
env.call_private(owner, Token::at(token_address).mint_to_private(to, amount));
69-
}
70-
71-
// Utility function to check private balance
72-
unconstrained fn check_private_balance(
73-
env: TestEnvironment,
74-
token_address: AztecAddress,
75-
address: AztecAddress,
76-
expected_amount: u128,
77-
) {
78-
assert_eq(
79-
env.simulate_utility(Token::at(token_address).balance_of_private(address)),
80-
expected_amount,
81-
);
82-
}
83-
84-
// Utility function to check public balance
85-
unconstrained fn check_public_balance(
86-
env: TestEnvironment,
87-
token_address: AztecAddress,
88-
address: AztecAddress,
89-
expected_amount: u128,
90-
) {
91-
assert_eq(
92-
env.view_public(Token::at(token_address).balance_of_public(address)),
93-
expected_amount,
94-
);
95-
}
96-
9714
// Happy path tests
9815

9916
#[test]
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use crate::Orderbook;
2+
use aztec::{
3+
protocol_types::address::AztecAddress, test::helpers::test_environment::TestEnvironment,
4+
};
5+
use token::Token;
6+
7+
// Utility function to setup two token contracts
8+
pub(crate) unconstrained fn setup_tokens(
9+
env: &mut TestEnvironment,
10+
owner: AztecAddress,
11+
) -> (AztecAddress, AztecAddress) {
12+
// Deploy first token contract
13+
let token0_initializer = Token::interface().constructor(
14+
owner,
15+
"Token00000000000000000000000000",
16+
"TK00000000000000000000000000000",
17+
18,
18+
);
19+
let token0_address =
20+
env.deploy("@token_contract/Token").with_public_initializer(owner, token0_initializer);
21+
22+
// Deploy second token contract
23+
let token1_initializer = Token::interface().constructor(
24+
owner,
25+
"Token11111111111111111111111111",
26+
"TK11111111111111111111111111111",
27+
18,
28+
);
29+
let token1_address =
30+
env.deploy("@token_contract/Token").with_public_initializer(owner, token1_initializer);
31+
32+
(token0_address, token1_address)
33+
}
34+
35+
// Utility function to setup orderbook with two tokens
36+
pub(crate) unconstrained fn setup_orderbook_with_tokens(
37+
with_account_contracts: bool,
38+
) -> (TestEnvironment, AztecAddress, AztecAddress, AztecAddress, AztecAddress, AztecAddress) {
39+
let mut env = TestEnvironment::new();
40+
let owner = if with_account_contracts {
41+
env.create_contract_account()
42+
} else {
43+
env.create_light_account()
44+
};
45+
46+
let (token0_address, token1_address) = setup_tokens(&mut env, owner);
47+
48+
// Deploy orderbook contract
49+
let orderbook_initializer = Orderbook::interface().constructor(token0_address, token1_address);
50+
let orderbook_address =
51+
env.deploy("Orderbook").with_public_initializer(owner, orderbook_initializer);
52+
53+
(env, orderbook_address, token0_address, token1_address, owner, owner)
54+
}
55+
56+
// Utility function to mint tokens to private balance
57+
pub(crate) unconstrained fn mint_to_private(
58+
env: TestEnvironment,
59+
token_address: AztecAddress,
60+
owner: AztecAddress,
61+
to: AztecAddress,
62+
amount: u128,
63+
) {
64+
env.call_private(owner, Token::at(token_address).mint_to_private(to, amount));
65+
}
66+
67+
// Utility function to check private balance
68+
pub(crate) unconstrained fn check_private_balance(
69+
env: TestEnvironment,
70+
token_address: AztecAddress,
71+
address: AztecAddress,
72+
expected_amount: u128,
73+
) {
74+
assert_eq(
75+
env.simulate_utility(Token::at(token_address).balance_of_private(address)),
76+
expected_amount,
77+
);
78+
}
79+
80+
// Utility function to check public balance
81+
pub(crate) unconstrained fn check_public_balance(
82+
env: TestEnvironment,
83+
token_address: AztecAddress,
84+
address: AztecAddress,
85+
expected_amount: u128,
86+
) {
87+
assert_eq(
88+
env.view_public(Token::at(token_address).balance_of_public(address)),
89+
expected_amount,
90+
);
91+
}

0 commit comments

Comments
 (0)