forked from solana-labs/solana-program-library
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathsolend_program_test.rs
More file actions
1917 lines (1705 loc) · 59 KB
/
solend_program_test.rs
File metadata and controls
1917 lines (1705 loc) · 59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use bytemuck::checked::from_bytes;
use solend_sdk::instruction::*;
use solend_sdk::pyth_mainnet;
use solend_sdk::state::*;
use solend_sdk::switchboard_v2_mainnet;
use super::{
flash_loan_proxy::proxy_program,
mock_switchboard::{init_switchboard, set_switchboard_price},
};
use crate::helpers::*;
use solana_program::native_token::LAMPORTS_PER_SOL;
use solend_program::state::RateLimiterConfig;
use solend_sdk::{instruction::update_reserve_config, NULL_PUBKEY};
use pyth_sdk_solana::state::PROD_ACCT_SIZE;
use solana_program::{
clock::Clock,
instruction::Instruction,
program_pack::{IsInitialized, Pack},
pubkey::Pubkey,
rent::Rent,
system_instruction, sysvar,
};
use solana_sdk::{
compute_budget::ComputeBudgetInstruction,
signature::{Keypair, Signer},
system_instruction::create_account,
transaction::Transaction,
};
use solend_program::{
instruction::{
deposit_obligation_collateral, deposit_reserve_liquidity, forgive_debt,
init_lending_market, init_reserve, liquidate_obligation_and_redeem_reserve_collateral,
redeem_fees, redeem_reserve_collateral, repay_obligation_liquidity,
set_lending_market_owner_and_config, withdraw_obligation_collateral,
},
processor::process_instruction,
state::{LendingMarket, Reserve, ReserveConfig},
};
use spl_token::state::{Account as Token, Mint};
use std::{
collections::{HashMap, HashSet},
str::FromStr,
};
use super::mock_pyth::{init, set_price};
pub struct SolendProgramTest {
pub context: ProgramTestContext,
rent: Rent,
// authority of all mints
authority: Keypair,
pub mints: HashMap<Pubkey, Option<Oracle>>,
}
#[derive(Debug, Clone, Copy)]
pub struct Oracle {
pub pyth_product_pubkey: Pubkey,
pub pyth_price_pubkey: Pubkey,
pub switchboard_feed_pubkey: Option<Pubkey>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Info<T> {
pub pubkey: Pubkey,
pub account: T,
}
impl SolendProgramTest {
pub async fn start_with_test(mut test: ProgramTest) -> Self {
test.prefer_bpf(false);
test.add_program(
"mock_pyth",
pyth_mainnet::id(),
processor!(mock_pyth::process_instruction),
);
test.add_program(
"mock_switchboard",
switchboard_v2_mainnet::id(),
processor!(mock_switchboard::process_instruction),
);
test.add_program(
"flash_loan_proxy",
proxy_program::id(),
processor!(flash_loan_proxy::process_instruction),
);
let authority = Keypair::new();
add_mint(&mut test, usdc_mint::id(), 6, authority.pubkey());
add_mint(&mut test, usdt_mint::id(), 6, authority.pubkey());
add_mint(&mut test, wsol_mint::id(), 9, authority.pubkey());
add_mint(&mut test, msol_mint::id(), 9, authority.pubkey());
add_mint(&mut test, bonk_mint::id(), 5, authority.pubkey());
let mut context = test.start_with_context().await;
let rent = context.banks_client.get_rent().await.unwrap();
SolendProgramTest {
context,
rent,
authority,
mints: HashMap::from([
(usdc_mint::id(), None),
(wsol_mint::id(), None),
(msol_mint::id(), None),
(usdt_mint::id(), None),
(bonk_mint::id(), None),
]),
}
}
pub async fn start_new() -> Self {
let mut test = ProgramTest::new(
"solend_program",
solend_program::id(),
processor!(process_instruction),
);
test.prefer_bpf(false);
test.add_program(
"mock_pyth",
pyth_mainnet::id(),
processor!(mock_pyth::process_instruction),
);
test.add_program(
"mock_switchboard",
switchboard_v2_mainnet::id(),
processor!(mock_switchboard::process_instruction),
);
test.add_program(
"flash_loan_proxy",
proxy_program::id(),
processor!(flash_loan_proxy::process_instruction),
);
let authority = Keypair::new();
add_mint(&mut test, usdc_mint::id(), 6, authority.pubkey());
add_mint(&mut test, usdt_mint::id(), 6, authority.pubkey());
add_mint(&mut test, wsol_mint::id(), 9, authority.pubkey());
add_mint(&mut test, msol_mint::id(), 9, authority.pubkey());
add_mint(&mut test, bonk_mint::id(), 5, authority.pubkey());
let mut context = test.start_with_context().await;
let rent = context.banks_client.get_rent().await.unwrap();
SolendProgramTest {
context,
rent,
authority,
mints: HashMap::from([
(usdc_mint::id(), None),
(wsol_mint::id(), None),
(msol_mint::id(), None),
(usdt_mint::id(), None),
(bonk_mint::id(), None),
]),
}
}
pub async fn process_transaction(
&mut self,
instructions: &[Instruction],
signers: Option<&[&Keypair]>,
) -> Result<(), BanksClientError> {
let mut transaction =
Transaction::new_with_payer(instructions, Some(&self.context.payer.pubkey()));
let mut all_signers = vec![&self.context.payer];
if let Some(signers) = signers {
all_signers.extend_from_slice(signers);
}
// This fails when warping is involved - https://gitmemory.com/issue/solana-labs/solana/18201/868325078
// let recent_blockhash = self.context.banks_client.get_recent_blockhash().await.unwrap();
transaction.sign(&all_signers, self.context.last_blockhash);
let serialized = bincode::serialize(&transaction).unwrap();
assert!(serialized.len() <= 1232);
self.context
.banks_client
.process_transaction(transaction)
.await
}
pub async fn load_optional_account<T: Pack + IsInitialized>(
&mut self,
acc_pk: Pubkey,
) -> Info<Option<T>> {
self.context
.banks_client
.get_account(acc_pk)
.await
.unwrap()
.map(|acc| Info {
pubkey: acc_pk,
account: T::unpack(&acc.data).ok(),
})
.unwrap()
}
pub async fn load_account<T: Pack + IsInitialized>(&mut self, acc_pk: Pubkey) -> Info<T> {
let acc = self
.context
.banks_client
.get_account(acc_pk)
.await
.unwrap()
.unwrap();
Info {
pubkey: acc_pk,
account: T::unpack(&acc.data).unwrap(),
}
}
pub async fn load_zeroable_account<T: Pod + Copy>(&mut self, acc_pk: Pubkey) -> Info<T> {
let acc = self
.context
.banks_client
.get_account(acc_pk)
.await
.unwrap()
.unwrap();
Info {
pubkey: acc_pk,
account: *from_bytes::<T>(&acc.data),
}
}
pub async fn get_bincode_account<T: serde::de::DeserializeOwned>(
&mut self,
address: &Pubkey,
) -> T {
self.context
.banks_client
.get_account(*address)
.await
.unwrap()
.map(|a| bincode::deserialize::<T>(&a.data).unwrap())
.unwrap_or_else(|| panic!("GET-TEST-ACCOUNT-ERROR"))
}
#[allow(dead_code)]
pub async fn get_clock(&mut self) -> Clock {
self.get_bincode_account::<Clock>(&sysvar::clock::id())
.await
}
/// Advances clock by x slots. note that transactions don't automatically increment the slot
/// value in Clock, so this function must be explicitly called whenever you want time to move
/// forward.
pub async fn advance_clock_by_slots(&mut self, slots: u64) {
let clock: Clock = self.get_clock().await;
self.context.warp_to_slot(clock.slot + slots).unwrap();
}
pub async fn create_account(
&mut self,
size: usize,
owner: &Pubkey,
keypair: Option<&Keypair>,
) -> Pubkey {
let rent = self.rent.minimum_balance(size);
let new_keypair = Keypair::new();
let keypair = match keypair {
None => &new_keypair,
Some(kp) => kp,
};
let instructions = [system_instruction::create_account(
&self.context.payer.pubkey(),
&keypair.pubkey(),
rent,
size as u64,
owner,
)];
self.process_transaction(&instructions, Some(&[keypair]))
.await
.unwrap();
keypair.pubkey()
}
pub async fn create_mint(&mut self, mint_authority: &Pubkey) -> Pubkey {
let keypair = Keypair::new();
let rent = self.rent.minimum_balance(Mint::LEN);
let instructions = [
system_instruction::create_account(
&self.context.payer.pubkey(),
&keypair.pubkey(),
rent,
Mint::LEN as u64,
&spl_token::id(),
),
spl_token::instruction::initialize_mint(
&spl_token::id(),
&keypair.pubkey(),
mint_authority,
None,
0,
)
.unwrap(),
];
self.process_transaction(&instructions, Some(&[&keypair]))
.await
.unwrap();
keypair.pubkey()
}
pub async fn create_token_account(&mut self, owner: &Pubkey, mint: &Pubkey) -> Pubkey {
let keypair = Keypair::new();
let instructions = [
system_instruction::create_account(
&self.context.payer.pubkey(),
&keypair.pubkey(),
self.rent.minimum_balance(Token::LEN),
spl_token::state::Account::LEN as u64,
&spl_token::id(),
),
spl_token::instruction::initialize_account(
&spl_token::id(),
&keypair.pubkey(),
mint,
owner,
)
.unwrap(),
];
self.process_transaction(&instructions, Some(&[&keypair]))
.await
.unwrap();
keypair.pubkey()
}
pub async fn mint_to(&mut self, mint: &Pubkey, dst: &Pubkey, amount: u64) {
assert!(self.mints.contains_key(mint));
let instructions = [spl_token::instruction::mint_to(
&spl_token::id(),
mint,
dst,
&self.authority.pubkey(),
&[],
amount,
)
.unwrap()];
let authority = Keypair::from_bytes(&self.authority.to_bytes()).unwrap(); // hack
self.process_transaction(&instructions, Some(&[&authority]))
.await
.unwrap();
}
// wrappers around solend instructions. these should be used to test logic things (eg you can't
// borrow more than the borrow limit, but these methods can't be used to test account-level
// security of an instruction (eg what happens if im not the lending market owner but i try to
// add a reserve anyways).
pub async fn init_lending_market(
&mut self,
owner: &User,
lending_market_key: &Keypair,
) -> Result<Info<LendingMarket>, BanksClientError> {
let payer = self.context.payer.pubkey();
let lamports = Rent::minimum_balance(&self.rent, LendingMarket::LEN);
let res = self
.process_transaction(
&[
create_account(
&payer,
&lending_market_key.pubkey(),
lamports,
LendingMarket::LEN as u64,
&solend_program::id(),
),
init_lending_market(
solend_program::id(),
owner.keypair.pubkey(),
QUOTE_CURRENCY,
lending_market_key.pubkey(),
pyth_mainnet::id(),
switchboard_v2_mainnet::id(),
),
],
Some(&[lending_market_key]),
)
.await;
match res {
Ok(()) => Ok(self
.load_account::<LendingMarket>(lending_market_key.pubkey())
.await),
Err(e) => Err(e),
}
}
pub async fn init_pyth_feed(&mut self, mint: &Pubkey) -> Pubkey {
let pyth_price_pubkey = self.create_account(3312, &pyth_mainnet::id(), None).await;
let pyth_product_pubkey = self
.create_account(PROD_ACCT_SIZE, &pyth_mainnet::id(), None)
.await;
self.process_transaction(
&[init(
pyth_mainnet::id(),
pyth_price_pubkey,
pyth_product_pubkey,
)],
None,
)
.await
.unwrap();
self.mints.insert(
*mint,
Some(Oracle {
pyth_product_pubkey,
pyth_price_pubkey,
switchboard_feed_pubkey: None,
}),
);
pyth_price_pubkey
}
pub async fn set_price(&mut self, mint: &Pubkey, price: &PriceArgs) {
let oracle = self.mints.get(mint).unwrap().unwrap();
self.process_transaction(
&[set_price(
pyth_mainnet::id(),
oracle.pyth_price_pubkey,
price.price,
price.conf,
price.expo,
price.ema_price,
price.ema_conf,
)],
None,
)
.await
.unwrap();
}
pub async fn init_switchboard_feed(&mut self, mint: &Pubkey) -> Pubkey {
let switchboard_feed_pubkey = self
.create_account(
std::mem::size_of::<AggregatorAccountData>() + 8,
&switchboard_v2_mainnet::id(),
None,
)
.await;
self.process_transaction(
&[init_switchboard(
switchboard_v2_mainnet::id(),
switchboard_feed_pubkey,
)],
None,
)
.await
.unwrap();
let oracle = self.mints.get_mut(mint).unwrap();
if let Some(ref mut oracle) = oracle {
oracle.switchboard_feed_pubkey = Some(switchboard_feed_pubkey);
switchboard_feed_pubkey
} else {
panic!("oracle not initialized");
}
}
pub async fn set_switchboard_price(&mut self, mint: &Pubkey, price: SwitchboardPriceArgs) {
let oracle = self.mints.get(mint).unwrap().unwrap();
self.process_transaction(
&[set_switchboard_price(
switchboard_v2_mainnet::id(),
oracle.switchboard_feed_pubkey.unwrap(),
price.price,
price.expo,
)],
None,
)
.await
.unwrap();
}
#[allow(clippy::too_many_arguments)]
pub async fn init_reserve(
&mut self,
lending_market: &Info<LendingMarket>,
lending_market_owner: &User,
mint: &Pubkey,
reserve_config: &ReserveConfig,
reserve_keypair: &Keypair,
liquidity_amount: u64,
oracle: Option<Oracle>,
) -> Result<Info<Reserve>, BanksClientError> {
let destination_collateral_pubkey = self
.create_account(Token::LEN, &spl_token::id(), None)
.await;
let reserve_liquidity_supply_pubkey = self
.create_account(Token::LEN, &spl_token::id(), None)
.await;
let reserve_pubkey = self
.create_account(Reserve::LEN, &solend_program::id(), Some(reserve_keypair))
.await;
let reserve_liquidity_fee_receiver = self
.create_account(Token::LEN, &spl_token::id(), None)
.await;
let reserve_collateral_mint_pubkey =
self.create_account(Mint::LEN, &spl_token::id(), None).await;
let reserve_collateral_supply_pubkey = self
.create_account(Token::LEN, &spl_token::id(), None)
.await;
let oracle = if let Some(o) = oracle {
o
} else {
self.mints.get(mint).unwrap().unwrap()
};
let res = self
.process_transaction(
&[
ComputeBudgetInstruction::set_compute_unit_limit(70_000),
init_reserve(
solend_program::id(),
liquidity_amount,
ReserveConfig {
fee_receiver: reserve_liquidity_fee_receiver,
..*reserve_config
},
lending_market_owner.get_account(mint).unwrap(),
destination_collateral_pubkey,
reserve_pubkey,
*mint,
reserve_liquidity_supply_pubkey,
reserve_collateral_mint_pubkey,
reserve_collateral_supply_pubkey,
oracle.pyth_product_pubkey,
oracle.pyth_price_pubkey,
Pubkey::from_str("nu11111111111111111111111111111111111111111").unwrap(),
lending_market.pubkey,
lending_market_owner.keypair.pubkey(),
lending_market_owner.keypair.pubkey(),
),
],
Some(&[&lending_market_owner.keypair]),
)
.await;
match res {
Ok(()) => Ok(self.load_account::<Reserve>(reserve_pubkey).await),
Err(e) => Err(e),
}
}
}
/// 1 User holds many token accounts
#[derive(Debug)]
pub struct User {
pub keypair: Keypair,
pub token_accounts: Vec<Info<Token>>,
}
impl User {
pub fn new_with_keypair(keypair: Keypair) -> Self {
User {
keypair,
token_accounts: Vec::new(),
}
}
/// Creates a user with specified token accounts and balances. This function only works if the
/// SolendProgramTest object owns the mint authorities. eg this won't work for native SOL.
pub async fn new_with_balances(
test: &mut SolendProgramTest,
mints_and_balances: &[(&Pubkey, u64)],
) -> Self {
let mut user = User {
keypair: Keypair::new(),
token_accounts: Vec::new(),
};
for (mint, balance) in mints_and_balances {
let token_account = user.create_token_account(mint, test).await;
if *balance > 0 {
test.mint_to(mint, &token_account.pubkey, *balance).await;
}
}
user
}
pub fn get_account(&self, mint: &Pubkey) -> Option<Pubkey> {
self.token_accounts.iter().find_map(|ta| {
if ta.account.mint == *mint {
Some(ta.pubkey)
} else {
None
}
})
}
pub async fn get_balance(&self, test: &mut SolendProgramTest, mint: &Pubkey) -> Option<u64> {
match self.get_account(mint) {
None => None,
Some(pubkey) => {
let token_account = test.load_account::<Token>(pubkey).await;
Some(token_account.account.amount)
}
}
}
pub async fn create_token_account(
&mut self,
mint: &Pubkey,
test: &mut SolendProgramTest,
) -> Info<Token> {
match self
.token_accounts
.iter()
.find(|ta| ta.account.mint == *mint)
{
None => {
let pubkey = test
.create_token_account(&self.keypair.pubkey(), mint)
.await;
let account = test.load_account::<Token>(pubkey).await;
self.token_accounts.push(account.clone());
account
}
Some(t) => t.clone(),
}
}
pub async fn transfer(
&self,
mint: &Pubkey,
destination_pubkey: Pubkey,
amount: u64,
test: &mut SolendProgramTest,
) {
let instruction = [spl_token::instruction::transfer(
&spl_token::id(),
&self.get_account(mint).unwrap(),
&destination_pubkey,
&self.keypair.pubkey(),
&[],
amount,
)
.unwrap()];
test.process_transaction(&instruction, Some(&[&self.keypair]))
.await
.unwrap();
}
}
#[derive(Debug, Clone)]
pub struct PriceArgs {
pub price: i64,
pub conf: u64,
pub expo: i32,
pub ema_price: i64,
pub ema_conf: u64,
}
pub struct SwitchboardPriceArgs {
pub price: i64,
pub expo: i32,
}
impl Info<LendingMarket> {
pub async fn set_obligation_closeability_status(
&self,
test: &mut SolendProgramTest,
obligation: &Info<Obligation>,
reserve: &Info<Reserve>,
risk_authority: &User,
closeable: bool,
) -> Result<(), BanksClientError> {
let refresh_ixs = self
.build_refresh_instructions(test, obligation, None)
.await;
test.process_transaction(&refresh_ixs, None).await.unwrap();
let ix = vec![set_obligation_closeability_status(
solend_program::id(),
obligation.pubkey,
reserve.pubkey,
self.pubkey,
risk_authority.keypair.pubkey(),
closeable,
)];
test.process_transaction(&ix, Some(&[&risk_authority.keypair]))
.await
}
pub async fn deposit(
&self,
test: &mut SolendProgramTest,
reserve: &Info<Reserve>,
user: &User,
liquidity_amount: u64,
) -> Result<(), BanksClientError> {
let instructions = [
ComputeBudgetInstruction::set_compute_unit_limit(50_000),
deposit_reserve_liquidity(
solend_program::id(),
liquidity_amount,
user.get_account(&reserve.account.liquidity.mint_pubkey)
.unwrap(),
user.get_account(&reserve.account.collateral.mint_pubkey)
.unwrap(),
reserve.pubkey,
reserve.account.liquidity.supply_pubkey,
reserve.account.collateral.mint_pubkey,
self.pubkey,
user.keypair.pubkey(),
),
];
test.process_transaction(&instructions, Some(&[&user.keypair]))
.await
}
pub async fn update_reserve_config(
&self,
test: &mut SolendProgramTest,
signer: &User, // lending market owner or risk authority
reserve: &Info<Reserve>,
config: ReserveConfig,
rate_limiter_config: RateLimiterConfig,
oracle: Option<&Oracle>,
) -> Result<(), BanksClientError> {
let default_oracle = test
.mints
.get(&reserve.account.liquidity.mint_pubkey)
.unwrap()
.unwrap();
let oracle = oracle.unwrap_or(&default_oracle);
let instructions = [
ComputeBudgetInstruction::set_compute_unit_limit(30_000),
update_reserve_config(
solend_program::id(),
config,
rate_limiter_config,
reserve.pubkey,
self.pubkey,
signer.keypair.pubkey(),
oracle.pyth_product_pubkey,
oracle.pyth_price_pubkey,
oracle.switchboard_feed_pubkey.unwrap_or(NULL_PUBKEY),
),
];
test.process_transaction(&instructions, Some(&[&signer.keypair]))
.await
}
pub async fn deposit_reserve_liquidity_and_obligation_collateral(
&self,
test: &mut SolendProgramTest,
reserve: &Info<Reserve>,
obligation: &Info<Obligation>,
user: &User,
liquidity_amount: u64,
) -> Result<(), BanksClientError> {
let instructions = [
ComputeBudgetInstruction::set_compute_unit_limit(70_000),
deposit_reserve_liquidity_and_obligation_collateral(
solend_program::id(),
liquidity_amount,
user.get_account(&reserve.account.liquidity.mint_pubkey)
.unwrap(),
user.get_account(&reserve.account.collateral.mint_pubkey)
.unwrap(),
reserve.pubkey,
reserve.account.liquidity.supply_pubkey,
reserve.account.collateral.mint_pubkey,
self.pubkey,
reserve.account.collateral.supply_pubkey,
obligation.pubkey,
user.keypair.pubkey(),
reserve.account.liquidity.pyth_oracle_pubkey,
reserve.account.liquidity.switchboard_oracle_pubkey,
user.keypair.pubkey(),
),
];
test.process_transaction(&instructions, Some(&[&user.keypair]))
.await
}
pub async fn redeem(
&self,
test: &mut SolendProgramTest,
reserve: &Info<Reserve>,
user: &User,
collateral_amount: u64,
) -> Result<(), BanksClientError> {
let instructions = [
ComputeBudgetInstruction::set_compute_unit_limit(48_000),
refresh_reserve(
solend_program::id(),
reserve.pubkey,
reserve.account.liquidity.pyth_oracle_pubkey,
reserve.account.liquidity.switchboard_oracle_pubkey,
reserve.account.config.extra_oracle_pubkey,
),
redeem_reserve_collateral(
solend_program::id(),
collateral_amount,
user.get_account(&reserve.account.collateral.mint_pubkey)
.unwrap(),
user.get_account(&reserve.account.liquidity.mint_pubkey)
.unwrap(),
reserve.pubkey,
reserve.account.collateral.mint_pubkey,
reserve.account.liquidity.supply_pubkey,
self.pubkey,
user.keypair.pubkey(),
),
];
test.process_transaction(&instructions, Some(&[&user.keypair]))
.await
}
pub async fn init_obligation(
&self,
test: &mut SolendProgramTest,
obligation_keypair: Keypair,
user: &User,
) -> Result<Info<Obligation>, BanksClientError> {
let instructions = [
ComputeBudgetInstruction::set_compute_unit_limit(10_000),
system_instruction::create_account(
&test.context.payer.pubkey(),
&obligation_keypair.pubkey(),
Rent::minimum_balance(&Rent::default(), Obligation::LEN),
Obligation::LEN as u64,
&solend_program::id(),
),
init_obligation(
solend_program::id(),
obligation_keypair.pubkey(),
self.pubkey,
user.keypair.pubkey(),
),
];
match test
.process_transaction(&instructions, Some(&[&obligation_keypair, &user.keypair]))
.await
{
Ok(()) => Ok(test
.load_account::<Obligation>(obligation_keypair.pubkey())
.await),
Err(e) => Err(e),
}
}
pub async fn deposit_obligation_collateral(
&self,
test: &mut SolendProgramTest,
reserve: &Info<Reserve>,
obligation: &Info<Obligation>,
user: &User,
collateral_amount: u64,
) -> Result<(), BanksClientError> {
let instructions = [
ComputeBudgetInstruction::set_compute_unit_limit(38_000),
deposit_obligation_collateral(
solend_program::id(),
collateral_amount,
user.get_account(&reserve.account.collateral.mint_pubkey)
.unwrap(),
reserve.account.collateral.supply_pubkey,
reserve.pubkey,
obligation.pubkey,
self.pubkey,
user.keypair.pubkey(),
user.keypair.pubkey(),
),
];
test.process_transaction(&instructions, Some(&[&user.keypair]))
.await
}
pub async fn refresh_reserve(
&self,
test: &mut SolendProgramTest,
reserve: &Info<Reserve>,
) -> Result<(), BanksClientError> {
test.process_transaction(
&[
ComputeBudgetInstruction::set_compute_unit_limit(40_000),
refresh_reserve(
solend_program::id(),
reserve.pubkey,
reserve.account.liquidity.pyth_oracle_pubkey,
reserve.account.liquidity.switchboard_oracle_pubkey,
reserve.account.config.extra_oracle_pubkey,
),
],
None,
)
.await
}
pub async fn build_refresh_instructions(
&self,
test: &mut SolendProgramTest,
obligation: &Info<Obligation>,
extra_reserve: Option<&Info<Reserve>>,
) -> Vec<Instruction> {
let obligation = test.load_account::<Obligation>(obligation.pubkey).await;
let reserve_pubkeys: Vec<Pubkey> = {
let mut r = HashSet::new();
r.extend(
obligation
.account
.deposits
.iter()
.map(|d| d.deposit_reserve),
);
r.extend(obligation.account.borrows.iter().map(|b| b.borrow_reserve));
if let Some(reserve) = extra_reserve {
r.insert(reserve.pubkey);
}
r.into_iter().collect()
};
let mut reserves = Vec::new();
for pubkey in reserve_pubkeys {
reserves.push(test.load_account::<Reserve>(pubkey).await);
}
let mut instructions: Vec<Instruction> = reserves
.into_iter()
.map(|reserve| {
refresh_reserve(
solend_program::id(),
reserve.pubkey,
reserve.account.liquidity.pyth_oracle_pubkey,
reserve.account.liquidity.switchboard_oracle_pubkey,
reserve.account.config.extra_oracle_pubkey,
)
})
.collect();
let reserve_pubkeys: Vec<Pubkey> = {
let mut r = Vec::new();
r.extend(
obligation
.account
.deposits
.iter()
.map(|d| d.deposit_reserve),
);
r.extend(obligation.account.borrows.iter().map(|b| b.borrow_reserve));
r
};
instructions.push(refresh_obligation(
solend_program::id(),
obligation.pubkey,
reserve_pubkeys,
));