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

Mainnet fixes #33

Merged
merged 3 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
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
10 changes: 3 additions & 7 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 All @@ -30,8 +26,8 @@ pub const PROGRAM_VERSION: u8 = 1;
pub const UNINITIALIZED_VERSION: u8 = 0;

/// Number of slots per year
pub const SLOTS_PER_YEAR: u64 =
DEFAULT_TICKS_PER_SECOND / DEFAULT_TICKS_PER_SLOT * SECONDS_PER_DAY * 365;
// 2 (slots per second) * 60 * 60 * 24 * 365 = 63072000
pub const SLOTS_PER_YEAR: u64 = 63072000;

// Helpers
fn pack_decimal(decimal: Decimal, dst: &mut [u8; 16]) {
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