@@ -308,6 +308,19 @@ pub enum LendingInstruction {
308
308
/// The amount that is to be borrowed - u64::MAX for up to 100% of available liquidity
309
309
amount : u64 ,
310
310
} ,
311
+
312
+ // 14
313
+ /// Modify the ReserveConfig parameters of an already initialized Reserve account
314
+ ///
315
+ /// Accounts expected by this instruction:
316
+ ///
317
+ /// 0. `[writable]` Reserve account
318
+ /// 1. `[]` Lending market account
319
+ /// 2. `[signer]` Lending market owner
320
+ ModifyReserveConfig {
321
+ /// Reserve configuration updated values
322
+ new_config : ReserveConfig ,
323
+ } ,
311
324
}
312
325
313
326
impl LendingInstruction {
@@ -331,32 +344,10 @@ impl LendingInstruction {
331
344
}
332
345
2 => {
333
346
let ( liquidity_amount, rest) = Self :: unpack_u64 ( rest) ?;
334
- let ( optimal_utilization_rate, rest) = Self :: unpack_u8 ( rest) ?;
335
- let ( loan_to_value_ratio, rest) = Self :: unpack_u8 ( rest) ?;
336
- let ( liquidation_bonus, rest) = Self :: unpack_u8 ( rest) ?;
337
- let ( liquidation_threshold, rest) = Self :: unpack_u8 ( rest) ?;
338
- let ( min_borrow_rate, rest) = Self :: unpack_u8 ( rest) ?;
339
- let ( optimal_borrow_rate, rest) = Self :: unpack_u8 ( rest) ?;
340
- let ( max_borrow_rate, rest) = Self :: unpack_u8 ( rest) ?;
341
- let ( borrow_fee_wad, rest) = Self :: unpack_u64 ( rest) ?;
342
- let ( flash_loan_fee_wad, rest) = Self :: unpack_u64 ( rest) ?;
343
- let ( host_fee_percentage, _rest) = Self :: unpack_u8 ( rest) ?;
347
+ let config = Self :: unpack_reserve_config ( rest) ?;
344
348
Self :: InitReserve {
345
349
liquidity_amount,
346
- config : ReserveConfig {
347
- optimal_utilization_rate,
348
- loan_to_value_ratio,
349
- liquidation_bonus,
350
- liquidation_threshold,
351
- min_borrow_rate,
352
- optimal_borrow_rate,
353
- max_borrow_rate,
354
- fees : ReserveFees {
355
- borrow_fee_wad,
356
- flash_loan_fee_wad,
357
- host_fee_percentage,
358
- } ,
359
- } ,
350
+ config,
360
351
}
361
352
}
362
353
3 => Self :: RefreshReserve ,
@@ -394,6 +385,10 @@ impl LendingInstruction {
394
385
let ( amount, _rest) = Self :: unpack_u64 ( rest) ?;
395
386
Self :: FlashLoan { amount }
396
387
}
388
+ 14 => {
389
+ let new_config = Self :: unpack_reserve_config ( rest) ?;
390
+ Self :: ModifyReserveConfig { new_config }
391
+ }
397
392
_ => {
398
393
msg ! ( "Instruction cannot be unpacked" ) ;
399
394
return Err ( LendingError :: InstructionUnpackError . into ( ) ) ;
@@ -453,6 +448,34 @@ impl LendingInstruction {
453
448
Ok ( ( pk, rest) )
454
449
}
455
450
451
+ fn unpack_reserve_config ( input : & [ u8 ] ) -> Result < ReserveConfig , ProgramError > {
452
+ let ( optimal_utilization_rate, rest) = Self :: unpack_u8 ( input) ?;
453
+ let ( loan_to_value_ratio, rest) = Self :: unpack_u8 ( rest) ?;
454
+ let ( liquidation_bonus, rest) = Self :: unpack_u8 ( rest) ?;
455
+ let ( liquidation_threshold, rest) = Self :: unpack_u8 ( rest) ?;
456
+ let ( min_borrow_rate, rest) = Self :: unpack_u8 ( rest) ?;
457
+ let ( optimal_borrow_rate, rest) = Self :: unpack_u8 ( rest) ?;
458
+ let ( max_borrow_rate, rest) = Self :: unpack_u8 ( rest) ?;
459
+ let ( borrow_fee_wad, rest) = Self :: unpack_u64 ( rest) ?;
460
+ let ( flash_loan_fee_wad, rest) = Self :: unpack_u64 ( rest) ?;
461
+ let ( host_fee_percentage, _rest) = Self :: unpack_u8 ( rest) ?;
462
+
463
+ Ok ( ReserveConfig {
464
+ optimal_utilization_rate,
465
+ loan_to_value_ratio,
466
+ liquidation_bonus,
467
+ liquidation_threshold,
468
+ min_borrow_rate,
469
+ optimal_borrow_rate,
470
+ max_borrow_rate,
471
+ fees : ReserveFees {
472
+ borrow_fee_wad,
473
+ flash_loan_fee_wad,
474
+ host_fee_percentage,
475
+ } ,
476
+ } )
477
+ }
478
+
456
479
/// Packs a [LendingInstruction](enum.LendingInstruction.html) into a byte buffer.
457
480
pub fn pack ( & self ) -> Vec < u8 > {
458
481
let mut buf = Vec :: with_capacity ( size_of :: < Self > ( ) ) ;
@@ -471,35 +494,11 @@ impl LendingInstruction {
471
494
}
472
495
Self :: InitReserve {
473
496
liquidity_amount,
474
- config :
475
- ReserveConfig {
476
- optimal_utilization_rate,
477
- loan_to_value_ratio,
478
- liquidation_bonus,
479
- liquidation_threshold,
480
- min_borrow_rate,
481
- optimal_borrow_rate,
482
- max_borrow_rate,
483
- fees :
484
- ReserveFees {
485
- borrow_fee_wad,
486
- flash_loan_fee_wad,
487
- host_fee_percentage,
488
- } ,
489
- } ,
497
+ config,
490
498
} => {
491
499
buf. push ( 2 ) ;
492
500
buf. extend_from_slice ( & liquidity_amount. to_le_bytes ( ) ) ;
493
- buf. extend_from_slice ( & optimal_utilization_rate. to_le_bytes ( ) ) ;
494
- buf. extend_from_slice ( & loan_to_value_ratio. to_le_bytes ( ) ) ;
495
- buf. extend_from_slice ( & liquidation_bonus. to_le_bytes ( ) ) ;
496
- buf. extend_from_slice ( & liquidation_threshold. to_le_bytes ( ) ) ;
497
- buf. extend_from_slice ( & min_borrow_rate. to_le_bytes ( ) ) ;
498
- buf. extend_from_slice ( & optimal_borrow_rate. to_le_bytes ( ) ) ;
499
- buf. extend_from_slice ( & max_borrow_rate. to_le_bytes ( ) ) ;
500
- buf. extend_from_slice ( & borrow_fee_wad. to_le_bytes ( ) ) ;
501
- buf. extend_from_slice ( & flash_loan_fee_wad. to_le_bytes ( ) ) ;
502
- buf. extend_from_slice ( & host_fee_percentage. to_le_bytes ( ) ) ;
501
+ Self :: extend_buffer_from_reserve_config ( & mut buf, & config) ;
503
502
}
504
503
Self :: RefreshReserve => {
505
504
buf. push ( 3 ) ;
@@ -542,9 +541,27 @@ impl LendingInstruction {
542
541
buf. push ( 13 ) ;
543
542
buf. extend_from_slice ( & amount. to_le_bytes ( ) ) ;
544
543
}
544
+ Self :: ModifyReserveConfig { new_config } => {
545
+ buf. push ( 14 ) ;
546
+ Self :: extend_buffer_from_reserve_config ( & mut buf, & new_config) ;
547
+ }
545
548
}
546
549
buf
547
550
}
551
+
552
+ // Helper function to pack a ReserveConfig into a Vec<u8> buffer
553
+ fn extend_buffer_from_reserve_config ( buf : & mut Vec < u8 > , config : & ReserveConfig ) {
554
+ buf. extend_from_slice ( & config. optimal_utilization_rate . to_le_bytes ( ) ) ;
555
+ buf. extend_from_slice ( & config. loan_to_value_ratio . to_le_bytes ( ) ) ;
556
+ buf. extend_from_slice ( & config. liquidation_bonus . to_le_bytes ( ) ) ;
557
+ buf. extend_from_slice ( & config. liquidation_threshold . to_le_bytes ( ) ) ;
558
+ buf. extend_from_slice ( & config. min_borrow_rate . to_le_bytes ( ) ) ;
559
+ buf. extend_from_slice ( & config. optimal_borrow_rate . to_le_bytes ( ) ) ;
560
+ buf. extend_from_slice ( & config. max_borrow_rate . to_le_bytes ( ) ) ;
561
+ buf. extend_from_slice ( & config. fees . borrow_fee_wad . to_le_bytes ( ) ) ;
562
+ buf. extend_from_slice ( & config. fees . flash_loan_fee_wad . to_le_bytes ( ) ) ;
563
+ buf. extend_from_slice ( & config. fees . host_fee_percentage . to_le_bytes ( ) ) ;
564
+ }
548
565
}
549
566
550
567
/// Creates an 'InitLendingMarket' instruction.
@@ -982,6 +999,27 @@ pub fn flash_loan(
982
999
}
983
1000
}
984
1001
1002
+ /// Creates a 'ModifyReserveConfig` instruction.
1003
+ #[ allow( clippy:: too_many_arguments) ]
1004
+ pub fn modify_reserve_config (
1005
+ program_id : Pubkey ,
1006
+ config : ReserveConfig ,
1007
+ reserve_pubkey : Pubkey ,
1008
+ lending_market_pubkey : Pubkey ,
1009
+ lending_market_owner_pubkey : Pubkey ,
1010
+ ) -> Instruction {
1011
+ let accounts = vec ! [
1012
+ AccountMeta :: new( reserve_pubkey, false ) ,
1013
+ AccountMeta :: new( lending_market_pubkey, false ) ,
1014
+ AccountMeta :: new( lending_market_owner_pubkey, true ) ,
1015
+ ] ;
1016
+ Instruction {
1017
+ program_id,
1018
+ accounts,
1019
+ data : LendingInstruction :: ModifyReserveConfig { new_config : config } . pack ( ) ,
1020
+ }
1021
+ }
1022
+
985
1023
#[ cfg( test) ]
986
1024
mod tests {
987
1025
use super :: * ;
@@ -1386,4 +1424,39 @@ mod tests {
1386
1424
LendingInstruction :: FlashLoan { amount } . pack( )
1387
1425
) ;
1388
1426
}
1427
+
1428
+ #[ test]
1429
+ fn test_modify_reserve_config ( ) {
1430
+ let program_id = Pubkey :: new_unique ( ) ;
1431
+ let config = ReserveConfig {
1432
+ optimal_utilization_rate : 60 ,
1433
+ loan_to_value_ratio : 1 ,
1434
+ liquidation_bonus : 10 ,
1435
+ liquidation_threshold : 5 ,
1436
+ min_borrow_rate : 2 ,
1437
+ optimal_borrow_rate : 4 ,
1438
+ max_borrow_rate : 10 ,
1439
+ fees : ReserveFees {
1440
+ borrow_fee_wad : 1 ,
1441
+ flash_loan_fee_wad : 3 ,
1442
+ host_fee_percentage : 1 ,
1443
+ } ,
1444
+ } ;
1445
+ let reserve_pubkey = Pubkey :: new_unique ( ) ;
1446
+ let lending_market_pubkey = Pubkey :: new_unique ( ) ;
1447
+ let lending_market_owner_pubkey = Pubkey :: new_unique ( ) ;
1448
+ let instruction = modify_reserve_config (
1449
+ program_id,
1450
+ config,
1451
+ reserve_pubkey,
1452
+ lending_market_pubkey,
1453
+ lending_market_owner_pubkey,
1454
+ ) ;
1455
+ assert_eq ! ( instruction. program_id, program_id) ;
1456
+ assert_eq ! ( instruction. accounts. len( ) , 3 ) ;
1457
+ assert_eq ! (
1458
+ instruction. data,
1459
+ LendingInstruction :: ModifyReserveConfig { new_config: config } . pack( )
1460
+ ) ;
1461
+ }
1389
1462
}
0 commit comments