Skip to content

Commit d42b43c

Browse files
author
Bengt Lofgren
committed
added some negative tests, some more refactoring
1 parent eb93168 commit d42b43c

22 files changed

+1673
-750
lines changed

solana/modules/matching-engine-testing/tests/README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
## How to read the tests
44

5-
Each test is found in the `integration_tests.rs` file.
5+
Each test is found in the `test_scenarios` directory.
6+
7+
Each file in this directory is a module that contains tests for a specific subset of scenarios (instructions).
68

79
Each test is a function that is annotated with `#[tokio::test]`.
810

@@ -12,7 +14,7 @@ The `TestingEngine` is initialised with a `TestingContext`. The `TestingContext`
1214

1315
The `TestingEngine` is used to execute the instruction triggers in the order they are provided. See the `testing_engine/engine.rs` file for more details.
1416

15-
## Integration Tests
17+
## Happy path integration tests
1618

1719
### Initialize program
1820

@@ -40,4 +42,20 @@ What is expected:
4042
- Guardian set is closed
4143
- Close account refund recipient is sent usdc
4244

45+
### Place initial offer (shimless)
46+
47+
What is expected:
48+
- Fast market order is initialised
49+
- Initial offer is placed
50+
- Auction account is created and corresponds to a vaa and the initial offer
51+
52+
### Place initial offer (shim)
53+
54+
What is expected:
55+
- Fast market order is posted as a vaa
56+
- Initial offer is placed
57+
- Auction account is created and corresponds to a vaa and the initial offer
58+
59+
60+
4361

solana/modules/matching-engine-testing/tests/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
mod shimful;
55
mod shimless;
6-
// Where tests are located
76
mod test_scenarios;
87
mod testing_engine;
98
mod utils;

solana/modules/matching-engine-testing/tests/shimful/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,10 @@
22

33
This directory contains tests that use the fallback program.
44

5+
## Files
6+
7+
- `fast_market_order_shim.rs` - A function that creates a fast market order account and one that closes it
8+
- `shims_make_offer.rs` - A function that places an initial offer and one that improves an offer
9+
- `shims_execute_order.rs` - A function that executes an order
10+
- `shims_prepare_order_response.rs` - A function that prepares an order response
11+
- `shims_settle_auction.rs` - A function that settles an auction

solana/modules/matching-engine-testing/tests/shimful/shims_make_offer.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::testing_engine::config::ExpectedError;
22
use crate::testing_engine::state::InitialOfferPlacedState;
33

44
use super::super::utils;
5-
use crate::testing_engine::setup::{Solver, TestingContext};
5+
use crate::testing_engine::setup::{TestingActor, TestingContext};
66
use matching_engine::fallback::place_initial_offer::{
77
PlaceInitialOfferCctpShim as PlaceInitialOfferCctpShimFallback,
88
PlaceInitialOfferCctpShimAccounts as PlaceInitialOfferCctpShimFallbackAccounts,
@@ -42,14 +42,14 @@ pub async fn place_initial_offer_fallback(
4242
test_context: &mut ProgramTestContext,
4343
payer_signer: &Rc<Keypair>,
4444
vaa_data: &utils::vaa::PostedVaaData,
45-
solver: Solver,
45+
actor: TestingActor,
4646
fast_market_order_account: &Pubkey,
4747
auction_accounts: &utils::auction::AuctionAccounts,
4848
offer_price: u64,
4949
expected_error: Option<&ExpectedError>,
5050
) -> Option<InitialOfferPlacedState> {
5151
let program_id = testing_context.get_matching_engine_program_id();
52-
let fast_market_order = create_fast_market_order_state_from_vaa_data(vaa_data, solver.pubkey());
52+
let fast_market_order = create_fast_market_order_state_from_vaa_data(vaa_data, actor.pubkey());
5353

5454
let auction_address = Pubkey::find_program_address(
5555
&[Auction::SEED_PREFIX, &fast_market_order.digest()],
@@ -76,11 +76,11 @@ pub async fn place_initial_offer_fallback(
7676
)
7777
.0;
7878

79-
solver
79+
actor
8080
.approve_usdc(test_context, &transfer_authority, 420_000__000_000)
8181
.await;
8282

83-
let solver_usdc_balance_before = solver.get_balance(test_context).await;
83+
let actor_usdc_balance_before = actor.get_token_account_balance(test_context).await;
8484

8585
let place_initial_offer_ix_data = PlaceInitialOfferCctpShimFallbackData::new(offer_price);
8686

@@ -122,9 +122,9 @@ pub async fn place_initial_offer_fallback(
122122
.execute_and_verify_transaction(test_context, transaction, expected_error)
123123
.await;
124124
if expected_error.is_none() {
125-
let solver_usdc_balance_after = solver.get_balance(test_context).await;
125+
let actor_usdc_balance_after = actor.get_token_account_balance(test_context).await;
126126
assert!(
127-
solver_usdc_balance_after < solver_usdc_balance_before,
127+
actor_usdc_balance_after < actor_usdc_balance_before,
128128
"Solver USDC balance should have decreased"
129129
);
130130
let new_active_auction_state = utils::auction::ActiveAuctionState {

solana/modules/matching-engine-testing/tests/shimless/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,11 @@
22

33
This directory contains tests that do not use the fallback program.
44

5+
## Files
6+
7+
- `place_initial_offer.rs` - A function that places an initial offer
8+
- `make_offer.rs` - A function that places an initial offer and one that improves an offer
9+
- `execute_order.rs` - A function that executes an order
10+
- `prepare_order_response.rs` - A function that prepares an order response
11+
- `settle_auction.rs` - A function that settles an auction
12+

solana/modules/matching-engine-testing/tests/shimless/initialize.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use anchor_lang::AccountDeserialize;
1010
use anchor_spl::{associated_token::spl_associated_token_account, token::spl_token};
1111
use solana_program::{bpf_loader_upgradeable, system_program};
1212

13-
use crate::testing_engine::config::ExpectedError;
13+
use crate::testing_engine::config::{ExpectedError, ExpectedLog};
1414

1515
use crate::testing_engine::setup::TestingContext;
1616
use anchor_lang::{InstructionData, ToAccountMetas};
@@ -142,14 +142,14 @@ pub async fn initialize_program(
142142
test_context: &mut ProgramTestContext,
143143
auction_parameters_config: AuctionParametersConfig,
144144
expected_error: Option<&ExpectedError>,
145+
expected_log_messages: Option<&Vec<ExpectedLog>>,
145146
) -> Option<InitializeFixture> {
146147
let program_id = testing_context.get_matching_engine_program_id();
147148
let usdc_mint_address = testing_context.get_usdc_mint_address();
148149
let cctp_mint_recipient = testing_context.get_cctp_mint_recipient();
149150
let (custodian, _custodian_bump) =
150151
Pubkey::find_program_address(&[Custodian::SEED_PREFIX], &program_id);
151152

152-
// TODO: Figure out this seed? Where does the 0u32 come from?
153153
let (auction_config, _auction_config_bump) = Pubkey::find_program_address(
154154
&[
155155
AuctionConfig::SEED_PREFIX,
@@ -218,6 +218,35 @@ pub async fn initialize_program(
218218
.execute_and_verify_transaction(test_context, versioned_transaction, expected_error)
219219
.await;
220220

221+
if let Some(expected_log_messages) = expected_log_messages {
222+
// Recreate the instruction
223+
let instruction = Instruction {
224+
program_id,
225+
accounts: accounts.to_account_metas(None),
226+
data: ix_data.data(),
227+
};
228+
let mut transaction =
229+
Transaction::new_with_payer(&[instruction], Some(&test_context.payer.pubkey()));
230+
let new_blockhash = testing_context
231+
.get_new_latest_blockhash(test_context)
232+
.await
233+
.expect("Could not get new blockhash");
234+
transaction.sign(
235+
&[
236+
&test_context.payer,
237+
&testing_context.testing_actors.owner.keypair(),
238+
],
239+
new_blockhash,
240+
);
241+
let versioned_transaction = VersionedTransaction::from(transaction);
242+
243+
// Simulate and verify logs
244+
testing_context
245+
.simulate_and_verify_logs(test_context, versioned_transaction, expected_log_messages)
246+
.await
247+
.expect("Failed to verify logs");
248+
}
249+
221250
if expected_error.is_none() {
222251
// Verify the results
223252
let custodian_account = test_context

solana/modules/matching-engine-testing/tests/shimless/make_offer.rs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ use solana_sdk::transaction::Transaction;
2525
use utils::auction::{ActiveAuctionState, AuctionAccounts, AuctionOffer, AuctionState};
2626
use utils::vaa::TestVaa;
2727

28+
/// Place an initial offer (shimless)
29+
///
30+
/// Place an initial offer by providing a price.
31+
///
32+
/// # Arguments
33+
///
34+
/// * `testing_context` - The testing context
35+
/// * `test_context` - The test context
36+
/// * `accounts` - The auction accounts
37+
/// * `fast_market_order` - The fast market order
38+
/// * `offer_price` - The price of the offer
39+
/// * `payer_signer` - The payer signer
40+
/// * `expected_error` - The expected error
41+
///
42+
/// # Returns
43+
///
44+
/// The new auction state if successful, otherwise the old auction state
2845
pub async fn place_initial_offer_shimless(
2946
testing_context: &TestingContext,
3047
test_context: &mut ProgramTestContext,
@@ -76,7 +93,7 @@ pub async fn place_initial_offer_shimless(
7693
.0;
7794
{
7895
// Check if solver has already approved usdc
79-
let usdc_account = accounts.solver.token_account_address().unwrap();
96+
let usdc_account = accounts.actor.token_account_address().unwrap();
8097
let usdc_account_info = test_context
8198
.banks_client
8299
.get_account(usdc_account)
@@ -89,14 +106,14 @@ pub async fn place_initial_offer_shimless(
89106
.expect("Failed to deserialize usdc account");
90107
if token_account_info.delegate.is_none() {
91108
accounts
92-
.solver
109+
.actor
93110
.approve_usdc(test_context, &transfer_authority, 420_000__000_000)
94111
.await;
95112
} else {
96113
let delegate = token_account_info.delegate.unwrap();
97114
if delegate != transfer_authority {
98115
accounts
99-
.solver
116+
.actor
100117
.approve_usdc(test_context, &transfer_authority, 420_000__000_000)
101118
.await;
102119
}
@@ -173,6 +190,23 @@ pub async fn place_initial_offer_shimless(
173190
}
174191
}
175192

193+
/// Improve an offer (shimless)
194+
///
195+
/// Improve an offer by providing a new price.
196+
///
197+
/// # Arguments
198+
///
199+
/// * `testing_context` - The testing context
200+
/// * `test_context` - The test context
201+
/// * `solver` - The solver
202+
/// * `offer_price` - The new price
203+
/// * `payer_signer` - The payer signer
204+
/// * `initial_auction_state` - The initial auction state
205+
/// * `expected_error` - The expected error
206+
///
207+
/// # Returns
208+
///
209+
/// The new auction state if successful, otherwise the old auction state
176210
pub async fn improve_offer(
177211
testing_context: &TestingContext,
178212
test_context: &mut ProgramTestContext,
@@ -181,7 +215,7 @@ pub async fn improve_offer(
181215
payer_signer: &Rc<Keypair>,
182216
initial_auction_state: &AuctionState,
183217
expected_error: Option<&ExpectedError>,
184-
) -> Option<AuctionState> {
218+
) -> AuctionState {
185219
let program_id = testing_context.get_matching_engine_program_id();
186220
let active_auction_state = initial_auction_state.get_active_auction().unwrap();
187221
let auction_config = active_auction_state.auction_config_address;
@@ -254,7 +288,7 @@ pub async fn improve_offer(
254288
.get_active_auction()
255289
.unwrap()
256290
.initial_offer;
257-
Some(AuctionState::Active(Box::new(ActiveAuctionState {
291+
AuctionState::Active(Box::new(ActiveAuctionState {
258292
auction_address,
259293
auction_custody_token_address,
260294
auction_config_address: auction_config,
@@ -264,8 +298,8 @@ pub async fn improve_offer(
264298
offer_token,
265299
offer_price,
266300
},
267-
})))
301+
}))
268302
} else {
269-
None
303+
initial_auction_state.clone()
270304
}
271305
}

solana/modules/matching-engine-testing/tests/shimless/prepare_order_response.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ pub struct PrepareOrderResponseFixture {
3030
pub prepared_custody_token: Pubkey,
3131
}
3232

33+
/// Prepare an order response (shimless)
34+
///
35+
/// Prepare an order response by providing a fast market order.
36+
///
37+
/// # Arguments
38+
///
39+
/// * `testing_context` - The testing context
40+
/// * `test_context` - The test context
41+
/// * `payer_signer` - The payer signer
42+
/// * `testing_engine_state` - The testing engine state
43+
/// * `to_endpoint_address` - The to endpoint address
44+
/// * `from_endpoint_address` - The from endpoint address
45+
/// * `base_fee_token_address` - The base fee token address
46+
/// * `expected_error` - The expected error
47+
/// * `expected_log_messages` - The expected log messages
48+
///
49+
/// # Returns
50+
///
51+
/// The prepared order response fixture if successful, otherwise None
3352
#[allow(clippy::too_many_arguments)]
3453
pub async fn prepare_order_response(
3554
testing_context: &TestingContext,
@@ -40,7 +59,7 @@ pub async fn prepare_order_response(
4059
from_endpoint_address: &Pubkey,
4160
base_fee_token_address: &Pubkey,
4261
expected_error: Option<&ExpectedError>,
43-
expected_log_message: Option<&Vec<ExpectedLog>>,
62+
expected_log_messages: Option<&Vec<ExpectedLog>>,
4463
) -> Option<PrepareOrderResponseFixture> {
4564
let matching_engine_program_id = &testing_context.get_matching_engine_program_id();
4665
let usdc_mint_address = &testing_context.get_usdc_mint_address();
@@ -206,13 +225,9 @@ pub async fn prepare_order_response(
206225
.await
207226
.expect("Failed to get new blockhash"),
208227
);
209-
if let Some(expected_log_message) = expected_log_message {
210-
assert!(
211-
expected_error.is_none(),
212-
"Expected error is not allowed when expected log message is provided"
213-
);
228+
if let Some(expected_log_messages) = expected_log_messages {
214229
testing_context
215-
.simulate_and_verify_logs(test_context, transaction, expected_log_message)
230+
.simulate_and_verify_logs(test_context, transaction, expected_log_messages)
216231
.await
217232
.unwrap();
218233
} else {

solana/modules/matching-engine-testing/tests/shimless/settle_auction.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ use solana_sdk::transaction::Transaction;
1414
use std::rc::Rc;
1515
use wormhole_svm_definitions::EVENT_AUTHORITY_SEED;
1616

17+
/// Settle an auction (shimless)
18+
///
19+
/// Settle an auction by providing a prepare order response address, prepared custody token, and expected error.
20+
///
21+
/// # Arguments
22+
///
23+
/// * `testing_context` - The testing context
24+
/// * `test_context` - The test context
25+
/// * `payer_signer` - The payer signer
26+
/// * `auction_state` - The auction state
27+
/// * `prepare_order_response_address` - The prepare order response address
28+
/// * `prepared_custody_token` - The prepared custody token
29+
/// * `expected_error` - The expected error
30+
///
31+
/// # Returns
32+
///
33+
/// The new auction state if successful, otherwise the old auction state
1734
pub async fn settle_auction_complete(
1835
testing_context: &TestingContext,
1936
test_context: &mut ProgramTestContext,

0 commit comments

Comments
 (0)