|
| 1 | +use crate::testing_engine::config::{InstructionConfig, SettleAuctionNoneInstructionConfig}; |
| 2 | +use crate::testing_engine::setup::TestingContext; |
| 3 | +use crate::testing_engine::state::{OrderPreparedState, TestingEngineState}; |
| 4 | +use crate::utils::auction::AuctionState; |
| 5 | +use crate::utils::token_account::SplTokenEnum; |
| 6 | +use anchor_lang::prelude::*; |
| 7 | +use anchor_lang::{InstructionData, ToAccountMetas}; |
| 8 | +use anchor_spl::token::spl_token; |
| 9 | +use matching_engine::accounts::RequiredSysvars; |
| 10 | +use matching_engine::accounts::{ |
| 11 | + CheckedCustodian, ClosePreparedOrderResponse, |
| 12 | + SettleAuctionNoneCctp as SettleAuctionNoneCctpAccounts, WormholePublishMessage, |
| 13 | +}; |
| 14 | +use matching_engine::instruction::SettleAuctionNoneCctp as SettleAuctionNoneCctpIx; |
| 15 | +use matching_engine::state::{Auction, PreparedOrderResponse}; |
| 16 | +use solana_program_test::ProgramTestContext; |
| 17 | +use solana_sdk::instruction::Instruction; |
| 18 | +use solana_sdk::signature::Signer; |
| 19 | +use solana_sdk::sysvar::SysvarId; |
| 20 | +use solana_sdk::transaction::Transaction; |
| 21 | +use wormhole_svm_definitions::EVENT_AUTHORITY_SEED; |
| 22 | + |
| 23 | +use super::execute_order::create_cctp_deposit_for_burn; |
| 24 | + |
| 25 | +/// Settle an auction none shimless |
| 26 | +pub async fn settle_auction_none_shimless( |
| 27 | + testing_context: &TestingContext, |
| 28 | + current_state: &TestingEngineState, |
| 29 | + test_context: &mut ProgramTestContext, |
| 30 | + config: &SettleAuctionNoneInstructionConfig, |
| 31 | +) -> AuctionState { |
| 32 | + let payer_signer = &config |
| 33 | + .payer_signer |
| 34 | + .clone() |
| 35 | + .unwrap_or_else(|| testing_context.testing_actors.payer_signer.clone()); |
| 36 | + |
| 37 | + let settle_auction_none_cctp_accounts = create_settle_auction_none_cctp_shimless_accounts( |
| 38 | + test_context, |
| 39 | + testing_context, |
| 40 | + current_state, |
| 41 | + config, |
| 42 | + ) |
| 43 | + .await; |
| 44 | + let settle_auction_none_ix = Instruction { |
| 45 | + program_id: testing_context.get_matching_engine_program_id(), |
| 46 | + accounts: settle_auction_none_cctp_accounts.to_account_metas(None), |
| 47 | + data: SettleAuctionNoneCctpIx {}.data(), |
| 48 | + }; |
| 49 | + let tx = Transaction::new_signed_with_payer( |
| 50 | + &[settle_auction_none_ix], |
| 51 | + Some(&payer_signer.pubkey()), |
| 52 | + &[&payer_signer], |
| 53 | + testing_context |
| 54 | + .get_new_latest_blockhash(test_context) |
| 55 | + .await |
| 56 | + .unwrap(), |
| 57 | + ); |
| 58 | + |
| 59 | + testing_context |
| 60 | + .execute_and_verify_transaction(test_context, tx, config.expected_error()) |
| 61 | + .await; |
| 62 | + if config.expected_error().is_some() { |
| 63 | + return current_state.auction_state().clone(); |
| 64 | + } |
| 65 | + |
| 66 | + AuctionState::Settled |
| 67 | +} |
| 68 | + |
| 69 | +async fn create_settle_auction_none_cctp_shimless_accounts( |
| 70 | + test_context: &mut ProgramTestContext, |
| 71 | + testing_context: &TestingContext, |
| 72 | + current_state: &TestingEngineState, |
| 73 | + config: &SettleAuctionNoneInstructionConfig, |
| 74 | +) -> SettleAuctionNoneCctpAccounts { |
| 75 | + let payer = config |
| 76 | + .payer_signer |
| 77 | + .clone() |
| 78 | + .unwrap_or_else(|| testing_context.testing_actors.payer_signer.clone()); |
| 79 | + |
| 80 | + let order_prepared_state = current_state.order_prepared().unwrap(); |
| 81 | + let OrderPreparedState { |
| 82 | + prepared_order_response_address, |
| 83 | + prepared_custody_token, |
| 84 | + base_fee_token: _, |
| 85 | + prepared_by, |
| 86 | + } = *order_prepared_state; |
| 87 | + |
| 88 | + let checked_custodian = CheckedCustodian { |
| 89 | + custodian: current_state.custodian_address().unwrap(), |
| 90 | + }; |
| 91 | + |
| 92 | + let prepared_order_response_data = test_context |
| 93 | + .banks_client |
| 94 | + .get_account(prepared_order_response_address) |
| 95 | + .await |
| 96 | + .unwrap() |
| 97 | + .unwrap() |
| 98 | + .data; |
| 99 | + let prepared_order = |
| 100 | + PreparedOrderResponse::try_deserialize(&mut &prepared_order_response_data[..]).unwrap(); |
| 101 | + let auction_seeds = &[ |
| 102 | + Auction::SEED_PREFIX, |
| 103 | + &prepared_order.seeds.fast_vaa_hash.as_ref(), |
| 104 | + ]; |
| 105 | + let (auction, _bump) = Pubkey::find_program_address( |
| 106 | + auction_seeds, |
| 107 | + &testing_context.get_matching_engine_program_id(), |
| 108 | + ); |
| 109 | + let (core_message, _bump) = Pubkey::find_program_address( |
| 110 | + &[common::CORE_MESSAGE_SEED_PREFIX, &auction.as_ref()], |
| 111 | + &testing_context.get_matching_engine_program_id(), |
| 112 | + ); |
| 113 | + |
| 114 | + let (cctp_message, _bump) = Pubkey::find_program_address( |
| 115 | + &[common::CCTP_MESSAGE_SEED_PREFIX, &auction.to_bytes()], |
| 116 | + &testing_context.get_matching_engine_program_id(), |
| 117 | + ); |
| 118 | + let close_prepare_order_response = ClosePreparedOrderResponse { |
| 119 | + by: prepared_by, |
| 120 | + custody_token: prepared_custody_token, |
| 121 | + order_response: prepared_order_response_address, |
| 122 | + }; |
| 123 | + let emitter_sequence = wormhole_svm_definitions::find_emitter_sequence_address( |
| 124 | + &checked_custodian.custodian, |
| 125 | + &wormhole_svm_definitions::solana::CORE_BRIDGE_PROGRAM_ID, |
| 126 | + ) |
| 127 | + .0; |
| 128 | + let wormhole_publish_message = WormholePublishMessage { |
| 129 | + config: wormhole_svm_definitions::solana::CORE_BRIDGE_CONFIG, |
| 130 | + emitter_sequence, |
| 131 | + fee_collector: wormhole_svm_definitions::solana::CORE_BRIDGE_FEE_COLLECTOR, |
| 132 | + core_bridge_program: wormhole_svm_definitions::solana::CORE_BRIDGE_PROGRAM_ID, |
| 133 | + }; |
| 134 | + |
| 135 | + let cctp = create_cctp_deposit_for_burn(current_state, testing_context); |
| 136 | + |
| 137 | + let sysvars = RequiredSysvars { |
| 138 | + clock: Clock::id(), |
| 139 | + rent: Rent::id(), |
| 140 | + }; |
| 141 | + |
| 142 | + let event_authority = Pubkey::find_program_address( |
| 143 | + &[EVENT_AUTHORITY_SEED], |
| 144 | + &testing_context.get_matching_engine_program_id(), |
| 145 | + ) |
| 146 | + .0; |
| 147 | + |
| 148 | + let spl_token_enum = current_state |
| 149 | + .spl_token_enum() |
| 150 | + .unwrap_or_else(|| SplTokenEnum::Usdc); |
| 151 | + let fee_recipient_token = testing_context |
| 152 | + .testing_actors |
| 153 | + .fee_recipient |
| 154 | + .token_account_address(&spl_token_enum) |
| 155 | + .unwrap(); |
| 156 | + |
| 157 | + SettleAuctionNoneCctpAccounts { |
| 158 | + payer: payer.pubkey(), |
| 159 | + custodian: checked_custodian, |
| 160 | + fee_recipient_token, |
| 161 | + core_message, |
| 162 | + cctp_message, |
| 163 | + prepared: close_prepare_order_response, |
| 164 | + auction, |
| 165 | + wormhole: wormhole_publish_message, |
| 166 | + cctp, |
| 167 | + token_program: spl_token::ID, |
| 168 | + system_program: solana_program::system_program::ID, |
| 169 | + event_authority, |
| 170 | + program: testing_context.get_matching_engine_program_id(), |
| 171 | + sysvars, |
| 172 | + } |
| 173 | +} |
0 commit comments