@solana/web3.js 1.42.0 → 1.43.2

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.42.0",
3
+ "version": "1.43.2",
4
4
  "description": "Solana Javascript API",
5
5
  "keywords": [
6
6
  "api",
@@ -59,15 +59,15 @@
59
59
  "@babel/runtime": "^7.12.5",
60
60
  "@ethersproject/sha2": "^5.5.0",
61
61
  "@solana/buffer-layout": "^4.0.0",
62
- "@solana/buffer-layout-utils": "^0.2.0",
62
+ "bigint-buffer": "^1.1.5",
63
63
  "bn.js": "^5.0.0",
64
64
  "borsh": "^0.7.0",
65
65
  "bs58": "^4.0.1",
66
66
  "buffer": "6.0.1",
67
- "cross-fetch": "^3.1.4",
68
67
  "fast-stable-stringify": "^1.0.0",
69
68
  "jayson": "^3.4.4",
70
69
  "js-sha3": "^0.8.0",
70
+ "node-fetch": "2",
71
71
  "rpc-websockets": "^7.4.2",
72
72
  "secp256k1": "^4.0.2",
73
73
  "superstruct": "^0.14.2",
@@ -81,10 +81,10 @@
81
81
  "@babel/preset-typescript": "^7.12.16",
82
82
  "@babel/register": "^7.12.13",
83
83
  "@commitlint/config-conventional": "^15.0.0",
84
- "@commitlint/travis-cli": "^16.2.3",
85
- "@rollup/plugin-alias": "^3.1.2",
84
+ "@commitlint/travis-cli": "^17.0.0",
85
+ "@rollup/plugin-alias": "^3.1.9",
86
86
  "@rollup/plugin-babel": "^5.2.3",
87
- "@rollup/plugin-commonjs": "^21.0.0",
87
+ "@rollup/plugin-commonjs": "^22.0.0",
88
88
  "@rollup/plugin-json": "^4.1.0",
89
89
  "@rollup/plugin-multi-entry": "^4.0.0",
90
90
  "@rollup/plugin-node-resolve": "^13.0.0",
@@ -98,6 +98,7 @@
98
98
  "@types/mocha": "^9.0.0",
99
99
  "@types/mz": "^2.7.3",
100
100
  "@types/node": "^17.0.24",
101
+ "@types/node-fetch": "2",
101
102
  "@types/secp256k1": "^4.0.1",
102
103
  "@types/sinon": "^10.0.0",
103
104
  "@types/sinon-chai": "^3.2.8",
@@ -110,7 +111,7 @@
110
111
  "eslint": "^7.19.0",
111
112
  "eslint-config-prettier": "^8.0.0",
112
113
  "eslint-plugin-import": "2.26.0",
113
- "eslint-plugin-mocha": "^9.0.0",
114
+ "eslint-plugin-mocha": "^10.0.4",
114
115
  "eslint-plugin-prettier": "^4.0.0",
115
116
  "esm": "^3.2.25",
116
117
  "http-server": "^14.0.0",
@@ -0,0 +1,4 @@
1
+ export const Headers = globalThis.Headers;
2
+ export const Request = globalThis.Request;
3
+ export const Response = globalThis.Response;
4
+ export default globalThis.fetch;
@@ -8,6 +8,7 @@ import {
8
8
  } from './instruction';
9
9
  import {PublicKey} from './publickey';
10
10
  import {TransactionInstruction} from './transaction';
11
+ import {u64} from './util/bigint';
11
12
 
12
13
  /**
13
14
  * Compute Budget Instruction class
@@ -76,6 +77,34 @@ export class ComputeBudgetInstruction {
76
77
  return {bytes};
77
78
  }
78
79
 
80
+ /**
81
+ * Decode set compute unit limit compute budget instruction and retrieve the instruction params.
82
+ */
83
+ static decodeSetComputeUnitLimit(
84
+ instruction: TransactionInstruction,
85
+ ): SetComputeUnitLimitParams {
86
+ this.checkProgramId(instruction.programId);
87
+ const {units} = decodeData(
88
+ COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.SetComputeUnitLimit,
89
+ instruction.data,
90
+ );
91
+ return {units};
92
+ }
93
+
94
+ /**
95
+ * Decode set compute unit price compute budget instruction and retrieve the instruction params.
96
+ */
97
+ static decodeSetComputeUnitPrice(
98
+ instruction: TransactionInstruction,
99
+ ): SetComputeUnitPriceParams {
100
+ this.checkProgramId(instruction.programId);
101
+ const {microLamports} = decodeData(
102
+ COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.SetComputeUnitPrice,
103
+ instruction.data,
104
+ );
105
+ return {microLamports};
106
+ }
107
+
79
108
  /**
80
109
  * @internal
81
110
  */
@@ -96,11 +125,18 @@ export type ComputeBudgetInstructionType =
96
125
  // It would be preferable for this type to be `keyof ComputeBudgetInstructionInputData`
97
126
  // but Typedoc does not transpile `keyof` expressions.
98
127
  // See https://github.com/TypeStrong/typedoc/issues/1894
99
- 'RequestUnits' | 'RequestHeapFrame';
128
+ | 'RequestUnits'
129
+ | 'RequestHeapFrame'
130
+ | 'SetComputeUnitLimit'
131
+ | 'SetComputeUnitPrice';
100
132
 
101
133
  type ComputeBudgetInstructionInputData = {
102
134
  RequestUnits: IInstructionInputData & Readonly<RequestUnitsParams>;
103
135
  RequestHeapFrame: IInstructionInputData & Readonly<RequestHeapFrameParams>;
136
+ SetComputeUnitLimit: IInstructionInputData &
137
+ Readonly<SetComputeUnitLimitParams>;
138
+ SetComputeUnitPrice: IInstructionInputData &
139
+ Readonly<SetComputeUnitPriceParams>;
104
140
  };
105
141
 
106
142
  /**
@@ -109,8 +145,7 @@ type ComputeBudgetInstructionInputData = {
109
145
  export interface RequestUnitsParams {
110
146
  /** Units to request for transaction-wide compute */
111
147
  units: number;
112
-
113
- /** Additional fee to pay */
148
+ /** Prioritization fee lamports */
114
149
  additionalFee: number;
115
150
  }
116
151
 
@@ -122,6 +157,22 @@ export type RequestHeapFrameParams = {
122
157
  bytes: number;
123
158
  };
124
159
 
160
+ /**
161
+ * Set compute unit limit instruction params
162
+ */
163
+ export interface SetComputeUnitLimitParams {
164
+ /** Transaction-wide compute unit limit */
165
+ units: number;
166
+ }
167
+
168
+ /**
169
+ * Set compute unit price instruction params
170
+ */
171
+ export interface SetComputeUnitPriceParams {
172
+ /** Transaction compute unit price used for prioritization fees */
173
+ microLamports: number | bigint;
174
+ }
175
+
125
176
  /**
126
177
  * An enumeration of valid ComputeBudget InstructionType's
127
178
  * @internal
@@ -147,6 +198,18 @@ export const COMPUTE_BUDGET_INSTRUCTION_LAYOUTS = Object.freeze<{
147
198
  ComputeBudgetInstructionInputData['RequestHeapFrame']
148
199
  >([BufferLayout.u8('instruction'), BufferLayout.u32('bytes')]),
149
200
  },
201
+ SetComputeUnitLimit: {
202
+ index: 2,
203
+ layout: BufferLayout.struct<
204
+ ComputeBudgetInstructionInputData['SetComputeUnitLimit']
205
+ >([BufferLayout.u8('instruction'), BufferLayout.u32('units')]),
206
+ },
207
+ SetComputeUnitPrice: {
208
+ index: 3,
209
+ layout: BufferLayout.struct<
210
+ ComputeBudgetInstructionInputData['SetComputeUnitPrice']
211
+ >([BufferLayout.u8('instruction'), u64('microLamports')]),
212
+ },
150
213
  });
151
214
 
152
215
  /**
@@ -186,4 +249,30 @@ export class ComputeBudgetProgram {
186
249
  data,
187
250
  });
188
251
  }
252
+
253
+ static setComputeUnitLimit(
254
+ params: SetComputeUnitLimitParams,
255
+ ): TransactionInstruction {
256
+ const type = COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.SetComputeUnitLimit;
257
+ const data = encodeData(type, params);
258
+ return new TransactionInstruction({
259
+ keys: [],
260
+ programId: this.programId,
261
+ data,
262
+ });
263
+ }
264
+
265
+ static setComputeUnitPrice(
266
+ params: SetComputeUnitPriceParams,
267
+ ): TransactionInstruction {
268
+ const type = COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.SetComputeUnitPrice;
269
+ const data = encodeData(type, {
270
+ microLamports: BigInt(params.microLamports),
271
+ });
272
+ return new TransactionInstruction({
273
+ keys: [],
274
+ programId: this.programId,
275
+ data,
276
+ });
277
+ }
189
278
  }
package/src/connection.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import bs58 from 'bs58';
2
2
  import {Buffer} from 'buffer';
3
- import crossFetch from 'cross-fetch';
4
3
  // @ts-ignore
5
4
  import fastStableStringify from 'fast-stable-stringify';
6
5
  import {
@@ -28,6 +27,7 @@ import RpcClient from 'jayson/lib/client/browser';
28
27
  import {AgentManager} from './agent-manager';
29
28
  import {EpochSchedule} from './epoch-schedule';
30
29
  import {SendTransactionError} from './errors';
30
+ import fetchImpl, {Response} from './fetch-impl';
31
31
  import {NonceAccount} from './nonce-account';
32
32
  import {PublicKey} from './publickey';
33
33
  import {Signer} from './keypair';
@@ -293,7 +293,7 @@ export type BlockhashWithExpiryBlockHeight = Readonly<{
293
293
  * A strategy for confirming transactions that uses the last valid
294
294
  * block height for a given blockhash to check for transaction expiration.
295
295
  */
296
- export type BlockheightBasedTransactionConfimationStrategy = {
296
+ export type BlockheightBasedTransactionConfirmationStrategy = {
297
297
  signature: TransactionSignature;
298
298
  } & BlockhashWithExpiryBlockHeight;
299
299
 
@@ -955,27 +955,25 @@ function createRpcClient(
955
955
  url: string,
956
956
  useHttps: boolean,
957
957
  httpHeaders?: HttpHeaders,
958
- customFetch?: typeof crossFetch,
958
+ customFetch?: FetchFn,
959
959
  fetchMiddleware?: FetchMiddleware,
960
960
  disableRetryOnRateLimit?: boolean,
961
961
  ): RpcClient {
962
- const fetch = customFetch ? customFetch : crossFetch;
962
+ const fetch = customFetch ? customFetch : fetchImpl;
963
963
  let agentManager: AgentManager | undefined;
964
964
  if (!process.env.BROWSER) {
965
965
  agentManager = new AgentManager(useHttps);
966
966
  }
967
967
 
968
- let fetchWithMiddleware:
969
- | ((url: string, options: any) => Promise<Response>)
970
- | undefined;
968
+ let fetchWithMiddleware: FetchFn | undefined;
971
969
 
972
970
  if (fetchMiddleware) {
973
- fetchWithMiddleware = async (url: string, options: any) => {
974
- const modifiedFetchArgs = await new Promise<[string, any]>(
971
+ fetchWithMiddleware = async (info, init) => {
972
+ const modifiedFetchArgs = await new Promise<Parameters<FetchFn>>(
975
973
  (resolve, reject) => {
976
974
  try {
977
- fetchMiddleware(url, options, (modifiedUrl, modifiedOptions) =>
978
- resolve([modifiedUrl, modifiedOptions]),
975
+ fetchMiddleware(info, init, (modifiedInfo, modifiedInit) =>
976
+ resolve([modifiedInfo, modifiedInit]),
979
977
  );
980
978
  } catch (error) {
981
979
  reject(error);
@@ -2162,13 +2160,18 @@ export type ConfirmedSignatureInfo = {
2162
2160
  */
2163
2161
  export type HttpHeaders = {[header: string]: string};
2164
2162
 
2163
+ /**
2164
+ * The type of the JavaScript `fetch()` API
2165
+ */
2166
+ export type FetchFn = typeof fetchImpl;
2167
+
2165
2168
  /**
2166
2169
  * A callback used to augment the outgoing HTTP request
2167
2170
  */
2168
2171
  export type FetchMiddleware = (
2169
- url: string,
2170
- options: any,
2171
- fetch: (modifiedUrl: string, modifiedOptions: any) => void,
2172
+ info: Parameters<FetchFn>[0],
2173
+ init: Parameters<FetchFn>[1],
2174
+ fetch: (...a: Parameters<FetchFn>) => void,
2172
2175
  ) => void;
2173
2176
 
2174
2177
  /**
@@ -2182,7 +2185,7 @@ export type ConnectionConfig = {
2182
2185
  /** Optional HTTP headers object */
2183
2186
  httpHeaders?: HttpHeaders;
2184
2187
  /** Optional custom fetch function */
2185
- fetch?: typeof crossFetch;
2188
+ fetch?: FetchFn;
2186
2189
  /** Optional fetch middleware callback */
2187
2190
  fetchMiddleware?: FetchMiddleware;
2188
2191
  /** Optional Disable retrying calls when server responds with HTTP 429 (Too Many Requests) */
@@ -2842,7 +2845,7 @@ export class Connection {
2842
2845
  }
2843
2846
 
2844
2847
  confirmTransaction(
2845
- strategy: BlockheightBasedTransactionConfimationStrategy,
2848
+ strategy: BlockheightBasedTransactionConfirmationStrategy,
2846
2849
  commitment?: Commitment,
2847
2850
  ): Promise<RpcResponseAndContext<SignatureResult>>;
2848
2851
 
@@ -2856,7 +2859,7 @@ export class Connection {
2856
2859
  // eslint-disable-next-line no-dupe-class-members
2857
2860
  async confirmTransaction(
2858
2861
  strategy:
2859
- | BlockheightBasedTransactionConfimationStrategy
2862
+ | BlockheightBasedTransactionConfirmationStrategy
2860
2863
  | TransactionSignature,
2861
2864
  commitment?: Commitment,
2862
2865
  ): Promise<RpcResponseAndContext<SignatureResult>> {
@@ -2865,7 +2868,8 @@ export class Connection {
2865
2868
  if (typeof strategy == 'string') {
2866
2869
  rawSignature = strategy;
2867
2870
  } else {
2868
- const config = strategy as BlockheightBasedTransactionConfimationStrategy;
2871
+ const config =
2872
+ strategy as BlockheightBasedTransactionConfirmationStrategy;
2869
2873
  rawSignature = config.signature;
2870
2874
  }
2871
2875
 
@@ -2942,7 +2946,8 @@ export class Connection {
2942
2946
  timeoutMs,
2943
2947
  );
2944
2948
  } else {
2945
- let config = strategy as BlockheightBasedTransactionConfimationStrategy;
2949
+ let config =
2950
+ strategy as BlockheightBasedTransactionConfirmationStrategy;
2946
2951
  (async () => {
2947
2952
  let currentBlockHeight = await checkBlockHeight();
2948
2953
  if (done) return;
@@ -0,0 +1,13 @@
1
+ import * as nodeFetch from 'node-fetch';
2
+
3
+ export * from 'node-fetch';
4
+ export default async function (
5
+ input: nodeFetch.RequestInfo,
6
+ init?: nodeFetch.RequestInit,
7
+ ): Promise<nodeFetch.Response> {
8
+ const processedInput =
9
+ typeof input === 'string' && input.slice(0, 2) === '//'
10
+ ? 'https:' + input
11
+ : input;
12
+ return await nodeFetch.default(processedInput, init);
13
+ }
@@ -1,5 +1,4 @@
1
1
  import * as BufferLayout from '@solana/buffer-layout';
2
- import {u64} from '@solana/buffer-layout-utils';
3
2
 
4
3
  import {
5
4
  encodeData,
@@ -13,6 +12,7 @@ import {PublicKey} from './publickey';
13
12
  import {SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY} from './sysvar';
14
13
  import {Transaction, TransactionInstruction} from './transaction';
15
14
  import {toBuffer} from './util/to-buffer';
15
+ import {u64} from './util/bigint';
16
16
 
17
17
  /**
18
18
  * Create account system transaction params
@@ -599,7 +599,6 @@ export class Transaction {
599
599
 
600
600
  const message = this._compile();
601
601
  this._partialSign(message, ...uniqueSigners);
602
- this._verifySignatures(message.serialize(), true);
603
602
  }
604
603
 
605
604
  /**
@@ -0,0 +1,43 @@
1
+ import {Buffer} from 'buffer';
2
+ import {blob, Layout} from '@solana/buffer-layout';
3
+ import {toBigIntLE, toBufferLE} from 'bigint-buffer';
4
+
5
+ interface EncodeDecode<T> {
6
+ decode(buffer: Buffer, offset?: number): T;
7
+ encode(src: T, buffer: Buffer, offset?: number): number;
8
+ }
9
+
10
+ const encodeDecode = <T>(layout: Layout<T>): EncodeDecode<T> => {
11
+ const decode = layout.decode.bind(layout);
12
+ const encode = layout.encode.bind(layout);
13
+ return {decode, encode};
14
+ };
15
+
16
+ const bigInt =
17
+ (length: number) =>
18
+ (property?: string): Layout<bigint> => {
19
+ const layout = blob(length, property);
20
+ const {encode, decode} = encodeDecode(layout);
21
+
22
+ const bigIntLayout = layout as Layout<unknown> as Layout<bigint>;
23
+
24
+ bigIntLayout.decode = (buffer: Buffer, offset: number) => {
25
+ const src = decode(buffer, offset);
26
+ return toBigIntLE(Buffer.from(src));
27
+ };
28
+
29
+ bigIntLayout.encode = (bigInt: bigint, buffer: Buffer, offset: number) => {
30
+ const src = toBufferLE(bigInt, length);
31
+ return encode(src, buffer, offset);
32
+ };
33
+
34
+ return bigIntLayout;
35
+ };
36
+
37
+ export const u64 = bigInt(8);
38
+
39
+ export const u128 = bigInt(16);
40
+
41
+ export const u192 = bigInt(24);
42
+
43
+ export const u256 = bigInt(32);
@@ -1,7 +1,7 @@
1
1
  import type {Buffer} from 'buffer';
2
2
 
3
3
  import {
4
- BlockheightBasedTransactionConfimationStrategy,
4
+ BlockheightBasedTransactionConfirmationStrategy,
5
5
  Connection,
6
6
  } from '../connection';
7
7
  import type {TransactionSignature} from '../transaction';
@@ -14,14 +14,14 @@ import type {ConfirmOptions} from '../connection';
14
14
  *
15
15
  * @param {Connection} connection
16
16
  * @param {Buffer} rawTransaction
17
- * @param {BlockheightBasedTransactionConfimationStrategy} confirmationStrategy
17
+ * @param {BlockheightBasedTransactionConfirmationStrategy} confirmationStrategy
18
18
  * @param {ConfirmOptions} [options]
19
19
  * @returns {Promise<TransactionSignature>}
20
20
  */
21
21
  export async function sendAndConfirmRawTransaction(
22
22
  connection: Connection,
23
23
  rawTransaction: Buffer,
24
- confirmationStrategy: BlockheightBasedTransactionConfimationStrategy,
24
+ confirmationStrategy: BlockheightBasedTransactionConfirmationStrategy,
25
25
  options?: ConfirmOptions,
26
26
  ): Promise<TransactionSignature>;
27
27
 
@@ -41,13 +41,13 @@ export async function sendAndConfirmRawTransaction(
41
41
  connection: Connection,
42
42
  rawTransaction: Buffer,
43
43
  confirmationStrategyOrConfirmOptions:
44
- | BlockheightBasedTransactionConfimationStrategy
44
+ | BlockheightBasedTransactionConfirmationStrategy
45
45
  | ConfirmOptions
46
46
  | undefined,
47
47
  maybeConfirmOptions?: ConfirmOptions,
48
48
  ): Promise<TransactionSignature> {
49
49
  let confirmationStrategy:
50
- | BlockheightBasedTransactionConfimationStrategy
50
+ | BlockheightBasedTransactionConfirmationStrategy
51
51
  | undefined;
52
52
  let options: ConfirmOptions | undefined;
53
53
  if (
@@ -58,7 +58,7 @@ export async function sendAndConfirmRawTransaction(
58
58
  )
59
59
  ) {
60
60
  confirmationStrategy =
61
- confirmationStrategyOrConfirmOptions as BlockheightBasedTransactionConfimationStrategy;
61
+ confirmationStrategyOrConfirmOptions as BlockheightBasedTransactionConfirmationStrategy;
62
62
  options = maybeConfirmOptions;
63
63
  } else {
64
64
  options = confirmationStrategyOrConfirmOptions as