@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.
@@ -2541,24 +2541,27 @@ class Transaction {
2541
2541
  return this._message;
2542
2542
  }
2543
2543
 
2544
- const {
2545
- nonceInfo
2546
- } = this;
2544
+ let recentBlockhash;
2545
+ let instructions;
2547
2546
 
2548
- if (nonceInfo && this.instructions[0] != nonceInfo.nonceInstruction) {
2549
- this.recentBlockhash = nonceInfo.nonce;
2550
- this.instructions.unshift(nonceInfo.nonceInstruction);
2551
- }
2547
+ if (this.nonceInfo) {
2548
+ recentBlockhash = this.nonceInfo.nonce;
2552
2549
 
2553
- const {
2554
- recentBlockhash
2555
- } = this;
2550
+ if (this.instructions[0] != this.nonceInfo.nonceInstruction) {
2551
+ instructions = [this.nonceInfo.nonceInstruction, ...this.instructions];
2552
+ } else {
2553
+ instructions = this.instructions;
2554
+ }
2555
+ } else {
2556
+ recentBlockhash = this.recentBlockhash;
2557
+ instructions = this.instructions;
2558
+ }
2556
2559
 
2557
2560
  if (!recentBlockhash) {
2558
2561
  throw new Error('Transaction recentBlockhash required');
2559
2562
  }
2560
2563
 
2561
- if (this.instructions.length < 1) {
2564
+ if (instructions.length < 1) {
2562
2565
  console.warn('No instructions provided');
2563
2566
  }
2564
2567
 
@@ -2573,15 +2576,15 @@ class Transaction {
2573
2576
  throw new Error('Transaction fee payer required');
2574
2577
  }
2575
2578
 
2576
- for (let i = 0; i < this.instructions.length; i++) {
2577
- if (this.instructions[i].programId === undefined) {
2579
+ for (let i = 0; i < instructions.length; i++) {
2580
+ if (instructions[i].programId === undefined) {
2578
2581
  throw new Error(`Transaction instruction index ${i} has undefined program id`);
2579
2582
  }
2580
2583
  }
2581
2584
 
2582
2585
  const programIds = [];
2583
2586
  const accountMetas = [];
2584
- this.instructions.forEach(instruction => {
2587
+ instructions.forEach(instruction => {
2585
2588
  instruction.keys.forEach(accountMeta => {
2586
2589
  accountMetas.push({ ...accountMeta
2587
2590
  });
@@ -2691,7 +2694,7 @@ class Transaction {
2691
2694
  }
2692
2695
  });
2693
2696
  const accountKeys = signedKeys.concat(unsignedKeys);
2694
- const instructions = this.instructions.map(instruction => {
2697
+ const compiledInstructions = instructions.map(instruction => {
2695
2698
  const {
2696
2699
  data,
2697
2700
  programId
@@ -2702,7 +2705,7 @@ class Transaction {
2702
2705
  data: bs58__default["default"].encode(data)
2703
2706
  };
2704
2707
  });
2705
- instructions.forEach(instruction => {
2708
+ compiledInstructions.forEach(instruction => {
2706
2709
  assert(instruction.programIdIndex >= 0);
2707
2710
  instruction.accounts.forEach(keyIndex => assert(keyIndex >= 0));
2708
2711
  });
@@ -2714,7 +2717,7 @@ class Transaction {
2714
2717
  },
2715
2718
  accountKeys,
2716
2719
  recentBlockhash,
2717
- instructions
2720
+ instructions: compiledInstructions
2718
2721
  });
2719
2722
  }
2720
2723
  /**
@@ -3101,7 +3104,8 @@ async function sendAndConfirmTransaction(connection, transaction, signers, optio
3101
3104
  const sendOptions = options && {
3102
3105
  skipPreflight: options.skipPreflight,
3103
3106
  preflightCommitment: options.preflightCommitment || options.commitment,
3104
- maxRetries: options.maxRetries
3107
+ maxRetries: options.maxRetries,
3108
+ minContextSlot: options.minContextSlot
3105
3109
  };
3106
3110
  const signature = await connection.sendTransaction(transaction, signers, sendOptions);
3107
3111
  const status = transaction.recentBlockhash != null && transaction.lastValidBlockHeight != null ? (await connection.confirmTransaction({
@@ -4596,6 +4600,7 @@ function makeWebsocketUrl(endpoint) {
4596
4600
  return url.toString();
4597
4601
  }
4598
4602
 
4603
+ var _process$env$npm_pack;
4599
4604
  const PublicKeyFromString = superstruct.coerce(superstruct.instance(PublicKey), superstruct.string(), value => new PublicKey(value));
4600
4605
  const RawAccountDataResult = superstruct.tuple([superstruct.string(), superstruct.literal('base64')]);
4601
4606
  const BufferFromRawAccountData = superstruct.coerce(superstruct.instance(buffer.Buffer), RawAccountDataResult, value => buffer.Buffer.from(value[0], 'base64'));
@@ -4612,9 +4617,32 @@ const BLOCKHASH_CACHE_TIMEOUT_MS = 30 * 1000;
4612
4617
  * https://gist.github.com/steveluscher/c057eca81d479ef705cdb53162f9971d
4613
4618
  */
4614
4619
 
4620
+ /** @internal */
4621
+ function extractCommitmentFromConfig(commitmentOrConfig) {
4622
+ let commitment;
4623
+ let config;
4624
+
4625
+ if (typeof commitmentOrConfig === 'string') {
4626
+ commitment = commitmentOrConfig;
4627
+ } else if (commitmentOrConfig) {
4628
+ const {
4629
+ commitment: specifiedCommitment,
4630
+ ...specifiedConfig
4631
+ } = commitmentOrConfig;
4632
+ commitment = specifiedCommitment;
4633
+ config = specifiedConfig;
4634
+ }
4635
+
4636
+ return {
4637
+ commitment,
4638
+ config
4639
+ };
4640
+ }
4615
4641
  /**
4616
4642
  * @internal
4617
4643
  */
4644
+
4645
+
4618
4646
  function createRpcResult(result) {
4619
4647
  return superstruct.union([superstruct.type({
4620
4648
  jsonrpc: superstruct.literal('2.0'),
@@ -4806,7 +4834,7 @@ function createRpcClient(url, useHttps, httpHeaders, customFetch, fetchMiddlewar
4806
4834
  agent,
4807
4835
  headers: Object.assign({
4808
4836
  'Content-Type': 'application/json'
4809
- }, httpHeaders || {})
4837
+ }, httpHeaders || {}, COMMON_HTTP_HEADERS)
4810
4838
  };
4811
4839
 
4812
4840
  try {
@@ -5479,9 +5507,14 @@ const LogsNotificationResult = superstruct.type({
5479
5507
  * Filter for log subscriptions.
5480
5508
  */
5481
5509
 
5510
+ /** @internal */
5511
+ const COMMON_HTTP_HEADERS = {
5512
+ 'solana-client': `js/${(_process$env$npm_pack = "0.0.0-development") !== null && _process$env$npm_pack !== void 0 ? _process$env$npm_pack : 'UNKNOWN'}`
5513
+ };
5482
5514
  /**
5483
5515
  * A connection to a fullnode JSON RPC endpoint
5484
5516
  */
5517
+
5485
5518
  class Connection {
5486
5519
  /** @internal */
5487
5520
 
@@ -5646,8 +5679,16 @@ class Connection {
5646
5679
  */
5647
5680
 
5648
5681
 
5649
- async getBalanceAndContext(publicKey, commitment) {
5650
- const args = this._buildArgs([publicKey.toBase58()], commitment);
5682
+ async getBalanceAndContext(publicKey, commitmentOrConfig) {
5683
+ /** @internal */
5684
+ const {
5685
+ commitment,
5686
+ config
5687
+ } = extractCommitmentFromConfig(commitmentOrConfig);
5688
+
5689
+ const args = this._buildArgs([publicKey.toBase58()], commitment, undefined
5690
+ /* encoding */
5691
+ , config);
5651
5692
 
5652
5693
  const unsafeRes = await this._rpcRequest('getBalance', args);
5653
5694
  const res = superstruct.create(unsafeRes, jsonRpcResultAndContext(superstruct.number()));
@@ -5663,8 +5704,8 @@ class Connection {
5663
5704
  */
5664
5705
 
5665
5706
 
5666
- async getBalance(publicKey, commitment) {
5667
- return await this.getBalanceAndContext(publicKey, commitment).then(x => x.value).catch(e => {
5707
+ async getBalance(publicKey, commitmentOrConfig) {
5708
+ return await this.getBalanceAndContext(publicKey, commitmentOrConfig).then(x => x.value).catch(e => {
5668
5709
  throw new Error('failed to get balance of account ' + publicKey.toBase58() + ': ' + e);
5669
5710
  });
5670
5711
  }
@@ -5786,7 +5827,11 @@ class Connection {
5786
5827
  */
5787
5828
 
5788
5829
 
5789
- async getTokenAccountsByOwner(ownerAddress, filter, commitment) {
5830
+ async getTokenAccountsByOwner(ownerAddress, filter, commitmentOrConfig) {
5831
+ const {
5832
+ commitment,
5833
+ config
5834
+ } = extractCommitmentFromConfig(commitmentOrConfig);
5790
5835
  let _args = [ownerAddress.toBase58()];
5791
5836
 
5792
5837
  if ('mint' in filter) {
@@ -5799,7 +5844,7 @@ class Connection {
5799
5844
  });
5800
5845
  }
5801
5846
 
5802
- const args = this._buildArgs(_args, commitment, 'base64');
5847
+ const args = this._buildArgs(_args, commitment, 'base64', config);
5803
5848
 
5804
5849
  const unsafeRes = await this._rpcRequest('getTokenAccountsByOwner', args);
5805
5850
  const res = superstruct.create(unsafeRes, GetTokenAccountsByOwner);
@@ -5883,8 +5928,13 @@ class Connection {
5883
5928
  */
5884
5929
 
5885
5930
 
5886
- async getAccountInfoAndContext(publicKey, commitment) {
5887
- const args = this._buildArgs([publicKey.toBase58()], commitment, 'base64');
5931
+ async getAccountInfoAndContext(publicKey, commitmentOrConfig) {
5932
+ const {
5933
+ commitment,
5934
+ config
5935
+ } = extractCommitmentFromConfig(commitmentOrConfig);
5936
+
5937
+ const args = this._buildArgs([publicKey.toBase58()], commitment, 'base64', config);
5888
5938
 
5889
5939
  const unsafeRes = await this._rpcRequest('getAccountInfo', args);
5890
5940
  const res = superstruct.create(unsafeRes, jsonRpcResultAndContext(superstruct.nullable(AccountInfoResult)));
@@ -5917,9 +5967,9 @@ class Connection {
5917
5967
  */
5918
5968
 
5919
5969
 
5920
- async getAccountInfo(publicKey, commitment) {
5970
+ async getAccountInfo(publicKey, commitmentOrConfig) {
5921
5971
  try {
5922
- const res = await this.getAccountInfoAndContext(publicKey, commitment);
5972
+ const res = await this.getAccountInfoAndContext(publicKey, commitmentOrConfig);
5923
5973
  return res.value;
5924
5974
  } catch (e) {
5925
5975
  throw new Error('failed to get info about account ' + publicKey.toBase58() + ': ' + e);
@@ -5930,10 +5980,14 @@ class Connection {
5930
5980
  */
5931
5981
 
5932
5982
 
5933
- async getMultipleAccountsInfoAndContext(publicKeys, commitment) {
5983
+ async getMultipleAccountsInfoAndContext(publicKeys, commitmentOrConfig) {
5984
+ const {
5985
+ commitment,
5986
+ config
5987
+ } = extractCommitmentFromConfig(commitmentOrConfig);
5934
5988
  const keys = publicKeys.map(key => key.toBase58());
5935
5989
 
5936
- const args = this._buildArgs([keys], commitment, 'base64');
5990
+ const args = this._buildArgs([keys], commitment, 'base64', config);
5937
5991
 
5938
5992
  const unsafeRes = await this._rpcRequest('getMultipleAccounts', args);
5939
5993
  const res = superstruct.create(unsafeRes, jsonRpcResultAndContext(superstruct.array(superstruct.nullable(AccountInfoResult))));
@@ -5949,8 +6003,8 @@ class Connection {
5949
6003
  */
5950
6004
 
5951
6005
 
5952
- async getMultipleAccountsInfo(publicKeys, commitment) {
5953
- const res = await this.getMultipleAccountsInfoAndContext(publicKeys, commitment);
6006
+ async getMultipleAccountsInfo(publicKeys, commitmentOrConfig) {
6007
+ const res = await this.getMultipleAccountsInfoAndContext(publicKeys, commitmentOrConfig);
5954
6008
  return res.value;
5955
6009
  }
5956
6010
  /**
@@ -5958,10 +6012,17 @@ class Connection {
5958
6012
  */
5959
6013
 
5960
6014
 
5961
- async getStakeActivation(publicKey, commitment, epoch) {
5962
- const args = this._buildArgs([publicKey.toBase58()], commitment, undefined, epoch !== undefined ? {
5963
- epoch
5964
- } : undefined);
6015
+ async getStakeActivation(publicKey, commitmentOrConfig, epoch) {
6016
+ const {
6017
+ commitment,
6018
+ config
6019
+ } = extractCommitmentFromConfig(commitmentOrConfig);
6020
+
6021
+ const args = this._buildArgs([publicKey.toBase58()], commitment, undefined
6022
+ /* encoding */
6023
+ , { ...config,
6024
+ epoch: epoch != null ? epoch : config === null || config === void 0 ? void 0 : config.epoch
6025
+ });
5965
6026
 
5966
6027
  const unsafeRes = await this._rpcRequest('getStakeActivation', args);
5967
6028
  const res = superstruct.create(unsafeRes, jsonRpcResult(StakeActivationResult));
@@ -5980,28 +6041,16 @@ class Connection {
5980
6041
 
5981
6042
 
5982
6043
  async getProgramAccounts(programId, configOrCommitment) {
5983
- const extra = {};
5984
- let commitment;
5985
- let encoding;
5986
-
5987
- if (configOrCommitment) {
5988
- if (typeof configOrCommitment === 'string') {
5989
- commitment = configOrCommitment;
5990
- } else {
5991
- commitment = configOrCommitment.commitment;
5992
- encoding = configOrCommitment.encoding;
5993
-
5994
- if (configOrCommitment.dataSlice) {
5995
- extra.dataSlice = configOrCommitment.dataSlice;
5996
- }
5997
-
5998
- if (configOrCommitment.filters) {
5999
- extra.filters = configOrCommitment.filters;
6000
- }
6001
- }
6002
- }
6044
+ const {
6045
+ commitment,
6046
+ config
6047
+ } = extractCommitmentFromConfig(configOrCommitment);
6048
+ const {
6049
+ encoding,
6050
+ ...configWithoutEncoding
6051
+ } = config || {};
6003
6052
 
6004
- const args = this._buildArgs([programId.toBase58()], commitment, encoding || 'base64', extra);
6053
+ const args = this._buildArgs([programId.toBase58()], commitment, encoding || 'base64', configWithoutEncoding);
6005
6054
 
6006
6055
  const unsafeRes = await this._rpcRequest('getProgramAccounts', args);
6007
6056
  const res = superstruct.create(unsafeRes, jsonRpcResult(superstruct.array(KeyedAccountInfoResult)));
@@ -6020,22 +6069,12 @@ class Connection {
6020
6069
 
6021
6070
 
6022
6071
  async getParsedProgramAccounts(programId, configOrCommitment) {
6023
- const extra = {};
6024
- let commitment;
6025
-
6026
- if (configOrCommitment) {
6027
- if (typeof configOrCommitment === 'string') {
6028
- commitment = configOrCommitment;
6029
- } else {
6030
- commitment = configOrCommitment.commitment;
6031
-
6032
- if (configOrCommitment.filters) {
6033
- extra.filters = configOrCommitment.filters;
6034
- }
6035
- }
6036
- }
6072
+ const {
6073
+ commitment,
6074
+ config
6075
+ } = extractCommitmentFromConfig(configOrCommitment);
6037
6076
 
6038
- const args = this._buildArgs([programId.toBase58()], commitment, 'jsonParsed', extra);
6077
+ const args = this._buildArgs([programId.toBase58()], commitment, 'jsonParsed', config);
6039
6078
 
6040
6079
  const unsafeRes = await this._rpcRequest('getProgramAccounts', args);
6041
6080
  const res = superstruct.create(unsafeRes, jsonRpcResult(superstruct.array(KeyedParsedAccountInfoResult)));
@@ -6202,8 +6241,15 @@ class Connection {
6202
6241
  */
6203
6242
 
6204
6243
 
6205
- async getSlot(commitment) {
6206
- const args = this._buildArgs([], commitment);
6244
+ async getSlot(commitmentOrConfig) {
6245
+ const {
6246
+ commitment,
6247
+ config
6248
+ } = extractCommitmentFromConfig(commitmentOrConfig);
6249
+
6250
+ const args = this._buildArgs([], commitment, undefined
6251
+ /* encoding */
6252
+ , config);
6207
6253
 
6208
6254
  const unsafeRes = await this._rpcRequest('getSlot', args);
6209
6255
  const res = superstruct.create(unsafeRes, jsonRpcResult(superstruct.number()));
@@ -6219,8 +6265,15 @@ class Connection {
6219
6265
  */
6220
6266
 
6221
6267
 
6222
- async getSlotLeader(commitment) {
6223
- const args = this._buildArgs([], commitment);
6268
+ async getSlotLeader(commitmentOrConfig) {
6269
+ const {
6270
+ commitment,
6271
+ config
6272
+ } = extractCommitmentFromConfig(commitmentOrConfig);
6273
+
6274
+ const args = this._buildArgs([], commitment, undefined
6275
+ /* encoding */
6276
+ , config);
6224
6277
 
6225
6278
  const unsafeRes = await this._rpcRequest('getSlotLeader', args);
6226
6279
  const res = superstruct.create(unsafeRes, jsonRpcResult(superstruct.string()));
@@ -6293,8 +6346,15 @@ class Connection {
6293
6346
  */
6294
6347
 
6295
6348
 
6296
- async getTransactionCount(commitment) {
6297
- const args = this._buildArgs([], commitment);
6349
+ async getTransactionCount(commitmentOrConfig) {
6350
+ const {
6351
+ commitment,
6352
+ config
6353
+ } = extractCommitmentFromConfig(commitmentOrConfig);
6354
+
6355
+ const args = this._buildArgs([], commitment, undefined
6356
+ /* encoding */
6357
+ , config);
6298
6358
 
6299
6359
  const unsafeRes = await this._rpcRequest('getTransactionCount', args);
6300
6360
  const res = superstruct.create(unsafeRes, jsonRpcResult(superstruct.number()));
@@ -6341,9 +6401,16 @@ class Connection {
6341
6401
  */
6342
6402
 
6343
6403
 
6344
- async getInflationReward(addresses, epoch, commitment) {
6345
- const args = this._buildArgs([addresses.map(pubkey => pubkey.toBase58())], commitment, undefined, {
6346
- epoch
6404
+ async getInflationReward(addresses, epoch, commitmentOrConfig) {
6405
+ const {
6406
+ commitment,
6407
+ config
6408
+ } = extractCommitmentFromConfig(commitmentOrConfig);
6409
+
6410
+ const args = this._buildArgs([addresses.map(pubkey => pubkey.toBase58())], commitment, undefined
6411
+ /* encoding */
6412
+ , { ...config,
6413
+ epoch: epoch != null ? epoch : config === null || config === void 0 ? void 0 : config.epoch
6347
6414
  });
6348
6415
 
6349
6416
  const unsafeRes = await this._rpcRequest('getInflationReward', args);
@@ -6360,8 +6427,15 @@ class Connection {
6360
6427
  */
6361
6428
 
6362
6429
 
6363
- async getEpochInfo(commitment) {
6364
- const args = this._buildArgs([], commitment);
6430
+ async getEpochInfo(commitmentOrConfig) {
6431
+ const {
6432
+ commitment,
6433
+ config
6434
+ } = extractCommitmentFromConfig(commitmentOrConfig);
6435
+
6436
+ const args = this._buildArgs([], commitment, undefined
6437
+ /* encoding */
6438
+ , config);
6365
6439
 
6366
6440
  const unsafeRes = await this._rpcRequest('getEpochInfo', args);
6367
6441
  const res = superstruct.create(unsafeRes, GetEpochInfoRpcResult);
@@ -6532,9 +6606,9 @@ class Connection {
6532
6606
  */
6533
6607
 
6534
6608
 
6535
- async getLatestBlockhash(commitment) {
6609
+ async getLatestBlockhash(commitmentOrConfig) {
6536
6610
  try {
6537
- const res = await this.getLatestBlockhashAndContext(commitment);
6611
+ const res = await this.getLatestBlockhashAndContext(commitmentOrConfig);
6538
6612
  return res.value;
6539
6613
  } catch (e) {
6540
6614
  throw new Error('failed to get recent blockhash: ' + e);
@@ -6546,8 +6620,15 @@ class Connection {
6546
6620
  */
6547
6621
 
6548
6622
 
6549
- async getLatestBlockhashAndContext(commitment) {
6550
- const args = this._buildArgs([], commitment);
6623
+ async getLatestBlockhashAndContext(commitmentOrConfig) {
6624
+ const {
6625
+ commitment,
6626
+ config
6627
+ } = extractCommitmentFromConfig(commitmentOrConfig);
6628
+
6629
+ const args = this._buildArgs([], commitment, undefined
6630
+ /* encoding */
6631
+ , config);
6551
6632
 
6552
6633
  const unsafeRes = await this._rpcRequest('getLatestBlockhash', args);
6553
6634
  const res = superstruct.create(unsafeRes, GetLatestBlockhashRpcResult);
@@ -6625,8 +6706,15 @@ class Connection {
6625
6706
  */
6626
6707
 
6627
6708
 
6628
- async getBlockHeight(commitment) {
6629
- const args = this._buildArgs([], commitment);
6709
+ async getBlockHeight(commitmentOrConfig) {
6710
+ const {
6711
+ commitment,
6712
+ config
6713
+ } = extractCommitmentFromConfig(commitmentOrConfig);
6714
+
6715
+ const args = this._buildArgs([], commitment, undefined
6716
+ /* encoding */
6717
+ , config);
6630
6718
 
6631
6719
  const unsafeRes = await this._rpcRequest('getBlockHeight', args);
6632
6720
  const res = superstruct.create(unsafeRes, jsonRpcResult(superstruct.number()));
@@ -7355,6 +7443,10 @@ class Connection {
7355
7443
  config.maxRetries = options.maxRetries;
7356
7444
  }
7357
7445
 
7446
+ if (options && options.minContextSlot != null) {
7447
+ config.minContextSlot = options.minContextSlot;
7448
+ }
7449
+
7358
7450
  if (skipPreflight) {
7359
7451
  config.skipPreflight = skipPreflight;
7360
7452
  }
@@ -9764,7 +9856,8 @@ async function sendAndConfirmRawTransaction(connection, rawTransaction, confirma
9764
9856
 
9765
9857
  const sendOptions = options && {
9766
9858
  skipPreflight: options.skipPreflight,
9767
- preflightCommitment: options.preflightCommitment || options.commitment
9859
+ preflightCommitment: options.preflightCommitment || options.commitment,
9860
+ minContextSlot: options.minContextSlot
9768
9861
  };
9769
9862
  const signature = await connection.sendRawTransaction(rawTransaction, sendOptions);
9770
9863
  const commitment = options && options.commitment;