Skip to content

Commit

Permalink
fix: make rpc client correctly infer response type
Browse files Browse the repository at this point in the history
  • Loading branch information
roushou committed Jun 20, 2024
1 parent 36fde94 commit c2e80d3
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 97 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-students-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@coinbase-platform/onchain": patch
---

fix: make rpc client correctly infer response type
26 changes: 7 additions & 19 deletions packages/onchain/src/address/list-address-transactions.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
import type { RpcClient, RpcRequestConfig } from "../rpc";

type AddressTransaction = {
name: string;
hash: string;
blockHash: string;
blockHeight: string;
// TODO: can be narrowed to a union
status: string;
};

export type ListAddressTransactionsResponse = {
id: number;
jsonrpc: string;
result: {
addressTransactions: AddressTransaction[];
nextPageToken?: string;
};
};

export type ListAddressTransactionsParameters = Extract<
RpcRequestConfig,
{ method: "cdp_listAddressTransactions" }
>["parameters"];

export type ListAddressTransactionsResponse = Extract<
RpcRequestConfig,
{ method: "cdp_listAddressTransactions" }
>["response"]["result"];

/**
* @returns The list of transactions of specified address.
*
Expand All @@ -46,8 +33,9 @@ export async function listAddressTransactions(
rpc: RpcClient,
parameters: ListAddressTransactionsParameters,
): Promise<ListAddressTransactionsResponse> {
return await rpc.request({
const response = await rpc.request({
method: "cdp_listAddressTransactions",
parameters,
});
return response.result;
}
35 changes: 7 additions & 28 deletions packages/onchain/src/balance/list-balance-details.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,15 @@
import type { RpcClient, RpcRequestConfig } from "../rpc";

type BalanceDetails = {
asset: {
id: string;
// TODO: Add all token types
type:
| "native"
| "erc20"
| "erc721"
| "erc1155"
| "creditAlphanum4"
| "fa2"
| (string & {});
groupId: string;
subGroupId: string;
};
value: number;
};

export type ListBalanceDetailsResponse = {
id: number;
jsonrpc: string;
result: {
balances: BalanceDetails[];
nextPageToken?: string;
};
};

export type ListBalanceDetailsParameters = Extract<
RpcRequestConfig,
{ method: "cdp_listBalanceDetails" }
>["parameters"];

export type ListBalanceDetailsResponse = Extract<
RpcRequestConfig,
{ method: "cdp_listBalanceDetails" }
>["response"]["result"];

/**
* @returns The list of balance details of assets specified in parameters.
*
Expand All @@ -56,8 +34,9 @@ export async function listBalanceDetails(
rpc: RpcClient,
parameters: ListBalanceDetailsParameters,
): Promise<ListBalanceDetailsResponse> {
return await rpc.request({
const response = await rpc.request({
method: "cdp_listBalanceDetails",
parameters,
});
return response.result;
}
23 changes: 7 additions & 16 deletions packages/onchain/src/balance/list-balance-histories.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
import type { RpcClient, RpcRequestConfig } from "../rpc";

type BalanceHistory = {
blockHeight: number;
blockHash: string;
value: number;
};

export type ListBalanceHistoriesResponse = {
id: number;
jsonrpc: string;
result: {
balanceHistories: BalanceHistory[] | null;
nextPageToken?: string;
};
};

export type ListBalanceHistoriesParameters = Extract<
RpcRequestConfig,
{ method: "cdp_listBalanceHistories" }
>["parameters"];

export type ListBalanceHistoriesResponse = Extract<
RpcRequestConfig,
{ method: "cdp_listBalanceHistories" }
>["response"]["result"];

/**
* @returns The list of balance histories of assets specified in parameters.
*
Expand All @@ -44,8 +34,9 @@ export async function listBalanceHistories(
rpc: RpcClient,
parameters: ListBalanceHistoriesParameters,
): Promise<ListBalanceHistoriesResponse> {
return await rpc.request({
const response = await rpc.request({
method: "cdp_listBalanceHistories",
parameters,
});
return response.result;
}
35 changes: 7 additions & 28 deletions packages/onchain/src/balance/list-balances.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,15 @@
import type { RpcClient, RpcRequestConfig } from "../rpc";

type Balance = {
asset: {
id: string;
// TODO: Add all token types
type:
| "native"
| "erc20"
| "erc721"
| "erc1155"
| "creditAlphanum4"
| "fa2"
| (string & {});
groupId: string;
subGroupId: string;
};
value: number;
};

export type ListBalancesResponse = {
id: number;
jsonrpc: string;
result: {
balances: Balance[];
nextPageToken?: string;
};
};

export type ListBalancesParameters = Extract<
RpcRequestConfig,
{ method: "cdp_listBalances" }
>["parameters"];

export type ListBalancesResponse = Extract<
RpcRequestConfig,
{ method: "cdp_listBalances" }
>["response"]["result"];

/**
* @returns The list of token balances specified in parameters.
*
Expand All @@ -55,8 +33,9 @@ export async function listBalances(
rpc: RpcClient,
parameters: ListBalancesParameters,
): Promise<ListBalancesResponse> {
return await rpc.request({
const response = await rpc.request({
method: "cdp_listBalances",
parameters,
});
return response.result;
}
10 changes: 5 additions & 5 deletions packages/onchain/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export { listAddressTransactions } from "./address/list-address-transactions";
export type {
ListAddressTransactionsResponse,
ListAddressTransactionsParameters,
} from "./address/list-address-transactions";
export { listBalances } from "./balance/list-balances";
export type {
ListBalancesParameters,
Expand All @@ -13,11 +18,6 @@ export type {
ListBalanceHistoriesResponse,
ListBalanceHistoriesParameters,
} from "./balance/list-balance-histories";
export { listAddressTransactions } from "./address/list-address-transactions";
export type {
ListAddressTransactionsResponse,
ListAddressTransactionsParameters,
} from "./address/list-address-transactions";
export { createClient } from "./client";
export type { Client, ClientConfig } from "./client";
export { createRpcClient } from "./rpc";
Expand Down
89 changes: 88 additions & 1 deletion packages/onchain/src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@ import * as http from "./http";

export type RpcClient = {
__url: string;
request: <TResponse>(config: RpcRequestConfig) => Promise<TResponse>;
request: <
TConfig extends RpcRequestConfig,
TMethod extends RpcRequestConfig["method"],
>(
config: TConfig extends {
method: TMethod;
parameters: infer TParameters;
}
? { method: TMethod; parameters: TParameters }
: never,
) => Promise<
TConfig extends { method: TMethod; response: infer TResponse }
? TResponse
: never
>;
};

export type RpcClientConfig = {
Expand Down Expand Up @@ -44,6 +58,10 @@ export type RpcRequestConfig =
/** Number of balances to receive in a page. The default value is 25. The maximum value is 100, and values supplied over this will be coerced to the maximum. */
pageSize?: number;
}>;
response: BaseRpcResponse<{
addressTransactions: RpcAddressTransaction[];
nextPageToken?: string;
}>;
}
| {
method: "cdp_listBalanceDetails";
Expand All @@ -55,6 +73,10 @@ export type RpcRequestConfig =
/** Number of balances to receive in a page. The default value is 25. The maximum value is 100, and values supplied over this will be coerced to the maximum. */
pageSize?: number;
}>;
response: BaseRpcResponse<{
balances: RpcBalanceDetails[];
nextPageToken?: string;
}>;
}
| {
method: "cdp_listBalanceHistories";
Expand All @@ -66,6 +88,10 @@ export type RpcRequestConfig =
/** Number of balances to receive in a page. The default value is 25. The maximum value is 100, and values supplied over this will be coerced to the maximum. */
pageSize?: number;
}>;
response: BaseRpcResponse<{
balanceHistories: RpcBalanceHistory[] | null;
nextPageToken?: string;
}>;
}
| {
method: "cdp_listBalances";
Expand All @@ -76,4 +102,65 @@ export type RpcRequestConfig =
/** Number of balances to receive in a page. The default value is 25. The maximum value is 100, and values supplied over this will be coerced to the maximum. */
pageSize?: number;
}>;
response: BaseRpcResponse<{
balances: RpcBalance[];
nextPageToken?: string;
}>;
};

type BaseRpcResponse<TResult> = {
id: number;
jsonrpc: string;
result: TResult;
};

export type RpcAddressTransaction = {
name: string;
hash: string;
blockHash: string;
blockHeight: string;
// TODO: can be narrowed to a union
status: string;
};

export type RpcBalance = {
asset: {
id: string;
// TODO: Add all token types
type:
| "native"
| "erc20"
| "erc721"
| "erc1155"
| "creditAlphanum4"
| "fa2"
| (string & {});
groupId: string;
subGroupId: string;
};
value: number;
};

export type RpcBalanceDetails = {
asset: {
id: string;
// TODO: Add all token types
type:
| "native"
| "erc20"
| "erc721"
| "erc1155"
| "creditAlphanum4"
| "fa2"
| (string & {});
groupId: string;
subGroupId: string;
};
value: number;
};

export type RpcBalanceHistory = {
blockHeight: number;
blockHash: string;
value: number;
};

0 comments on commit c2e80d3

Please sign in to comment.