@solana/web3.js 2.0.0-experimental.c42ccfd → 2.0.0-experimental.c588817

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/README.md CHANGED
@@ -80,7 +80,7 @@ The inability to customize web3.js has been a source of frustration for some:
80
80
 
81
81
  ## Lagging Behind Modern JavaScript
82
82
 
83
- The advance of modern JavaScript features presents an opportunity to developers of crypto applcations, such as the ability to use native Ed25519 keys and to express large values as native `bigint`.
83
+ The advance of modern JavaScript features presents an opportunity to developers of crypto applications, such as the ability to use native Ed25519 keys and to express large values as native `bigint`.
84
84
 
85
85
  The Web Incubator Community Group has advocated for the addition of Ed25519 support to the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API), and support has already landed in *most* modern JavaScript runtimes.
86
86
 
@@ -102,7 +102,7 @@ Enter web3.js 2.0. The new API aims to deliver a re-imagined experience of build
102
102
 
103
103
  ## Features
104
104
 
105
- The new (2.0) version of `@solana/web3.js` aims to address shortcomings in the legacy library first, then goes even further .
105
+ The new (2.0) version of `@solana/web3.js` aims to address shortcomings in the legacy library first, then goes even further.
106
106
 
107
107
  ### Tree-Shaking
108
108
 
@@ -251,8 +251,7 @@ const rpc = createJsonRpc<SolanaRpcMethods>({ api, transport });
251
251
  If you want to, you can also reduce the scope of the API’s type-spec so you are left only with the types you need. Keep in mind types don’t affect bundle size, but you may choose to scope the type-spec for a variety of reasons, including reducing TypeScript noise.
252
252
 
253
253
  ```tsx
254
- import { createSolanaRpcApi } from '@solana/rpc-core';
255
- import type { GetAccountInfoApi } from '@solana/rpc-core/dist/types/rpc-methods/getAccountInfo';
254
+ import { createSolanaRpcApi, type GetAccountInfoApi } from '@solana/rpc-core';
256
255
  import { createHttpTransport, createJsonRpc } from '@solana/rpc-transport';
257
256
 
258
257
  const api = createSolanaRpcApi();
@@ -325,8 +324,7 @@ Here’s an example of how someone might implement a “round robin” approach
325
324
 
326
325
  ```tsx
327
326
  import { createSolanaRpcApi } from '@solana/rpc-core';
328
- import { createJsonRpc } from '@solana/rpc-transport';
329
- import { IRpcTransport } from '@solana/rpc-transport/dist/types/transports/transport-types';
327
+ import { createJsonRpc, type IRpcTransport } from '@solana/rpc-transport';
330
328
  import { createDefaultRpcTransport } from '@solana/web3.js';
331
329
 
332
330
  // Create a transport for each RPC server
@@ -364,10 +362,9 @@ Another example of a possible customization for RPC transports is sharding. Here
364
362
  The transport library can also be used to implement custom retry logic on any request:
365
363
 
366
364
  ```tsx
367
- import { createDefaultRpcTransport } from "@solana/web3.js";
368
- import { IRpcTransport } from "@solana/rpc-transport/dist/types/transports/transport-types";
369
- import { createJsonRpc } from "@solana/rpc-transport";
370
- import { createSolanaRpcApi } from "@solana/rpc-core";
365
+ import { createDefaultRpcTransport } from '@solana/web3.js';
366
+ import { createJsonRpc, IRpcTransport } from '@solana/rpc-transport';
367
+ import { createSolanaRpcApi } from '@solana/rpc-core';
371
368
 
372
369
  // Set the maximum number of attempts to retry a request
373
370
  const MAX_ATTEMPTS = 4;
@@ -425,15 +422,14 @@ Perhaps your application needs to make a large number of requests, or needs to f
425
422
 
426
423
  ```tsx
427
424
  import { createSolanaRpcApi } from '@solana/rpc-core';
428
- import { createJsonRpc } from '@solana/rpc-transport';
429
- import { IRpcTransport } from '@solana/rpc-transport/dist/types/transports/transport-types';
425
+ import { createJsonRpc, IRpcTransport } from '@solana/rpc-transport';
430
426
  import { createDefaultRpcTransport } from '@solana/web3.js';
431
427
 
432
428
  // Create multiple transports
433
- const transportA = createDefaultRpcTransport({ url: 'https://mainnet-beta.my-server-1.com' }));
434
- const transportB = createDefaultRpcTransport({ url: 'https://mainnet-beta.my-server-2.com' }));
435
- const transportC = createDefaultRpcTransport({ url: 'https://mainnet-beta.my-server-3.com' }));
436
- const transportD = createDefaultRpcTransport({ url: 'https://mainnet-beta.my-server-4.com' }));
429
+ const transportA = createDefaultRpcTransport({ url: 'https://mainnet-beta.my-server-1.com' });
430
+ const transportB = createDefaultRpcTransport({ url: 'https://mainnet-beta.my-server-2.com' });
431
+ const transportC = createDefaultRpcTransport({ url: 'https://mainnet-beta.my-server-3.com' });
432
+ const transportD = createDefaultRpcTransport({ url: 'https://mainnet-beta.my-server-4.com' });
437
433
 
438
434
  // Function to determine which shard to use based on the request method
439
435
  function selectShard(method: string): IRpcTransport {
@@ -551,7 +547,7 @@ for await (const notification of accountNotifications) {
551
547
 
552
548
  One of the most crucial aspects of any subscription API is managing potential missed messages. Missing messages, such as account state updates, could be catastrophic for an application. That’s why the new library provides native support for recovering missed messages using the `AsyncIterator`.
553
549
 
554
- When a connection fails unexpectedly, any messages you miss while disconnected can result in your UI falling behind or becoming corrupt. Because subscription failure is now made explicit in the new API, you can implement ‘catch up’ logic after re-estabilshing the subscription.
550
+ When a connection fails unexpectedly, any messages you miss while disconnected can result in your UI falling behind or becoming corrupt. Because subscription failure is now made explicit in the new API, you can implement ‘catch up’ logic after re-establishing the subscription.
555
551
 
556
552
  Here’s an example of such logic:
557
553
 
@@ -754,7 +750,7 @@ const transactionSignedWithFeePayer = await signTransaction([signer], transactio
754
750
 
755
751
  Transaction objects are also ********frozen by these functions******** to prevent transactions from being mutated in place by functions you pass them to.
756
752
 
757
- Building transactions in this manner might feel different to what you’re used to. Also, we certainly wouldn’t want you to have to bind transformed transactions to a new variable at each step, so we have released a functional programming library dubbed `@solana/functional` that lets you build transactions in **********************************pipelines**********************************. Here’s how it can be used:
753
+ Building transactions in this manner might feel different from what you’re used to. Also, we certainly wouldn’t want you to have to bind transformed transactions to a new variable at each step, so we have released a functional programming library dubbed `@solana/functional` that lets you build transactions in **********************************pipelines**********************************. Here’s how it can be used:
758
754
 
759
755
  ```tsx
760
756
  import { pipe } from '@solana/functional';
@@ -913,7 +909,7 @@ const blockWithRewardsAndTransactionsResponse = await rpc.getBlock(0n, {
913
909
 
914
910
  ### Catching Compile-Time Bugs with TypeScript
915
911
 
916
- As previously mentioned, the type coverage in web3.js 2.0 allow developers to catch common bugs at compile time, rather than runtime.
912
+ As previously mentioned, the type coverage in web3.js 2.0 allows developers to catch common bugs at compile time, rather than runtime.
917
913
 
918
914
  In the example below, a transaction is created and then attempted to be compiled without setting the fee payer. This would result in a runtime error from the RPC, but instead you will see a type error from TypeScript as you type:
919
915
 
@@ -1,11 +1,12 @@
1
1
  'use strict';
2
2
 
3
+ var accounts = require('@solana/accounts');
3
4
  var addresses = require('@solana/addresses');
5
+ var functional = require('@solana/functional');
4
6
  var instructions = require('@solana/instructions');
5
7
  var keys = require('@solana/keys');
6
8
  var rpcTypes = require('@solana/rpc-types');
7
9
  var transactions = require('@solana/transactions');
8
- var functional = require('@solana/functional');
9
10
  var rpcCore = require('@solana/rpc-core');
10
11
  var rpcTransport = require('@solana/rpc-transport');
11
12
  var fastStableStringify = require('fast-stable-stringify');
@@ -179,22 +180,26 @@ var SolanaJsonRpcIntegerOverflowError = class extends Error {
179
180
  keyPath;
180
181
  value;
181
182
  constructor(methodName, keyPath, value) {
182
- const argPosition = (typeof keyPath[0] === "number" ? keyPath[0] : parseInt(keyPath[0], 10)) + 1;
183
- let ordinal = "";
184
- const lastDigit = argPosition % 10;
185
- const lastTwoDigits = argPosition % 100;
186
- if (lastDigit == 1 && lastTwoDigits != 11) {
187
- ordinal = argPosition + "st";
188
- } else if (lastDigit == 2 && lastTwoDigits != 12) {
189
- ordinal = argPosition + "nd";
190
- } else if (lastDigit == 3 && lastTwoDigits != 13) {
191
- ordinal = argPosition + "rd";
183
+ let argumentLabel = "";
184
+ if (typeof keyPath[0] === "number") {
185
+ const argPosition = keyPath[0] + 1;
186
+ const lastDigit = argPosition % 10;
187
+ const lastTwoDigits = argPosition % 100;
188
+ if (lastDigit == 1 && lastTwoDigits != 11) {
189
+ argumentLabel = argPosition + "st";
190
+ } else if (lastDigit == 2 && lastTwoDigits != 12) {
191
+ argumentLabel = argPosition + "nd";
192
+ } else if (lastDigit == 3 && lastTwoDigits != 13) {
193
+ argumentLabel = argPosition + "rd";
194
+ } else {
195
+ argumentLabel = argPosition + "th";
196
+ }
192
197
  } else {
193
- ordinal = argPosition + "th";
198
+ argumentLabel = `\`${keyPath[0].toString()}\``;
194
199
  }
195
200
  const path = keyPath.length > 1 ? keyPath.slice(1).map((pathPart) => typeof pathPart === "number" ? `[${pathPart}]` : pathPart).join(".") : null;
196
201
  super(
197
- `The ${ordinal} 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
+ `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\`.`
198
203
  );
199
204
  this.keyPath = keyPath;
200
205
  this.methodName = methodName;
@@ -207,6 +212,7 @@ var SolanaJsonRpcIntegerOverflowError = class extends Error {
207
212
 
208
213
  // src/rpc-default-config.ts
209
214
  var DEFAULT_RPC_CONFIG = {
215
+ defaultCommitment: "confirmed",
210
216
  onIntegerOverflow(methodName, keyPath, value) {
211
217
  throw new SolanaJsonRpcIntegerOverflowError(methodName, keyPath, value);
212
218
  }
@@ -613,22 +619,52 @@ function createDefaultRpcSubscriptionsTransport(config) {
613
619
  }
614
620
 
615
621
  // src/transaction-confirmation-strategy-blockheight.ts
616
- function createBlockHeightExceedencePromiseFactory(rpcSubscriptions) {
617
- return async function getBlockHeightExceedencePromise({ abortSignal: callerAbortSignal, lastValidBlockHeight }) {
622
+ function createBlockHeightExceedencePromiseFactory({
623
+ rpc,
624
+ rpcSubscriptions
625
+ }) {
626
+ return async function getBlockHeightExceedencePromise({
627
+ abortSignal: callerAbortSignal,
628
+ commitment,
629
+ lastValidBlockHeight
630
+ }) {
618
631
  const abortController = new AbortController();
619
- function handleAbort() {
632
+ const handleAbort = () => {
620
633
  abortController.abort();
621
- }
634
+ };
622
635
  callerAbortSignal.addEventListener("abort", handleAbort, { signal: abortController.signal });
623
- const slotNotifications = await rpcSubscriptions.slotNotifications().subscribe({ abortSignal: abortController.signal });
636
+ async function getBlockHeightAndDifferenceBetweenSlotHeightAndBlockHeight() {
637
+ const { absoluteSlot, blockHeight } = await rpc.getEpochInfo({ commitment }).send({ abortSignal: abortController.signal });
638
+ return {
639
+ blockHeight,
640
+ differenceBetweenSlotHeightAndBlockHeight: absoluteSlot - blockHeight
641
+ };
642
+ }
624
643
  try {
625
- for await (const slotNotification of slotNotifications) {
626
- if (slotNotification.slot > lastValidBlockHeight) {
627
- throw new Error(
628
- "The network has progressed past the last block for which this transaction could have committed."
629
- );
644
+ const [slotNotifications, { blockHeight, differenceBetweenSlotHeightAndBlockHeight }] = await Promise.all([
645
+ rpcSubscriptions.slotNotifications().subscribe({ abortSignal: abortController.signal }),
646
+ getBlockHeightAndDifferenceBetweenSlotHeightAndBlockHeight()
647
+ ]);
648
+ if (blockHeight <= lastValidBlockHeight) {
649
+ let lastKnownDifferenceBetweenSlotHeightAndBlockHeight = differenceBetweenSlotHeightAndBlockHeight;
650
+ for await (const slotNotification of slotNotifications) {
651
+ const { slot } = slotNotification;
652
+ if (slot - lastKnownDifferenceBetweenSlotHeightAndBlockHeight > lastValidBlockHeight) {
653
+ const {
654
+ blockHeight: currentBlockHeight,
655
+ differenceBetweenSlotHeightAndBlockHeight: currentDifferenceBetweenSlotHeightAndBlockHeight
656
+ } = await getBlockHeightAndDifferenceBetweenSlotHeightAndBlockHeight();
657
+ if (currentBlockHeight > lastValidBlockHeight) {
658
+ break;
659
+ } else {
660
+ lastKnownDifferenceBetweenSlotHeightAndBlockHeight = currentDifferenceBetweenSlotHeightAndBlockHeight;
661
+ }
662
+ }
630
663
  }
631
664
  }
665
+ throw new Error(
666
+ "The network has progressed past the last block for which this transaction could have been committed."
667
+ );
632
668
  } finally {
633
669
  abortController.abort();
634
670
  }
@@ -720,7 +756,10 @@ function createDefaultRecentTransactionConfirmer({
720
756
  rpc,
721
757
  rpcSubscriptions
722
758
  }) {
723
- const getBlockHeightExceedencePromise = createBlockHeightExceedencePromiseFactory(rpcSubscriptions);
759
+ const getBlockHeightExceedencePromise = createBlockHeightExceedencePromiseFactory({
760
+ rpc,
761
+ rpcSubscriptions
762
+ });
724
763
  const getRecentSignatureConfirmationPromise = createRecentSignatureConfirmationPromiseFactory(
725
764
  rpc,
726
765
  rpcSubscriptions
@@ -753,10 +792,16 @@ async function waitForRecentTransactionConfirmation(config) {
753
792
  await raceStrategies(
754
793
  transactions.getSignatureFromTransaction(config.transaction),
755
794
  config,
756
- function getSpecificStrategiesForRace({ abortSignal, getBlockHeightExceedencePromise, transaction }) {
795
+ function getSpecificStrategiesForRace({
796
+ abortSignal,
797
+ commitment,
798
+ getBlockHeightExceedencePromise,
799
+ transaction
800
+ }) {
757
801
  return [
758
802
  getBlockHeightExceedencePromise({
759
803
  abortSignal,
804
+ commitment,
760
805
  lastValidBlockHeight: transaction.lifetimeConstraint.lastValidBlockHeight
761
806
  })
762
807
  ];
@@ -896,12 +941,24 @@ exports.sendAndConfirmDurableNonceTransaction = sendAndConfirmDurableNonceTransa
896
941
  exports.sendAndConfirmTransaction = sendAndConfirmTransaction;
897
942
  exports.waitForDurableNonceTransactionConfirmation = waitForDurableNonceTransactionConfirmation;
898
943
  exports.waitForRecentTransactionConfirmation = waitForRecentTransactionConfirmation;
944
+ Object.keys(accounts).forEach(function (k) {
945
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
946
+ enumerable: true,
947
+ get: function () { return accounts[k]; }
948
+ });
949
+ });
899
950
  Object.keys(addresses).forEach(function (k) {
900
951
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
901
952
  enumerable: true,
902
953
  get: function () { return addresses[k]; }
903
954
  });
904
955
  });
956
+ Object.keys(functional).forEach(function (k) {
957
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
958
+ enumerable: true,
959
+ get: function () { return functional[k]; }
960
+ });
961
+ });
905
962
  Object.keys(instructions).forEach(function (k) {
906
963
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
907
964
  enumerable: true,