Skip to content

Commit 0db6403

Browse files
committed
change borrow weight bps to added borrow weight bps
1 parent 5fc0810 commit 0db6403

File tree

7 files changed

+28
-131
lines changed

7 files changed

+28
-131
lines changed

token-lending/cli/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ fn main() {
872872
fee_receiver: liquidity_fee_receiver_keypair.pubkey(),
873873
protocol_liquidation_fee,
874874
protocol_take_rate,
875-
borrow_weight_bps: 10000,
875+
added_borrow_weight_bps: 10000,
876876
},
877877
source_liquidity_pubkey,
878878
source_liquidity_owner_keypair,

token-lending/program/src/processor.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2877,10 +2877,6 @@ fn validate_reserve_config(config: ReserveConfig) -> ProgramResult {
28772877
msg!("Protocol take rate must be in range [0, 100]");
28782878
return Err(LendingError::InvalidConfig.into());
28792879
}
2880-
if config.borrow_weight_bps < 10000 {
2881-
msg!("Borrow weight bps must be greater than 10000!");
2882-
return Err(LendingError::InvalidConfig.into());
2883-
}
28842880
Ok(())
28852881
}
28862882

token-lending/program/tests/borrow_weight.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async fn test_refresh_obligation() {
2626
let (mut test, lending_market, _, _, _, obligation) = scenario_1(
2727
&test_reserve_config(),
2828
&ReserveConfig {
29-
borrow_weight_bps: 20_000,
29+
added_borrow_weight_bps: 10_000,
3030
..test_reserve_config()
3131
},
3232
)
@@ -55,7 +55,7 @@ async fn test_borrow() {
5555
let (mut test, lending_market, usdc_reserve, wsol_reserve, _, _) = setup_world(
5656
&test_reserve_config(),
5757
&ReserveConfig {
58-
borrow_weight_bps: 20_000,
58+
added_borrow_weight_bps: 10_000,
5959
fees: ReserveFees {
6060
borrow_fee_wad: 10_000_000_000_000_000, // 1%
6161
host_fee_percentage: 20,
@@ -207,7 +207,7 @@ async fn test_liquidation() {
207207
setup_world(
208208
&test_reserve_config(),
209209
&ReserveConfig {
210-
borrow_weight_bps: 10_000,
210+
added_borrow_weight_bps: 0,
211211
fees: ReserveFees {
212212
borrow_fee_wad: 10_000_000_000_000_000, // 1%
213213
host_fee_percentage: 20,
@@ -341,7 +341,7 @@ async fn test_liquidation() {
341341
&lending_market_owner,
342342
&wsol_reserve,
343343
ReserveConfig {
344-
borrow_weight_bps: 11_000,
344+
added_borrow_weight_bps: 1_000,
345345
..wsol_reserve.account.config
346346
},
347347
wsol_reserve.account.rate_limiter.config,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn test_reserve_config() -> ReserveConfig {
5353
fee_receiver: Keypair::new().pubkey(),
5454
protocol_liquidation_fee: 0,
5555
protocol_take_rate: 0,
56-
borrow_weight_bps: 10_000,
56+
added_borrow_weight_bps: 0,
5757
}
5858
}
5959

token-lending/program/tests/init_reserve.rs

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -299,35 +299,6 @@ async fn test_invalid_fees() {
299299
}
300300
}
301301

302-
#[tokio::test]
303-
async fn test_init_reserve_invalid_borrow_weight_bps() {
304-
let (mut test, lending_market, lending_market_owner) = setup().await;
305-
let res = test
306-
.init_reserve(
307-
&lending_market,
308-
&lending_market_owner,
309-
&usdc_mint::id(),
310-
&ReserveConfig {
311-
borrow_weight_bps: 9999,
312-
..test_reserve_config()
313-
},
314-
&Keypair::new(),
315-
1000,
316-
None,
317-
)
318-
.await
319-
.unwrap_err()
320-
.unwrap();
321-
322-
assert_eq!(
323-
res,
324-
TransactionError::InstructionError(
325-
1,
326-
InstructionError::Custom(LendingError::InvalidConfig as u32)
327-
)
328-
);
329-
}
330-
331302
#[tokio::test]
332303
async fn test_update_reserve_config() {
333304
let (mut test, lending_market, lending_market_owner) = setup().await;
@@ -424,56 +395,3 @@ async fn test_update_invalid_oracle_config() {
424395
)
425396
);
426397
}
427-
428-
#[tokio::test]
429-
async fn test_update_invalid_reserve_config_bad_borrow_weight_bps() {
430-
let (mut test, lending_market, lending_market_owner) = setup().await;
431-
let wsol_reserve = test
432-
.init_reserve(
433-
&lending_market,
434-
&lending_market_owner,
435-
&wsol_mint::id(),
436-
&test_reserve_config(),
437-
&Keypair::new(),
438-
1000,
439-
None,
440-
)
441-
.await
442-
.unwrap();
443-
444-
let oracle = test.mints.get(&wsol_mint::id()).unwrap().unwrap();
445-
446-
let new_reserve_config = ReserveConfig {
447-
borrow_weight_bps: 9999,
448-
..test_reserve_config()
449-
};
450-
let new_rate_limiter_config = RateLimiterConfig {
451-
window_duration: 50,
452-
max_outflow: 100,
453-
};
454-
455-
let res = lending_market
456-
.update_reserve_config(
457-
&mut test,
458-
&lending_market_owner,
459-
&wsol_reserve,
460-
new_reserve_config,
461-
new_rate_limiter_config,
462-
Some(&Oracle {
463-
pyth_product_pubkey: oracle.pyth_product_pubkey,
464-
pyth_price_pubkey: oracle.pyth_price_pubkey,
465-
switchboard_feed_pubkey: Some(NULL_PUBKEY),
466-
}),
467-
)
468-
.await
469-
.unwrap_err()
470-
.unwrap();
471-
472-
assert_eq!(
473-
res,
474-
TransactionError::InstructionError(
475-
0,
476-
InstructionError::Custom(LendingError::InvalidConfig as u32)
477-
)
478-
);
479-
}

token-lending/sdk/src/instruction.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ impl LendingInstruction {
510510
let (fee_receiver, rest) = Self::unpack_pubkey(rest)?;
511511
let (protocol_liquidation_fee, rest) = Self::unpack_u8(rest)?;
512512
let (protocol_take_rate, rest) = Self::unpack_u8(rest)?;
513-
let (borrow_weight_bps, _rest) = Self::unpack_u64(rest)?;
513+
let (added_borrow_weight_bps, _rest) = Self::unpack_u64(rest)?;
514514
Self::InitReserve {
515515
liquidity_amount,
516516
config: ReserveConfig {
@@ -531,7 +531,7 @@ impl LendingInstruction {
531531
fee_receiver,
532532
protocol_liquidation_fee,
533533
protocol_take_rate,
534-
borrow_weight_bps,
534+
added_borrow_weight_bps,
535535
},
536536
}
537537
}
@@ -594,7 +594,7 @@ impl LendingInstruction {
594594
let (fee_receiver, rest) = Self::unpack_pubkey(rest)?;
595595
let (protocol_liquidation_fee, rest) = Self::unpack_u8(rest)?;
596596
let (protocol_take_rate, rest) = Self::unpack_u8(rest)?;
597-
let (borrow_weight_bps, rest) = Self::unpack_u64(rest)?;
597+
let (added_borrow_weight_bps, rest) = Self::unpack_u64(rest)?;
598598
let (window_duration, rest) = Self::unpack_u64(rest)?;
599599
let (max_outflow, _rest) = Self::unpack_u64(rest)?;
600600

@@ -617,7 +617,7 @@ impl LendingInstruction {
617617
fee_receiver,
618618
protocol_liquidation_fee,
619619
protocol_take_rate,
620-
borrow_weight_bps,
620+
added_borrow_weight_bps,
621621
},
622622
rate_limiter_config: RateLimiterConfig {
623623
window_duration,
@@ -744,7 +744,7 @@ impl LendingInstruction {
744744
fee_receiver,
745745
protocol_liquidation_fee,
746746
protocol_take_rate,
747-
borrow_weight_bps,
747+
added_borrow_weight_bps: borrow_weight_bps,
748748
},
749749
} => {
750750
buf.push(2);
@@ -835,7 +835,7 @@ impl LendingInstruction {
835835
buf.extend_from_slice(&config.fee_receiver.to_bytes());
836836
buf.extend_from_slice(&config.protocol_liquidation_fee.to_le_bytes());
837837
buf.extend_from_slice(&config.protocol_take_rate.to_le_bytes());
838-
buf.extend_from_slice(&config.borrow_weight_bps.to_le_bytes());
838+
buf.extend_from_slice(&config.added_borrow_weight_bps.to_le_bytes());
839839
buf.extend_from_slice(&rate_limiter_config.window_duration.to_le_bytes());
840840
buf.extend_from_slice(&rate_limiter_config.max_outflow.to_le_bytes());
841841
}

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

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ impl Reserve {
6666

6767
/// get borrow weight. Guaranteed to be greater than 1
6868
pub fn borrow_weight(&self) -> Decimal {
69-
Decimal::from_bps(std::cmp::max(self.config.borrow_weight_bps, 10_000))
69+
Decimal::one()
70+
.try_add(Decimal::from_bps(self.config.added_borrow_weight_bps))
71+
.unwrap()
7072
}
7173

7274
/// find price of tokens in quote currency
@@ -718,8 +720,9 @@ pub struct ReserveConfig {
718720
pub protocol_liquidation_fee: u8,
719721
/// Protocol take rate is the amount borrowed interest protocol recieves, as a percentage
720722
pub protocol_take_rate: u8,
721-
/// Borrow weight in basis points. This value cannot be less than 1.
722-
pub borrow_weight_bps: u64,
723+
/// Added borrow weight in basis points. THIS FIELD SHOULD NEVER BE USED DIRECTLY. Always use
724+
/// borrow_weight()
725+
pub added_borrow_weight_bps: u64,
723726
}
724727

725728
/// Additional fee information on a reserve
@@ -877,7 +880,7 @@ impl Pack for Reserve {
877880
config_protocol_take_rate,
878881
liquidity_accumulated_protocol_fees_wads,
879882
rate_limiter,
880-
config_borrow_weight_bps,
883+
config_added_borrow_weight_bps,
881884
_padding,
882885
) = mut_array_refs![
883886
output,
@@ -970,7 +973,7 @@ impl Pack for Reserve {
970973

971974
self.rate_limiter.pack_into_slice(rate_limiter);
972975

973-
*config_borrow_weight_bps = self.config.borrow_weight_bps.to_le_bytes();
976+
*config_added_borrow_weight_bps = self.config.added_borrow_weight_bps.to_le_bytes();
974977
}
975978

976979
/// Unpacks a byte buffer into a [ReserveInfo](struct.ReserveInfo.html).
@@ -1011,7 +1014,7 @@ impl Pack for Reserve {
10111014
config_protocol_take_rate,
10121015
liquidity_accumulated_protocol_fees_wads,
10131016
rate_limiter,
1014-
config_borrow_weight_bps,
1017+
config_added_borrow_weight_bps,
10151018
_padding,
10161019
) = array_refs![
10171020
input,
@@ -1104,11 +1107,7 @@ impl Pack for Reserve {
11041107
fee_receiver: Pubkey::new_from_array(*config_fee_receiver),
11051108
protocol_liquidation_fee: u8::from_le_bytes(*config_protocol_liquidation_fee),
11061109
protocol_take_rate: u8::from_le_bytes(*config_protocol_take_rate),
1107-
borrow_weight_bps: {
1108-
// this is a new field, so we need to handle the case where borrow_weight == 0
1109-
let borrow_weight_bps = u64::from_le_bytes(*config_borrow_weight_bps);
1110-
std::cmp::max(borrow_weight_bps, 10_000)
1111-
},
1110+
added_borrow_weight_bps: u64::from_le_bytes(*config_added_borrow_weight_bps),
11121111
},
11131112
rate_limiter: RateLimiter::unpack_from_slice(rate_limiter)?,
11141113
})
@@ -1653,7 +1652,7 @@ mod test {
16531652
// reserve state
16541653
market_price: Decimal,
16551654
decimal: u8,
1656-
borrow_weight_bps: u64,
1655+
added_borrow_weight_bps: u64,
16571656

16581657
borrow_fee_wad: u64,
16591658
host_fee: u8,
@@ -1671,7 +1670,7 @@ mod test {
16711670

16721671
market_price: Decimal::from(1u64),
16731672
decimal: 9,
1674-
borrow_weight_bps: 10000,
1673+
added_borrow_weight_bps: 0,
16751674

16761675
borrow_fee_wad: 10_000_000_000_000_000, // 1%
16771676
host_fee: 20,
@@ -1691,7 +1690,7 @@ mod test {
16911690

16921691
market_price: Decimal::from(1u64),
16931692
decimal: 9,
1694-
borrow_weight_bps: 10000,
1693+
added_borrow_weight_bps: 0,
16951694

16961695
borrow_fee_wad: 10_000_000_000_000_000, // 1%
16971696
host_fee: 20,
@@ -1711,7 +1710,7 @@ mod test {
17111710

17121711
market_price: Decimal::from(1u64),
17131712
decimal: 9,
1714-
borrow_weight_bps: 20_000,
1713+
added_borrow_weight_bps: 10_000,
17151714

17161715
borrow_fee_wad: 0,
17171716
host_fee: 0,
@@ -1731,7 +1730,7 @@ mod test {
17311730

17321731
market_price: Decimal::from(1u64),
17331732
decimal: 9,
1734-
borrow_weight_bps: 20_000,
1733+
added_borrow_weight_bps: 10_000,
17351734

17361735
borrow_fee_wad: 0,
17371736
host_fee: 0,
@@ -1751,7 +1750,7 @@ mod test {
17511750
fn calculate_borrow(test_case in calculate_borrow_test_cases()) {
17521751
let reserve = Reserve {
17531752
config: ReserveConfig {
1754-
borrow_weight_bps: test_case.borrow_weight_bps,
1753+
added_borrow_weight_bps: test_case.added_borrow_weight_bps,
17551754
fees: ReserveFees {
17561755
borrow_fee_wad: test_case.borrow_fee_wad,
17571756
host_fee_percentage: test_case.host_fee,
@@ -1774,20 +1773,4 @@ mod test {
17741773
), test_case.result);
17751774
}
17761775
}
1777-
1778-
// test that unpacking a reserve with a borrow weight of 0 fails
1779-
#[test]
1780-
fn test_unpack_reserve_with_zero_borrow_weight() {
1781-
let mut reserve = Reserve {
1782-
version: PROGRAM_VERSION,
1783-
..Reserve::default()
1784-
};
1785-
reserve.config.borrow_weight_bps = 0;
1786-
1787-
let mut buf = [0u8; Reserve::LEN];
1788-
Reserve::pack(reserve, &mut buf).unwrap();
1789-
1790-
let unpacked_reserve = Reserve::unpack(&buf).unwrap();
1791-
assert_eq!(unpacked_reserve.config.borrow_weight_bps, 10_000);
1792-
}
17931776
}

0 commit comments

Comments
 (0)