Skip to content

Commit

Permalink
refactor(experimental): decouple @solana/accounts from @solana/rpc-co…
Browse files Browse the repository at this point in the history
…re (#2051)

This PR removes the `@solana/rpc-core` dependency from `@solana/accounts` which avoids ending up with unused nested dependencies such as `@solana/transactions` when we only need the `GetAccountInfoApi` and `GetMultipleAccountsApi` types.

It does this by simply duplicating the types locally in the `@solana/accounts` package.
  • Loading branch information
lorisleiva authored Jan 24, 2024
1 parent 098806e commit 35de86b
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 23 deletions.
1 change: 0 additions & 1 deletion packages/accounts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
"@solana/addresses": "workspace:*",
"@solana/codecs-core": "workspace:*",
"@solana/codecs-strings": "workspace:*",
"@solana/rpc-core": "workspace:*",
"@solana/rpc-types": "workspace:*"
},
"devDependencies": {
Expand Down
16 changes: 7 additions & 9 deletions packages/accounts/src/__tests__/__setup__.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import type { Address } from '@solana/addresses';
import type { Decoder } from '@solana/codecs-core';
import type { GetAccountInfoApi, GetMultipleAccountsApi } from '@solana/rpc-core';
import type { PendingRpcRequest, Rpc, RpcResponse } from '@solana/rpc-types';

import type {
AccountInfoBase,
AccountInfoWithBase58Bytes,
AccountInfoWithBase58EncodedData,
AccountInfoWithBase64EncodedData,
} from '@solana/rpc-core/dist/types/rpc-methods/common';
import type { PendingRpcRequest, Rpc, RpcResponse, U64UnsafeBeyond2Pow53Minus1 } from '@solana/rpc-types';
GetAccountInfoApi,
GetMultipleAccountsApi,
JsonParsedDataResponse,
} from '../rpc-api';

export type Base64RpcAccount = AccountInfoBase & AccountInfoWithBase64EncodedData;
export type Base58RpcAccount = AccountInfoBase & (AccountInfoWithBase58Bytes | AccountInfoWithBase58EncodedData);
export type JsonParsedRpcAccount = AccountInfoBase & Readonly<{ data: JsonParsedData<unknown> }>;
export type JsonParsedData<TData> = Readonly<{
program: string;
parsed: { info: TData; type: string };
space: U64UnsafeBeyond2Pow53Minus1;
}>;
export type JsonParsedRpcAccount = AccountInfoBase & { readonly data: JsonParsedDataResponse<unknown> };

export function getMockRpc(
accounts: Record<Address, Base64RpcAccount | JsonParsedRpcAccount>,
Expand Down
6 changes: 3 additions & 3 deletions packages/accounts/src/__typetests__/fetch-account-typetest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Address } from '@solana/addresses';
import type { GetAccountInfoApi, GetMultipleAccountsApi } from '@solana/rpc-core';
import type { Address } from '@solana/addresses';
import type { Rpc } from '@solana/rpc-types';

import {
Expand All @@ -8,7 +7,8 @@ import {
fetchJsonParsedAccount,
fetchJsonParsedAccounts,
} from '../fetch-account';
import { MaybeAccount, MaybeEncodedAccount } from '../maybe-account';
import type { MaybeAccount, MaybeEncodedAccount } from '../maybe-account';
import type { GetAccountInfoApi, GetMultipleAccountsApi } from '../rpc-api';

const rpc = {} as Rpc<GetAccountInfoApi & GetMultipleAccountsApi>;
const address = '1111' as Address<'1111'>;
Expand Down
2 changes: 1 addition & 1 deletion packages/accounts/src/fetch-account.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Address } from '@solana/addresses';
import type { GetAccountInfoApi, GetMultipleAccountsApi } from '@solana/rpc-core';
import type { Commitment, Rpc, Slot } from '@solana/rpc-types';

import type { MaybeAccount, MaybeEncodedAccount } from './maybe-account';
import { parseBase64RpcAccount, parseJsonRpcAccount } from './parse-account';
import type { GetAccountInfoApi, GetMultipleAccountsApi } from './rpc-api';

/** Optional configuration for fetching a singular account. */
export type FetchAccountConfig = {
Expand Down
12 changes: 6 additions & 6 deletions packages/accounts/src/parse-account.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { Address } from '@solana/addresses';
import { getBase58Encoder, getBase64Encoder } from '@solana/codecs-strings';

import type { Account, BaseAccount, EncodedAccount } from './account';
import { MaybeAccount, MaybeEncodedAccount } from './maybe-account';
import type {
AccountInfoBase,
AccountInfoWithBase58Bytes,
AccountInfoWithBase58EncodedData,
AccountInfoWithBase64EncodedData,
} from '@solana/rpc-core/dist/types/rpc-methods/common';

import type { Account, BaseAccount, EncodedAccount } from './account';
import { MaybeAccount, MaybeEncodedAccount } from './maybe-account';
JsonParsedDataResponse,
} from './rpc-api';

type Base64EncodedRpcAccount = AccountInfoBase & AccountInfoWithBase64EncodedData;

Expand Down Expand Up @@ -50,8 +51,7 @@ export function parseBase58RpcAccount<TAddress extends string = string>(
return Object.freeze({ ...parseBaseAccount(rpcAccount), address, data, exists: true });
}

type JsonParsedRpcAccount = AccountInfoBase & { readonly data: JsonParsedData<unknown> };
type JsonParsedData<TData> = { readonly parsed: { readonly info: TData } };
type JsonParsedRpcAccount = AccountInfoBase & { readonly data: JsonParsedDataResponse<unknown> };

/** Parse an account object received from a json-parsed RPC call into an Account or MaybeAccount type. */
export function parseJsonRpcAccount<TData extends object, TAddress extends string = string>(
Expand Down
61 changes: 61 additions & 0 deletions packages/accounts/src/rpc-api/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Address } from '@solana/addresses';
import type {
Base58EncodedBytes,
Base58EncodedDataResponse,
Base64EncodedDataResponse,
Base64EncodedZStdCompressedDataResponse,
LamportsUnsafeBeyond2Pow53Minus1,
U64UnsafeBeyond2Pow53Minus1,
} from '@solana/rpc-types';

export type DataSlice = Readonly<{
offset: number;
length: number;
}>;

export type AccountInfoBase = Readonly<{
/** indicates if the account contains a program (and is strictly read-only) */
executable: boolean;
/** number of lamports assigned to this account */
lamports: LamportsUnsafeBeyond2Pow53Minus1;
/** pubkey of the program this account has been assigned to */
owner: Address;
/** the epoch at which this account will next owe rent */
rentEpoch: U64UnsafeBeyond2Pow53Minus1;
}>;

/** @deprecated */
export type AccountInfoWithBase58Bytes = Readonly<{
data: Base58EncodedBytes;
}>;

/** @deprecated */
export type AccountInfoWithBase58EncodedData = Readonly<{
data: Base58EncodedDataResponse;
}>;

export type AccountInfoWithBase64EncodedData = Readonly<{
data: Base64EncodedDataResponse;
}>;

export type AccountInfoWithBase64EncodedZStdCompressedData = Readonly<{
data: Base64EncodedZStdCompressedDataResponse;
}>;

export type AccountInfoWithJsonData = Readonly<{
data:
| JsonParsedDataResponse
// If `jsonParsed` encoding is requested but a parser cannot be found for the given
// account the `data` field falls back to `base64`.
| Base64EncodedDataResponse;
}>;

export type JsonParsedDataResponse<TData = object> = Readonly<{
// Name of the program that owns this account.
program: string;
parsed: {
info?: TData;
type: string;
};
space: U64UnsafeBeyond2Pow53Minus1;
}>;
71 changes: 71 additions & 0 deletions packages/accounts/src/rpc-api/getAccountInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import type { Address } from '@solana/addresses';
import type { Commitment, IRpcApiMethods, RpcResponse, Slot } from '@solana/rpc-types';

import {
AccountInfoBase,
AccountInfoWithBase58Bytes,
AccountInfoWithBase58EncodedData,
AccountInfoWithBase64EncodedData,
AccountInfoWithBase64EncodedZStdCompressedData,
AccountInfoWithJsonData,
DataSlice,
} from './common';

type GetAccountInfoApiResponseBase = RpcResponse<AccountInfoBase | null>;

type NestInRpcResponseOrNull<T> = Readonly<{
value: T | null;
}>;

type GetAccountInfoApiCommonConfig = Readonly<{
// Defaults to `finalized`
commitment?: Commitment;
// The minimum slot that the request can be evaluated at
minContextSlot?: Slot;
}>;

type GetAccountInfoApiSliceableCommonConfig = Readonly<{
// Limit the returned account data using the provided "offset: <usize>" and "length: <usize>" fields.
dataSlice?: DataSlice;
}>;

export interface GetAccountInfoApi extends IRpcApiMethods {
/**
* Returns all information associated with the account of provided public key
*/
getAccountInfo(
address: Address,
config: GetAccountInfoApiCommonConfig &
GetAccountInfoApiSliceableCommonConfig &
Readonly<{
encoding: 'base64';
}>,
): GetAccountInfoApiResponseBase & NestInRpcResponseOrNull<AccountInfoWithBase64EncodedData>;
getAccountInfo(
address: Address,
config: GetAccountInfoApiCommonConfig &
GetAccountInfoApiSliceableCommonConfig &
Readonly<{
encoding: 'base64+zstd';
}>,
): GetAccountInfoApiResponseBase & NestInRpcResponseOrNull<AccountInfoWithBase64EncodedZStdCompressedData>;
getAccountInfo(
address: Address,
config: GetAccountInfoApiCommonConfig &
Readonly<{
encoding: 'jsonParsed';
}>,
): GetAccountInfoApiResponseBase & NestInRpcResponseOrNull<AccountInfoWithJsonData>;
getAccountInfo(
address: Address,
config: GetAccountInfoApiCommonConfig &
GetAccountInfoApiSliceableCommonConfig &
Readonly<{
encoding: 'base58';
}>,
): GetAccountInfoApiResponseBase & NestInRpcResponseOrNull<AccountInfoWithBase58EncodedData>;
getAccountInfo(
address: Address,
config?: GetAccountInfoApiCommonConfig,
): GetAccountInfoApiResponseBase & NestInRpcResponseOrNull<AccountInfoWithBase58Bytes>;
}
71 changes: 71 additions & 0 deletions packages/accounts/src/rpc-api/getMultipleAccounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import type { Address } from '@solana/addresses';
import type { Commitment, IRpcApiMethods, RpcResponse, Slot } from '@solana/rpc-types';

import {
AccountInfoBase,
AccountInfoWithBase58EncodedData,
AccountInfoWithBase64EncodedData,
AccountInfoWithBase64EncodedZStdCompressedData,
AccountInfoWithJsonData,
DataSlice,
} from './common';

type GetMultipleAccountsApiResponseBase = AccountInfoBase | null;

type GetMultipleAccountsApiCommonConfig = Readonly<{
/** Defaults to `finalized` */
commitment?: Commitment;
/** The minimum slot that the request can be evaluated at */
minContextSlot?: Slot;
}>;

type GetMultipleAccountsApiSliceableCommonConfig = Readonly<{
/** Limit the returned account data */
dataSlice?: DataSlice;
}>;

export interface GetMultipleAccountsApi extends IRpcApiMethods {
/**
* Returns the account information for a list of Pubkeys.
*/
getMultipleAccounts(
/** An array of up to 100 Pubkeys to query */
addresses: Address[],
config: GetMultipleAccountsApiCommonConfig &
GetMultipleAccountsApiSliceableCommonConfig &
Readonly<{
encoding: 'base64';
}>,
): RpcResponse<(GetMultipleAccountsApiResponseBase & (AccountInfoWithBase64EncodedData | null))[]>;
getMultipleAccounts(
/** An array of up to 100 Pubkeys to query */
addresses: Address[],
config: GetMultipleAccountsApiCommonConfig &
GetMultipleAccountsApiSliceableCommonConfig &
Readonly<{
encoding: 'base64+zstd';
}>,
): RpcResponse<(GetMultipleAccountsApiResponseBase & (AccountInfoWithBase64EncodedZStdCompressedData | null))[]>;
getMultipleAccounts(
/** An array of up to 100 Pubkeys to query */
addresses: Address[],
config: GetMultipleAccountsApiCommonConfig &
Readonly<{
encoding: 'jsonParsed';
}>,
): RpcResponse<(GetMultipleAccountsApiResponseBase & (AccountInfoWithJsonData | null))[]>;
getMultipleAccounts(
/** An array of up to 100 Pubkeys to query */
addresses: Address[],
config: GetMultipleAccountsApiCommonConfig &
GetMultipleAccountsApiSliceableCommonConfig &
Readonly<{
encoding: 'base58';
}>,
): RpcResponse<(GetMultipleAccountsApiResponseBase & (AccountInfoWithBase58EncodedData | null))[]>;
getMultipleAccounts(
/** An array of up to 100 Pubkeys to query */
addresses: Address[],
config?: GetMultipleAccountsApiCommonConfig,
): RpcResponse<(GetMultipleAccountsApiResponseBase & (AccountInfoWithBase64EncodedData | null))[]>;
}
3 changes: 3 additions & 0 deletions packages/accounts/src/rpc-api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './common';
export * from './getAccountInfo';
export * from './getMultipleAccounts';
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 35de86b

Please sign in to comment.