#![cfg(feature = "test-bpf")] pub mod common; pub mod utils; use common::*; use mpl_testing_utils::{solana::airdrop, utils::Metadata}; use mtly_auction_house::pda::find_auctioneer_pda; use solana_sdk::{signature::Keypair, signer::Signer}; use std::assert_eq; use utils::{helpers::default_scopes, setup_functions::*}; #[tokio::test] async fn deposit_success() { let mut context = auction_house_program_test().start_with_context().await; // Payer Wallet let (ah, ahkey, _) = existing_auction_house_test_context(&mut context) .await .unwrap(); let test_metadata = Metadata::new(); airdrop(&mut context, &test_metadata.token.pubkey(), 1000000000) .await .unwrap(); test_metadata .create( &mut context, "Test".to_string(), "TST".to_string(), "uri".to_string(), None, 10, false, 1, ) .await .unwrap(); let buyer = Keypair::new(); airdrop(&mut context, &buyer.pubkey(), ONE_SOL * 2) .await .unwrap(); let (acc, deposit_tx) = deposit(&mut context, &ahkey, &ah, &test_metadata, &buyer, ONE_SOL); context .banks_client .process_transaction(deposit_tx) .await .unwrap(); let escrow_payment_account_data_len = 0; let rent = context.banks_client.get_rent().await.unwrap(); let rent_exempt_min: u64 = rent.minimum_balance(escrow_payment_account_data_len); let escrow = context .banks_client .get_account(acc.escrow_payment_account) .await .expect("Error Getting Escrow") .expect("Trade State Escrow"); assert_eq!(escrow.lamports, ONE_SOL + rent_exempt_min); } #[tokio::test] async fn auctioneer_deposit_success() { let mut context = auction_house_program_test().start_with_context().await; // Payer Wallet let (ah, ahkey, ah_auth) = existing_auction_house_test_context(&mut context) .await .unwrap(); let test_metadata = Metadata::new(); airdrop(&mut context, &test_metadata.token.pubkey(), 1000000000) .await .unwrap(); test_metadata .create( &mut context, "Test".to_string(), "TST".to_string(), "uri".to_string(), None, 10, false, 1, ) .await .unwrap(); let deposit_amount = 1_000_000_000; // Delegate external auctioneer authority. let auctioneer_authority = Keypair::new(); let (auctioneer_pda, _) = find_auctioneer_pda(&ahkey, &auctioneer_authority.pubkey()); delegate_auctioneer( &mut context, ahkey, &ah_auth, auctioneer_authority.pubkey(), auctioneer_pda, default_scopes(), ) .await .unwrap(); let buyer = Keypair::new(); airdrop(&mut context, &buyer.pubkey(), deposit_amount * 2) .await .unwrap(); let (acc, deposit_tx) = auctioneer_deposit( &mut context, &ahkey, &ah, &test_metadata, &buyer, &auctioneer_authority, deposit_amount, ); context .banks_client .process_transaction(deposit_tx) .await .unwrap(); let escrow_payment_account_data_len = 0; let rent = context.banks_client.get_rent().await.unwrap(); let rent_exempt_min: u64 = rent.minimum_balance(escrow_payment_account_data_len); let escrow = context .banks_client .get_account(acc.escrow_payment_account) .await .expect("Error Getting Escrow") .expect("Trade State Escrow"); assert_eq!(escrow.lamports, deposit_amount + rent_exempt_min); } #[tokio::test] async fn auctioneer_deposit_missing_scope_fails() { let mut context = auction_house_program_test().start_with_context().await; // Payer Wallet let (ah, ahkey, ah_auth) = existing_auction_house_test_context(&mut context) .await .unwrap(); let test_metadata = Metadata::new(); airdrop(&mut context, &test_metadata.token.pubkey(), 1000000000) .await .unwrap(); test_metadata .create( &mut context, "Test".to_string(), "TST".to_string(), "uri".to_string(), None, 10, false, 1, ) .await .unwrap(); // Delegate external auctioneer authority. let auctioneer_authority = Keypair::new(); let (auctioneer_pda, _) = find_auctioneer_pda(&ahkey, &auctioneer_authority.pubkey()); // Missing Deposit scope, so tx should fail. let scopes = vec![]; delegate_auctioneer( &mut context, ahkey, &ah_auth, auctioneer_authority.pubkey(), auctioneer_pda, scopes.clone(), ) .await .unwrap(); let buyer = Keypair::new(); airdrop(&mut context, &buyer.pubkey(), 2000000000) .await .unwrap(); let (_, deposit_tx) = auctioneer_deposit( &mut context, &ahkey, &ah, &test_metadata, &buyer, &auctioneer_authority, 1000000000, ); let error = context .banks_client .process_transaction(deposit_tx) .await .unwrap_err(); assert_error!(error, MISSING_AUCTIONEER_SCOPE); } #[tokio::test] async fn auctioneer_deposit_no_delegate_fails() { let mut context = auction_house_program_test().start_with_context().await; // Payer Wallet let (ah, ahkey, _) = existing_auction_house_test_context(&mut context) .await .unwrap(); let test_metadata = Metadata::new(); airdrop(&mut context, &test_metadata.token.pubkey(), 1000000000) .await .unwrap(); test_metadata .create( &mut context, "Test".to_string(), "TST".to_string(), "uri".to_string(), None, 10, false, 1, ) .await .unwrap(); // Delegate external auctioneer authority. let auctioneer_authority = Keypair::new(); let buyer = Keypair::new(); airdrop(&mut context, &buyer.pubkey(), 2000000000) .await .unwrap(); let (_acc, deposit_tx) = auctioneer_deposit( &mut context, &ahkey, &ah, &test_metadata, &buyer, &auctioneer_authority, 1000000000, ); let error = context .banks_client .process_transaction(deposit_tx) .await .unwrap_err(); assert_error!(error, ACCOUNT_NOT_INITIALIZED); }