forked from solana-labs/solana-program-library
-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
whitelist liquidations #61
Open
nope-finance
wants to merge
3
commits into
upcoming
Choose a base branch
from
whitelist_liquidators
base: upcoming
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,14 +6,18 @@ use helpers::*; | |
use solana_program_test::*; | ||
use solana_sdk::{ | ||
pubkey::Pubkey, | ||
signature::{Keypair, Signer}, | ||
transaction::Transaction, | ||
signature::{read_keypair_file, Keypair, Signer}, | ||
transaction::{Transaction, TransactionError}, | ||
}; | ||
use spl_token::{ | ||
instruction::approve, | ||
solana_program::instruction::InstructionError, | ||
}; | ||
use spl_token::instruction::approve; | ||
use spl_token_lending::{ | ||
instruction::{liquidate_obligation, refresh_obligation}, | ||
processor::process_instruction, | ||
state::INITIAL_COLLATERAL_RATIO, | ||
error::LendingError, | ||
}; | ||
|
||
#[tokio::test] | ||
|
@@ -40,7 +44,8 @@ async fn test_success() { | |
const USDC_RESERVE_LIQUIDITY_FRACTIONAL: u64 = 2 * USDC_BORROW_AMOUNT_FRACTIONAL; | ||
|
||
let user_accounts_owner = Keypair::new(); | ||
let user_transfer_authority = Keypair::new(); | ||
let user_transfer_authority = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. have you tested with a non whitelisted liquidator? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point yeh let me add |
||
read_keypair_file("tests/fixtures/lending_market_owner.json").unwrap(); | ||
let lending_market = add_lending_market(&mut test); | ||
|
||
let mut reserve_config = test_reserve_config(); | ||
|
@@ -182,3 +187,143 @@ async fn test_success() { | |
(USDC_BORROW_AMOUNT_FRACTIONAL - USDC_LIQUIDATION_AMOUNT_FRACTIONAL).into() | ||
) | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_not_whitelist() { | ||
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(51_000); | ||
|
||
// 100 SOL collateral | ||
const SOL_DEPOSIT_AMOUNT_LAMPORTS: u64 = 100 * LAMPORTS_TO_SOL * INITIAL_COLLATERAL_RATIO; | ||
// 100 SOL * 80% LTV -> 80 SOL * 20 USDC -> 1600 USDC borrow | ||
const USDC_BORROW_AMOUNT_FRACTIONAL: u64 = 1_600 * FRACTIONAL_TO_USDC; | ||
// 1600 USDC * 50% -> 800 USDC liquidation | ||
const USDC_LIQUIDATION_AMOUNT_FRACTIONAL: u64 = USDC_BORROW_AMOUNT_FRACTIONAL / 2; | ||
// 800 USDC / 20 USDC per SOL -> 40 SOL + 10% bonus -> 44 SOL | ||
const _SOL_LIQUIDATION_AMOUNT_LAMPORTS: u64 = 44 * LAMPORTS_TO_SOL * INITIAL_COLLATERAL_RATIO; | ||
|
||
const SOL_RESERVE_COLLATERAL_LAMPORTS: u64 = 2 * SOL_DEPOSIT_AMOUNT_LAMPORTS; | ||
const USDC_RESERVE_LIQUIDITY_FRACTIONAL: u64 = 2 * USDC_BORROW_AMOUNT_FRACTIONAL; | ||
|
||
let user_accounts_owner = Keypair::new(); | ||
let user_transfer_authority = Keypair::new(); | ||
let lending_market = add_lending_market(&mut test); | ||
|
||
let mut reserve_config = test_reserve_config(); | ||
reserve_config.loan_to_value_ratio = 50; | ||
reserve_config.liquidation_threshold = 80; | ||
reserve_config.liquidation_bonus = 10; | ||
|
||
let sol_oracle = add_sol_oracle(&mut test); | ||
let sol_test_reserve = add_reserve( | ||
&mut test, | ||
&lending_market, | ||
&sol_oracle, | ||
&user_accounts_owner, | ||
AddReserveArgs { | ||
collateral_amount: SOL_RESERVE_COLLATERAL_LAMPORTS, | ||
liquidity_mint_pubkey: spl_token::native_mint::id(), | ||
liquidity_mint_decimals: 9, | ||
config: reserve_config, | ||
mark_fresh: true, | ||
..AddReserveArgs::default() | ||
}, | ||
); | ||
|
||
let usdc_mint = add_usdc_mint(&mut test); | ||
let usdc_oracle = add_usdc_oracle(&mut test); | ||
let usdc_test_reserve = add_reserve( | ||
&mut test, | ||
&lending_market, | ||
&usdc_oracle, | ||
&user_accounts_owner, | ||
AddReserveArgs { | ||
borrow_amount: USDC_BORROW_AMOUNT_FRACTIONAL, | ||
user_liquidity_amount: USDC_BORROW_AMOUNT_FRACTIONAL, | ||
liquidity_amount: USDC_RESERVE_LIQUIDITY_FRACTIONAL, | ||
liquidity_mint_pubkey: usdc_mint.pubkey, | ||
liquidity_mint_decimals: usdc_mint.decimals, | ||
config: reserve_config, | ||
mark_fresh: true, | ||
..AddReserveArgs::default() | ||
}, | ||
); | ||
|
||
let test_obligation = add_obligation( | ||
&mut test, | ||
&lending_market, | ||
&user_accounts_owner, | ||
AddObligationArgs { | ||
deposits: &[(&sol_test_reserve, SOL_DEPOSIT_AMOUNT_LAMPORTS)], | ||
borrows: &[(&usdc_test_reserve, USDC_BORROW_AMOUNT_FRACTIONAL)], | ||
..AddObligationArgs::default() | ||
}, | ||
); | ||
|
||
let (mut banks_client, payer, recent_blockhash) = test.start().await; | ||
|
||
let _initial_user_liquidity_balance = | ||
get_token_balance(&mut banks_client, usdc_test_reserve.user_liquidity_pubkey).await; | ||
let _initial_liquidity_supply_balance = | ||
get_token_balance(&mut banks_client, usdc_test_reserve.liquidity_supply_pubkey).await; | ||
let _initial_user_collateral_balance = | ||
get_token_balance(&mut banks_client, sol_test_reserve.user_collateral_pubkey).await; | ||
let _initial_collateral_supply_balance = | ||
get_token_balance(&mut banks_client, sol_test_reserve.collateral_supply_pubkey).await; | ||
|
||
let mut transaction = Transaction::new_with_payer( | ||
&[ | ||
approve( | ||
&spl_token::id(), | ||
&usdc_test_reserve.user_liquidity_pubkey, | ||
&user_transfer_authority.pubkey(), | ||
&user_accounts_owner.pubkey(), | ||
&[], | ||
USDC_LIQUIDATION_AMOUNT_FRACTIONAL, | ||
) | ||
.unwrap(), | ||
refresh_obligation( | ||
spl_token_lending::id(), | ||
test_obligation.pubkey, | ||
vec![sol_test_reserve.pubkey, usdc_test_reserve.pubkey], | ||
), | ||
liquidate_obligation( | ||
spl_token_lending::id(), | ||
USDC_LIQUIDATION_AMOUNT_FRACTIONAL, | ||
usdc_test_reserve.user_liquidity_pubkey, | ||
sol_test_reserve.user_collateral_pubkey, | ||
usdc_test_reserve.pubkey, | ||
usdc_test_reserve.liquidity_supply_pubkey, | ||
sol_test_reserve.pubkey, | ||
sol_test_reserve.collateral_supply_pubkey, | ||
test_obligation.pubkey, | ||
lending_market.pubkey, | ||
user_transfer_authority.pubkey(), | ||
), | ||
], | ||
Some(&payer.pubkey()), | ||
); | ||
|
||
transaction.sign( | ||
&[&payer, &user_accounts_owner, &user_transfer_authority], | ||
recent_blockhash, | ||
); | ||
assert_eq!( | ||
banks_client | ||
.process_transaction(transaction) | ||
.await | ||
.unwrap_err() | ||
.unwrap(), | ||
TransactionError::InstructionError( | ||
2, | ||
InstructionError::Custom(LendingError::NonWhitelistLiquidator as u32) | ||
) | ||
); | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This account has on-chain transactions from a year ago. Is that account's private key leaked?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's literally in the repo so yeah
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its the account they use for tests in repo