Skip to content

Commit

Permalink
[experimental] Remove the concept of the transport from the types of …
Browse files Browse the repository at this point in the history
…the RPC

# 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..
  • Loading branch information
steveluscher committed Mar 11, 2023
1 parent ab87a55 commit 0734ca9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 18 deletions.
15 changes: 11 additions & 4 deletions packages/rpc-core/src/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import { IJsonRpcTransport } from '@solana/rpc-transport';
import { JsonRpcApi } from './types/jsonRpcApi';

export const rpc = /* #__PURE__ */ new Proxy<JsonRpcApi>({} as JsonRpcApi, {
type RpcCore = {
[TMethodName in keyof JsonRpcApi]: (
transport: IJsonRpcTransport,
...params: Parameters<JsonRpcApi[TMethodName]>
) => ReturnType<JsonRpcApi[TMethodName]>;
};

export const rpc = /* #__PURE__ */ new Proxy<RpcCore>({} as RpcCore, {
defineProperty() {
return false;
},
deleteProperty() {
return false;
},
get<TMethodName extends keyof JsonRpcApi>(target: JsonRpcApi, p: TMethodName) {
get<TMethodName extends keyof JsonRpcApi>(target: RpcCore, p: TMethodName) {
if (target[p] == null) {
const method = p.toString();
target[p] = async function (transport: IJsonRpcTransport, ...params: Parameters<JsonRpcApi[TMethodName]>) {
target[p] = async function (transport, ...params) {
const normalizedParams = params.length ? params : undefined;
const result = await transport.send(method, normalizedParams);
return result;
} as unknown as JsonRpcApi[TMethodName];
} as RpcCore[TMethodName];
}
return target[p];
},
Expand Down
2 changes: 1 addition & 1 deletion packages/rpc-core/src/types/jsonRpcApi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import { GetAccountInfoApi } from './rpc-methods/getAccountInfo';
import { GetBlockHeightApi } from './rpc-methods/getBlockHeight';
import { GetBlocksApi } from './rpc-methods/getBlocks';

declare interface JsonRpcApi extends GetAccountInfoApi, GetBlockHeightApi, GetBlocksApi {}
export interface JsonRpcApi extends GetAccountInfoApi, GetBlockHeightApi, GetBlocksApi {}
6 changes: 1 addition & 5 deletions packages/rpc-core/src/types/rpc-methods/getAccountInfo.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Base58EncodedAddress } from '@solana/keys';
import { IJsonRPCTransport } from '../../rpc';

type Base64EncodedBytes = string & { readonly __base64EncodedBytes: unique symbol };
type Base64EncodedZStdCompressedBytes = string & { readonly __base64EncodedZStdCompressedBytes: unique symbol };
Expand Down Expand Up @@ -59,12 +58,11 @@ type GetAccountInfoApiBase64EncodingCommonConfig = readonly {
dataSlice?: DataSlice;
};

declare interface GetAccountInfoApi {
export interface GetAccountInfoApi {
/**
* Returns all information associated with the account of provided public key
*/
getAccountInfo(
transport: IJsonRPCTransport,
address: Base58EncodedAddress,
config?: readonly {
encoding: 'base64';
Expand All @@ -73,7 +71,6 @@ declare interface GetAccountInfoApi {
GetAccountInfoApiBase64EncodingCommonConfig
): Promise<GetAccountInfoApiResponseBase & GetAccountInfoApiResponseWithEncodedData>;
getAccountInfo(
transport: IJsonRPCTransport,
address: Base58EncodedAddress,
config?: readonly {
encoding: 'base64+zstd';
Expand All @@ -82,7 +79,6 @@ declare interface GetAccountInfoApi {
GetAccountInfoApiBase64EncodingCommonConfig
): Promise<GetAccountInfoApiResponseBase & GetAccountInfoApiResponseWithEncodedZStdCompressedData>;
getAccountInfo(
transport: IJsonRPCTransport,
address: Base58EncodedAddress,
config?: readonly {
encoding: 'jsonParsed';
Expand Down
5 changes: 1 addition & 4 deletions packages/rpc-core/src/types/rpc-methods/getBlockHeight.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { IJsonRpcTransport } from '@solana/rpc-transport';

type GetBlockHeightApiResponse =
// TODO(solana-labs/solana/issues/30341) Represent as bigint
number;

declare interface GetBlockHeightApi {
export interface GetBlockHeightApi {
/**
* Returns the current block height of the node
*/
getBlockHeight(
transport: IJsonRpcTransport,
config?: readonly {
// Defaults to `finalized`
commitment?: Commitment;
Expand Down
5 changes: 1 addition & 4 deletions packages/rpc-core/src/types/rpc-methods/getBlocks.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { IJsonRpcTransport } from '@solana/rpc-transport';

type GetBlocksApiResponse = Slot[];

declare interface GetBlocksApi {
export interface GetBlocksApi {
/**
* Returns a list of confirmed blocks between two slots
*/
getBlocks(
transport: IJsonRpcTransport,
startSlot: Slot,
endSlotInclusive?: Slot,
config?: readonly {
Expand Down

0 comments on commit 0734ca9

Please sign in to comment.