@solana/web3.js 2.0.0-experimental.ffeddf6 → 2.0.0-preview.1
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 +70 -28
- package/dist/index.browser.cjs +176 -785
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.js +125 -764
- package/dist/index.browser.js.map +1 -1
- package/dist/index.development.js +5376 -2956
- package/dist/index.development.js.map +1 -1
- package/dist/index.native.js +125 -753
- package/dist/index.native.js.map +1 -1
- package/dist/index.node.cjs +176 -774
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.js +125 -753
- package/dist/index.node.js.map +1 -1
- package/dist/index.production.min.js +286 -78
- package/dist/types/airdrop-internal.d.ts +16 -0
- package/dist/types/airdrop-internal.d.ts.map +1 -0
- package/dist/types/airdrop.d.ts +8 -19
- package/dist/types/airdrop.d.ts.map +1 -1
- package/dist/types/decode-transaction.d.ts +9 -0
- package/dist/types/decode-transaction.d.ts.map +1 -0
- package/dist/types/index.d.ts +11 -9
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/send-transaction-internal.d.ts +27 -0
- package/dist/types/send-transaction-internal.d.ts.map +1 -0
- package/dist/types/send-transaction.d.ts +17 -31
- package/dist/types/send-transaction.d.ts.map +1 -1
- package/package.json +24 -40
- package/dist/types/airdrop-confirmer.d.ts +0 -20
- package/dist/types/airdrop-confirmer.d.ts.map +0 -1
- package/dist/types/cached-abortable-iterable.d.ts +0 -11
- package/dist/types/cached-abortable-iterable.d.ts.map +0 -1
- package/dist/types/rpc-default-config.d.ts +0 -3
- package/dist/types/rpc-default-config.d.ts.map +0 -1
- package/dist/types/rpc-integer-overflow-error.d.ts +0 -8
- package/dist/types/rpc-integer-overflow-error.d.ts.map +0 -1
- package/dist/types/rpc-request-coalescer.d.ts +0 -5
- package/dist/types/rpc-request-coalescer.d.ts.map +0 -1
- package/dist/types/rpc-request-deduplication.d.ts +0 -2
- package/dist/types/rpc-request-deduplication.d.ts.map +0 -1
- package/dist/types/rpc-subscription-coalescer.d.ts +0 -10
- package/dist/types/rpc-subscription-coalescer.d.ts.map +0 -1
- package/dist/types/rpc-transport.d.ts +0 -4
- package/dist/types/rpc-transport.d.ts.map +0 -1
- package/dist/types/rpc-websocket-autopinger.d.ts +0 -8
- package/dist/types/rpc-websocket-autopinger.d.ts.map +0 -1
- package/dist/types/rpc-websocket-connection-sharding.d.ts +0 -13
- package/dist/types/rpc-websocket-connection-sharding.d.ts.map +0 -1
- package/dist/types/rpc-websocket-transport.d.ts +0 -13
- package/dist/types/rpc-websocket-transport.d.ts.map +0 -1
- package/dist/types/rpc.d.ts +0 -7
- package/dist/types/rpc.d.ts.map +0 -1
- package/dist/types/transaction-confirmation-strategy-blockheight.d.ts +0 -10
- package/dist/types/transaction-confirmation-strategy-blockheight.d.ts.map +0 -1
- package/dist/types/transaction-confirmation-strategy-nonce.d.ts +0 -15
- package/dist/types/transaction-confirmation-strategy-nonce.d.ts.map +0 -1
- package/dist/types/transaction-confirmation-strategy-racer.d.ts +0 -14
- package/dist/types/transaction-confirmation-strategy-racer.d.ts.map +0 -1
- package/dist/types/transaction-confirmation-strategy-recent-signature.d.ts +0 -13
- package/dist/types/transaction-confirmation-strategy-recent-signature.d.ts.map +0 -1
- package/dist/types/transaction-confirmation-strategy-timeout.d.ts +0 -8
- package/dist/types/transaction-confirmation-strategy-timeout.d.ts.map +0 -1
- package/dist/types/transaction-confirmation.d.ts +0 -37
- package/dist/types/transaction-confirmation.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -34,17 +34,17 @@ In response to your feedback, we began a process of modernizing the library to p
|
|
|
34
34
|
### For use in Node.js or a web application
|
|
35
35
|
|
|
36
36
|
```shell
|
|
37
|
-
npm install --save @solana/web3.js@
|
|
37
|
+
npm install --save @solana/web3.js@tp
|
|
38
38
|
```
|
|
39
39
|
|
|
40
40
|
### For use in a browser, without a build system
|
|
41
41
|
|
|
42
42
|
```html
|
|
43
43
|
<!-- Development (debug mode, unminified) -->
|
|
44
|
-
<script src="https://unpkg.com/@solana/web3.js@
|
|
44
|
+
<script src="https://unpkg.com/@solana/web3.js@tp/dist/index.development.js"></script>
|
|
45
45
|
|
|
46
46
|
<!-- Production (minified) -->
|
|
47
|
-
<script src="https://unpkg.com/@solana/web3.js@
|
|
47
|
+
<script src="https://unpkg.com/@solana/web3.js@tp/dist/index.production.min.js"></script>
|
|
48
48
|
```
|
|
49
49
|
|
|
50
50
|
What follows is an overview of *why* the library was re-engineered, what changes have been introduced, and how the JavaScript landscape might look across Solana in the near future.
|
|
@@ -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
|
|
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,7 +362,50 @@ 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
|
-
|
|
365
|
+
import { createDefaultRpcTransport } from '@solana/web3.js';
|
|
366
|
+
import { createJsonRpc, IRpcTransport } from '@solana/rpc-transport';
|
|
367
|
+
import { createSolanaRpcApi } from '@solana/rpc-core';
|
|
368
|
+
|
|
369
|
+
// Set the maximum number of attempts to retry a request
|
|
370
|
+
const MAX_ATTEMPTS = 4;
|
|
371
|
+
|
|
372
|
+
// Create the default transport
|
|
373
|
+
const defaultTransport = createDefaultRpcTransport({ url: 'https://mainnet-beta.my-server-1.com' });
|
|
374
|
+
|
|
375
|
+
// Sleep function to wait for a given number of milliseconds
|
|
376
|
+
function sleep(ms: number): Promise<void> {
|
|
377
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// Calculate the delay for a given attempt
|
|
381
|
+
function calculateRetryDelay(attempt: number): number {
|
|
382
|
+
// Exponential backoff with a maximum of 1.5 seconds
|
|
383
|
+
return Math.min(100 * Math.pow(2, attempt), 1500);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// A retrying transport that will retry up to MAX_ATTEMPTS times before failing
|
|
387
|
+
async function retryingTransport<TResponse>(...args: Parameters<IRpcTransport>): Promise<TResponse> {
|
|
388
|
+
let requestError;
|
|
389
|
+
for (let attempts = 0; attempts < MAX_ATTEMPTS; attempts++) {
|
|
390
|
+
try {
|
|
391
|
+
return await defaultTransport(...args);
|
|
392
|
+
} catch (err) {
|
|
393
|
+
requestError = err;
|
|
394
|
+
// Only sleep if we have more attempts remaining
|
|
395
|
+
if (attempts < MAX_ATTEMPTS - 1) {
|
|
396
|
+
const retryDelay = calculateRetryDelay(attempts);
|
|
397
|
+
await sleep(retryDelay);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
throw requestError;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// Create the RPC client
|
|
405
|
+
const rpc = createJsonRpc({
|
|
406
|
+
api: createSolanaRpcApi(),
|
|
407
|
+
transport: retryingTransport,
|
|
408
|
+
});
|
|
368
409
|
```
|
|
369
410
|
|
|
370
411
|
### Failover
|
|
@@ -381,15 +422,14 @@ Perhaps your application needs to make a large number of requests, or needs to f
|
|
|
381
422
|
|
|
382
423
|
```tsx
|
|
383
424
|
import { createSolanaRpcApi } from '@solana/rpc-core';
|
|
384
|
-
import { createJsonRpc } from '@solana/rpc-transport';
|
|
385
|
-
import { IRpcTransport } from '@solana/rpc-transport/dist/types/transports/transport-types';
|
|
425
|
+
import { createJsonRpc, IRpcTransport } from '@solana/rpc-transport';
|
|
386
426
|
import { createDefaultRpcTransport } from '@solana/web3.js';
|
|
387
427
|
|
|
388
428
|
// Create multiple transports
|
|
389
|
-
const transportA = createDefaultRpcTransport({ url: 'https://mainnet-beta.my-server-1.com' })
|
|
390
|
-
const transportB = createDefaultRpcTransport({ url: 'https://mainnet-beta.my-server-2.com' })
|
|
391
|
-
const transportC = createDefaultRpcTransport({ url: 'https://mainnet-beta.my-server-3.com' })
|
|
392
|
-
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' });
|
|
393
433
|
|
|
394
434
|
// Function to determine which shard to use based on the request method
|
|
395
435
|
function selectShard(method: string): IRpcTransport {
|
|
@@ -507,7 +547,7 @@ for await (const notification of accountNotifications) {
|
|
|
507
547
|
|
|
508
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`.
|
|
509
549
|
|
|
510
|
-
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-
|
|
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.
|
|
511
551
|
|
|
512
552
|
Here’s an example of such logic:
|
|
513
553
|
|
|
@@ -548,7 +588,7 @@ const keyPair: CryptoKeyPair = await generateKeyPair();
|
|
|
548
588
|
const message = new Uint8Array(8).fill(0);
|
|
549
589
|
|
|
550
590
|
const signedMessage = await signBytes(keyPair.privateKey, message);
|
|
551
|
-
// ^
|
|
591
|
+
// ^ Signature
|
|
552
592
|
|
|
553
593
|
const verified = await verifySignature(keyPair.publicKey, signedMessage, message);
|
|
554
594
|
```
|
|
@@ -576,21 +616,21 @@ Operations on `CryptoKey` objects using the Web Crypto API *or* the polyfill are
|
|
|
576
616
|
|
|
577
617
|
### String Addresses
|
|
578
618
|
|
|
579
|
-
All addresses are now JavaScript strings. They are represented by the opaque type `
|
|
619
|
+
All addresses are now JavaScript strings. They are represented by the opaque type `Address`, which describes exactly what a Solana address actually is.
|
|
580
620
|
|
|
581
621
|
Consequently, that means no more `PublicKey`.
|
|
582
622
|
|
|
583
623
|
Here’s what they look like in development:
|
|
584
624
|
|
|
585
625
|
```tsx
|
|
586
|
-
import { address, getAddressFromPublicKey, generateKeyPair
|
|
626
|
+
import { Address, address, getAddressFromPublicKey, generateKeyPair } from '@solana/web3.js';
|
|
587
627
|
|
|
588
|
-
// Coerce a string to
|
|
628
|
+
// Coerce a string to an `Address`
|
|
589
629
|
const myOtherAddress = address('AxZfZWeqztBCL37Mkjkd4b8Hf6J13WCcfozrBY6vZzv3');
|
|
590
630
|
|
|
591
631
|
// Typecast it instead
|
|
592
632
|
const myAddress =
|
|
593
|
-
'AxZfZWeqztBCL37Mkjkd4b8Hf6J13WCcfozrBY6vZzv3' as
|
|
633
|
+
'AxZfZWeqztBCL37Mkjkd4b8Hf6J13WCcfozrBY6vZzv3' as Address<'AxZfZWeqztBCL37Mkjkd4b8Hf6J13WCcfozrBY6vZzv3'>;
|
|
594
634
|
|
|
595
635
|
// From CryptoKey
|
|
596
636
|
const keyPair = await generateKeyPair();
|
|
@@ -710,7 +750,7 @@ const transactionSignedWithFeePayer = await signTransaction([signer], transactio
|
|
|
710
750
|
|
|
711
751
|
Transaction objects are also ********frozen by these functions******** to prevent transactions from being mutated in place by functions you pass them to.
|
|
712
752
|
|
|
713
|
-
Building transactions in this manner might feel different
|
|
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:
|
|
714
754
|
|
|
715
755
|
```tsx
|
|
716
756
|
import { pipe } from '@solana/functional';
|
|
@@ -744,6 +784,8 @@ Solana’s codecs libraries are broken up into modular components so you only ne
|
|
|
744
784
|
- `@solana/codecs-data-structures`: Codecs and serializers for structs
|
|
745
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
|
|
746
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
|
+
|
|
747
789
|
Here’s an example of encoding and decoding a custom struct with some strings and numbers:
|
|
748
790
|
|
|
749
791
|
```tsx
|
|
@@ -770,7 +812,7 @@ const myToken = {
|
|
|
770
812
|
};
|
|
771
813
|
|
|
772
814
|
const myEncodedToken: Uint8Array = structCodec.encode(myToken);
|
|
773
|
-
const myDecodedToken = structCodec.decode(myEncodedToken)
|
|
815
|
+
const myDecodedToken = structCodec.decode(myEncodedToken);
|
|
774
816
|
|
|
775
817
|
myDecodedToken satisfies {
|
|
776
818
|
amount: bigint;
|
|
@@ -869,7 +911,7 @@ const blockWithRewardsAndTransactionsResponse = await rpc.getBlock(0n, {
|
|
|
869
911
|
|
|
870
912
|
### Catching Compile-Time Bugs with TypeScript
|
|
871
913
|
|
|
872
|
-
As previously mentioned, the type coverage in web3.js 2.0
|
|
914
|
+
As previously mentioned, the type coverage in web3.js 2.0 allows developers to catch common bugs at compile time, rather than runtime.
|
|
873
915
|
|
|
874
916
|
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:
|
|
875
917
|
|
|
@@ -958,15 +1000,15 @@ const signature = rpc.requestAirdrop(myAddress, airdropAmount).send();With the n
|
|
|
958
1000
|
|
|
959
1001
|
You will have noticed by now that web3.js is a complete and total breaking change from the 1.x line. We want to provide you with a strategy for interacting with 1.x APIs while building your application using 2.0. You need a tool for commuting between 1.x and 2.0 data types.
|
|
960
1002
|
|
|
961
|
-
The `@solana/compat` library allows for interoperability between functions and class objects from the legacy library - such as `VersionedTransaction`, `PublicKey`, and `Keypair` - and functions and types of the new library - such as `
|
|
1003
|
+
The `@solana/compat` library allows for interoperability between functions and class objects from the legacy library - such as `VersionedTransaction`, `PublicKey`, and `Keypair` - and functions and types of the new library - such as `Address`, `Transaction`, and `CryptoKeyPair`.
|
|
962
1004
|
|
|
963
|
-
Here’s how you can use `@solana/compat` to convert from a legacy `PublicKey` to
|
|
1005
|
+
Here’s how you can use `@solana/compat` to convert from a legacy `PublicKey` to an `Address`:
|
|
964
1006
|
|
|
965
1007
|
```tsx
|
|
966
1008
|
import { fromLegacyPublicKey } from '@solana/compat';
|
|
967
1009
|
|
|
968
1010
|
const publicKey = new PublicKey('B3piXWBQLLRuk56XG5VihxR4oe2PSsDM8nTF6s1DeVF5');
|
|
969
|
-
const
|
|
1011
|
+
const address: Address = fromLegacyPublicKey(publicKey);
|
|
970
1012
|
```
|
|
971
1013
|
|
|
972
1014
|
Here’s how to convert from a legacy `Keypair` to a `CryptoKeyPair`:
|