@@ -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