@solana/web3.js 1.41.11 → 1.42.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
636
  export type BlockheightBasedTransactionConfimationStrategy = {
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>
@@ -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
  */
@@ -3354,9 +3352,20 @@ declare module '@solana/web3.js' {
3354
3352
  *
3355
3353
  * @param {Connection} connection
3356
3354
  * @param {Buffer} rawTransaction
3355
+ * @param {BlockheightBasedTransactionConfimationStrategy} confirmationStrategy
3357
3356
  * @param {ConfirmOptions} [options]
3358
3357
  * @returns {Promise<TransactionSignature>}
3359
3358
  */
3359
+ export function sendAndConfirmRawTransaction(
3360
+ connection: Connection,
3361
+ rawTransaction: Buffer,
3362
+ confirmationStrategy: BlockheightBasedTransactionConfimationStrategy,
3363
+ options?: ConfirmOptions,
3364
+ ): Promise<TransactionSignature>;
3365
+ /**
3366
+ * @deprecated Calling `sendAndConfirmRawTransaction()` without a `confirmationStrategy`
3367
+ * is no longer supported and will be removed in a future version.
3368
+ */
3360
3369
  export function sendAndConfirmRawTransaction(
3361
3370
  connection: Connection,
3362
3371
  rawTransaction: Buffer,
package/lib/index.esm.js CHANGED
@@ -5529,7 +5529,7 @@ class Connection {
5529
5529
  this._disableBlockhashCaching = false;
5530
5530
  this._pollingBlockhash = false;
5531
5531
  this._blockhashInfo = {
5532
- recentBlockhash: null,
5532
+ latestBlockhash: null,
5533
5533
  lastFetch: 0,
5534
5534
  transactionSignatures: [],
5535
5535
  simulatedSignatures: []
@@ -6492,7 +6492,7 @@ class Connection {
6492
6492
  }
6493
6493
  /**
6494
6494
  * Fetch the latest blockhash from the cluster
6495
- * @return {Promise<{blockhash: Blockhash, lastValidBlockHeight: number}>}
6495
+ * @return {Promise<BlockhashWithExpiryBlockHeight>}
6496
6496
  */
6497
6497
 
6498
6498
 
@@ -6506,7 +6506,7 @@ class Connection {
6506
6506
  }
6507
6507
  /**
6508
6508
  * Fetch the latest blockhash from the cluster
6509
- * @return {Promise<{blockhash: Blockhash, lastValidBlockHeight: number}>}
6509
+ * @return {Promise<BlockhashWithExpiryBlockHeight>}
6510
6510
  */
6511
6511
 
6512
6512
 
@@ -7089,7 +7089,7 @@ class Connection {
7089
7089
  */
7090
7090
 
7091
7091
 
7092
- async _recentBlockhash(disableCache) {
7092
+ async _blockhashWithExpiryBlockHeight(disableCache) {
7093
7093
  if (!disableCache) {
7094
7094
  // Wait for polling to finish
7095
7095
  while (this._pollingBlockhash) {
@@ -7100,8 +7100,8 @@ class Connection {
7100
7100
 
7101
7101
  const expired = timeSinceFetch >= BLOCKHASH_CACHE_TIMEOUT_MS;
7102
7102
 
7103
- if (this._blockhashInfo.recentBlockhash !== null && !expired) {
7104
- return this._blockhashInfo.recentBlockhash;
7103
+ if (this._blockhashInfo.latestBlockhash !== null && !expired) {
7104
+ return this._blockhashInfo.latestBlockhash;
7105
7105
  }
7106
7106
  }
7107
7107
 
@@ -7117,20 +7117,20 @@ class Connection {
7117
7117
 
7118
7118
  try {
7119
7119
  const startTime = Date.now();
7120
+ const cachedLatestBlockhash = this._blockhashInfo.latestBlockhash;
7121
+ const cachedBlockhash = cachedLatestBlockhash ? cachedLatestBlockhash.blockhash : null;
7120
7122
 
7121
7123
  for (let i = 0; i < 50; i++) {
7122
- const {
7123
- blockhash
7124
- } = await this.getRecentBlockhash('finalized');
7124
+ const latestBlockhash = await this.getLatestBlockhash('finalized');
7125
7125
 
7126
- if (this._blockhashInfo.recentBlockhash != blockhash) {
7126
+ if (cachedBlockhash !== latestBlockhash.blockhash) {
7127
7127
  this._blockhashInfo = {
7128
- recentBlockhash: blockhash,
7128
+ latestBlockhash,
7129
7129
  lastFetch: Date.now(),
7130
7130
  transactionSignatures: [],
7131
7131
  simulatedSignatures: []
7132
7132
  };
7133
- return blockhash;
7133
+ return latestBlockhash;
7134
7134
  } // Sleep for approximately half a slot
7135
7135
 
7136
7136
 
@@ -7152,13 +7152,11 @@ class Connection {
7152
7152
 
7153
7153
  if (transactionOrMessage instanceof Transaction) {
7154
7154
  let originalTx = transactionOrMessage;
7155
- transaction = new Transaction({
7156
- recentBlockhash: originalTx.recentBlockhash,
7157
- nonceInfo: originalTx.nonceInfo,
7158
- feePayer: originalTx.feePayer,
7159
- signatures: [...originalTx.signatures]
7160
- });
7155
+ transaction = new Transaction();
7156
+ transaction.feePayer = originalTx.feePayer;
7161
7157
  transaction.instructions = transactionOrMessage.instructions;
7158
+ transaction.nonceInfo = originalTx.nonceInfo;
7159
+ transaction.signatures = originalTx.signatures;
7162
7160
  } else {
7163
7161
  transaction = Transaction.populate(transactionOrMessage); // HACK: this function relies on mutating the populated transaction
7164
7162
 
@@ -7171,7 +7169,9 @@ class Connection {
7171
7169
  let disableCache = this._disableBlockhashCaching;
7172
7170
 
7173
7171
  for (;;) {
7174
- transaction.recentBlockhash = await this._recentBlockhash(disableCache);
7172
+ const latestBlockhash = await this._blockhashWithExpiryBlockHeight(disableCache);
7173
+ transaction.lastValidBlockHeight = latestBlockhash.lastValidBlockHeight;
7174
+ transaction.recentBlockhash = latestBlockhash.blockhash;
7175
7175
  if (!signers) break;
7176
7176
  transaction.sign(...signers);
7177
7177
 
@@ -7255,7 +7255,9 @@ class Connection {
7255
7255
  let disableCache = this._disableBlockhashCaching;
7256
7256
 
7257
7257
  for (;;) {
7258
- transaction.recentBlockhash = await this._recentBlockhash(disableCache);
7258
+ const latestBlockhash = await this._blockhashWithExpiryBlockHeight(disableCache);
7259
+ transaction.lastValidBlockHeight = latestBlockhash.lastValidBlockHeight;
7260
+ transaction.recentBlockhash = latestBlockhash.blockhash;
7259
7261
  transaction.sign(...signers);
7260
7262
 
7261
7263
  if (!transaction.signature) {
@@ -9694,16 +9696,36 @@ VoteProgram.space = 3731;
9694
9696
  *
9695
9697
  * @param {Connection} connection
9696
9698
  * @param {Buffer} rawTransaction
9699
+ * @param {BlockheightBasedTransactionConfimationStrategy} confirmationStrategy
9697
9700
  * @param {ConfirmOptions} [options]
9698
9701
  * @returns {Promise<TransactionSignature>}
9699
9702
  */
9700
- async function sendAndConfirmRawTransaction(connection, rawTransaction, options) {
9703
+
9704
+ /**
9705
+ * @deprecated Calling `sendAndConfirmRawTransaction()` without a `confirmationStrategy`
9706
+ * is no longer supported and will be removed in a future version.
9707
+ */
9708
+ // eslint-disable-next-line no-redeclare
9709
+ // eslint-disable-next-line no-redeclare
9710
+ async function sendAndConfirmRawTransaction(connection, rawTransaction, confirmationStrategyOrConfirmOptions, maybeConfirmOptions) {
9711
+ let confirmationStrategy;
9712
+ let options;
9713
+
9714
+ if (confirmationStrategyOrConfirmOptions && Object.prototype.hasOwnProperty.call(confirmationStrategyOrConfirmOptions, 'lastValidBlockHeight')) {
9715
+ confirmationStrategy = confirmationStrategyOrConfirmOptions;
9716
+ options = maybeConfirmOptions;
9717
+ } else {
9718
+ options = confirmationStrategyOrConfirmOptions;
9719
+ }
9720
+
9701
9721
  const sendOptions = options && {
9702
9722
  skipPreflight: options.skipPreflight,
9703
9723
  preflightCommitment: options.preflightCommitment || options.commitment
9704
9724
  };
9705
9725
  const signature = await connection.sendRawTransaction(rawTransaction, sendOptions);
9706
- const status = (await connection.confirmTransaction(signature, options && options.commitment)).value;
9726
+ const commitment = options && options.commitment;
9727
+ const confirmationPromise = confirmationStrategy ? connection.confirmTransaction(confirmationStrategy, commitment) : connection.confirmTransaction(signature, commitment);
9728
+ const status = (await confirmationPromise).value;
9707
9729
 
9708
9730
  if (status.err) {
9709
9731
  throw new Error(`Raw transaction ${signature} failed (${JSON.stringify(status)})`);