use mpl_gavel::{ accounts::Bid, instructions::{BidBuilder, IncreaseBuilder, InitializeBuilder}, pod::WrappedAuction, types::Configuration, AggregatedConfiguration, }; use solana_program::{pubkey::Pubkey, system_instruction}; use solana_program_test::{BanksClientError, ProgramTestContext}; use solana_sdk::{signature::Keypair, signer::Signer, transaction::Transaction}; pub struct AuctionManager { pub auction: Keypair, } impl Default for AuctionManager { fn default() -> Self { Self { auction: Keypair::new(), } } } impl AuctionManager { pub async fn initialize( &mut self, context: &mut ProgramTestContext, authority: Keypair, treasury: Pubkey, args: Configuration, ) -> Result<(), BanksClientError> { let rent = context.banks_client.get_rent().await.unwrap(); let AggregatedConfiguration { capacity, .. } = AggregatedConfiguration::from(args.clone()); let expected_size = WrappedAuction::data_len(capacity as usize); let initialize_ix = InitializeBuilder::new() .auction(self.auction.pubkey()) .authority(authority.pubkey()) .treasury(treasury) .configuration(args) .instruction(); let tx = Transaction::new_signed_with_payer( &[ system_instruction::create_account( &context.payer.pubkey(), &self.auction.pubkey(), rent.minimum_balance(expected_size), expected_size as u64, &mpl_gavel::ID, ), initialize_ix, ], Some(&context.payer.pubkey()), &[&self.auction, &authority, &context.payer], context.last_blockhash, ); context.banks_client.process_transaction(tx).await } pub async fn bid( &mut self, context: &mut ProgramTestContext, owner: Keypair, payer: Keypair, amount: u64, ) -> Result<(), BanksClientError> { let (bid, _) = Bid::find_pda(&self.auction.pubkey(), &owner.pubkey()); let bid_ix = BidBuilder::new() .auction(self.auction.pubkey()) .bid(bid) .owner(owner.pubkey()) .payer(payer.pubkey()) .amount(amount) .instruction(); let tx = Transaction::new_signed_with_payer( &[bid_ix], Some(&context.payer.pubkey()), &[&owner, &payer], context.last_blockhash, ); context.banks_client.process_transaction(tx).await } pub async fn increase( &mut self, context: &mut ProgramTestContext, owner: Keypair, payer: Keypair, amount: u64, ) -> Result<(), BanksClientError> { let (bid, _) = Bid::find_pda(&self.auction.pubkey(), &owner.pubkey()); let increase_ix = IncreaseBuilder::new() .auction(self.auction.pubkey()) .bid(bid) .owner(owner.pubkey()) .payer(payer.pubkey()) .amount(amount) .instruction(); let tx = Transaction::new_signed_with_payer( &[increase_ix], Some(&context.payer.pubkey()), &[&owner, &payer], context.last_blockhash, ); context.banks_client.process_transaction(tx).await } }