@solana/web3.js 1.41.10 → 1.43.1

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/lib/index.d.ts CHANGED
@@ -625,15 +625,17 @@ declare module '@solana/web3.js' {
625
625
  /** response value */
626
626
  value: T;
627
627
  };
628
+ export type BlockhashWithExpiryBlockHeight = Readonly<{
629
+ blockhash: Blockhash;
630
+ lastValidBlockHeight: number;
631
+ }>;
628
632
  /**
629
633
  * A strategy for confirming transactions that uses the last valid
630
634
  * block height for a given blockhash to check for transaction expiration.
631
635
  */
632
- export type BlockheightBasedTransactionConfimationStrategy = {
636
+ export type BlockheightBasedTransactionConfirmationStrategy = {
633
637
  signature: TransactionSignature;
634
- blockhash: Blockhash;
635
- lastValidBlockHeight: number;
636
- };
638
+ } & BlockhashWithExpiryBlockHeight;
637
639
  /**
638
640
  * The level of commitment desired when querying state
639
641
  * <pre>
@@ -1657,7 +1659,7 @@ declare module '@solana/web3.js' {
1657
1659
  }>
1658
1660
  >;
1659
1661
  confirmTransaction(
1660
- strategy: BlockheightBasedTransactionConfimationStrategy,
1662
+ strategy: BlockheightBasedTransactionConfirmationStrategy,
1661
1663
  commitment?: Commitment,
1662
1664
  ): Promise<RpcResponseAndContext<SignatureResult>>;
1663
1665
  /** @deprecated Instead, call `confirmTransaction` using a `TransactionConfirmationConfig` */
@@ -1790,22 +1792,18 @@ declare module '@solana/web3.js' {
1790
1792
  }>;
1791
1793
  /**
1792
1794
  * Fetch the latest blockhash from the cluster
1793
- * @return {Promise<{blockhash: Blockhash, lastValidBlockHeight: number}>}
1795
+ * @return {Promise<BlockhashWithExpiryBlockHeight>}
1794
1796
  */
1795
- getLatestBlockhash(commitment?: Commitment): Promise<{
1796
- blockhash: Blockhash;
1797
- lastValidBlockHeight: number;
1798
- }>;
1797
+ getLatestBlockhash(
1798
+ commitment?: Commitment,
1799
+ ): Promise<BlockhashWithExpiryBlockHeight>;
1799
1800
  /**
1800
1801
  * Fetch the latest blockhash from the cluster
1801
- * @return {Promise<{blockhash: Blockhash, lastValidBlockHeight: number}>}
1802
+ * @return {Promise<BlockhashWithExpiryBlockHeight>}
1802
1803
  */
1803
- getLatestBlockhashAndContext(commitment?: Commitment): Promise<
1804
- RpcResponseAndContext<{
1805
- blockhash: Blockhash;
1806
- lastValidBlockHeight: number;
1807
- }>
1808
- >;
1804
+ getLatestBlockhashAndContext(
1805
+ commitment?: Commitment,
1806
+ ): Promise<RpcResponseAndContext<BlockhashWithExpiryBlockHeight>>;
1809
1807
  /**
1810
1808
  * Fetch the node version
1811
1809
  */
@@ -2228,20 +2226,34 @@ declare module '@solana/web3.js' {
2228
2226
  static decodeRequestHeapFrame(
2229
2227
  instruction: TransactionInstruction,
2230
2228
  ): RequestHeapFrameParams;
2229
+ /**
2230
+ * Decode set compute unit limit compute budget instruction and retrieve the instruction params.
2231
+ */
2232
+ static decodeSetComputeUnitLimit(
2233
+ instruction: TransactionInstruction,
2234
+ ): SetComputeUnitLimitParams;
2235
+ /**
2236
+ * Decode set compute unit price compute budget instruction and retrieve the instruction params.
2237
+ */
2238
+ static decodeSetComputeUnitPrice(
2239
+ instruction: TransactionInstruction,
2240
+ ): SetComputeUnitPriceParams;
2231
2241
  }
2232
2242
  /**
2233
2243
  * An enumeration of valid ComputeBudgetInstructionType's
2234
2244
  */
2235
2245
  export type ComputeBudgetInstructionType =
2236
2246
  | 'RequestUnits'
2237
- | 'RequestHeapFrame';
2247
+ | 'RequestHeapFrame'
2248
+ | 'SetComputeUnitLimit'
2249
+ | 'SetComputeUnitPrice';
2238
2250
  /**
2239
2251
  * Request units instruction params
2240
2252
  */
2241
2253
  interface RequestUnitsParams {
2242
2254
  /** Units to request for transaction-wide compute */
2243
2255
  units: number;
2244
- /** Additional fee to pay */
2256
+ /** Prioritization fee lamports */
2245
2257
  additionalFee: number;
2246
2258
  }
2247
2259
  /**
@@ -2251,6 +2263,20 @@ declare module '@solana/web3.js' {
2251
2263
  /** Requested transaction-wide program heap size in bytes. Must be multiple of 1024. Applies to each program, including CPIs. */
2252
2264
  bytes: number;
2253
2265
  };
2266
+ /**
2267
+ * Set compute unit limit instruction params
2268
+ */
2269
+ interface SetComputeUnitLimitParams {
2270
+ /** Transaction-wide compute unit limit */
2271
+ units: number;
2272
+ }
2273
+ /**
2274
+ * Set compute unit price instruction params
2275
+ */
2276
+ interface SetComputeUnitPriceParams {
2277
+ /** Transaction compute unit price used for prioritization fees */
2278
+ microLamports: number | bigint;
2279
+ }
2254
2280
  /**
2255
2281
  * Factory class for transaction instructions to interact with the Compute Budget program
2256
2282
  */
@@ -2263,6 +2289,12 @@ declare module '@solana/web3.js' {
2263
2289
  static requestHeapFrame(
2264
2290
  params: RequestHeapFrameParams,
2265
2291
  ): TransactionInstruction;
2292
+ static setComputeUnitLimit(
2293
+ params: SetComputeUnitLimitParams,
2294
+ ): TransactionInstruction;
2295
+ static setComputeUnitPrice(
2296
+ params: SetComputeUnitPriceParams,
2297
+ ): TransactionInstruction;
2266
2298
  }
2267
2299
 
2268
2300
  /**
@@ -3354,9 +3386,20 @@ declare module '@solana/web3.js' {
3354
3386
  *
3355
3387
  * @param {Connection} connection
3356
3388
  * @param {Buffer} rawTransaction
3389
+ * @param {BlockheightBasedTransactionConfirmationStrategy} confirmationStrategy
3357
3390
  * @param {ConfirmOptions} [options]
3358
3391
  * @returns {Promise<TransactionSignature>}
3359
3392
  */
3393
+ export function sendAndConfirmRawTransaction(
3394
+ connection: Connection,
3395
+ rawTransaction: Buffer,
3396
+ confirmationStrategy: BlockheightBasedTransactionConfirmationStrategy,
3397
+ options?: ConfirmOptions,
3398
+ ): Promise<TransactionSignature>;
3399
+ /**
3400
+ * @deprecated Calling `sendAndConfirmRawTransaction()` without a `confirmationStrategy`
3401
+ * is no longer supported and will be removed in a future version.
3402
+ */
3360
3403
  export function sendAndConfirmRawTransaction(
3361
3404
  connection: Connection,
3362
3405
  rawTransaction: Buffer,
package/lib/index.esm.js CHANGED
@@ -4,7 +4,8 @@ import BN from 'bn.js';
4
4
  import bs58 from 'bs58';
5
5
  import { serialize, deserialize, deserializeUnchecked } from 'borsh';
6
6
  import * as BufferLayout from '@solana/buffer-layout';
7
- import { u64 } from '@solana/buffer-layout-utils';
7
+ import { blob } from '@solana/buffer-layout';
8
+ import { toBigIntLE, toBufferLE } from 'bigint-buffer';
8
9
  import crossFetch from 'cross-fetch';
9
10
  import { coerce, instance, string, tuple, literal, unknown, union, type, optional, any, number, array, nullable, create, boolean, record, assert as assert$7 } from 'superstruct';
10
11
  import { Client } from 'rpc-websockets';
@@ -2805,8 +2806,6 @@ class Transaction {
2805
2806
  const message = this._compile();
2806
2807
 
2807
2808
  this._partialSign(message, ...uniqueSigners);
2808
-
2809
- this._verifySignatures(message.serialize(), true);
2810
2809
  }
2811
2810
  /**
2812
2811
  * Partially sign a transaction with the specified accounts. All accounts must
@@ -3186,6 +3185,38 @@ class NonceAccount {
3186
3185
 
3187
3186
  }
3188
3187
 
3188
+ const encodeDecode = layout => {
3189
+ const decode = layout.decode.bind(layout);
3190
+ const encode = layout.encode.bind(layout);
3191
+ return {
3192
+ decode,
3193
+ encode
3194
+ };
3195
+ };
3196
+
3197
+ const bigInt = length => property => {
3198
+ const layout = blob(length, property);
3199
+ const {
3200
+ encode,
3201
+ decode
3202
+ } = encodeDecode(layout);
3203
+ const bigIntLayout = layout;
3204
+
3205
+ bigIntLayout.decode = (buffer, offset) => {
3206
+ const src = decode(buffer, offset);
3207
+ return toBigIntLE(Buffer.from(src));
3208
+ };
3209
+
3210
+ bigIntLayout.encode = (bigInt, buffer, offset) => {
3211
+ const src = toBufferLE(bigInt, length);
3212
+ return encode(src, buffer, offset);
3213
+ };
3214
+
3215
+ return bigIntLayout;
3216
+ };
3217
+
3218
+ const u64 = bigInt(8);
3219
+
3189
3220
  /**
3190
3221
  * Create account system transaction params
3191
3222
  */
@@ -4164,6 +4195,34 @@ class ComputeBudgetInstruction {
4164
4195
  bytes
4165
4196
  };
4166
4197
  }
4198
+ /**
4199
+ * Decode set compute unit limit compute budget instruction and retrieve the instruction params.
4200
+ */
4201
+
4202
+
4203
+ static decodeSetComputeUnitLimit(instruction) {
4204
+ this.checkProgramId(instruction.programId);
4205
+ const {
4206
+ units
4207
+ } = decodeData(COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.SetComputeUnitLimit, instruction.data);
4208
+ return {
4209
+ units
4210
+ };
4211
+ }
4212
+ /**
4213
+ * Decode set compute unit price compute budget instruction and retrieve the instruction params.
4214
+ */
4215
+
4216
+
4217
+ static decodeSetComputeUnitPrice(instruction) {
4218
+ this.checkProgramId(instruction.programId);
4219
+ const {
4220
+ microLamports
4221
+ } = decodeData(COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.SetComputeUnitPrice, instruction.data);
4222
+ return {
4223
+ microLamports
4224
+ };
4225
+ }
4167
4226
  /**
4168
4227
  * @internal
4169
4228
  */
@@ -4192,6 +4251,14 @@ const COMPUTE_BUDGET_INSTRUCTION_LAYOUTS = Object.freeze({
4192
4251
  RequestHeapFrame: {
4193
4252
  index: 1,
4194
4253
  layout: BufferLayout.struct([BufferLayout.u8('instruction'), BufferLayout.u32('bytes')])
4254
+ },
4255
+ SetComputeUnitLimit: {
4256
+ index: 2,
4257
+ layout: BufferLayout.struct([BufferLayout.u8('instruction'), BufferLayout.u32('units')])
4258
+ },
4259
+ SetComputeUnitPrice: {
4260
+ index: 3,
4261
+ layout: BufferLayout.struct([BufferLayout.u8('instruction'), u64('microLamports')])
4195
4262
  }
4196
4263
  });
4197
4264
  /**
@@ -4228,6 +4295,28 @@ class ComputeBudgetProgram {
4228
4295
  });
4229
4296
  }
4230
4297
 
4298
+ static setComputeUnitLimit(params) {
4299
+ const type = COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.SetComputeUnitLimit;
4300
+ const data = encodeData(type, params);
4301
+ return new TransactionInstruction({
4302
+ keys: [],
4303
+ programId: this.programId,
4304
+ data
4305
+ });
4306
+ }
4307
+
4308
+ static setComputeUnitPrice(params) {
4309
+ const type = COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.SetComputeUnitPrice;
4310
+ const data = encodeData(type, {
4311
+ microLamports: BigInt(params.microLamports)
4312
+ });
4313
+ return new TransactionInstruction({
4314
+ keys: [],
4315
+ programId: this.programId,
4316
+ data
4317
+ });
4318
+ }
4319
+
4231
4320
  }
4232
4321
  ComputeBudgetProgram.programId = new PublicKey('ComputeBudget111111111111111111111111111111');
4233
4322
 
@@ -5500,7 +5589,7 @@ class Connection {
5500
5589
  this._disableBlockhashCaching = false;
5501
5590
  this._pollingBlockhash = false;
5502
5591
  this._blockhashInfo = {
5503
- recentBlockhash: null,
5592
+ latestBlockhash: null,
5504
5593
  lastFetch: 0,
5505
5594
  transactionSignatures: [],
5506
5595
  simulatedSignatures: []
@@ -6463,7 +6552,7 @@ class Connection {
6463
6552
  }
6464
6553
  /**
6465
6554
  * Fetch the latest blockhash from the cluster
6466
- * @return {Promise<{blockhash: Blockhash, lastValidBlockHeight: number}>}
6555
+ * @return {Promise<BlockhashWithExpiryBlockHeight>}
6467
6556
  */
6468
6557
 
6469
6558
 
@@ -6477,7 +6566,7 @@ class Connection {
6477
6566
  }
6478
6567
  /**
6479
6568
  * Fetch the latest blockhash from the cluster
6480
- * @return {Promise<{blockhash: Blockhash, lastValidBlockHeight: number}>}
6569
+ * @return {Promise<BlockhashWithExpiryBlockHeight>}
6481
6570
  */
6482
6571
 
6483
6572
 
@@ -7060,7 +7149,7 @@ class Connection {
7060
7149
  */
7061
7150
 
7062
7151
 
7063
- async _recentBlockhash(disableCache) {
7152
+ async _blockhashWithExpiryBlockHeight(disableCache) {
7064
7153
  if (!disableCache) {
7065
7154
  // Wait for polling to finish
7066
7155
  while (this._pollingBlockhash) {
@@ -7071,8 +7160,8 @@ class Connection {
7071
7160
 
7072
7161
  const expired = timeSinceFetch >= BLOCKHASH_CACHE_TIMEOUT_MS;
7073
7162
 
7074
- if (this._blockhashInfo.recentBlockhash !== null && !expired) {
7075
- return this._blockhashInfo.recentBlockhash;
7163
+ if (this._blockhashInfo.latestBlockhash !== null && !expired) {
7164
+ return this._blockhashInfo.latestBlockhash;
7076
7165
  }
7077
7166
  }
7078
7167
 
@@ -7088,20 +7177,20 @@ class Connection {
7088
7177
 
7089
7178
  try {
7090
7179
  const startTime = Date.now();
7180
+ const cachedLatestBlockhash = this._blockhashInfo.latestBlockhash;
7181
+ const cachedBlockhash = cachedLatestBlockhash ? cachedLatestBlockhash.blockhash : null;
7091
7182
 
7092
7183
  for (let i = 0; i < 50; i++) {
7093
- const {
7094
- blockhash
7095
- } = await this.getRecentBlockhash('finalized');
7184
+ const latestBlockhash = await this.getLatestBlockhash('finalized');
7096
7185
 
7097
- if (this._blockhashInfo.recentBlockhash != blockhash) {
7186
+ if (cachedBlockhash !== latestBlockhash.blockhash) {
7098
7187
  this._blockhashInfo = {
7099
- recentBlockhash: blockhash,
7188
+ latestBlockhash,
7100
7189
  lastFetch: Date.now(),
7101
7190
  transactionSignatures: [],
7102
7191
  simulatedSignatures: []
7103
7192
  };
7104
- return blockhash;
7193
+ return latestBlockhash;
7105
7194
  } // Sleep for approximately half a slot
7106
7195
 
7107
7196
 
@@ -7123,13 +7212,11 @@ class Connection {
7123
7212
 
7124
7213
  if (transactionOrMessage instanceof Transaction) {
7125
7214
  let originalTx = transactionOrMessage;
7126
- transaction = new Transaction({
7127
- recentBlockhash: originalTx.recentBlockhash,
7128
- nonceInfo: originalTx.nonceInfo,
7129
- feePayer: originalTx.feePayer,
7130
- signatures: [...originalTx.signatures]
7131
- });
7215
+ transaction = new Transaction();
7216
+ transaction.feePayer = originalTx.feePayer;
7132
7217
  transaction.instructions = transactionOrMessage.instructions;
7218
+ transaction.nonceInfo = originalTx.nonceInfo;
7219
+ transaction.signatures = originalTx.signatures;
7133
7220
  } else {
7134
7221
  transaction = Transaction.populate(transactionOrMessage); // HACK: this function relies on mutating the populated transaction
7135
7222
 
@@ -7142,7 +7229,9 @@ class Connection {
7142
7229
  let disableCache = this._disableBlockhashCaching;
7143
7230
 
7144
7231
  for (;;) {
7145
- transaction.recentBlockhash = await this._recentBlockhash(disableCache);
7232
+ const latestBlockhash = await this._blockhashWithExpiryBlockHeight(disableCache);
7233
+ transaction.lastValidBlockHeight = latestBlockhash.lastValidBlockHeight;
7234
+ transaction.recentBlockhash = latestBlockhash.blockhash;
7146
7235
  if (!signers) break;
7147
7236
  transaction.sign(...signers);
7148
7237
 
@@ -7226,7 +7315,9 @@ class Connection {
7226
7315
  let disableCache = this._disableBlockhashCaching;
7227
7316
 
7228
7317
  for (;;) {
7229
- transaction.recentBlockhash = await this._recentBlockhash(disableCache);
7318
+ const latestBlockhash = await this._blockhashWithExpiryBlockHeight(disableCache);
7319
+ transaction.lastValidBlockHeight = latestBlockhash.lastValidBlockHeight;
7320
+ transaction.recentBlockhash = latestBlockhash.blockhash;
7230
7321
  transaction.sign(...signers);
7231
7322
 
7232
7323
  if (!transaction.signature) {
@@ -9667,16 +9758,36 @@ VoteProgram.space = 3731;
9667
9758
  *
9668
9759
  * @param {Connection} connection
9669
9760
  * @param {Buffer} rawTransaction
9761
+ * @param {BlockheightBasedTransactionConfirmationStrategy} confirmationStrategy
9670
9762
  * @param {ConfirmOptions} [options]
9671
9763
  * @returns {Promise<TransactionSignature>}
9672
9764
  */
9673
- async function sendAndConfirmRawTransaction(connection, rawTransaction, options) {
9765
+
9766
+ /**
9767
+ * @deprecated Calling `sendAndConfirmRawTransaction()` without a `confirmationStrategy`
9768
+ * is no longer supported and will be removed in a future version.
9769
+ */
9770
+ // eslint-disable-next-line no-redeclare
9771
+ // eslint-disable-next-line no-redeclare
9772
+ async function sendAndConfirmRawTransaction(connection, rawTransaction, confirmationStrategyOrConfirmOptions, maybeConfirmOptions) {
9773
+ let confirmationStrategy;
9774
+ let options;
9775
+
9776
+ if (confirmationStrategyOrConfirmOptions && Object.prototype.hasOwnProperty.call(confirmationStrategyOrConfirmOptions, 'lastValidBlockHeight')) {
9777
+ confirmationStrategy = confirmationStrategyOrConfirmOptions;
9778
+ options = maybeConfirmOptions;
9779
+ } else {
9780
+ options = confirmationStrategyOrConfirmOptions;
9781
+ }
9782
+
9674
9783
  const sendOptions = options && {
9675
9784
  skipPreflight: options.skipPreflight,
9676
9785
  preflightCommitment: options.preflightCommitment || options.commitment
9677
9786
  };
9678
9787
  const signature = await connection.sendRawTransaction(rawTransaction, sendOptions);
9679
- const status = (await connection.confirmTransaction(signature, options && options.commitment)).value;
9788
+ const commitment = options && options.commitment;
9789
+ const confirmationPromise = confirmationStrategy ? connection.confirmTransaction(confirmationStrategy, commitment) : connection.confirmTransaction(signature, commitment);
9790
+ const status = (await confirmationPromise).value;
9680
9791
 
9681
9792
  if (status.err) {
9682
9793
  throw new Error(`Raw transaction ${signature} failed (${JSON.stringify(status)})`);