solana-web3-stable 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +22 -0
  3. package/lib/index.browser.cjs.js +10564 -0
  4. package/lib/index.browser.cjs.js.map +1 -0
  5. package/lib/index.browser.esm.js +10463 -0
  6. package/lib/index.browser.esm.js.map +1 -0
  7. package/lib/index.cjs.js +11351 -0
  8. package/lib/index.cjs.js.map +1 -0
  9. package/lib/index.d.ts +4025 -0
  10. package/lib/index.esm.js +11246 -0
  11. package/lib/index.esm.js.map +1 -0
  12. package/lib/index.iife.js +26085 -0
  13. package/lib/index.iife.js.map +1 -0
  14. package/lib/index.iife.min.js +20 -0
  15. package/lib/index.iife.min.js.map +1 -0
  16. package/lib/index.native.js +10564 -0
  17. package/lib/index.native.js.map +1 -0
  18. package/package.json +87 -0
  19. package/src/__forks__/browser/fetch-impl.ts +4 -0
  20. package/src/__forks__/react-native/fetch-impl.ts +4 -0
  21. package/src/account-data.ts +39 -0
  22. package/src/account.ts +55 -0
  23. package/src/blockhash.ts +4 -0
  24. package/src/bpf-loader-deprecated.ts +5 -0
  25. package/src/bpf-loader.ts +50 -0
  26. package/src/connection.ts +6961 -0
  27. package/src/epoch-schedule.ts +102 -0
  28. package/src/errors.ts +133 -0
  29. package/src/fee-calculator.ts +18 -0
  30. package/src/fetch-impl.ts +16 -0
  31. package/src/index.ts +24 -0
  32. package/src/instruction.ts +58 -0
  33. package/src/keypair.ts +102 -0
  34. package/src/layout.ts +188 -0
  35. package/src/loader.ts +267 -0
  36. package/src/message/account-keys.ts +79 -0
  37. package/src/message/compiled-keys.ts +165 -0
  38. package/src/message/index.ts +47 -0
  39. package/src/message/legacy.ts +323 -0
  40. package/src/message/v0.ts +513 -0
  41. package/src/message/versioned.ts +36 -0
  42. package/src/nonce-account.ts +82 -0
  43. package/src/programs/address-lookup-table/index.ts +438 -0
  44. package/src/programs/address-lookup-table/state.ts +84 -0
  45. package/src/programs/compute-budget.ts +281 -0
  46. package/src/programs/ed25519.ts +157 -0
  47. package/src/programs/index.ts +7 -0
  48. package/src/programs/secp256k1.ts +228 -0
  49. package/src/programs/stake.ts +952 -0
  50. package/src/programs/system.ts +1048 -0
  51. package/src/programs/vote.ts +586 -0
  52. package/src/publickey.ts +259 -0
  53. package/src/rpc-websocket.ts +75 -0
  54. package/src/sysvar.ts +37 -0
  55. package/src/timing.ts +23 -0
  56. package/src/transaction/constants.ts +12 -0
  57. package/src/transaction/expiry-custom-errors.ts +48 -0
  58. package/src/transaction/index.ts +5 -0
  59. package/src/transaction/legacy.ts +970 -0
  60. package/src/transaction/message.ts +140 -0
  61. package/src/transaction/versioned.ts +127 -0
  62. package/src/utils/assert.ts +8 -0
  63. package/src/utils/bigint.ts +24 -0
  64. package/src/utils/borsh-schema.ts +38 -0
  65. package/src/utils/cluster.ts +35 -0
  66. package/src/utils/ed25519.ts +43 -0
  67. package/src/utils/guarded-array-utils.ts +34 -0
  68. package/src/utils/index.ts +5 -0
  69. package/src/utils/makeWebsocketUrl.ts +26 -0
  70. package/src/utils/promise-timeout.ts +14 -0
  71. package/src/utils/secp256k1.ts +11 -0
  72. package/src/utils/send-and-confirm-raw-transaction.ts +110 -0
  73. package/src/utils/send-and-confirm-transaction.ts +106 -0
  74. package/src/utils/shortvec-encoding.ts +28 -0
  75. package/src/utils/sleep.ts +4 -0
  76. package/src/utils/to-buffer.ts +11 -0
  77. package/src/validator-info.ts +108 -0
  78. package/src/vote-account.ts +236 -0
@@ -0,0 +1,586 @@
1
+ import * as BufferLayout from '@solana/buffer-layout';
2
+
3
+ import {
4
+ encodeData,
5
+ decodeData,
6
+ InstructionType,
7
+ IInstructionInputData,
8
+ } from '../instruction';
9
+ import * as Layout from '../layout';
10
+ import {PublicKey} from '../publickey';
11
+ import {SystemProgram} from './system';
12
+ import {SYSVAR_CLOCK_PUBKEY, SYSVAR_RENT_PUBKEY} from '../sysvar';
13
+ import {Transaction, TransactionInstruction} from '../transaction';
14
+ import {toBuffer} from '../utils/to-buffer';
15
+
16
+ /**
17
+ * Vote account info
18
+ */
19
+ export class VoteInit {
20
+ nodePubkey: PublicKey;
21
+ authorizedVoter: PublicKey;
22
+ authorizedWithdrawer: PublicKey;
23
+ commission: number; /** [0, 100] */
24
+
25
+ constructor(
26
+ nodePubkey: PublicKey,
27
+ authorizedVoter: PublicKey,
28
+ authorizedWithdrawer: PublicKey,
29
+ commission: number,
30
+ ) {
31
+ this.nodePubkey = nodePubkey;
32
+ this.authorizedVoter = authorizedVoter;
33
+ this.authorizedWithdrawer = authorizedWithdrawer;
34
+ this.commission = commission;
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Create vote account transaction params
40
+ */
41
+ export type CreateVoteAccountParams = {
42
+ fromPubkey: PublicKey;
43
+ votePubkey: PublicKey;
44
+ voteInit: VoteInit;
45
+ lamports: number;
46
+ };
47
+
48
+ /**
49
+ * InitializeAccount instruction params
50
+ */
51
+ export type InitializeAccountParams = {
52
+ votePubkey: PublicKey;
53
+ nodePubkey: PublicKey;
54
+ voteInit: VoteInit;
55
+ };
56
+
57
+ /**
58
+ * Authorize instruction params
59
+ */
60
+ export type AuthorizeVoteParams = {
61
+ votePubkey: PublicKey;
62
+ /** Current vote or withdraw authority, depending on `voteAuthorizationType` */
63
+ authorizedPubkey: PublicKey;
64
+ newAuthorizedPubkey: PublicKey;
65
+ voteAuthorizationType: VoteAuthorizationType;
66
+ };
67
+
68
+ /**
69
+ * AuthorizeWithSeed instruction params
70
+ */
71
+ export type AuthorizeVoteWithSeedParams = {
72
+ currentAuthorityDerivedKeyBasePubkey: PublicKey;
73
+ currentAuthorityDerivedKeyOwnerPubkey: PublicKey;
74
+ currentAuthorityDerivedKeySeed: string;
75
+ newAuthorizedPubkey: PublicKey;
76
+ voteAuthorizationType: VoteAuthorizationType;
77
+ votePubkey: PublicKey;
78
+ };
79
+
80
+ /**
81
+ * Withdraw from vote account transaction params
82
+ */
83
+ export type WithdrawFromVoteAccountParams = {
84
+ votePubkey: PublicKey;
85
+ authorizedWithdrawerPubkey: PublicKey;
86
+ lamports: number;
87
+ toPubkey: PublicKey;
88
+ };
89
+
90
+ /**
91
+ * Update validator identity (node pubkey) vote account instruction params.
92
+ */
93
+ export type UpdateValidatorIdentityParams = {
94
+ votePubkey: PublicKey;
95
+ authorizedWithdrawerPubkey: PublicKey;
96
+ nodePubkey: PublicKey;
97
+ };
98
+
99
+ /**
100
+ * Vote Instruction class
101
+ */
102
+ export class VoteInstruction {
103
+ /**
104
+ * @internal
105
+ */
106
+ constructor() {}
107
+
108
+ /**
109
+ * Decode a vote instruction and retrieve the instruction type.
110
+ */
111
+ static decodeInstructionType(
112
+ instruction: TransactionInstruction,
113
+ ): VoteInstructionType {
114
+ this.checkProgramId(instruction.programId);
115
+
116
+ const instructionTypeLayout = BufferLayout.u32('instruction');
117
+ const typeIndex = instructionTypeLayout.decode(instruction.data);
118
+
119
+ let type: VoteInstructionType | undefined;
120
+ for (const [ixType, layout] of Object.entries(VOTE_INSTRUCTION_LAYOUTS)) {
121
+ if (layout.index == typeIndex) {
122
+ type = ixType as VoteInstructionType;
123
+ break;
124
+ }
125
+ }
126
+
127
+ if (!type) {
128
+ throw new Error('Instruction type incorrect; not a VoteInstruction');
129
+ }
130
+
131
+ return type;
132
+ }
133
+
134
+ /**
135
+ * Decode an initialize vote instruction and retrieve the instruction params.
136
+ */
137
+ static decodeInitializeAccount(
138
+ instruction: TransactionInstruction,
139
+ ): InitializeAccountParams {
140
+ this.checkProgramId(instruction.programId);
141
+ this.checkKeyLength(instruction.keys, 4);
142
+
143
+ const {voteInit} = decodeData(
144
+ VOTE_INSTRUCTION_LAYOUTS.InitializeAccount,
145
+ instruction.data,
146
+ );
147
+
148
+ return {
149
+ votePubkey: instruction.keys[0].pubkey,
150
+ nodePubkey: instruction.keys[3].pubkey,
151
+ voteInit: new VoteInit(
152
+ new PublicKey(voteInit.nodePubkey),
153
+ new PublicKey(voteInit.authorizedVoter),
154
+ new PublicKey(voteInit.authorizedWithdrawer),
155
+ voteInit.commission,
156
+ ),
157
+ };
158
+ }
159
+
160
+ /**
161
+ * Decode an authorize instruction and retrieve the instruction params.
162
+ */
163
+ static decodeAuthorize(
164
+ instruction: TransactionInstruction,
165
+ ): AuthorizeVoteParams {
166
+ this.checkProgramId(instruction.programId);
167
+ this.checkKeyLength(instruction.keys, 3);
168
+
169
+ const {newAuthorized, voteAuthorizationType} = decodeData(
170
+ VOTE_INSTRUCTION_LAYOUTS.Authorize,
171
+ instruction.data,
172
+ );
173
+
174
+ return {
175
+ votePubkey: instruction.keys[0].pubkey,
176
+ authorizedPubkey: instruction.keys[2].pubkey,
177
+ newAuthorizedPubkey: new PublicKey(newAuthorized),
178
+ voteAuthorizationType: {
179
+ index: voteAuthorizationType,
180
+ },
181
+ };
182
+ }
183
+
184
+ /**
185
+ * Decode an authorize instruction and retrieve the instruction params.
186
+ */
187
+ static decodeAuthorizeWithSeed(
188
+ instruction: TransactionInstruction,
189
+ ): AuthorizeVoteWithSeedParams {
190
+ this.checkProgramId(instruction.programId);
191
+ this.checkKeyLength(instruction.keys, 3);
192
+
193
+ const {
194
+ voteAuthorizeWithSeedArgs: {
195
+ currentAuthorityDerivedKeyOwnerPubkey,
196
+ currentAuthorityDerivedKeySeed,
197
+ newAuthorized,
198
+ voteAuthorizationType,
199
+ },
200
+ } = decodeData(
201
+ VOTE_INSTRUCTION_LAYOUTS.AuthorizeWithSeed,
202
+ instruction.data,
203
+ );
204
+
205
+ return {
206
+ currentAuthorityDerivedKeyBasePubkey: instruction.keys[2].pubkey,
207
+ currentAuthorityDerivedKeyOwnerPubkey: new PublicKey(
208
+ currentAuthorityDerivedKeyOwnerPubkey,
209
+ ),
210
+ currentAuthorityDerivedKeySeed: currentAuthorityDerivedKeySeed,
211
+ newAuthorizedPubkey: new PublicKey(newAuthorized),
212
+ voteAuthorizationType: {
213
+ index: voteAuthorizationType,
214
+ },
215
+ votePubkey: instruction.keys[0].pubkey,
216
+ };
217
+ }
218
+
219
+ /**
220
+ * Decode a withdraw instruction and retrieve the instruction params.
221
+ */
222
+ static decodeWithdraw(
223
+ instruction: TransactionInstruction,
224
+ ): WithdrawFromVoteAccountParams {
225
+ this.checkProgramId(instruction.programId);
226
+ this.checkKeyLength(instruction.keys, 3);
227
+
228
+ const {lamports} = decodeData(
229
+ VOTE_INSTRUCTION_LAYOUTS.Withdraw,
230
+ instruction.data,
231
+ );
232
+
233
+ return {
234
+ votePubkey: instruction.keys[0].pubkey,
235
+ authorizedWithdrawerPubkey: instruction.keys[2].pubkey,
236
+ lamports,
237
+ toPubkey: instruction.keys[1].pubkey,
238
+ };
239
+ }
240
+
241
+ /**
242
+ * @internal
243
+ */
244
+ static checkProgramId(programId: PublicKey) {
245
+ if (!programId.equals(VoteProgram.programId)) {
246
+ throw new Error('invalid instruction; programId is not VoteProgram');
247
+ }
248
+ }
249
+
250
+ /**
251
+ * @internal
252
+ */
253
+ static checkKeyLength(keys: Array<any>, expectedLength: number) {
254
+ if (keys.length < expectedLength) {
255
+ throw new Error(
256
+ `invalid instruction; found ${keys.length} keys, expected at least ${expectedLength}`,
257
+ );
258
+ }
259
+ }
260
+ }
261
+
262
+ /**
263
+ * An enumeration of valid VoteInstructionType's
264
+ */
265
+ export type VoteInstructionType =
266
+ // FIXME
267
+ // It would be preferable for this type to be `keyof VoteInstructionInputData`
268
+ // but Typedoc does not transpile `keyof` expressions.
269
+ // See https://github.com/TypeStrong/typedoc/issues/1894
270
+ | 'Authorize'
271
+ | 'AuthorizeWithSeed'
272
+ | 'InitializeAccount'
273
+ | 'Withdraw'
274
+ | 'UpdateValidatorIdentity';
275
+
276
+ /** @internal */
277
+ export type VoteAuthorizeWithSeedArgs = Readonly<{
278
+ currentAuthorityDerivedKeyOwnerPubkey: Uint8Array;
279
+ currentAuthorityDerivedKeySeed: string;
280
+ newAuthorized: Uint8Array;
281
+ voteAuthorizationType: number;
282
+ }>;
283
+ type VoteInstructionInputData = {
284
+ Authorize: IInstructionInputData & {
285
+ newAuthorized: Uint8Array;
286
+ voteAuthorizationType: number;
287
+ };
288
+ AuthorizeWithSeed: IInstructionInputData & {
289
+ voteAuthorizeWithSeedArgs: VoteAuthorizeWithSeedArgs;
290
+ };
291
+ InitializeAccount: IInstructionInputData & {
292
+ voteInit: Readonly<{
293
+ authorizedVoter: Uint8Array;
294
+ authorizedWithdrawer: Uint8Array;
295
+ commission: number;
296
+ nodePubkey: Uint8Array;
297
+ }>;
298
+ };
299
+ Withdraw: IInstructionInputData & {
300
+ lamports: number;
301
+ };
302
+ UpdateValidatorIdentity: IInstructionInputData;
303
+ };
304
+
305
+ const VOTE_INSTRUCTION_LAYOUTS = Object.freeze<{
306
+ [Instruction in VoteInstructionType]: InstructionType<
307
+ VoteInstructionInputData[Instruction]
308
+ >;
309
+ }>({
310
+ InitializeAccount: {
311
+ index: 0,
312
+ layout: BufferLayout.struct<VoteInstructionInputData['InitializeAccount']>([
313
+ BufferLayout.u32('instruction'),
314
+ Layout.voteInit(),
315
+ ]),
316
+ },
317
+ Authorize: {
318
+ index: 1,
319
+ layout: BufferLayout.struct<VoteInstructionInputData['Authorize']>([
320
+ BufferLayout.u32('instruction'),
321
+ Layout.publicKey('newAuthorized'),
322
+ BufferLayout.u32('voteAuthorizationType'),
323
+ ]),
324
+ },
325
+ Withdraw: {
326
+ index: 3,
327
+ layout: BufferLayout.struct<VoteInstructionInputData['Withdraw']>([
328
+ BufferLayout.u32('instruction'),
329
+ BufferLayout.ns64('lamports'),
330
+ ]),
331
+ },
332
+ UpdateValidatorIdentity: {
333
+ index: 4,
334
+ layout: BufferLayout.struct<
335
+ VoteInstructionInputData['UpdateValidatorIdentity']
336
+ >([BufferLayout.u32('instruction')]),
337
+ },
338
+ AuthorizeWithSeed: {
339
+ index: 10,
340
+ layout: BufferLayout.struct<VoteInstructionInputData['AuthorizeWithSeed']>([
341
+ BufferLayout.u32('instruction'),
342
+ Layout.voteAuthorizeWithSeedArgs(),
343
+ ]),
344
+ },
345
+ });
346
+
347
+ /**
348
+ * VoteAuthorize type
349
+ */
350
+ export type VoteAuthorizationType = {
351
+ /** The VoteAuthorize index (from solana-vote-program) */
352
+ index: number;
353
+ };
354
+
355
+ /**
356
+ * An enumeration of valid VoteAuthorization layouts.
357
+ */
358
+ export const VoteAuthorizationLayout = Object.freeze({
359
+ Voter: {
360
+ index: 0,
361
+ },
362
+ Withdrawer: {
363
+ index: 1,
364
+ },
365
+ });
366
+
367
+ /**
368
+ * Factory class for transactions to interact with the Vote program
369
+ */
370
+ export class VoteProgram {
371
+ /**
372
+ * @internal
373
+ */
374
+ constructor() {}
375
+
376
+ /**
377
+ * Public key that identifies the Vote program
378
+ */
379
+ static programId: PublicKey = new PublicKey(
380
+ 'Vote111111111111111111111111111111111111111',
381
+ );
382
+
383
+ /**
384
+ * Max space of a Vote account
385
+ *
386
+ * This is generated from the solana-vote-program VoteState struct as
387
+ * `VoteState::size_of()`:
388
+ * https://docs.rs/solana-vote-program/1.9.5/solana_vote_program/vote_state/struct.VoteState.html#method.size_of
389
+ *
390
+ * KEEP IN SYNC WITH `VoteState::size_of()` in https://github.com/solana-labs/solana/blob/a474cb24b9238f5edcc982f65c0b37d4a1046f7e/sdk/program/src/vote/state/mod.rs#L340-L342
391
+ */
392
+ static space: number = 3762;
393
+
394
+ /**
395
+ * Generate an Initialize instruction.
396
+ */
397
+ static initializeAccount(
398
+ params: InitializeAccountParams,
399
+ ): TransactionInstruction {
400
+ const {votePubkey, nodePubkey, voteInit} = params;
401
+ const type = VOTE_INSTRUCTION_LAYOUTS.InitializeAccount;
402
+ const data = encodeData(type, {
403
+ voteInit: {
404
+ nodePubkey: toBuffer(voteInit.nodePubkey.toBuffer()),
405
+ authorizedVoter: toBuffer(voteInit.authorizedVoter.toBuffer()),
406
+ authorizedWithdrawer: toBuffer(
407
+ voteInit.authorizedWithdrawer.toBuffer(),
408
+ ),
409
+ commission: voteInit.commission,
410
+ },
411
+ });
412
+ const instructionData = {
413
+ keys: [
414
+ {pubkey: votePubkey, isSigner: false, isWritable: true},
415
+ {pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false},
416
+ {pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false},
417
+ {pubkey: nodePubkey, isSigner: true, isWritable: false},
418
+ ],
419
+ programId: this.programId,
420
+ data,
421
+ };
422
+ return new TransactionInstruction(instructionData);
423
+ }
424
+
425
+ /**
426
+ * Generate a transaction that creates a new Vote account.
427
+ */
428
+ static createAccount(params: CreateVoteAccountParams): Transaction {
429
+ const transaction = new Transaction();
430
+ transaction.add(
431
+ SystemProgram.createAccount({
432
+ fromPubkey: params.fromPubkey,
433
+ newAccountPubkey: params.votePubkey,
434
+ lamports: params.lamports,
435
+ space: this.space,
436
+ programId: this.programId,
437
+ }),
438
+ );
439
+
440
+ return transaction.add(
441
+ this.initializeAccount({
442
+ votePubkey: params.votePubkey,
443
+ nodePubkey: params.voteInit.nodePubkey,
444
+ voteInit: params.voteInit,
445
+ }),
446
+ );
447
+ }
448
+
449
+ /**
450
+ * Generate a transaction that authorizes a new Voter or Withdrawer on the Vote account.
451
+ */
452
+ static authorize(params: AuthorizeVoteParams): Transaction {
453
+ const {
454
+ votePubkey,
455
+ authorizedPubkey,
456
+ newAuthorizedPubkey,
457
+ voteAuthorizationType,
458
+ } = params;
459
+
460
+ const type = VOTE_INSTRUCTION_LAYOUTS.Authorize;
461
+ const data = encodeData(type, {
462
+ newAuthorized: toBuffer(newAuthorizedPubkey.toBuffer()),
463
+ voteAuthorizationType: voteAuthorizationType.index,
464
+ });
465
+
466
+ const keys = [
467
+ {pubkey: votePubkey, isSigner: false, isWritable: true},
468
+ {pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false},
469
+ {pubkey: authorizedPubkey, isSigner: true, isWritable: false},
470
+ ];
471
+
472
+ return new Transaction().add({
473
+ keys,
474
+ programId: this.programId,
475
+ data,
476
+ });
477
+ }
478
+
479
+ /**
480
+ * Generate a transaction that authorizes a new Voter or Withdrawer on the Vote account
481
+ * where the current Voter or Withdrawer authority is a derived key.
482
+ */
483
+ static authorizeWithSeed(params: AuthorizeVoteWithSeedParams): Transaction {
484
+ const {
485
+ currentAuthorityDerivedKeyBasePubkey,
486
+ currentAuthorityDerivedKeyOwnerPubkey,
487
+ currentAuthorityDerivedKeySeed,
488
+ newAuthorizedPubkey,
489
+ voteAuthorizationType,
490
+ votePubkey,
491
+ } = params;
492
+
493
+ const type = VOTE_INSTRUCTION_LAYOUTS.AuthorizeWithSeed;
494
+ const data = encodeData(type, {
495
+ voteAuthorizeWithSeedArgs: {
496
+ currentAuthorityDerivedKeyOwnerPubkey: toBuffer(
497
+ currentAuthorityDerivedKeyOwnerPubkey.toBuffer(),
498
+ ),
499
+ currentAuthorityDerivedKeySeed: currentAuthorityDerivedKeySeed,
500
+ newAuthorized: toBuffer(newAuthorizedPubkey.toBuffer()),
501
+ voteAuthorizationType: voteAuthorizationType.index,
502
+ },
503
+ });
504
+
505
+ const keys = [
506
+ {pubkey: votePubkey, isSigner: false, isWritable: true},
507
+ {pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false},
508
+ {
509
+ pubkey: currentAuthorityDerivedKeyBasePubkey,
510
+ isSigner: true,
511
+ isWritable: false,
512
+ },
513
+ ];
514
+
515
+ return new Transaction().add({
516
+ keys,
517
+ programId: this.programId,
518
+ data,
519
+ });
520
+ }
521
+
522
+ /**
523
+ * Generate a transaction to withdraw from a Vote account.
524
+ */
525
+ static withdraw(params: WithdrawFromVoteAccountParams): Transaction {
526
+ const {votePubkey, authorizedWithdrawerPubkey, lamports, toPubkey} = params;
527
+ const type = VOTE_INSTRUCTION_LAYOUTS.Withdraw;
528
+ const data = encodeData(type, {lamports});
529
+
530
+ const keys = [
531
+ {pubkey: votePubkey, isSigner: false, isWritable: true},
532
+ {pubkey: toPubkey, isSigner: false, isWritable: true},
533
+ {pubkey: authorizedWithdrawerPubkey, isSigner: true, isWritable: false},
534
+ ];
535
+
536
+ return new Transaction().add({
537
+ keys,
538
+ programId: this.programId,
539
+ data,
540
+ });
541
+ }
542
+
543
+ /**
544
+ * Generate a transaction to withdraw safely from a Vote account.
545
+ *
546
+ * This function was created as a safeguard for vote accounts running validators, `safeWithdraw`
547
+ * checks that the withdraw amount will not exceed the specified balance while leaving enough left
548
+ * to cover rent. If you wish to close the vote account by withdrawing the full amount, call the
549
+ * `withdraw` method directly.
550
+ */
551
+ static safeWithdraw(
552
+ params: WithdrawFromVoteAccountParams,
553
+ currentVoteAccountBalance: number,
554
+ rentExemptMinimum: number,
555
+ ): Transaction {
556
+ if (params.lamports > currentVoteAccountBalance - rentExemptMinimum) {
557
+ throw new Error(
558
+ 'Withdraw will leave vote account with insufficient funds.',
559
+ );
560
+ }
561
+ return VoteProgram.withdraw(params);
562
+ }
563
+
564
+ /**
565
+ * Generate a transaction to update the validator identity (node pubkey) of a Vote account.
566
+ */
567
+ static updateValidatorIdentity(
568
+ params: UpdateValidatorIdentityParams,
569
+ ): Transaction {
570
+ const {votePubkey, authorizedWithdrawerPubkey, nodePubkey} = params;
571
+ const type = VOTE_INSTRUCTION_LAYOUTS.UpdateValidatorIdentity;
572
+ const data = encodeData(type);
573
+
574
+ const keys = [
575
+ {pubkey: votePubkey, isSigner: false, isWritable: true},
576
+ {pubkey: nodePubkey, isSigner: true, isWritable: false},
577
+ {pubkey: authorizedWithdrawerPubkey, isSigner: true, isWritable: false},
578
+ ];
579
+
580
+ return new Transaction().add({
581
+ keys,
582
+ programId: this.programId,
583
+ data,
584
+ });
585
+ }
586
+ }