@solana/web3.js 1.47.4 → 1.50.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solana/web3.js",
3
- "version": "1.47.4",
3
+ "version": "1.50.0",
4
4
  "description": "Solana Javascript API",
5
5
  "keywords": [
6
6
  "api",
@@ -0,0 +1,433 @@
1
+ import {toBufferLE} from 'bigint-buffer';
2
+ import * as BufferLayout from '@solana/buffer-layout';
3
+
4
+ import * as Layout from './layout';
5
+ import {PublicKey} from './publickey';
6
+ import * as bigintLayout from './util/bigint';
7
+ import {SystemProgram} from './system-program';
8
+ import {TransactionInstruction} from './transaction';
9
+ import {decodeData, encodeData, IInstructionInputData} from './instruction';
10
+
11
+ export type CreateLookupTableParams = {
12
+ /** Account used to derive and control the new address lookup table. */
13
+ authority: PublicKey;
14
+ /** Account that will fund the new address lookup table. */
15
+ payer: PublicKey;
16
+ /** A recent slot must be used in the derivation path for each initialized table. */
17
+ recentSlot: bigint | number;
18
+ };
19
+
20
+ export type FreezeLookupTableParams = {
21
+ /** Address lookup table account to freeze. */
22
+ lookupTable: PublicKey;
23
+ /** Account which is the current authority. */
24
+ authority: PublicKey;
25
+ };
26
+
27
+ export type ExtendLookupTableParams = {
28
+ /** Address lookup table account to extend. */
29
+ lookupTable: PublicKey;
30
+ /** Account which is the current authority. */
31
+ authority: PublicKey;
32
+ /** Account that will fund the table reallocation.
33
+ * Not required if the reallocation has already been funded. */
34
+ payer?: PublicKey;
35
+ /** List of Public Keys to be added to the lookup table. */
36
+ addresses: Array<PublicKey>;
37
+ };
38
+
39
+ export type DeactivateLookupTableParams = {
40
+ /** Address lookup table account to deactivate. */
41
+ lookupTable: PublicKey;
42
+ /** Account which is the current authority. */
43
+ authority: PublicKey;
44
+ };
45
+
46
+ export type CloseLookupTableParams = {
47
+ /** Address lookup table account to close. */
48
+ lookupTable: PublicKey;
49
+ /** Account which is the current authority. */
50
+ authority: PublicKey;
51
+ /** Recipient of closed account lamports. */
52
+ recipient: PublicKey;
53
+ };
54
+
55
+ /**
56
+ * An enumeration of valid LookupTableInstructionType's
57
+ */
58
+ export type LookupTableInstructionType =
59
+ | 'CreateLookupTable'
60
+ | 'ExtendLookupTable'
61
+ | 'CloseLookupTable'
62
+ | 'FreezeLookupTable'
63
+ | 'DeactivateLookupTable';
64
+
65
+ type LookupTableInstructionInputData = {
66
+ CreateLookupTable: IInstructionInputData &
67
+ Readonly<{
68
+ recentSlot: bigint;
69
+ bumpSeed: number;
70
+ }>;
71
+ FreezeLookupTable: IInstructionInputData;
72
+ ExtendLookupTable: IInstructionInputData &
73
+ Readonly<{
74
+ numberOfAddresses: bigint;
75
+ addresses: Array<Uint8Array>;
76
+ }>;
77
+ DeactivateLookupTable: IInstructionInputData;
78
+ CloseLookupTable: IInstructionInputData;
79
+ };
80
+
81
+ /**
82
+ * An enumeration of valid address lookup table InstructionType's
83
+ * @internal
84
+ */
85
+ export const LOOKUP_TABLE_INSTRUCTION_LAYOUTS = Object.freeze({
86
+ CreateLookupTable: {
87
+ index: 0,
88
+ layout: BufferLayout.struct<
89
+ LookupTableInstructionInputData['CreateLookupTable']
90
+ >([
91
+ BufferLayout.u32('instruction'),
92
+ bigintLayout.u64('recentSlot'),
93
+ BufferLayout.u8('bumpSeed'),
94
+ ]),
95
+ },
96
+ FreezeLookupTable: {
97
+ index: 1,
98
+ layout: BufferLayout.struct<
99
+ LookupTableInstructionInputData['FreezeLookupTable']
100
+ >([BufferLayout.u32('instruction')]),
101
+ },
102
+ ExtendLookupTable: {
103
+ index: 2,
104
+ layout: BufferLayout.struct<
105
+ LookupTableInstructionInputData['ExtendLookupTable']
106
+ >([
107
+ BufferLayout.u32('instruction'),
108
+ bigintLayout.u64(),
109
+ BufferLayout.seq(
110
+ Layout.publicKey(),
111
+ BufferLayout.offset(BufferLayout.u32(), -8),
112
+ 'addresses',
113
+ ),
114
+ ]),
115
+ },
116
+ DeactivateLookupTable: {
117
+ index: 3,
118
+ layout: BufferLayout.struct<
119
+ LookupTableInstructionInputData['DeactivateLookupTable']
120
+ >([BufferLayout.u32('instruction')]),
121
+ },
122
+ CloseLookupTable: {
123
+ index: 4,
124
+ layout: BufferLayout.struct<
125
+ LookupTableInstructionInputData['CloseLookupTable']
126
+ >([BufferLayout.u32('instruction')]),
127
+ },
128
+ });
129
+
130
+ export class AddressLookupTableInstruction {
131
+ /**
132
+ * @internal
133
+ */
134
+ constructor() {}
135
+
136
+ static decodeInstructionType(
137
+ instruction: TransactionInstruction,
138
+ ): LookupTableInstructionType {
139
+ this.checkProgramId(instruction.programId);
140
+
141
+ const instructionTypeLayout = BufferLayout.u32('instruction');
142
+ const index = instructionTypeLayout.decode(instruction.data);
143
+
144
+ let type: LookupTableInstructionType | undefined;
145
+ for (const [layoutType, layout] of Object.entries(
146
+ LOOKUP_TABLE_INSTRUCTION_LAYOUTS,
147
+ )) {
148
+ if ((layout as any).index == index) {
149
+ type = layoutType as LookupTableInstructionType;
150
+ break;
151
+ }
152
+ }
153
+ if (!type) {
154
+ throw new Error(
155
+ 'Invalid Instruction. Should be a LookupTable Instruction',
156
+ );
157
+ }
158
+ return type;
159
+ }
160
+
161
+ static decodeCreateLookupTable(
162
+ instruction: TransactionInstruction,
163
+ ): CreateLookupTableParams {
164
+ this.checkProgramId(instruction.programId);
165
+ this.checkKeysLength(instruction.keys, 4);
166
+
167
+ const {recentSlot} = decodeData(
168
+ LOOKUP_TABLE_INSTRUCTION_LAYOUTS.CreateLookupTable,
169
+ instruction.data,
170
+ );
171
+
172
+ return {
173
+ authority: instruction.keys[1].pubkey,
174
+ payer: instruction.keys[2].pubkey,
175
+ recentSlot: Number(recentSlot),
176
+ };
177
+ }
178
+
179
+ static decodeExtendLookupTable(
180
+ instruction: TransactionInstruction,
181
+ ): ExtendLookupTableParams {
182
+ this.checkProgramId(instruction.programId);
183
+ if (instruction.keys.length < 2) {
184
+ throw new Error(
185
+ `invalid instruction; found ${instruction.keys.length} keys, expected at least 2`,
186
+ );
187
+ }
188
+
189
+ const {addresses} = decodeData(
190
+ LOOKUP_TABLE_INSTRUCTION_LAYOUTS.ExtendLookupTable,
191
+ instruction.data,
192
+ );
193
+ return {
194
+ lookupTable: instruction.keys[0].pubkey,
195
+ authority: instruction.keys[1].pubkey,
196
+ payer:
197
+ instruction.keys.length > 2 ? instruction.keys[2].pubkey : undefined,
198
+ addresses: addresses.map(buffer => new PublicKey(buffer)),
199
+ };
200
+ }
201
+
202
+ static decodeCloseLookupTable(
203
+ instruction: TransactionInstruction,
204
+ ): CloseLookupTableParams {
205
+ this.checkProgramId(instruction.programId);
206
+ this.checkKeysLength(instruction.keys, 3);
207
+
208
+ return {
209
+ lookupTable: instruction.keys[0].pubkey,
210
+ authority: instruction.keys[1].pubkey,
211
+ recipient: instruction.keys[2].pubkey,
212
+ };
213
+ }
214
+
215
+ static decodeFreezeLookupTable(
216
+ instruction: TransactionInstruction,
217
+ ): FreezeLookupTableParams {
218
+ this.checkProgramId(instruction.programId);
219
+ this.checkKeysLength(instruction.keys, 2);
220
+
221
+ return {
222
+ lookupTable: instruction.keys[0].pubkey,
223
+ authority: instruction.keys[1].pubkey,
224
+ };
225
+ }
226
+
227
+ static decodeDeactivateLookupTable(
228
+ instruction: TransactionInstruction,
229
+ ): DeactivateLookupTableParams {
230
+ this.checkProgramId(instruction.programId);
231
+ this.checkKeysLength(instruction.keys, 2);
232
+
233
+ return {
234
+ lookupTable: instruction.keys[0].pubkey,
235
+ authority: instruction.keys[1].pubkey,
236
+ };
237
+ }
238
+
239
+ /**
240
+ * @internal
241
+ */
242
+ static checkProgramId(programId: PublicKey) {
243
+ if (!programId.equals(AddressLookupTableProgram.programId)) {
244
+ throw new Error(
245
+ 'invalid instruction; programId is not AddressLookupTable Program',
246
+ );
247
+ }
248
+ }
249
+ /**
250
+ * @internal
251
+ */
252
+ static checkKeysLength(keys: Array<any>, expectedLength: number) {
253
+ if (keys.length < expectedLength) {
254
+ throw new Error(
255
+ `invalid instruction; found ${keys.length} keys, expected at least ${expectedLength}`,
256
+ );
257
+ }
258
+ }
259
+ }
260
+
261
+ export class AddressLookupTableProgram {
262
+ /**
263
+ * @internal
264
+ */
265
+ constructor() {}
266
+
267
+ static programId: PublicKey = new PublicKey(
268
+ 'AddressLookupTab1e1111111111111111111111111',
269
+ );
270
+
271
+ static createLookupTable(params: CreateLookupTableParams) {
272
+ const [lookupTableAddress, bumpSeed] = PublicKey.findProgramAddressSync(
273
+ [params.authority.toBuffer(), toBufferLE(BigInt(params.recentSlot), 8)],
274
+ this.programId,
275
+ );
276
+
277
+ const type = LOOKUP_TABLE_INSTRUCTION_LAYOUTS.CreateLookupTable;
278
+ const data = encodeData(type, {
279
+ recentSlot: BigInt(params.recentSlot),
280
+ bumpSeed: bumpSeed,
281
+ });
282
+
283
+ const keys = [
284
+ {
285
+ pubkey: lookupTableAddress,
286
+ isSigner: false,
287
+ isWritable: true,
288
+ },
289
+ {
290
+ pubkey: params.authority,
291
+ isSigner: true,
292
+ isWritable: false,
293
+ },
294
+ {
295
+ pubkey: params.payer,
296
+ isSigner: true,
297
+ isWritable: true,
298
+ },
299
+ {
300
+ pubkey: SystemProgram.programId,
301
+ isSigner: false,
302
+ isWritable: false,
303
+ },
304
+ ];
305
+
306
+ return [
307
+ new TransactionInstruction({
308
+ programId: this.programId,
309
+ keys: keys,
310
+ data: data,
311
+ }),
312
+ lookupTableAddress,
313
+ ] as [TransactionInstruction, PublicKey];
314
+ }
315
+
316
+ static freezeLookupTable(params: FreezeLookupTableParams) {
317
+ const type = LOOKUP_TABLE_INSTRUCTION_LAYOUTS.FreezeLookupTable;
318
+ const data = encodeData(type);
319
+
320
+ const keys = [
321
+ {
322
+ pubkey: params.lookupTable,
323
+ isSigner: false,
324
+ isWritable: true,
325
+ },
326
+ {
327
+ pubkey: params.authority,
328
+ isSigner: true,
329
+ isWritable: false,
330
+ },
331
+ ];
332
+
333
+ return new TransactionInstruction({
334
+ programId: this.programId,
335
+ keys: keys,
336
+ data: data,
337
+ });
338
+ }
339
+
340
+ static extendLookupTable(params: ExtendLookupTableParams) {
341
+ const type = LOOKUP_TABLE_INSTRUCTION_LAYOUTS.ExtendLookupTable;
342
+ const data = encodeData(type, {
343
+ addresses: params.addresses.map(addr => addr.toBytes()),
344
+ });
345
+
346
+ const keys = [
347
+ {
348
+ pubkey: params.lookupTable,
349
+ isSigner: false,
350
+ isWritable: true,
351
+ },
352
+ {
353
+ pubkey: params.authority,
354
+ isSigner: true,
355
+ isWritable: false,
356
+ },
357
+ ];
358
+
359
+ if (params.payer) {
360
+ keys.push(
361
+ {
362
+ pubkey: params.payer,
363
+ isSigner: true,
364
+ isWritable: true,
365
+ },
366
+ {
367
+ pubkey: SystemProgram.programId,
368
+ isSigner: false,
369
+ isWritable: false,
370
+ },
371
+ );
372
+ }
373
+
374
+ return new TransactionInstruction({
375
+ programId: this.programId,
376
+ keys: keys,
377
+ data: data,
378
+ });
379
+ }
380
+
381
+ static deactivateLookupTable(params: DeactivateLookupTableParams) {
382
+ const type = LOOKUP_TABLE_INSTRUCTION_LAYOUTS.DeactivateLookupTable;
383
+ const data = encodeData(type);
384
+
385
+ const keys = [
386
+ {
387
+ pubkey: params.lookupTable,
388
+ isSigner: false,
389
+ isWritable: true,
390
+ },
391
+ {
392
+ pubkey: params.authority,
393
+ isSigner: true,
394
+ isWritable: false,
395
+ },
396
+ ];
397
+
398
+ return new TransactionInstruction({
399
+ programId: this.programId,
400
+ keys: keys,
401
+ data: data,
402
+ });
403
+ }
404
+
405
+ static closeLookupTable(params: CloseLookupTableParams) {
406
+ const type = LOOKUP_TABLE_INSTRUCTION_LAYOUTS.CloseLookupTable;
407
+ const data = encodeData(type);
408
+
409
+ const keys = [
410
+ {
411
+ pubkey: params.lookupTable,
412
+ isSigner: false,
413
+ isWritable: true,
414
+ },
415
+ {
416
+ pubkey: params.authority,
417
+ isSigner: true,
418
+ isWritable: false,
419
+ },
420
+ {
421
+ pubkey: params.recipient,
422
+ isSigner: false,
423
+ isWritable: true,
424
+ },
425
+ ];
426
+
427
+ return new TransactionInstruction({
428
+ programId: this.programId,
429
+ keys: keys,
430
+ data: data,
431
+ });
432
+ }
433
+ }
package/src/connection.ts CHANGED
@@ -443,6 +443,24 @@ export type GetBalanceConfig = {
443
443
  minContextSlot?: number;
444
444
  };
445
445
 
446
+ /**
447
+ * Configuration object for changing `getBlock` query behavior
448
+ */
449
+ export type GetBlockConfig = {
450
+ /** The level of finality desired */
451
+ commitment?: Finality;
452
+ /** The max transaction version to return in responses. If the requested transaction is a higher version, an error will be returned */
453
+ maxSupportedTransactionVersion?: number;
454
+ };
455
+
456
+ /**
457
+ * Configuration object for changing `getStakeMinimumDelegation` query behavior
458
+ */
459
+ export type GetStakeMinimumDelegationConfig = {
460
+ /** The level of commitment desired */
461
+ commitment?: Commitment;
462
+ };
463
+
446
464
  /**
447
465
  * Configuration object for changing `getBlockHeight` query behavior
448
466
  */
@@ -505,6 +523,16 @@ export type GetSlotLeaderConfig = {
505
523
  minContextSlot?: number;
506
524
  };
507
525
 
526
+ /**
527
+ * Configuration object for changing `getTransaction` query behavior
528
+ */
529
+ export type GetTransactionConfig = {
530
+ /** The level of finality desired */
531
+ commitment?: Finality;
532
+ /** The max transaction version to return in responses. If the requested transaction is a higher version, an error will be returned */
533
+ maxSupportedTransactionVersion?: number;
534
+ };
535
+
508
536
  /**
509
537
  * Configuration object for changing `getLargestAccounts` query behavior
510
538
  */
@@ -3592,11 +3620,14 @@ export class Connection {
3592
3620
  */
3593
3621
  async getBlock(
3594
3622
  slot: number,
3595
- opts?: {commitment?: Finality},
3623
+ rawConfig?: GetBlockConfig,
3596
3624
  ): Promise<BlockResponse | null> {
3625
+ const {commitment, config} = extractCommitmentFromConfig(rawConfig);
3597
3626
  const args = this._buildArgsAtLeastConfirmed(
3598
3627
  [slot],
3599
- opts && opts.commitment,
3628
+ commitment as Finality,
3629
+ undefined /* encoding */,
3630
+ config,
3600
3631
  );
3601
3632
  const unsafeRes = await this._rpcRequest('getBlock', args);
3602
3633
  const res = create(unsafeRes, GetBlockRpcResult);
@@ -3684,11 +3715,14 @@ export class Connection {
3684
3715
  */
3685
3716
  async getTransaction(
3686
3717
  signature: string,
3687
- opts?: {commitment?: Finality},
3718
+ rawConfig?: GetTransactionConfig,
3688
3719
  ): Promise<TransactionResponse | null> {
3720
+ const {commitment, config} = extractCommitmentFromConfig(rawConfig);
3689
3721
  const args = this._buildArgsAtLeastConfirmed(
3690
3722
  [signature],
3691
- opts && opts.commitment,
3723
+ commitment as Finality,
3724
+ undefined /* encoding */,
3725
+ config,
3692
3726
  );
3693
3727
  const unsafeRes = await this._rpcRequest('getTransaction', args);
3694
3728
  const res = create(unsafeRes, GetTransactionRpcResult);
@@ -3713,12 +3747,15 @@ export class Connection {
3713
3747
  */
3714
3748
  async getParsedTransaction(
3715
3749
  signature: TransactionSignature,
3716
- commitment?: Finality,
3750
+ commitmentOrConfig?: GetTransactionConfig | Finality,
3717
3751
  ): Promise<ParsedConfirmedTransaction | null> {
3752
+ const {commitment, config} =
3753
+ extractCommitmentFromConfig(commitmentOrConfig);
3718
3754
  const args = this._buildArgsAtLeastConfirmed(
3719
3755
  [signature],
3720
- commitment,
3756
+ commitment as Finality,
3721
3757
  'jsonParsed',
3758
+ config,
3722
3759
  );
3723
3760
  const unsafeRes = await this._rpcRequest('getTransaction', args);
3724
3761
  const res = create(unsafeRes, GetParsedTransactionRpcResult);
@@ -3733,13 +3770,16 @@ export class Connection {
3733
3770
  */
3734
3771
  async getParsedTransactions(
3735
3772
  signatures: TransactionSignature[],
3736
- commitment?: Finality,
3773
+ commitmentOrConfig?: GetTransactionConfig | Finality,
3737
3774
  ): Promise<(ParsedConfirmedTransaction | null)[]> {
3775
+ const {commitment, config} =
3776
+ extractCommitmentFromConfig(commitmentOrConfig);
3738
3777
  const batch = signatures.map(signature => {
3739
3778
  const args = this._buildArgsAtLeastConfirmed(
3740
3779
  [signature],
3741
- commitment,
3780
+ commitment as Finality,
3742
3781
  'jsonParsed',
3782
+ config,
3743
3783
  );
3744
3784
  return {
3745
3785
  methodName: 'getTransaction',
@@ -3765,10 +3805,17 @@ export class Connection {
3765
3805
  */
3766
3806
  async getTransactions(
3767
3807
  signatures: TransactionSignature[],
3768
- commitment?: Finality,
3808
+ commitmentOrConfig?: GetTransactionConfig | Finality,
3769
3809
  ): Promise<(TransactionResponse | null)[]> {
3810
+ const {commitment, config} =
3811
+ extractCommitmentFromConfig(commitmentOrConfig);
3770
3812
  const batch = signatures.map(signature => {
3771
- const args = this._buildArgsAtLeastConfirmed([signature], commitment);
3813
+ const args = this._buildArgsAtLeastConfirmed(
3814
+ [signature],
3815
+ commitment as Finality,
3816
+ undefined /* encoding */,
3817
+ config,
3818
+ );
3772
3819
  return {
3773
3820
  methodName: 'getTransaction',
3774
3821
  args,
@@ -4278,6 +4325,25 @@ export class Connection {
4278
4325
  }
4279
4326
  }
4280
4327
 
4328
+ /**
4329
+ * get the stake minimum delegation
4330
+ */
4331
+ async getStakeMinimumDelegation(
4332
+ config?: GetStakeMinimumDelegationConfig,
4333
+ ): Promise<RpcResponseAndContext<number>> {
4334
+ const {commitment, config: configArg} = extractCommitmentFromConfig(config);
4335
+ const args = this._buildArgs([], commitment, 'base64', configArg);
4336
+ const unsafeRes = await this._rpcRequest('getStakeMinimumDelegation', args);
4337
+ const res = create(unsafeRes, jsonRpcResultAndContext(number()));
4338
+ if ('error' in res) {
4339
+ throw new SolanaJSONRPCError(
4340
+ res.error,
4341
+ `failed to get stake minimum delegation`,
4342
+ );
4343
+ }
4344
+ return res.result;
4345
+ }
4346
+
4281
4347
  /**
4282
4348
  * Simulate a transaction
4283
4349
  */
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './account';
2
+ export * from './address-lookup-table-program';
2
3
  export * from './blockhash';
3
4
  export * from './bpf-loader-deprecated';
4
5
  export * from './bpf-loader';
package/src/layout.ts CHANGED
@@ -135,13 +135,25 @@ export const voteInit = (property: string = 'voteInit') => {
135
135
  };
136
136
 
137
137
  export function getAlloc(type: any, fields: any): number {
138
- let alloc = 0;
139
- type.layout.fields.forEach((item: any) => {
138
+ const getItemAlloc = (item: any): number => {
140
139
  if (item.span >= 0) {
141
- alloc += item.span;
140
+ return item.span;
142
141
  } else if (typeof item.alloc === 'function') {
143
- alloc += item.alloc(fields[item.property]);
142
+ return item.alloc(fields[item.property]);
143
+ } else if ('count' in item && 'elementLayout' in item) {
144
+ const field = fields[item.property];
145
+ if (Array.isArray(field)) {
146
+ return field.length * getItemAlloc(item.elementLayout);
147
+ }
144
148
  }
149
+ // Couldn't determine allocated size of layout
150
+ return 0;
151
+ };
152
+
153
+ let alloc = 0;
154
+ type.layout.fields.forEach((item: any) => {
155
+ alloc += getItemAlloc(item);
145
156
  });
157
+
146
158
  return alloc;
147
159
  }