Skip to content

Commit

Permalink
🔖 Release 1.7.3 (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreihrs authored Nov 5, 2024
1 parent 2f628cd commit 68adbcd
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ use anchor_spl::token_interface::{self, Mint, TokenAccount, TokenInterface};
use lending_operations::refresh_reserve;

use crate::{
check_cpi, gen_signer_seeds,
gen_signer_seeds,
lending_market::{lending_checks, lending_operations},
state::{LendingMarket, Reserve},
utils::{seeds, token_transfer},
LendingAction,
};

pub fn process(ctx: Context<DepositReserveLiquidity>, liquidity_amount: u64) -> Result<()> {
check_cpi!(ctx);
lending_checks::deposit_reserve_liquidity_checks(
&crate::state::nested_accounts::DepositReserveLiquidityAccounts {
lending_market: ctx.accounts.lending_market.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ use anchor_spl::token::Token;
use anchor_spl::token_interface::{self, Mint, TokenAccount, TokenInterface};

use crate::{
check_cpi, gen_signer_seeds,
gen_signer_seeds,
lending_market::{lending_checks, lending_operations},
state::{LendingMarket, RedeemReserveCollateralAccounts, Reserve},
utils::{seeds, token_transfer},
LendingAction,
};

pub fn process(ctx: Context<RedeemReserveCollateral>, collateral_amount: u64) -> Result<()> {
check_cpi!(ctx);
lending_checks::redeem_reserve_collateral_checks(&RedeemReserveCollateralAccounts {
user_source_collateral: ctx.accounts.user_source_collateral.clone(),
user_destination_liquidity: ctx.accounts.user_destination_liquidity.clone(),
Expand Down
62 changes: 50 additions & 12 deletions programs/klend/src/handlers/handler_update_lending_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ pub fn process(
UpdateLendingMarketMode::UpdateOwner => {
let value: [u8; 32] = value[0..32].try_into().unwrap();
let value = Pubkey::from(value);
msg!("Prv value is {:?}", market.lending_market_owner_cached);
msg!("New value is {:?}", value);
market.lending_market_owner_cached = value;
msg!("Value is {:?}", value);
}
UpdateLendingMarketMode::UpdateEmergencyMode => {
let emergency_mode = value[0];
msg!("Value is {:?}", emergency_mode);
msg!("Prv value is {:?}", market.emergency_mode);
msg!("New value is {:?}", emergency_mode);
if emergency_mode == 0 {
market.emergency_mode = 0
} else if emergency_mode == 1 {
Expand All @@ -46,33 +48,47 @@ pub fn process(
}
UpdateLendingMarketMode::UpdateLiquidationCloseFactor => {
let liquidation_close_factor = value[0];
msg!("Value is {:?}", liquidation_close_factor);
msg!(
"Prv value is {:?}",
market.liquidation_max_debt_close_factor_pct
);
msg!("New value is {:?}", liquidation_close_factor);
if !(5..=100).contains(&liquidation_close_factor) {
return err!(LendingError::InvalidFlag);
}
market.liquidation_max_debt_close_factor_pct = liquidation_close_factor;
}
UpdateLendingMarketMode::UpdateLiquidationMaxValue => {
let value = u64::from_le_bytes(value[..8].try_into().unwrap());
msg!("Value is {:?}", value);
msg!(
"Prv value is {:?}",
market.max_liquidatable_debt_market_value_at_once
);
msg!("New value is {:?}", value);
if value == 0 {
return err!(LendingError::InvalidFlag);
}
market.max_liquidatable_debt_market_value_at_once = value;
}
UpdateLendingMarketMode::UpdateGlobalAllowedBorrow => {
let value = u64::from_le_bytes(value[..8].try_into().unwrap());
msg!("Value is {:?}", value);
msg!("Prv value is {:?}", market.global_allowed_borrow_value);
msg!("New value is {:?}", value);
market.global_allowed_borrow_value = value;
}
UpdateLendingMarketMode::UpdateGlobalUnhealthyBorrow => {
let value = u64::from_le_bytes(value[..8].try_into().unwrap());
msg!("Value is {:?}", value);
msg!("Prv value is {:?}", market.global_unhealthy_borrow_value);
msg!("New value is {:?}", value);
market.global_unhealthy_borrow_value = value;
}
UpdateLendingMarketMode::UpdateMinFullLiquidationThreshold => {
let value = u64::from_le_bytes(value[..8].try_into().unwrap());
msg!("Value is {:?}", value);
msg!(
"Prv value is {:?}",
market.min_full_liquidation_value_threshold
);
msg!("New value is {:?}", value);
if value == 0 {
return err!(LendingError::InvalidFlag);
}
Expand All @@ -81,12 +97,17 @@ pub fn process(
UpdateLendingMarketMode::UpdateRiskCouncil => {
let value: [u8; 32] = value[0..32].try_into().unwrap();
let value = Pubkey::from(value);
msg!("Prv value is {:?}", market.risk_council);
msg!("New value is {:?}", value);
market.risk_council = value;
msg!("Value is {:?}", value);
}
UpdateLendingMarketMode::UpdateInsolvencyRiskLtv => {
let insolvency_risk_ltv = value[0];
msg!("Value is {:?}", insolvency_risk_ltv);
msg!(
"Prv value is {:?}",
market.insolvency_risk_unhealthy_ltv_pct
);
msg!("New value is {:?}", value);

if !(5..=100).contains(&insolvency_risk_ltv) {
return err!(LendingError::InvalidFlag);
Expand All @@ -96,7 +117,6 @@ pub fn process(
UpdateLendingMarketMode::UpdateElevationGroup => {
let elevation_group: ElevationGroup =
BorshDeserialize::deserialize(&mut &value[..]).unwrap();
msg!("Value is {:?}", elevation_group);

if elevation_group.id > MAX_NUM_ELEVATION_GROUPS {
return err!(LendingError::InvalidElevationGroupConfig);
Expand Down Expand Up @@ -132,11 +152,17 @@ pub fn process(
return err!(LendingError::InvalidElevationGroupConfig);
}

let prev_elevation_group = market.get_elevation_group(elevation_group.id);

msg!("Prev value is {:?}", prev_elevation_group);
msg!("New value is {:?}", elevation_group);

market.set_elevation_group(elevation_group)?;
}
UpdateLendingMarketMode::UpdateReferralFeeBps => {
let value = u16::from_le_bytes(value[..2].try_into().unwrap());
msg!("Value is {:?}", value);
msg!("Prev value is {:?}", market.referral_fee_bps);
msg!("New value is {:?}", value);
if value > FULL_BPS {
msg!("Referral fee bps must be in range [0, 10000]");
return err!(LendingError::InvalidConfig);
Expand All @@ -148,7 +174,11 @@ pub fn process(
}
UpdateLendingMarketMode::UpdatePriceRefreshTriggerToMaxAgePct => {
let value = value[0];
msg!("Value is {:?}", value);
msg!(
"Prev value is {:?}",
market.price_refresh_trigger_to_max_age_pct
);
msg!("New value is {:?}", value);
if value > 100 {
msg!("Price refresh trigger to max age pct must be in range [0, 100]");
return err!(LendingError::InvalidConfig);
Expand Down Expand Up @@ -211,6 +241,14 @@ pub fn process(
UpdateLendingMarketMode::DeprecatedUpdateMultiplierPoints => {
panic!("Deprecated field")
}
UpdateLendingMarketMode::UpdateName => {
let name_bytes = &value[0..market.name.len()];
let name = std::str::from_utf8(name_bytes).unwrap();
let previous_name = std::str::from_utf8(&market.name).unwrap();
msg!("Prev Value is {}", previous_name.trim_end_matches('\0'));
msg!("New Value is {}", name.trim_end_matches('\0'));
market.name.copy_from_slice(name_bytes);
}
}

Ok(())
Expand Down
22 changes: 11 additions & 11 deletions programs/klend/src/lending_market/lending_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1293,10 +1293,14 @@ where

let slot = clock.slot;

if withdraw_reserve_ref.config.loan_to_value_pct == 0
|| withdraw_reserve_ref.config.liquidation_threshold_pct == 0
{
xmsg!("Max LTV of the withdraw reserve is 0 and can't be used for liquidation");
let elevation_group = get_elevation_group(obligation.elevation_group, lending_market)?;
let (_, collateral_liquidation_threshold_pct) =
get_max_ltv_and_liquidation_threshold(&withdraw_reserve_ref, elevation_group)?;

if collateral_liquidation_threshold_pct == 0 {
xmsg!(
"Liquidation threshold of the withdraw reserve is 0 and can't be used for liquidation"
);
return err!(LendingError::CollateralNonLiquidatable);
}

Expand Down Expand Up @@ -1326,11 +1330,7 @@ where
let is_debt_reserve_highest_borrow_factor =
repay_reserve_ref.config.borrow_factor_pct >= obligation.highest_borrow_factor_pct;

let elevation_group = get_elevation_group(obligation.elevation_group, lending_market)?;
let (_, collateral_liquidation_threshold) =
get_max_ltv_and_liquidation_threshold(&withdraw_reserve_ref, elevation_group)?;

let is_collateral_reserve_lowest_liquidation_ltv = collateral_liquidation_threshold as u64
let is_collateral_reserve_lowest_liquidation_ltv = collateral_liquidation_threshold_pct as u64
<= obligation.lowest_reserve_deposit_liquidation_ltv;

let CalculateLiquidationResult {
Expand Down Expand Up @@ -1840,8 +1840,8 @@ pub fn update_reserve_config(reserve: &mut Reserve, mode: UpdateConfigMode, valu
let str_name = std::str::from_utf8(&value).unwrap();
let cached = reserve.config.token_info.name;
let cached_name = std::str::from_utf8(&cached).unwrap();
msg!("Prev token name was {}", cached_name);
msg!("Setting token name to {}", str_name);
msg!("Prev token name was {}", cached_name.trim_end_matches('\0'));
msg!("Setting token name to {}", str_name.trim_end_matches('\0'));
reserve.config.token_info.name = value;
}
UpdateConfigMode::UpdateTokenInfoPriceMaxAge => {
Expand Down
20 changes: 12 additions & 8 deletions programs/klend/src/state/lending_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,20 @@ pub struct LendingMarket {

pub min_value_skip_liquidation_ltv_bf_checks: u64,

#[cfg_attr(feature = "serde", serde(with = "serde_utf_string", default))]
pub name: [u8; 32],

#[cfg_attr(
feature = "serde",
serde(skip_deserializing, skip_serializing, default = "default_padding_177")
serde(skip_deserializing, skip_serializing, default = "default_padding_173")
)]
#[derivative(Debug = "ignore")]
pub padding1: [u64; 177],
pub padding1: [u64; 173],
}

#[cfg(feature = "serde")]
fn default_padding_177() -> [u64; 177] {
[0; 177]
fn default_padding_173() -> [u64; 173] {
[0; 173]
}

#[cfg(feature = "serde")]
Expand Down Expand Up @@ -126,7 +129,8 @@ impl Default for LendingMarket {
min_value_skip_liquidation_ltv_bf_checks: 0,
elevation_group_padding: [0; 90],
min_net_value_in_obligation_sf: MIN_NET_VALUE_IN_OBLIGATION.to_bits(),
padding1: [0; 177],
name: [0; 32],
padding1: [0; 173],
}
}
}
Expand All @@ -142,14 +146,14 @@ impl LendingMarket {

pub fn get_elevation_group(
&self,
index: u8,
id: u8,
) -> std::result::Result<Option<&ElevationGroup>, LendingError> {
if index == ELEVATION_GROUP_NONE {
if id == ELEVATION_GROUP_NONE {
Ok(None)
} else {
Ok(Some(
self.elevation_groups
.get(index as usize - 1)
.get(id as usize - 1)
.ok_or(LendingError::InvalidElevationGroup)?,
))
}
Expand Down
14 changes: 6 additions & 8 deletions programs/klend/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ pub enum UpdateLendingMarketConfigValue {
U128(u128),
Pubkey(Pubkey),
ElevationGroup(ElevationGroup),
Name([u8; 32]),
}

impl UpdateLendingMarketConfigValue {
Expand All @@ -152,37 +153,33 @@ impl UpdateLendingMarketConfigValue {
match self {
UpdateLendingMarketConfigValue::Bool(v) => {
val[0] = *v as u8;
val
}
UpdateLendingMarketConfigValue::U8(v) => {
val[0] = *v;
val
}
UpdateLendingMarketConfigValue::U16(v) => {
val[..2].copy_from_slice(&v.to_le_bytes());
val
}
UpdateLendingMarketConfigValue::U64(v) => {
val[..8].copy_from_slice(&v.to_le_bytes());
val
}
UpdateLendingMarketConfigValue::U128(v) => {
val[..16].copy_from_slice(&v.to_le_bytes());
val
}
UpdateLendingMarketConfigValue::Pubkey(v) => {
val[..32].copy_from_slice(v.as_ref());
val
}
UpdateLendingMarketConfigValue::ElevationGroup(v) => {
val[..72].copy_from_slice(v.try_to_vec().unwrap().as_slice());
val
}
UpdateLendingMarketConfigValue::U8Array(value) => {
val[..8].copy_from_slice(value);
val
}
UpdateLendingMarketConfigValue::Name(v) => {
val[..v.len()].copy_from_slice(v);
}
}
val
}
}

Expand Down Expand Up @@ -217,6 +214,7 @@ pub enum UpdateLendingMarketMode {
UpdateMinNetValueObligationPostAction = 15,
UpdateMinValueSkipPriorityLiqCheck = 16,
UpdatePaddingFields = 17,
UpdateName = 18,
}

#[cfg(feature = "serde")]
Expand Down
7 changes: 6 additions & 1 deletion programs/klend/src/utils/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ pub const EXPONENT_INTEGRATION_ID_MAINNET: Pubkey =
pub const EXPONENT_CORE_ID_MAINNET: Pubkey =
pubkey!("ExponentnaRg3CQbW6dqQNZKXp7gtZ9DGMp1cwC4HAS7");

pub const CPI_WHITELISTED_ACCOUNTS: [CpiWhitelistedAccount; 14] = [
pub const AGRO_ID_MAINNET: Pubkey = pubkey!("AgroFiE3bX7j4Tvfa7YAoFLqjjb35Bw6eed5BuYukPEn");
pub const AGRO_STAGING_ID_MAINNET: Pubkey = pubkey!("E7jPY6J5s2uAxAjJQX5tqoASkmFr6TYxVoMm97hPLNZ1");

pub const CPI_WHITELISTED_ACCOUNTS: [CpiWhitelistedAccount; 16] = [
CpiWhitelistedAccount::new(FLEX_LEND_ID_MAINNET_PROD, 1),
CpiWhitelistedAccount::new(SQUADS_PROGRAM_ID_V3_MAINNET_PROD, 1),
CpiWhitelistedAccount::new(SQUADS_PROGRAM_ID_V3_MAINNET_DEV, 1),
Expand All @@ -144,6 +147,8 @@ pub const CPI_WHITELISTED_ACCOUNTS: [CpiWhitelistedAccount; 14] = [
CpiWhitelistedAccount::new(DIVVY_ID_MAINNET, 1),
CpiWhitelistedAccount::new(EXPONENT_INTEGRATION_ID_MAINNET, 2),
CpiWhitelistedAccount::new(EXPONENT_CORE_ID_MAINNET, 3),
CpiWhitelistedAccount::new(AGRO_ID_MAINNET, 1),
CpiWhitelistedAccount::new(AGRO_STAGING_ID_MAINNET, 1),
];

pub struct CpiWhitelistedAccount {
Expand Down

0 comments on commit 68adbcd

Please sign in to comment.