Skip to content
Merged
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
20 changes: 15 additions & 5 deletions packages/rpc-core/src/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
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);
const result = await transport.send<
Parameters<JsonRpcApi[TMethodName]> | undefined,
ReturnType<RpcCore[TMethodName]>
>(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