Skip to content

Commit a56c813

Browse files
author
Bengt Lofgren
committed
added some more tests
1 parent ea757c6 commit a56c813

File tree

9 files changed

+332
-24
lines changed

9 files changed

+332
-24
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,14 @@ impl PrepareOrderResponseShimAccountsFixture {
6262
guardian_signature_info: &GuardianSignatureInfo,
6363
) -> Self {
6464
let usdc_mint_address = testing_context.get_usdc_mint_address();
65-
let auction_accounts = current_state
66-
.auction_accounts()
67-
.expect("Auction accounts not found");
65+
let auction_accounts = config
66+
.overwrite_auction_accounts
67+
.as_ref()
68+
.unwrap_or_else(|| {
69+
current_state
70+
.auction_accounts()
71+
.expect("Auction accounts not found")
72+
});
6873
let to_endpoint = auction_accounts.to_router_endpoint;
6974
let from_endpoint = auction_accounts.from_router_endpoint;
7075
let fast_market_order = current_state

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,14 @@ pub async fn prepare_order_response(
5151
current_state: &TestingEngineState,
5252
base_fee_token_address: &Pubkey,
5353
) -> Option<PrepareOrderResponseFixture> {
54-
let auction_accounts = current_state
55-
.auction_accounts()
56-
.expect("Auction accounts not found");
54+
let auction_accounts = config
55+
.overwrite_auction_accounts
56+
.as_ref()
57+
.unwrap_or_else(|| {
58+
current_state
59+
.auction_accounts()
60+
.expect("Auction accounts not found")
61+
});
5762
let to_endpoint_address = &auction_accounts.to_router_endpoint;
5863
let from_endpoint_address = &auction_accounts.from_router_endpoint;
5964
let payer_signer = config

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::testing_engine::config::ExpectedError;
2+
use crate::testing_engine::config::SettleAuctionInstructionConfig;
23
use crate::testing_engine::setup::TestingContext;
34
use crate::testing_engine::state::OrderPreparedState;
45
use crate::testing_engine::state::TestingEngineState;
@@ -11,9 +12,8 @@ use matching_engine::accounts::SettleAuctionComplete as SettleAuctionCompleteCpi
1112
use matching_engine::instruction::SettleAuctionComplete;
1213
use solana_program_test::ProgramTestContext;
1314
use solana_sdk::instruction::Instruction;
14-
use solana_sdk::signature::{Keypair, Signer};
15+
use solana_sdk::signature::Signer;
1516
use solana_sdk::transaction::Transaction;
16-
use std::rc::Rc;
1717
use wormhole_svm_definitions::EVENT_AUTHORITY_SEED;
1818

1919
/// Settle an auction (shimless)
@@ -37,10 +37,23 @@ pub async fn settle_auction_complete(
3737
testing_context: &TestingContext,
3838
current_state: &TestingEngineState,
3939
test_context: &mut ProgramTestContext,
40-
payer_signer: &Rc<Keypair>,
40+
config: &SettleAuctionInstructionConfig,
4141
expected_error: Option<&ExpectedError>,
4242
) -> AuctionState {
43-
let auction_state = current_state.auction_state();
43+
let payer_signer = &config
44+
.payer_signer
45+
.clone()
46+
.unwrap_or_else(|| testing_context.testing_actors.payer_signer.clone());
47+
let active_auction = config
48+
.overwrite_active_auction_state
49+
.as_ref()
50+
.unwrap_or_else(|| {
51+
current_state
52+
.auction_state()
53+
.get_active_auction()
54+
.expect("Failed to get active auction")
55+
});
56+
4457
let order_prepared_state = current_state
4558
.order_prepared()
4659
.expect("Order prepared not found");
@@ -52,9 +65,6 @@ pub async fn settle_auction_complete(
5265
} = *order_prepared_state;
5366

5467
let matching_engine_program_id = testing_context.get_matching_engine_program_id();
55-
let active_auction = auction_state
56-
.get_active_auction()
57-
.expect("Failed to get active auction");
5868
let event_seeds = EVENT_AUTHORITY_SEED;
5969
let event_authority =
6070
Pubkey::find_program_address(&[event_seeds], &matching_engine_program_id).0;
@@ -94,6 +104,6 @@ pub async fn settle_auction_complete(
94104
if expected_error.is_none() {
95105
AuctionState::Settled
96106
} else {
97-
auction_state.clone()
107+
AuctionState::Active(Box::new(active_auction.clone()))
98108
}
99109
}

solana/modules/matching-engine-testing/tests/test_scenarios/prepare_order.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,56 @@ pub async fn test_prepare_order_response_shim_after_custodian_is_paused_after_in
315315
.await;
316316
}
317317

318+
/// Prepare order response shim for completed auction after grace period
319+
#[tokio::test]
320+
pub async fn test_prepare_order_response_shim_for_completed_auction_after_grace_period() {
321+
let transfer_direction = TransferDirection::FromEthereumToArbitrum;
322+
let (place_initial_offer_state, mut test_context, testing_engine) =
323+
Box::pin(place_initial_offer_shim(
324+
PlaceInitialOfferInstructionConfig::default(),
325+
None,
326+
transfer_direction,
327+
))
328+
.await;
329+
testing_engine
330+
.make_auction_passed_grace_period(&mut test_context, &place_initial_offer_state, 1)
331+
.await;
332+
let instruction_triggers = vec![
333+
InstructionTrigger::ExecuteOrderShim(ExecuteOrderInstructionConfig::default()),
334+
InstructionTrigger::PrepareOrderShim(PrepareOrderResponseInstructionConfig::default()),
335+
];
336+
testing_engine
337+
.execute(
338+
&mut test_context,
339+
instruction_triggers,
340+
Some(place_initial_offer_state),
341+
)
342+
.await;
343+
}
344+
345+
/// Prepare order response shim for active auction
346+
#[tokio::test]
347+
pub async fn test_prepare_order_response_shim_within_auction_period() {
348+
let transfer_direction = TransferDirection::FromEthereumToArbitrum;
349+
let (place_initial_offer_state, mut test_context, testing_engine) =
350+
Box::pin(place_initial_offer_shim(
351+
PlaceInitialOfferInstructionConfig::default(),
352+
None,
353+
transfer_direction,
354+
))
355+
.await;
356+
let instruction_triggers = vec![InstructionTrigger::PrepareOrderShim(
357+
PrepareOrderResponseInstructionConfig::default(),
358+
)];
359+
testing_engine
360+
.execute(
361+
&mut test_context,
362+
instruction_triggers,
363+
Some(place_initial_offer_state),
364+
)
365+
.await;
366+
}
367+
318368
/*
319369
Sad path tests section
320370

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

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ use crate::testing_engine::config::{
1414
InitializeInstructionConfig, PlaceInitialOfferInstructionConfig,
1515
};
1616
use crate::utils;
17+
use crate::utils::auction::{ActiveAuctionState, AuctionAccounts};
1718

19+
use anchor_lang::error::ErrorCode;
1820
use solana_program_test::tokio;
1921
use testing_engine::config::*;
2022
use testing_engine::engine::{InstructionTrigger, TestingEngine};
@@ -150,6 +152,179 @@ pub async fn test_settle_auction_balance_changes() {
150152
helpers::compare_balance_changes(&balance_changes_shim, &balance_changes_shimless);
151153
}
152154

155+
/// Test settle auction prepare order before active auction
156+
#[tokio::test]
157+
pub async fn test_settle_auction_prepare_order_before_active_auction() {
158+
let transfer_direction = TransferDirection::FromEthereumToArbitrum;
159+
let (testing_context, mut test_context) = setup_environment(
160+
ShimMode::VerifyAndPostSignature,
161+
transfer_direction,
162+
Some(vec![VaaArgs::default()]),
163+
)
164+
.await;
165+
let testing_engine = TestingEngine::new(testing_context).await;
166+
167+
let instruction_triggers = vec![
168+
InstructionTrigger::InitializeProgram(InitializeInstructionConfig::default()),
169+
InstructionTrigger::CreateCctpRouterEndpoints(
170+
CreateCctpRouterEndpointsInstructionConfig::default(),
171+
),
172+
];
173+
let create_cctp_router_endpoints_state = testing_engine
174+
.execute(&mut test_context, instruction_triggers, None)
175+
.await;
176+
177+
// This is just needed to get the router endpoint accounts when prepare order happens before place initial offer, it is not used for anything else
178+
let fake_auction_accounts = AuctionAccounts::fake_auction_accounts(
179+
&create_cctp_router_endpoints_state,
180+
&testing_engine.testing_context,
181+
);
182+
let instruction_triggers = vec![
183+
InstructionTrigger::InitializeFastMarketOrderShim(
184+
InitializeFastMarketOrderShimInstructionConfig::default(),
185+
),
186+
InstructionTrigger::PrepareOrderShim(PrepareOrderResponseInstructionConfig {
187+
overwrite_auction_accounts: Some(fake_auction_accounts),
188+
..Default::default()
189+
}),
190+
];
191+
let prepared_order_state = testing_engine
192+
.execute(
193+
&mut test_context,
194+
instruction_triggers,
195+
Some(create_cctp_router_endpoints_state),
196+
)
197+
.await;
198+
199+
let instruction_triggers = vec![
200+
InstructionTrigger::PlaceInitialOfferShim(PlaceInitialOfferInstructionConfig::default()),
201+
InstructionTrigger::ExecuteOrderShim(ExecuteOrderInstructionConfig::default()),
202+
InstructionTrigger::SettleAuction(SettleAuctionInstructionConfig::default()),
203+
];
204+
testing_engine
205+
.execute(
206+
&mut test_context,
207+
instruction_triggers,
208+
Some(prepared_order_state),
209+
)
210+
.await;
211+
}
212+
213+
/// Test settle auction with base_fee_token != best offer actor
214+
#[tokio::test]
215+
pub async fn test_settle_auction_base_fee_token_not_best_offer_actor() {
216+
let transfer_direction = TransferDirection::FromEthereumToArbitrum;
217+
let (place_initial_offer_state, mut test_context, testing_engine) =
218+
Box::pin(place_initial_offer_shim(
219+
PlaceInitialOfferInstructionConfig::default(),
220+
None,
221+
transfer_direction,
222+
))
223+
.await;
224+
225+
let instruction_triggers = vec![
226+
InstructionTrigger::ExecuteOrderShim(ExecuteOrderInstructionConfig::default()),
227+
InstructionTrigger::PrepareOrderShim(PrepareOrderResponseInstructionConfig {
228+
actor_enum: TestingActorEnum::Solver(2),
229+
..Default::default()
230+
}),
231+
InstructionTrigger::SettleAuction(SettleAuctionInstructionConfig::default()),
232+
];
233+
testing_engine
234+
.execute(
235+
&mut test_context,
236+
instruction_triggers,
237+
Some(place_initial_offer_state),
238+
)
239+
.await;
240+
}
241+
242+
/*
243+
Sad path tests section
244+
245+
*****************
246+
****** ******
247+
**** ****
248+
**** ***
249+
*** ***
250+
** *** *** **
251+
** ******* ******* ***
252+
** ******* ******* **
253+
** ******* ******* **
254+
** *** *** **
255+
** **
256+
** **
257+
** **
258+
** **
259+
** ************ **
260+
** ****** ****** **
261+
*** ***** ***** ***
262+
** *** *** **
263+
*** ** ** ***
264+
**** ****
265+
**** ****
266+
****** ******
267+
*****************
268+
*/
269+
270+
/// Test cannot settle non-existent auction
271+
#[tokio::test]
272+
pub async fn test_settle_auction_non_existent() {
273+
let transfer_direction = TransferDirection::FromEthereumToArbitrum;
274+
let (testing_context, mut test_context) = setup_environment(
275+
ShimMode::VerifyAndPostSignature,
276+
transfer_direction,
277+
Some(vec![VaaArgs::default()]),
278+
)
279+
.await;
280+
let testing_engine = TestingEngine::new(testing_context).await;
281+
282+
let instruction_triggers = vec![
283+
InstructionTrigger::InitializeProgram(InitializeInstructionConfig::default()),
284+
InstructionTrigger::CreateCctpRouterEndpoints(
285+
CreateCctpRouterEndpointsInstructionConfig::default(),
286+
),
287+
];
288+
let create_cctp_router_endpoints_state = testing_engine
289+
.execute(&mut test_context, instruction_triggers, None)
290+
.await;
291+
292+
let fake_auction_accounts = AuctionAccounts::fake_auction_accounts(
293+
&create_cctp_router_endpoints_state,
294+
&testing_engine.testing_context,
295+
);
296+
let fake_active_auction_state =
297+
ActiveAuctionState::fake_active_auction_state(&fake_auction_accounts);
298+
let instruction_triggers = vec![
299+
InstructionTrigger::InitializeFastMarketOrderShim(
300+
InitializeFastMarketOrderShimInstructionConfig::default(),
301+
),
302+
InstructionTrigger::PrepareOrderShim(PrepareOrderResponseInstructionConfig {
303+
overwrite_auction_accounts: Some(fake_auction_accounts),
304+
..Default::default()
305+
}),
306+
InstructionTrigger::SettleAuction(SettleAuctionInstructionConfig {
307+
overwrite_active_auction_state: Some(fake_active_auction_state),
308+
expected_error: Some(ExpectedError {
309+
instruction_index: 0,
310+
error_code: u32::from(ErrorCode::AccountNotInitialized),
311+
error_string: "AccountNotInitialized".to_string(),
312+
}),
313+
..SettleAuctionInstructionConfig::default()
314+
}),
315+
];
316+
testing_engine
317+
.execute(
318+
&mut test_context,
319+
instruction_triggers,
320+
Some(create_cctp_router_endpoints_state),
321+
)
322+
.await;
323+
}
324+
325+
/*
326+
Helper code
327+
*/
153328
mod helpers {
154329
use super::*;
155330

solana/modules/matching-engine-testing/tests/testing_engine/config.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ use std::{
2222

2323
use crate::{
2424
shimless::initialize::AuctionParametersConfig,
25-
utils::{token_account::SplTokenEnum, Chain},
25+
utils::{
26+
auction::{ActiveAuctionState, AuctionAccounts},
27+
token_account::SplTokenEnum,
28+
Chain,
29+
},
2630
};
2731
use anchor_lang::prelude::*;
2832
use solana_program_test::ProgramTestContext;
@@ -153,6 +157,7 @@ impl InstructionConfig for SetPauseCustodianInstructionConfig {
153157
#[derive(Clone, Default)]
154158
pub struct PrepareOrderResponseInstructionConfig {
155159
pub fast_market_order_address: OverwriteCurrentState<Pubkey>,
160+
pub overwrite_auction_accounts: OverwriteCurrentState<AuctionAccounts>,
156161
pub actor_enum: TestingActorEnum,
157162
pub token_enum: SplTokenEnum,
158163
pub vaa_index: usize,
@@ -208,6 +213,7 @@ impl InstructionConfig for ExecuteOrderInstructionConfig {
208213

209214
#[derive(Clone, Default)]
210215
pub struct SettleAuctionInstructionConfig {
216+
pub overwrite_active_auction_state: OverwriteCurrentState<ActiveAuctionState>,
211217
pub payer_signer: Option<Rc<Keypair>>,
212218
pub expected_error: Option<ExpectedError>,
213219
pub expected_log_messages: Option<Vec<ExpectedLog>>,

0 commit comments

Comments
 (0)