Skip to content

Commit 089bf84

Browse files
committed
deposit limit implementation rebased
1 parent 3f00ace commit 089bf84

File tree

6 files changed

+33
-9
lines changed

6 files changed

+33
-9
lines changed

token-lending/program/src/instruction.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,8 @@ impl LendingInstruction {
408408
let (max_borrow_rate, rest) = Self::unpack_u8(rest)?;
409409
let (borrow_fee_wad, rest) = Self::unpack_u64(rest)?;
410410
let (flash_loan_fee_wad, rest) = Self::unpack_u64(rest)?;
411-
let (host_fee_percentage, _rest) = Self::unpack_u8(rest)?;
411+
let (host_fee_percentage, rest) = Self::unpack_u8(rest)?;
412+
let (deposit_limit, _) = Self::unpack_u64(rest)?;
412413
Self::InitReserve {
413414
liquidity_amount,
414415
config: ReserveConfig {
@@ -424,6 +425,7 @@ impl LendingInstruction {
424425
flash_loan_fee_wad,
425426
host_fee_percentage,
426427
},
428+
deposit_limit,
427429
},
428430
}
429431
}
@@ -481,6 +483,7 @@ impl LendingInstruction {
481483
let (borrow_fee_wad, _rest) = Self::unpack_u64(_rest)?;
482484
let (flash_loan_fee_wad, _rest) = Self::unpack_u64(_rest)?;
483485
let (host_fee_percentage, _rest) = Self::unpack_u8(_rest)?;
486+
let (deposit_limit, _rest) = Self::unpack_u64(_rest)?;
484487

485488
Self::UpdateReserveConfig {
486489
config: ReserveConfig {
@@ -496,6 +499,7 @@ impl LendingInstruction {
496499
flash_loan_fee_wad,
497500
host_fee_percentage,
498501
},
502+
deposit_limit,
499503
},
500504
}
501505
}
@@ -591,6 +595,7 @@ impl LendingInstruction {
591595
flash_loan_fee_wad,
592596
host_fee_percentage,
593597
},
598+
deposit_limit,
594599
},
595600
} => {
596601
buf.push(2);
@@ -605,6 +610,7 @@ impl LendingInstruction {
605610
buf.extend_from_slice(&borrow_fee_wad.to_le_bytes());
606611
buf.extend_from_slice(&flash_loan_fee_wad.to_le_bytes());
607612
buf.extend_from_slice(&host_fee_percentage.to_le_bytes());
613+
buf.extend_from_slice(&deposit_limit.to_le_bytes());
608614
}
609615
Self::RefreshReserve => {
610616
buf.push(3);
@@ -667,6 +673,7 @@ impl LendingInstruction {
667673
buf.extend_from_slice(&config.fees.borrow_fee_wad.to_le_bytes());
668674
buf.extend_from_slice(&config.fees.flash_loan_fee_wad.to_le_bytes());
669675
buf.extend_from_slice(&config.fees.host_fee_percentage.to_le_bytes());
676+
buf.extend_from_slice(&config.deposit_limit.to_le_bytes());
670677
}
671678
}
672679
buf

token-lending/program/src/processor.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,15 @@ fn _deposit_reserve_liquidity<'a>(
560560
return Err(LendingError::InvalidMarketAuthority.into());
561561
}
562562

563+
if Decimal::from(liquidity_amount)
564+
.try_add(reserve.liquidity.total_supply()?)?
565+
.try_floor_u64()?
566+
> reserve.config.deposit_limit
567+
{
568+
msg!("Cannot deposit liquidity above the reserve deposit limit");
569+
return Err(LendingError::InvalidAmount.into());
570+
}
571+
563572
let collateral_amount = reserve.deposit_liquidity(liquidity_amount)?;
564573
reserve.last_update.mark_stale();
565574
Reserve::pack(reserve, &mut reserve_info.data.borrow_mut())?;

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,8 @@ pub struct ReserveConfig {
611611
pub max_borrow_rate: u8,
612612
/// Program owner fees assessed, separate from gains due to interest accrual
613613
pub fees: ReserveFees,
614+
/// Maximum deposit limit of liquidity in native units, u64::MAX for inf
615+
pub deposit_limit: u64,
614616
}
615617

616618
/// Additional fee information on a reserve
@@ -717,7 +719,7 @@ impl IsInitialized for Reserve {
717719
}
718720
}
719721

720-
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
722+
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
721723
impl Pack for Reserve {
722724
const LEN: usize = RESERVE_LEN;
723725

@@ -753,6 +755,7 @@ impl Pack for Reserve {
753755
config_fees_borrow_fee_wad,
754756
config_fees_flash_loan_fee_wad,
755757
config_fees_host_fee_percentage,
758+
config_deposit_limit,
756759
_padding,
757760
) = mut_array_refs![
758761
output,
@@ -783,6 +786,7 @@ impl Pack for Reserve {
783786
8,
784787
8,
785788
1,
789+
8,
786790
248
787791
];
788792

@@ -827,6 +831,7 @@ impl Pack for Reserve {
827831
*config_fees_borrow_fee_wad = self.config.fees.borrow_fee_wad.to_le_bytes();
828832
*config_fees_flash_loan_fee_wad = self.config.fees.flash_loan_fee_wad.to_le_bytes();
829833
*config_fees_host_fee_percentage = self.config.fees.host_fee_percentage.to_le_bytes();
834+
*config_deposit_limit = self.config.deposit_limit.to_le_bytes();
830835
}
831836

832837
/// Unpacks a byte buffer into a [ReserveInfo](struct.ReserveInfo.html).
@@ -861,6 +866,7 @@ impl Pack for Reserve {
861866
config_fees_borrow_fee_wad,
862867
config_fees_flash_loan_fee_wad,
863868
config_fees_host_fee_percentage,
869+
config_deposit_limit,
864870
_padding,
865871
) = array_refs![
866872
input,
@@ -891,6 +897,7 @@ impl Pack for Reserve {
891897
8,
892898
8,
893899
1,
900+
8,
894901
248
895902
];
896903

@@ -939,6 +946,7 @@ impl Pack for Reserve {
939946
flash_loan_fee_wad: u64::from_le_bytes(*config_fees_flash_loan_fee_wad),
940947
host_fee_percentage: u8::from_le_bytes(*config_fees_host_fee_percentage),
941948
},
949+
deposit_limit: u64::from_le_bytes(*config_deposit_limit),
942950
},
943951
})
944952
}

token-lending/program/tests/deposit_reserve_liquidity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ async fn test_success() {
1616
);
1717

1818
// limit to track compute unit increase
19-
test.set_bpf_compute_max_units(27_000);
19+
test.set_bpf_compute_max_units(30_000);
2020

2121
let user_accounts_owner = Keypair::new();
2222
let lending_market = add_lending_market(&mut test);

token-lending/program/tests/helpers/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub const TEST_RESERVE_CONFIG: ReserveConfig = ReserveConfig {
5454
flash_loan_fee_wad: 3_000_000_000_000_000,
5555
host_fee_percentage: 20,
5656
},
57+
deposit_limit: 100_000_000_000,
5758
};
5859

5960
pub const SOL_PYTH_PRODUCT: &str = "3Mnn2fX6rQyUsyELYms1sBJyChWofzSNRoqYzvgMVz5E";
@@ -883,7 +884,6 @@ impl TestReserve {
883884
],
884885
Some(&payer.pubkey()),
885886
);
886-
887887
let recent_blockhash = banks_client.get_recent_blockhash().await.unwrap();
888888
transaction.sign(
889889
&vec![
@@ -901,16 +901,15 @@ impl TestReserve {
901901
],
902902
recent_blockhash,
903903
);
904-
905904
banks_client
906905
.process_transaction(transaction)
907906
.await
908907
.map(|_| Self {
909-
name,
908+
name: name,
910909
pubkey: reserve_pubkey,
911910
lending_market_pubkey: lending_market.pubkey,
912-
config,
913-
liquidity_mint_pubkey,
911+
config: config,
912+
liquidity_mint_pubkey: liquidity_mint_pubkey,
914913
liquidity_mint_decimals: liquidity_mint.decimals,
915914
liquidity_supply_pubkey: liquidity_supply_keypair.pubkey(),
916915
liquidity_fee_receiver_pubkey: liquidity_fee_receiver_keypair.pubkey(),
@@ -919,7 +918,7 @@ impl TestReserve {
919918
liquidity_switchboard_oracle_pubkey: oracle.switchboard_feed_pubkey,
920919
collateral_mint_pubkey: collateral_mint_keypair.pubkey(),
921920
collateral_supply_pubkey: collateral_supply_keypair.pubkey(),
922-
user_liquidity_pubkey,
921+
user_liquidity_pubkey: user_liquidity_pubkey,
923922
user_collateral_pubkey: user_collateral_token_keypair.pubkey(),
924923
market_price: oracle.price,
925924
})

token-lending/program/tests/init_reserve.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ async fn test_update_reserve_config() {
313313
min_borrow_rate: 1,
314314
optimal_borrow_rate: 5,
315315
max_borrow_rate: 45,
316+
deposit_limit: 1_000_000,
316317
fees: ReserveFees {
317318
borrow_fee_wad: 200_000_000_000,
318319
flash_loan_fee_wad: 5_000_000_000_000_000,

0 commit comments

Comments
 (0)