|
| 1 | +use anchor_lang::prelude::*; |
| 2 | +use anchor_spl::token::spl_token; |
| 3 | +use matching_engine::{ |
| 4 | + fallback::{ |
| 5 | + settle_auction_none_cctp::{ |
| 6 | + SettleAuctionNoneCctpShimAccounts, SettleAuctionNoneCctpShimData, |
| 7 | + }, |
| 8 | + FallbackMatchingEngineInstruction, |
| 9 | + }, |
| 10 | + state::Auction, |
| 11 | +}; |
| 12 | +use solana_program::instruction::Instruction; |
| 13 | +use solana_program_test::ProgramTestContext; |
| 14 | +use solana_sdk::{signature::Signer, sysvar::SysvarId, transaction::Transaction}; |
| 15 | +use wormhole_svm_definitions::solana::{ |
| 16 | + CORE_BRIDGE_PROGRAM_ID, POST_MESSAGE_SHIM_EVENT_AUTHORITY, POST_MESSAGE_SHIM_PROGRAM_ID, |
| 17 | +}; |
| 18 | + |
| 19 | +use crate::{ |
| 20 | + testing_engine::{ |
| 21 | + config::{InstructionConfig, SettleAuctionNoneInstructionConfig}, |
| 22 | + setup::TestingContext, |
| 23 | + state::{OrderPreparedState, TestingEngineState}, |
| 24 | + }, |
| 25 | + utils::{ |
| 26 | + auction::AuctionState, token_account::SplTokenEnum, CORE_BRIDGE_CONFIG, |
| 27 | + CORE_BRIDGE_FEE_COLLECTOR, |
| 28 | + }, |
| 29 | +}; |
| 30 | + |
| 31 | +use super::shims_execute_order::{create_cctp_accounts, CctpAccounts}; |
| 32 | + |
| 33 | +pub struct SettleAuctionNoneCctpShim<'ix> { |
| 34 | + pub program_id: &'ix Pubkey, |
| 35 | + pub accounts: SettleAuctionNoneCctpShimAccounts<'ix>, |
| 36 | + pub data: SettleAuctionNoneCctpShimData, |
| 37 | +} |
| 38 | + |
| 39 | +impl SettleAuctionNoneCctpShim<'_> { |
| 40 | + pub fn instruction(self) -> Instruction { |
| 41 | + Instruction { |
| 42 | + program_id: *self.program_id, |
| 43 | + accounts: self.accounts.to_account_metas(), |
| 44 | + data: FallbackMatchingEngineInstruction::SettleAuctionNoneCctpShim(&self.data).to_vec(), |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +pub async fn settle_auction_none_shimful( |
| 50 | + testing_context: &TestingContext, |
| 51 | + test_context: &mut ProgramTestContext, |
| 52 | + current_state: &TestingEngineState, |
| 53 | + config: &SettleAuctionNoneInstructionConfig, |
| 54 | +) -> AuctionState { |
| 55 | + let payer_signer = &config |
| 56 | + .payer_signer |
| 57 | + .clone() |
| 58 | + .unwrap_or_else(|| testing_context.testing_actors.payer_signer.clone()); |
| 59 | + |
| 60 | + let settle_auction_none_cctp_accounts = |
| 61 | + create_settle_auction_none_cctp_shimful_accounts(testing_context, current_state, config); |
| 62 | + let settle_auction_none_cctp_data = settle_auction_none_cctp_accounts.bumps; |
| 63 | + |
| 64 | + let settle_auction_none_cctp_ix = SettleAuctionNoneCctpShim { |
| 65 | + program_id: &testing_context.get_matching_engine_program_id(), |
| 66 | + accounts: settle_auction_none_cctp_accounts.as_ref(), |
| 67 | + data: settle_auction_none_cctp_data, |
| 68 | + } |
| 69 | + .instruction(); |
| 70 | + let last_blockhash = test_context.get_new_latest_blockhash().await.unwrap(); |
| 71 | + let tx = Transaction::new_signed_with_payer( |
| 72 | + &[settle_auction_none_cctp_ix], |
| 73 | + Some(&payer_signer.pubkey()), |
| 74 | + &[&payer_signer], |
| 75 | + last_blockhash, |
| 76 | + ); |
| 77 | + testing_context |
| 78 | + .execute_and_verify_transaction(test_context, tx, config.expected_error()) |
| 79 | + .await; |
| 80 | + if config.expected_error().is_some() { |
| 81 | + return current_state.auction_state().clone(); |
| 82 | + } |
| 83 | + |
| 84 | + AuctionState::Settled |
| 85 | +} |
| 86 | + |
| 87 | +struct SettleAuctionNoneCctpShimAccountsOwned { |
| 88 | + pub payer: Pubkey, |
| 89 | + pub post_message_message: Pubkey, |
| 90 | + pub post_message_sequence: Pubkey, |
| 91 | + pub post_message_shim_event_authority: Pubkey, |
| 92 | + pub post_message_shim_program: Pubkey, |
| 93 | + pub cctp_message: Pubkey, |
| 94 | + pub custodian: Pubkey, |
| 95 | + pub fee_recipient_token: Pubkey, |
| 96 | + pub closed_prepared_order_response_actor: Pubkey, |
| 97 | + pub closed_prepared_order_response: Pubkey, |
| 98 | + pub closed_prepared_order_response_custody_token: Pubkey, |
| 99 | + pub auction: Pubkey, |
| 100 | + pub cctp_mint: Pubkey, |
| 101 | + pub cctp_local_token: Pubkey, |
| 102 | + pub cctp_token_messenger_minter_event_authority: Pubkey, |
| 103 | + pub cctp_remote_token_messenger: Pubkey, |
| 104 | + pub cctp_token_messenger: Pubkey, |
| 105 | + pub cctp_token_messenger_minter_sender_authority: Pubkey, |
| 106 | + pub cctp_token_minter: Pubkey, |
| 107 | + pub cctp_token_messenger_minter_program: Pubkey, |
| 108 | + pub cctp_message_transmitter_config: Pubkey, |
| 109 | + pub cctp_message_transmitter_program: Pubkey, |
| 110 | + pub core_bridge_program: Pubkey, |
| 111 | + pub core_bridge_fee_collector: Pubkey, |
| 112 | + pub core_bridge_config: Pubkey, |
| 113 | + pub token_program: Pubkey, |
| 114 | + pub system_program: Pubkey, |
| 115 | + pub clock: Pubkey, |
| 116 | + pub rent: Pubkey, |
| 117 | + pub bumps: SettleAuctionNoneCctpShimData, |
| 118 | +} |
| 119 | + |
| 120 | +impl SettleAuctionNoneCctpShimAccountsOwned { |
| 121 | + pub fn as_ref(&self) -> SettleAuctionNoneCctpShimAccounts { |
| 122 | + SettleAuctionNoneCctpShimAccounts { |
| 123 | + payer: &self.payer, |
| 124 | + post_shim_message: &self.post_message_message, |
| 125 | + core_bridge_emitter_sequence: &self.post_message_sequence, |
| 126 | + post_message_shim_event_authority: &self.post_message_shim_event_authority, |
| 127 | + post_message_shim_program: &self.post_message_shim_program, |
| 128 | + cctp_message: &self.cctp_message, |
| 129 | + custodian: &self.custodian, |
| 130 | + fee_recipient_token: &self.fee_recipient_token, |
| 131 | + closed_prepared_order_response_actor: &self.closed_prepared_order_response_actor, |
| 132 | + closed_prepared_order_response: &self.closed_prepared_order_response, |
| 133 | + closed_prepared_order_response_custody_token: &self |
| 134 | + .closed_prepared_order_response_custody_token, |
| 135 | + auction: &self.auction, |
| 136 | + cctp_mint: &self.cctp_mint, |
| 137 | + cctp_local_token: &self.cctp_local_token, |
| 138 | + cctp_token_messenger_minter_event_authority: &self |
| 139 | + .cctp_token_messenger_minter_event_authority, |
| 140 | + cctp_remote_token_messenger: &self.cctp_remote_token_messenger, |
| 141 | + cctp_token_messenger: &self.cctp_token_messenger, |
| 142 | + cctp_token_messenger_minter_sender_authority: &self |
| 143 | + .cctp_token_messenger_minter_sender_authority, |
| 144 | + cctp_token_minter: &self.cctp_token_minter, |
| 145 | + cctp_token_messenger_minter_program: &self.cctp_token_messenger_minter_program, |
| 146 | + cctp_message_transmitter_config: &self.cctp_message_transmitter_config, |
| 147 | + cctp_message_transmitter_program: &self.cctp_message_transmitter_program, |
| 148 | + core_bridge_program: &self.core_bridge_program, |
| 149 | + core_bridge_fee_collector: &self.core_bridge_fee_collector, |
| 150 | + core_bridge_config: &self.core_bridge_config, |
| 151 | + token_program: &self.token_program, |
| 152 | + system_program: &self.system_program, |
| 153 | + clock: &self.clock, |
| 154 | + rent: &self.rent, |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +fn create_settle_auction_none_cctp_shimful_accounts( |
| 160 | + testing_context: &TestingContext, |
| 161 | + current_state: &TestingEngineState, |
| 162 | + config: &SettleAuctionNoneInstructionConfig, |
| 163 | +) -> SettleAuctionNoneCctpShimAccountsOwned { |
| 164 | + let payer_signer = &config |
| 165 | + .payer_signer |
| 166 | + .clone() |
| 167 | + .unwrap_or_else(|| testing_context.testing_actors.payer_signer.clone()); |
| 168 | + |
| 169 | + let order_prepared_state = current_state.order_prepared().unwrap(); |
| 170 | + let OrderPreparedState { |
| 171 | + prepared_order_response_address, |
| 172 | + prepared_custody_token, |
| 173 | + base_fee_token, |
| 174 | + actor_enum: _, |
| 175 | + prepared_by, |
| 176 | + } = *order_prepared_state; |
| 177 | + |
| 178 | + let custodian = current_state |
| 179 | + .custodian_address() |
| 180 | + .expect("Custodian address not found"); |
| 181 | + println!("Settle auction custodian address: {:?}", custodian); |
| 182 | + |
| 183 | + let fast_market_order = current_state.fast_market_order().unwrap().fast_market_order; |
| 184 | + let fast_vaa_hash = fast_market_order.digest(); |
| 185 | + let (auction, auction_bump) = Pubkey::find_program_address( |
| 186 | + &[Auction::SEED_PREFIX, fast_vaa_hash.as_ref()], |
| 187 | + &testing_context.get_matching_engine_program_id(), |
| 188 | + ); |
| 189 | + |
| 190 | + let (cctp_message, cctp_message_bump) = Pubkey::find_program_address( |
| 191 | + &[common::CCTP_MESSAGE_SEED_PREFIX, &auction.to_bytes()], |
| 192 | + &testing_context.get_matching_engine_program_id(), |
| 193 | + ); |
| 194 | + |
| 195 | + let post_message_sequence = wormhole_svm_definitions::find_emitter_sequence_address( |
| 196 | + &custodian, |
| 197 | + &CORE_BRIDGE_PROGRAM_ID, |
| 198 | + ) |
| 199 | + .0; |
| 200 | + let post_message_message = wormhole_svm_definitions::find_shim_message_address( |
| 201 | + &custodian, |
| 202 | + &POST_MESSAGE_SHIM_PROGRAM_ID, |
| 203 | + ) |
| 204 | + .0; |
| 205 | + |
| 206 | + let fee_recipient_token = testing_context |
| 207 | + .testing_actors |
| 208 | + .fee_recipient |
| 209 | + .token_account_address(&SplTokenEnum::Usdc) |
| 210 | + .unwrap(); |
| 211 | + |
| 212 | + let CctpAccounts { |
| 213 | + mint, |
| 214 | + local_token, |
| 215 | + token_messenger_minter_event_authority, |
| 216 | + remote_token_messenger, |
| 217 | + token_messenger, |
| 218 | + token_messenger_minter_sender_authority, |
| 219 | + token_minter, |
| 220 | + token_messenger_minter_program, |
| 221 | + message_transmitter_config, |
| 222 | + message_transmitter_program, |
| 223 | + } = create_cctp_accounts(current_state, testing_context); |
| 224 | + SettleAuctionNoneCctpShimAccountsOwned { |
| 225 | + payer: payer_signer.pubkey(), |
| 226 | + post_message_message, |
| 227 | + post_message_sequence, |
| 228 | + post_message_shim_event_authority: POST_MESSAGE_SHIM_EVENT_AUTHORITY, |
| 229 | + post_message_shim_program: POST_MESSAGE_SHIM_PROGRAM_ID, |
| 230 | + cctp_message, |
| 231 | + custodian, |
| 232 | + fee_recipient_token, |
| 233 | + closed_prepared_order_response_actor: prepared_by, |
| 234 | + closed_prepared_order_response: prepared_order_response_address, |
| 235 | + closed_prepared_order_response_custody_token: prepared_custody_token, |
| 236 | + auction, |
| 237 | + cctp_mint: mint, |
| 238 | + cctp_local_token: local_token, |
| 239 | + cctp_token_messenger_minter_event_authority: token_messenger_minter_event_authority, |
| 240 | + cctp_remote_token_messenger: remote_token_messenger, |
| 241 | + cctp_token_messenger: token_messenger, |
| 242 | + cctp_token_messenger_minter_sender_authority: token_messenger_minter_sender_authority, |
| 243 | + cctp_token_minter: token_minter, |
| 244 | + cctp_token_messenger_minter_program: token_messenger_minter_program, |
| 245 | + cctp_message_transmitter_config: message_transmitter_config, |
| 246 | + cctp_message_transmitter_program: message_transmitter_program, |
| 247 | + core_bridge_program: CORE_BRIDGE_PROGRAM_ID, |
| 248 | + core_bridge_fee_collector: CORE_BRIDGE_FEE_COLLECTOR, |
| 249 | + core_bridge_config: CORE_BRIDGE_CONFIG, |
| 250 | + token_program: spl_token::ID, |
| 251 | + system_program: solana_program::system_program::ID, |
| 252 | + clock: solana_program::clock::Clock::id(), |
| 253 | + rent: solana_program::rent::Rent::id(), |
| 254 | + bumps: SettleAuctionNoneCctpShimData { |
| 255 | + cctp_message_bump, |
| 256 | + auction_bump, |
| 257 | + }, |
| 258 | + } |
| 259 | +} |
0 commit comments