@solana/web3.js 0.0.0-next

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 (53) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +158 -0
  3. package/lib/index.browser.cjs.js +10062 -0
  4. package/lib/index.browser.cjs.js.map +1 -0
  5. package/lib/index.browser.esm.js +9976 -0
  6. package/lib/index.browser.esm.js.map +1 -0
  7. package/lib/index.cjs.js +9568 -0
  8. package/lib/index.cjs.js.map +1 -0
  9. package/lib/index.d.ts +3311 -0
  10. package/lib/index.esm.js +9479 -0
  11. package/lib/index.esm.js.map +1 -0
  12. package/lib/index.iife.js +30136 -0
  13. package/lib/index.iife.js.map +1 -0
  14. package/lib/index.iife.min.js +40 -0
  15. package/lib/index.iife.min.js.map +1 -0
  16. package/package.json +140 -0
  17. package/src/account.ts +46 -0
  18. package/src/agent-manager.ts +44 -0
  19. package/src/blockhash.ts +4 -0
  20. package/src/bpf-loader-deprecated.ts +5 -0
  21. package/src/bpf-loader.ts +45 -0
  22. package/src/connection.ts +4935 -0
  23. package/src/ed25519-program.ts +157 -0
  24. package/src/epoch-schedule.ts +102 -0
  25. package/src/errors.ts +9 -0
  26. package/src/fee-calculator.ts +16 -0
  27. package/src/index.ts +31 -0
  28. package/src/instruction.ts +58 -0
  29. package/src/keypair.ts +98 -0
  30. package/src/layout.ts +147 -0
  31. package/src/loader.ts +236 -0
  32. package/src/message.ts +271 -0
  33. package/src/nonce-account.ts +78 -0
  34. package/src/publickey.ts +296 -0
  35. package/src/secp256k1-program.ts +229 -0
  36. package/src/stake-program.ts +923 -0
  37. package/src/system-program.ts +1007 -0
  38. package/src/sysvar.ts +37 -0
  39. package/src/timing.ts +23 -0
  40. package/src/transaction.ts +808 -0
  41. package/src/util/assert.ts +8 -0
  42. package/src/util/borsh-schema.ts +38 -0
  43. package/src/util/cluster.ts +31 -0
  44. package/src/util/promise-timeout.ts +14 -0
  45. package/src/util/send-and-confirm-raw-transaction.ts +46 -0
  46. package/src/util/send-and-confirm-transaction.ts +50 -0
  47. package/src/util/shortvec-encoding.ts +28 -0
  48. package/src/util/sleep.ts +4 -0
  49. package/src/util/to-buffer.ts +11 -0
  50. package/src/util/url.ts +18 -0
  51. package/src/validator-info.ts +106 -0
  52. package/src/vote-account.ts +236 -0
  53. package/src/vote-program.ts +413 -0
@@ -0,0 +1,413 @@
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-program';
12
+ import {SYSVAR_CLOCK_PUBKEY, SYSVAR_RENT_PUBKEY} from './sysvar';
13
+ import {Transaction, TransactionInstruction} from './transaction';
14
+ import {toBuffer} from './util/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
+ * Withdraw from vote account transaction params
70
+ */
71
+ export type WithdrawFromVoteAccountParams = {
72
+ votePubkey: PublicKey;
73
+ authorizedWithdrawerPubkey: PublicKey;
74
+ lamports: number;
75
+ toPubkey: PublicKey;
76
+ };
77
+
78
+ /**
79
+ * Vote Instruction class
80
+ */
81
+ export class VoteInstruction {
82
+ /**
83
+ * @internal
84
+ */
85
+ constructor() {}
86
+
87
+ /**
88
+ * Decode a vote instruction and retrieve the instruction type.
89
+ */
90
+ static decodeInstructionType(
91
+ instruction: TransactionInstruction,
92
+ ): VoteInstructionType {
93
+ this.checkProgramId(instruction.programId);
94
+
95
+ const instructionTypeLayout = BufferLayout.u32('instruction');
96
+ const typeIndex = instructionTypeLayout.decode(instruction.data);
97
+
98
+ let type: VoteInstructionType | undefined;
99
+ for (const [ixType, layout] of Object.entries(VOTE_INSTRUCTION_LAYOUTS)) {
100
+ if (layout.index == typeIndex) {
101
+ type = ixType as VoteInstructionType;
102
+ break;
103
+ }
104
+ }
105
+
106
+ if (!type) {
107
+ throw new Error('Instruction type incorrect; not a VoteInstruction');
108
+ }
109
+
110
+ return type;
111
+ }
112
+
113
+ /**
114
+ * Decode an initialize vote instruction and retrieve the instruction params.
115
+ */
116
+ static decodeInitializeAccount(
117
+ instruction: TransactionInstruction,
118
+ ): InitializeAccountParams {
119
+ this.checkProgramId(instruction.programId);
120
+ this.checkKeyLength(instruction.keys, 4);
121
+
122
+ const {voteInit} = decodeData(
123
+ VOTE_INSTRUCTION_LAYOUTS.InitializeAccount,
124
+ instruction.data,
125
+ );
126
+
127
+ return {
128
+ votePubkey: instruction.keys[0].pubkey,
129
+ nodePubkey: instruction.keys[3].pubkey,
130
+ voteInit: new VoteInit(
131
+ new PublicKey(voteInit.nodePubkey),
132
+ new PublicKey(voteInit.authorizedVoter),
133
+ new PublicKey(voteInit.authorizedWithdrawer),
134
+ voteInit.commission,
135
+ ),
136
+ };
137
+ }
138
+
139
+ /**
140
+ * Decode an authorize instruction and retrieve the instruction params.
141
+ */
142
+ static decodeAuthorize(
143
+ instruction: TransactionInstruction,
144
+ ): AuthorizeVoteParams {
145
+ this.checkProgramId(instruction.programId);
146
+ this.checkKeyLength(instruction.keys, 3);
147
+
148
+ const {newAuthorized, voteAuthorizationType} = decodeData(
149
+ VOTE_INSTRUCTION_LAYOUTS.Authorize,
150
+ instruction.data,
151
+ );
152
+
153
+ return {
154
+ votePubkey: instruction.keys[0].pubkey,
155
+ authorizedPubkey: instruction.keys[2].pubkey,
156
+ newAuthorizedPubkey: new PublicKey(newAuthorized),
157
+ voteAuthorizationType: {
158
+ index: voteAuthorizationType,
159
+ },
160
+ };
161
+ }
162
+
163
+ /**
164
+ * Decode a withdraw instruction and retrieve the instruction params.
165
+ */
166
+ static decodeWithdraw(
167
+ instruction: TransactionInstruction,
168
+ ): WithdrawFromVoteAccountParams {
169
+ this.checkProgramId(instruction.programId);
170
+ this.checkKeyLength(instruction.keys, 3);
171
+
172
+ const {lamports} = decodeData(
173
+ VOTE_INSTRUCTION_LAYOUTS.Withdraw,
174
+ instruction.data,
175
+ );
176
+
177
+ return {
178
+ votePubkey: instruction.keys[0].pubkey,
179
+ authorizedWithdrawerPubkey: instruction.keys[2].pubkey,
180
+ lamports,
181
+ toPubkey: instruction.keys[1].pubkey,
182
+ };
183
+ }
184
+
185
+ /**
186
+ * @internal
187
+ */
188
+ static checkProgramId(programId: PublicKey) {
189
+ if (!programId.equals(VoteProgram.programId)) {
190
+ throw new Error('invalid instruction; programId is not VoteProgram');
191
+ }
192
+ }
193
+
194
+ /**
195
+ * @internal
196
+ */
197
+ static checkKeyLength(keys: Array<any>, expectedLength: number) {
198
+ if (keys.length < expectedLength) {
199
+ throw new Error(
200
+ `invalid instruction; found ${keys.length} keys, expected at least ${expectedLength}`,
201
+ );
202
+ }
203
+ }
204
+ }
205
+
206
+ /**
207
+ * An enumeration of valid VoteInstructionType's
208
+ */
209
+ export type VoteInstructionType =
210
+ // FIXME
211
+ // It would be preferable for this type to be `keyof VoteInstructionInputData`
212
+ // but Typedoc does not transpile `keyof` expressions.
213
+ // See https://github.com/TypeStrong/typedoc/issues/1894
214
+ 'Authorize' | 'InitializeAccount' | 'Withdraw';
215
+
216
+ type VoteInstructionInputData = {
217
+ Authorize: IInstructionInputData & {
218
+ newAuthorized: Uint8Array;
219
+ voteAuthorizationType: number;
220
+ };
221
+ InitializeAccount: IInstructionInputData & {
222
+ voteInit: Readonly<{
223
+ authorizedVoter: Uint8Array;
224
+ authorizedWithdrawer: Uint8Array;
225
+ commission: number;
226
+ nodePubkey: Uint8Array;
227
+ }>;
228
+ };
229
+ Withdraw: IInstructionInputData & {
230
+ lamports: number;
231
+ };
232
+ };
233
+
234
+ const VOTE_INSTRUCTION_LAYOUTS = Object.freeze<{
235
+ [Instruction in VoteInstructionType]: InstructionType<
236
+ VoteInstructionInputData[Instruction]
237
+ >;
238
+ }>({
239
+ InitializeAccount: {
240
+ index: 0,
241
+ layout: BufferLayout.struct<VoteInstructionInputData['InitializeAccount']>([
242
+ BufferLayout.u32('instruction'),
243
+ Layout.voteInit(),
244
+ ]),
245
+ },
246
+ Authorize: {
247
+ index: 1,
248
+ layout: BufferLayout.struct<VoteInstructionInputData['Authorize']>([
249
+ BufferLayout.u32('instruction'),
250
+ Layout.publicKey('newAuthorized'),
251
+ BufferLayout.u32('voteAuthorizationType'),
252
+ ]),
253
+ },
254
+ Withdraw: {
255
+ index: 3,
256
+ layout: BufferLayout.struct<VoteInstructionInputData['Withdraw']>([
257
+ BufferLayout.u32('instruction'),
258
+ BufferLayout.ns64('lamports'),
259
+ ]),
260
+ },
261
+ });
262
+
263
+ /**
264
+ * VoteAuthorize type
265
+ */
266
+ export type VoteAuthorizationType = {
267
+ /** The VoteAuthorize index (from solana-vote-program) */
268
+ index: number;
269
+ };
270
+
271
+ /**
272
+ * An enumeration of valid VoteAuthorization layouts.
273
+ */
274
+ export const VoteAuthorizationLayout = Object.freeze({
275
+ Voter: {
276
+ index: 0,
277
+ },
278
+ Withdrawer: {
279
+ index: 1,
280
+ },
281
+ });
282
+
283
+ /**
284
+ * Factory class for transactions to interact with the Vote program
285
+ */
286
+ export class VoteProgram {
287
+ /**
288
+ * @internal
289
+ */
290
+ constructor() {}
291
+
292
+ /**
293
+ * Public key that identifies the Vote program
294
+ */
295
+ static programId: PublicKey = new PublicKey(
296
+ 'Vote111111111111111111111111111111111111111',
297
+ );
298
+
299
+ /**
300
+ * Max space of a Vote account
301
+ *
302
+ * This is generated from the solana-vote-program VoteState struct as
303
+ * `VoteState::size_of()`:
304
+ * https://docs.rs/solana-vote-program/1.9.5/solana_vote_program/vote_state/struct.VoteState.html#method.size_of
305
+ */
306
+ static space: number = 3731;
307
+
308
+ /**
309
+ * Generate an Initialize instruction.
310
+ */
311
+ static initializeAccount(
312
+ params: InitializeAccountParams,
313
+ ): TransactionInstruction {
314
+ const {votePubkey, nodePubkey, voteInit} = params;
315
+ const type = VOTE_INSTRUCTION_LAYOUTS.InitializeAccount;
316
+ const data = encodeData(type, {
317
+ voteInit: {
318
+ nodePubkey: toBuffer(voteInit.nodePubkey.toBuffer()),
319
+ authorizedVoter: toBuffer(voteInit.authorizedVoter.toBuffer()),
320
+ authorizedWithdrawer: toBuffer(
321
+ voteInit.authorizedWithdrawer.toBuffer(),
322
+ ),
323
+ commission: voteInit.commission,
324
+ },
325
+ });
326
+ const instructionData = {
327
+ keys: [
328
+ {pubkey: votePubkey, isSigner: false, isWritable: true},
329
+ {pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false},
330
+ {pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false},
331
+ {pubkey: nodePubkey, isSigner: true, isWritable: false},
332
+ ],
333
+ programId: this.programId,
334
+ data,
335
+ };
336
+ return new TransactionInstruction(instructionData);
337
+ }
338
+
339
+ /**
340
+ * Generate a transaction that creates a new Vote account.
341
+ */
342
+ static createAccount(params: CreateVoteAccountParams): Transaction {
343
+ const transaction = new Transaction();
344
+ transaction.add(
345
+ SystemProgram.createAccount({
346
+ fromPubkey: params.fromPubkey,
347
+ newAccountPubkey: params.votePubkey,
348
+ lamports: params.lamports,
349
+ space: this.space,
350
+ programId: this.programId,
351
+ }),
352
+ );
353
+
354
+ return transaction.add(
355
+ this.initializeAccount({
356
+ votePubkey: params.votePubkey,
357
+ nodePubkey: params.voteInit.nodePubkey,
358
+ voteInit: params.voteInit,
359
+ }),
360
+ );
361
+ }
362
+
363
+ /**
364
+ * Generate a transaction that authorizes a new Voter or Withdrawer on the Vote account.
365
+ */
366
+ static authorize(params: AuthorizeVoteParams): Transaction {
367
+ const {
368
+ votePubkey,
369
+ authorizedPubkey,
370
+ newAuthorizedPubkey,
371
+ voteAuthorizationType,
372
+ } = params;
373
+
374
+ const type = VOTE_INSTRUCTION_LAYOUTS.Authorize;
375
+ const data = encodeData(type, {
376
+ newAuthorized: toBuffer(newAuthorizedPubkey.toBuffer()),
377
+ voteAuthorizationType: voteAuthorizationType.index,
378
+ });
379
+
380
+ const keys = [
381
+ {pubkey: votePubkey, isSigner: false, isWritable: true},
382
+ {pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false},
383
+ {pubkey: authorizedPubkey, isSigner: true, isWritable: false},
384
+ ];
385
+
386
+ return new Transaction().add({
387
+ keys,
388
+ programId: this.programId,
389
+ data,
390
+ });
391
+ }
392
+
393
+ /**
394
+ * Generate a transaction to withdraw from a Vote account.
395
+ */
396
+ static withdraw(params: WithdrawFromVoteAccountParams): Transaction {
397
+ const {votePubkey, authorizedWithdrawerPubkey, lamports, toPubkey} = params;
398
+ const type = VOTE_INSTRUCTION_LAYOUTS.Withdraw;
399
+ const data = encodeData(type, {lamports});
400
+
401
+ const keys = [
402
+ {pubkey: votePubkey, isSigner: false, isWritable: true},
403
+ {pubkey: toPubkey, isSigner: false, isWritable: true},
404
+ {pubkey: authorizedWithdrawerPubkey, isSigner: true, isWritable: false},
405
+ ];
406
+
407
+ return new Transaction().add({
408
+ keys,
409
+ programId: this.programId,
410
+ data,
411
+ });
412
+ }
413
+ }