@solana/accounts 6.3.1 → 6.3.2-canary-20260313112147
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/package.json +9 -8
- package/src/account.ts +98 -0
- package/src/decode-account.ts +146 -0
- package/src/fetch-account.ts +224 -0
- package/src/index.ts +40 -0
- package/src/maybe-account.ts +126 -0
- package/src/parse-account.ts +128 -0
- package/src/rpc-api/common.ts +9 -0
- package/src/rpc-api/getAccountInfo.ts +147 -0
- package/src/rpc-api/getMultipleAccounts.ts +156 -0
- package/src/rpc-api/index.ts +3 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solana/accounts",
|
|
3
|
-
"version": "6.3.
|
|
3
|
+
"version": "6.3.2-canary-20260313112147",
|
|
4
4
|
"description": "Helpers for representing, fetching and decoding Solana accounts",
|
|
5
5
|
"homepage": "https://www.solanakit.com/api#solanaaccounts",
|
|
6
6
|
"exports": {
|
|
@@ -33,7 +33,8 @@
|
|
|
33
33
|
"types": "./dist/types/index.d.ts",
|
|
34
34
|
"type": "commonjs",
|
|
35
35
|
"files": [
|
|
36
|
-
"./dist/"
|
|
36
|
+
"./dist/",
|
|
37
|
+
"./src/"
|
|
37
38
|
],
|
|
38
39
|
"sideEffects": false,
|
|
39
40
|
"keywords": [
|
|
@@ -55,12 +56,12 @@
|
|
|
55
56
|
"maintained node versions"
|
|
56
57
|
],
|
|
57
58
|
"dependencies": {
|
|
58
|
-
"@solana/addresses": "6.3.
|
|
59
|
-
"@solana/codecs-
|
|
60
|
-
"@solana/
|
|
61
|
-
"@solana/rpc-spec": "6.3.
|
|
62
|
-
"@solana/
|
|
63
|
-
"@solana/rpc-types": "6.3.
|
|
59
|
+
"@solana/addresses": "6.3.2-canary-20260313112147",
|
|
60
|
+
"@solana/codecs-strings": "6.3.2-canary-20260313112147",
|
|
61
|
+
"@solana/errors": "6.3.2-canary-20260313112147",
|
|
62
|
+
"@solana/rpc-spec": "6.3.2-canary-20260313112147",
|
|
63
|
+
"@solana/codecs-core": "6.3.2-canary-20260313112147",
|
|
64
|
+
"@solana/rpc-types": "6.3.2-canary-20260313112147"
|
|
64
65
|
},
|
|
65
66
|
"peerDependencies": {
|
|
66
67
|
"typescript": "^5.0.0"
|
package/src/account.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { Address } from '@solana/addresses';
|
|
2
|
+
import { ReadonlyUint8Array } from '@solana/codecs-core';
|
|
3
|
+
import type { Lamports } from '@solana/rpc-types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The number of bytes required to store the {@link BaseAccount} information without its data.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const myTotalAccountSize = myAccountDataSize + BASE_ACCOUNT_SIZE;
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export const BASE_ACCOUNT_SIZE = 128;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Defines the attributes common to all Solana accounts. Namely, it contains everything stored
|
|
17
|
+
* on-chain except the account data itself.
|
|
18
|
+
*
|
|
19
|
+
* @interface
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const BaseAccount: BaseAccount = {
|
|
24
|
+
* executable: false,
|
|
25
|
+
* lamports: lamports(1_000_000_000n),
|
|
26
|
+
* programAddress: address('1111..1111'),
|
|
27
|
+
* space: 42n,
|
|
28
|
+
* };
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export type BaseAccount = {
|
|
32
|
+
readonly executable: boolean;
|
|
33
|
+
readonly lamports: Lamports;
|
|
34
|
+
readonly programAddress: Address;
|
|
35
|
+
readonly space: bigint;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Contains all the information relevant to a Solana account. It includes the account's address and
|
|
40
|
+
* data, as well as the properties of {@link BaseAccount}.
|
|
41
|
+
*
|
|
42
|
+
* @interface
|
|
43
|
+
*
|
|
44
|
+
* @typeParam TAddress - Supply a string literal to define an account having a particular address.
|
|
45
|
+
* @typeParam TData - The nature of this account's data. It can be represented as either a
|
|
46
|
+
* `Uint8Array` – meaning the account is encoded – or a custom data type – meaning
|
|
47
|
+
* the account is decoded.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* // Encoded
|
|
52
|
+
* const myEncodedAccount: Account<Uint8Array, '1234..5678'> = {
|
|
53
|
+
* address: address('1234..5678'),
|
|
54
|
+
* data: new Uint8Array([1, 2, 3]),
|
|
55
|
+
* executable: false,
|
|
56
|
+
* lamports: lamports(1_000_000_000n),
|
|
57
|
+
* programAddress: address('1111..1111'),
|
|
58
|
+
* space: 42n,
|
|
59
|
+
* };
|
|
60
|
+
*
|
|
61
|
+
* // Decoded
|
|
62
|
+
* type MyAccountData = { name: string; age: number };
|
|
63
|
+
* const myDecodedAccount: Account<MyAccountData, '1234..5678'> = {
|
|
64
|
+
* address: address('1234..5678'),
|
|
65
|
+
* data: { name: 'Alice', age: 30 },
|
|
66
|
+
* executable: false,
|
|
67
|
+
* lamports: lamports(1_000_000_000n),
|
|
68
|
+
* programAddress: address('1111..1111'),
|
|
69
|
+
* space: 42n,
|
|
70
|
+
* };
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export type Account<TData extends Uint8Array | object, TAddress extends string = string> = BaseAccount & {
|
|
74
|
+
readonly address: Address<TAddress>;
|
|
75
|
+
readonly data: TData;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Represents an encoded account and is equivalent to an {@link Account} with `Uint8Array` account
|
|
80
|
+
* data.
|
|
81
|
+
*
|
|
82
|
+
* @interface
|
|
83
|
+
*
|
|
84
|
+
* @typeParam TAddress - Supply a string literal to define an account having a particular address.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* {
|
|
89
|
+
* address: address('1234..5678'),
|
|
90
|
+
* data: new Uint8Array([1, 2, 3]),
|
|
91
|
+
* executable: false,
|
|
92
|
+
* lamports: lamports(1_000_000_000n),
|
|
93
|
+
* programAddress: address('1111..1111'),
|
|
94
|
+
* space: 42n,
|
|
95
|
+
* } satisfies EncodedAccount<'1234..5678'>;
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export type EncodedAccount<TAddress extends string = string> = Account<ReadonlyUint8Array, TAddress>;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import type { Decoder, ReadonlyUint8Array } from '@solana/codecs-core';
|
|
2
|
+
import {
|
|
3
|
+
SOLANA_ERROR__ACCOUNTS__EXPECTED_ALL_ACCOUNTS_TO_BE_DECODED,
|
|
4
|
+
SOLANA_ERROR__ACCOUNTS__EXPECTED_DECODED_ACCOUNT,
|
|
5
|
+
SOLANA_ERROR__ACCOUNTS__FAILED_TO_DECODE_ACCOUNT,
|
|
6
|
+
SolanaError,
|
|
7
|
+
} from '@solana/errors';
|
|
8
|
+
|
|
9
|
+
import type { Account, EncodedAccount } from './account';
|
|
10
|
+
import type { MaybeAccount, MaybeEncodedAccount } from './maybe-account';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Transforms an {@link EncodedAccount} into an {@link Account} (or a {@link MaybeEncodedAccount}
|
|
14
|
+
* into a {@link MaybeAccount}) by decoding the account data using the provided {@link Decoder}
|
|
15
|
+
* instance.
|
|
16
|
+
*
|
|
17
|
+
* @typeParam TAddress - Supply a string literal to define an account having a particular address.
|
|
18
|
+
* @typeParam TData - The type of this account's data.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* type MyAccountData = { name: string; age: number };
|
|
23
|
+
*
|
|
24
|
+
* const myAccount: EncodedAccount<'1234..5678'>;
|
|
25
|
+
* const myDecoder: Decoder<MyAccountData> = getStructDecoder([
|
|
26
|
+
* ['name', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())],
|
|
27
|
+
* ['age', getU32Decoder()],
|
|
28
|
+
* ]);
|
|
29
|
+
*
|
|
30
|
+
* const myDecodedAccount = decodeAccount(myAccount, myDecoder);
|
|
31
|
+
* myDecodedAccount satisfies Account<MyAccountData, '1234..5678'>;
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export function decodeAccount<TData extends object, TAddress extends string = string>(
|
|
35
|
+
encodedAccount: EncodedAccount<TAddress>,
|
|
36
|
+
decoder: Decoder<TData>,
|
|
37
|
+
): Account<TData, TAddress>;
|
|
38
|
+
export function decodeAccount<TData extends object, TAddress extends string = string>(
|
|
39
|
+
encodedAccount: MaybeEncodedAccount<TAddress>,
|
|
40
|
+
decoder: Decoder<TData>,
|
|
41
|
+
): MaybeAccount<TData, TAddress>;
|
|
42
|
+
export function decodeAccount<TData extends object, TAddress extends string = string>(
|
|
43
|
+
encodedAccount: EncodedAccount<TAddress> | MaybeEncodedAccount<TAddress>,
|
|
44
|
+
decoder: Decoder<TData>,
|
|
45
|
+
): Account<TData, TAddress> | MaybeAccount<TData, TAddress> {
|
|
46
|
+
try {
|
|
47
|
+
if ('exists' in encodedAccount && !encodedAccount.exists) {
|
|
48
|
+
return encodedAccount;
|
|
49
|
+
}
|
|
50
|
+
return Object.freeze({ ...encodedAccount, data: decoder.decode(encodedAccount.data) });
|
|
51
|
+
} catch {
|
|
52
|
+
throw new SolanaError(SOLANA_ERROR__ACCOUNTS__FAILED_TO_DECODE_ACCOUNT, {
|
|
53
|
+
address: encodedAccount.address,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function accountExists<TData extends object>(account: Account<TData> | MaybeAccount<TData>): account is Account<TData> {
|
|
59
|
+
return !('exists' in account) || ('exists' in account && account.exists);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Asserts that an account stores decoded data, ie. not a `Uint8Array`.
|
|
64
|
+
*
|
|
65
|
+
* Note that it does not check the shape of the data matches the decoded type, only that it is not a
|
|
66
|
+
* `Uint8Array`.
|
|
67
|
+
*
|
|
68
|
+
* @typeParam TAddress - Supply a string literal to define an account having a particular address.
|
|
69
|
+
* @typeParam TData - The type of this account's data.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* type MyAccountData = { name: string; age: number };
|
|
74
|
+
*
|
|
75
|
+
* const myAccount: Account<MyAccountData | Uint8Array, '1234..5678'>;
|
|
76
|
+
* assertAccountDecoded(myAccount);
|
|
77
|
+
*
|
|
78
|
+
* // now the account data can be used as MyAccountData
|
|
79
|
+
* account.data satisfies MyAccountData;
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* This is particularly useful for narrowing the result of fetching a JSON parsed account.
|
|
83
|
+
*
|
|
84
|
+
* ```ts
|
|
85
|
+
* const account: MaybeAccount<MockData | Uint8Array> = await fetchJsonParsedAccount<MockData>(
|
|
86
|
+
* rpc,
|
|
87
|
+
* '1234..5678' as Address,
|
|
88
|
+
* );
|
|
89
|
+
*
|
|
90
|
+
* assertAccountDecoded(account);
|
|
91
|
+
* // now we have a MaybeAccount<MockData>
|
|
92
|
+
* account satisfies MaybeAccount<MockData>;
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export function assertAccountDecoded<TData extends object, TAddress extends string = string>(
|
|
96
|
+
account: Account<TData | Uint8Array, TAddress>,
|
|
97
|
+
): asserts account is Account<TData, TAddress>;
|
|
98
|
+
export function assertAccountDecoded<TData extends object, TAddress extends string = string>(
|
|
99
|
+
account: MaybeAccount<TData | Uint8Array, TAddress>,
|
|
100
|
+
): asserts account is MaybeAccount<TData, TAddress>;
|
|
101
|
+
export function assertAccountDecoded<TData extends object, TAddress extends string = string>(
|
|
102
|
+
account: Account<TData | Uint8Array, TAddress> | MaybeAccount<TData | Uint8Array, TAddress>,
|
|
103
|
+
): asserts account is Account<TData, TAddress> | MaybeAccount<TData, TAddress> {
|
|
104
|
+
if (accountExists(account) && account.data instanceof Uint8Array) {
|
|
105
|
+
throw new SolanaError(SOLANA_ERROR__ACCOUNTS__EXPECTED_DECODED_ACCOUNT, {
|
|
106
|
+
address: account.address,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Asserts that all input accounts store decoded data, ie. not a `Uint8Array`.
|
|
113
|
+
*
|
|
114
|
+
* As with {@link assertAccountDecoded} it does not check the shape of the data matches the decoded
|
|
115
|
+
* type, only that it is not a `Uint8Array`.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* type MyAccountData = { name: string; age: number };
|
|
120
|
+
*
|
|
121
|
+
* const myAccounts: Account<MyAccountData | Uint8Array, Address>[];
|
|
122
|
+
* assertAccountsDecoded(myAccounts);
|
|
123
|
+
*
|
|
124
|
+
* // now the account data can be used as MyAccountData
|
|
125
|
+
* for (const a of account) {
|
|
126
|
+
* account.data satisfies MyAccountData;
|
|
127
|
+
* }
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
export function assertAccountsDecoded<TData extends object, TAddress extends string = string>(
|
|
131
|
+
accounts: Account<ReadonlyUint8Array | TData, TAddress>[],
|
|
132
|
+
): asserts accounts is Account<TData, TAddress>[];
|
|
133
|
+
export function assertAccountsDecoded<TData extends object, TAddress extends string = string>(
|
|
134
|
+
accounts: MaybeAccount<ReadonlyUint8Array | TData, TAddress>[],
|
|
135
|
+
): asserts accounts is MaybeAccount<TData, TAddress>[];
|
|
136
|
+
export function assertAccountsDecoded<TData extends object, TAddress extends string = string>(
|
|
137
|
+
accounts: (Account<ReadonlyUint8Array | TData, TAddress> | MaybeAccount<ReadonlyUint8Array | TData, TAddress>)[],
|
|
138
|
+
): asserts accounts is (Account<TData, TAddress> | MaybeAccount<TData, TAddress>)[] {
|
|
139
|
+
const encoded = accounts.filter(a => accountExists(a) && a.data instanceof Uint8Array);
|
|
140
|
+
if (encoded.length > 0) {
|
|
141
|
+
const encodedAddresses = encoded.map(a => a.address);
|
|
142
|
+
throw new SolanaError(SOLANA_ERROR__ACCOUNTS__EXPECTED_ALL_ACCOUNTS_TO_BE_DECODED, {
|
|
143
|
+
addresses: encodedAddresses,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import type { Address } from '@solana/addresses';
|
|
2
|
+
import type { Rpc } from '@solana/rpc-spec';
|
|
3
|
+
import type { Commitment, Slot } from '@solana/rpc-types';
|
|
4
|
+
|
|
5
|
+
import type { MaybeAccount, MaybeEncodedAccount } from './maybe-account';
|
|
6
|
+
import { parseBase64RpcAccount, parseJsonRpcAccount } from './parse-account';
|
|
7
|
+
import type { GetAccountInfoApi, GetMultipleAccountsApi } from './rpc-api';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Optional configuration for fetching a singular account.
|
|
11
|
+
*
|
|
12
|
+
* @interface
|
|
13
|
+
*/
|
|
14
|
+
export type FetchAccountConfig = {
|
|
15
|
+
abortSignal?: AbortSignal;
|
|
16
|
+
/**
|
|
17
|
+
* Fetch the details of the account as of the highest slot that has reached this level of
|
|
18
|
+
* commitment.
|
|
19
|
+
*
|
|
20
|
+
* @defaultValue Whichever default is applied by the underlying {@link RpcApi} in use. For
|
|
21
|
+
* example, when using an API created by a `createSolanaRpc*()` helper, the default commitment
|
|
22
|
+
* is `"confirmed"` unless configured otherwise. Unmitigated by an API layer on the client, the
|
|
23
|
+
* default commitment applied by the server is `"finalized"`.
|
|
24
|
+
*/
|
|
25
|
+
commitment?: Commitment;
|
|
26
|
+
/**
|
|
27
|
+
* Prevents accessing stale data by enforcing that the RPC node has processed transactions up to
|
|
28
|
+
* this slot
|
|
29
|
+
*/
|
|
30
|
+
minContextSlot?: Slot;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Fetches a {@link MaybeEncodedAccount} from the provided RPC client and address.
|
|
35
|
+
*
|
|
36
|
+
* It uses the {@link GetAccountInfoApi.getAccountInfo | getAccountInfo} RPC method under the hood
|
|
37
|
+
* with base64 encoding and an additional configuration object can be provided to customize the
|
|
38
|
+
* behavior of the RPC call.
|
|
39
|
+
*
|
|
40
|
+
* @typeParam TAddress - Supply a string literal to define an account having a particular address.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const myAddress = address('1234..5678');
|
|
45
|
+
* const myAccount: MaybeEncodedAccount<'1234..5678'> = await fetchEncodedAccount(rpc, myAddress);
|
|
46
|
+
*
|
|
47
|
+
* // With custom configuration.
|
|
48
|
+
* const myAccount: MaybeEncodedAccount<'1234..5678'> = await fetchEncodedAccount(rpc, myAddress, {
|
|
49
|
+
* abortSignal: myAbortController.signal,
|
|
50
|
+
* commitment: 'confirmed',
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export async function fetchEncodedAccount<TAddress extends string = string>(
|
|
55
|
+
rpc: Rpc<GetAccountInfoApi>,
|
|
56
|
+
address: Address<TAddress>,
|
|
57
|
+
config: FetchAccountConfig = {},
|
|
58
|
+
): Promise<MaybeEncodedAccount<TAddress>> {
|
|
59
|
+
const { abortSignal, ...rpcConfig } = config;
|
|
60
|
+
const response = await rpc.getAccountInfo(address, { ...rpcConfig, encoding: 'base64' }).send({ abortSignal });
|
|
61
|
+
return parseBase64RpcAccount(address, response.value);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Fetches a {@link MaybeAccount} from the provided RPC client and address by using
|
|
66
|
+
* {@link GetAccountInfoApi.getAccountInfo | getAccountInfo} under the hood with the `jsonParsed`
|
|
67
|
+
* encoding.
|
|
68
|
+
*
|
|
69
|
+
* It may also return a {@link MaybeEncodedAccount} if the RPC client does not know how to parse the
|
|
70
|
+
* account at the requested address. In any case, the expected data type should be explicitly
|
|
71
|
+
* provided as the first type parameter.
|
|
72
|
+
*
|
|
73
|
+
* @typeParam TAddress - Supply a string literal to define an account having a particular address.
|
|
74
|
+
* @typeParam TData - The expected type of this account's data.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* type TokenData = { mint: Address; owner: Address };
|
|
79
|
+
* const myAccount = await fetchJsonParsedAccount<TokenData>(rpc, myAddress);
|
|
80
|
+
* myAccount satisfies MaybeAccount<TokenData> | MaybeEncodedAccount;
|
|
81
|
+
*
|
|
82
|
+
* // With custom configuration.
|
|
83
|
+
* const myAccount = await fetchJsonParsedAccount<TokenData>(rpc, myAddress, {
|
|
84
|
+
* abortSignal: myAbortController.signal,
|
|
85
|
+
* commitment: 'confirmed',
|
|
86
|
+
* });
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export async function fetchJsonParsedAccount<TData extends object, TAddress extends string = string>(
|
|
90
|
+
rpc: Rpc<GetAccountInfoApi>,
|
|
91
|
+
address: Address<TAddress>,
|
|
92
|
+
config: FetchAccountConfig = {},
|
|
93
|
+
): Promise<
|
|
94
|
+
| MaybeAccount<TData & { parsedAccountMeta?: { program: string; type?: string } }, TAddress>
|
|
95
|
+
| MaybeEncodedAccount<TAddress>
|
|
96
|
+
> {
|
|
97
|
+
const { abortSignal, ...rpcConfig } = config;
|
|
98
|
+
const { value: account } = await rpc
|
|
99
|
+
.getAccountInfo(address, { ...rpcConfig, encoding: 'jsonParsed' })
|
|
100
|
+
.send({ abortSignal });
|
|
101
|
+
return !!account && typeof account === 'object' && 'parsed' in account.data
|
|
102
|
+
? parseJsonRpcAccount<TData, TAddress>(address, account as Parameters<typeof parseJsonRpcAccount>[1])
|
|
103
|
+
: parseBase64RpcAccount<TAddress>(address, account as Parameters<typeof parseBase64RpcAccount>[1]);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Optional configuration for fetching multiple accounts.
|
|
108
|
+
*
|
|
109
|
+
* @interface
|
|
110
|
+
*/
|
|
111
|
+
export type FetchAccountsConfig = {
|
|
112
|
+
abortSignal?: AbortSignal;
|
|
113
|
+
/**
|
|
114
|
+
* Fetch the details of the accounts as of the highest slot that has reached this level of
|
|
115
|
+
* commitment.
|
|
116
|
+
*
|
|
117
|
+
* @defaultValue Whichever default is applied by the underlying {@link RpcApi} in use. For
|
|
118
|
+
* example, when using an API created by a `createSolanaRpc*()` helper, the default commitment
|
|
119
|
+
* is `"confirmed"` unless configured otherwise. Unmitigated by an API layer on the client, the
|
|
120
|
+
* default commitment applied by the server is `"finalized"`.
|
|
121
|
+
*/
|
|
122
|
+
commitment?: Commitment;
|
|
123
|
+
/**
|
|
124
|
+
* Prevents accessing stale data by enforcing that the RPC node has processed transactions up to
|
|
125
|
+
* this slot
|
|
126
|
+
*/
|
|
127
|
+
minContextSlot?: Slot;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Fetches an array of {@link MaybeEncodedAccount | MaybeEncodedAccounts} from the provided RPC
|
|
132
|
+
* client and an array of addresses.
|
|
133
|
+
*
|
|
134
|
+
* It uses the {@link GetMultipleAccountsApi#getMultipleAccounts | getMultipleAccounts} RPC method
|
|
135
|
+
* under the hood with base64 encodings and an additional configuration object can be provided to
|
|
136
|
+
* customize the behavior of the RPC call.
|
|
137
|
+
*
|
|
138
|
+
* @typeParam TAddresses - Supply an array of string literals to define accounts having particular
|
|
139
|
+
* addresses.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```ts
|
|
143
|
+
* const myAddressA = address('1234..5678');
|
|
144
|
+
* const myAddressB = address('8765..4321');
|
|
145
|
+
* const [myAccountA, myAccountB] = await fetchEncodedAccounts(rpc, [myAddressA, myAddressB]);
|
|
146
|
+
* myAccountA satisfies MaybeEncodedAccount<'1234..5678'>;
|
|
147
|
+
* myAccountB satisfies MaybeEncodedAccount<'8765..4321'>;
|
|
148
|
+
*
|
|
149
|
+
* // With custom configuration.
|
|
150
|
+
* const [myAccountA, myAccountB] = await fetchEncodedAccounts(rpc, [myAddressA, myAddressB], {
|
|
151
|
+
* abortSignal: myAbortController.signal,
|
|
152
|
+
* commitment: 'confirmed',
|
|
153
|
+
* });
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
export async function fetchEncodedAccounts<
|
|
157
|
+
TAddresses extends string[] = string[],
|
|
158
|
+
TWrappedAddresses extends { [P in keyof TAddresses]: Address<TAddresses[P]> } = {
|
|
159
|
+
[P in keyof TAddresses]: Address<TAddresses[P]>;
|
|
160
|
+
},
|
|
161
|
+
>(rpc: Rpc<GetMultipleAccountsApi>, addresses: TWrappedAddresses, config: FetchAccountsConfig = {}) {
|
|
162
|
+
const { abortSignal, ...rpcConfig } = config;
|
|
163
|
+
const response = await rpc
|
|
164
|
+
.getMultipleAccounts(addresses, { ...rpcConfig, encoding: 'base64' })
|
|
165
|
+
.send({ abortSignal });
|
|
166
|
+
return response.value.map((account, index) => parseBase64RpcAccount(addresses[index], account)) as {
|
|
167
|
+
[P in keyof TAddresses]: MaybeEncodedAccount<TAddresses[P]>;
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Fetches an array of {@link MaybeAccount | MaybeAccounts} from a provided RPC client and an array
|
|
173
|
+
* of addresses.
|
|
174
|
+
*
|
|
175
|
+
* It uses the {@link GetMultipleAccountsApi#getMultipleAccounts | getMultipleAccounts} RPC method
|
|
176
|
+
* under the hood with the `jsonParsed` encoding. It may also return a
|
|
177
|
+
* {@link MaybeEncodedAccount} instead of the expected {@link MaybeAccount} if the RPC client does
|
|
178
|
+
* not know how to parse some of the requested accounts. In any case, the array of expected data
|
|
179
|
+
* types should be explicitly provided as the first type parameter.
|
|
180
|
+
*
|
|
181
|
+
* @typeParam TAddresses - Supply an array of string literals to define accounts having particular
|
|
182
|
+
* addresses.
|
|
183
|
+
* @typeParam TData - The expected types of these accounts' data.
|
|
184
|
+
|
|
185
|
+
* @example
|
|
186
|
+
* ```ts
|
|
187
|
+
* type TokenData = { mint: Address; owner: Address };
|
|
188
|
+
* type MintData = { supply: bigint };
|
|
189
|
+
* const [myAccountA, myAccountB] = await fetchJsonParsedAccounts<[TokenData, MintData]>(rpc, [myAddressA, myAddressB]);
|
|
190
|
+
* myAccountA satisfies MaybeAccount<TokenData> | MaybeEncodedAccount;
|
|
191
|
+
* myAccountB satisfies MaybeAccount<MintData> | MaybeEncodedAccount;
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
export async function fetchJsonParsedAccounts<
|
|
195
|
+
TData extends object[],
|
|
196
|
+
TAddresses extends string[] = string[],
|
|
197
|
+
TWrappedAddresses extends { [P in keyof TAddresses]: Address<TAddresses[P]> } = {
|
|
198
|
+
[P in keyof TAddresses]: Address<TAddresses[P]>;
|
|
199
|
+
},
|
|
200
|
+
>(rpc: Rpc<GetMultipleAccountsApi>, addresses: TWrappedAddresses, config: FetchAccountsConfig = {}) {
|
|
201
|
+
const { abortSignal, ...rpcConfig } = config;
|
|
202
|
+
const response = await rpc
|
|
203
|
+
.getMultipleAccounts(addresses, { ...rpcConfig, encoding: 'jsonParsed' })
|
|
204
|
+
.send({ abortSignal });
|
|
205
|
+
return response.value.map((account, index) => {
|
|
206
|
+
return !!account && typeof account === 'object' && 'parsed' in account.data
|
|
207
|
+
? parseJsonRpcAccount(addresses[index], account as Parameters<typeof parseJsonRpcAccount>[1])
|
|
208
|
+
: parseBase64RpcAccount(addresses[index], account as Parameters<typeof parseBase64RpcAccount>[1]);
|
|
209
|
+
}) as {
|
|
210
|
+
[P in keyof TAddresses]:
|
|
211
|
+
| MaybeAccount<
|
|
212
|
+
TData[P & keyof TData] & { parsedAccountMeta?: { program: string; type?: string } },
|
|
213
|
+
TAddresses[P]
|
|
214
|
+
>
|
|
215
|
+
| MaybeEncodedAccount<TAddresses[P]>;
|
|
216
|
+
} & {
|
|
217
|
+
[P in keyof TData]:
|
|
218
|
+
| MaybeAccount<
|
|
219
|
+
TData[P] & { parsedAccountMeta?: { program: string; type?: string } },
|
|
220
|
+
TAddresses[P & keyof TAddresses]
|
|
221
|
+
>
|
|
222
|
+
| MaybeEncodedAccount<TAddresses[P & keyof TAddresses]>;
|
|
223
|
+
};
|
|
224
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This package contains types and helper methods for representing, fetching and decoding Solana
|
|
3
|
+
* accounts. It can be used standalone, but it is also exported as part of Kit
|
|
4
|
+
* [`@solana/kit`](https://github.com/anza-xyz/kit/tree/main/packages/kit).
|
|
5
|
+
*
|
|
6
|
+
* It provides a unified definition of a Solana account regardless of how it was retrieved and can
|
|
7
|
+
* represent both encoded and decoded accounts. It also introduces the concept of a
|
|
8
|
+
* {@link MaybeAccount} which represents a fetched account that may or may not exist on-chain whilst
|
|
9
|
+
* keeping track of its address in both cases.
|
|
10
|
+
*
|
|
11
|
+
* Helper functions are provided for fetching, parsing and decoding accounts as well as asserting
|
|
12
|
+
* that an account exists.
|
|
13
|
+
*
|
|
14
|
+
* @packageDocumentation
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* // Fetch.
|
|
18
|
+
* const myAddress = address('1234..5678');
|
|
19
|
+
* const myAccount = await fetchEncodedAccount(rpc, myAddress);
|
|
20
|
+
* myAccount satisfies MaybeEncodedAccount<'1234..5678'>;
|
|
21
|
+
*
|
|
22
|
+
* // Assert.
|
|
23
|
+
* assertAccountExists(myAccount);
|
|
24
|
+
* myAccount satisfies EncodedAccount<'1234..5678'>;
|
|
25
|
+
*
|
|
26
|
+
* // Decode.
|
|
27
|
+
* type MyAccountData = { name: string; age: number };
|
|
28
|
+
* const myDecoder: Decoder<MyAccountData> = getStructDecoder([
|
|
29
|
+
* ['name', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())],
|
|
30
|
+
* ['age', getU32Decoder()],
|
|
31
|
+
* ]);
|
|
32
|
+
* const myDecodedAccount = decodeAccount(myAccount, myDecoder);
|
|
33
|
+
* myDecodedAccount satisfies Account<MyAccountData, '1234..5678'>;
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export * from './account';
|
|
37
|
+
export * from './decode-account';
|
|
38
|
+
export * from './fetch-account';
|
|
39
|
+
export * from './maybe-account';
|
|
40
|
+
export * from './parse-account';
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { Address } from '@solana/addresses';
|
|
2
|
+
import {
|
|
3
|
+
SOLANA_ERROR__ACCOUNTS__ACCOUNT_NOT_FOUND,
|
|
4
|
+
SOLANA_ERROR__ACCOUNTS__ONE_OR_MORE_ACCOUNTS_NOT_FOUND,
|
|
5
|
+
SolanaError,
|
|
6
|
+
} from '@solana/errors';
|
|
7
|
+
|
|
8
|
+
import { Account } from './account';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Represents an account that may or may not exist on-chain.
|
|
12
|
+
*
|
|
13
|
+
* When the account exists, it is represented as an {@link Account} type with an additional `exists`
|
|
14
|
+
* attribute set to `true`. When it does not exist, it is represented by an object containing only
|
|
15
|
+
* the address of the account and an `exists` attribute set to `false`.
|
|
16
|
+
*
|
|
17
|
+
* @typeParam TAddress - Supply a string literal to define an account having a particular address.
|
|
18
|
+
* @typeParam TData - The nature of this account's data. It can be represented as either a
|
|
19
|
+
* `Uint8Array` – meaning the account is encoded – or a custom data type – meaning
|
|
20
|
+
* the account is decoded.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* // Account exists
|
|
25
|
+
* const myExistingAccount: MaybeAccount<MyAccountData, '1234..5678'> = {
|
|
26
|
+
* exists: true,
|
|
27
|
+
* address: address('1234..5678'),
|
|
28
|
+
* data: { name: 'Alice', age: 30 },
|
|
29
|
+
* // ...
|
|
30
|
+
* };
|
|
31
|
+
*
|
|
32
|
+
* // Account does not exist
|
|
33
|
+
* const myMissingAccount: MaybeAccount<MyAccountData, '8765..4321'> = {
|
|
34
|
+
* exists: false,
|
|
35
|
+
* address: address('8765..4321'),
|
|
36
|
+
* };
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export type MaybeAccount<TData extends Uint8Array | object, TAddress extends string = string> =
|
|
40
|
+
| { readonly address: Address<TAddress>; readonly exists: false }
|
|
41
|
+
| (Account<TData, TAddress> & { readonly exists: true });
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Represents an encoded account that may or may not exist on-chain.
|
|
45
|
+
*
|
|
46
|
+
* When the account exists, it is represented as an {@link Account} type having its `TData` type
|
|
47
|
+
* parameter set to `Uint8Array` with an additional `exists` attribute set to `true`. When it does
|
|
48
|
+
* not exist, it is represented by an object containing only the address of the account and an
|
|
49
|
+
* `exists` attribute set to `false`.
|
|
50
|
+
*
|
|
51
|
+
* @typeParam TAddress - Supply a string literal to define an account having a particular address.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* // Encoded account exists
|
|
56
|
+
* const myExistingAccount: MaybeEncodedAccount<'1234..5678'> = {
|
|
57
|
+
* exists: true,
|
|
58
|
+
* address: address('1234..5678'),
|
|
59
|
+
* data: new Uint8Array([1, 2, 3]),
|
|
60
|
+
* // ...
|
|
61
|
+
* };
|
|
62
|
+
*
|
|
63
|
+
* // Encoded account does not exist
|
|
64
|
+
* const myMissingAccount: MaybeEncodedAccount<'8765..4321'> = {
|
|
65
|
+
* exists: false,
|
|
66
|
+
* address: address('8765..4321'),
|
|
67
|
+
* };
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export type MaybeEncodedAccount<TAddress extends string = string> = MaybeAccount<Uint8Array, TAddress>;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Given a {@link MaybeAccount}, asserts that the account exists and allows it to be used as an
|
|
74
|
+
* {@link Account} type going forward.
|
|
75
|
+
*
|
|
76
|
+
* @typeParam TAddress - Supply a string literal to define an account having a particular address.
|
|
77
|
+
* @typeParam TData - The nature of this account's data. It can be represented as either a
|
|
78
|
+
* `Uint8Array` – meaning the account is encoded – or a custom data type – meaning
|
|
79
|
+
* the account is decoded.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* const myAccount: MaybeEncodedAccount<'1234..5678'>;
|
|
84
|
+
* assertAccountExists(myAccount);
|
|
85
|
+
*
|
|
86
|
+
* // Now we can use myAccount as an `EncodedAccount`
|
|
87
|
+
* myAccount satisfies EncodedAccount<'1234..5678'>;
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export function assertAccountExists<TData extends Uint8Array | object, TAddress extends string = string>(
|
|
91
|
+
account: MaybeAccount<TData, TAddress>,
|
|
92
|
+
): asserts account is Account<TData, TAddress> & { exists: true } {
|
|
93
|
+
if (!account.exists) {
|
|
94
|
+
throw new SolanaError(SOLANA_ERROR__ACCOUNTS__ACCOUNT_NOT_FOUND, { address: account.address });
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Given an array of {@link MaybeAccount | MaybeAccounts}, asserts that all the accounts exist and
|
|
100
|
+
* allows them to be used as an array of {@link Account | Accounts} going forward.
|
|
101
|
+
*
|
|
102
|
+
* @typeParam TAddress - Supply a string literal to define an account having a particular address.
|
|
103
|
+
* @typeParam TData - The nature of this account's data. It can be represented as either a
|
|
104
|
+
* `Uint8Array` – meaning the account is encoded – or a custom data type – meaning
|
|
105
|
+
* the account is decoded.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* const myAccounts: MaybeEncodedAccount<Address>[];
|
|
110
|
+
* assertAccountsExist(myAccounts);
|
|
111
|
+
*
|
|
112
|
+
* // Now we can use them as an array of `EncodedAccounts`
|
|
113
|
+
* for (const a of myAccounts) {
|
|
114
|
+
* a satisfies EncodedAccount<Address>;
|
|
115
|
+
* }
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
export function assertAccountsExist<TData extends Uint8Array | object, TAddress extends string = string>(
|
|
119
|
+
accounts: MaybeAccount<TData, TAddress>[],
|
|
120
|
+
): asserts accounts is (Account<TData, TAddress> & { exists: true })[] {
|
|
121
|
+
const missingAccounts = accounts.filter(a => !a.exists);
|
|
122
|
+
if (missingAccounts.length > 0) {
|
|
123
|
+
const missingAddresses = missingAccounts.map(a => a.address);
|
|
124
|
+
throw new SolanaError(SOLANA_ERROR__ACCOUNTS__ONE_OR_MORE_ACCOUNTS_NOT_FOUND, { addresses: missingAddresses });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import type { Address } from '@solana/addresses';
|
|
2
|
+
import { getBase58Encoder, getBase64Encoder } from '@solana/codecs-strings';
|
|
3
|
+
import type {
|
|
4
|
+
AccountInfoBase,
|
|
5
|
+
AccountInfoWithBase58Bytes,
|
|
6
|
+
AccountInfoWithBase58EncodedData,
|
|
7
|
+
AccountInfoWithBase64EncodedData,
|
|
8
|
+
} from '@solana/rpc-types';
|
|
9
|
+
|
|
10
|
+
import type { Account, BaseAccount, EncodedAccount } from './account';
|
|
11
|
+
import type { MaybeAccount, MaybeEncodedAccount } from './maybe-account';
|
|
12
|
+
import type { JsonParsedDataResponse } from './rpc-api';
|
|
13
|
+
|
|
14
|
+
type Base64EncodedRpcAccount = AccountInfoBase & AccountInfoWithBase64EncodedData;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Parses a base64-encoded account provided by the RPC client into an {@link EncodedAccount} type or
|
|
18
|
+
* a {@link MaybeEncodedAccount} type if the raw data can be set to `null`.
|
|
19
|
+
*
|
|
20
|
+
* @typeParam TAddress - Supply a string literal to define an account having a particular address.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* const myAddress = address('1234..5678');
|
|
25
|
+
* const myRpcAccount = await rpc.getAccountInfo(myAddress, { encoding: 'base64' }).send();
|
|
26
|
+
* const myAccount: MaybeEncodedAccount<'1234..5678'> = parseBase64RpcAccount(myRpcAccount);
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function parseBase64RpcAccount<TAddress extends string = string>(
|
|
30
|
+
address: Address<TAddress>,
|
|
31
|
+
rpcAccount: Base64EncodedRpcAccount,
|
|
32
|
+
): EncodedAccount<TAddress>;
|
|
33
|
+
export function parseBase64RpcAccount<TAddress extends string = string>(
|
|
34
|
+
address: Address<TAddress>,
|
|
35
|
+
rpcAccount: Base64EncodedRpcAccount | null,
|
|
36
|
+
): MaybeEncodedAccount<TAddress>;
|
|
37
|
+
export function parseBase64RpcAccount<TAddress extends string = string>(
|
|
38
|
+
address: Address<TAddress>,
|
|
39
|
+
rpcAccount: Base64EncodedRpcAccount | null,
|
|
40
|
+
): EncodedAccount<TAddress> | MaybeEncodedAccount<TAddress> {
|
|
41
|
+
if (!rpcAccount) return Object.freeze({ address, exists: false });
|
|
42
|
+
const data = getBase64Encoder().encode(rpcAccount.data[0]);
|
|
43
|
+
return Object.freeze({ ...parseBaseAccount(rpcAccount), address, data, exists: true });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
type Base58EncodedRpcAccount = AccountInfoBase & (AccountInfoWithBase58Bytes | AccountInfoWithBase58EncodedData);
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Parses a base58-encoded account provided by the RPC client into an {@link EncodedAccount} type or
|
|
50
|
+
* a {@link MaybeEncodedAccount} type if the raw data can be set to `null`.
|
|
51
|
+
*
|
|
52
|
+
* @typeParam TAddress - Supply a string literal to define an account having a particular address.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* const myAddress = address('1234..5678');
|
|
57
|
+
* const myRpcAccount = await rpc.getAccountInfo(myAddress, { encoding: 'base58' }).send();
|
|
58
|
+
* const myAccount: MaybeEncodedAccount<'1234..5678'> = parseBase58RpcAccount(myRpcAccount);
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export function parseBase58RpcAccount<TAddress extends string = string>(
|
|
62
|
+
address: Address<TAddress>,
|
|
63
|
+
rpcAccount: Base58EncodedRpcAccount,
|
|
64
|
+
): EncodedAccount<TAddress>;
|
|
65
|
+
export function parseBase58RpcAccount<TAddress extends string = string>(
|
|
66
|
+
address: Address<TAddress>,
|
|
67
|
+
rpcAccount: Base58EncodedRpcAccount | null,
|
|
68
|
+
): MaybeEncodedAccount<TAddress>;
|
|
69
|
+
export function parseBase58RpcAccount<TAddress extends string = string>(
|
|
70
|
+
address: Address<TAddress>,
|
|
71
|
+
rpcAccount: Base58EncodedRpcAccount | null,
|
|
72
|
+
): EncodedAccount<TAddress> | MaybeEncodedAccount<TAddress> {
|
|
73
|
+
if (!rpcAccount) return Object.freeze({ address, exists: false });
|
|
74
|
+
const data = getBase58Encoder().encode(typeof rpcAccount.data === 'string' ? rpcAccount.data : rpcAccount.data[0]);
|
|
75
|
+
return Object.freeze({ ...parseBaseAccount(rpcAccount), address, data, exists: true });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
type JsonParsedRpcAccount = AccountInfoBase & { readonly data: JsonParsedDataResponse<unknown> };
|
|
79
|
+
type ParsedAccountMeta = { program: string; type?: string };
|
|
80
|
+
type JsonParsedAccountData<TData extends object> = TData & { parsedAccountMeta?: ParsedAccountMeta };
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Parses an arbitrary `jsonParsed` account provided by the RPC client into an {@link Account} type
|
|
84
|
+
* or a {@link MaybeAccount} type if the raw data can be set to `null`.
|
|
85
|
+
*
|
|
86
|
+
* The expected data type should be explicitly provided as the first type parameter.
|
|
87
|
+
*
|
|
88
|
+
* @typeParam TAddress - Supply a string literal to define an account having a particular address.
|
|
89
|
+
* @typeParam TData - The expected type of this account's data.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* const myAccount: Account<MyData> = parseJsonRpcAccount<MyData>(myJsonRpcAccount);
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export function parseJsonRpcAccount<TData extends object, TAddress extends string = string>(
|
|
97
|
+
address: Address<TAddress>,
|
|
98
|
+
rpcAccount: JsonParsedRpcAccount,
|
|
99
|
+
): Account<JsonParsedAccountData<TData>, TAddress>;
|
|
100
|
+
export function parseJsonRpcAccount<TData extends object, TAddress extends string = string>(
|
|
101
|
+
address: Address<TAddress>,
|
|
102
|
+
rpcAccount: JsonParsedRpcAccount | null,
|
|
103
|
+
): MaybeAccount<JsonParsedAccountData<TData>, TAddress>;
|
|
104
|
+
export function parseJsonRpcAccount<TData extends object, TAddress extends string = string>(
|
|
105
|
+
address: Address<TAddress>,
|
|
106
|
+
rpcAccount: JsonParsedRpcAccount | null,
|
|
107
|
+
): Account<JsonParsedAccountData<TData>, TAddress> | MaybeAccount<JsonParsedAccountData<TData>, TAddress> {
|
|
108
|
+
if (!rpcAccount) return Object.freeze({ address, exists: false });
|
|
109
|
+
const data = (rpcAccount.data.parsed.info || {}) as TData;
|
|
110
|
+
|
|
111
|
+
if (rpcAccount.data.program || rpcAccount.data.parsed.type) {
|
|
112
|
+
(data as JsonParsedAccountData<TData>).parsedAccountMeta = {
|
|
113
|
+
program: rpcAccount.data.program,
|
|
114
|
+
type: rpcAccount.data.parsed.type,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return Object.freeze({ ...parseBaseAccount(rpcAccount), address, data, exists: true });
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function parseBaseAccount(rpcAccount: AccountInfoBase): BaseAccount {
|
|
122
|
+
return Object.freeze({
|
|
123
|
+
executable: rpcAccount.executable,
|
|
124
|
+
lamports: rpcAccount.lamports,
|
|
125
|
+
programAddress: rpcAccount.owner,
|
|
126
|
+
space: rpcAccount.space,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import type { Address } from '@solana/addresses';
|
|
2
|
+
import type {
|
|
3
|
+
AccountInfoBase,
|
|
4
|
+
AccountInfoWithBase58Bytes,
|
|
5
|
+
AccountInfoWithBase58EncodedData,
|
|
6
|
+
AccountInfoWithBase64EncodedData,
|
|
7
|
+
AccountInfoWithBase64EncodedZStdCompressedData,
|
|
8
|
+
AccountInfoWithJsonData,
|
|
9
|
+
Commitment,
|
|
10
|
+
DataSlice,
|
|
11
|
+
Slot,
|
|
12
|
+
SolanaRpcResponse,
|
|
13
|
+
} from '@solana/rpc-types';
|
|
14
|
+
|
|
15
|
+
type GetAccountInfoApiResponseBase = SolanaRpcResponse<AccountInfoBase | null>;
|
|
16
|
+
|
|
17
|
+
type NestInRpcResponseOrNull<T> = Readonly<{
|
|
18
|
+
value: T | null;
|
|
19
|
+
}>;
|
|
20
|
+
|
|
21
|
+
type GetAccountInfoApiCommonConfig = Readonly<{
|
|
22
|
+
/**
|
|
23
|
+
* Fetch the details of the account as of the highest slot that has reached this level of
|
|
24
|
+
* commitment.
|
|
25
|
+
*
|
|
26
|
+
* @defaultValue Whichever default is applied by the underlying {@link RpcApi} in use. For
|
|
27
|
+
* example, when using an API created by a `createSolanaRpc*()` helper, the default commitment
|
|
28
|
+
* is `"confirmed"` unless configured otherwise. Unmitigated by an API layer on the client, the
|
|
29
|
+
* default commitment applied by the server is `"finalized"`.
|
|
30
|
+
*/
|
|
31
|
+
commitment?: Commitment;
|
|
32
|
+
/**
|
|
33
|
+
* Determines how the account data should be encoded in the response.
|
|
34
|
+
*
|
|
35
|
+
* - `'base58'` returns a tuple whose first element is the data as a base58-encoded string. If
|
|
36
|
+
* the account contains more than 129 bytes of data, an error will be raised.
|
|
37
|
+
* - `'base64'` returns a tuple whose first element is the data as a base64-encoded string.
|
|
38
|
+
* - `'base64+zstd'` returns a tuple whose first element is the
|
|
39
|
+
* [ZStandard](https://facebook.github.io/zstd/)-compressed data as a base64-encoded string.
|
|
40
|
+
* - `'jsonParsed'` will cause the server to attempt to process the data using a parser specific
|
|
41
|
+
* to the account's owning program. If successful, the parsed data will be returned in the
|
|
42
|
+
* response as JSON. Otherwise, the raw account data will be returned in the response as a
|
|
43
|
+
* tuple whose first element is a base64-encoded string.
|
|
44
|
+
*/
|
|
45
|
+
encoding: 'base58' | 'base64' | 'base64+zstd' | 'jsonParsed';
|
|
46
|
+
/**
|
|
47
|
+
* Prevents accessing stale data by enforcing that the RPC node has processed transactions up to
|
|
48
|
+
* this slot
|
|
49
|
+
*/
|
|
50
|
+
minContextSlot?: Slot;
|
|
51
|
+
}>;
|
|
52
|
+
|
|
53
|
+
type GetAccountInfoApiSliceableCommonConfig = Readonly<{
|
|
54
|
+
/**
|
|
55
|
+
* Define which slice of the account's data you want the RPC to return.
|
|
56
|
+
*
|
|
57
|
+
* Use this to save network bandwidth and encoding time when you do not need the entire buffer.
|
|
58
|
+
*
|
|
59
|
+
* Data slicing is only available for `"base58"`, `"base64"`, and `"base64+zstd"` encodings.
|
|
60
|
+
*/
|
|
61
|
+
dataSlice?: DataSlice;
|
|
62
|
+
}>;
|
|
63
|
+
|
|
64
|
+
export type GetAccountInfoApi = {
|
|
65
|
+
/**
|
|
66
|
+
* Fetches information associated with the account at the given address.
|
|
67
|
+
*
|
|
68
|
+
* If the account has data, it will be returned in the response as a base64-encoded string.
|
|
69
|
+
*
|
|
70
|
+
* {@label base64}
|
|
71
|
+
* @see https://solana.com/docs/rpc/http/getaccountinfo
|
|
72
|
+
*/
|
|
73
|
+
getAccountInfo(
|
|
74
|
+
address: Address,
|
|
75
|
+
config: GetAccountInfoApiCommonConfig &
|
|
76
|
+
GetAccountInfoApiSliceableCommonConfig &
|
|
77
|
+
Readonly<{
|
|
78
|
+
encoding: 'base64';
|
|
79
|
+
}>,
|
|
80
|
+
): GetAccountInfoApiResponseBase & NestInRpcResponseOrNull<AccountInfoWithBase64EncodedData>;
|
|
81
|
+
/**
|
|
82
|
+
* Fetches information associated with the account at the given address.
|
|
83
|
+
*
|
|
84
|
+
* If the account has data, it will first be compressed using
|
|
85
|
+
* [ZStandard](https://facebook.github.io/zstd/) and the result will be returned in the response
|
|
86
|
+
* as a base64-encoded string.
|
|
87
|
+
*
|
|
88
|
+
* {@label base64-zstd-compressed}
|
|
89
|
+
* @see https://solana.com/docs/rpc/http/getaccountinfo
|
|
90
|
+
*/
|
|
91
|
+
getAccountInfo(
|
|
92
|
+
address: Address,
|
|
93
|
+
config: GetAccountInfoApiCommonConfig &
|
|
94
|
+
GetAccountInfoApiSliceableCommonConfig &
|
|
95
|
+
Readonly<{
|
|
96
|
+
encoding: 'base64+zstd';
|
|
97
|
+
}>,
|
|
98
|
+
): GetAccountInfoApiResponseBase & NestInRpcResponseOrNull<AccountInfoWithBase64EncodedZStdCompressedData>;
|
|
99
|
+
/**
|
|
100
|
+
* Fetches information associated with the account at the given address.
|
|
101
|
+
*
|
|
102
|
+
* If the account has data, the server will attempt to process it using a parser specific to the
|
|
103
|
+
* account's owning program. If successful, the parsed data will be returned in the response as
|
|
104
|
+
* JSON. Otherwise, the raw account data will be returned in the response as a base64-encoded
|
|
105
|
+
* string.
|
|
106
|
+
*
|
|
107
|
+
* {@label parsed}
|
|
108
|
+
* @see https://solana.com/docs/rpc/http/getaccountinfo
|
|
109
|
+
*/
|
|
110
|
+
getAccountInfo(
|
|
111
|
+
address: Address,
|
|
112
|
+
config: GetAccountInfoApiCommonConfig &
|
|
113
|
+
Readonly<{
|
|
114
|
+
encoding: 'jsonParsed';
|
|
115
|
+
}>,
|
|
116
|
+
): GetAccountInfoApiResponseBase & NestInRpcResponseOrNull<AccountInfoWithJsonData>;
|
|
117
|
+
/**
|
|
118
|
+
* Fetches information associated with the account at the given address.
|
|
119
|
+
*
|
|
120
|
+
* If the account has data, it will be returned in the response as a base58-encoded string. If
|
|
121
|
+
* the account contains more than 129 bytes of data, this method will raise an error.
|
|
122
|
+
*
|
|
123
|
+
* {@label base58}
|
|
124
|
+
* @see https://solana.com/docs/rpc/http/getaccountinfo
|
|
125
|
+
*/
|
|
126
|
+
getAccountInfo(
|
|
127
|
+
address: Address,
|
|
128
|
+
config: GetAccountInfoApiCommonConfig &
|
|
129
|
+
GetAccountInfoApiSliceableCommonConfig &
|
|
130
|
+
Readonly<{
|
|
131
|
+
encoding: 'base58';
|
|
132
|
+
}>,
|
|
133
|
+
): GetAccountInfoApiResponseBase & NestInRpcResponseOrNull<AccountInfoWithBase58EncodedData>;
|
|
134
|
+
/**
|
|
135
|
+
* Fetches information associated with the account at the given address.
|
|
136
|
+
*
|
|
137
|
+
* If the account has data, it will be returned in the response as a base58-encoded string. If
|
|
138
|
+
* the account contains more than 129 bytes of data, this method will raise an error.
|
|
139
|
+
*
|
|
140
|
+
* {@label base58-legacy}
|
|
141
|
+
* @see https://solana.com/docs/rpc/http/getaccountinfo
|
|
142
|
+
*/
|
|
143
|
+
getAccountInfo(
|
|
144
|
+
address: Address,
|
|
145
|
+
config?: Omit<GetAccountInfoApiCommonConfig, 'encoding'>,
|
|
146
|
+
): GetAccountInfoApiResponseBase & NestInRpcResponseOrNull<AccountInfoWithBase58Bytes>;
|
|
147
|
+
};
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import type { Address } from '@solana/addresses';
|
|
2
|
+
import type {
|
|
3
|
+
AccountInfoBase,
|
|
4
|
+
AccountInfoWithBase58EncodedData,
|
|
5
|
+
AccountInfoWithBase64EncodedData,
|
|
6
|
+
AccountInfoWithBase64EncodedZStdCompressedData,
|
|
7
|
+
AccountInfoWithJsonData,
|
|
8
|
+
Commitment,
|
|
9
|
+
DataSlice,
|
|
10
|
+
Slot,
|
|
11
|
+
SolanaRpcResponse,
|
|
12
|
+
} from '@solana/rpc-types';
|
|
13
|
+
|
|
14
|
+
type GetMultipleAccountsApiResponseBase = AccountInfoBase | null;
|
|
15
|
+
|
|
16
|
+
type GetMultipleAccountsApiCommonConfig = Readonly<{
|
|
17
|
+
/**
|
|
18
|
+
* Fetch the details of the accounts as of the highest slot that has reached this level of
|
|
19
|
+
* commitment.
|
|
20
|
+
*
|
|
21
|
+
* @defaultValue Whichever default is applied by the underlying {@link RpcApi} in use. For
|
|
22
|
+
* example, when using an API created by a `createSolanaRpc*()` helper, the default commitment
|
|
23
|
+
* is `"confirmed"` unless configured otherwise. Unmitigated by an API layer on the client, the
|
|
24
|
+
* default commitment applied by the server is `"finalized"`.
|
|
25
|
+
*/
|
|
26
|
+
commitment?: Commitment;
|
|
27
|
+
/**
|
|
28
|
+
* Determines how the accounts' data should be encoded in the response.
|
|
29
|
+
*
|
|
30
|
+
* - `'base58'` returns a tuple whose first element is the data as a base58-encoded string. If
|
|
31
|
+
* the account contains more than 129 bytes of data, an error will be raised.
|
|
32
|
+
* - `'base64'` returns a tuple whose first element is the data as a base64-encoded string.
|
|
33
|
+
* - `'base64+zstd'` returns a tuple whose first element is the
|
|
34
|
+
* [ZStandard](https://facebook.github.io/zstd/)-compressed data as a base64-encoded string.
|
|
35
|
+
* - `'jsonParsed'` will cause the server to attempt to process the data using a parser specific
|
|
36
|
+
* to each account's owning program. If successful, the parsed data will be returned in the
|
|
37
|
+
* response as JSON. Otherwise, the raw account data will be returned in the response as a
|
|
38
|
+
* tuple whose first element is a base64-encoded string.
|
|
39
|
+
*/
|
|
40
|
+
encoding?: 'base58' | 'base64' | 'base64+zstd' | 'jsonParsed';
|
|
41
|
+
/**
|
|
42
|
+
* Prevents accessing stale data by enforcing that the RPC node has processed transactions up to
|
|
43
|
+
* this slot
|
|
44
|
+
*/
|
|
45
|
+
minContextSlot?: Slot;
|
|
46
|
+
}>;
|
|
47
|
+
|
|
48
|
+
type GetMultipleAccountsApiSliceableCommonConfig = Readonly<{
|
|
49
|
+
/**
|
|
50
|
+
* Define which slice of the accounts' data you want the RPC to return.
|
|
51
|
+
*
|
|
52
|
+
* Use this to save network bandwidth and encoding time when you do not need the entire buffer.
|
|
53
|
+
*
|
|
54
|
+
* Data slicing is only available for `"base58"`, `"base64"`, and `"base64+zstd"` encodings.
|
|
55
|
+
*/
|
|
56
|
+
dataSlice?: DataSlice;
|
|
57
|
+
}>;
|
|
58
|
+
|
|
59
|
+
export type GetMultipleAccountsApi = {
|
|
60
|
+
/**
|
|
61
|
+
* Fetches information associated with the accounts at the given addresses.
|
|
62
|
+
*
|
|
63
|
+
* If the accounts have data, it will be returned in the response as a tuple whose first element
|
|
64
|
+
* is a base64-encoded string.
|
|
65
|
+
*
|
|
66
|
+
* @param addresses A maximum of 100 addresses for which to fetch account data
|
|
67
|
+
*
|
|
68
|
+
* {@label base64}
|
|
69
|
+
* @see https://solana.com/docs/rpc/http/getmultipleaccounts
|
|
70
|
+
*/
|
|
71
|
+
getMultipleAccounts(
|
|
72
|
+
addresses: Address[],
|
|
73
|
+
config: GetMultipleAccountsApiCommonConfig &
|
|
74
|
+
GetMultipleAccountsApiSliceableCommonConfig &
|
|
75
|
+
Readonly<{
|
|
76
|
+
encoding: 'base64';
|
|
77
|
+
}>,
|
|
78
|
+
): SolanaRpcResponse<(GetMultipleAccountsApiResponseBase & (AccountInfoWithBase64EncodedData | null))[]>;
|
|
79
|
+
/**
|
|
80
|
+
* Fetches information associated with the accounts at the given addresses.
|
|
81
|
+
*
|
|
82
|
+
* If the accounts have data, it will first be compressed using
|
|
83
|
+
* [ZStandard](https://facebook.github.io/zstd/) and the result will be returned in the response
|
|
84
|
+
* as a tuple whose first element is a base64-encoded string.
|
|
85
|
+
*
|
|
86
|
+
* @param addresses A maximum of 100 addresses for which to fetch account data
|
|
87
|
+
*
|
|
88
|
+
* {@label base64-zstd-compressed}
|
|
89
|
+
* @see https://solana.com/docs/rpc/http/getmultipleaccounts
|
|
90
|
+
*/
|
|
91
|
+
getMultipleAccounts(
|
|
92
|
+
addresses: Address[],
|
|
93
|
+
config: GetMultipleAccountsApiCommonConfig &
|
|
94
|
+
GetMultipleAccountsApiSliceableCommonConfig &
|
|
95
|
+
Readonly<{
|
|
96
|
+
encoding: 'base64+zstd';
|
|
97
|
+
}>,
|
|
98
|
+
): SolanaRpcResponse<
|
|
99
|
+
(GetMultipleAccountsApiResponseBase & (AccountInfoWithBase64EncodedZStdCompressedData | null))[]
|
|
100
|
+
>;
|
|
101
|
+
/**
|
|
102
|
+
* Fetches information associated with the accounts at the given addresses.
|
|
103
|
+
*
|
|
104
|
+
* If the accounts have data, the server will attempt to process it using a parser specific to
|
|
105
|
+
* each account's owning program. If successful, the parsed data will be returned in the
|
|
106
|
+
* response as JSON. Otherwise, the raw account data will be returned in the response as a tuple
|
|
107
|
+
* whose first element is a base64-encoded string.
|
|
108
|
+
*
|
|
109
|
+
* @param addresses A maximum of 100 addresses for which to fetch account data
|
|
110
|
+
*
|
|
111
|
+
* {@label parsed}
|
|
112
|
+
* @see https://solana.com/docs/rpc/http/getmultipleaccounts
|
|
113
|
+
*/
|
|
114
|
+
getMultipleAccounts(
|
|
115
|
+
addresses: Address[],
|
|
116
|
+
config: GetMultipleAccountsApiCommonConfig &
|
|
117
|
+
Readonly<{
|
|
118
|
+
encoding: 'jsonParsed';
|
|
119
|
+
}>,
|
|
120
|
+
): SolanaRpcResponse<(GetMultipleAccountsApiResponseBase & (AccountInfoWithJsonData | null))[]>;
|
|
121
|
+
/**
|
|
122
|
+
* Fetches information associated with the accounts at the given addresses.
|
|
123
|
+
*
|
|
124
|
+
* If the accounts have data, it will be returned in the response as a tuple whose first element
|
|
125
|
+
* is a base58-encoded string. If any account contains more than 129 bytes of data, this method
|
|
126
|
+
* will raise an error.
|
|
127
|
+
*
|
|
128
|
+
* @param addresses A maximum of 100 addresses for which to fetch account data
|
|
129
|
+
*
|
|
130
|
+
* {@label base58}
|
|
131
|
+
* @see https://solana.com/docs/rpc/http/getmultipleaccounts
|
|
132
|
+
*/
|
|
133
|
+
getMultipleAccounts(
|
|
134
|
+
addresses: Address[],
|
|
135
|
+
config: GetMultipleAccountsApiCommonConfig &
|
|
136
|
+
GetMultipleAccountsApiSliceableCommonConfig &
|
|
137
|
+
Readonly<{
|
|
138
|
+
encoding: 'base58';
|
|
139
|
+
}>,
|
|
140
|
+
): SolanaRpcResponse<(GetMultipleAccountsApiResponseBase & (AccountInfoWithBase58EncodedData | null))[]>;
|
|
141
|
+
/**
|
|
142
|
+
* Fetches information associated with the accounts at the given addresses.
|
|
143
|
+
*
|
|
144
|
+
* If the accounts have data, it will be returned in the response as a base58-encoded string. If
|
|
145
|
+
* any account contains more than 129 bytes of data, this method will raise an error.
|
|
146
|
+
*
|
|
147
|
+
* @param addresses A maximum of 100 addresses for which to fetch account data
|
|
148
|
+
*
|
|
149
|
+
* {@label base58-legacy}
|
|
150
|
+
* @see https://solana.com/docs/rpc/http/getmultipleaccounts
|
|
151
|
+
*/
|
|
152
|
+
getMultipleAccounts(
|
|
153
|
+
addresses: Address[],
|
|
154
|
+
config?: GetMultipleAccountsApiCommonConfig,
|
|
155
|
+
): SolanaRpcResponse<(GetMultipleAccountsApiResponseBase & (AccountInfoWithBase64EncodedData | null))[]>;
|
|
156
|
+
};
|