Skip to content

Commit

Permalink
fix obligation liquidity initialization and remove unused market auth…
Browse files Browse the repository at this point in the history
…ority pda
  • Loading branch information
nope-finance committed Aug 9, 2021
1 parent 19aa0cd commit 512c129
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 36 deletions.
14 changes: 4 additions & 10 deletions token-lending/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,10 @@ pub enum LendingInstruction {
/// 2. `[]` Deposit reserve account - refreshed.
/// 3. `[writable]` Obligation account.
/// 4. `[]` Lending market account.
/// 5. `[]` Derived lending market authority.
/// 6. `[signer]` Obligation owner.
/// 7. `[signer]` User transfer authority ($authority).
/// 8. `[]` Clock sysvar.
/// 9. `[]` Token program id.
/// 5. `[signer]` Obligation owner.
/// 6. `[signer]` User transfer authority ($authority).
/// 7. `[]` Clock sysvar.
/// 8. `[]` Token program id.
DepositObligationCollateral {
/// Amount of collateral tokens to deposit
collateral_amount: u64,
Expand Down Expand Up @@ -941,10 +940,6 @@ pub fn deposit_obligation_collateral(
obligation_owner_pubkey: Pubkey,
user_transfer_authority_pubkey: Pubkey,
) -> Instruction {
let (lending_market_authority_pubkey, _bump_seed) = Pubkey::find_program_address(
&[&lending_market_pubkey.to_bytes()[..PUBKEY_BYTES]],
&program_id,
);
Instruction {
program_id,
accounts: vec![
Expand All @@ -953,7 +948,6 @@ pub fn deposit_obligation_collateral(
AccountMeta::new_readonly(deposit_reserve_pubkey, false),
AccountMeta::new(obligation_pubkey, false),
AccountMeta::new_readonly(lending_market_pubkey, false),
AccountMeta::new_readonly(lending_market_authority_pubkey, false),
AccountMeta::new_readonly(obligation_owner_pubkey, true),
AccountMeta::new_readonly(user_transfer_authority_pubkey, true),
AccountMeta::new_readonly(sysvar::clock::id(), false),
Expand Down
29 changes: 9 additions & 20 deletions token-lending/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,6 @@ fn process_deposit_obligation_collateral(
let deposit_reserve_info = next_account_info(account_info_iter)?;
let obligation_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 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)?)?;
Expand All @@ -926,7 +925,6 @@ fn process_deposit_obligation_collateral(
deposit_reserve_info,
obligation_info,
lending_market_info,
lending_market_authority_info,
obligation_owner_info,
user_transfer_authority_info,
clock,
Expand All @@ -943,7 +941,6 @@ fn _deposit_obligation_collateral<'a>(
deposit_reserve_info: &AccountInfo<'a>,
obligation_info: &AccountInfo<'a>,
lending_market_info: &AccountInfo<'a>,
lending_market_authority_info: &AccountInfo<'a>,
obligation_owner_info: &AccountInfo<'a>,
user_transfer_authority_info: &AccountInfo<'a>,
clock: &Clock,
Expand Down Expand Up @@ -1005,19 +1002,6 @@ fn _deposit_obligation_collateral<'a>(
return Err(LendingError::InvalidSigner.into());
}

let authority_signer_seeds = &[
lending_market_info.key.as_ref(),
&[lending_market.bump_seed],
];
let lending_market_authority_pubkey =
Pubkey::create_program_address(authority_signer_seeds, program_id)?;
if &lending_market_authority_pubkey != lending_market_authority_info.key {
msg!(
"Derived lending market authority does not match the lending market authority provided"
);
return Err(LendingError::InvalidMarketAuthority.into());
}

obligation
.find_or_add_collateral_to_deposits(*deposit_reserve_info.key)?
.deposit(collateral_amount)?;
Expand Down Expand Up @@ -1093,7 +1077,6 @@ fn process_deposit_reserve_liquidity_and_obligation_collateral(
reserve_info,
obligation_info,
lending_market_info,
lending_market_authority_info,
obligation_owner_info,
user_transfer_authority_info,
clock,
Expand Down Expand Up @@ -1415,13 +1398,19 @@ fn process_borrow_obligation_liquidity(
return Err(LendingError::BorrowTooSmall.into());
}

// logically i think this should be below but is equiv because how borrow doesn't update rate
let cumulative_borrow_rate_wads = borrow_reserve.liquidity.cumulative_borrow_rate_wads;

borrow_reserve.liquidity.borrow(borrow_amount)?;
borrow_reserve.last_update.mark_stale();
Reserve::pack(borrow_reserve, &mut borrow_reserve_info.data.borrow_mut())?;

obligation
.find_or_add_liquidity_to_borrows(*borrow_reserve_info.key)?
.borrow(borrow_amount)?;
let obligation_liquidity =
obligation.find_or_add_liquidity_to_borrows(*borrow_reserve_info.key)?;
if obligation_liquidity.cumulative_borrow_rate_wads == Decimal::zero() {
obligation_liquidity.cumulative_borrow_rate_wads = cumulative_borrow_rate_wads;
}
obligation_liquidity.borrow(borrow_amount)?;
obligation.last_update.mark_stale();
Obligation::pack(obligation, &mut obligation_info.data.borrow_mut())?;

Expand Down
6 changes: 1 addition & 5 deletions token-lending/program/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ pub use obligation::*;
pub use reserve::*;

use crate::math::{Decimal, WAD};
use solana_program::{
clock::{DEFAULT_TICKS_PER_SECOND, DEFAULT_TICKS_PER_SLOT, SECONDS_PER_DAY},
msg,
program_error::ProgramError,
};
use solana_program::{msg, program_error::ProgramError};

/// Collateral tokens are initially valued at a ratio of 5:1 (collateral:liquidity)
// @FIXME: restore to 5
Expand Down
2 changes: 1 addition & 1 deletion token-lending/program/src/state/obligation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ impl ObligationLiquidity {
pub fn new(borrow_reserve: Pubkey) -> Self {
Self {
borrow_reserve,
cumulative_borrow_rate_wads: Decimal::one(),
cumulative_borrow_rate_wads: Decimal::zero(),
borrowed_amount_wads: Decimal::zero(),
market_value: Decimal::zero(),
}
Expand Down

0 comments on commit 512c129

Please sign in to comment.