Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(experimental): reduce getProgramAccounts types using generics #1531

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 48 additions & 71 deletions packages/rpc-core/src/rpc-methods/getProgramAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,107 +34,84 @@ type GetProgramAccountsApiCommonConfig = Readonly<{
filters?: (GetProgramAccountsMemcmpFilter | GetProgramAccountsDatasizeFilter)[];
}>;

type GetProgramAccountsApiContextConfig = Readonly<{
withContext?: boolean;
}>;

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

type GetProgramAccountsApiResponseWithContextConfig<
TContextConfig extends GetProgramAccountsApiContextConfig | void,
TResponse,
> = TContextConfig extends object
? TContextConfig['withContext'] extends true
? RpcResponse<TResponse>
: TResponse
: TResponse;

export interface GetProgramAccountsApi {
/**
* Returns the account information for a list of Pubkeys.
*/
getProgramAccounts(
getProgramAccounts<TContextConfig extends GetProgramAccountsApiContextConfig>(
program: Base58EncodedAddress,
config: GetProgramAccountsApiCommonConfig &
GetProgramAccountsApiSliceableCommonConfig &
TContextConfig &
Readonly<{
encoding: 'base64';
withContext: true;
}>
): RpcResponse<AccountInfoWithPubkey<AccountInfoBase & AccountInfoWithBase64EncodedData>[]>;
}>,
): GetProgramAccountsApiResponseWithContextConfig<
TContextConfig,
AccountInfoWithPubkey<AccountInfoBase & AccountInfoWithBase64EncodedData>[]
>;

getProgramAccounts(
program: Base58EncodedAddress,
config: GetProgramAccountsApiCommonConfig &
GetProgramAccountsApiSliceableCommonConfig &
Readonly<{
encoding: 'base64';
withContext?: boolean;
}>
): AccountInfoWithPubkey<AccountInfoBase & AccountInfoWithBase64EncodedData>[];

getProgramAccounts(
program: Base58EncodedAddress,
config: GetProgramAccountsApiCommonConfig &
GetProgramAccountsApiSliceableCommonConfig &
Readonly<{
encoding: 'base64+zstd';
withContext: true;
}>
): RpcResponse<AccountInfoWithPubkey<AccountInfoBase & AccountInfoWithBase64EncodedZStdCompressedData>[]>;

getProgramAccounts(
getProgramAccounts<TContextConfig extends GetProgramAccountsApiContextConfig>(
program: Base58EncodedAddress,
config: GetProgramAccountsApiCommonConfig &
GetProgramAccountsApiSliceableCommonConfig &
TContextConfig &
Readonly<{
encoding: 'base64+zstd';
withContext?: boolean;
}>
): AccountInfoWithPubkey<AccountInfoBase & AccountInfoWithBase64EncodedZStdCompressedData>[];

getProgramAccounts(
program: Base58EncodedAddress,
config: GetProgramAccountsApiCommonConfig &
Readonly<{
encoding: 'jsonParsed';
withContext: true;
}>
): RpcResponse<AccountInfoWithPubkey<AccountInfoBase & AccountInfoWithJsonData>[]>;
}>,
): GetProgramAccountsApiResponseWithContextConfig<
TContextConfig,
AccountInfoWithPubkey<AccountInfoBase & AccountInfoWithBase64EncodedZStdCompressedData>[]
>;

getProgramAccounts(
getProgramAccounts<TContextConfig extends GetProgramAccountsApiContextConfig>(
program: Base58EncodedAddress,
config: GetProgramAccountsApiCommonConfig &
TContextConfig &
Readonly<{
encoding: 'jsonParsed';
withContext?: boolean;
}>
): AccountInfoWithPubkey<AccountInfoBase & AccountInfoWithJsonData>[];
}>,
): GetProgramAccountsApiResponseWithContextConfig<
TContextConfig,
AccountInfoWithPubkey<AccountInfoBase & AccountInfoWithJsonData>[]
>;

getProgramAccounts(
getProgramAccounts<TContextConfig extends GetProgramAccountsApiContextConfig>(
program: Base58EncodedAddress,
config: GetProgramAccountsApiCommonConfig &
GetProgramAccountsApiSliceableCommonConfig &
TContextConfig &
Readonly<{
encoding: 'base58';
withContext: true;
}>
): RpcResponse<AccountInfoWithPubkey<AccountInfoBase & AccountInfoWithBase58EncodedData>[]>;
}>,
): GetProgramAccountsApiResponseWithContextConfig<
TContextConfig,
AccountInfoWithPubkey<AccountInfoBase & AccountInfoWithBase58EncodedData>[]
>;

getProgramAccounts(
getProgramAccounts<TContextConfig extends GetProgramAccountsApiContextConfig>(
program: Base58EncodedAddress,
config: GetProgramAccountsApiCommonConfig &
GetProgramAccountsApiSliceableCommonConfig &
Readonly<{
encoding: 'base58';
withContext?: boolean;
}>
): AccountInfoWithPubkey<AccountInfoBase & AccountInfoWithBase58EncodedData>[];

getProgramAccounts(
program: Base58EncodedAddress,
config: GetProgramAccountsApiCommonConfig &
GetProgramAccountsApiSliceableCommonConfig &
Readonly<{
withContext: true;
}>
): RpcResponse<AccountInfoWithPubkey<AccountInfoBase & AccountInfoWithBase58Bytes>[]>;

getProgramAccounts(
program: Base58EncodedAddress,
config?: GetProgramAccountsApiCommonConfig &
GetProgramAccountsApiSliceableCommonConfig &
Readonly<{
withContext?: boolean;
}>
): AccountInfoWithPubkey<AccountInfoBase & AccountInfoWithBase58Bytes>[];
config?: GetProgramAccountsApiCommonConfig & GetProgramAccountsApiSliceableCommonConfig & TContextConfig,
): GetProgramAccountsApiResponseWithContextConfig<
TContextConfig,
AccountInfoWithPubkey<AccountInfoBase & AccountInfoWithBase58Bytes>[]
>;
}
109 changes: 27 additions & 82 deletions packages/rpc-transport/src/json-rpc-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { IRpcTransport } from './transports/transport-types';
*/
export type IRpcApi<TRpcMethods> = {
[MethodName in keyof TRpcMethods]: TRpcMethods[MethodName] extends Callable
? (...rawParams: unknown[]) => RpcRequest<ReturnType<TRpcMethods[MethodName]>>
: never;
? (...rawParams: Parameters<TRpcMethods[MethodName]>) => RpcRequest<ReturnType<TRpcMethods[MethodName]>>
: never;
};
export type Rpc<TRpcMethods> = RpcMethods<TRpcMethods>;
export type RpcConfig<TRpcMethods> = Readonly<{
Expand Down Expand Up @@ -54,93 +54,38 @@ type PendingRpcRequestBuilder<TMethodImplementations> = UnionToIntersection<
type Callable = (...args: any[]) => any;
type Flatten<T> = T extends (infer Item)[] ? Item : never;
type Overloads<T> =
// Have an RPC method with more than 10 overloads? Add another section and update this comment
// Have an RPC method with more than 5 overloads? Add another section and update this comment
T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
(...args: infer A5): infer R5;
(...args: infer A6): infer R6;
(...args: infer A7): infer R7;
(...args: infer A8): infer R8;
(...args: infer A9): infer R9;
(...args: infer A10): infer R10;
}
? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3, (...args: A4) => R4, (...args: A5) => R5, (...args: A6) => R6, (...args: A7) => R7, (...args: A8) => R8, (...args: A9) => R9, (...args: A10) => R10]
: T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
(...args: infer A5): infer R5;
(...args: infer A6): infer R6;
(...args: infer A7): infer R7;
(...args: infer A8): infer R8;
(...args: infer A9): infer R9;
}
? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3, (...args: A4) => R4, (...args: A5) => R5, (...args: A6) => R6, (...args: A7) => R7, (...args: A8) => R8, (...args: A9) => R9]
: T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
(...args: infer A5): infer R5;
(...args: infer A6): infer R6;
(...args: infer A7): infer R7;
(...args: infer A8): infer R8;
}
? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3, (...args: A4) => R4, (...args: A5) => R5, (...args: A6) => R6, (...args: A7) => R7, (...args: A8) => R8]
: T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
(...args: infer A5): infer R5;
(...args: infer A6): infer R6;
(...args: infer A7): infer R7;
}
? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3, (...args: A4) => R4, (...args: A5) => R5, (...args: A6) => R6, (...args: A7) => R7]
: T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
(...args: infer A5): infer R5;
(...args: infer A6): infer R6;
}
? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3, (...args: A4) => R4, (...args: A5) => R5, (...args: A6) => R6]
: T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
(...args: infer A5): infer R5;
}
? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3, (...args: A4) => R4, (...args: A5) => R5]
: T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
}
? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3, (...args: A4) => R4]
: T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
}
? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3]
: T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
}
? [(...args: A1) => R1, (...args: A2) => R2]
: T extends {
(...args: infer A1): infer R1;
}
? [(...args: A1) => R1]
: unknown;
? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3, (...args: A4) => R4, (...args: A5) => R5]
: T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4;
}
? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3, (...args: A4) => R4]
: T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
}
? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3]
: T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
}
? [(...args: A1) => R1, (...args: A2) => R2]
: T extends {
(...args: infer A1): infer R1;
}
? [(...args: A1) => R1]
: unknown;
type UnionToIntersection<T> = (T extends unknown ? (x: T) => unknown : never) extends (x: infer R) => unknown
? R
: never;
10 changes: 9 additions & 1 deletion packages/rpc-transport/src/json-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { SolanaJsonRpcError } from './json-rpc-errors';
import { createJsonRpcMessage } from './json-rpc-message';
import { PendingRpcRequest, Rpc, RpcConfig, RpcRequest, SendOptions } from './json-rpc-types';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Callable = (...args: any[]) => any;
interface IHasIdentifier {
readonly id: number;
}
Expand Down Expand Up @@ -38,7 +40,13 @@ function makeProxy<TRpcMethods>(rpcConfig: RpcConfig<TRpcMethods>): Rpc<TRpcMeth
return false;
},
get(target, p, receiver) {
return function (...rawParams: unknown[]) {
return function (
...rawParams: typeof p extends keyof TRpcMethods
? TRpcMethods[typeof p] extends Callable
? Parameters<TRpcMethods[typeof p]>
: never
: never
) {
const methodName = p.toString();
const createRpcRequest = Reflect.get(target, methodName, receiver);
const newRequest = createRpcRequest
Expand Down