2020-12-18 16:17:07 -08:00
|
|
|
#![cfg(feature = "test-bpf")]
|
|
|
|
|
|
|
|
mod helpers;
|
|
|
|
|
|
|
|
use helpers::*;
|
|
|
|
use solana_program_test::*;
|
2021-01-07 01:10:55 -08:00
|
|
|
use solana_sdk::{pubkey::Pubkey, signature::Keypair};
|
2020-12-18 16:17:07 -08:00
|
|
|
use spl_token_lending::{
|
2021-01-21 16:40:44 -08:00
|
|
|
math::Decimal, processor::process_instruction, state::INITIAL_COLLATERAL_RATIO,
|
2020-12-18 16:17:07 -08:00
|
|
|
};
|
|
|
|
|
2021-01-07 01:10:55 -08:00
|
|
|
const LAMPORTS_TO_SOL: u64 = 1_000_000_000;
|
2020-12-18 16:17:07 -08:00
|
|
|
const FRACTIONAL_TO_USDC: u64 = 1_000_000;
|
|
|
|
|
2021-01-07 01:10:55 -08:00
|
|
|
const INITIAL_SOL_RESERVE_SUPPLY_LAMPORTS: u64 = 100 * LAMPORTS_TO_SOL;
|
|
|
|
const INITIAL_USDC_RESERVE_SUPPLY_FRACTIONAL: u64 = 100 * FRACTIONAL_TO_USDC;
|
2020-12-18 16:17:07 -08:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_success() {
|
|
|
|
let mut test = ProgramTest::new(
|
|
|
|
"spl_token_lending",
|
|
|
|
spl_token_lending::id(),
|
|
|
|
processor!(process_instruction),
|
|
|
|
);
|
|
|
|
|
2021-01-07 01:10:55 -08:00
|
|
|
// limit to track compute unit increase
|
2021-03-17 01:46:18 -07:00
|
|
|
test.set_bpf_compute_max_units(200_000);
|
2021-01-07 01:10:55 -08:00
|
|
|
|
|
|
|
// set loan values to about 90% of collateral value so that it gets liquidated
|
2021-03-11 07:28:52 -08:00
|
|
|
// assumes SOL is ~$14
|
|
|
|
const USDC_LOAN: u64 = 12 * FRACTIONAL_TO_USDC;
|
2021-01-16 23:10:04 -08:00
|
|
|
const USDC_LOAN_SOL_COLLATERAL: u64 = INITIAL_COLLATERAL_RATIO * LAMPORTS_TO_SOL;
|
2021-01-07 01:10:55 -08:00
|
|
|
|
|
|
|
const SOL_LOAN: u64 = LAMPORTS_TO_SOL;
|
2021-03-11 07:28:52 -08:00
|
|
|
const SOL_LOAN_USDC_COLLATERAL: u64 = 12 * INITIAL_COLLATERAL_RATIO * FRACTIONAL_TO_USDC;
|
2020-12-18 16:17:07 -08:00
|
|
|
|
|
|
|
let user_accounts_owner = Keypair::new();
|
2021-01-06 22:01:14 -08:00
|
|
|
let sol_usdc_dex_market = TestDexMarket::setup(&mut test, TestDexMarketPair::SOL_USDC);
|
2020-12-18 16:17:07 -08:00
|
|
|
let usdc_mint = add_usdc_mint(&mut test);
|
|
|
|
let lending_market = add_lending_market(&mut test, usdc_mint.pubkey);
|
|
|
|
|
2021-01-07 01:10:55 -08:00
|
|
|
// Loans are unhealthy if borrow is more than 80% of collateral
|
|
|
|
let mut reserve_config = TEST_RESERVE_CONFIG;
|
|
|
|
reserve_config.liquidation_threshold = 80;
|
|
|
|
|
2020-12-18 16:17:07 -08:00
|
|
|
let usdc_reserve = add_reserve(
|
|
|
|
&mut test,
|
|
|
|
&user_accounts_owner,
|
|
|
|
&lending_market,
|
|
|
|
AddReserveArgs {
|
2021-01-07 01:10:55 -08:00
|
|
|
config: reserve_config,
|
2021-01-21 16:40:44 -08:00
|
|
|
initial_borrow_rate: 1,
|
2020-12-18 16:17:07 -08:00
|
|
|
liquidity_amount: INITIAL_USDC_RESERVE_SUPPLY_FRACTIONAL,
|
|
|
|
liquidity_mint_pubkey: usdc_mint.pubkey,
|
|
|
|
liquidity_mint_decimals: usdc_mint.decimals,
|
2021-01-21 16:40:44 -08:00
|
|
|
borrow_amount: USDC_LOAN * 101 / 100,
|
2021-01-07 01:10:55 -08:00
|
|
|
user_liquidity_amount: USDC_LOAN,
|
|
|
|
collateral_amount: SOL_LOAN_USDC_COLLATERAL,
|
2020-12-18 16:17:07 -08:00
|
|
|
..AddReserveArgs::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
let sol_reserve = add_reserve(
|
|
|
|
&mut test,
|
|
|
|
&user_accounts_owner,
|
|
|
|
&lending_market,
|
|
|
|
AddReserveArgs {
|
2021-01-07 01:10:55 -08:00
|
|
|
config: reserve_config,
|
2021-01-21 16:40:44 -08:00
|
|
|
initial_borrow_rate: 1,
|
2020-12-18 16:17:07 -08:00
|
|
|
liquidity_amount: INITIAL_SOL_RESERVE_SUPPLY_LAMPORTS,
|
|
|
|
liquidity_mint_decimals: 9,
|
|
|
|
liquidity_mint_pubkey: spl_token::native_mint::id(),
|
|
|
|
dex_market_pubkey: Some(sol_usdc_dex_market.pubkey),
|
2021-01-07 01:10:55 -08:00
|
|
|
collateral_amount: USDC_LOAN_SOL_COLLATERAL,
|
2021-01-21 16:40:44 -08:00
|
|
|
borrow_amount: SOL_LOAN * 101 / 100,
|
2021-01-07 01:10:55 -08:00
|
|
|
user_liquidity_amount: SOL_LOAN,
|
2020-12-18 16:17:07 -08:00
|
|
|
..AddReserveArgs::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-01-07 01:10:55 -08:00
|
|
|
let usdc_obligation = add_obligation(
|
2020-12-18 16:17:07 -08:00
|
|
|
&mut test,
|
|
|
|
&user_accounts_owner,
|
|
|
|
&lending_market,
|
2021-01-07 01:10:55 -08:00
|
|
|
AddObligationArgs {
|
|
|
|
borrow_reserve: &usdc_reserve,
|
|
|
|
collateral_reserve: &sol_reserve,
|
|
|
|
collateral_amount: USDC_LOAN_SOL_COLLATERAL,
|
|
|
|
borrowed_liquidity_wads: Decimal::from(USDC_LOAN),
|
|
|
|
},
|
2020-12-18 16:17:07 -08:00
|
|
|
);
|
|
|
|
|
2021-01-07 01:10:55 -08:00
|
|
|
let sol_obligation = add_obligation(
|
|
|
|
&mut test,
|
|
|
|
&user_accounts_owner,
|
|
|
|
&lending_market,
|
|
|
|
AddObligationArgs {
|
|
|
|
borrow_reserve: &sol_reserve,
|
|
|
|
collateral_reserve: &usdc_reserve,
|
|
|
|
collateral_amount: SOL_LOAN_USDC_COLLATERAL,
|
|
|
|
borrowed_liquidity_wads: Decimal::from(SOL_LOAN),
|
|
|
|
},
|
2020-12-18 16:17:07 -08:00
|
|
|
);
|
|
|
|
|
2021-01-07 01:10:55 -08:00
|
|
|
let (mut banks_client, payer, _recent_blockhash) = test.start().await;
|
|
|
|
|
|
|
|
lending_market
|
|
|
|
.liquidate(
|
|
|
|
&mut banks_client,
|
|
|
|
&payer,
|
|
|
|
LiquidateArgs {
|
|
|
|
repay_reserve: &usdc_reserve,
|
|
|
|
withdraw_reserve: &sol_reserve,
|
|
|
|
dex_market: &sol_usdc_dex_market,
|
|
|
|
amount: USDC_LOAN,
|
|
|
|
user_accounts_owner: &user_accounts_owner,
|
|
|
|
obligation: &usdc_obligation,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
lending_market
|
|
|
|
.liquidate(
|
|
|
|
&mut banks_client,
|
2021-01-06 11:55:56 -08:00
|
|
|
&payer,
|
2021-01-07 01:10:55 -08:00
|
|
|
LiquidateArgs {
|
|
|
|
repay_reserve: &sol_reserve,
|
|
|
|
withdraw_reserve: &usdc_reserve,
|
|
|
|
dex_market: &sol_usdc_dex_market,
|
|
|
|
amount: SOL_LOAN,
|
|
|
|
user_accounts_owner: &user_accounts_owner,
|
|
|
|
obligation: &sol_obligation,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let usdc_liquidity_supply =
|
|
|
|
get_token_balance(&mut banks_client, usdc_reserve.liquidity_supply).await;
|
|
|
|
let usdc_loan_state = usdc_obligation.get_state(&mut banks_client).await;
|
|
|
|
let usdc_liquidated = usdc_liquidity_supply - INITIAL_USDC_RESERVE_SUPPLY_FRACTIONAL;
|
|
|
|
assert!(usdc_liquidated > USDC_LOAN / 2);
|
|
|
|
assert_eq!(
|
|
|
|
usdc_liquidated,
|
2021-01-16 23:10:04 -08:00
|
|
|
usdc_loan_state
|
|
|
|
.borrowed_liquidity_wads
|
2021-01-21 17:56:19 -08:00
|
|
|
.try_floor_u64()
|
2021-01-16 23:10:04 -08:00
|
|
|
.unwrap()
|
2021-01-07 01:10:55 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
let sol_liquidity_supply =
|
|
|
|
get_token_balance(&mut banks_client, sol_reserve.liquidity_supply).await;
|
|
|
|
let sol_loan_state = sol_obligation.get_state(&mut banks_client).await;
|
|
|
|
let sol_liquidated = sol_liquidity_supply - INITIAL_SOL_RESERVE_SUPPLY_LAMPORTS;
|
|
|
|
assert!(sol_liquidated > SOL_LOAN / 2);
|
|
|
|
assert_eq!(
|
|
|
|
sol_liquidated,
|
2021-01-16 23:10:04 -08:00
|
|
|
sol_loan_state
|
|
|
|
.borrowed_liquidity_wads
|
2021-01-21 17:56:19 -08:00
|
|
|
.try_floor_u64()
|
2021-01-16 23:10:04 -08:00
|
|
|
.unwrap()
|
2020-12-18 16:17:07 -08:00
|
|
|
);
|
|
|
|
}
|