@solana/web3.js 1.87.3 → 2.0.0-experimental.0099b2a

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 (113) hide show
  1. package/README.md +56 -112
  2. package/dist/index.browser.cjs +927 -0
  3. package/dist/index.browser.cjs.map +1 -0
  4. package/dist/index.browser.js +876 -0
  5. package/dist/index.browser.js.map +1 -0
  6. package/dist/index.development.js +4180 -0
  7. package/dist/index.development.js.map +1 -0
  8. package/dist/index.native.js +865 -0
  9. package/dist/index.native.js.map +1 -0
  10. package/dist/index.node.cjs +916 -0
  11. package/dist/index.node.cjs.map +1 -0
  12. package/dist/index.node.js +865 -0
  13. package/dist/index.node.js.map +1 -0
  14. package/dist/index.production.min.js +87 -0
  15. package/dist/types/airdrop-confirmer.d.ts +20 -0
  16. package/dist/types/airdrop.d.ts +23 -0
  17. package/dist/types/cached-abortable-iterable.d.ts +11 -0
  18. package/dist/types/index.d.ts +15 -0
  19. package/dist/types/rpc-default-config.d.ts +3 -0
  20. package/dist/types/rpc-integer-overflow-error.d.ts +8 -0
  21. package/dist/types/rpc-request-coalescer.d.ts +5 -0
  22. package/dist/types/rpc-request-deduplication.d.ts +2 -0
  23. package/dist/types/rpc-subscription-coalescer.d.ts +10 -0
  24. package/dist/types/rpc-transport.d.ts +4 -0
  25. package/dist/types/rpc-websocket-autopinger.d.ts +8 -0
  26. package/dist/types/rpc-websocket-connection-sharding.d.ts +13 -0
  27. package/dist/types/rpc-websocket-transport.d.ts +13 -0
  28. package/dist/types/rpc.d.ts +7 -0
  29. package/dist/types/send-transaction.d.ts +37 -0
  30. package/dist/types/transaction-confirmation-strategy-blockheight.d.ts +10 -0
  31. package/dist/types/transaction-confirmation-strategy-nonce.d.ts +15 -0
  32. package/dist/types/transaction-confirmation-strategy-racer.d.ts +14 -0
  33. package/dist/types/transaction-confirmation-strategy-recent-signature.d.ts +13 -0
  34. package/dist/types/transaction-confirmation-strategy-timeout.d.ts +8 -0
  35. package/dist/types/transaction-confirmation.d.ts +37 -0
  36. package/package.json +85 -109
  37. package/lib/index.browser.cjs.js +0 -10296
  38. package/lib/index.browser.cjs.js.map +0 -1
  39. package/lib/index.browser.esm.js +0 -10193
  40. package/lib/index.browser.esm.js.map +0 -1
  41. package/lib/index.cjs.js +0 -12589
  42. package/lib/index.cjs.js.map +0 -1
  43. package/lib/index.d.ts +0 -3947
  44. package/lib/index.esm.js +0 -12480
  45. package/lib/index.esm.js.map +0 -1
  46. package/lib/index.iife.js +0 -26233
  47. package/lib/index.iife.js.map +0 -1
  48. package/lib/index.iife.min.js +0 -20
  49. package/lib/index.iife.min.js.map +0 -1
  50. package/lib/index.native.js +0 -10296
  51. package/lib/index.native.js.map +0 -1
  52. package/src/__forks__/browser/fetch-impl.ts +0 -4
  53. package/src/__forks__/browser/rpc-websocket-factory.ts +0 -1
  54. package/src/__forks__/react-native/fetch-impl.ts +0 -4
  55. package/src/__forks__/react-native/rpc-websocket-factory.ts +0 -1
  56. package/src/account-data.ts +0 -39
  57. package/src/account.ts +0 -55
  58. package/src/blockhash.ts +0 -4
  59. package/src/bpf-loader-deprecated.ts +0 -5
  60. package/src/bpf-loader.ts +0 -45
  61. package/src/connection.ts +0 -6796
  62. package/src/epoch-schedule.ts +0 -102
  63. package/src/errors.ts +0 -50
  64. package/src/fee-calculator.ts +0 -18
  65. package/src/fetch-impl.ts +0 -16
  66. package/src/index.ts +0 -24
  67. package/src/instruction.ts +0 -58
  68. package/src/keypair.ts +0 -102
  69. package/src/layout.ts +0 -188
  70. package/src/loader.ts +0 -267
  71. package/src/message/account-keys.ts +0 -79
  72. package/src/message/compiled-keys.ts +0 -165
  73. package/src/message/index.ts +0 -47
  74. package/src/message/legacy.ts +0 -326
  75. package/src/message/v0.ts +0 -496
  76. package/src/message/versioned.ts +0 -36
  77. package/src/nonce-account.ts +0 -82
  78. package/src/programs/address-lookup-table/index.ts +0 -435
  79. package/src/programs/address-lookup-table/state.ts +0 -84
  80. package/src/programs/compute-budget.ts +0 -281
  81. package/src/programs/ed25519.ts +0 -157
  82. package/src/programs/index.ts +0 -7
  83. package/src/programs/secp256k1.ts +0 -228
  84. package/src/programs/stake.ts +0 -935
  85. package/src/programs/system.ts +0 -1048
  86. package/src/programs/vote.ts +0 -543
  87. package/src/publickey.ts +0 -259
  88. package/src/rpc-websocket-factory.ts +0 -4
  89. package/src/rpc-websocket.ts +0 -79
  90. package/src/sysvar.ts +0 -37
  91. package/src/timing.ts +0 -23
  92. package/src/transaction/constants.ts +0 -12
  93. package/src/transaction/expiry-custom-errors.ts +0 -48
  94. package/src/transaction/index.ts +0 -5
  95. package/src/transaction/legacy.ts +0 -948
  96. package/src/transaction/message.ts +0 -140
  97. package/src/transaction/versioned.ts +0 -126
  98. package/src/utils/assert.ts +0 -8
  99. package/src/utils/bigint.ts +0 -43
  100. package/src/utils/borsh-schema.ts +0 -38
  101. package/src/utils/cluster.ts +0 -35
  102. package/src/utils/ed25519.ts +0 -43
  103. package/src/utils/index.ts +0 -5
  104. package/src/utils/makeWebsocketUrl.ts +0 -26
  105. package/src/utils/promise-timeout.ts +0 -14
  106. package/src/utils/secp256k1.ts +0 -11
  107. package/src/utils/send-and-confirm-raw-transaction.ts +0 -102
  108. package/src/utils/send-and-confirm-transaction.ts +0 -98
  109. package/src/utils/shortvec-encoding.ts +0 -28
  110. package/src/utils/sleep.ts +0 -4
  111. package/src/utils/to-buffer.ts +0 -11
  112. package/src/validator-info.ts +0 -104
  113. package/src/vote-account.ts +0 -236
@@ -1,102 +0,0 @@
1
- const MINIMUM_SLOT_PER_EPOCH = 32;
2
-
3
- // Returns the number of trailing zeros in the binary representation of self.
4
- function trailingZeros(n: number) {
5
- let trailingZeros = 0;
6
- while (n > 1) {
7
- n /= 2;
8
- trailingZeros++;
9
- }
10
- return trailingZeros;
11
- }
12
-
13
- // Returns the smallest power of two greater than or equal to n
14
- function nextPowerOfTwo(n: number) {
15
- if (n === 0) return 1;
16
- n--;
17
- n |= n >> 1;
18
- n |= n >> 2;
19
- n |= n >> 4;
20
- n |= n >> 8;
21
- n |= n >> 16;
22
- n |= n >> 32;
23
- return n + 1;
24
- }
25
-
26
- /**
27
- * Epoch schedule
28
- * (see https://docs.solana.com/terminology#epoch)
29
- * Can be retrieved with the {@link Connection.getEpochSchedule} method
30
- */
31
- export class EpochSchedule {
32
- /** The maximum number of slots in each epoch */
33
- public slotsPerEpoch: number;
34
- /** The number of slots before beginning of an epoch to calculate a leader schedule for that epoch */
35
- public leaderScheduleSlotOffset: number;
36
- /** Indicates whether epochs start short and grow */
37
- public warmup: boolean;
38
- /** The first epoch with `slotsPerEpoch` slots */
39
- public firstNormalEpoch: number;
40
- /** The first slot of `firstNormalEpoch` */
41
- public firstNormalSlot: number;
42
-
43
- constructor(
44
- slotsPerEpoch: number,
45
- leaderScheduleSlotOffset: number,
46
- warmup: boolean,
47
- firstNormalEpoch: number,
48
- firstNormalSlot: number,
49
- ) {
50
- this.slotsPerEpoch = slotsPerEpoch;
51
- this.leaderScheduleSlotOffset = leaderScheduleSlotOffset;
52
- this.warmup = warmup;
53
- this.firstNormalEpoch = firstNormalEpoch;
54
- this.firstNormalSlot = firstNormalSlot;
55
- }
56
-
57
- getEpoch(slot: number): number {
58
- return this.getEpochAndSlotIndex(slot)[0];
59
- }
60
-
61
- getEpochAndSlotIndex(slot: number): [number, number] {
62
- if (slot < this.firstNormalSlot) {
63
- const epoch =
64
- trailingZeros(nextPowerOfTwo(slot + MINIMUM_SLOT_PER_EPOCH + 1)) -
65
- trailingZeros(MINIMUM_SLOT_PER_EPOCH) -
66
- 1;
67
-
68
- const epochLen = this.getSlotsInEpoch(epoch);
69
- const slotIndex = slot - (epochLen - MINIMUM_SLOT_PER_EPOCH);
70
- return [epoch, slotIndex];
71
- } else {
72
- const normalSlotIndex = slot - this.firstNormalSlot;
73
- const normalEpochIndex = Math.floor(normalSlotIndex / this.slotsPerEpoch);
74
- const epoch = this.firstNormalEpoch + normalEpochIndex;
75
- const slotIndex = normalSlotIndex % this.slotsPerEpoch;
76
- return [epoch, slotIndex];
77
- }
78
- }
79
-
80
- getFirstSlotInEpoch(epoch: number): number {
81
- if (epoch <= this.firstNormalEpoch) {
82
- return (Math.pow(2, epoch) - 1) * MINIMUM_SLOT_PER_EPOCH;
83
- } else {
84
- return (
85
- (epoch - this.firstNormalEpoch) * this.slotsPerEpoch +
86
- this.firstNormalSlot
87
- );
88
- }
89
- }
90
-
91
- getLastSlotInEpoch(epoch: number): number {
92
- return this.getFirstSlotInEpoch(epoch) + this.getSlotsInEpoch(epoch) - 1;
93
- }
94
-
95
- getSlotsInEpoch(epoch: number) {
96
- if (epoch < this.firstNormalEpoch) {
97
- return Math.pow(2, epoch + trailingZeros(MINIMUM_SLOT_PER_EPOCH));
98
- } else {
99
- return this.slotsPerEpoch;
100
- }
101
- }
102
- }
package/src/errors.ts DELETED
@@ -1,50 +0,0 @@
1
- export class SendTransactionError extends Error {
2
- logs: string[] | undefined;
3
-
4
- constructor(message: string, logs?: string[]) {
5
- super(message);
6
-
7
- this.logs = logs;
8
- }
9
- }
10
-
11
- // Keep in sync with client/src/rpc_custom_errors.rs
12
- // Typescript `enums` thwart tree-shaking. See https://bargsten.org/jsts/enums/
13
- export const SolanaJSONRPCErrorCode = {
14
- JSON_RPC_SERVER_ERROR_BLOCK_CLEANED_UP: -32001,
15
- JSON_RPC_SERVER_ERROR_SEND_TRANSACTION_PREFLIGHT_FAILURE: -32002,
16
- JSON_RPC_SERVER_ERROR_TRANSACTION_SIGNATURE_VERIFICATION_FAILURE: -32003,
17
- JSON_RPC_SERVER_ERROR_BLOCK_NOT_AVAILABLE: -32004,
18
- JSON_RPC_SERVER_ERROR_NODE_UNHEALTHY: -32005,
19
- JSON_RPC_SERVER_ERROR_TRANSACTION_PRECOMPILE_VERIFICATION_FAILURE: -32006,
20
- JSON_RPC_SERVER_ERROR_SLOT_SKIPPED: -32007,
21
- JSON_RPC_SERVER_ERROR_NO_SNAPSHOT: -32008,
22
- JSON_RPC_SERVER_ERROR_LONG_TERM_STORAGE_SLOT_SKIPPED: -32009,
23
- JSON_RPC_SERVER_ERROR_KEY_EXCLUDED_FROM_SECONDARY_INDEX: -32010,
24
- JSON_RPC_SERVER_ERROR_TRANSACTION_HISTORY_NOT_AVAILABLE: -32011,
25
- JSON_RPC_SCAN_ERROR: -32012,
26
- JSON_RPC_SERVER_ERROR_TRANSACTION_SIGNATURE_LEN_MISMATCH: -32013,
27
- JSON_RPC_SERVER_ERROR_BLOCK_STATUS_NOT_AVAILABLE_YET: -32014,
28
- JSON_RPC_SERVER_ERROR_UNSUPPORTED_TRANSACTION_VERSION: -32015,
29
- JSON_RPC_SERVER_ERROR_MIN_CONTEXT_SLOT_NOT_REACHED: -32016,
30
- } as const;
31
- export type SolanaJSONRPCErrorCodeEnum =
32
- (typeof SolanaJSONRPCErrorCode)[keyof typeof SolanaJSONRPCErrorCode];
33
-
34
- export class SolanaJSONRPCError extends Error {
35
- code: SolanaJSONRPCErrorCodeEnum | unknown;
36
- data?: any;
37
- constructor(
38
- {
39
- code,
40
- message,
41
- data,
42
- }: Readonly<{code: unknown; message: string; data?: any}>,
43
- customMessage?: string,
44
- ) {
45
- super(customMessage != null ? `${customMessage}: ${message}` : message);
46
- this.code = code;
47
- this.data = data;
48
- this.name = 'SolanaJSONRPCError';
49
- }
50
- }
@@ -1,18 +0,0 @@
1
- import * as BufferLayout from '@solana/buffer-layout';
2
-
3
- /**
4
- * https://github.com/solana-labs/solana/blob/90bedd7e067b5b8f3ddbb45da00a4e9cabb22c62/sdk/src/fee_calculator.rs#L7-L11
5
- *
6
- * @internal
7
- */
8
- export const FeeCalculatorLayout = BufferLayout.nu64('lamportsPerSignature');
9
-
10
- /**
11
- * Calculator for transaction fees.
12
- *
13
- * @deprecated Deprecated since Solana v1.8.0.
14
- */
15
- export interface FeeCalculator {
16
- /** Cost in lamports to validate a signature. */
17
- lamportsPerSignature: number;
18
- }
package/src/fetch-impl.ts DELETED
@@ -1,16 +0,0 @@
1
- import * as nodeFetch from 'node-fetch';
2
-
3
- export default (typeof globalThis.fetch === 'function'
4
- ? // The Fetch API is supported experimentally in Node 17.5+ and natively in Node 18+.
5
- globalThis.fetch
6
- : // Otherwise use the polyfill.
7
- async function (
8
- input: nodeFetch.RequestInfo,
9
- init?: nodeFetch.RequestInit,
10
- ): Promise<nodeFetch.Response> {
11
- const processedInput =
12
- typeof input === 'string' && input.slice(0, 2) === '//'
13
- ? 'https:' + input
14
- : input;
15
- return await nodeFetch.default(processedInput, init);
16
- }) as typeof globalThis.fetch;
package/src/index.ts DELETED
@@ -1,24 +0,0 @@
1
- export * from './account';
2
- export * from './blockhash';
3
- export * from './bpf-loader-deprecated';
4
- export * from './bpf-loader';
5
- export * from './connection';
6
- export * from './epoch-schedule';
7
- export * from './errors';
8
- export * from './fee-calculator';
9
- export * from './keypair';
10
- export * from './loader';
11
- export * from './message';
12
- export * from './nonce-account';
13
- export * from './programs';
14
- export * from './publickey';
15
- export * from './transaction';
16
- export * from './validator-info';
17
- export * from './vote-account';
18
- export * from './sysvar';
19
- export * from './utils';
20
-
21
- /**
22
- * There are 1-billion lamports in one SOL
23
- */
24
- export const LAMPORTS_PER_SOL = 1000000000;
@@ -1,58 +0,0 @@
1
- import {Buffer} from 'buffer';
2
- import * as BufferLayout from '@solana/buffer-layout';
3
-
4
- import * as Layout from './layout';
5
-
6
- export interface IInstructionInputData {
7
- readonly instruction: number;
8
- }
9
-
10
- /**
11
- * @internal
12
- */
13
- export type InstructionType<TInputData extends IInstructionInputData> = {
14
- /** The Instruction index (from solana upstream program) */
15
- index: number;
16
- /** The BufferLayout to use to build data */
17
- layout: BufferLayout.Layout<TInputData>;
18
- };
19
-
20
- /**
21
- * Populate a buffer of instruction data using an InstructionType
22
- * @internal
23
- */
24
- export function encodeData<TInputData extends IInstructionInputData>(
25
- type: InstructionType<TInputData>,
26
- fields?: any,
27
- ): Buffer {
28
- const allocLength =
29
- type.layout.span >= 0 ? type.layout.span : Layout.getAlloc(type, fields);
30
- const data = Buffer.alloc(allocLength);
31
- const layoutFields = Object.assign({instruction: type.index}, fields);
32
- type.layout.encode(layoutFields, data);
33
- return data;
34
- }
35
-
36
- /**
37
- * Decode instruction data buffer using an InstructionType
38
- * @internal
39
- */
40
- export function decodeData<TInputData extends IInstructionInputData>(
41
- type: InstructionType<TInputData>,
42
- buffer: Buffer,
43
- ): TInputData {
44
- let data: TInputData;
45
- try {
46
- data = type.layout.decode(buffer);
47
- } catch (err) {
48
- throw new Error('invalid instruction; ' + err);
49
- }
50
-
51
- if (data.instruction !== type.index) {
52
- throw new Error(
53
- `invalid instruction; instruction index mismatch ${data.instruction} != ${type.index}`,
54
- );
55
- }
56
-
57
- return data;
58
- }
package/src/keypair.ts DELETED
@@ -1,102 +0,0 @@
1
- import {generateKeypair, getPublicKey, Ed25519Keypair} from './utils/ed25519';
2
- import {PublicKey} from './publickey';
3
-
4
- /**
5
- * Keypair signer interface
6
- */
7
- export interface Signer {
8
- publicKey: PublicKey;
9
- secretKey: Uint8Array;
10
- }
11
-
12
- /**
13
- * An account keypair used for signing transactions.
14
- */
15
- export class Keypair {
16
- private _keypair: Ed25519Keypair;
17
-
18
- /**
19
- * Create a new keypair instance.
20
- * Generate random keypair if no {@link Ed25519Keypair} is provided.
21
- *
22
- * @param {Ed25519Keypair} keypair ed25519 keypair
23
- */
24
- constructor(keypair?: Ed25519Keypair) {
25
- this._keypair = keypair ?? generateKeypair();
26
- }
27
-
28
- /**
29
- * Generate a new random keypair
30
- *
31
- * @returns {Keypair} Keypair
32
- */
33
- static generate(): Keypair {
34
- return new Keypair(generateKeypair());
35
- }
36
-
37
- /**
38
- * Create a keypair from a raw secret key byte array.
39
- *
40
- * This method should only be used to recreate a keypair from a previously
41
- * generated secret key. Generating keypairs from a random seed should be done
42
- * with the {@link Keypair.fromSeed} method.
43
- *
44
- * @throws error if the provided secret key is invalid and validation is not skipped.
45
- *
46
- * @param secretKey secret key byte array
47
- * @param options skip secret key validation
48
- *
49
- * @returns {Keypair} Keypair
50
- */
51
- static fromSecretKey(
52
- secretKey: Uint8Array,
53
- options?: {skipValidation?: boolean},
54
- ): Keypair {
55
- if (secretKey.byteLength !== 64) {
56
- throw new Error('bad secret key size');
57
- }
58
- const publicKey = secretKey.slice(32, 64);
59
- if (!options || !options.skipValidation) {
60
- const privateScalar = secretKey.slice(0, 32);
61
- const computedPublicKey = getPublicKey(privateScalar);
62
- for (let ii = 0; ii < 32; ii++) {
63
- if (publicKey[ii] !== computedPublicKey[ii]) {
64
- throw new Error('provided secretKey is invalid');
65
- }
66
- }
67
- }
68
- return new Keypair({publicKey, secretKey});
69
- }
70
-
71
- /**
72
- * Generate a keypair from a 32 byte seed.
73
- *
74
- * @param seed seed byte array
75
- *
76
- * @returns {Keypair} Keypair
77
- */
78
- static fromSeed(seed: Uint8Array): Keypair {
79
- const publicKey = getPublicKey(seed);
80
- const secretKey = new Uint8Array(64);
81
- secretKey.set(seed);
82
- secretKey.set(publicKey, 32);
83
- return new Keypair({publicKey, secretKey});
84
- }
85
-
86
- /**
87
- * The public key for this keypair
88
- *
89
- * @returns {PublicKey} PublicKey
90
- */
91
- get publicKey(): PublicKey {
92
- return new PublicKey(this._keypair.publicKey);
93
- }
94
-
95
- /**
96
- * The raw secret key for this keypair
97
- * @returns {Uint8Array} Secret key in an array of Uint8 bytes
98
- */
99
- get secretKey(): Uint8Array {
100
- return new Uint8Array(this._keypair.secretKey);
101
- }
102
- }
package/src/layout.ts DELETED
@@ -1,188 +0,0 @@
1
- import {Buffer} from 'buffer';
2
- import * as BufferLayout from '@solana/buffer-layout';
3
-
4
- import {VoteAuthorizeWithSeedArgs} from './programs/vote';
5
-
6
- /**
7
- * Layout for a public key
8
- */
9
- export const publicKey = (property: string = 'publicKey') => {
10
- return BufferLayout.blob(32, property);
11
- };
12
-
13
- /**
14
- * Layout for a signature
15
- */
16
- export const signature = (property: string = 'signature') => {
17
- return BufferLayout.blob(64, property);
18
- };
19
-
20
- /**
21
- * Layout for a 64bit unsigned value
22
- */
23
- export const uint64 = (property: string = 'uint64') => {
24
- return BufferLayout.blob(8, property);
25
- };
26
-
27
- interface IRustStringShim
28
- extends Omit<
29
- BufferLayout.Structure<
30
- Readonly<{
31
- length: number;
32
- lengthPadding: number;
33
- chars: Uint8Array;
34
- }>
35
- >,
36
- 'decode' | 'encode' | 'replicate'
37
- > {
38
- alloc: (str: string) => number;
39
- decode: (b: Uint8Array, offset?: number) => string;
40
- encode: (str: string, b: Uint8Array, offset?: number) => number;
41
- replicate: (property: string) => this;
42
- }
43
-
44
- /**
45
- * Layout for a Rust String type
46
- */
47
- export const rustString = (
48
- property: string = 'string',
49
- ): BufferLayout.Layout<string> => {
50
- const rsl = BufferLayout.struct<
51
- Readonly<{
52
- length?: number;
53
- lengthPadding?: number;
54
- chars: Uint8Array;
55
- }>
56
- >(
57
- [
58
- BufferLayout.u32('length'),
59
- BufferLayout.u32('lengthPadding'),
60
- BufferLayout.blob(BufferLayout.offset(BufferLayout.u32(), -8), 'chars'),
61
- ],
62
- property,
63
- );
64
- const _decode = rsl.decode.bind(rsl);
65
- const _encode = rsl.encode.bind(rsl);
66
-
67
- const rslShim = rsl as unknown as IRustStringShim;
68
-
69
- rslShim.decode = (b: Uint8Array, offset?: number) => {
70
- const data = _decode(b, offset);
71
- return data['chars'].toString();
72
- };
73
-
74
- rslShim.encode = (str: string, b: Uint8Array, offset?: number) => {
75
- const data = {
76
- chars: Buffer.from(str, 'utf8'),
77
- };
78
- return _encode(data, b, offset);
79
- };
80
-
81
- rslShim.alloc = (str: string) => {
82
- return (
83
- BufferLayout.u32().span +
84
- BufferLayout.u32().span +
85
- Buffer.from(str, 'utf8').length
86
- );
87
- };
88
-
89
- return rslShim;
90
- };
91
-
92
- /**
93
- * Layout for an Authorized object
94
- */
95
- export const authorized = (property: string = 'authorized') => {
96
- return BufferLayout.struct<
97
- Readonly<{
98
- staker: Uint8Array;
99
- withdrawer: Uint8Array;
100
- }>
101
- >([publicKey('staker'), publicKey('withdrawer')], property);
102
- };
103
-
104
- /**
105
- * Layout for a Lockup object
106
- */
107
- export const lockup = (property: string = 'lockup') => {
108
- return BufferLayout.struct<
109
- Readonly<{
110
- custodian: Uint8Array;
111
- epoch: number;
112
- unixTimestamp: number;
113
- }>
114
- >(
115
- [
116
- BufferLayout.ns64('unixTimestamp'),
117
- BufferLayout.ns64('epoch'),
118
- publicKey('custodian'),
119
- ],
120
- property,
121
- );
122
- };
123
-
124
- /**
125
- * Layout for a VoteInit object
126
- */
127
- export const voteInit = (property: string = 'voteInit') => {
128
- return BufferLayout.struct<
129
- Readonly<{
130
- authorizedVoter: Uint8Array;
131
- authorizedWithdrawer: Uint8Array;
132
- commission: number;
133
- nodePubkey: Uint8Array;
134
- }>
135
- >(
136
- [
137
- publicKey('nodePubkey'),
138
- publicKey('authorizedVoter'),
139
- publicKey('authorizedWithdrawer'),
140
- BufferLayout.u8('commission'),
141
- ],
142
- property,
143
- );
144
- };
145
-
146
- /**
147
- * Layout for a VoteAuthorizeWithSeedArgs object
148
- */
149
- export const voteAuthorizeWithSeedArgs = (
150
- property: string = 'voteAuthorizeWithSeedArgs',
151
- ) => {
152
- return BufferLayout.struct<VoteAuthorizeWithSeedArgs>(
153
- [
154
- BufferLayout.u32('voteAuthorizationType'),
155
- publicKey('currentAuthorityDerivedKeyOwnerPubkey'),
156
- rustString('currentAuthorityDerivedKeySeed'),
157
- publicKey('newAuthorized'),
158
- ],
159
- property,
160
- );
161
- };
162
-
163
- export function getAlloc(type: any, fields: any): number {
164
- const getItemAlloc = (item: any): number => {
165
- if (item.span >= 0) {
166
- return item.span;
167
- } else if (typeof item.alloc === 'function') {
168
- return item.alloc(fields[item.property]);
169
- } else if ('count' in item && 'elementLayout' in item) {
170
- const field = fields[item.property];
171
- if (Array.isArray(field)) {
172
- return field.length * getItemAlloc(item.elementLayout);
173
- }
174
- } else if ('fields' in item) {
175
- // This is a `Structure` whose size needs to be recursively measured.
176
- return getAlloc({layout: item}, fields[item.property]);
177
- }
178
- // Couldn't determine allocated size of layout
179
- return 0;
180
- };
181
-
182
- let alloc = 0;
183
- type.layout.fields.forEach((item: any) => {
184
- alloc += getItemAlloc(item);
185
- });
186
-
187
- return alloc;
188
- }