@solana/web3.js 1.95.2 → 2.0.0-20241006045741

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 (110) hide show
  1. package/README.md +128 -113
  2. package/dist/index.browser.cjs +433 -0
  3. package/dist/index.browser.cjs.map +1 -0
  4. package/dist/index.browser.mjs +343 -0
  5. package/dist/index.browser.mjs.map +1 -0
  6. package/dist/index.development.js +8071 -0
  7. package/dist/index.development.js.map +1 -0
  8. package/dist/index.native.mjs +343 -0
  9. package/dist/index.native.mjs.map +1 -0
  10. package/dist/index.node.cjs +433 -0
  11. package/dist/index.node.cjs.map +1 -0
  12. package/dist/index.node.mjs +343 -0
  13. package/dist/index.node.mjs.map +1 -0
  14. package/dist/index.production.min.js +584 -0
  15. package/dist/types/airdrop-internal.d.ts +16 -0
  16. package/dist/types/airdrop-internal.d.ts.map +1 -0
  17. package/dist/types/airdrop.d.ts +18 -0
  18. package/dist/types/airdrop.d.ts.map +1 -0
  19. package/dist/types/compute-limit-internal.d.ts +70 -0
  20. package/dist/types/compute-limit-internal.d.ts.map +1 -0
  21. package/dist/types/compute-limit.d.ts +10 -0
  22. package/dist/types/compute-limit.d.ts.map +1 -0
  23. package/dist/types/decode-transaction-message.d.ts +9 -0
  24. package/dist/types/decode-transaction-message.d.ts.map +1 -0
  25. package/dist/types/index.d.ts +22 -0
  26. package/dist/types/index.d.ts.map +1 -0
  27. package/dist/types/send-and-confirm-durable-nonce-transaction.d.ts +18 -0
  28. package/dist/types/send-and-confirm-durable-nonce-transaction.d.ts.map +1 -0
  29. package/dist/types/send-and-confirm-transaction.d.ts +18 -0
  30. package/dist/types/send-and-confirm-transaction.d.ts.map +1 -0
  31. package/dist/types/send-transaction-internal.d.ts +26 -0
  32. package/dist/types/send-transaction-internal.d.ts.map +1 -0
  33. package/dist/types/send-transaction-without-confirming.d.ts +10 -0
  34. package/dist/types/send-transaction-without-confirming.d.ts.map +1 -0
  35. package/package.json +88 -57
  36. package/lib/index.browser.cjs.js +0 -10559
  37. package/lib/index.browser.cjs.js.map +0 -1
  38. package/lib/index.browser.esm.js +0 -10458
  39. package/lib/index.browser.esm.js.map +0 -1
  40. package/lib/index.cjs.js +0 -11256
  41. package/lib/index.cjs.js.map +0 -1
  42. package/lib/index.d.ts +0 -4021
  43. package/lib/index.esm.js +0 -11151
  44. package/lib/index.esm.js.map +0 -1
  45. package/lib/index.iife.js +0 -25175
  46. package/lib/index.iife.js.map +0 -1
  47. package/lib/index.iife.min.js +0 -19
  48. package/lib/index.iife.min.js.map +0 -1
  49. package/lib/index.native.js +0 -10559
  50. package/lib/index.native.js.map +0 -1
  51. package/src/__forks__/browser/fetch-impl.ts +0 -4
  52. package/src/__forks__/react-native/fetch-impl.ts +0 -4
  53. package/src/account-data.ts +0 -39
  54. package/src/account.ts +0 -55
  55. package/src/blockhash.ts +0 -4
  56. package/src/bpf-loader-deprecated.ts +0 -5
  57. package/src/bpf-loader.ts +0 -50
  58. package/src/connection.ts +0 -6951
  59. package/src/epoch-schedule.ts +0 -102
  60. package/src/errors.ts +0 -133
  61. package/src/fee-calculator.ts +0 -18
  62. package/src/fetch-impl.ts +0 -16
  63. package/src/index.ts +0 -24
  64. package/src/instruction.ts +0 -58
  65. package/src/keypair.ts +0 -102
  66. package/src/layout.ts +0 -188
  67. package/src/loader.ts +0 -267
  68. package/src/message/account-keys.ts +0 -79
  69. package/src/message/compiled-keys.ts +0 -165
  70. package/src/message/index.ts +0 -47
  71. package/src/message/legacy.ts +0 -323
  72. package/src/message/v0.ts +0 -513
  73. package/src/message/versioned.ts +0 -36
  74. package/src/nonce-account.ts +0 -82
  75. package/src/programs/address-lookup-table/index.ts +0 -435
  76. package/src/programs/address-lookup-table/state.ts +0 -84
  77. package/src/programs/compute-budget.ts +0 -281
  78. package/src/programs/ed25519.ts +0 -157
  79. package/src/programs/index.ts +0 -7
  80. package/src/programs/secp256k1.ts +0 -228
  81. package/src/programs/stake.ts +0 -952
  82. package/src/programs/system.ts +0 -1048
  83. package/src/programs/vote.ts +0 -586
  84. package/src/publickey.ts +0 -259
  85. package/src/rpc-websocket.ts +0 -75
  86. package/src/sysvar.ts +0 -37
  87. package/src/timing.ts +0 -23
  88. package/src/transaction/constants.ts +0 -12
  89. package/src/transaction/expiry-custom-errors.ts +0 -48
  90. package/src/transaction/index.ts +0 -5
  91. package/src/transaction/legacy.ts +0 -970
  92. package/src/transaction/message.ts +0 -140
  93. package/src/transaction/versioned.ts +0 -127
  94. package/src/utils/assert.ts +0 -8
  95. package/src/utils/bigint.ts +0 -43
  96. package/src/utils/borsh-schema.ts +0 -38
  97. package/src/utils/cluster.ts +0 -35
  98. package/src/utils/ed25519.ts +0 -43
  99. package/src/utils/guarded-array-utils.ts +0 -34
  100. package/src/utils/index.ts +0 -5
  101. package/src/utils/makeWebsocketUrl.ts +0 -26
  102. package/src/utils/promise-timeout.ts +0 -14
  103. package/src/utils/secp256k1.ts +0 -11
  104. package/src/utils/send-and-confirm-raw-transaction.ts +0 -110
  105. package/src/utils/send-and-confirm-transaction.ts +0 -106
  106. package/src/utils/shortvec-encoding.ts +0 -28
  107. package/src/utils/sleep.ts +0 -4
  108. package/src/utils/to-buffer.ts +0 -11
  109. package/src/validator-info.ts +0 -108
  110. package/src/vote-account.ts +0 -236
package/README.md CHANGED
@@ -6,150 +6,165 @@
6
6
 
7
7
  [code-style-prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square
8
8
  [code-style-prettier-url]: https://github.com/prettier/prettier
9
- [npm-downloads-image]: https://img.shields.io/npm/dm/@solana/web3.js.svg?style=flat
10
- [npm-image]: https://img.shields.io/npm/v/@solana/web3.js.svg?style=flat
11
- [npm-url]: https://www.npmjs.com/package/@solana/web3.js
9
+ [npm-downloads-image]: https://img.shields.io/npm/dm/@solana/web3.js/rc.svg?style=flat
10
+ [npm-image]: https://img.shields.io/npm/v/@solana/web3.js/rc.svg?style=flat
11
+ [npm-url]: https://www.npmjs.com/package/@solana/web3.js/v/rc
12
12
  [semantic-release-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
13
13
  [semantic-release-url]: https://github.com/semantic-release/semantic-release
14
14
 
15
- # Solana JavaScript SDK
15
+ # @solana/web3.js
16
16
 
17
- Use this to interact with accounts and programs on the Solana network through the Solana [JSON RPC API](https://solana.com/docs/rpc).
17
+ This is the JavaScript SDK for building Solana apps for Node, web, and React Native.
18
18
 
19
- ## Installation
19
+ ## Functions
20
20
 
21
- ### For use in Node.js or a web application
21
+ In addition to reexporting functions from packages in the `@solana/*` namespace, this package offers additional helpers for building Solana applications, with sensible defaults.
22
22
 
23
- ```
24
- $ npm install --save @solana/web3.js
25
- ```
23
+ ### `airdropFactory({rpc, rpcSubscriptions})`
24
+
25
+ Returns a function that you can call to airdrop a certain amount of `Lamports` to a Solana address.
26
26
 
27
- ### For use in a browser, without a build system
27
+ ```ts
28
+ import {
29
+ address,
30
+ airdropFactory,
31
+ createSolanaRpc,
32
+ createSolanaRpcSubscriptions,
33
+ devnet,
34
+ lamports,
35
+ } from '@solana/web3.js';
28
36
 
29
- ```html
30
- <!-- Development (un-minified) -->
31
- <script src="https://unpkg.com/@solana/web3.js@latest/lib/index.iife.js"></script>
37
+ const rpc = createSolanaRpc(devnet('http://127.0.0.1:8899'));
38
+ const rpcSubscriptions = createSolanaRpcSubscriptions(devnet('ws://127.0.0.1:8900'));
32
39
 
33
- <!-- Production (minified) -->
34
- <script src="https://unpkg.com/@solana/web3.js@latest/lib/index.iife.min.js"></script>
40
+ const airdrop = airdropFactory({ rpc, rpcSubscriptions });
41
+
42
+ await airdrop({
43
+ commitment: 'confirmed',
44
+ recipientAddress: address('FnHyam9w4NZoWR6mKN1CuGBritdsEWZQa4Z4oawLZGxa'),
45
+ lamports: lamports(10_000_000n),
46
+ });
35
47
  ```
36
48
 
37
- ## Documentation and examples
49
+ > [!NOTE] This only works on test clusters.
38
50
 
39
- - [The Solana Cookbook](https://solanacookbook.com/) has extensive task-based documentation using this library.
40
- - For more detail on individual functions, see the [latest API Documentation](https://solana-labs.github.io/solana-web3.js)
51
+ ### `decodeTransactionMessage(compiledTransactionMessage, rpc, config)`
41
52
 
42
- ## Getting help
53
+ Returns a `TransactionMessage` from a `CompiledTransactionMessage`. If any of the accounts in the compiled message require an address lookup table to find their address, this function will use the supplied RPC instance to fetch the contents of the address lookup table from the network.
43
54
 
44
- Have a question or a problem? Check the [Solana Stack Exchange](https://solana.stackexchange.com) to see if anyone else is having the same one. If not, [post a new question](https://solana.stackexchange.com/questions/ask).
55
+ ### `fetchLookupTables(lookupTableAddresses, rpc, config)`
45
56
 
46
- Include:
57
+ Given a list of addresses belonging to address lookup tables, returns a map of lookup table addresses to an ordered array of the addresses they contain.
47
58
 
48
- - A detailed description of what you're trying to achieve
49
- - Source code, if possible
50
- - The text of any errors you encountered, with stacktraces if available
59
+ ### `getComputeUnitEstimateForTransactionMessageFactory({rpc})`
51
60
 
52
- ## Compatibility
61
+ Correctly budgeting a compute unit limit for your transaction message can increase the probability that your transaction will be accepted for processing. If you don't declare a compute unit limit on your transaction, validators will assume an upper limit of 200K compute units (CU) per instruction.
53
62
 
54
- This library requires a JavaScript runtime that supports [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) and the [exponentiation operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Exponentiation). Both are supported in the following runtimes:
63
+ Since validators have an incentive to pack as many transactions into each block as possible, they may choose to include transactions that they know will fit into the remaining compute budget for the current block over transactions that might not. For this reason, you should set a compute unit limit on each of your transaction messages, whenever possible.
55
64
 
56
- - Browsers, by [release date](https://caniuse.com/bigint):
57
- - Chrome: May 2018
58
- - Firefox: July 2019
59
- - Safari: September 2020
60
- - Mobile Safari: September 2020
61
- - Edge: January 2020
62
- - Opera: June 2018
63
- - Samsung Internet: April 2019
64
- - Runtimes, [by version](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt):
65
- - Deno: >=1.0
66
- - Node: >=10.4.0
67
- - React Native:
68
- - \>=0.7.0 using the [Hermes](https://reactnative.dev/blog/2022/07/08/hermes-as-the-default) engine ([integration guide](https://solanacookbook.com/integrations/react-native.html#how-to-use-solana-web3-js-in-a-react-native-app)):
65
+ Use this utility to estimate the actual compute unit cost of a given transaction message.
69
66
 
70
- ## Development environment setup
67
+ ```ts
68
+ import { getSetComputeUnitLimitInstruction } from '@solana-program/compute-budget';
69
+ import { createSolanaRpc, getComputeUnitEstimateForTransactionMessageFactory, pipe } from '@solana/web3.js';
71
70
 
72
- ### Testing
71
+ // Create an estimator function.
72
+ const rpc = createSolanaRpc('http://127.0.0.1:8899');
73
+ const getComputeUnitEstimateForTransactionMessage = getComputeUnitEstimateForTransactionMessageFactory({
74
+ rpc,
75
+ });
73
76
 
74
- #### Unit tests
77
+ // Create your transaction message.
78
+ const transactionMessage = pipe(
79
+ createTransactionMessage({ version: 'legacy' }),
80
+ /* ... */
81
+ );
75
82
 
76
- To run the full suite of unit tests, execute the following in the root:
83
+ // Request an estimate of the actual compute units this message will consume.
84
+ const computeUnitsEstimate = await getComputeUnitEstimateForTransactionMessage(transactionMessage);
77
85
 
78
- ```shell
79
- $ npm test
86
+ // Set the transaction message's compute unit budget.
87
+ const transactionMessageWithComputeUnitLimit = prependTransactionMessageInstruction(
88
+ getSetComputeUnitLimitInstruction({ units: computeUnitsEstimate }),
89
+ transactionMessage,
90
+ );
80
91
  ```
81
92
 
82
- #### Integration tests
93
+ > [!WARNING]
94
+ > The compute unit estimate is just that &ndash; an estimate. The compute unit consumption of the actual transaction might be higher or lower than what was observed in simulation. Unless you are confident that your particular transaction message will consume the same or fewer compute units as was estimated, you might like to augment the estimate by either a fixed number of CUs or a multiplier.
95
+
96
+ > [!NOTE]
97
+ > If you are preparing an _unsigned_ transaction, destined to be signed and submitted to the network by a wallet, you might like to leave it up to the wallet to determine the compute unit limit. Consider that the wallet might have a more global view of how many compute units certain types of transactions consume, and might be able to make better estimates of an appropriate compute unit budget.
98
+
99
+ ### `sendAndConfirmDurableNonceTransactionFactory({rpc, rpcSubscriptions})`
100
+
101
+ Returns a function that you can call to send a nonce-based transaction to the network and to wait until it has been confirmed.
102
+
103
+ ```ts
104
+ import {
105
+ isSolanaError,
106
+ sendAndConfirmDurableNonceTransactionFactory,
107
+ SOLANA_ERROR__INVALID_NONCE,
108
+ SOLANA_ERROR__NONCE_ACCOUNT_NOT_FOUND,
109
+ } from '@solana/web3.js';
110
+
111
+ const sendAndConfirmNonceTransaction = sendAndConfirmDurableNonceTransactionFactory({ rpc, rpcSubscriptions });
112
+
113
+ try {
114
+ await sendAndConfirmNonceTransaction(transaction, { commitment: 'confirmed' });
115
+ } catch (e) {
116
+ if (isSolanaError(e, SOLANA_ERROR__NONCE_ACCOUNT_NOT_FOUND)) {
117
+ console.error(
118
+ 'The lifetime specified by this transaction refers to a nonce account ' +
119
+ `\`${e.context.nonceAccountAddress}\` that does not exist`,
120
+ );
121
+ } else if (isSolanaError(e, SOLANA_ERROR__INVALID_NONCE)) {
122
+ console.error('This transaction depends on a nonce that is no longer valid');
123
+ } else {
124
+ throw e;
125
+ }
126
+ }
127
+ ```
83
128
 
84
- Integration tests require a validator client running on your machine.
129
+ ### `sendAndConfirmTransactionFactory({rpc, rpcSubscriptions})`
85
130
 
86
- To install a test validator:
131
+ Returns a function that you can call to send a blockhash-based transaction to the network and to wait until it has been confirmed.
87
132
 
88
- ```shell
89
- $ npm run test:live-with-test-validator:setup
90
- ```
133
+ ```ts
134
+ import { isSolanaError, sendAndConfirmTransactionFactory, SOLANA_ERROR__BLOCK_HEIGHT_EXCEEDED } from '@solana/web3.js';
91
135
 
92
- To start the test validator and run all of the integration tests in live mode:
136
+ const sendAndConfirmTransaction = sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions });
93
137
 
94
- ```shell
95
- $ cd packages/library-legacy
96
- $ npm run test:live-with-test-validator
138
+ try {
139
+ await sendAndConfirmTransaction(transaction, { commitment: 'confirmed' });
140
+ } catch (e) {
141
+ if (isSolanaError(e, SOLANA_ERROR__BLOCK_HEIGHT_EXCEEDED)) {
142
+ console.error('This transaction depends on a blockhash that has expired');
143
+ } else {
144
+ throw e;
145
+ }
146
+ }
97
147
  ```
98
148
 
99
- ### Speed up build times with remote caching
100
-
101
- Cache build artifacts remotely so that you, others, and the CI server can take advantage of each others' build efforts.
102
-
103
- 1. Log the Turborepo CLI into the Solana Vercel account
104
- ```shell
105
- pnpm turbo login
106
- ```
107
- 2. Link the repository to the remote cache
108
- ```shell
109
- pnpm turbo link
110
- ```
111
-
112
- ## Contributing
113
-
114
- If you found a bug or would like to request a feature, please [file an issue](https://github.com/solana-labs/solana-web3.js/issues/new). If, based on the discussion on an issue you would like to offer a code change, please make a [pull request](https://github.com/solana-labs/solana-web3.js/compare). If neither of these describes what you would like to contribute, read the [getting help](#getting-help) section above.
115
-
116
- ## Disclaimer
117
-
118
- All claims, content, designs, algorithms, estimates, roadmaps,
119
- specifications, and performance measurements described in this project
120
- are done with the Solana Foundation's ("SF") best efforts. It is up to
121
- the reader to check and validate their accuracy and truthfulness.
122
- Furthermore nothing in this project constitutes a solicitation for
123
- investment.
124
-
125
- Any content produced by SF or developer resources that SF provides, are
126
- for educational and inspiration purposes only. SF does not encourage,
127
- induce or sanction the deployment, integration or use of any such
128
- applications (including the code comprising the Solana blockchain
129
- protocol) in violation of applicable laws or regulations and hereby
130
- prohibits any such deployment, integration or use. This includes use of
131
- any such applications by the reader (a) in violation of export control
132
- or sanctions laws of the United States or any other applicable
133
- jurisdiction, (b) if the reader is located in or ordinarily resident in
134
- a country or territory subject to comprehensive sanctions administered
135
- by the U.S. Office of Foreign Assets Control (OFAC), or (c) if the
136
- reader is or is working on behalf of a Specially Designated National
137
- (SDN) or a person subject to similar blocking or denied party
138
- prohibitions.
139
-
140
- The reader should be aware that U.S. export control and sanctions laws
141
- prohibit U.S. persons (and other persons that are subject to such laws)
142
- from transacting with persons in certain countries and territories or
143
- that are on the SDN list. As a project based primarily on open-source
144
- software, it is possible that such sanctioned persons may nevertheless
145
- bypass prohibitions, obtain the code comprising the Solana blockchain
146
- protocol (or other project code or applications) and deploy, integrate,
147
- or otherwise use it. Accordingly, there is a risk to individuals that
148
- other persons using the Solana blockchain protocol may be sanctioned
149
- persons and that transactions with such persons would be a violation of
150
- U.S. export controls and sanctions law. This risk applies to
151
- individuals, organizations, and other ecosystem participants that
152
- deploy, integrate, or use the Solana blockchain protocol code directly
153
- (e.g., as a node operator), and individuals that transact on the Solana
154
- blockchain through light clients, third party interfaces, and/or wallet
155
- software.
149
+ ### `sendTransactionWithoutConfirmingFactory({rpc, rpcSubscriptions})`
150
+
151
+ Returns a function that you can call to send a transaction with any kind of lifetime to the network without waiting for it to be confirmed.
152
+
153
+ ```ts
154
+ import {
155
+ sendTransactionWithoutConfirmingFactory,
156
+ SOLANA_ERROR__JSON_RPC__SERVER_ERROR_SEND_TRANSACTION_PREFLIGHT_FAILURE,
157
+ } from '@solana/web3.js';
158
+
159
+ const sendTransaction = sendTransactionWithoutConfirmingFactory({ rpc });
160
+
161
+ try {
162
+ await sendTransaction(transaction, { commitment: 'confirmed' });
163
+ } catch (e) {
164
+ if (isSolanaError(e, SOLANA_ERROR__JSON_RPC__SERVER_ERROR_SEND_TRANSACTION_PREFLIGHT_FAILURE)) {
165
+ console.error('The transaction failed in simulation', e.cause);
166
+ } else {
167
+ throw e;
168
+ }
169
+ }
170
+ ```