@solana/web3.js 1.41.10 → 1.42.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/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
@@ -5500,7 +5500,7 @@ class Connection {
5500
5500
  this._disableBlockhashCaching = false;
5501
5501
  this._pollingBlockhash = false;
5502
5502
  this._blockhashInfo = {
5503
- recentBlockhash: null,
5503
+ latestBlockhash: null,
5504
5504
  lastFetch: 0,
5505
5505
  transactionSignatures: [],
5506
5506
  simulatedSignatures: []
@@ -6463,7 +6463,7 @@ class Connection {
6463
6463
  }
6464
6464
  /**
6465
6465
  * Fetch the latest blockhash from the cluster
6466
- * @return {Promise<{blockhash: Blockhash, lastValidBlockHeight: number}>}
6466
+ * @return {Promise<BlockhashWithExpiryBlockHeight>}
6467
6467
  */
6468
6468
 
6469
6469
 
@@ -6477,7 +6477,7 @@ class Connection {
6477
6477
  }
6478
6478
  /**
6479
6479
  * Fetch the latest blockhash from the cluster
6480
- * @return {Promise<{blockhash: Blockhash, lastValidBlockHeight: number}>}
6480
+ * @return {Promise<BlockhashWithExpiryBlockHeight>}
6481
6481
  */
6482
6482
 
6483
6483
 
@@ -7060,7 +7060,7 @@ class Connection {
7060
7060
  */
7061
7061
 
7062
7062
 
7063
- async _recentBlockhash(disableCache) {
7063
+ async _blockhashWithExpiryBlockHeight(disableCache) {
7064
7064
  if (!disableCache) {
7065
7065
  // Wait for polling to finish
7066
7066
  while (this._pollingBlockhash) {
@@ -7071,8 +7071,8 @@ class Connection {
7071
7071
 
7072
7072
  const expired = timeSinceFetch >= BLOCKHASH_CACHE_TIMEOUT_MS;
7073
7073
 
7074
- if (this._blockhashInfo.recentBlockhash !== null && !expired) {
7075
- return this._blockhashInfo.recentBlockhash;
7074
+ if (this._blockhashInfo.latestBlockhash !== null && !expired) {
7075
+ return this._blockhashInfo.latestBlockhash;
7076
7076
  }
7077
7077
  }
7078
7078
 
@@ -7088,20 +7088,20 @@ class Connection {
7088
7088
 
7089
7089
  try {
7090
7090
  const startTime = Date.now();
7091
+ const cachedLatestBlockhash = this._blockhashInfo.latestBlockhash;
7092
+ const cachedBlockhash = cachedLatestBlockhash ? cachedLatestBlockhash.blockhash : null;
7091
7093
 
7092
7094
  for (let i = 0; i < 50; i++) {
7093
- const {
7094
- blockhash
7095
- } = await this.getRecentBlockhash('finalized');
7095
+ const latestBlockhash = await this.getLatestBlockhash('finalized');
7096
7096
 
7097
- if (this._blockhashInfo.recentBlockhash != blockhash) {
7097
+ if (cachedBlockhash !== latestBlockhash.blockhash) {
7098
7098
  this._blockhashInfo = {
7099
- recentBlockhash: blockhash,
7099
+ latestBlockhash,
7100
7100
  lastFetch: Date.now(),
7101
7101
  transactionSignatures: [],
7102
7102
  simulatedSignatures: []
7103
7103
  };
7104
- return blockhash;
7104
+ return latestBlockhash;
7105
7105
  } // Sleep for approximately half a slot
7106
7106
 
7107
7107
 
@@ -7123,13 +7123,11 @@ class Connection {
7123
7123
 
7124
7124
  if (transactionOrMessage instanceof Transaction) {
7125
7125
  let originalTx = transactionOrMessage;
7126
- transaction = new Transaction({
7127
- recentBlockhash: originalTx.recentBlockhash,
7128
- nonceInfo: originalTx.nonceInfo,
7129
- feePayer: originalTx.feePayer,
7130
- signatures: [...originalTx.signatures]
7131
- });
7126
+ transaction = new Transaction();
7127
+ transaction.feePayer = originalTx.feePayer;
7132
7128
  transaction.instructions = transactionOrMessage.instructions;
7129
+ transaction.nonceInfo = originalTx.nonceInfo;
7130
+ transaction.signatures = originalTx.signatures;
7133
7131
  } else {
7134
7132
  transaction = Transaction.populate(transactionOrMessage); // HACK: this function relies on mutating the populated transaction
7135
7133
 
@@ -7142,7 +7140,9 @@ class Connection {
7142
7140
  let disableCache = this._disableBlockhashCaching;
7143
7141
 
7144
7142
  for (;;) {
7145
- transaction.recentBlockhash = await this._recentBlockhash(disableCache);
7143
+ const latestBlockhash = await this._blockhashWithExpiryBlockHeight(disableCache);
7144
+ transaction.lastValidBlockHeight = latestBlockhash.lastValidBlockHeight;
7145
+ transaction.recentBlockhash = latestBlockhash.blockhash;
7146
7146
  if (!signers) break;
7147
7147
  transaction.sign(...signers);
7148
7148
 
@@ -7226,7 +7226,9 @@ class Connection {
7226
7226
  let disableCache = this._disableBlockhashCaching;
7227
7227
 
7228
7228
  for (;;) {
7229
- transaction.recentBlockhash = await this._recentBlockhash(disableCache);
7229
+ const latestBlockhash = await this._blockhashWithExpiryBlockHeight(disableCache);
7230
+ transaction.lastValidBlockHeight = latestBlockhash.lastValidBlockHeight;
7231
+ transaction.recentBlockhash = latestBlockhash.blockhash;
7230
7232
  transaction.sign(...signers);
7231
7233
 
7232
7234
  if (!transaction.signature) {
@@ -9667,16 +9669,36 @@ VoteProgram.space = 3731;
9667
9669
  *
9668
9670
  * @param {Connection} connection
9669
9671
  * @param {Buffer} rawTransaction
9672
+ * @param {BlockheightBasedTransactionConfimationStrategy} confirmationStrategy
9670
9673
  * @param {ConfirmOptions} [options]
9671
9674
  * @returns {Promise<TransactionSignature>}
9672
9675
  */
9673
- async function sendAndConfirmRawTransaction(connection, rawTransaction, options) {
9676
+
9677
+ /**
9678
+ * @deprecated Calling `sendAndConfirmRawTransaction()` without a `confirmationStrategy`
9679
+ * is no longer supported and will be removed in a future version.
9680
+ */
9681
+ // eslint-disable-next-line no-redeclare
9682
+ // eslint-disable-next-line no-redeclare
9683
+ async function sendAndConfirmRawTransaction(connection, rawTransaction, confirmationStrategyOrConfirmOptions, maybeConfirmOptions) {
9684
+ let confirmationStrategy;
9685
+ let options;
9686
+
9687
+ if (confirmationStrategyOrConfirmOptions && Object.prototype.hasOwnProperty.call(confirmationStrategyOrConfirmOptions, 'lastValidBlockHeight')) {
9688
+ confirmationStrategy = confirmationStrategyOrConfirmOptions;
9689
+ options = maybeConfirmOptions;
9690
+ } else {
9691
+ options = confirmationStrategyOrConfirmOptions;
9692
+ }
9693
+
9674
9694
  const sendOptions = options && {
9675
9695
  skipPreflight: options.skipPreflight,
9676
9696
  preflightCommitment: options.preflightCommitment || options.commitment
9677
9697
  };
9678
9698
  const signature = await connection.sendRawTransaction(rawTransaction, sendOptions);
9679
- const status = (await connection.confirmTransaction(signature, options && options.commitment)).value;
9699
+ const commitment = options && options.commitment;
9700
+ const confirmationPromise = confirmationStrategy ? connection.confirmTransaction(confirmationStrategy, commitment) : connection.confirmTransaction(signature, commitment);
9701
+ const status = (await confirmationPromise).value;
9680
9702
 
9681
9703
  if (status.err) {
9682
9704
  throw new Error(`Raw transaction ${signature} failed (${JSON.stringify(status)})`);