Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
refactor(experimental): add cluster level API for rpc-core
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Jan 21, 2024
1 parent 8b5554c commit 4d08ea7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
29 changes: 29 additions & 0 deletions packages/rpc-core/src/rpc-methods/__typetests__/api-type-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { IRpcApi, IRpcApiDevnet, IRpcApiMainnet, IRpcApiTestnet } from '@solana/rpc-transport';

import { createSolanaRpcApi, SolanaRpcMethods } from '..';

const config = null as unknown as Parameters<typeof createSolanaRpcApi>[0];

// No cluster specified should be generic `IRpcApi`
createSolanaRpcApi(config) satisfies IRpcApi<SolanaRpcMethods>;

// When devnet is specified, should be `IRpcApiDevnet`
createSolanaRpcApi<'devnet'>(config) satisfies IRpcApiDevnet<SolanaRpcMethods>;
// @ts-expect-error Should not be a testnet API
createSolanaRpcApi<'devnet'>(config) satisfies IRpcApiTestnet<SolanaRpcMethods>;
// @ts-expect-error Should not be a mainnet API
createSolanaRpcApi<'devnet'>(config) satisfies IRpcApiMainnet<SolanaRpcMethods>;

// When testnet is specified, should be `IRpcApiTestnet`
createSolanaRpcApi<'testnet'>(config) satisfies IRpcApiTestnet<SolanaRpcMethods>;
// @ts-expect-error Should not be a devnet API
createSolanaRpcApi<'testnet'>(config) satisfies IRpcApiDevnet<SolanaRpcMethods>;
// @ts-expect-error Should not be a mainnet API
createSolanaRpcApi<'testnet'>(config) satisfies IRpcApiMainnet<SolanaRpcMethods>;

// When mainnet is specified, should be `IRpcApiMainnet`
createSolanaRpcApi<'mainnet'>(config) satisfies IRpcApiMainnet<Omit<SolanaRpcMethods, 'requestAirdrop'>>;
// @ts-expect-error Should not be a devnet API
createSolanaRpcApi<'mainnet'>(config) satisfies IRpcApiDevnet<Omit<SolanaRpcMethods, 'requestAirdrop'>>;
// @ts-expect-error Should not be a testnet API
createSolanaRpcApi<'mainnet'>(config) satisfies IRpcApiTestnet<Omit<SolanaRpcMethods, 'requestAirdrop'>>;
22 changes: 18 additions & 4 deletions packages/rpc-core/src/rpc-methods/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createJsonRpcApi } from '@solana/rpc-transport';
import { createJsonRpcApi, IRpcApiDevnet, IRpcApiMainnet, IRpcApiTestnet } from '@solana/rpc-transport';
import { IRpcApi } from '@solana/rpc-transport';

import { getParamsPatcherForSolanaLabsRpc, ParamsPatcherConfig } from '../params-patcher';
Expand Down Expand Up @@ -113,11 +113,25 @@ export type SolanaRpcMethods = GetAccountInfoApi &
SendTransactionApi &
SimulateTransactionApi;

export function createSolanaRpcApi(config?: Config): IRpcApi<SolanaRpcMethods> {
return createJsonRpcApi<SolanaRpcMethods>({
export function createSolanaRpcApi<TCluster extends 'devnet' | 'testnet' | 'mainnet' | void = void>(
config?: Config,
): TCluster extends 'devnet'
? IRpcApiDevnet<SolanaRpcMethods>
: TCluster extends 'testnet'
? IRpcApiTestnet<SolanaRpcMethods>
: TCluster extends 'mainnet'
? IRpcApiMainnet<Omit<SolanaRpcMethods, 'requestAirdrop'>>
: IRpcApi<SolanaRpcMethods> {
return createJsonRpcApi<SolanaRpcMethods, TCluster>({
parametersTransformer: getParamsPatcherForSolanaLabsRpc(config) as (params: unknown[]) => unknown[],
responseTransformer: patchResponseForSolanaLabsRpc,
});
}) as TCluster extends 'devnet'
? IRpcApiDevnet<SolanaRpcMethods>
: TCluster extends 'testnet'
? IRpcApiTestnet<SolanaRpcMethods>
: TCluster extends 'mainnet'
? IRpcApiMainnet<Omit<SolanaRpcMethods, 'requestAirdrop'>>
: IRpcApi<SolanaRpcMethods>;
}

export type {
Expand Down

0 comments on commit 4d08ea7

Please sign in to comment.