-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
instruction.rs
1504 lines (1444 loc) · 53.7 KB
/
instruction.rs
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
//! Instruction types
#![allow(clippy::too_many_arguments)]
use {
crate::{
find_deposit_authority_program_address, find_ephemeral_stake_program_address,
find_stake_program_address, find_transient_stake_program_address,
find_withdraw_authority_program_address,
state::{Fee, FeeType, StakePool, ValidatorList},
MAX_VALIDATORS_TO_UPDATE,
},
borsh::{BorshDeserialize, BorshSchema, BorshSerialize},
mpl_token_metadata::pda::find_metadata_account,
solana_program::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
stake, system_program, sysvar,
},
std::num::NonZeroU32,
};
/// Defines which validator vote account is set during the
/// `SetPreferredValidator` instruction
#[repr(C)]
#[derive(Clone, Debug, PartialEq, BorshSerialize, BorshDeserialize, BorshSchema)]
pub enum PreferredValidatorType {
/// Set preferred validator for deposits
Deposit,
/// Set preferred validator for withdraws
Withdraw,
}
/// Defines which authority to update in the `SetFundingAuthority`
/// instruction
#[repr(C)]
#[derive(Clone, Debug, PartialEq, BorshSerialize, BorshDeserialize, BorshSchema)]
pub enum FundingType {
/// Sets the stake deposit authority
StakeDeposit,
/// Sets the SOL deposit authority
SolDeposit,
/// Sets the SOL withdraw authority
SolWithdraw,
}
/// Instructions supported by the StakePool program.
#[repr(C)]
#[derive(Clone, Debug, PartialEq, BorshSerialize, BorshDeserialize)]
pub enum StakePoolInstruction {
/// Initializes a new StakePool.
///
/// 0. `[w]` New StakePool to create.
/// 1. `[s]` Manager
/// 2. `[]` Staker
/// 3. `[]` Stake pool withdraw authority
/// 4. `[w]` Uninitialized validator stake list storage account
/// 5. `[]` Reserve stake account must be initialized, have zero balance,
/// and staker / withdrawer authority set to pool withdraw authority.
/// 6. `[]` Pool token mint. Must have zero supply, owned by withdraw authority.
/// 7. `[]` Pool account to deposit the generated fee for manager.
/// 8. `[]` Token program id
/// 9. `[]` (Optional) Deposit authority that must sign all deposits.
/// Defaults to the program address generated using
/// `find_deposit_authority_program_address`, making deposits permissionless.
Initialize {
/// Fee assessed as percentage of perceived rewards
fee: Fee,
/// Fee charged per withdrawal as percentage of withdrawal
withdrawal_fee: Fee,
/// Fee charged per deposit as percentage of deposit
deposit_fee: Fee,
/// Percentage [0-100] of deposit_fee that goes to referrer
referral_fee: u8,
/// Maximum expected number of validators
max_validators: u32,
},
/// (Staker only) Adds stake account delegated to validator to the pool's
/// list of managed validators.
///
/// The stake account will have the rent-exempt amount plus
/// `max(crate::MINIMUM_ACTIVE_STAKE, solana_program::stake::tools::get_minimum_delegation())`.
/// It is funded from the stake pool reserve.
///
/// 0. `[w]` Stake pool
/// 1. `[s]` Staker
/// 2. `[w]` Reserve stake account
/// 3. `[]` Stake pool withdraw authority
/// 4. `[w]` Validator stake list storage account
/// 5. `[w]` Stake account to add to the pool
/// 6. `[]` Validator this stake account will be delegated to
/// 7. `[]` Rent sysvar
/// 8. `[]` Clock sysvar
/// 9. '[]' Stake history sysvar
/// 10. '[]' Stake config sysvar
/// 11. `[]` System program
/// 12. `[]` Stake program
///
/// userdata: optional non-zero u32 seed used for generating the validator
/// stake address
AddValidatorToPool(u32),
/// (Staker only) Removes validator from the pool, deactivating its stake
///
/// Only succeeds if the validator stake account has the minimum of
/// `max(crate::MINIMUM_ACTIVE_STAKE, solana_program::stake::tools::get_minimum_delegation())`.
/// plus the rent-exempt amount.
///
/// 0. `[w]` Stake pool
/// 1. `[s]` Staker
/// 2. `[]` Stake pool withdraw authority
/// 3. `[w]` Validator stake list storage account
/// 4. `[w]` Stake account to remove from the pool
/// 5. `[]` Transient stake account, to check that that we're not trying to activate
/// 6. `[]` Sysvar clock
/// 7. `[]` Stake program id,
RemoveValidatorFromPool,
/// (Staker only) Decrease active stake on a validator, eventually moving it to the reserve
///
/// Internally, this instruction splits a validator stake account into its
/// corresponding transient stake account and deactivates it.
///
/// In order to rebalance the pool without taking custody, the staker needs
/// a way of reducing the stake on a stake account. This instruction splits
/// some amount of stake, up to the total activated stake, from the canonical
/// validator stake account, into its "transient" stake account.
///
/// The instruction only succeeds if the transient stake account does not
/// exist. The amount of lamports to move must be at least rent-exemption plus
/// `max(crate::MINIMUM_ACTIVE_STAKE, solana_program::stake::tools::get_minimum_delegation())`.
///
/// 0. `[]` Stake pool
/// 1. `[s]` Stake pool staker
/// 2. `[]` Stake pool withdraw authority
/// 3. `[w]` Validator list
/// 4. `[w]` Canonical stake account to split from
/// 5. `[w]` Transient stake account to receive split
/// 6. `[]` Clock sysvar
/// 7. `[]` Rent sysvar
/// 8. `[]` System program
/// 9. `[]` Stake program
DecreaseValidatorStake {
/// amount of lamports to split into the transient stake account
lamports: u64,
/// seed used to create transient stake account
transient_stake_seed: u64,
},
/// (Staker only) Increase stake on a validator from the reserve account
///
/// Internally, this instruction splits reserve stake into a transient stake
/// account and delegate to the appropriate validator. `UpdateValidatorListBalance`
/// will do the work of merging once it's ready.
///
/// This instruction only succeeds if the transient stake account does not exist.
/// The minimum amount to move is rent-exemption plus
/// `max(crate::MINIMUM_ACTIVE_STAKE, solana_program::stake::tools::get_minimum_delegation())`.
///
/// 0. `[]` Stake pool
/// 1. `[s]` Stake pool staker
/// 2. `[]` Stake pool withdraw authority
/// 3. `[w]` Validator list
/// 4. `[w]` Stake pool reserve stake
/// 5. `[w]` Transient stake account
/// 6. `[]` Validator stake account
/// 7. `[]` Validator vote account to delegate to
/// 8. '[]' Clock sysvar
/// 9. '[]' Rent sysvar
/// 10. `[]` Stake History sysvar
/// 11. `[]` Stake Config sysvar
/// 12. `[]` System program
/// 13. `[]` Stake program
/// userdata: amount of lamports to increase on the given validator.
/// The actual amount split into the transient stake account is:
/// `lamports + stake_rent_exemption`
/// The rent-exemption of the stake account is withdrawn back to the reserve
/// after it is merged.
IncreaseValidatorStake {
/// amount of lamports to increase on the given validator
lamports: u64,
/// seed used to create transient stake account
transient_stake_seed: u64,
},
/// (Staker only) Set the preferred deposit or withdraw stake account for the
/// stake pool
///
/// In order to avoid users abusing the stake pool as a free conversion
/// between SOL staked on different validators, the staker can force all
/// deposits and/or withdraws to go to one chosen account, or unset that account.
///
/// 0. `[w]` Stake pool
/// 1. `[s]` Stake pool staker
/// 2. `[]` Validator list
///
/// Fails if the validator is not part of the stake pool.
SetPreferredValidator {
/// Affected operation (deposit or withdraw)
validator_type: PreferredValidatorType,
/// Validator vote account that deposits or withdraws must go through,
/// unset with None
validator_vote_address: Option<Pubkey>,
},
/// Updates balances of validator and transient stake accounts in the pool
///
/// While going through the pairs of validator and transient stake accounts,
/// if the transient stake is inactive, it is merged into the reserve stake
/// account. If the transient stake is active and has matching credits
/// observed, it is merged into the canonical validator stake account. In
/// all other states, nothing is done, and the balance is simply added to
/// the canonical stake account balance.
///
/// 0. `[]` Stake pool
/// 1. `[]` Stake pool withdraw authority
/// 2. `[w]` Validator stake list storage account
/// 3. `[w]` Reserve stake account
/// 4. `[]` Sysvar clock
/// 5. `[]` Sysvar stake history
/// 6. `[]` Stake program
/// 7. ..7+2N ` [] N pairs of validator and transient stake accounts
UpdateValidatorListBalance {
/// Index to start updating on the validator list
start_index: u32,
/// If true, don't try merging transient stake accounts into the reserve or
/// validator stake account. Useful for testing or if a particular stake
/// account is in a bad state, but we still want to update
no_merge: bool,
},
/// Updates total pool balance based on balances in the reserve and validator list
///
/// 0. `[w]` Stake pool
/// 1. `[]` Stake pool withdraw authority
/// 2. `[w]` Validator stake list storage account
/// 3. `[]` Reserve stake account
/// 4. `[w]` Account to receive pool fee tokens
/// 5. `[w]` Pool mint account
/// 6. `[]` Pool token program
UpdateStakePoolBalance,
/// Cleans up validator stake account entries marked as `ReadyForRemoval`
///
/// 0. `[]` Stake pool
/// 1. `[w]` Validator stake list storage account
CleanupRemovedValidatorEntries,
/// Deposit some stake into the pool. The output is a "pool" token representing ownership
/// into the pool. Inputs are converted to the current ratio.
///
/// 0. `[w]` Stake pool
/// 1. `[w]` Validator stake list storage account
/// 2. `[s]/[]` Stake pool deposit authority
/// 3. `[]` Stake pool withdraw authority
/// 4. `[w]` Stake account to join the pool (withdraw authority for the stake account should be first set to the stake pool deposit authority)
/// 5. `[w]` Validator stake account for the stake account to be merged with
/// 6. `[w]` Reserve stake account, to withdraw rent exempt reserve
/// 7. `[w]` User account to receive pool tokens
/// 8. `[w]` Account to receive pool fee tokens
/// 9. `[w]` Account to receive a portion of pool fee tokens as referral fees
/// 10. `[w]` Pool token mint account
/// 11. '[]' Sysvar clock account
/// 12. '[]' Sysvar stake history account
/// 13. `[]` Pool token program id,
/// 14. `[]` Stake program id,
DepositStake,
/// Withdraw the token from the pool at the current ratio.
///
/// Succeeds if the stake account has enough SOL to cover the desired amount
/// of pool tokens, and if the withdrawal keeps the total staked amount
/// above the minimum of rent-exempt amount +
/// `max(crate::MINIMUM_ACTIVE_STAKE, solana_program::stake::tools::get_minimum_delegation())`.
///
/// When allowing withdrawals, the order of priority goes:
///
/// * preferred withdraw validator stake account (if set)
/// * validator stake accounts
/// * transient stake accounts
/// * reserve stake account OR totally remove validator stake accounts
///
/// A user can freely withdraw from a validator stake account, and if they
/// are all at the minimum, then they can withdraw from transient stake
/// accounts, and if they are all at minimum, then they can withdraw from
/// the reserve or remove any validator from the pool.
///
/// 0. `[w]` Stake pool
/// 1. `[w]` Validator stake list storage account
/// 2. `[]` Stake pool withdraw authority
/// 3. `[w]` Validator or reserve stake account to split
/// 4. `[w]` Unitialized stake account to receive withdrawal
/// 5. `[]` User account to set as a new withdraw authority
/// 6. `[s]` User transfer authority, for pool token account
/// 7. `[w]` User account with pool tokens to burn from
/// 8. `[w]` Account to receive pool fee tokens
/// 9. `[w]` Pool token mint account
/// 10. `[]` Sysvar clock account (required)
/// 11. `[]` Pool token program id
/// 12. `[]` Stake program id,
/// userdata: amount of pool tokens to withdraw
WithdrawStake(u64),
/// (Manager only) Update manager
///
/// 0. `[w]` StakePool
/// 1. `[s]` Manager
/// 2. `[s]` New manager
/// 3. `[]` New manager fee account
SetManager,
/// (Manager only) Update fee
///
/// 0. `[w]` StakePool
/// 1. `[s]` Manager
SetFee {
/// Type of fee to update and value to update it to
fee: FeeType,
},
/// (Manager or staker only) Update staker
///
/// 0. `[w]` StakePool
/// 1. `[s]` Manager or current staker
/// 2. '[]` New staker pubkey
SetStaker,
/// Deposit SOL directly into the pool's reserve account. The output is a "pool" token
/// representing ownership into the pool. Inputs are converted to the current ratio.
///
/// 0. `[w]` Stake pool
/// 1. `[]` Stake pool withdraw authority
/// 2. `[w]` Reserve stake account, to deposit SOL
/// 3. `[s]` Account providing the lamports to be deposited into the pool
/// 4. `[w]` User account to receive pool tokens
/// 5. `[w]` Account to receive fee tokens
/// 6. `[w]` Account to receive a portion of fee as referral fees
/// 7. `[w]` Pool token mint account
/// 8. `[]` System program account
/// 9. `[]` Token program id
/// 10. `[s]` (Optional) Stake pool sol deposit authority.
DepositSol(u64),
/// (Manager only) Update SOL deposit, stake deposit, or SOL withdrawal authority.
///
/// 0. `[w]` StakePool
/// 1. `[s]` Manager
/// 2. '[]` New authority pubkey or none
SetFundingAuthority(FundingType),
/// Withdraw SOL directly from the pool's reserve account. Fails if the
/// reserve does not have enough SOL.
///
/// 0. `[w]` Stake pool
/// 1. `[]` Stake pool withdraw authority
/// 2. `[s]` User transfer authority, for pool token account
/// 3. `[w]` User account to burn pool tokens
/// 4. `[w]` Reserve stake account, to withdraw SOL
/// 5. `[w]` Account receiving the lamports from the reserve, must be a system account
/// 6. `[w]` Account to receive pool fee tokens
/// 7. `[w]` Pool token mint account
/// 8. '[]' Clock sysvar
/// 9. '[]' Stake history sysvar
/// 10. `[]` Stake program account
/// 11. `[]` Token program id
/// 12. `[s]` (Optional) Stake pool sol withdraw authority
WithdrawSol(u64),
/// Create token metadata for the stake-pool token in the
/// metaplex-token program
/// 0. `[]` Stake pool
/// 1. `[s]` Manager
/// 2. `[]` Stake pool withdraw authority
/// 3. `[]` Pool token mint account
/// 4. `[s, w]` Payer for creation of token metadata account
/// 5. `[w]` Token metadata account
/// 6. `[]` Metadata program id
/// 7. `[]` System program id
/// 8. `[]` Rent sysvar
CreateTokenMetadata {
/// Token name
name: String,
/// Token symbol e.g. stkSOL
symbol: String,
/// URI of the uploaded metadata of the spl-token
uri: String,
},
/// Update token metadata for the stake-pool token in the
/// metaplex-token program
///
/// 0. `[]` Stake pool
/// 1. `[s]` Manager
/// 2. `[]` Stake pool withdraw authority
/// 3. `[w]` Token metadata account
/// 4. `[]` Metadata program id
UpdateTokenMetadata {
/// Token name
name: String,
/// Token symbol e.g. stkSOL
symbol: String,
/// URI of the uploaded metadata of the spl-token
uri: String,
},
/// (Staker only) Decrease active stake again from a validator, eventually moving it to the reserve
///
/// Works regardless if the transient stake account already exists.
///
/// Internally, this instruction splits a validator stake account into an
/// ephemeral stake account, deactivates it, then merges or splits it into
/// the transient stake account delegated to the appropriate validator.
///
/// The amount of lamports to move must be at least rent-exemption plus
/// `max(crate::MINIMUM_ACTIVE_STAKE, solana_program::stake::tools::get_minimum_delegation())`.
///
/// 0. `[]` Stake pool
/// 1. `[s]` Stake pool staker
/// 2. `[]` Stake pool withdraw authority
/// 3. `[w]` Validator list
/// 4. `[w]` Canonical stake account to split from
/// 5. `[w]` Uninitialized ephemeral stake account to receive stake
/// 6. `[w]` Transient stake account
/// 7. `[]` Clock sysvar
/// 8. '[]' Stake history sysvar
/// 9. `[]` System program
/// 10. `[]` Stake program
DecreaseAdditionalValidatorStake {
/// amount of lamports to split into the transient stake account
lamports: u64,
/// seed used to create transient stake account
transient_stake_seed: u64,
/// seed used to create ephemeral account.
ephemeral_stake_seed: u64,
},
}
/// Creates an 'initialize' instruction.
pub fn initialize(
program_id: &Pubkey,
stake_pool: &Pubkey,
manager: &Pubkey,
staker: &Pubkey,
stake_pool_withdraw_authority: &Pubkey,
validator_list: &Pubkey,
reserve_stake: &Pubkey,
pool_mint: &Pubkey,
manager_pool_account: &Pubkey,
token_program_id: &Pubkey,
deposit_authority: Option<Pubkey>,
fee: Fee,
withdrawal_fee: Fee,
deposit_fee: Fee,
referral_fee: u8,
max_validators: u32,
) -> Instruction {
let init_data = StakePoolInstruction::Initialize {
fee,
withdrawal_fee,
deposit_fee,
referral_fee,
max_validators,
};
let data = init_data.try_to_vec().unwrap();
let mut accounts = vec![
AccountMeta::new(*stake_pool, false),
AccountMeta::new_readonly(*manager, true),
AccountMeta::new_readonly(*staker, false),
AccountMeta::new_readonly(*stake_pool_withdraw_authority, false),
AccountMeta::new(*validator_list, false),
AccountMeta::new_readonly(*reserve_stake, false),
AccountMeta::new(*pool_mint, false),
AccountMeta::new(*manager_pool_account, false),
AccountMeta::new_readonly(*token_program_id, false),
];
if let Some(deposit_authority) = deposit_authority {
accounts.push(AccountMeta::new_readonly(deposit_authority, true));
}
Instruction {
program_id: *program_id,
accounts,
data,
}
}
/// Creates `AddValidatorToPool` instruction (add new validator stake account to the pool)
pub fn add_validator_to_pool(
program_id: &Pubkey,
stake_pool: &Pubkey,
staker: &Pubkey,
reserve: &Pubkey,
stake_pool_withdraw: &Pubkey,
validator_list: &Pubkey,
stake: &Pubkey,
validator: &Pubkey,
seed: Option<NonZeroU32>,
) -> Instruction {
let accounts = vec![
AccountMeta::new(*stake_pool, false),
AccountMeta::new_readonly(*staker, true),
AccountMeta::new(*reserve, false),
AccountMeta::new_readonly(*stake_pool_withdraw, false),
AccountMeta::new(*validator_list, false),
AccountMeta::new(*stake, false),
AccountMeta::new_readonly(*validator, false),
AccountMeta::new_readonly(sysvar::rent::id(), false),
AccountMeta::new_readonly(sysvar::clock::id(), false),
AccountMeta::new_readonly(sysvar::stake_history::id(), false),
AccountMeta::new_readonly(stake::config::id(), false),
AccountMeta::new_readonly(system_program::id(), false),
AccountMeta::new_readonly(stake::program::id(), false),
];
let data = StakePoolInstruction::AddValidatorToPool(seed.map(|s| s.get()).unwrap_or(0))
.try_to_vec()
.unwrap();
Instruction {
program_id: *program_id,
accounts,
data,
}
}
/// Creates `RemoveValidatorFromPool` instruction (remove validator stake account from the pool)
pub fn remove_validator_from_pool(
program_id: &Pubkey,
stake_pool: &Pubkey,
staker: &Pubkey,
stake_pool_withdraw: &Pubkey,
validator_list: &Pubkey,
stake_account: &Pubkey,
transient_stake_account: &Pubkey,
) -> Instruction {
let accounts = vec![
AccountMeta::new(*stake_pool, false),
AccountMeta::new_readonly(*staker, true),
AccountMeta::new_readonly(*stake_pool_withdraw, false),
AccountMeta::new(*validator_list, false),
AccountMeta::new(*stake_account, false),
AccountMeta::new_readonly(*transient_stake_account, false),
AccountMeta::new_readonly(sysvar::clock::id(), false),
AccountMeta::new_readonly(stake::program::id(), false),
];
Instruction {
program_id: *program_id,
accounts,
data: StakePoolInstruction::RemoveValidatorFromPool
.try_to_vec()
.unwrap(),
}
}
/// Creates `DecreaseValidatorStake` instruction (rebalance from validator account to
/// transient account)
pub fn decrease_validator_stake(
program_id: &Pubkey,
stake_pool: &Pubkey,
staker: &Pubkey,
stake_pool_withdraw_authority: &Pubkey,
validator_list: &Pubkey,
validator_stake: &Pubkey,
transient_stake: &Pubkey,
lamports: u64,
transient_stake_seed: u64,
) -> Instruction {
let accounts = vec![
AccountMeta::new_readonly(*stake_pool, false),
AccountMeta::new_readonly(*staker, true),
AccountMeta::new_readonly(*stake_pool_withdraw_authority, false),
AccountMeta::new(*validator_list, false),
AccountMeta::new(*validator_stake, false),
AccountMeta::new(*transient_stake, false),
AccountMeta::new_readonly(sysvar::clock::id(), false),
AccountMeta::new_readonly(sysvar::rent::id(), false),
AccountMeta::new_readonly(system_program::id(), false),
AccountMeta::new_readonly(stake::program::id(), false),
];
Instruction {
program_id: *program_id,
accounts,
data: StakePoolInstruction::DecreaseValidatorStake {
lamports,
transient_stake_seed,
}
.try_to_vec()
.unwrap(),
}
}
/// Creates `DecreaseAdditionalValidatorStake` instruction (rebalance from
/// validator account to transient account)
pub fn decrease_additional_validator_stake(
program_id: &Pubkey,
stake_pool: &Pubkey,
staker: &Pubkey,
stake_pool_withdraw_authority: &Pubkey,
validator_list: &Pubkey,
validator_stake: &Pubkey,
ephemeral_stake: &Pubkey,
transient_stake: &Pubkey,
lamports: u64,
transient_stake_seed: u64,
ephemeral_stake_seed: u64,
) -> Instruction {
let accounts = vec![
AccountMeta::new_readonly(*stake_pool, false),
AccountMeta::new_readonly(*staker, true),
AccountMeta::new_readonly(*stake_pool_withdraw_authority, false),
AccountMeta::new(*validator_list, false),
AccountMeta::new(*validator_stake, false),
AccountMeta::new(*ephemeral_stake, false),
AccountMeta::new(*transient_stake, false),
AccountMeta::new_readonly(sysvar::clock::id(), false),
AccountMeta::new_readonly(sysvar::stake_history::id(), false),
AccountMeta::new_readonly(system_program::id(), false),
AccountMeta::new_readonly(stake::program::id(), false),
];
Instruction {
program_id: *program_id,
accounts,
data: StakePoolInstruction::DecreaseAdditionalValidatorStake {
lamports,
transient_stake_seed,
ephemeral_stake_seed,
}
.try_to_vec()
.unwrap(),
}
}
/// Creates `IncreaseValidatorStake` instruction (rebalance from reserve account to
/// transient account)
pub fn increase_validator_stake(
program_id: &Pubkey,
stake_pool: &Pubkey,
staker: &Pubkey,
stake_pool_withdraw_authority: &Pubkey,
validator_list: &Pubkey,
reserve_stake: &Pubkey,
transient_stake: &Pubkey,
validator_stake: &Pubkey,
validator: &Pubkey,
lamports: u64,
transient_stake_seed: u64,
) -> Instruction {
let accounts = vec![
AccountMeta::new_readonly(*stake_pool, false),
AccountMeta::new_readonly(*staker, true),
AccountMeta::new_readonly(*stake_pool_withdraw_authority, false),
AccountMeta::new(*validator_list, false),
AccountMeta::new(*reserve_stake, false),
AccountMeta::new(*transient_stake, false),
AccountMeta::new_readonly(*validator_stake, false),
AccountMeta::new_readonly(*validator, false),
AccountMeta::new_readonly(sysvar::clock::id(), false),
AccountMeta::new_readonly(sysvar::rent::id(), false),
AccountMeta::new_readonly(sysvar::stake_history::id(), false),
AccountMeta::new_readonly(stake::config::id(), false),
AccountMeta::new_readonly(system_program::id(), false),
AccountMeta::new_readonly(stake::program::id(), false),
];
Instruction {
program_id: *program_id,
accounts,
data: StakePoolInstruction::IncreaseValidatorStake {
lamports,
transient_stake_seed,
}
.try_to_vec()
.unwrap(),
}
}
/// Creates `SetPreferredDepositValidator` instruction
pub fn set_preferred_validator(
program_id: &Pubkey,
stake_pool_address: &Pubkey,
staker: &Pubkey,
validator_list_address: &Pubkey,
validator_type: PreferredValidatorType,
validator_vote_address: Option<Pubkey>,
) -> Instruction {
Instruction {
program_id: *program_id,
accounts: vec![
AccountMeta::new(*stake_pool_address, false),
AccountMeta::new_readonly(*staker, true),
AccountMeta::new_readonly(*validator_list_address, false),
],
data: StakePoolInstruction::SetPreferredValidator {
validator_type,
validator_vote_address,
}
.try_to_vec()
.unwrap(),
}
}
/// Create an `AddValidatorToPool` instruction given an existing stake pool and
/// vote account
pub fn add_validator_to_pool_with_vote(
program_id: &Pubkey,
stake_pool: &StakePool,
stake_pool_address: &Pubkey,
vote_account_address: &Pubkey,
seed: Option<NonZeroU32>,
) -> Instruction {
let pool_withdraw_authority =
find_withdraw_authority_program_address(program_id, stake_pool_address).0;
let (stake_account_address, _) =
find_stake_program_address(program_id, vote_account_address, stake_pool_address, seed);
add_validator_to_pool(
program_id,
stake_pool_address,
&stake_pool.staker,
&stake_pool.reserve_stake,
&pool_withdraw_authority,
&stake_pool.validator_list,
&stake_account_address,
vote_account_address,
seed,
)
}
/// Create an `RemoveValidatorFromPool` instruction given an existing stake pool and
/// vote account
pub fn remove_validator_from_pool_with_vote(
program_id: &Pubkey,
stake_pool: &StakePool,
stake_pool_address: &Pubkey,
vote_account_address: &Pubkey,
validator_stake_seed: Option<NonZeroU32>,
transient_stake_seed: u64,
) -> Instruction {
let pool_withdraw_authority =
find_withdraw_authority_program_address(program_id, stake_pool_address).0;
let (stake_account_address, _) = find_stake_program_address(
program_id,
vote_account_address,
stake_pool_address,
validator_stake_seed,
);
let (transient_stake_account, _) = find_transient_stake_program_address(
program_id,
vote_account_address,
stake_pool_address,
transient_stake_seed,
);
remove_validator_from_pool(
program_id,
stake_pool_address,
&stake_pool.staker,
&pool_withdraw_authority,
&stake_pool.validator_list,
&stake_account_address,
&transient_stake_account,
)
}
/// Create an `IncreaseValidatorStake` instruction given an existing stake pool and
/// vote account
pub fn increase_validator_stake_with_vote(
program_id: &Pubkey,
stake_pool: &StakePool,
stake_pool_address: &Pubkey,
vote_account_address: &Pubkey,
lamports: u64,
validator_stake_seed: Option<NonZeroU32>,
transient_stake_seed: u64,
) -> Instruction {
let pool_withdraw_authority =
find_withdraw_authority_program_address(program_id, stake_pool_address).0;
let (transient_stake_address, _) = find_transient_stake_program_address(
program_id,
vote_account_address,
stake_pool_address,
transient_stake_seed,
);
let (validator_stake_address, _) = find_stake_program_address(
program_id,
vote_account_address,
stake_pool_address,
validator_stake_seed,
);
increase_validator_stake(
program_id,
stake_pool_address,
&stake_pool.staker,
&pool_withdraw_authority,
&stake_pool.validator_list,
&stake_pool.reserve_stake,
&transient_stake_address,
&validator_stake_address,
vote_account_address,
lamports,
transient_stake_seed,
)
}
/// Create a `DecreaseValidatorStake` instruction given an existing stake pool and
/// vote account
pub fn decrease_validator_stake_with_vote(
program_id: &Pubkey,
stake_pool: &StakePool,
stake_pool_address: &Pubkey,
vote_account_address: &Pubkey,
lamports: u64,
validator_stake_seed: Option<NonZeroU32>,
transient_stake_seed: u64,
) -> Instruction {
let pool_withdraw_authority =
find_withdraw_authority_program_address(program_id, stake_pool_address).0;
let (validator_stake_address, _) = find_stake_program_address(
program_id,
vote_account_address,
stake_pool_address,
validator_stake_seed,
);
let (transient_stake_address, _) = find_transient_stake_program_address(
program_id,
vote_account_address,
stake_pool_address,
transient_stake_seed,
);
decrease_validator_stake(
program_id,
stake_pool_address,
&stake_pool.staker,
&pool_withdraw_authority,
&stake_pool.validator_list,
&validator_stake_address,
&transient_stake_address,
lamports,
transient_stake_seed,
)
}
/// Create a `DecreaseAdditionalValidatorStake` instruction given an existing
/// stake pool and vote account
pub fn decrease_additional_validator_stake_with_vote(
program_id: &Pubkey,
stake_pool: &StakePool,
stake_pool_address: &Pubkey,
vote_account_address: &Pubkey,
lamports: u64,
validator_stake_seed: Option<NonZeroU32>,
transient_stake_seed: u64,
ephemeral_stake_seed: u64,
) -> Instruction {
let pool_withdraw_authority =
find_withdraw_authority_program_address(program_id, stake_pool_address).0;
let (validator_stake_address, _) = find_stake_program_address(
program_id,
vote_account_address,
stake_pool_address,
validator_stake_seed,
);
let (ephemeral_stake_address, _) =
find_ephemeral_stake_program_address(program_id, stake_pool_address, ephemeral_stake_seed);
let (transient_stake_address, _) = find_transient_stake_program_address(
program_id,
vote_account_address,
stake_pool_address,
transient_stake_seed,
);
decrease_additional_validator_stake(
program_id,
stake_pool_address,
&stake_pool.staker,
&pool_withdraw_authority,
&stake_pool.validator_list,
&validator_stake_address,
&ephemeral_stake_address,
&transient_stake_address,
lamports,
transient_stake_seed,
ephemeral_stake_seed,
)
}
/// Creates `UpdateValidatorListBalance` instruction (update validator stake account balances)
pub fn update_validator_list_balance(
program_id: &Pubkey,
stake_pool: &Pubkey,
stake_pool_withdraw_authority: &Pubkey,
validator_list_address: &Pubkey,
reserve_stake: &Pubkey,
validator_list: &ValidatorList,
validator_vote_accounts: &[Pubkey],
start_index: u32,
no_merge: bool,
) -> Instruction {
let mut accounts = vec![
AccountMeta::new_readonly(*stake_pool, false),
AccountMeta::new_readonly(*stake_pool_withdraw_authority, false),
AccountMeta::new(*validator_list_address, false),
AccountMeta::new(*reserve_stake, false),
AccountMeta::new_readonly(sysvar::clock::id(), false),
AccountMeta::new_readonly(sysvar::stake_history::id(), false),
AccountMeta::new_readonly(stake::program::id(), false),
];
accounts.append(
&mut validator_vote_accounts
.iter()
.flat_map(|vote_account_address| {
let validator_stake_info = validator_list.find(vote_account_address);
if let Some(validator_stake_info) = validator_stake_info {
let (validator_stake_account, _) = find_stake_program_address(
program_id,
vote_account_address,
stake_pool,
NonZeroU32::new(validator_stake_info.validator_seed_suffix),
);
let (transient_stake_account, _) = find_transient_stake_program_address(
program_id,
vote_account_address,
stake_pool,
validator_stake_info.transient_seed_suffix,
);
vec![
AccountMeta::new(validator_stake_account, false),
AccountMeta::new(transient_stake_account, false),
]
} else {
vec![]
}
})
.collect::<Vec<AccountMeta>>(),
);
Instruction {
program_id: *program_id,
accounts,
data: StakePoolInstruction::UpdateValidatorListBalance {
start_index,
no_merge,
}
.try_to_vec()
.unwrap(),
}
}
/// Creates `UpdateStakePoolBalance` instruction (pool balance from the stake account list balances)
pub fn update_stake_pool_balance(
program_id: &Pubkey,
stake_pool: &Pubkey,
withdraw_authority: &Pubkey,
validator_list_storage: &Pubkey,
reserve_stake: &Pubkey,
manager_fee_account: &Pubkey,
stake_pool_mint: &Pubkey,
token_program_id: &Pubkey,
) -> Instruction {
let accounts = vec![
AccountMeta::new(*stake_pool, false),
AccountMeta::new_readonly(*withdraw_authority, false),
AccountMeta::new(*validator_list_storage, false),
AccountMeta::new_readonly(*reserve_stake, false),
AccountMeta::new(*manager_fee_account, false),
AccountMeta::new(*stake_pool_mint, false),
AccountMeta::new_readonly(*token_program_id, false),
];
Instruction {
program_id: *program_id,
accounts,
data: StakePoolInstruction::UpdateStakePoolBalance
.try_to_vec()
.unwrap(),
}
}
/// Creates `CleanupRemovedValidatorEntries` instruction (removes entries from the validator list)
pub fn cleanup_removed_validator_entries(
program_id: &Pubkey,
stake_pool: &Pubkey,
validator_list_storage: &Pubkey,
) -> Instruction {
let accounts = vec![
AccountMeta::new_readonly(*stake_pool, false),
AccountMeta::new(*validator_list_storage, false),
];
Instruction {
program_id: *program_id,
accounts,
data: StakePoolInstruction::CleanupRemovedValidatorEntries
.try_to_vec()
.unwrap(),
}
}
/// Creates all `UpdateValidatorListBalance` and `UpdateStakePoolBalance`
/// instructions for fully updating a stake pool each epoch
pub fn update_stake_pool(
program_id: &Pubkey,
stake_pool: &StakePool,
validator_list: &ValidatorList,
stake_pool_address: &Pubkey,
no_merge: bool,
) -> (Vec<Instruction>, Vec<Instruction>) {
let vote_accounts: Vec<Pubkey> = validator_list
.validators