@solana/web3.js 1.44.2 → 1.46.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.esm.js CHANGED
@@ -2534,24 +2534,27 @@ class Transaction {
2534
2534
  return this._message;
2535
2535
  }
2536
2536
 
2537
- const {
2538
- nonceInfo
2539
- } = this;
2537
+ let recentBlockhash;
2538
+ let instructions;
2540
2539
 
2541
- if (nonceInfo && this.instructions[0] != nonceInfo.nonceInstruction) {
2542
- this.recentBlockhash = nonceInfo.nonce;
2543
- this.instructions.unshift(nonceInfo.nonceInstruction);
2544
- }
2540
+ if (this.nonceInfo) {
2541
+ recentBlockhash = this.nonceInfo.nonce;
2545
2542
 
2546
- const {
2547
- recentBlockhash
2548
- } = this;
2543
+ if (this.instructions[0] != this.nonceInfo.nonceInstruction) {
2544
+ instructions = [this.nonceInfo.nonceInstruction, ...this.instructions];
2545
+ } else {
2546
+ instructions = this.instructions;
2547
+ }
2548
+ } else {
2549
+ recentBlockhash = this.recentBlockhash;
2550
+ instructions = this.instructions;
2551
+ }
2549
2552
 
2550
2553
  if (!recentBlockhash) {
2551
2554
  throw new Error('Transaction recentBlockhash required');
2552
2555
  }
2553
2556
 
2554
- if (this.instructions.length < 1) {
2557
+ if (instructions.length < 1) {
2555
2558
  console.warn('No instructions provided');
2556
2559
  }
2557
2560
 
@@ -2566,15 +2569,15 @@ class Transaction {
2566
2569
  throw new Error('Transaction fee payer required');
2567
2570
  }
2568
2571
 
2569
- for (let i = 0; i < this.instructions.length; i++) {
2570
- if (this.instructions[i].programId === undefined) {
2572
+ for (let i = 0; i < instructions.length; i++) {
2573
+ if (instructions[i].programId === undefined) {
2571
2574
  throw new Error(`Transaction instruction index ${i} has undefined program id`);
2572
2575
  }
2573
2576
  }
2574
2577
 
2575
2578
  const programIds = [];
2576
2579
  const accountMetas = [];
2577
- this.instructions.forEach(instruction => {
2580
+ instructions.forEach(instruction => {
2578
2581
  instruction.keys.forEach(accountMeta => {
2579
2582
  accountMetas.push({ ...accountMeta
2580
2583
  });
@@ -2684,7 +2687,7 @@ class Transaction {
2684
2687
  }
2685
2688
  });
2686
2689
  const accountKeys = signedKeys.concat(unsignedKeys);
2687
- const instructions = this.instructions.map(instruction => {
2690
+ const compiledInstructions = instructions.map(instruction => {
2688
2691
  const {
2689
2692
  data,
2690
2693
  programId
@@ -2695,7 +2698,7 @@ class Transaction {
2695
2698
  data: bs58.encode(data)
2696
2699
  };
2697
2700
  });
2698
- instructions.forEach(instruction => {
2701
+ compiledInstructions.forEach(instruction => {
2699
2702
  assert(instruction.programIdIndex >= 0);
2700
2703
  instruction.accounts.forEach(keyIndex => assert(keyIndex >= 0));
2701
2704
  });
@@ -2707,7 +2710,7 @@ class Transaction {
2707
2710
  },
2708
2711
  accountKeys,
2709
2712
  recentBlockhash,
2710
- instructions
2713
+ instructions: compiledInstructions
2711
2714
  });
2712
2715
  }
2713
2716
  /**
@@ -3094,7 +3097,8 @@ async function sendAndConfirmTransaction(connection, transaction, signers, optio
3094
3097
  const sendOptions = options && {
3095
3098
  skipPreflight: options.skipPreflight,
3096
3099
  preflightCommitment: options.preflightCommitment || options.commitment,
3097
- maxRetries: options.maxRetries
3100
+ maxRetries: options.maxRetries,
3101
+ minContextSlot: options.minContextSlot
3098
3102
  };
3099
3103
  const signature = await connection.sendTransaction(transaction, signers, sendOptions);
3100
3104
  const status = transaction.recentBlockhash != null && transaction.lastValidBlockHeight != null ? (await connection.confirmTransaction({
@@ -4641,6 +4645,7 @@ function makeWebsocketUrl(endpoint) {
4641
4645
  return url.toString();
4642
4646
  }
4643
4647
 
4648
+ var _process$env$npm_pack;
4644
4649
  const PublicKeyFromString = coerce(instance(PublicKey), string(), value => new PublicKey(value));
4645
4650
  const RawAccountDataResult = tuple([string(), literal('base64')]);
4646
4651
  const BufferFromRawAccountData = coerce(instance(Buffer), RawAccountDataResult, value => Buffer.from(value[0], 'base64'));
@@ -4657,9 +4662,32 @@ const BLOCKHASH_CACHE_TIMEOUT_MS = 30 * 1000;
4657
4662
  * https://gist.github.com/steveluscher/c057eca81d479ef705cdb53162f9971d
4658
4663
  */
4659
4664
 
4665
+ /** @internal */
4666
+ function extractCommitmentFromConfig(commitmentOrConfig) {
4667
+ let commitment;
4668
+ let config;
4669
+
4670
+ if (typeof commitmentOrConfig === 'string') {
4671
+ commitment = commitmentOrConfig;
4672
+ } else if (commitmentOrConfig) {
4673
+ const {
4674
+ commitment: specifiedCommitment,
4675
+ ...specifiedConfig
4676
+ } = commitmentOrConfig;
4677
+ commitment = specifiedCommitment;
4678
+ config = specifiedConfig;
4679
+ }
4680
+
4681
+ return {
4682
+ commitment,
4683
+ config
4684
+ };
4685
+ }
4660
4686
  /**
4661
4687
  * @internal
4662
4688
  */
4689
+
4690
+
4663
4691
  function createRpcResult(result) {
4664
4692
  return union([type({
4665
4693
  jsonrpc: literal('2.0'),
@@ -4856,7 +4884,7 @@ function createRpcClient(url, useHttps, httpHeaders, customFetch, fetchMiddlewar
4856
4884
  agent,
4857
4885
  headers: Object.assign({
4858
4886
  'Content-Type': 'application/json'
4859
- }, httpHeaders || {})
4887
+ }, httpHeaders || {}, COMMON_HTTP_HEADERS)
4860
4888
  };
4861
4889
 
4862
4890
  try {
@@ -5530,9 +5558,14 @@ const LogsNotificationResult = type({
5530
5558
  * Filter for log subscriptions.
5531
5559
  */
5532
5560
 
5561
+ /** @internal */
5562
+ const COMMON_HTTP_HEADERS = {
5563
+ 'solana-client': `js/${(_process$env$npm_pack = "0.0.0-development") !== null && _process$env$npm_pack !== void 0 ? _process$env$npm_pack : 'UNKNOWN'}`
5564
+ };
5533
5565
  /**
5534
5566
  * A connection to a fullnode JSON RPC endpoint
5535
5567
  */
5568
+
5536
5569
  class Connection {
5537
5570
  /** @internal */
5538
5571
 
@@ -5697,8 +5730,16 @@ class Connection {
5697
5730
  */
5698
5731
 
5699
5732
 
5700
- async getBalanceAndContext(publicKey, commitment) {
5701
- const args = this._buildArgs([publicKey.toBase58()], commitment);
5733
+ async getBalanceAndContext(publicKey, commitmentOrConfig) {
5734
+ /** @internal */
5735
+ const {
5736
+ commitment,
5737
+ config
5738
+ } = extractCommitmentFromConfig(commitmentOrConfig);
5739
+
5740
+ const args = this._buildArgs([publicKey.toBase58()], commitment, undefined
5741
+ /* encoding */
5742
+ , config);
5702
5743
 
5703
5744
  const unsafeRes = await this._rpcRequest('getBalance', args);
5704
5745
  const res = create(unsafeRes, jsonRpcResultAndContext(number()));
@@ -5714,8 +5755,8 @@ class Connection {
5714
5755
  */
5715
5756
 
5716
5757
 
5717
- async getBalance(publicKey, commitment) {
5718
- return await this.getBalanceAndContext(publicKey, commitment).then(x => x.value).catch(e => {
5758
+ async getBalance(publicKey, commitmentOrConfig) {
5759
+ return await this.getBalanceAndContext(publicKey, commitmentOrConfig).then(x => x.value).catch(e => {
5719
5760
  throw new Error('failed to get balance of account ' + publicKey.toBase58() + ': ' + e);
5720
5761
  });
5721
5762
  }
@@ -5837,7 +5878,11 @@ class Connection {
5837
5878
  */
5838
5879
 
5839
5880
 
5840
- async getTokenAccountsByOwner(ownerAddress, filter, commitment) {
5881
+ async getTokenAccountsByOwner(ownerAddress, filter, commitmentOrConfig) {
5882
+ const {
5883
+ commitment,
5884
+ config
5885
+ } = extractCommitmentFromConfig(commitmentOrConfig);
5841
5886
  let _args = [ownerAddress.toBase58()];
5842
5887
 
5843
5888
  if ('mint' in filter) {
@@ -5850,7 +5895,7 @@ class Connection {
5850
5895
  });
5851
5896
  }
5852
5897
 
5853
- const args = this._buildArgs(_args, commitment, 'base64');
5898
+ const args = this._buildArgs(_args, commitment, 'base64', config);
5854
5899
 
5855
5900
  const unsafeRes = await this._rpcRequest('getTokenAccountsByOwner', args);
5856
5901
  const res = create(unsafeRes, GetTokenAccountsByOwner);
@@ -5934,8 +5979,13 @@ class Connection {
5934
5979
  */
5935
5980
 
5936
5981
 
5937
- async getAccountInfoAndContext(publicKey, commitment) {
5938
- const args = this._buildArgs([publicKey.toBase58()], commitment, 'base64');
5982
+ async getAccountInfoAndContext(publicKey, commitmentOrConfig) {
5983
+ const {
5984
+ commitment,
5985
+ config
5986
+ } = extractCommitmentFromConfig(commitmentOrConfig);
5987
+
5988
+ const args = this._buildArgs([publicKey.toBase58()], commitment, 'base64', config);
5939
5989
 
5940
5990
  const unsafeRes = await this._rpcRequest('getAccountInfo', args);
5941
5991
  const res = create(unsafeRes, jsonRpcResultAndContext(nullable(AccountInfoResult)));
@@ -5968,9 +6018,9 @@ class Connection {
5968
6018
  */
5969
6019
 
5970
6020
 
5971
- async getAccountInfo(publicKey, commitment) {
6021
+ async getAccountInfo(publicKey, commitmentOrConfig) {
5972
6022
  try {
5973
- const res = await this.getAccountInfoAndContext(publicKey, commitment);
6023
+ const res = await this.getAccountInfoAndContext(publicKey, commitmentOrConfig);
5974
6024
  return res.value;
5975
6025
  } catch (e) {
5976
6026
  throw new Error('failed to get info about account ' + publicKey.toBase58() + ': ' + e);
@@ -5981,10 +6031,14 @@ class Connection {
5981
6031
  */
5982
6032
 
5983
6033
 
5984
- async getMultipleAccountsInfoAndContext(publicKeys, commitment) {
6034
+ async getMultipleAccountsInfoAndContext(publicKeys, commitmentOrConfig) {
6035
+ const {
6036
+ commitment,
6037
+ config
6038
+ } = extractCommitmentFromConfig(commitmentOrConfig);
5985
6039
  const keys = publicKeys.map(key => key.toBase58());
5986
6040
 
5987
- const args = this._buildArgs([keys], commitment, 'base64');
6041
+ const args = this._buildArgs([keys], commitment, 'base64', config);
5988
6042
 
5989
6043
  const unsafeRes = await this._rpcRequest('getMultipleAccounts', args);
5990
6044
  const res = create(unsafeRes, jsonRpcResultAndContext(array(nullable(AccountInfoResult))));
@@ -6000,8 +6054,8 @@ class Connection {
6000
6054
  */
6001
6055
 
6002
6056
 
6003
- async getMultipleAccountsInfo(publicKeys, commitment) {
6004
- const res = await this.getMultipleAccountsInfoAndContext(publicKeys, commitment);
6057
+ async getMultipleAccountsInfo(publicKeys, commitmentOrConfig) {
6058
+ const res = await this.getMultipleAccountsInfoAndContext(publicKeys, commitmentOrConfig);
6005
6059
  return res.value;
6006
6060
  }
6007
6061
  /**
@@ -6009,10 +6063,17 @@ class Connection {
6009
6063
  */
6010
6064
 
6011
6065
 
6012
- async getStakeActivation(publicKey, commitment, epoch) {
6013
- const args = this._buildArgs([publicKey.toBase58()], commitment, undefined, epoch !== undefined ? {
6014
- epoch
6015
- } : undefined);
6066
+ async getStakeActivation(publicKey, commitmentOrConfig, epoch) {
6067
+ const {
6068
+ commitment,
6069
+ config
6070
+ } = extractCommitmentFromConfig(commitmentOrConfig);
6071
+
6072
+ const args = this._buildArgs([publicKey.toBase58()], commitment, undefined
6073
+ /* encoding */
6074
+ , { ...config,
6075
+ epoch: epoch != null ? epoch : config === null || config === void 0 ? void 0 : config.epoch
6076
+ });
6016
6077
 
6017
6078
  const unsafeRes = await this._rpcRequest('getStakeActivation', args);
6018
6079
  const res = create(unsafeRes, jsonRpcResult(StakeActivationResult));
@@ -6031,28 +6092,16 @@ class Connection {
6031
6092
 
6032
6093
 
6033
6094
  async getProgramAccounts(programId, configOrCommitment) {
6034
- const extra = {};
6035
- let commitment;
6036
- let encoding;
6037
-
6038
- if (configOrCommitment) {
6039
- if (typeof configOrCommitment === 'string') {
6040
- commitment = configOrCommitment;
6041
- } else {
6042
- commitment = configOrCommitment.commitment;
6043
- encoding = configOrCommitment.encoding;
6044
-
6045
- if (configOrCommitment.dataSlice) {
6046
- extra.dataSlice = configOrCommitment.dataSlice;
6047
- }
6048
-
6049
- if (configOrCommitment.filters) {
6050
- extra.filters = configOrCommitment.filters;
6051
- }
6052
- }
6053
- }
6095
+ const {
6096
+ commitment,
6097
+ config
6098
+ } = extractCommitmentFromConfig(configOrCommitment);
6099
+ const {
6100
+ encoding,
6101
+ ...configWithoutEncoding
6102
+ } = config || {};
6054
6103
 
6055
- const args = this._buildArgs([programId.toBase58()], commitment, encoding || 'base64', extra);
6104
+ const args = this._buildArgs([programId.toBase58()], commitment, encoding || 'base64', configWithoutEncoding);
6056
6105
 
6057
6106
  const unsafeRes = await this._rpcRequest('getProgramAccounts', args);
6058
6107
  const res = create(unsafeRes, jsonRpcResult(array(KeyedAccountInfoResult)));
@@ -6071,22 +6120,12 @@ class Connection {
6071
6120
 
6072
6121
 
6073
6122
  async getParsedProgramAccounts(programId, configOrCommitment) {
6074
- const extra = {};
6075
- let commitment;
6076
-
6077
- if (configOrCommitment) {
6078
- if (typeof configOrCommitment === 'string') {
6079
- commitment = configOrCommitment;
6080
- } else {
6081
- commitment = configOrCommitment.commitment;
6082
-
6083
- if (configOrCommitment.filters) {
6084
- extra.filters = configOrCommitment.filters;
6085
- }
6086
- }
6087
- }
6123
+ const {
6124
+ commitment,
6125
+ config
6126
+ } = extractCommitmentFromConfig(configOrCommitment);
6088
6127
 
6089
- const args = this._buildArgs([programId.toBase58()], commitment, 'jsonParsed', extra);
6128
+ const args = this._buildArgs([programId.toBase58()], commitment, 'jsonParsed', config);
6090
6129
 
6091
6130
  const unsafeRes = await this._rpcRequest('getProgramAccounts', args);
6092
6131
  const res = create(unsafeRes, jsonRpcResult(array(KeyedParsedAccountInfoResult)));
@@ -6253,8 +6292,15 @@ class Connection {
6253
6292
  */
6254
6293
 
6255
6294
 
6256
- async getSlot(commitment) {
6257
- const args = this._buildArgs([], commitment);
6295
+ async getSlot(commitmentOrConfig) {
6296
+ const {
6297
+ commitment,
6298
+ config
6299
+ } = extractCommitmentFromConfig(commitmentOrConfig);
6300
+
6301
+ const args = this._buildArgs([], commitment, undefined
6302
+ /* encoding */
6303
+ , config);
6258
6304
 
6259
6305
  const unsafeRes = await this._rpcRequest('getSlot', args);
6260
6306
  const res = create(unsafeRes, jsonRpcResult(number()));
@@ -6270,8 +6316,15 @@ class Connection {
6270
6316
  */
6271
6317
 
6272
6318
 
6273
- async getSlotLeader(commitment) {
6274
- const args = this._buildArgs([], commitment);
6319
+ async getSlotLeader(commitmentOrConfig) {
6320
+ const {
6321
+ commitment,
6322
+ config
6323
+ } = extractCommitmentFromConfig(commitmentOrConfig);
6324
+
6325
+ const args = this._buildArgs([], commitment, undefined
6326
+ /* encoding */
6327
+ , config);
6275
6328
 
6276
6329
  const unsafeRes = await this._rpcRequest('getSlotLeader', args);
6277
6330
  const res = create(unsafeRes, jsonRpcResult(string()));
@@ -6344,8 +6397,15 @@ class Connection {
6344
6397
  */
6345
6398
 
6346
6399
 
6347
- async getTransactionCount(commitment) {
6348
- const args = this._buildArgs([], commitment);
6400
+ async getTransactionCount(commitmentOrConfig) {
6401
+ const {
6402
+ commitment,
6403
+ config
6404
+ } = extractCommitmentFromConfig(commitmentOrConfig);
6405
+
6406
+ const args = this._buildArgs([], commitment, undefined
6407
+ /* encoding */
6408
+ , config);
6349
6409
 
6350
6410
  const unsafeRes = await this._rpcRequest('getTransactionCount', args);
6351
6411
  const res = create(unsafeRes, jsonRpcResult(number()));
@@ -6392,9 +6452,16 @@ class Connection {
6392
6452
  */
6393
6453
 
6394
6454
 
6395
- async getInflationReward(addresses, epoch, commitment) {
6396
- const args = this._buildArgs([addresses.map(pubkey => pubkey.toBase58())], commitment, undefined, {
6397
- epoch
6455
+ async getInflationReward(addresses, epoch, commitmentOrConfig) {
6456
+ const {
6457
+ commitment,
6458
+ config
6459
+ } = extractCommitmentFromConfig(commitmentOrConfig);
6460
+
6461
+ const args = this._buildArgs([addresses.map(pubkey => pubkey.toBase58())], commitment, undefined
6462
+ /* encoding */
6463
+ , { ...config,
6464
+ epoch: epoch != null ? epoch : config === null || config === void 0 ? void 0 : config.epoch
6398
6465
  });
6399
6466
 
6400
6467
  const unsafeRes = await this._rpcRequest('getInflationReward', args);
@@ -6411,8 +6478,15 @@ class Connection {
6411
6478
  */
6412
6479
 
6413
6480
 
6414
- async getEpochInfo(commitment) {
6415
- const args = this._buildArgs([], commitment);
6481
+ async getEpochInfo(commitmentOrConfig) {
6482
+ const {
6483
+ commitment,
6484
+ config
6485
+ } = extractCommitmentFromConfig(commitmentOrConfig);
6486
+
6487
+ const args = this._buildArgs([], commitment, undefined
6488
+ /* encoding */
6489
+ , config);
6416
6490
 
6417
6491
  const unsafeRes = await this._rpcRequest('getEpochInfo', args);
6418
6492
  const res = create(unsafeRes, GetEpochInfoRpcResult);
@@ -6583,9 +6657,9 @@ class Connection {
6583
6657
  */
6584
6658
 
6585
6659
 
6586
- async getLatestBlockhash(commitment) {
6660
+ async getLatestBlockhash(commitmentOrConfig) {
6587
6661
  try {
6588
- const res = await this.getLatestBlockhashAndContext(commitment);
6662
+ const res = await this.getLatestBlockhashAndContext(commitmentOrConfig);
6589
6663
  return res.value;
6590
6664
  } catch (e) {
6591
6665
  throw new Error('failed to get recent blockhash: ' + e);
@@ -6597,8 +6671,15 @@ class Connection {
6597
6671
  */
6598
6672
 
6599
6673
 
6600
- async getLatestBlockhashAndContext(commitment) {
6601
- const args = this._buildArgs([], commitment);
6674
+ async getLatestBlockhashAndContext(commitmentOrConfig) {
6675
+ const {
6676
+ commitment,
6677
+ config
6678
+ } = extractCommitmentFromConfig(commitmentOrConfig);
6679
+
6680
+ const args = this._buildArgs([], commitment, undefined
6681
+ /* encoding */
6682
+ , config);
6602
6683
 
6603
6684
  const unsafeRes = await this._rpcRequest('getLatestBlockhash', args);
6604
6685
  const res = create(unsafeRes, GetLatestBlockhashRpcResult);
@@ -6676,8 +6757,15 @@ class Connection {
6676
6757
  */
6677
6758
 
6678
6759
 
6679
- async getBlockHeight(commitment) {
6680
- const args = this._buildArgs([], commitment);
6760
+ async getBlockHeight(commitmentOrConfig) {
6761
+ const {
6762
+ commitment,
6763
+ config
6764
+ } = extractCommitmentFromConfig(commitmentOrConfig);
6765
+
6766
+ const args = this._buildArgs([], commitment, undefined
6767
+ /* encoding */
6768
+ , config);
6681
6769
 
6682
6770
  const unsafeRes = await this._rpcRequest('getBlockHeight', args);
6683
6771
  const res = create(unsafeRes, jsonRpcResult(number()));
@@ -7406,6 +7494,10 @@ class Connection {
7406
7494
  config.maxRetries = options.maxRetries;
7407
7495
  }
7408
7496
 
7497
+ if (options && options.minContextSlot != null) {
7498
+ config.minContextSlot = options.minContextSlot;
7499
+ }
7500
+
7409
7501
  if (skipPreflight) {
7410
7502
  config.skipPreflight = skipPreflight;
7411
7503
  }
@@ -9815,7 +9907,8 @@ async function sendAndConfirmRawTransaction(connection, rawTransaction, confirma
9815
9907
 
9816
9908
  const sendOptions = options && {
9817
9909
  skipPreflight: options.skipPreflight,
9818
- preflightCommitment: options.preflightCommitment || options.commitment
9910
+ preflightCommitment: options.preflightCommitment || options.commitment,
9911
+ minContextSlot: options.minContextSlot
9819
9912
  };
9820
9913
  const signature = await connection.sendRawTransaction(rawTransaction, sendOptions);
9821
9914
  const commitment = options && options.commitment;