@solana/web3.js 2.0.0-experimental.5e737f9 → 2.0.0-experimental.606040b

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.
Files changed (49) hide show
  1. package/README.md +2 -0
  2. package/dist/index.browser.cjs +180 -56
  3. package/dist/index.browser.cjs.map +1 -1
  4. package/dist/index.browser.js +146 -56
  5. package/dist/index.browser.js.map +1 -1
  6. package/dist/index.development.js +3816 -2283
  7. package/dist/index.development.js.map +1 -1
  8. package/dist/index.native.js +146 -56
  9. package/dist/index.native.js.map +1 -1
  10. package/dist/index.node.cjs +180 -56
  11. package/dist/index.node.cjs.map +1 -1
  12. package/dist/index.node.js +146 -56
  13. package/dist/index.node.js.map +1 -1
  14. package/dist/index.production.min.js +273 -82
  15. package/dist/types/airdrop-confirmer.d.ts +1 -1
  16. package/dist/types/airdrop-confirmer.d.ts.map +1 -1
  17. package/dist/types/airdrop.d.ts +1 -1
  18. package/dist/types/airdrop.d.ts.map +1 -1
  19. package/dist/types/decode-transaction.d.ts +10 -0
  20. package/dist/types/decode-transaction.d.ts.map +1 -0
  21. package/dist/types/index.d.ts +7 -0
  22. package/dist/types/index.d.ts.map +1 -1
  23. package/dist/types/rpc-integer-overflow-error.d.ts +2 -7
  24. package/dist/types/rpc-integer-overflow-error.d.ts.map +1 -1
  25. package/dist/types/rpc-request-coalescer.d.ts +1 -1
  26. package/dist/types/rpc-request-coalescer.d.ts.map +1 -1
  27. package/dist/types/rpc-subscription-coalescer.d.ts +1 -1
  28. package/dist/types/rpc-subscription-coalescer.d.ts.map +1 -1
  29. package/dist/types/rpc-transport.d.ts +8 -2
  30. package/dist/types/rpc-transport.d.ts.map +1 -1
  31. package/dist/types/rpc-websocket-autopinger.d.ts +3 -3
  32. package/dist/types/rpc-websocket-autopinger.d.ts.map +1 -1
  33. package/dist/types/rpc-websocket-connection-sharding.d.ts +3 -3
  34. package/dist/types/rpc-websocket-connection-sharding.d.ts.map +1 -1
  35. package/dist/types/rpc-websocket-transport.d.ts +7 -3
  36. package/dist/types/rpc-websocket-transport.d.ts.map +1 -1
  37. package/dist/types/rpc.d.ts +12 -5
  38. package/dist/types/rpc.d.ts.map +1 -1
  39. package/dist/types/send-transaction.d.ts +1 -2
  40. package/dist/types/send-transaction.d.ts.map +1 -1
  41. package/dist/types/transaction-confirmation-strategy-blockheight.d.ts +9 -4
  42. package/dist/types/transaction-confirmation-strategy-blockheight.d.ts.map +1 -1
  43. package/dist/types/transaction-confirmation-strategy-nonce.d.ts +1 -2
  44. package/dist/types/transaction-confirmation-strategy-nonce.d.ts.map +1 -1
  45. package/dist/types/transaction-confirmation-strategy-recent-signature.d.ts +1 -2
  46. package/dist/types/transaction-confirmation-strategy-recent-signature.d.ts.map +1 -1
  47. package/dist/types/transaction-confirmation.d.ts +3 -3
  48. package/dist/types/transaction-confirmation.d.ts.map +1 -1
  49. package/package.json +15 -10
package/README.md CHANGED
@@ -784,6 +784,8 @@ Solana’s codecs libraries are broken up into modular components so you only ne
784
784
  - `@solana/codecs-data-structures`: Codecs and serializers for structs
785
785
  - `@solana/options`: Designed to build codecs and serializers for types that mimic Rust’s enums, which can include embedded data within their variants such as values, tuples, and structs
786
786
 
787
+ These packages are included in the main `@solana/web3.js` library but you may also import them from `@solana/codecs` if you only need the codecs.
788
+
787
789
  Here’s an example of encoding and decoding a custom struct with some strings and numbers:
788
790
 
789
791
  ```tsx
@@ -1,15 +1,20 @@
1
1
  'use strict';
2
2
 
3
+ var accounts = require('@solana/accounts');
3
4
  var addresses = require('@solana/addresses');
5
+ var codecs = require('@solana/codecs');
6
+ var functional = require('@solana/functional');
4
7
  var instructions = require('@solana/instructions');
5
8
  var keys = require('@solana/keys');
9
+ var programs = require('@solana/programs');
10
+ var rpcParsedTypes = require('@solana/rpc-parsed-types');
6
11
  var rpcTypes = require('@solana/rpc-types');
12
+ var signers = require('@solana/signers');
7
13
  var transactions = require('@solana/transactions');
8
- var functional = require('@solana/functional');
9
14
  var rpcCore = require('@solana/rpc-core');
10
15
  var rpcTransport = require('@solana/rpc-transport');
11
16
  var fastStableStringify = require('fast-stable-stringify');
12
- var codecsStrings = require('@solana/codecs-strings');
17
+ var errors = require('@solana/errors');
13
18
 
14
19
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
15
20
 
@@ -172,48 +177,74 @@ async function requestAndConfirmAirdrop({
172
177
  });
173
178
  return airdropTransactionSignature;
174
179
  }
175
-
176
- // src/rpc-integer-overflow-error.ts
177
- var SolanaJsonRpcIntegerOverflowError = class extends Error {
178
- methodName;
179
- keyPath;
180
- value;
181
- constructor(methodName, keyPath, value) {
182
- let argumentLabel = "";
183
- if (typeof keyPath[0] === "number") {
184
- const argPosition = keyPath[0] + 1;
185
- const lastDigit = argPosition % 10;
186
- const lastTwoDigits = argPosition % 100;
187
- if (lastDigit == 1 && lastTwoDigits != 11) {
188
- argumentLabel = argPosition + "st";
189
- } else if (lastDigit == 2 && lastTwoDigits != 12) {
190
- argumentLabel = argPosition + "nd";
191
- } else if (lastDigit == 3 && lastTwoDigits != 13) {
192
- argumentLabel = argPosition + "rd";
193
- } else {
194
- argumentLabel = argPosition + "th";
195
- }
180
+ var compiledTransactionDecoder = void 0;
181
+ async function fetchLookupTables(lookupTableAddresses, rpc, config) {
182
+ const fetchedLookupTables = await accounts.fetchJsonParsedAccounts(
183
+ rpc,
184
+ lookupTableAddresses,
185
+ config
186
+ );
187
+ accounts.assertAccountsDecoded(fetchedLookupTables);
188
+ accounts.assertAccountsExist(fetchedLookupTables);
189
+ return fetchedLookupTables.reduce((acc, lookup) => {
190
+ return {
191
+ ...acc,
192
+ [lookup.address]: lookup.data.addresses
193
+ };
194
+ }, {});
195
+ }
196
+ async function decodeTransaction(encodedTransaction, rpc, config) {
197
+ const { lastValidBlockHeight, ...fetchAccountsConfig } = config ?? {};
198
+ if (!compiledTransactionDecoder)
199
+ compiledTransactionDecoder = transactions.getCompiledTransactionDecoder();
200
+ const compiledTransaction = compiledTransactionDecoder.decode(encodedTransaction);
201
+ const { compiledMessage } = compiledTransaction;
202
+ const lookupTables = "addressTableLookups" in compiledMessage && compiledMessage.addressTableLookups !== void 0 && compiledMessage.addressTableLookups.length > 0 ? compiledMessage.addressTableLookups : [];
203
+ const lookupTableAddresses = lookupTables.map((l) => l.lookupTableAddress);
204
+ const fetchedLookupTables = lookupTableAddresses.length > 0 ? await fetchLookupTables(lookupTableAddresses, rpc, fetchAccountsConfig) : {};
205
+ return transactions.decompileTransaction(compiledTransaction, {
206
+ addressesByLookupTableAddress: fetchedLookupTables,
207
+ lastValidBlockHeight
208
+ });
209
+ }
210
+ function createSolanaJsonRpcIntegerOverflowError(methodName, keyPath, value) {
211
+ let argumentLabel = "";
212
+ if (typeof keyPath[0] === "number") {
213
+ const argPosition = keyPath[0] + 1;
214
+ const lastDigit = argPosition % 10;
215
+ const lastTwoDigits = argPosition % 100;
216
+ if (lastDigit == 1 && lastTwoDigits != 11) {
217
+ argumentLabel = argPosition + "st";
218
+ } else if (lastDigit == 2 && lastTwoDigits != 12) {
219
+ argumentLabel = argPosition + "nd";
220
+ } else if (lastDigit == 3 && lastTwoDigits != 13) {
221
+ argumentLabel = argPosition + "rd";
196
222
  } else {
197
- argumentLabel = `\`${keyPath[0].toString()}\``;
223
+ argumentLabel = argPosition + "th";
198
224
  }
199
- const path = keyPath.length > 1 ? keyPath.slice(1).map((pathPart) => typeof pathPart === "number" ? `[${pathPart}]` : pathPart).join(".") : null;
200
- super(
201
- `The ${argumentLabel} argument to the \`${methodName}\` RPC method${path ? ` at path \`${path}\`` : ""} was \`${value}\`. This number is unsafe for use with the Solana JSON-RPC because it exceeds \`Number.MAX_SAFE_INTEGER\`.`
202
- );
203
- this.keyPath = keyPath;
204
- this.methodName = methodName;
205
- this.value = value;
225
+ } else {
226
+ argumentLabel = `\`${keyPath[0].toString()}\``;
206
227
  }
207
- get name() {
208
- return "SolanaJsonRpcIntegerOverflowError";
228
+ const path = keyPath.length > 1 ? keyPath.slice(1).map((pathPart) => typeof pathPart === "number" ? `[${pathPart}]` : pathPart).join(".") : void 0;
229
+ const error = new errors.SolanaError(errors.SOLANA_ERROR__RPC_INTEGER_OVERFLOW, {
230
+ argumentLabel,
231
+ keyPath,
232
+ methodName,
233
+ optionalPathLabel: path ? ` at path \`${path}\`` : "",
234
+ value,
235
+ ...path !== void 0 ? { path } : void 0
236
+ });
237
+ if ("captureStackTrace" in Error && typeof Error.captureStackTrace === "function") {
238
+ Error.captureStackTrace(error, createSolanaJsonRpcIntegerOverflowError);
209
239
  }
210
- };
240
+ return error;
241
+ }
211
242
 
212
243
  // src/rpc-default-config.ts
213
244
  var DEFAULT_RPC_CONFIG = {
214
245
  defaultCommitment: "confirmed",
215
246
  onIntegerOverflow(methodName, keyPath, value) {
216
- throw new SolanaJsonRpcIntegerOverflowError(methodName, keyPath, value);
247
+ throw createSolanaJsonRpcIntegerOverflowError(methodName, keyPath, value);
217
248
  }
218
249
  };
219
250
 
@@ -406,9 +437,10 @@ function getRpcSubscriptionsWithSubscriptionCoalescing({
406
437
 
407
438
  // src/rpc.ts
408
439
  function createSolanaRpc(config) {
440
+ const api = rpcCore.createSolanaRpcApi(DEFAULT_RPC_CONFIG);
409
441
  return rpcTransport.createJsonRpc({
410
442
  ...config,
411
- api: rpcCore.createSolanaRpcApi(DEFAULT_RPC_CONFIG)
443
+ api
412
444
  });
413
445
  }
414
446
  function createSolanaRpcSubscriptions(config) {
@@ -447,13 +479,23 @@ function getRpcTransportWithRequestCoalescing(transport, getDeduplicationKey) {
447
479
  }
448
480
  if (coalescedRequestsByDeduplicationKey[deduplicationKey] == null) {
449
481
  const abortController = new AbortController();
482
+ const responsePromise = (async () => {
483
+ try {
484
+ return await transport({
485
+ ...config,
486
+ signal: abortController.signal
487
+ });
488
+ } catch (e) {
489
+ if (e && typeof e === "object" && "name" in e && e.name === "AbortError") {
490
+ return;
491
+ }
492
+ throw e;
493
+ }
494
+ })();
450
495
  coalescedRequestsByDeduplicationKey[deduplicationKey] = {
451
496
  abortController,
452
497
  numConsumers: 0,
453
- responsePromise: transport({
454
- ...config,
455
- signal: abortController.signal
456
- })
498
+ responsePromise
457
499
  };
458
500
  }
459
501
  const coalescedRequest = coalescedRequestsByDeduplicationKey[deduplicationKey];
@@ -520,7 +562,10 @@ var PING_PAYLOAD = {
520
562
  jsonrpc: "2.0",
521
563
  method: "ping"
522
564
  };
523
- function getWebSocketTransportWithAutoping({ intervalMs, transport }) {
565
+ function getWebSocketTransportWithAutoping({
566
+ intervalMs,
567
+ transport
568
+ }) {
524
569
  const pingableConnections = /* @__PURE__ */ new Map();
525
570
  return async (...args) => {
526
571
  const connection = await transport(...args);
@@ -582,7 +627,10 @@ function getWebSocketTransportWithAutoping({ intervalMs, transport }) {
582
627
  var NULL_SHARD_CACHE_KEY = Symbol(
583
628
  __DEV__ ? "Cache key to use when there is no connection sharding strategy" : void 0
584
629
  );
585
- function getWebSocketTransportWithConnectionSharding({ getShard, transport }) {
630
+ function getWebSocketTransportWithConnectionSharding({
631
+ getShard,
632
+ transport
633
+ }) {
586
634
  return getCachedAbortableIterableFactory({
587
635
  getAbortSignalFromInputArgs: ({ signal }) => signal,
588
636
  getCacheEntryMissingError(shardKey) {
@@ -618,22 +666,52 @@ function createDefaultRpcSubscriptionsTransport(config) {
618
666
  }
619
667
 
620
668
  // src/transaction-confirmation-strategy-blockheight.ts
621
- function createBlockHeightExceedencePromiseFactory(rpcSubscriptions) {
622
- return async function getBlockHeightExceedencePromise({ abortSignal: callerAbortSignal, lastValidBlockHeight }) {
669
+ function createBlockHeightExceedencePromiseFactory({
670
+ rpc,
671
+ rpcSubscriptions
672
+ }) {
673
+ return async function getBlockHeightExceedencePromise({
674
+ abortSignal: callerAbortSignal,
675
+ commitment,
676
+ lastValidBlockHeight
677
+ }) {
623
678
  const abortController = new AbortController();
624
- function handleAbort() {
679
+ const handleAbort = () => {
625
680
  abortController.abort();
626
- }
681
+ };
627
682
  callerAbortSignal.addEventListener("abort", handleAbort, { signal: abortController.signal });
628
- const slotNotifications = await rpcSubscriptions.slotNotifications().subscribe({ abortSignal: abortController.signal });
683
+ async function getBlockHeightAndDifferenceBetweenSlotHeightAndBlockHeight() {
684
+ const { absoluteSlot, blockHeight } = await rpc.getEpochInfo({ commitment }).send({ abortSignal: abortController.signal });
685
+ return {
686
+ blockHeight,
687
+ differenceBetweenSlotHeightAndBlockHeight: absoluteSlot - blockHeight
688
+ };
689
+ }
629
690
  try {
630
- for await (const slotNotification of slotNotifications) {
631
- if (slotNotification.slot > lastValidBlockHeight) {
632
- throw new Error(
633
- "The network has progressed past the last block for which this transaction could have committed."
634
- );
691
+ const [slotNotifications, { blockHeight, differenceBetweenSlotHeightAndBlockHeight }] = await Promise.all([
692
+ rpcSubscriptions.slotNotifications().subscribe({ abortSignal: abortController.signal }),
693
+ getBlockHeightAndDifferenceBetweenSlotHeightAndBlockHeight()
694
+ ]);
695
+ if (blockHeight <= lastValidBlockHeight) {
696
+ let lastKnownDifferenceBetweenSlotHeightAndBlockHeight = differenceBetweenSlotHeightAndBlockHeight;
697
+ for await (const slotNotification of slotNotifications) {
698
+ const { slot } = slotNotification;
699
+ if (slot - lastKnownDifferenceBetweenSlotHeightAndBlockHeight > lastValidBlockHeight) {
700
+ const {
701
+ blockHeight: currentBlockHeight,
702
+ differenceBetweenSlotHeightAndBlockHeight: currentDifferenceBetweenSlotHeightAndBlockHeight
703
+ } = await getBlockHeightAndDifferenceBetweenSlotHeightAndBlockHeight();
704
+ if (currentBlockHeight > lastValidBlockHeight) {
705
+ break;
706
+ } else {
707
+ lastKnownDifferenceBetweenSlotHeightAndBlockHeight = currentDifferenceBetweenSlotHeightAndBlockHeight;
708
+ }
709
+ }
635
710
  }
636
711
  }
712
+ throw new Error(
713
+ "The network has progressed past the last block for which this transaction could have been committed."
714
+ );
637
715
  } finally {
638
716
  abortController.abort();
639
717
  }
@@ -655,8 +733,8 @@ function createNonceInvalidationPromiseFactory(rpc, rpcSubscriptions) {
655
733
  }
656
734
  callerAbortSignal.addEventListener("abort", handleAbort, { signal: abortController.signal });
657
735
  const accountNotifications = await rpcSubscriptions.accountNotifications(nonceAccountAddress, { commitment, encoding: "base64" }).subscribe({ abortSignal: abortController.signal });
658
- const base58Decoder = codecsStrings.getBase58Decoder();
659
- const base64Encoder = codecsStrings.getBase64Encoder();
736
+ const base58Decoder = codecs.getBase58Decoder();
737
+ const base64Encoder = codecs.getBase64Encoder();
660
738
  function getNonceFromAccountData([base64EncodedBytes]) {
661
739
  const data = base64Encoder.encode(base64EncodedBytes);
662
740
  const nonceValueBytes = data.slice(NONCE_VALUE_OFFSET, NONCE_VALUE_OFFSET + 32);
@@ -725,7 +803,10 @@ function createDefaultRecentTransactionConfirmer({
725
803
  rpc,
726
804
  rpcSubscriptions
727
805
  }) {
728
- const getBlockHeightExceedencePromise = createBlockHeightExceedencePromiseFactory(rpcSubscriptions);
806
+ const getBlockHeightExceedencePromise = createBlockHeightExceedencePromiseFactory({
807
+ rpc,
808
+ rpcSubscriptions
809
+ });
729
810
  const getRecentSignatureConfirmationPromise = createRecentSignatureConfirmationPromiseFactory(
730
811
  rpc,
731
812
  rpcSubscriptions
@@ -758,10 +839,16 @@ async function waitForRecentTransactionConfirmation(config) {
758
839
  await raceStrategies(
759
840
  transactions.getSignatureFromTransaction(config.transaction),
760
841
  config,
761
- function getSpecificStrategiesForRace({ abortSignal, getBlockHeightExceedencePromise, transaction }) {
842
+ function getSpecificStrategiesForRace({
843
+ abortSignal,
844
+ commitment,
845
+ getBlockHeightExceedencePromise,
846
+ transaction
847
+ }) {
762
848
  return [
763
849
  getBlockHeightExceedencePromise({
764
850
  abortSignal,
851
+ commitment,
765
852
  lastValidBlockHeight: transaction.lifetimeConstraint.lastValidBlockHeight
766
853
  })
767
854
  ];
@@ -896,17 +983,36 @@ exports.createRecentSignatureConfirmationPromiseFactory = createRecentSignatureC
896
983
  exports.createSolanaRpc = createSolanaRpc;
897
984
  exports.createSolanaRpcSubscriptions = createSolanaRpcSubscriptions;
898
985
  exports.createSolanaRpcSubscriptions_UNSTABLE = createSolanaRpcSubscriptions_UNSTABLE;
986
+ exports.decodeTransaction = decodeTransaction;
899
987
  exports.requestAndConfirmAirdrop = requestAndConfirmAirdrop;
900
988
  exports.sendAndConfirmDurableNonceTransaction = sendAndConfirmDurableNonceTransaction;
901
989
  exports.sendAndConfirmTransaction = sendAndConfirmTransaction;
902
990
  exports.waitForDurableNonceTransactionConfirmation = waitForDurableNonceTransactionConfirmation;
903
991
  exports.waitForRecentTransactionConfirmation = waitForRecentTransactionConfirmation;
992
+ Object.keys(accounts).forEach(function (k) {
993
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
994
+ enumerable: true,
995
+ get: function () { return accounts[k]; }
996
+ });
997
+ });
904
998
  Object.keys(addresses).forEach(function (k) {
905
999
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
906
1000
  enumerable: true,
907
1001
  get: function () { return addresses[k]; }
908
1002
  });
909
1003
  });
1004
+ Object.keys(codecs).forEach(function (k) {
1005
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
1006
+ enumerable: true,
1007
+ get: function () { return codecs[k]; }
1008
+ });
1009
+ });
1010
+ Object.keys(functional).forEach(function (k) {
1011
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
1012
+ enumerable: true,
1013
+ get: function () { return functional[k]; }
1014
+ });
1015
+ });
910
1016
  Object.keys(instructions).forEach(function (k) {
911
1017
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
912
1018
  enumerable: true,
@@ -919,12 +1025,30 @@ Object.keys(keys).forEach(function (k) {
919
1025
  get: function () { return keys[k]; }
920
1026
  });
921
1027
  });
1028
+ Object.keys(programs).forEach(function (k) {
1029
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
1030
+ enumerable: true,
1031
+ get: function () { return programs[k]; }
1032
+ });
1033
+ });
1034
+ Object.keys(rpcParsedTypes).forEach(function (k) {
1035
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
1036
+ enumerable: true,
1037
+ get: function () { return rpcParsedTypes[k]; }
1038
+ });
1039
+ });
922
1040
  Object.keys(rpcTypes).forEach(function (k) {
923
1041
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
924
1042
  enumerable: true,
925
1043
  get: function () { return rpcTypes[k]; }
926
1044
  });
927
1045
  });
1046
+ Object.keys(signers).forEach(function (k) {
1047
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
1048
+ enumerable: true,
1049
+ get: function () { return signers[k]; }
1050
+ });
1051
+ });
928
1052
  Object.keys(transactions).forEach(function (k) {
929
1053
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
930
1054
  enumerable: true,