Skip to content

Commit

Permalink
deposit limit implementation rebased
Browse files Browse the repository at this point in the history
  • Loading branch information
DaSichuan committed Jul 21, 2021
1 parent 3f00ace commit 089bf84
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
9 changes: 8 additions & 1 deletion token-lending/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ impl LendingInstruction {
let (max_borrow_rate, rest) = Self::unpack_u8(rest)?;
let (borrow_fee_wad, rest) = Self::unpack_u64(rest)?;
let (flash_loan_fee_wad, rest) = Self::unpack_u64(rest)?;
let (host_fee_percentage, _rest) = Self::unpack_u8(rest)?;
let (host_fee_percentage, rest) = Self::unpack_u8(rest)?;
let (deposit_limit, _) = Self::unpack_u64(rest)?;
Self::InitReserve {
liquidity_amount,
config: ReserveConfig {
Expand All @@ -424,6 +425,7 @@ impl LendingInstruction {
flash_loan_fee_wad,
host_fee_percentage,
},
deposit_limit,
},
}
}
Expand Down Expand Up @@ -481,6 +483,7 @@ impl LendingInstruction {
let (borrow_fee_wad, _rest) = Self::unpack_u64(_rest)?;
let (flash_loan_fee_wad, _rest) = Self::unpack_u64(_rest)?;
let (host_fee_percentage, _rest) = Self::unpack_u8(_rest)?;
let (deposit_limit, _rest) = Self::unpack_u64(_rest)?;

Self::UpdateReserveConfig {
config: ReserveConfig {
Expand All @@ -496,6 +499,7 @@ impl LendingInstruction {
flash_loan_fee_wad,
host_fee_percentage,
},
deposit_limit,
},
}
}
Expand Down Expand Up @@ -591,6 +595,7 @@ impl LendingInstruction {
flash_loan_fee_wad,
host_fee_percentage,
},
deposit_limit,
},
} => {
buf.push(2);
Expand All @@ -605,6 +610,7 @@ impl LendingInstruction {
buf.extend_from_slice(&borrow_fee_wad.to_le_bytes());
buf.extend_from_slice(&flash_loan_fee_wad.to_le_bytes());
buf.extend_from_slice(&host_fee_percentage.to_le_bytes());
buf.extend_from_slice(&deposit_limit.to_le_bytes());
}
Self::RefreshReserve => {
buf.push(3);
Expand Down Expand Up @@ -667,6 +673,7 @@ impl LendingInstruction {
buf.extend_from_slice(&config.fees.borrow_fee_wad.to_le_bytes());
buf.extend_from_slice(&config.fees.flash_loan_fee_wad.to_le_bytes());
buf.extend_from_slice(&config.fees.host_fee_percentage.to_le_bytes());
buf.extend_from_slice(&config.deposit_limit.to_le_bytes());
}
}
buf
Expand Down
9 changes: 9 additions & 0 deletions token-lending/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,15 @@ fn _deposit_reserve_liquidity<'a>(
return Err(LendingError::InvalidMarketAuthority.into());
}

if Decimal::from(liquidity_amount)
.try_add(reserve.liquidity.total_supply()?)?
.try_floor_u64()?
> reserve.config.deposit_limit
{
msg!("Cannot deposit liquidity above the reserve deposit limit");
return Err(LendingError::InvalidAmount.into());
}

let collateral_amount = reserve.deposit_liquidity(liquidity_amount)?;
reserve.last_update.mark_stale();
Reserve::pack(reserve, &mut reserve_info.data.borrow_mut())?;
Expand Down
10 changes: 9 additions & 1 deletion token-lending/program/src/state/reserve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,8 @@ pub struct ReserveConfig {
pub max_borrow_rate: u8,
/// Program owner fees assessed, separate from gains due to interest accrual
pub fees: ReserveFees,
/// Maximum deposit limit of liquidity in native units, u64::MAX for inf
pub deposit_limit: u64,
}

/// Additional fee information on a reserve
Expand Down Expand Up @@ -717,7 +719,7 @@ impl IsInitialized for Reserve {
}
}

const RESERVE_LEN: usize = 603; // 1 + 8 + 1 + 32 + 32 + 1 + 32 + 32 + 32 + 32 + 8 + 16 + 16 + 16 + 32 + 8 + 32 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 8 + 8 + 1 + 248
const RESERVE_LEN: usize = 611; // 1 + 8 + 1 + 32 + 32 + 1 + 32 + 32 + 32 + 32 + 8 + 16 + 16 + 16 + 32 + 8 + 32 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 8 + 8 + 1 + 8 + 248
impl Pack for Reserve {
const LEN: usize = RESERVE_LEN;

Expand Down Expand Up @@ -753,6 +755,7 @@ impl Pack for Reserve {
config_fees_borrow_fee_wad,
config_fees_flash_loan_fee_wad,
config_fees_host_fee_percentage,
config_deposit_limit,
_padding,
) = mut_array_refs![
output,
Expand Down Expand Up @@ -783,6 +786,7 @@ impl Pack for Reserve {
8,
8,
1,
8,
248
];

Expand Down Expand Up @@ -827,6 +831,7 @@ impl Pack for Reserve {
*config_fees_borrow_fee_wad = self.config.fees.borrow_fee_wad.to_le_bytes();
*config_fees_flash_loan_fee_wad = self.config.fees.flash_loan_fee_wad.to_le_bytes();
*config_fees_host_fee_percentage = self.config.fees.host_fee_percentage.to_le_bytes();
*config_deposit_limit = self.config.deposit_limit.to_le_bytes();
}

/// Unpacks a byte buffer into a [ReserveInfo](struct.ReserveInfo.html).
Expand Down Expand Up @@ -861,6 +866,7 @@ impl Pack for Reserve {
config_fees_borrow_fee_wad,
config_fees_flash_loan_fee_wad,
config_fees_host_fee_percentage,
config_deposit_limit,
_padding,
) = array_refs![
input,
Expand Down Expand Up @@ -891,6 +897,7 @@ impl Pack for Reserve {
8,
8,
1,
8,
248
];

Expand Down Expand Up @@ -939,6 +946,7 @@ impl Pack for Reserve {
flash_loan_fee_wad: u64::from_le_bytes(*config_fees_flash_loan_fee_wad),
host_fee_percentage: u8::from_le_bytes(*config_fees_host_fee_percentage),
},
deposit_limit: u64::from_le_bytes(*config_deposit_limit),
},
})
}
Expand Down
2 changes: 1 addition & 1 deletion token-lending/program/tests/deposit_reserve_liquidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async fn test_success() {
);

// limit to track compute unit increase
test.set_bpf_compute_max_units(27_000);
test.set_bpf_compute_max_units(30_000);

let user_accounts_owner = Keypair::new();
let lending_market = add_lending_market(&mut test);
Expand Down
11 changes: 5 additions & 6 deletions token-lending/program/tests/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub const TEST_RESERVE_CONFIG: ReserveConfig = ReserveConfig {
flash_loan_fee_wad: 3_000_000_000_000_000,
host_fee_percentage: 20,
},
deposit_limit: 100_000_000_000,
};

pub const SOL_PYTH_PRODUCT: &str = "3Mnn2fX6rQyUsyELYms1sBJyChWofzSNRoqYzvgMVz5E";
Expand Down Expand Up @@ -883,7 +884,6 @@ impl TestReserve {
],
Some(&payer.pubkey()),
);

let recent_blockhash = banks_client.get_recent_blockhash().await.unwrap();
transaction.sign(
&vec![
Expand All @@ -901,16 +901,15 @@ impl TestReserve {
],
recent_blockhash,
);

banks_client
.process_transaction(transaction)
.await
.map(|_| Self {
name,
name: name,
pubkey: reserve_pubkey,
lending_market_pubkey: lending_market.pubkey,
config,
liquidity_mint_pubkey,
config: config,
liquidity_mint_pubkey: liquidity_mint_pubkey,
liquidity_mint_decimals: liquidity_mint.decimals,
liquidity_supply_pubkey: liquidity_supply_keypair.pubkey(),
liquidity_fee_receiver_pubkey: liquidity_fee_receiver_keypair.pubkey(),
Expand All @@ -919,7 +918,7 @@ impl TestReserve {
liquidity_switchboard_oracle_pubkey: oracle.switchboard_feed_pubkey,
collateral_mint_pubkey: collateral_mint_keypair.pubkey(),
collateral_supply_pubkey: collateral_supply_keypair.pubkey(),
user_liquidity_pubkey,
user_liquidity_pubkey: user_liquidity_pubkey,
user_collateral_pubkey: user_collateral_token_keypair.pubkey(),
market_price: oracle.price,
})
Expand Down
1 change: 1 addition & 0 deletions token-lending/program/tests/init_reserve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ async fn test_update_reserve_config() {
min_borrow_rate: 1,
optimal_borrow_rate: 5,
max_borrow_rate: 45,
deposit_limit: 1_000_000,
fees: ReserveFees {
borrow_fee_wad: 200_000_000_000,
flash_loan_fee_wad: 5_000_000_000_000_000,
Expand Down

0 comments on commit 089bf84

Please sign in to comment.