@solana/web3.js 1.56.0 → 1.57.0

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/lib/index.esm.js CHANGED
@@ -115,12 +115,14 @@ const PUBLIC_KEY_LENGTH = 32;
115
115
 
116
116
  function isPublicKeyData(value) {
117
117
  return value._bn !== undefined;
118
- }
118
+ } // local counter used by PublicKey.unique()
119
+
120
+
121
+ let uniquePublicKeyCounter = 1;
119
122
  /**
120
123
  * A public key
121
124
  */
122
125
 
123
-
124
126
  class PublicKey extends Struct {
125
127
  /** @internal */
126
128
 
@@ -153,6 +155,16 @@ class PublicKey extends Struct {
153
155
  }
154
156
  }
155
157
  }
158
+ /**
159
+ * Returns a unique PublicKey for tests and benchmarks using acounter
160
+ */
161
+
162
+
163
+ static unique() {
164
+ const key = new PublicKey(uniquePublicKeyCounter);
165
+ uniquePublicKeyCounter += 1;
166
+ return new PublicKey(key.toBuffer());
167
+ }
156
168
  /**
157
169
  * Default public key value. (All zeros)
158
170
  */
@@ -409,6 +421,71 @@ Object.defineProperty(TransactionExpiredTimeoutError.prototype, 'name', {
409
421
  value: 'TransactionExpiredTimeoutError'
410
422
  });
411
423
 
424
+ class MessageAccountKeys {
425
+ constructor(staticAccountKeys, accountKeysFromLookups) {
426
+ this.staticAccountKeys = void 0;
427
+ this.accountKeysFromLookups = void 0;
428
+ this.staticAccountKeys = staticAccountKeys;
429
+ this.accountKeysFromLookups = accountKeysFromLookups;
430
+ }
431
+
432
+ keySegments() {
433
+ const keySegments = [this.staticAccountKeys];
434
+
435
+ if (this.accountKeysFromLookups) {
436
+ keySegments.push(this.accountKeysFromLookups.writable);
437
+ keySegments.push(this.accountKeysFromLookups.readonly);
438
+ }
439
+
440
+ return keySegments;
441
+ }
442
+
443
+ get(index) {
444
+ for (const keySegment of this.keySegments()) {
445
+ if (index < keySegment.length) {
446
+ return keySegment[index];
447
+ } else {
448
+ index -= keySegment.length;
449
+ }
450
+ }
451
+
452
+ return;
453
+ }
454
+
455
+ get length() {
456
+ return this.keySegments().flat().length;
457
+ }
458
+
459
+ compileInstructions(instructions) {
460
+ // Bail early if any account indexes would overflow a u8
461
+ const U8_MAX = 255;
462
+
463
+ if (this.length > U8_MAX + 1) {
464
+ throw new Error('Account index overflow encountered during compilation');
465
+ }
466
+
467
+ const keyIndexMap = new Map();
468
+ this.keySegments().flat().forEach((key, index) => {
469
+ keyIndexMap.set(key.toBase58(), index);
470
+ });
471
+
472
+ const findKeyIndex = key => {
473
+ const keyIndex = keyIndexMap.get(key.toBase58());
474
+ if (keyIndex === undefined) throw new Error('Encountered an unknown instruction account key during compilation');
475
+ return keyIndex;
476
+ };
477
+
478
+ return instructions.map(instruction => {
479
+ return {
480
+ programIdIndex: findKeyIndex(instruction.programId),
481
+ accountKeyIndexes: instruction.keys.map(meta => findKeyIndex(meta.pubkey)),
482
+ data: instruction.data
483
+ };
484
+ });
485
+ }
486
+
487
+ }
488
+
412
489
  /**
413
490
  * Layout for a public key
414
491
  */
@@ -705,6 +782,115 @@ function assert (condition, message) {
705
782
  }
706
783
  }
707
784
 
785
+ class CompiledKeys {
786
+ constructor(payer, keyMetaMap) {
787
+ this.payer = void 0;
788
+ this.keyMetaMap = void 0;
789
+ this.payer = payer;
790
+ this.keyMetaMap = keyMetaMap;
791
+ }
792
+
793
+ static compile(instructions, payer) {
794
+ const keyMetaMap = new Map();
795
+
796
+ const getOrInsertDefault = pubkey => {
797
+ const address = pubkey.toBase58();
798
+ let keyMeta = keyMetaMap.get(address);
799
+
800
+ if (keyMeta === undefined) {
801
+ keyMeta = {
802
+ isSigner: false,
803
+ isWritable: false,
804
+ isInvoked: false
805
+ };
806
+ keyMetaMap.set(address, keyMeta);
807
+ }
808
+
809
+ return keyMeta;
810
+ };
811
+
812
+ const payerKeyMeta = getOrInsertDefault(payer);
813
+ payerKeyMeta.isSigner = true;
814
+ payerKeyMeta.isWritable = true;
815
+
816
+ for (const ix of instructions) {
817
+ getOrInsertDefault(ix.programId).isInvoked = true;
818
+
819
+ for (const accountMeta of ix.keys) {
820
+ const keyMeta = getOrInsertDefault(accountMeta.pubkey);
821
+ keyMeta.isSigner || (keyMeta.isSigner = accountMeta.isSigner);
822
+ keyMeta.isWritable || (keyMeta.isWritable = accountMeta.isWritable);
823
+ }
824
+ }
825
+
826
+ return new CompiledKeys(payer, keyMetaMap);
827
+ }
828
+
829
+ getMessageComponents() {
830
+ const mapEntries = [...this.keyMetaMap.entries()];
831
+ assert(mapEntries.length <= 256, 'Max static account keys length exceeded');
832
+ const writableSigners = mapEntries.filter(([, meta]) => meta.isSigner && meta.isWritable);
833
+ const readonlySigners = mapEntries.filter(([, meta]) => meta.isSigner && !meta.isWritable);
834
+ const writableNonSigners = mapEntries.filter(([, meta]) => !meta.isSigner && meta.isWritable);
835
+ const readonlyNonSigners = mapEntries.filter(([, meta]) => !meta.isSigner && !meta.isWritable);
836
+ const header = {
837
+ numRequiredSignatures: writableSigners.length + readonlySigners.length,
838
+ numReadonlySignedAccounts: readonlySigners.length,
839
+ numReadonlyUnsignedAccounts: readonlyNonSigners.length
840
+ }; // sanity checks
841
+
842
+ {
843
+ assert(writableSigners.length > 0, 'Expected at least one writable signer key');
844
+ const [payerAddress] = writableSigners[0];
845
+ assert(payerAddress === this.payer.toBase58(), 'Expected first writable signer key to be the fee payer');
846
+ }
847
+ const staticAccountKeys = [...writableSigners.map(([address]) => new PublicKey(address)), ...readonlySigners.map(([address]) => new PublicKey(address)), ...writableNonSigners.map(([address]) => new PublicKey(address)), ...readonlyNonSigners.map(([address]) => new PublicKey(address))];
848
+ return [header, staticAccountKeys];
849
+ }
850
+
851
+ extractTableLookup(lookupTable) {
852
+ const [writableIndexes, drainedWritableKeys] = this.drainKeysFoundInLookupTable(lookupTable.state.addresses, keyMeta => !keyMeta.isSigner && !keyMeta.isInvoked && keyMeta.isWritable);
853
+ const [readonlyIndexes, drainedReadonlyKeys] = this.drainKeysFoundInLookupTable(lookupTable.state.addresses, keyMeta => !keyMeta.isSigner && !keyMeta.isInvoked && !keyMeta.isWritable); // Don't extract lookup if no keys were found
854
+
855
+ if (writableIndexes.length === 0 && readonlyIndexes.length === 0) {
856
+ return;
857
+ }
858
+
859
+ return [{
860
+ accountKey: lookupTable.key,
861
+ writableIndexes,
862
+ readonlyIndexes
863
+ }, {
864
+ writable: drainedWritableKeys,
865
+ readonly: drainedReadonlyKeys
866
+ }];
867
+ }
868
+ /** @internal */
869
+
870
+
871
+ drainKeysFoundInLookupTable(lookupTableEntries, keyMetaFilter) {
872
+ const lookupTableIndexes = new Array();
873
+ const drainedKeys = new Array();
874
+
875
+ for (const [address, keyMeta] of this.keyMetaMap.entries()) {
876
+ if (keyMetaFilter(keyMeta)) {
877
+ const key = new PublicKey(address);
878
+ const lookupTableIndex = lookupTableEntries.findIndex(entry => entry.equals(key));
879
+
880
+ if (lookupTableIndex >= 0) {
881
+ assert(lookupTableIndex < 256, 'Max lookup table index exceeded');
882
+ lookupTableIndexes.push(lookupTableIndex);
883
+ drainedKeys.push(key);
884
+ this.keyMetaMap.delete(address);
885
+ }
886
+ }
887
+ }
888
+
889
+ return [lookupTableIndexes, drainedKeys];
890
+ }
891
+
892
+ }
893
+
708
894
  /**
709
895
  * Message constructor arguments
710
896
  */
@@ -727,6 +913,41 @@ class MessageV0 {
727
913
  return 0;
728
914
  }
729
915
 
916
+ static compile(args) {
917
+ const compiledKeys = CompiledKeys.compile(args.instructions, args.payerKey);
918
+ const addressTableLookups = new Array();
919
+ const accountKeysFromLookups = {
920
+ writable: new Array(),
921
+ readonly: new Array()
922
+ };
923
+ const lookupTableAccounts = args.addressLookupTableAccounts || [];
924
+
925
+ for (const lookupTable of lookupTableAccounts) {
926
+ const extractResult = compiledKeys.extractTableLookup(lookupTable);
927
+
928
+ if (extractResult !== undefined) {
929
+ const [addressTableLookup, {
930
+ writable,
931
+ readonly
932
+ }] = extractResult;
933
+ addressTableLookups.push(addressTableLookup);
934
+ accountKeysFromLookups.writable.push(...writable);
935
+ accountKeysFromLookups.readonly.push(...readonly);
936
+ }
937
+ }
938
+
939
+ const [header, staticAccountKeys] = compiledKeys.getMessageComponents();
940
+ const accountKeys = new MessageAccountKeys(staticAccountKeys, accountKeysFromLookups);
941
+ const compiledInstructions = accountKeys.compileInstructions(args.instructions);
942
+ return new MessageV0({
943
+ header,
944
+ staticAccountKeys,
945
+ recentBlockhash: args.recentBlockhash,
946
+ compiledInstructions,
947
+ addressTableLookups
948
+ });
949
+ }
950
+
730
951
  serialize() {
731
952
  const encodedStaticAccountKeysLength = Array();
732
953
  encodeLength(encodedStaticAccountKeysLength, this.staticAccountKeys.length);
@@ -3945,7 +4166,8 @@ const ConfirmedTransactionMetaResult = type({
3945
4166
  logMessages: optional(nullable(array(string()))),
3946
4167
  preTokenBalances: optional(nullable(array(TokenBalanceResult))),
3947
4168
  postTokenBalances: optional(nullable(array(TokenBalanceResult))),
3948
- loadedAddresses: optional(LoadedAddressesResult)
4169
+ loadedAddresses: optional(LoadedAddressesResult),
4170
+ computeUnitsConsumed: optional(number())
3949
4171
  });
3950
4172
  /**
3951
4173
  * @internal
@@ -3963,7 +4185,8 @@ const ParsedConfirmedTransactionMetaResult = type({
3963
4185
  logMessages: optional(nullable(array(string()))),
3964
4186
  preTokenBalances: optional(nullable(array(TokenBalanceResult))),
3965
4187
  postTokenBalances: optional(nullable(array(TokenBalanceResult))),
3966
- loadedAddresses: optional(LoadedAddressesResult)
4188
+ loadedAddresses: optional(LoadedAddressesResult),
4189
+ computeUnitsConsumed: optional(number())
3967
4190
  });
3968
4191
  const TransactionVersionStruct = union([literal(0), literal('legacy')]);
3969
4192
  /**
@@ -9113,5 +9336,5 @@ async function sendAndConfirmRawTransaction(connection, rawTransaction, confirma
9113
9336
 
9114
9337
  const LAMPORTS_PER_SOL = 1000000000;
9115
9338
 
9116
- export { Account, AddressLookupTableAccount, AddressLookupTableInstruction, AddressLookupTableProgram, Authorized, BLOCKHASH_CACHE_TIMEOUT_MS, BPF_LOADER_DEPRECATED_PROGRAM_ID, BPF_LOADER_PROGRAM_ID, BpfLoader, COMPUTE_BUDGET_INSTRUCTION_LAYOUTS, ComputeBudgetInstruction, ComputeBudgetProgram, Connection, Ed25519Program, Enum, EpochSchedule, FeeCalculatorLayout, Keypair, LAMPORTS_PER_SOL, LOOKUP_TABLE_INSTRUCTION_LAYOUTS, Loader, Lockup, MAX_SEED_LENGTH, Message, MessageV0, NONCE_ACCOUNT_LENGTH, NonceAccount, PACKET_DATA_SIZE, PUBLIC_KEY_LENGTH, PublicKey, SIGNATURE_LENGTH_IN_BYTES, SOLANA_SCHEMA, STAKE_CONFIG_ID, STAKE_INSTRUCTION_LAYOUTS, SYSTEM_INSTRUCTION_LAYOUTS, SYSVAR_CLOCK_PUBKEY, SYSVAR_EPOCH_SCHEDULE_PUBKEY, SYSVAR_INSTRUCTIONS_PUBKEY, SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY, SYSVAR_REWARDS_PUBKEY, SYSVAR_SLOT_HASHES_PUBKEY, SYSVAR_SLOT_HISTORY_PUBKEY, SYSVAR_STAKE_HISTORY_PUBKEY, Secp256k1Program, SendTransactionError, SolanaJSONRPCError, SolanaJSONRPCErrorCode, StakeAuthorizationLayout, StakeInstruction, StakeProgram, Struct, SystemInstruction, SystemProgram, Transaction, TransactionExpiredBlockheightExceededError, TransactionExpiredTimeoutError, TransactionInstruction, TransactionStatus, VALIDATOR_INFO_KEY, VERSION_PREFIX_MASK, VOTE_PROGRAM_ID, ValidatorInfo, VersionedMessage, VersionedTransaction, VoteAccount, VoteAuthorizationLayout, VoteInit, VoteInstruction, VoteProgram, clusterApiUrl, sendAndConfirmRawTransaction, sendAndConfirmTransaction };
9339
+ export { Account, AddressLookupTableAccount, AddressLookupTableInstruction, AddressLookupTableProgram, Authorized, BLOCKHASH_CACHE_TIMEOUT_MS, BPF_LOADER_DEPRECATED_PROGRAM_ID, BPF_LOADER_PROGRAM_ID, BpfLoader, COMPUTE_BUDGET_INSTRUCTION_LAYOUTS, ComputeBudgetInstruction, ComputeBudgetProgram, Connection, Ed25519Program, Enum, EpochSchedule, FeeCalculatorLayout, Keypair, LAMPORTS_PER_SOL, LOOKUP_TABLE_INSTRUCTION_LAYOUTS, Loader, Lockup, MAX_SEED_LENGTH, Message, MessageAccountKeys, MessageV0, NONCE_ACCOUNT_LENGTH, NonceAccount, PACKET_DATA_SIZE, PUBLIC_KEY_LENGTH, PublicKey, SIGNATURE_LENGTH_IN_BYTES, SOLANA_SCHEMA, STAKE_CONFIG_ID, STAKE_INSTRUCTION_LAYOUTS, SYSTEM_INSTRUCTION_LAYOUTS, SYSVAR_CLOCK_PUBKEY, SYSVAR_EPOCH_SCHEDULE_PUBKEY, SYSVAR_INSTRUCTIONS_PUBKEY, SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY, SYSVAR_REWARDS_PUBKEY, SYSVAR_SLOT_HASHES_PUBKEY, SYSVAR_SLOT_HISTORY_PUBKEY, SYSVAR_STAKE_HISTORY_PUBKEY, Secp256k1Program, SendTransactionError, SolanaJSONRPCError, SolanaJSONRPCErrorCode, StakeAuthorizationLayout, StakeInstruction, StakeProgram, Struct, SystemInstruction, SystemProgram, Transaction, TransactionExpiredBlockheightExceededError, TransactionExpiredTimeoutError, TransactionInstruction, TransactionStatus, VALIDATOR_INFO_KEY, VERSION_PREFIX_MASK, VOTE_PROGRAM_ID, ValidatorInfo, VersionedMessage, VersionedTransaction, VoteAccount, VoteAuthorizationLayout, VoteInit, VoteInstruction, VoteProgram, clusterApiUrl, sendAndConfirmRawTransaction, sendAndConfirmTransaction };
9117
9340
  //# sourceMappingURL=index.esm.js.map