#![cfg(feature = "test-bpf")] mod helpers; use helpers::*; use solana_program_test::*; use solana_sdk::{ instruction::InstructionError, pubkey::Pubkey, signature::{Keypair, Signer}, transaction::{Transaction, TransactionError}, }; use spl_token_lending::{ error::LendingError, instruction::init_obligation, math::Decimal, processor::process_instruction, state::SLOTS_PER_YEAR, }; #[tokio::test] async fn test_success() { let mut test = ProgramTest::new( "spl_token_lending", spl_token_lending::id(), processor!(process_instruction), ); // limit to track compute unit increase test.set_bpf_compute_max_units(60_000); let user_accounts_owner = Keypair::new(); let sol_usdc_dex_market = TestDexMarket::setup(&mut test, TestDexMarketPair::SOL_USDC); let usdc_mint = add_usdc_mint(&mut test); let lending_market = add_lending_market(&mut test, usdc_mint.pubkey); let usdc_reserve = add_reserve( &mut test, &user_accounts_owner, &lending_market, AddReserveArgs { slots_elapsed: SLOTS_PER_YEAR, liquidity_mint_pubkey: usdc_mint.pubkey, liquidity_mint_decimals: usdc_mint.decimals, config: TEST_RESERVE_CONFIG, ..AddReserveArgs::default() }, ); let sol_reserve = add_reserve( &mut test, &user_accounts_owner, &lending_market, AddReserveArgs { dex_market_pubkey: Some(sol_usdc_dex_market.pubkey), liquidity_mint_pubkey: spl_token::native_mint::id(), liquidity_mint_decimals: 9, config: TEST_RESERVE_CONFIG, ..AddReserveArgs::default() }, ); let (mut banks_client, payer, _recent_blockhash) = test.start().await; let obligation = TestObligation::init( &mut banks_client, &lending_market, &sol_reserve, &usdc_reserve, &payer, &user_accounts_owner, ) .await .unwrap(); obligation.validate_state(&mut banks_client).await; let obligation_token_balance = get_token_balance(&mut banks_client, obligation.token_account).await; assert_eq!(obligation_token_balance, 0); } #[tokio::test] async fn test_already_initialized() { let mut test = ProgramTest::new( "spl_token_lending", spl_token_lending::id(), processor!(process_instruction), ); // limit to track compute unit increase test.set_bpf_compute_max_units(14_000); let user_accounts_owner = Keypair::new(); let sol_usdc_dex_market = TestDexMarket::setup(&mut test, TestDexMarketPair::SOL_USDC); let usdc_mint = add_usdc_mint(&mut test); let lending_market = add_lending_market(&mut test, usdc_mint.pubkey); let usdc_reserve = add_reserve( &mut test, &user_accounts_owner, &lending_market, AddReserveArgs { slots_elapsed: SLOTS_PER_YEAR, liquidity_mint_pubkey: usdc_mint.pubkey, liquidity_mint_decimals: usdc_mint.decimals, config: TEST_RESERVE_CONFIG, ..AddReserveArgs::default() }, ); let sol_reserve = add_reserve( &mut test, &user_accounts_owner, &lending_market, AddReserveArgs { dex_market_pubkey: Some(sol_usdc_dex_market.pubkey), liquidity_mint_pubkey: spl_token::native_mint::id(), liquidity_mint_decimals: 9, config: TEST_RESERVE_CONFIG, ..AddReserveArgs::default() }, ); let usdc_obligation = add_obligation( &mut test, &user_accounts_owner, &lending_market, AddObligationArgs { borrow_reserve: &usdc_reserve, collateral_reserve: &sol_reserve, collateral_amount: 0, borrowed_liquidity_wads: Decimal::zero(), }, ); let (mut banks_client, payer, recent_blockhash) = test.start().await; let mut transaction = Transaction::new_with_payer( &[init_obligation( spl_token_lending::id(), sol_reserve.pubkey, usdc_reserve.pubkey, lending_market.pubkey, usdc_obligation.pubkey, usdc_obligation.token_mint, usdc_obligation.token_account, user_accounts_owner.pubkey(), )], Some(&payer.pubkey()), ); transaction.sign(&[&payer], recent_blockhash); assert_eq!( banks_client .process_transaction(transaction) .await .unwrap_err() .unwrap(), TransactionError::InstructionError( 0, InstructionError::Custom(LendingError::AlreadyInitialized as u32) ) ); }