@solana/web3.js 1.74.0 → 2.0.0-experimental.1b52291

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 (102) hide show
  1. package/README.md +55 -112
  2. package/dist/index.browser.cjs +77 -0
  3. package/dist/index.browser.cjs.map +1 -0
  4. package/dist/index.browser.js +68 -0
  5. package/dist/index.browser.js.map +1 -0
  6. package/dist/index.development.js +539 -0
  7. package/dist/index.development.js.map +1 -0
  8. package/dist/index.native.js +70 -0
  9. package/dist/index.native.js.map +1 -0
  10. package/dist/index.node.cjs +79 -0
  11. package/dist/index.node.cjs.map +1 -0
  12. package/dist/index.node.js +70 -0
  13. package/dist/index.node.js.map +1 -0
  14. package/dist/index.production.min.js +13 -0
  15. package/dist/types/index.d.ts +4 -0
  16. package/dist/types/index.d.ts.map +1 -0
  17. package/dist/types/rpc-default-config.d.ts +3 -0
  18. package/dist/types/rpc-default-config.d.ts.map +1 -0
  19. package/dist/types/rpc-integer-overflow-error.d.ts +8 -0
  20. package/dist/types/rpc-integer-overflow-error.d.ts.map +1 -0
  21. package/dist/types/rpc-transport.d.ts +4 -0
  22. package/dist/types/rpc-transport.d.ts.map +1 -0
  23. package/dist/types/rpc.d.ts +5 -0
  24. package/dist/types/rpc.d.ts.map +1 -0
  25. package/package.json +86 -113
  26. package/lib/index.browser.cjs.js +0 -9485
  27. package/lib/index.browser.cjs.js.map +0 -1
  28. package/lib/index.browser.esm.js +0 -9379
  29. package/lib/index.browser.esm.js.map +0 -1
  30. package/lib/index.cjs.js +0 -12009
  31. package/lib/index.cjs.js.map +0 -1
  32. package/lib/index.d.ts +0 -3865
  33. package/lib/index.esm.js +0 -11896
  34. package/lib/index.esm.js.map +0 -1
  35. package/lib/index.iife.js +0 -24568
  36. package/lib/index.iife.js.map +0 -1
  37. package/lib/index.iife.min.js +0 -13
  38. package/lib/index.iife.min.js.map +0 -1
  39. package/lib/index.native.js +0 -9485
  40. package/lib/index.native.js.map +0 -1
  41. package/src/__forks__/browser/fetch-impl.ts +0 -4
  42. package/src/__forks__/browser/rpc-websocket-factory.ts +0 -1
  43. package/src/__forks__/react-native/fetch-impl.ts +0 -4
  44. package/src/__forks__/react-native/rpc-websocket-factory.ts +0 -1
  45. package/src/account-data.ts +0 -39
  46. package/src/account.ts +0 -55
  47. package/src/blockhash.ts +0 -4
  48. package/src/bpf-loader-deprecated.ts +0 -5
  49. package/src/bpf-loader.ts +0 -45
  50. package/src/connection.ts +0 -6678
  51. package/src/epoch-schedule.ts +0 -102
  52. package/src/errors.ts +0 -50
  53. package/src/fee-calculator.ts +0 -18
  54. package/src/fetch-impl.ts +0 -13
  55. package/src/index.ts +0 -24
  56. package/src/instruction.ts +0 -58
  57. package/src/keypair.ts +0 -93
  58. package/src/layout.ts +0 -188
  59. package/src/loader.ts +0 -236
  60. package/src/message/account-keys.ts +0 -79
  61. package/src/message/compiled-keys.ts +0 -165
  62. package/src/message/index.ts +0 -47
  63. package/src/message/legacy.ts +0 -326
  64. package/src/message/v0.ts +0 -496
  65. package/src/message/versioned.ts +0 -36
  66. package/src/nonce-account.ts +0 -82
  67. package/src/programs/address-lookup-table/index.ts +0 -435
  68. package/src/programs/address-lookup-table/state.ts +0 -84
  69. package/src/programs/compute-budget.ts +0 -281
  70. package/src/programs/ed25519.ts +0 -157
  71. package/src/programs/index.ts +0 -7
  72. package/src/programs/secp256k1.ts +0 -228
  73. package/src/programs/stake.ts +0 -935
  74. package/src/programs/system.ts +0 -1048
  75. package/src/programs/vote.ts +0 -541
  76. package/src/publickey.ts +0 -259
  77. package/src/rpc-websocket-factory.ts +0 -4
  78. package/src/rpc-websocket.ts +0 -79
  79. package/src/sysvar.ts +0 -37
  80. package/src/timing.ts +0 -23
  81. package/src/transaction/constants.ts +0 -12
  82. package/src/transaction/expiry-custom-errors.ts +0 -48
  83. package/src/transaction/index.ts +0 -5
  84. package/src/transaction/legacy.ts +0 -908
  85. package/src/transaction/message.ts +0 -140
  86. package/src/transaction/versioned.ts +0 -126
  87. package/src/utils/assert.ts +0 -8
  88. package/src/utils/bigint.ts +0 -43
  89. package/src/utils/borsh-schema.ts +0 -38
  90. package/src/utils/cluster.ts +0 -31
  91. package/src/utils/ed25519.ts +0 -46
  92. package/src/utils/index.ts +0 -5
  93. package/src/utils/makeWebsocketUrl.ts +0 -26
  94. package/src/utils/promise-timeout.ts +0 -14
  95. package/src/utils/secp256k1.ts +0 -18
  96. package/src/utils/send-and-confirm-raw-transaction.ts +0 -102
  97. package/src/utils/send-and-confirm-transaction.ts +0 -98
  98. package/src/utils/shortvec-encoding.ts +0 -28
  99. package/src/utils/sleep.ts +0 -4
  100. package/src/utils/to-buffer.ts +0 -11
  101. package/src/validator-info.ts +0 -104
  102. package/src/vote-account.ts +0 -236
package/src/loader.ts DELETED
@@ -1,236 +0,0 @@
1
- import {Buffer} from 'buffer';
2
- import * as BufferLayout from '@solana/buffer-layout';
3
-
4
- import {PublicKey} from './publickey';
5
- import {Transaction, PACKET_DATA_SIZE} from './transaction';
6
- import {SYSVAR_RENT_PUBKEY} from './sysvar';
7
- import {sendAndConfirmTransaction} from './utils/send-and-confirm-transaction';
8
- import {sleep} from './utils/sleep';
9
- import type {Connection} from './connection';
10
- import type {Signer} from './keypair';
11
- import {SystemProgram} from './programs/system';
12
- import {IInstructionInputData} from './instruction';
13
-
14
- // Keep program chunks under PACKET_DATA_SIZE, leaving enough room for the
15
- // rest of the Transaction fields
16
- //
17
- // TODO: replace 300 with a proper constant for the size of the other
18
- // Transaction fields
19
- const CHUNK_SIZE = PACKET_DATA_SIZE - 300;
20
-
21
- /**
22
- * Program loader interface
23
- */
24
- export class Loader {
25
- /**
26
- * @internal
27
- */
28
- constructor() {}
29
-
30
- /**
31
- * Amount of program data placed in each load Transaction
32
- */
33
- static chunkSize: number = CHUNK_SIZE;
34
-
35
- /**
36
- * Minimum number of signatures required to load a program not including
37
- * retries
38
- *
39
- * Can be used to calculate transaction fees
40
- */
41
- static getMinNumSignatures(dataLength: number): number {
42
- return (
43
- 2 * // Every transaction requires two signatures (payer + program)
44
- (Math.ceil(dataLength / Loader.chunkSize) +
45
- 1 + // Add one for Create transaction
46
- 1) // Add one for Finalize transaction
47
- );
48
- }
49
-
50
- /**
51
- * Loads a generic program
52
- *
53
- * @param connection The connection to use
54
- * @param payer System account that pays to load the program
55
- * @param program Account to load the program into
56
- * @param programId Public key that identifies the loader
57
- * @param data Program octets
58
- * @return true if program was loaded successfully, false if program was already loaded
59
- */
60
- static async load(
61
- connection: Connection,
62
- payer: Signer,
63
- program: Signer,
64
- programId: PublicKey,
65
- data: Buffer | Uint8Array | Array<number>,
66
- ): Promise<boolean> {
67
- {
68
- const balanceNeeded = await connection.getMinimumBalanceForRentExemption(
69
- data.length,
70
- );
71
-
72
- // Fetch program account info to check if it has already been created
73
- const programInfo = await connection.getAccountInfo(
74
- program.publicKey,
75
- 'confirmed',
76
- );
77
-
78
- let transaction: Transaction | null = null;
79
- if (programInfo !== null) {
80
- if (programInfo.executable) {
81
- console.error('Program load failed, account is already executable');
82
- return false;
83
- }
84
-
85
- if (programInfo.data.length !== data.length) {
86
- transaction = transaction || new Transaction();
87
- transaction.add(
88
- SystemProgram.allocate({
89
- accountPubkey: program.publicKey,
90
- space: data.length,
91
- }),
92
- );
93
- }
94
-
95
- if (!programInfo.owner.equals(programId)) {
96
- transaction = transaction || new Transaction();
97
- transaction.add(
98
- SystemProgram.assign({
99
- accountPubkey: program.publicKey,
100
- programId,
101
- }),
102
- );
103
- }
104
-
105
- if (programInfo.lamports < balanceNeeded) {
106
- transaction = transaction || new Transaction();
107
- transaction.add(
108
- SystemProgram.transfer({
109
- fromPubkey: payer.publicKey,
110
- toPubkey: program.publicKey,
111
- lamports: balanceNeeded - programInfo.lamports,
112
- }),
113
- );
114
- }
115
- } else {
116
- transaction = new Transaction().add(
117
- SystemProgram.createAccount({
118
- fromPubkey: payer.publicKey,
119
- newAccountPubkey: program.publicKey,
120
- lamports: balanceNeeded > 0 ? balanceNeeded : 1,
121
- space: data.length,
122
- programId,
123
- }),
124
- );
125
- }
126
-
127
- // If the account is already created correctly, skip this step
128
- // and proceed directly to loading instructions
129
- if (transaction !== null) {
130
- await sendAndConfirmTransaction(
131
- connection,
132
- transaction,
133
- [payer, program],
134
- {
135
- commitment: 'confirmed',
136
- },
137
- );
138
- }
139
- }
140
-
141
- const dataLayout = BufferLayout.struct<
142
- Readonly<{
143
- bytes: number[];
144
- bytesLength: number;
145
- bytesLengthPadding: number;
146
- instruction: number;
147
- offset: number;
148
- }>
149
- >([
150
- BufferLayout.u32('instruction'),
151
- BufferLayout.u32('offset'),
152
- BufferLayout.u32('bytesLength'),
153
- BufferLayout.u32('bytesLengthPadding'),
154
- BufferLayout.seq(
155
- BufferLayout.u8('byte'),
156
- BufferLayout.offset(BufferLayout.u32(), -8),
157
- 'bytes',
158
- ),
159
- ]);
160
-
161
- const chunkSize = Loader.chunkSize;
162
- let offset = 0;
163
- let array = data;
164
- let transactions = [];
165
- while (array.length > 0) {
166
- const bytes = array.slice(0, chunkSize);
167
- const data = Buffer.alloc(chunkSize + 16);
168
- dataLayout.encode(
169
- {
170
- instruction: 0, // Load instruction
171
- offset,
172
- bytes: bytes as number[],
173
- bytesLength: 0,
174
- bytesLengthPadding: 0,
175
- },
176
- data,
177
- );
178
-
179
- const transaction = new Transaction().add({
180
- keys: [{pubkey: program.publicKey, isSigner: true, isWritable: true}],
181
- programId,
182
- data,
183
- });
184
- transactions.push(
185
- sendAndConfirmTransaction(connection, transaction, [payer, program], {
186
- commitment: 'confirmed',
187
- }),
188
- );
189
-
190
- // Delay between sends in an attempt to reduce rate limit errors
191
- if (connection._rpcEndpoint.includes('solana.com')) {
192
- const REQUESTS_PER_SECOND = 4;
193
- await sleep(1000 / REQUESTS_PER_SECOND);
194
- }
195
-
196
- offset += chunkSize;
197
- array = array.slice(chunkSize);
198
- }
199
- await Promise.all(transactions);
200
-
201
- // Finalize the account loaded with program data for execution
202
- {
203
- const dataLayout = BufferLayout.struct<IInstructionInputData>([
204
- BufferLayout.u32('instruction'),
205
- ]);
206
-
207
- const data = Buffer.alloc(dataLayout.span);
208
- dataLayout.encode(
209
- {
210
- instruction: 1, // Finalize instruction
211
- },
212
- data,
213
- );
214
-
215
- const transaction = new Transaction().add({
216
- keys: [
217
- {pubkey: program.publicKey, isSigner: true, isWritable: true},
218
- {pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false},
219
- ],
220
- programId,
221
- data,
222
- });
223
- await sendAndConfirmTransaction(
224
- connection,
225
- transaction,
226
- [payer, program],
227
- {
228
- commitment: 'confirmed',
229
- },
230
- );
231
- }
232
-
233
- // success
234
- return true;
235
- }
236
- }
@@ -1,79 +0,0 @@
1
- import {LoadedAddresses} from '../connection';
2
- import {PublicKey} from '../publickey';
3
- import {TransactionInstruction} from '../transaction';
4
- import {MessageCompiledInstruction} from './index';
5
-
6
- export type AccountKeysFromLookups = LoadedAddresses;
7
-
8
- export class MessageAccountKeys {
9
- staticAccountKeys: Array<PublicKey>;
10
- accountKeysFromLookups?: AccountKeysFromLookups;
11
-
12
- constructor(
13
- staticAccountKeys: Array<PublicKey>,
14
- accountKeysFromLookups?: AccountKeysFromLookups,
15
- ) {
16
- this.staticAccountKeys = staticAccountKeys;
17
- this.accountKeysFromLookups = accountKeysFromLookups;
18
- }
19
-
20
- keySegments(): Array<Array<PublicKey>> {
21
- const keySegments = [this.staticAccountKeys];
22
- if (this.accountKeysFromLookups) {
23
- keySegments.push(this.accountKeysFromLookups.writable);
24
- keySegments.push(this.accountKeysFromLookups.readonly);
25
- }
26
- return keySegments;
27
- }
28
-
29
- get(index: number): PublicKey | undefined {
30
- for (const keySegment of this.keySegments()) {
31
- if (index < keySegment.length) {
32
- return keySegment[index];
33
- } else {
34
- index -= keySegment.length;
35
- }
36
- }
37
- return;
38
- }
39
-
40
- get length(): number {
41
- return this.keySegments().flat().length;
42
- }
43
-
44
- compileInstructions(
45
- instructions: Array<TransactionInstruction>,
46
- ): Array<MessageCompiledInstruction> {
47
- // Bail early if any account indexes would overflow a u8
48
- const U8_MAX = 255;
49
- if (this.length > U8_MAX + 1) {
50
- throw new Error('Account index overflow encountered during compilation');
51
- }
52
-
53
- const keyIndexMap = new Map();
54
- this.keySegments()
55
- .flat()
56
- .forEach((key, index) => {
57
- keyIndexMap.set(key.toBase58(), index);
58
- });
59
-
60
- const findKeyIndex = (key: PublicKey) => {
61
- const keyIndex = keyIndexMap.get(key.toBase58());
62
- if (keyIndex === undefined)
63
- throw new Error(
64
- 'Encountered an unknown instruction account key during compilation',
65
- );
66
- return keyIndex;
67
- };
68
-
69
- return instructions.map((instruction): MessageCompiledInstruction => {
70
- return {
71
- programIdIndex: findKeyIndex(instruction.programId),
72
- accountKeyIndexes: instruction.keys.map(meta =>
73
- findKeyIndex(meta.pubkey),
74
- ),
75
- data: instruction.data,
76
- };
77
- });
78
- }
79
- }
@@ -1,165 +0,0 @@
1
- import {MessageHeader, MessageAddressTableLookup} from './index';
2
- import {AccountKeysFromLookups} from './account-keys';
3
- import {AddressLookupTableAccount} from '../programs';
4
- import {TransactionInstruction} from '../transaction';
5
- import assert from '../utils/assert';
6
- import {PublicKey} from '../publickey';
7
-
8
- export type CompiledKeyMeta = {
9
- isSigner: boolean;
10
- isWritable: boolean;
11
- isInvoked: boolean;
12
- };
13
-
14
- type KeyMetaMap = Map<string, CompiledKeyMeta>;
15
-
16
- export class CompiledKeys {
17
- payer: PublicKey;
18
- keyMetaMap: KeyMetaMap;
19
-
20
- constructor(payer: PublicKey, keyMetaMap: KeyMetaMap) {
21
- this.payer = payer;
22
- this.keyMetaMap = keyMetaMap;
23
- }
24
-
25
- static compile(
26
- instructions: Array<TransactionInstruction>,
27
- payer: PublicKey,
28
- ): CompiledKeys {
29
- const keyMetaMap: KeyMetaMap = new Map();
30
- const getOrInsertDefault = (pubkey: PublicKey): CompiledKeyMeta => {
31
- const address = pubkey.toBase58();
32
- let keyMeta = keyMetaMap.get(address);
33
- if (keyMeta === undefined) {
34
- keyMeta = {
35
- isSigner: false,
36
- isWritable: false,
37
- isInvoked: false,
38
- };
39
- keyMetaMap.set(address, keyMeta);
40
- }
41
- return keyMeta;
42
- };
43
-
44
- const payerKeyMeta = getOrInsertDefault(payer);
45
- payerKeyMeta.isSigner = true;
46
- payerKeyMeta.isWritable = true;
47
-
48
- for (const ix of instructions) {
49
- getOrInsertDefault(ix.programId).isInvoked = true;
50
- for (const accountMeta of ix.keys) {
51
- const keyMeta = getOrInsertDefault(accountMeta.pubkey);
52
- keyMeta.isSigner ||= accountMeta.isSigner;
53
- keyMeta.isWritable ||= accountMeta.isWritable;
54
- }
55
- }
56
-
57
- return new CompiledKeys(payer, keyMetaMap);
58
- }
59
-
60
- getMessageComponents(): [MessageHeader, Array<PublicKey>] {
61
- const mapEntries = [...this.keyMetaMap.entries()];
62
- assert(mapEntries.length <= 256, 'Max static account keys length exceeded');
63
-
64
- const writableSigners = mapEntries.filter(
65
- ([, meta]) => meta.isSigner && meta.isWritable,
66
- );
67
- const readonlySigners = mapEntries.filter(
68
- ([, meta]) => meta.isSigner && !meta.isWritable,
69
- );
70
- const writableNonSigners = mapEntries.filter(
71
- ([, meta]) => !meta.isSigner && meta.isWritable,
72
- );
73
- const readonlyNonSigners = mapEntries.filter(
74
- ([, meta]) => !meta.isSigner && !meta.isWritable,
75
- );
76
-
77
- const header: MessageHeader = {
78
- numRequiredSignatures: writableSigners.length + readonlySigners.length,
79
- numReadonlySignedAccounts: readonlySigners.length,
80
- numReadonlyUnsignedAccounts: readonlyNonSigners.length,
81
- };
82
-
83
- // sanity checks
84
- {
85
- assert(
86
- writableSigners.length > 0,
87
- 'Expected at least one writable signer key',
88
- );
89
- const [payerAddress] = writableSigners[0];
90
- assert(
91
- payerAddress === this.payer.toBase58(),
92
- 'Expected first writable signer key to be the fee payer',
93
- );
94
- }
95
-
96
- const staticAccountKeys = [
97
- ...writableSigners.map(([address]) => new PublicKey(address)),
98
- ...readonlySigners.map(([address]) => new PublicKey(address)),
99
- ...writableNonSigners.map(([address]) => new PublicKey(address)),
100
- ...readonlyNonSigners.map(([address]) => new PublicKey(address)),
101
- ];
102
-
103
- return [header, staticAccountKeys];
104
- }
105
-
106
- extractTableLookup(
107
- lookupTable: AddressLookupTableAccount,
108
- ): [MessageAddressTableLookup, AccountKeysFromLookups] | undefined {
109
- const [writableIndexes, drainedWritableKeys] =
110
- this.drainKeysFoundInLookupTable(
111
- lookupTable.state.addresses,
112
- keyMeta =>
113
- !keyMeta.isSigner && !keyMeta.isInvoked && keyMeta.isWritable,
114
- );
115
- const [readonlyIndexes, drainedReadonlyKeys] =
116
- this.drainKeysFoundInLookupTable(
117
- lookupTable.state.addresses,
118
- keyMeta =>
119
- !keyMeta.isSigner && !keyMeta.isInvoked && !keyMeta.isWritable,
120
- );
121
-
122
- // Don't extract lookup if no keys were found
123
- if (writableIndexes.length === 0 && readonlyIndexes.length === 0) {
124
- return;
125
- }
126
-
127
- return [
128
- {
129
- accountKey: lookupTable.key,
130
- writableIndexes,
131
- readonlyIndexes,
132
- },
133
- {
134
- writable: drainedWritableKeys,
135
- readonly: drainedReadonlyKeys,
136
- },
137
- ];
138
- }
139
-
140
- /** @internal */
141
- private drainKeysFoundInLookupTable(
142
- lookupTableEntries: Array<PublicKey>,
143
- keyMetaFilter: (keyMeta: CompiledKeyMeta) => boolean,
144
- ): [Array<number>, Array<PublicKey>] {
145
- const lookupTableIndexes = new Array();
146
- const drainedKeys = new Array();
147
-
148
- for (const [address, keyMeta] of this.keyMetaMap.entries()) {
149
- if (keyMetaFilter(keyMeta)) {
150
- const key = new PublicKey(address);
151
- const lookupTableIndex = lookupTableEntries.findIndex(entry =>
152
- entry.equals(key),
153
- );
154
- if (lookupTableIndex >= 0) {
155
- assert(lookupTableIndex < 256, 'Max lookup table index exceeded');
156
- lookupTableIndexes.push(lookupTableIndex);
157
- drainedKeys.push(key);
158
- this.keyMetaMap.delete(address);
159
- }
160
- }
161
- }
162
-
163
- return [lookupTableIndexes, drainedKeys];
164
- }
165
- }
@@ -1,47 +0,0 @@
1
- import {PublicKey} from '../publickey';
2
-
3
- export * from './account-keys';
4
- // note: compiled-keys is internal and doesn't need to be exported
5
- export * from './legacy';
6
- export * from './versioned';
7
- export * from './v0';
8
-
9
- /**
10
- * The message header, identifying signed and read-only account
11
- */
12
- export type MessageHeader = {
13
- /**
14
- * The number of signatures required for this message to be considered valid. The
15
- * signatures must match the first `numRequiredSignatures` of `accountKeys`.
16
- */
17
- numRequiredSignatures: number;
18
- /** The last `numReadonlySignedAccounts` of the signed keys are read-only accounts */
19
- numReadonlySignedAccounts: number;
20
- /** The last `numReadonlySignedAccounts` of the unsigned keys are read-only accounts */
21
- numReadonlyUnsignedAccounts: number;
22
- };
23
-
24
- /**
25
- * An address table lookup used to load additional accounts
26
- */
27
- export type MessageAddressTableLookup = {
28
- accountKey: PublicKey;
29
- writableIndexes: Array<number>;
30
- readonlyIndexes: Array<number>;
31
- };
32
-
33
- /**
34
- * An instruction to execute by a program
35
- *
36
- * @property {number} programIdIndex
37
- * @property {number[]} accountKeyIndexes
38
- * @property {Uint8Array} data
39
- */
40
- export type MessageCompiledInstruction = {
41
- /** Index into the transaction keys array indicating the program account that executes this instruction */
42
- programIdIndex: number;
43
- /** Ordered indices into the transaction keys array indicating which accounts to pass to the program */
44
- accountKeyIndexes: number[];
45
- /** The program input data */
46
- data: Uint8Array;
47
- };