Skip to content
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

Oracleless ctoken mint and redeem #69

Merged
merged 4 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions token-lending/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub enum LendingInstruction {
/// 0. `[writable]` Source collateral token account.
/// $authority can transfer $collateral_amount.
/// 1. `[writable]` Destination liquidity token account.
/// 2. `[writable]` Reserve account. - refreshed
/// 2. `[writable]` Reserve account.
/// 3. `[writable]` Reserve collateral SPL Token mint.
/// 4. `[writable]` Reserve liquidity supply SPL Token account.
/// 5. `[]` Lending market account.
Expand Down Expand Up @@ -161,15 +161,15 @@ pub enum LendingInstruction {
RefreshObligation,

// 8
/// Deposit collateral to an obligation. Requires a refreshed reserve.
/// Deposit collateral to an obligation.
///
/// Accounts expected by this instruction:
///
/// 0. `[writable]` Source collateral token account.
/// Minted by deposit reserve collateral mint.
/// $authority can transfer $collateral_amount.
/// 1. `[writable]` Destination deposit reserve collateral supply SPL Token account.
/// 2. `[]` Deposit reserve account - refreshed.
/// 2. `[writable]` Deposit reserve account.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Do we need to make the same comment change for RedeemReserveCollateral given it no longer needs to be refreshed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yeh prob should change comment (the reserve over there was alright writable so just removing the "refreshed"

/// 3. `[writable]` Obligation account.
/// 4. `[]` Lending market account.
/// 5. `[signer]` Obligation owner.
Expand Down Expand Up @@ -948,7 +948,7 @@ pub fn deposit_obligation_collateral(
accounts: vec![
AccountMeta::new(source_collateral_pubkey, false),
AccountMeta::new(destination_collateral_pubkey, false),
AccountMeta::new_readonly(deposit_reserve_pubkey, false),
AccountMeta::new(deposit_reserve_pubkey, false),
AccountMeta::new(obligation_pubkey, false),
AccountMeta::new_readonly(lending_market_pubkey, false),
AccountMeta::new_readonly(obligation_owner_pubkey, true),
Expand Down
19 changes: 16 additions & 3 deletions token-lending/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ fn process_deposit_reserve_liquidity(
let clock = &Clock::from_account_info(next_account_info(account_info_iter)?)?;
let token_program_id = next_account_info(account_info_iter)?;

// We don't care about the return value here, so just ignore it.
_refresh_reserve_interest(program_id, reserve_info, clock)?;
_deposit_reserve_liquidity(
program_id,
liquidity_amount,
Expand All @@ -473,6 +473,7 @@ fn process_deposit_reserve_liquidity(
clock,
token_program_id,
)?;

Ok(())
}

Expand Down Expand Up @@ -599,6 +600,8 @@ fn process_redeem_reserve_collateral(
let user_transfer_authority_info = next_account_info(account_info_iter)?;
let clock = &Clock::from_account_info(next_account_info(account_info_iter)?)?;
let token_program_id = next_account_info(account_info_iter)?;

_refresh_reserve_interest(program_id, reserve_info, clock)?;
_redeem_reserve_collateral(
program_id,
collateral_amount,
Expand All @@ -612,7 +615,12 @@ fn process_redeem_reserve_collateral(
user_transfer_authority_info,
clock,
token_program_id,
)
)?;
let mut reserve = Reserve::unpack(&reserve_info.data.borrow())?;
reserve.last_update.mark_stale();
Reserve::pack(reserve, &mut reserve_info.data.borrow_mut())?;

Ok(())
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -896,6 +904,7 @@ fn process_deposit_obligation_collateral(
let user_transfer_authority_info = next_account_info(account_info_iter)?;
let clock = &Clock::from_account_info(next_account_info(account_info_iter)?)?;
let token_program_id = next_account_info(account_info_iter)?;
_refresh_reserve_interest(program_id, deposit_reserve_info, clock)?;
_deposit_obligation_collateral(
program_id,
collateral_amount,
Expand All @@ -908,7 +917,11 @@ fn process_deposit_obligation_collateral(
user_transfer_authority_info,
clock,
token_program_id,
)
)?;
let mut reserve = Reserve::unpack(&deposit_reserve_info.data.borrow())?;
reserve.last_update.mark_stale();
Reserve::pack(reserve, &mut deposit_reserve_info.data.borrow_mut())?;
Ok(())
}

#[allow(clippy::too_many_arguments)]
Expand Down
19 changes: 18 additions & 1 deletion token-lending/program/tests/deposit_obligation_collateral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ async fn test_success() {

const SOL_DEPOSIT_AMOUNT_LAMPORTS: u64 = 10 * LAMPORTS_TO_SOL * INITIAL_COLLATERAL_RATIO;
const SOL_RESERVE_COLLATERAL_LAMPORTS: u64 = 2 * SOL_DEPOSIT_AMOUNT_LAMPORTS;
const SOL_BORROWED_AMOUNT_LAMPORTS: u64 = SOL_DEPOSIT_AMOUNT_LAMPORTS;

let user_accounts_owner = Keypair::new();
let user_transfer_authority = Keypair::new();
Expand All @@ -45,6 +46,7 @@ async fn test_success() {
liquidity_amount: SOL_RESERVE_COLLATERAL_LAMPORTS,
liquidity_mint_decimals: 9,
liquidity_mint_pubkey: spl_token::native_mint::id(),
borrow_amount: SOL_BORROWED_AMOUNT_LAMPORTS,
config: test_reserve_config(),
mark_fresh: true,
..AddReserveArgs::default()
Expand All @@ -58,14 +60,24 @@ async fn test_success() {
AddObligationArgs::default(),
);

let (mut banks_client, payer, recent_blockhash) = test.start().await;
let mut test_context = test.start_with_context().await;
test_context.warp_to_slot(300).unwrap(); // clock.slot = 300

let ProgramTestContext {
mut banks_client,
payer,
last_blockhash: recent_blockhash,
..
} = test_context;

test_obligation.validate_state(&mut banks_client).await;

let initial_collateral_supply_balance =
get_token_balance(&mut banks_client, sol_test_reserve.collateral_supply_pubkey).await;
let initial_user_collateral_balance =
get_token_balance(&mut banks_client, sol_test_reserve.user_collateral_pubkey).await;
let pre_sol_reserve = sol_test_reserve.get_state(&mut banks_client).await;
let old_borrow_rate = pre_sol_reserve.liquidity.cumulative_borrow_rate_wads;

let mut transaction = Transaction::new_with_payer(
&[
Expand Down Expand Up @@ -99,6 +111,9 @@ async fn test_success() {
);
assert!(banks_client.process_transaction(transaction).await.is_ok());

let sol_reserve = sol_test_reserve.get_state(&mut banks_client).await;
assert_eq!(sol_reserve.last_update.stale, true);

// check that collateral tokens were transferred
let collateral_supply_balance =
get_token_balance(&mut banks_client, sol_test_reserve.collateral_supply_pubkey).await;
Expand All @@ -112,4 +127,6 @@ async fn test_success() {
user_collateral_balance,
initial_user_collateral_balance - SOL_DEPOSIT_AMOUNT_LAMPORTS
);

assert!(sol_reserve.liquidity.cumulative_borrow_rate_wads > old_borrow_rate);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
30 changes: 28 additions & 2 deletions token-lending/program/tests/deposit_reserve_liquidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async fn test_success() {
);

// limit to track compute unit increase
test.set_bpf_compute_max_units(30_000);
test.set_bpf_compute_max_units(45_000);

let user_accounts_owner = Keypair::new();
let lending_market = add_lending_market(&mut test);
Expand All @@ -33,13 +33,26 @@ async fn test_success() {
liquidity_amount: 10_000 * FRACTIONAL_TO_USDC,
liquidity_mint_decimals: usdc_mint.decimals,
liquidity_mint_pubkey: usdc_mint.pubkey,
borrow_amount: 5_000 * FRACTIONAL_TO_USDC,
config: test_reserve_config(),
mark_fresh: true,
..AddReserveArgs::default()
},
);

let (mut banks_client, payer, _recent_blockhash) = test.start().await;
let mut test_context = test.start_with_context().await;
test_context.warp_to_slot(300).unwrap(); // clock.slot = 300

let ProgramTestContext {
mut banks_client,
payer,
..
} = test_context;

let initial_ctoken_amount =
get_token_balance(&mut banks_client, usdc_test_reserve.user_collateral_pubkey).await;
let pre_usdc_reserve = usdc_test_reserve.get_state(&mut banks_client).await;
let old_borrow_rate = pre_usdc_reserve.liquidity.cumulative_borrow_rate_wads;

lending_market
.deposit(
Expand All @@ -50,4 +63,17 @@ async fn test_success() {
100 * FRACTIONAL_TO_USDC,
)
.await;

let usdc_reserve = usdc_test_reserve.get_state(&mut banks_client).await;
assert_eq!(usdc_reserve.last_update.stale, true);

let user_remaining_liquidity_amount =
get_token_balance(&mut banks_client, usdc_test_reserve.user_liquidity_pubkey).await;
assert_eq!(user_remaining_liquidity_amount, 0);

let final_ctoken_amount =
get_token_balance(&mut banks_client, usdc_test_reserve.user_collateral_pubkey).await;
assert!(final_ctoken_amount - initial_ctoken_amount < 100 * FRACTIONAL_TO_USDC);

assert!(usdc_reserve.liquidity.cumulative_borrow_rate_wads > old_borrow_rate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ async fn test_success() {

test_obligation.validate_state(&mut banks_client).await;

// let initial_collateral_supply_balance =
// get_token_balance(&mut banks_client, usdc_test_reserve.collateral_supply_pubkey).await;
// let initial_user_collateral_balance =
// get_token_balance(&mut banks_client, usdc_test_reserve.user_collateral_pubkey).await;

lending_market
.deposit_obligation_and_collateral(
&mut banks_client,
Expand Down
56 changes: 25 additions & 31 deletions token-lending/program/tests/obligation_end_to_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,6 @@ async fn test_success() {
user_accounts_owner_pubkey,
),
// 2
refresh_reserve(
spl_token_lending::id(),
sol_test_reserve.pubkey,
sol_oracle.pyth_price_pubkey,
sol_oracle.switchboard_feed_pubkey,
),
// 3
approve(
&spl_token::id(),
&sol_test_reserve.user_collateral_pubkey,
Expand All @@ -139,7 +132,7 @@ async fn test_success() {
SOL_DEPOSIT_AMOUNT_LAMPORTS,
)
.unwrap(),
// 4
// 3
deposit_obligation_collateral(
spl_token_lending::id(),
SOL_DEPOSIT_AMOUNT_LAMPORTS,
Expand All @@ -151,19 +144,26 @@ async fn test_success() {
user_accounts_owner_pubkey,
user_transfer_authority_pubkey,
),
// 5
refresh_obligation(
spl_token_lending::id(),
obligation_pubkey,
vec![sol_test_reserve.pubkey],
),
// 6
// 4
refresh_reserve(
spl_token_lending::id(),
usdc_test_reserve.pubkey,
usdc_oracle.pyth_price_pubkey,
usdc_oracle.switchboard_feed_pubkey,
),
// 5
refresh_reserve(
spl_token_lending::id(),
sol_test_reserve.pubkey,
sol_oracle.pyth_price_pubkey,
sol_oracle.switchboard_feed_pubkey,
),
// 6
refresh_obligation(
spl_token_lending::id(),
obligation_pubkey,
vec![sol_test_reserve.pubkey],
),
// 7
borrow_obligation_liquidity(
spl_token_lending::id(),
Expand All @@ -178,19 +178,6 @@ async fn test_success() {
Some(usdc_test_reserve.liquidity_host_pubkey),
),
// 8
refresh_reserve(
spl_token_lending::id(),
usdc_test_reserve.pubkey,
usdc_oracle.pyth_price_pubkey,
usdc_oracle.switchboard_feed_pubkey,
),
// 9
refresh_obligation(
spl_token_lending::id(),
obligation_pubkey,
vec![sol_test_reserve.pubkey, usdc_test_reserve.pubkey],
),
// 10
approve(
&spl_token::id(),
&usdc_test_reserve.user_liquidity_pubkey,
Expand All @@ -200,7 +187,7 @@ async fn test_success() {
USDC_REPAY_AMOUNT_FRACTIONAL,
)
.unwrap(),
// 11
// 9
repay_obligation_liquidity(
spl_token_lending::id(),
USDC_REPAY_AMOUNT_FRACTIONAL,
Expand All @@ -211,13 +198,20 @@ async fn test_success() {
lending_market.pubkey,
user_transfer_authority_pubkey,
),
// 12
// 10
refresh_reserve(
spl_token_lending::id(),
usdc_test_reserve.pubkey,
usdc_oracle.pyth_price_pubkey,
usdc_oracle.switchboard_feed_pubkey,
),
// 11
refresh_obligation(
spl_token_lending::id(),
obligation_pubkey,
vec![sol_test_reserve.pubkey],
),
// 13
// 12
withdraw_obligation_collateral(
spl_token_lending::id(),
SOL_DEPOSIT_AMOUNT_LAMPORTS,
Expand Down
24 changes: 21 additions & 3 deletions token-lending/program/tests/redeem_reserve_collateral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ async fn test_success() {
);

// limit to track compute unit increase
test.set_bpf_compute_max_units(29_000);
test.set_bpf_compute_max_units(45_000);

let user_accounts_owner = Keypair::new();
let lending_market = add_lending_market(&mut test);

const USDC_RESERVE_LIQUIDITY_FRACTIONAL: u64 = 10 * FRACTIONAL_TO_USDC;
const COLLATERAL_AMOUNT: u64 = USDC_RESERVE_LIQUIDITY_FRACTIONAL * INITIAL_COLLATERAL_RATIO;
const BORROWED_AMOUNT: u64 = FRACTIONAL_TO_USDC;

let usdc_mint = add_usdc_mint(&mut test);
let usdc_oracle = add_usdc_oracle(&mut test);
Expand All @@ -41,16 +42,28 @@ async fn test_success() {
&user_accounts_owner,
AddReserveArgs {
collateral_amount: COLLATERAL_AMOUNT,
liquidity_amount: USDC_RESERVE_LIQUIDITY_FRACTIONAL,
liquidity_amount: 2 * USDC_RESERVE_LIQUIDITY_FRACTIONAL,
liquidity_mint_decimals: usdc_mint.decimals,
liquidity_mint_pubkey: usdc_mint.pubkey,
borrow_amount: BORROWED_AMOUNT,
config: test_reserve_config(),
mark_fresh: true,
..AddReserveArgs::default()
},
);

let (mut banks_client, payer, recent_blockhash) = test.start().await;
let mut test_context = test.start_with_context().await;
test_context.warp_to_slot(300).unwrap(); // clock.slot = 300

let ProgramTestContext {
mut banks_client,
payer,
last_blockhash: recent_blockhash,
..
} = test_context;

let pre_usdc_reserve = usdc_test_reserve.get_state(&mut banks_client).await;
let old_borrow_rate = pre_usdc_reserve.liquidity.cumulative_borrow_rate_wads;

let user_transfer_authority = Keypair::new();
let mut transaction = Transaction::new_with_payer(
Expand Down Expand Up @@ -84,4 +97,9 @@ async fn test_success() {
recent_blockhash,
);
assert!(banks_client.process_transaction(transaction).await.is_ok());

let usdc_reserve = usdc_test_reserve.get_state(&mut banks_client).await;
assert_eq!(usdc_reserve.last_update.stale, true);

assert!(usdc_reserve.liquidity.cumulative_borrow_rate_wads > old_borrow_rate);
}
2 changes: 1 addition & 1 deletion token-lending/program/tests/set_lending_market_owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async fn test_success() {
);

// limit to track compute unit increase
test.set_bpf_compute_max_units(3_000);
test.set_bpf_compute_max_units(4_000);

let lending_market = add_lending_market(&mut test);
let (mut banks_client, payer, recent_blockhash) = test.start().await;
Expand Down