Skip to content

Commit 042ab15

Browse files
committed
WIp
1 parent b7f7207 commit 042ab15

File tree

3 files changed

+128
-2
lines changed

3 files changed

+128
-2
lines changed

token-lending/program/src/instruction.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,23 @@ pub enum LendingInstruction {
362362
/// liquidity_amount is the amount of collateral tokens to withdraw
363363
collateral_amount: u64,
364364
},
365+
366+
// 16
367+
/// Updates a reserve config parameter
368+
///
369+
/// Accounts expected by this instruction:
370+
///
371+
/// 1. `[writable]` Reserve account - refreshed
372+
/// 2 `[]` Lending market account.
373+
/// 3 `[]` Derived lending market authority.
374+
/// 4 `[signer]` Lending market owner.
375+
/// 6 `[]` Clock sysvar.
376+
UpdateReserveConfig {
377+
/// Specifies which config to change
378+
config_enum: u8,
379+
/// New value for the config
380+
new_value: u8,
381+
},
365382
}
366383

367384
impl LendingInstruction {
@@ -456,6 +473,11 @@ impl LendingInstruction {
456473
let (collateral_amount, _rest) = Self::unpack_u64(rest)?;
457474
Self::WithdrawObligationCollateralAndRedeemReserveCollateral { collateral_amount }
458475
}
476+
16 => {
477+
let (config_enum, _rest) = Self::unpack_u8(rest)?;
478+
let (new_value, _rest) = Self::unpack_u8(_rest)?;
479+
Self::UpdateReserveConfig { config_enum, new_value }
480+
}
459481
_ => {
460482
msg!("Instruction cannot be unpacked");
461483
return Err(LendingError::InstructionUnpackError.into());
@@ -612,6 +634,11 @@ impl LendingInstruction {
612634
buf.push(15);
613635
buf.extend_from_slice(&collateral_amount.to_le_bytes());
614636
}
637+
Self::UpdateReserveConfig {config_enum, new_value} => {
638+
buf.push(16);
639+
buf.extend_from_slice(&config_enum.to_le_bytes());
640+
buf.extend_from_slice(&new_value.to_le_bytes());
641+
}
615642
}
616643
buf
617644
}

token-lending/program/src/processor.rs

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
CalculateBorrowResult, CalculateLiquidationResult, CalculateRepayResult,
1010
InitLendingMarketParams, InitObligationParams, InitReserveParams, LendingMarket,
1111
NewReserveCollateralParams, NewReserveLiquidityParams, Obligation, Reserve,
12-
ReserveCollateral, ReserveConfig, ReserveLiquidity,
12+
ReserveCollateral, ReserveConfig, ReserveConfigKey, ReserveLiquidity,
1313
},
1414
};
1515
use num_traits::FromPrimitive;
@@ -114,13 +114,24 @@ pub fn process_instruction(
114114
LendingInstruction::WithdrawObligationCollateralAndRedeemReserveCollateral {
115115
collateral_amount,
116116
} => {
117-
msg!("Instruction: Withdraw Obligation Collateral and Redeem Reserve Collateral ");
117+
msg!("Instruction: Withdraw Obligation Collateral and Redeem Reserve Collateral");
118118
process_withdraw_obligation_collateral_and_redeem_reserve_liquidity(
119119
program_id,
120120
collateral_amount,
121121
accounts,
122122
)
123123
}
124+
LendingInstruction::UpdateReserveConfig {
125+
config_enum,
126+
new_value,
127+
} => {
128+
msg!("Instruction: UpdateReserveConfig");
129+
process_update_reserve_config(
130+
program_id,
131+
ReserveConfigKey::from_u8(config_enum)?,
132+
new_value,
133+
accounts)
134+
}
124135
}
125136
}
126137

@@ -1994,6 +2005,78 @@ fn process_withdraw_obligation_collateral_and_redeem_reserve_liquidity(
19942005
)
19952006
}
19962007

2008+
2009+
#[inline(never)] // avoid stack frame limit
2010+
fn process_update_reserve_config(
2011+
program_id: &Pubkey,
2012+
config_enum: ReserveConfigKey,
2013+
new_value: u8,
2014+
accounts: &[AccountInfo],
2015+
) -> ProgramResult {
2016+
2017+
let account_info_iter = &mut accounts.iter().peekable();
2018+
let reserve_info = next_account_info(account_info_iter)?;
2019+
let lending_market_info = next_account_info(account_info_iter)?;
2020+
let lending_market_authority_info = next_account_info(account_info_iter)?;
2021+
let lending_market_owner_info = next_account_info(account_info_iter)?;
2022+
let clock = &Clock::from_account_info(next_account_info(account_info_iter)?)?;
2023+
2024+
let mut reserve = assert_uninitialized::<Reserve>(reserve_info)?;
2025+
if reserve_info.owner != program_id {
2026+
msg!(
2027+
"Reserve provided is not owned by the lending program {} != {}",
2028+
&reserve_info.owner.to_string(),
2029+
&program_id.to_string(),
2030+
);
2031+
return Err(LendingError::InvalidAccountOwner.into());
2032+
}
2033+
2034+
2035+
let lending_market = LendingMarket::unpack(&lending_market_info.data.borrow())?;
2036+
if lending_market_info.owner != program_id {
2037+
msg!(
2038+
"Lending market provided is not owned by the lending program {} != {}",
2039+
&lending_market_info.owner.to_string(),
2040+
&program_id.to_string(),
2041+
);
2042+
return Err(LendingError::InvalidAccountOwner.into());
2043+
}
2044+
if &lending_market.owner != lending_market_owner_info.key {
2045+
msg!("Lending market owner does not match the lending market owner provided");
2046+
return Err(LendingError::InvalidMarketOwner.into());
2047+
}
2048+
if !lending_market_owner_info.is_signer {
2049+
msg!("Lending market owner provided must be a signer");
2050+
return Err(LendingError::InvalidSigner.into());
2051+
}
2052+
2053+
let authority_signer_seeds = &[
2054+
lending_market_info.key.as_ref(),
2055+
&[lending_market.bump_seed],
2056+
];
2057+
let lending_market_authority_pubkey =
2058+
Pubkey::create_program_address(authority_signer_seeds, program_id)?;
2059+
if &lending_market_authority_pubkey != lending_market_authority_info.key {
2060+
msg!(
2061+
"Derived lending market authority does not match the lending market authority provided"
2062+
);
2063+
return Err(LendingError::InvalidMarketAuthority.into());
2064+
}
2065+
match config_enum {
2066+
// Make this an enum, make this decimal friendly
2067+
ReserveConfigKey::LoanToValueRatio => {
2068+
reserve.config.loan_to_value_ratio = new_value;
2069+
}
2070+
_ => {
2071+
msg!("Did not recognize reserve config key");
2072+
return Err(LendingError::InvalidConfig.into());
2073+
}
2074+
};
2075+
Reserve::pack(reserve, &mut reserve_info.data.borrow_mut())?;
2076+
Ok(())
2077+
}
2078+
2079+
19972080
fn assert_rent_exempt(rent: &Rent, account_info: &AccountInfo) -> ProgramResult {
19982081
if !rent.is_exempt(account_info.lamports(), account_info.data_len()) {
19992082
msg!(

token-lending/program/src/state/reserve.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,22 @@ pub struct ReserveConfig {
613613
pub fees: ReserveFees,
614614
}
615615

616+
/// ReserveConfig configurable values
617+
#[repr(u8)]
618+
pub enum ReserveConfigKey {
619+
/// Enum to specify LoanToValueRatio
620+
LoanToValueRatio = 0
621+
}
622+
623+
impl ReserveConfigKey {
624+
pub fn from_u8(value: u8) -> Result<ReserveConfigKey, ProgramError> {
625+
match value {
626+
0 => Ok(ReserveConfigKey::LoanToValueRatio)
627+
_ => Err(LendingError::InvalidConfig.into())
628+
}
629+
}
630+
}
631+
616632
/// Additional fee information on a reserve
617633
///
618634
/// These exist separately from interest accrual fees, and are specifically for the program owner

0 commit comments

Comments
 (0)