Skip to content

Commit 1a51843

Browse files
authored
[experimental] Remove the concept of the transport from the types of the RPC (#1196)
# Summary @joncinque pointed out in #1190 that the Typescript types of `@solana/rpc-core` relied on a cast through `unknown` to work. The more I thought about it the more I realized that the `transport` shouldn't form part of the typespec for the RPC. In this PR we remove it from the source types, and map it back in where it's needed in the implementation..
1 parent ab87a55 commit 1a51843

File tree

5 files changed

+19
-19
lines changed

5 files changed

+19
-19
lines changed

packages/rpc-core/src/rpc.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
import { IJsonRpcTransport } from '@solana/rpc-transport';
22
import { JsonRpcApi } from './types/jsonRpcApi';
33

4-
export const rpc = /* #__PURE__ */ new Proxy<JsonRpcApi>({} as JsonRpcApi, {
4+
type RpcCore = {
5+
[TMethodName in keyof JsonRpcApi]: (
6+
transport: IJsonRpcTransport,
7+
...params: Parameters<JsonRpcApi[TMethodName]>
8+
) => ReturnType<JsonRpcApi[TMethodName]>;
9+
};
10+
11+
export const rpc = /* #__PURE__ */ new Proxy<RpcCore>({} as RpcCore, {
512
defineProperty() {
613
return false;
714
},
815
deleteProperty() {
916
return false;
1017
},
11-
get<TMethodName extends keyof JsonRpcApi>(target: JsonRpcApi, p: TMethodName) {
18+
get<TMethodName extends keyof JsonRpcApi>(target: RpcCore, p: TMethodName) {
1219
if (target[p] == null) {
1320
const method = p.toString();
14-
target[p] = async function (transport: IJsonRpcTransport, ...params: Parameters<JsonRpcApi[TMethodName]>) {
21+
target[p] = async function (transport, ...params) {
1522
const normalizedParams = params.length ? params : undefined;
16-
const result = await transport.send(method, normalizedParams);
23+
const result = await transport.send<
24+
Parameters<JsonRpcApi[TMethodName]> | undefined,
25+
ReturnType<RpcCore[TMethodName]>
26+
>(method, normalizedParams);
1727
return result;
18-
} as unknown as JsonRpcApi[TMethodName];
28+
} as RpcCore[TMethodName];
1929
}
2030
return target[p];
2131
},

packages/rpc-core/src/types/jsonRpcApi.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import { GetAccountInfoApi } from './rpc-methods/getAccountInfo';
22
import { GetBlockHeightApi } from './rpc-methods/getBlockHeight';
33
import { GetBlocksApi } from './rpc-methods/getBlocks';
44

5-
declare interface JsonRpcApi extends GetAccountInfoApi, GetBlockHeightApi, GetBlocksApi {}
5+
export interface JsonRpcApi extends GetAccountInfoApi, GetBlockHeightApi, GetBlocksApi {}

packages/rpc-core/src/types/rpc-methods/getAccountInfo.d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Base58EncodedAddress } from '@solana/keys';
2-
import { IJsonRPCTransport } from '../../rpc';
32

43
type Base64EncodedBytes = string & { readonly __base64EncodedBytes: unique symbol };
54
type Base64EncodedZStdCompressedBytes = string & { readonly __base64EncodedZStdCompressedBytes: unique symbol };
@@ -59,12 +58,11 @@ type GetAccountInfoApiBase64EncodingCommonConfig = readonly {
5958
dataSlice?: DataSlice;
6059
};
6160

62-
declare interface GetAccountInfoApi {
61+
export interface GetAccountInfoApi {
6362
/**
6463
* Returns all information associated with the account of provided public key
6564
*/
6665
getAccountInfo(
67-
transport: IJsonRPCTransport,
6866
address: Base58EncodedAddress,
6967
config?: readonly {
7068
encoding: 'base64';
@@ -73,7 +71,6 @@ declare interface GetAccountInfoApi {
7371
GetAccountInfoApiBase64EncodingCommonConfig
7472
): Promise<GetAccountInfoApiResponseBase & GetAccountInfoApiResponseWithEncodedData>;
7573
getAccountInfo(
76-
transport: IJsonRPCTransport,
7774
address: Base58EncodedAddress,
7875
config?: readonly {
7976
encoding: 'base64+zstd';
@@ -82,7 +79,6 @@ declare interface GetAccountInfoApi {
8279
GetAccountInfoApiBase64EncodingCommonConfig
8380
): Promise<GetAccountInfoApiResponseBase & GetAccountInfoApiResponseWithEncodedZStdCompressedData>;
8481
getAccountInfo(
85-
transport: IJsonRPCTransport,
8682
address: Base58EncodedAddress,
8783
config?: readonly {
8884
encoding: 'jsonParsed';

packages/rpc-core/src/types/rpc-methods/getBlockHeight.d.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { IJsonRpcTransport } from '@solana/rpc-transport';
2-
31
type GetBlockHeightApiResponse =
42
// TODO(solana-labs/solana/issues/30341) Represent as bigint
53
number;
64

7-
declare interface GetBlockHeightApi {
5+
export interface GetBlockHeightApi {
86
/**
97
* Returns the current block height of the node
108
*/
119
getBlockHeight(
12-
transport: IJsonRpcTransport,
1310
config?: readonly {
1411
// Defaults to `finalized`
1512
commitment?: Commitment;

packages/rpc-core/src/types/rpc-methods/getBlocks.d.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import { IJsonRpcTransport } from '@solana/rpc-transport';
2-
31
type GetBlocksApiResponse = Slot[];
42

5-
declare interface GetBlocksApi {
3+
export interface GetBlocksApi {
64
/**
75
* Returns a list of confirmed blocks between two slots
86
*/
97
getBlocks(
10-
transport: IJsonRpcTransport,
118
startSlot: Slot,
129
endSlotInclusive?: Slot,
1310
config?: readonly {

0 commit comments

Comments
 (0)