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

Add function to deposit reserve liquidity and collateral #8

Merged
merged 1 commit into from
Jun 28, 2021
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
32 changes: 32 additions & 0 deletions token-lending/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,30 @@ pub enum LendingInstruction {
/// The amount that is to be borrowed - u64::MAX for up to 100% of available liquidity
amount: u64,
},

Copy link
Author

@DaSichuan DaSichuan Jun 25, 2021

Choose a reason for hiding this comment

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

TBH I'm not 100% sure what accounts I should be using here and if the ones here are correct.

Copy link
Member

Choose a reason for hiding this comment

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

uhh you should be able to check this in the code side (this is just a comment)

    let source_liquidity_info = next_account_info(account_info_iter)?;
    let user_collateral_info = next_account_info(account_info_iter)?;
    let reserve_info = next_account_info(account_info_iter)?;
    let reserve_liquidity_supply_info = next_account_info(account_info_iter)?;
    let reserve_collateral_mint_info = next_account_info(account_info_iter)?;
    let lending_market_info = next_account_info(account_info_iter)?;
    let lending_market_authority_info = next_account_info(account_info_iter)?;
    let destination_collateral_info = next_account_info(account_info_iter)?;
    let obligation_info = next_account_info(account_info_iter)?;
    let obligation_owner_info = next_account_info(account_info_iter)?;
    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)?;

so it looks fine at first glance

// 14
/// Combines DepositReserveLiquidity and DepositObligationCollateral
///
/// Accounts expected by this instruction:
///
/// 0. `[writable]` Source liquidity token account.
/// $authority can transfer $liquidity_amount.
/// 1. `[writable]` Destination collateral token account.
/// 2. `[writable]` Reserve account.
/// 3. `[writable]` Reserve liquidity supply SPL Token account.
/// 4. `[writable]` Reserve collateral SPL Token mint.
/// 5. `[]` Lending market account.
/// 6. `[]` Derived lending market authority.
/// 7. `[writable]` Destination deposit reserve collateral supply SPL Token account.
/// 8. `[writable]` Obligation account.
/// 9. `[signer]` Obligation owner.
/// 10. `[signer]` User transfer authority ($authority).
/// 11. `[]` Clock sysvar.
/// 12. `[]` Token program id.
DepositReserveLiquidityAndObligationCollateral {
/// Amount of liquidity to deposit in exchange
liquidity_amount: u64,
},
}

impl LendingInstruction {
Expand Down Expand Up @@ -395,6 +419,10 @@ impl LendingInstruction {
let (amount, _rest) = Self::unpack_u64(rest)?;
Self::FlashLoan { amount }
}
14 => {
let (liquidity_amount, _rest) = Self::unpack_u64(rest)?;
Self::DepositReserveLiquidityAndObligationCollateral { liquidity_amount }
}
_ => {
msg!("Instruction cannot be unpacked");
return Err(LendingError::InstructionUnpackError.into());
Expand Down Expand Up @@ -543,6 +571,10 @@ impl LendingInstruction {
buf.push(13);
buf.extend_from_slice(&amount.to_le_bytes());
}
Self::DepositReserveLiquidityAndObligationCollateral { liquidity_amount } => {
buf.push(14);
buf.extend_from_slice(&liquidity_amount.to_le_bytes());
}
}
buf
}
Expand Down
69 changes: 67 additions & 2 deletions token-lending/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ pub fn process_instruction(
msg!("Instruction: Flash Loan");
process_flash_loan(program_id, amount, accounts)
}
LendingInstruction::DepositReserveLiquidityAndObligationCollateral { liquidity_amount } => {
msg!("Instruction Deposit Reserve Liquidity and Obligation Collateral");
process_deposit_reserve_liquidity_and_obligation_collateral(
program_id,
liquidity_amount,
accounts,
)
}
}
}

Expand Down Expand Up @@ -451,7 +459,7 @@ fn process_deposit_reserve_liquidity(
let token_program_id = next_account_info(account_info_iter)?;

// We don't care about the return value here, so just ignore it.
_process_deposit_reserve_liquidity(
_deposit_reserve_liquidity(
program_id,
liquidity_amount,
source_liquidity_info,
Expand All @@ -469,7 +477,7 @@ fn process_deposit_reserve_liquidity(
}

#[allow(clippy::too_many_arguments)]
fn _process_deposit_reserve_liquidity<'a>(
fn _deposit_reserve_liquidity<'a>(
program_id: &Pubkey,
liquidity_amount: u64,
source_liquidity_info: &AccountInfo<'a>,
Expand Down Expand Up @@ -970,6 +978,63 @@ fn _deposit_obligation_collateral<'a>(
Ok(())
}

#[inline(never)] // avoid stack frame limit
fn process_deposit_reserve_liquidity_and_obligation_collateral(
program_id: &Pubkey,
liquidity_amount: u64,
accounts: &[AccountInfo],
) -> ProgramResult {
if liquidity_amount == 0 {
msg!("Liquidity amount provided cannot be zero");
return Err(LendingError::InvalidAmount.into());
}

let account_info_iter = &mut accounts.iter();
let source_liquidity_info = next_account_info(account_info_iter)?;
let user_collateral_info = next_account_info(account_info_iter)?;
let reserve_info = next_account_info(account_info_iter)?;
let reserve_liquidity_supply_info = next_account_info(account_info_iter)?;
let reserve_collateral_mint_info = next_account_info(account_info_iter)?;
let lending_market_info = next_account_info(account_info_iter)?;
let lending_market_authority_info = next_account_info(account_info_iter)?;
let destination_collateral_info = next_account_info(account_info_iter)?;
let obligation_info = next_account_info(account_info_iter)?;
let obligation_owner_info = next_account_info(account_info_iter)?;
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)?;

Copy link
Member

Choose a reason for hiding this comment

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

are there no extra checks we should do here?

Copy link
Author

Choose a reason for hiding this comment

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

I figured no, since all it's doing is calling two existing endpoints, all which do their own checks

Copy link
Member

@nope-finance nope-finance Jun 28, 2021

Choose a reason for hiding this comment

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

ah yeh kk sounds fine then. the only main thing is the 4 vs 14
so lgtm

let collateral_amount = _deposit_reserve_liquidity(
program_id,
liquidity_amount,
source_liquidity_info,
user_collateral_info,
reserve_info,
reserve_liquidity_supply_info,
reserve_collateral_mint_info,
lending_market_info,
lending_market_authority_info,
user_transfer_authority_info,
clock,
token_program_id,
)?;
_deposit_obligation_collateral(
Copy link
Member

Choose a reason for hiding this comment

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

maybe there is a check on like collateral amount we should do?

Copy link
Author

Choose a reason for hiding this comment

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

The _deposit_obligation_collateral checks for nonzero collateral amount. Anything else? I suupose we can change it to a >0 check

Copy link
Author

Choose a reason for hiding this comment

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

just kidding its unsigned

program_id,
collateral_amount,
user_collateral_info,
destination_collateral_info,
reserve_info,
obligation_info,
lending_market_info,
lending_market_authority_info,
obligation_owner_info,
user_transfer_authority_info,
clock,
token_program_id,
)?;
Ok(())
}

#[inline(never)] // avoid stack frame limit
fn process_withdraw_obligation_collateral(
program_id: &Pubkey,
Expand Down