From 3124187177ef2269915e68bd5a90fe6f02214725 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 24 Aug 2023 16:46:19 +0200 Subject: [PATCH] Remove Adaptor abstractions --- CHANGELOG.md | 5 ++ .../src/comet38/adaptor/index.ts | 13 ++--- .../src/comet38/adaptor/responses.spec.ts | 2 +- .../src/comet38/adaptor/types.ts | 50 ------------------- .../src/comet38/comet38client.ts | 48 ++++++++---------- .../src/tendermint34/adaptor/index.ts | 13 ++--- .../tendermint34/adaptor/responses.spec.ts | 2 +- .../src/tendermint34/adaptor/types.ts | 50 ------------------- .../src/tendermint34/tendermint34client.ts | 48 ++++++++---------- .../src/tendermint37/adaptor/index.ts | 13 ++--- .../tendermint37/adaptor/responses.spec.ts | 2 +- .../src/tendermint37/adaptor/types.ts | 50 ------------------- .../src/tendermint37/tendermint37client.ts | 48 ++++++++---------- 13 files changed, 83 insertions(+), 261 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 351861b0c4..c842224023 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to ## [Unreleased] +### Changed + +- @cosmjs/tendermint-rpc: Remove `Adaptor` abstractions which are not needed + anymore by haing a dedicated client for each backend. + ## [0.31.1] - 2023-08-21 ### Fixed diff --git a/packages/tendermint-rpc/src/comet38/adaptor/index.ts b/packages/tendermint-rpc/src/comet38/adaptor/index.ts index f6e2998492..d592b38acb 100644 --- a/packages/tendermint-rpc/src/comet38/adaptor/index.ts +++ b/packages/tendermint-rpc/src/comet38/adaptor/index.ts @@ -1,10 +1,3 @@ -import { Params } from "./requests"; -import { Responses } from "./responses"; -import { Adaptor } from "./types"; - -export { Decoder, Encoder, Params, Responses } from "./types"; - -export const adaptor38: Adaptor = { - params: Params, - responses: Responses, -}; +export { Params } from "./requests"; +export { Responses } from "./responses"; +export { Decoder, Encoder } from "./types"; diff --git a/packages/tendermint-rpc/src/comet38/adaptor/responses.spec.ts b/packages/tendermint-rpc/src/comet38/adaptor/responses.spec.ts index c85b753328..38baf246c9 100644 --- a/packages/tendermint-rpc/src/comet38/adaptor/responses.spec.ts +++ b/packages/tendermint-rpc/src/comet38/adaptor/responses.spec.ts @@ -3,7 +3,7 @@ import { fromBase64, fromHex } from "@cosmjs/encoding"; import { decodeEvent, decodeValidatorGenesis, decodeValidatorInfo, decodeValidatorUpdate } from "./responses"; -describe("Adaptor Responses", () => { +describe("Responses", () => { describe("decodeEvent", () => { it("works with attributes", () => { // from https://rpc.mainnet-1.tgrade.confio.run/tx?hash=0x2C44715748022DB2FB5F40105383719BFCFCEE51DBC02FF4088BE3F5924CD7BF diff --git a/packages/tendermint-rpc/src/comet38/adaptor/types.ts b/packages/tendermint-rpc/src/comet38/adaptor/types.ts index 1ce9ea1c17..f9bf1d11ab 100644 --- a/packages/tendermint-rpc/src/comet38/adaptor/types.ts +++ b/packages/tendermint-rpc/src/comet38/adaptor/types.ts @@ -1,60 +1,10 @@ import { JsonRpcRequest, JsonRpcSuccessResponse } from "@cosmjs/json-rpc"; -import { SubscriptionEvent } from "../../rpcclients"; import * as requests from "../requests"; import * as responses from "../responses"; -export interface Adaptor { - readonly params: Params; - readonly responses: Responses; -} - // Encoder is a generic that matches all methods of Params export type Encoder = (req: T) => JsonRpcRequest; // Decoder is a generic that matches all methods of Responses export type Decoder = (res: JsonRpcSuccessResponse) => T; - -export interface Params { - readonly encodeAbciInfo: (req: requests.AbciInfoRequest) => JsonRpcRequest; - readonly encodeAbciQuery: (req: requests.AbciQueryRequest) => JsonRpcRequest; - readonly encodeBlock: (req: requests.BlockRequest) => JsonRpcRequest; - readonly encodeBlockchain: (req: requests.BlockchainRequest) => JsonRpcRequest; - readonly encodeBlockResults: (req: requests.BlockResultsRequest) => JsonRpcRequest; - readonly encodeBlockSearch: (req: requests.BlockSearchRequest) => JsonRpcRequest; - readonly encodeBroadcastTx: (req: requests.BroadcastTxRequest) => JsonRpcRequest; - readonly encodeCommit: (req: requests.CommitRequest) => JsonRpcRequest; - readonly encodeGenesis: (req: requests.GenesisRequest) => JsonRpcRequest; - readonly encodeHealth: (req: requests.HealthRequest) => JsonRpcRequest; - readonly encodeNumUnconfirmedTxs: (req: requests.NumUnconfirmedTxsRequest) => JsonRpcRequest; - readonly encodeStatus: (req: requests.StatusRequest) => JsonRpcRequest; - readonly encodeSubscribe: (req: requests.SubscribeRequest) => JsonRpcRequest; - readonly encodeTx: (req: requests.TxRequest) => JsonRpcRequest; - readonly encodeTxSearch: (req: requests.TxSearchRequest) => JsonRpcRequest; - readonly encodeValidators: (req: requests.ValidatorsRequest) => JsonRpcRequest; -} - -export interface Responses { - readonly decodeAbciInfo: (response: JsonRpcSuccessResponse) => responses.AbciInfoResponse; - readonly decodeAbciQuery: (response: JsonRpcSuccessResponse) => responses.AbciQueryResponse; - readonly decodeBlock: (response: JsonRpcSuccessResponse) => responses.BlockResponse; - readonly decodeBlockResults: (response: JsonRpcSuccessResponse) => responses.BlockResultsResponse; - readonly decodeBlockSearch: (response: JsonRpcSuccessResponse) => responses.BlockSearchResponse; - readonly decodeBlockchain: (response: JsonRpcSuccessResponse) => responses.BlockchainResponse; - readonly decodeBroadcastTxSync: (response: JsonRpcSuccessResponse) => responses.BroadcastTxSyncResponse; - readonly decodeBroadcastTxAsync: (response: JsonRpcSuccessResponse) => responses.BroadcastTxAsyncResponse; - readonly decodeBroadcastTxCommit: (response: JsonRpcSuccessResponse) => responses.BroadcastTxCommitResponse; - readonly decodeCommit: (response: JsonRpcSuccessResponse) => responses.CommitResponse; - readonly decodeGenesis: (response: JsonRpcSuccessResponse) => responses.GenesisResponse; - readonly decodeHealth: (response: JsonRpcSuccessResponse) => responses.HealthResponse; - readonly decodeNumUnconfirmedTxs: (response: JsonRpcSuccessResponse) => responses.NumUnconfirmedTxsResponse; - readonly decodeStatus: (response: JsonRpcSuccessResponse) => responses.StatusResponse; - readonly decodeTx: (response: JsonRpcSuccessResponse) => responses.TxResponse; - readonly decodeTxSearch: (response: JsonRpcSuccessResponse) => responses.TxSearchResponse; - readonly decodeValidators: (response: JsonRpcSuccessResponse) => responses.ValidatorsResponse; - - // events - readonly decodeNewBlockEvent: (response: SubscriptionEvent) => responses.NewBlockEvent; - readonly decodeNewBlockHeaderEvent: (response: SubscriptionEvent) => responses.NewBlockHeaderEvent; - readonly decodeTxEvent: (response: SubscriptionEvent) => responses.TxEvent; -} diff --git a/packages/tendermint-rpc/src/comet38/comet38client.ts b/packages/tendermint-rpc/src/comet38/comet38client.ts index f6c4bc88de..f8037aa240 100644 --- a/packages/tendermint-rpc/src/comet38/comet38client.ts +++ b/packages/tendermint-rpc/src/comet38/comet38client.ts @@ -9,7 +9,7 @@ import { SubscriptionEvent, WebsocketClient, } from "../rpcclients"; -import { adaptor38, Decoder, Encoder, Params, Responses } from "./adaptor"; +import { Decoder, Encoder, Params, Responses } from "./adaptor"; import * as requests from "./requests"; import * as responses from "./responses"; @@ -61,16 +61,12 @@ export class Comet38Client { } private readonly client: RpcClient; - private readonly p: Params; - private readonly r: Responses; /** * Use `Tendermint37Client.connect` or `Tendermint37Client.create` to create an instance. */ private constructor(client: RpcClient) { this.client = client; - this.p = adaptor38.params; - this.r = adaptor38.responses; } public disconnect(): void { @@ -79,17 +75,17 @@ export class Comet38Client { public async abciInfo(): Promise { const query: requests.AbciInfoRequest = { method: requests.Method.AbciInfo }; - return this.doCall(query, this.p.encodeAbciInfo, this.r.decodeAbciInfo); + return this.doCall(query, Params.encodeAbciInfo, Responses.decodeAbciInfo); } public async abciQuery(params: requests.AbciQueryParams): Promise { const query: requests.AbciQueryRequest = { params: params, method: requests.Method.AbciQuery }; - return this.doCall(query, this.p.encodeAbciQuery, this.r.decodeAbciQuery); + return this.doCall(query, Params.encodeAbciQuery, Responses.decodeAbciQuery); } public async block(height?: number): Promise { const query: requests.BlockRequest = { method: requests.Method.Block, params: { height: height } }; - return this.doCall(query, this.p.encodeBlock, this.r.decodeBlock); + return this.doCall(query, Params.encodeBlock, Responses.decodeBlock); } public async blockResults(height?: number): Promise { @@ -97,7 +93,7 @@ export class Comet38Client { method: requests.Method.BlockResults, params: { height: height }, }; - return this.doCall(query, this.p.encodeBlockResults, this.r.decodeBlockResults); + return this.doCall(query, Params.encodeBlockResults, Responses.decodeBlockResults); } /** @@ -110,7 +106,7 @@ export class Comet38Client { */ public async blockSearch(params: requests.BlockSearchParams): Promise { const query: requests.BlockSearchRequest = { params: params, method: requests.Method.BlockSearch }; - const resp = await this.doCall(query, this.p.encodeBlockSearch, this.r.decodeBlockSearch); + const resp = await this.doCall(query, Params.encodeBlockSearch, Responses.decodeBlockSearch); return { ...resp, // make sure we sort by height, as tendermint may be sorting by string value of the height @@ -161,7 +157,7 @@ export class Comet38Client { maxHeight: maxHeight, }, }; - return this.doCall(query, this.p.encodeBlockchain, this.r.decodeBlockchain); + return this.doCall(query, Params.encodeBlockchain, Responses.decodeBlockchain); } /** @@ -173,7 +169,7 @@ export class Comet38Client { params: requests.BroadcastTxParams, ): Promise { const query: requests.BroadcastTxRequest = { params: params, method: requests.Method.BroadcastTxSync }; - return this.doCall(query, this.p.encodeBroadcastTx, this.r.decodeBroadcastTxSync); + return this.doCall(query, Params.encodeBroadcastTx, Responses.decodeBroadcastTxSync); } /** @@ -185,7 +181,7 @@ export class Comet38Client { params: requests.BroadcastTxParams, ): Promise { const query: requests.BroadcastTxRequest = { params: params, method: requests.Method.BroadcastTxAsync }; - return this.doCall(query, this.p.encodeBroadcastTx, this.r.decodeBroadcastTxAsync); + return this.doCall(query, Params.encodeBroadcastTx, Responses.decodeBroadcastTxAsync); } /** @@ -197,32 +193,32 @@ export class Comet38Client { params: requests.BroadcastTxParams, ): Promise { const query: requests.BroadcastTxRequest = { params: params, method: requests.Method.BroadcastTxCommit }; - return this.doCall(query, this.p.encodeBroadcastTx, this.r.decodeBroadcastTxCommit); + return this.doCall(query, Params.encodeBroadcastTx, Responses.decodeBroadcastTxCommit); } public async commit(height?: number): Promise { const query: requests.CommitRequest = { method: requests.Method.Commit, params: { height: height } }; - return this.doCall(query, this.p.encodeCommit, this.r.decodeCommit); + return this.doCall(query, Params.encodeCommit, Responses.decodeCommit); } public async genesis(): Promise { const query: requests.GenesisRequest = { method: requests.Method.Genesis }; - return this.doCall(query, this.p.encodeGenesis, this.r.decodeGenesis); + return this.doCall(query, Params.encodeGenesis, Responses.decodeGenesis); } public async health(): Promise { const query: requests.HealthRequest = { method: requests.Method.Health }; - return this.doCall(query, this.p.encodeHealth, this.r.decodeHealth); + return this.doCall(query, Params.encodeHealth, Responses.decodeHealth); } public async numUnconfirmedTxs(): Promise { const query: requests.NumUnconfirmedTxsRequest = { method: requests.Method.NumUnconfirmedTxs }; - return this.doCall(query, this.p.encodeNumUnconfirmedTxs, this.r.decodeNumUnconfirmedTxs); + return this.doCall(query, Params.encodeNumUnconfirmedTxs, Responses.decodeNumUnconfirmedTxs); } public async status(): Promise { const query: requests.StatusRequest = { method: requests.Method.Status }; - return this.doCall(query, this.p.encodeStatus, this.r.decodeStatus); + return this.doCall(query, Params.encodeStatus, Responses.decodeStatus); } public subscribeNewBlock(): Stream { @@ -230,7 +226,7 @@ export class Comet38Client { method: requests.Method.Subscribe, query: { type: requests.SubscriptionEventType.NewBlock }, }; - return this.subscribe(request, this.r.decodeNewBlockEvent); + return this.subscribe(request, Responses.decodeNewBlockEvent); } public subscribeNewBlockHeader(): Stream { @@ -238,7 +234,7 @@ export class Comet38Client { method: requests.Method.Subscribe, query: { type: requests.SubscriptionEventType.NewBlockHeader }, }; - return this.subscribe(request, this.r.decodeNewBlockHeaderEvent); + return this.subscribe(request, Responses.decodeNewBlockHeaderEvent); } public subscribeTx(query?: string): Stream { @@ -249,7 +245,7 @@ export class Comet38Client { raw: query, }, }; - return this.subscribe(request, this.r.decodeTxEvent); + return this.subscribe(request, Responses.decodeTxEvent); } /** @@ -259,7 +255,7 @@ export class Comet38Client { */ public async tx(params: requests.TxParams): Promise { const query: requests.TxRequest = { params: params, method: requests.Method.Tx }; - return this.doCall(query, this.p.encodeTx, this.r.decodeTx); + return this.doCall(query, Params.encodeTx, Responses.decodeTx); } /** @@ -269,7 +265,7 @@ export class Comet38Client { */ public async txSearch(params: requests.TxSearchParams): Promise { const query: requests.TxSearchRequest = { params: params, method: requests.Method.TxSearch }; - return this.doCall(query, this.p.encodeTxSearch, this.r.decodeTxSearch); + return this.doCall(query, Params.encodeTxSearch, Responses.decodeTxSearch); } // this should paginate through all txSearch options to ensure it returns all results. @@ -300,7 +296,7 @@ export class Comet38Client { method: requests.Method.Validators, params: params, }; - return this.doCall(query, this.p.encodeValidators, this.r.decodeValidators); + return this.doCall(query, Params.encodeValidators, Responses.decodeValidators); } public async validatorsAll(height?: number): Promise { @@ -349,7 +345,7 @@ export class Comet38Client { throw new Error("This RPC client type cannot subscribe to events"); } - const req = this.p.encodeSubscribe(request); + const req = Params.encodeSubscribe(request); const eventStream = this.client.listen(req); return eventStream.map((event) => { return decode(event); diff --git a/packages/tendermint-rpc/src/tendermint34/adaptor/index.ts b/packages/tendermint-rpc/src/tendermint34/adaptor/index.ts index 5e63cb3d39..d592b38acb 100644 --- a/packages/tendermint-rpc/src/tendermint34/adaptor/index.ts +++ b/packages/tendermint-rpc/src/tendermint34/adaptor/index.ts @@ -1,10 +1,3 @@ -import { Params } from "./requests"; -import { Responses } from "./responses"; -import { Adaptor } from "./types"; - -export { Decoder, Encoder, Params, Responses } from "./types"; - -export const adaptor34: Adaptor = { - params: Params, - responses: Responses, -}; +export { Params } from "./requests"; +export { Responses } from "./responses"; +export { Decoder, Encoder } from "./types"; diff --git a/packages/tendermint-rpc/src/tendermint34/adaptor/responses.spec.ts b/packages/tendermint-rpc/src/tendermint34/adaptor/responses.spec.ts index 7de09654d0..056cdc3d02 100644 --- a/packages/tendermint-rpc/src/tendermint34/adaptor/responses.spec.ts +++ b/packages/tendermint-rpc/src/tendermint34/adaptor/responses.spec.ts @@ -3,7 +3,7 @@ import { fromBase64, fromHex, toUtf8 } from "@cosmjs/encoding"; import { decodeEvent, decodeValidatorGenesis, decodeValidatorInfo, decodeValidatorUpdate } from "./responses"; -describe("Adaptor Responses", () => { +describe("Responses", () => { describe("decodeEvent", () => { it("works with attributes", () => { // from https://rpc.mainnet-1.tgrade.confio.run/tx?hash=0x2C44715748022DB2FB5F40105383719BFCFCEE51DBC02FF4088BE3F5924CD7BF diff --git a/packages/tendermint-rpc/src/tendermint34/adaptor/types.ts b/packages/tendermint-rpc/src/tendermint34/adaptor/types.ts index 1ce9ea1c17..f9bf1d11ab 100644 --- a/packages/tendermint-rpc/src/tendermint34/adaptor/types.ts +++ b/packages/tendermint-rpc/src/tendermint34/adaptor/types.ts @@ -1,60 +1,10 @@ import { JsonRpcRequest, JsonRpcSuccessResponse } from "@cosmjs/json-rpc"; -import { SubscriptionEvent } from "../../rpcclients"; import * as requests from "../requests"; import * as responses from "../responses"; -export interface Adaptor { - readonly params: Params; - readonly responses: Responses; -} - // Encoder is a generic that matches all methods of Params export type Encoder = (req: T) => JsonRpcRequest; // Decoder is a generic that matches all methods of Responses export type Decoder = (res: JsonRpcSuccessResponse) => T; - -export interface Params { - readonly encodeAbciInfo: (req: requests.AbciInfoRequest) => JsonRpcRequest; - readonly encodeAbciQuery: (req: requests.AbciQueryRequest) => JsonRpcRequest; - readonly encodeBlock: (req: requests.BlockRequest) => JsonRpcRequest; - readonly encodeBlockchain: (req: requests.BlockchainRequest) => JsonRpcRequest; - readonly encodeBlockResults: (req: requests.BlockResultsRequest) => JsonRpcRequest; - readonly encodeBlockSearch: (req: requests.BlockSearchRequest) => JsonRpcRequest; - readonly encodeBroadcastTx: (req: requests.BroadcastTxRequest) => JsonRpcRequest; - readonly encodeCommit: (req: requests.CommitRequest) => JsonRpcRequest; - readonly encodeGenesis: (req: requests.GenesisRequest) => JsonRpcRequest; - readonly encodeHealth: (req: requests.HealthRequest) => JsonRpcRequest; - readonly encodeNumUnconfirmedTxs: (req: requests.NumUnconfirmedTxsRequest) => JsonRpcRequest; - readonly encodeStatus: (req: requests.StatusRequest) => JsonRpcRequest; - readonly encodeSubscribe: (req: requests.SubscribeRequest) => JsonRpcRequest; - readonly encodeTx: (req: requests.TxRequest) => JsonRpcRequest; - readonly encodeTxSearch: (req: requests.TxSearchRequest) => JsonRpcRequest; - readonly encodeValidators: (req: requests.ValidatorsRequest) => JsonRpcRequest; -} - -export interface Responses { - readonly decodeAbciInfo: (response: JsonRpcSuccessResponse) => responses.AbciInfoResponse; - readonly decodeAbciQuery: (response: JsonRpcSuccessResponse) => responses.AbciQueryResponse; - readonly decodeBlock: (response: JsonRpcSuccessResponse) => responses.BlockResponse; - readonly decodeBlockResults: (response: JsonRpcSuccessResponse) => responses.BlockResultsResponse; - readonly decodeBlockSearch: (response: JsonRpcSuccessResponse) => responses.BlockSearchResponse; - readonly decodeBlockchain: (response: JsonRpcSuccessResponse) => responses.BlockchainResponse; - readonly decodeBroadcastTxSync: (response: JsonRpcSuccessResponse) => responses.BroadcastTxSyncResponse; - readonly decodeBroadcastTxAsync: (response: JsonRpcSuccessResponse) => responses.BroadcastTxAsyncResponse; - readonly decodeBroadcastTxCommit: (response: JsonRpcSuccessResponse) => responses.BroadcastTxCommitResponse; - readonly decodeCommit: (response: JsonRpcSuccessResponse) => responses.CommitResponse; - readonly decodeGenesis: (response: JsonRpcSuccessResponse) => responses.GenesisResponse; - readonly decodeHealth: (response: JsonRpcSuccessResponse) => responses.HealthResponse; - readonly decodeNumUnconfirmedTxs: (response: JsonRpcSuccessResponse) => responses.NumUnconfirmedTxsResponse; - readonly decodeStatus: (response: JsonRpcSuccessResponse) => responses.StatusResponse; - readonly decodeTx: (response: JsonRpcSuccessResponse) => responses.TxResponse; - readonly decodeTxSearch: (response: JsonRpcSuccessResponse) => responses.TxSearchResponse; - readonly decodeValidators: (response: JsonRpcSuccessResponse) => responses.ValidatorsResponse; - - // events - readonly decodeNewBlockEvent: (response: SubscriptionEvent) => responses.NewBlockEvent; - readonly decodeNewBlockHeaderEvent: (response: SubscriptionEvent) => responses.NewBlockHeaderEvent; - readonly decodeTxEvent: (response: SubscriptionEvent) => responses.TxEvent; -} diff --git a/packages/tendermint-rpc/src/tendermint34/tendermint34client.ts b/packages/tendermint-rpc/src/tendermint34/tendermint34client.ts index c0538bfbba..701960de43 100644 --- a/packages/tendermint-rpc/src/tendermint34/tendermint34client.ts +++ b/packages/tendermint-rpc/src/tendermint34/tendermint34client.ts @@ -9,7 +9,7 @@ import { SubscriptionEvent, WebsocketClient, } from "../rpcclients"; -import { adaptor34, Decoder, Encoder, Params, Responses } from "./adaptor"; +import { Decoder, Encoder, Params, Responses } from "./adaptor"; import * as requests from "./requests"; import * as responses from "./responses"; @@ -61,16 +61,12 @@ export class Tendermint34Client { } private readonly client: RpcClient; - private readonly p: Params; - private readonly r: Responses; /** * Use `Tendermint34Client.connect` or `Tendermint34Client.create` to create an instance. */ private constructor(client: RpcClient) { this.client = client; - this.p = adaptor34.params; - this.r = adaptor34.responses; } public disconnect(): void { @@ -79,17 +75,17 @@ export class Tendermint34Client { public async abciInfo(): Promise { const query: requests.AbciInfoRequest = { method: requests.Method.AbciInfo }; - return this.doCall(query, this.p.encodeAbciInfo, this.r.decodeAbciInfo); + return this.doCall(query, Params.encodeAbciInfo, Responses.decodeAbciInfo); } public async abciQuery(params: requests.AbciQueryParams): Promise { const query: requests.AbciQueryRequest = { params: params, method: requests.Method.AbciQuery }; - return this.doCall(query, this.p.encodeAbciQuery, this.r.decodeAbciQuery); + return this.doCall(query, Params.encodeAbciQuery, Responses.decodeAbciQuery); } public async block(height?: number): Promise { const query: requests.BlockRequest = { method: requests.Method.Block, params: { height: height } }; - return this.doCall(query, this.p.encodeBlock, this.r.decodeBlock); + return this.doCall(query, Params.encodeBlock, Responses.decodeBlock); } public async blockResults(height?: number): Promise { @@ -97,7 +93,7 @@ export class Tendermint34Client { method: requests.Method.BlockResults, params: { height: height }, }; - return this.doCall(query, this.p.encodeBlockResults, this.r.decodeBlockResults); + return this.doCall(query, Params.encodeBlockResults, Responses.decodeBlockResults); } /** @@ -110,7 +106,7 @@ export class Tendermint34Client { */ public async blockSearch(params: requests.BlockSearchParams): Promise { const query: requests.BlockSearchRequest = { params: params, method: requests.Method.BlockSearch }; - const resp = await this.doCall(query, this.p.encodeBlockSearch, this.r.decodeBlockSearch); + const resp = await this.doCall(query, Params.encodeBlockSearch, Responses.decodeBlockSearch); return { ...resp, // make sure we sort by height, as tendermint may be sorting by string value of the height @@ -161,7 +157,7 @@ export class Tendermint34Client { maxHeight: maxHeight, }, }; - return this.doCall(query, this.p.encodeBlockchain, this.r.decodeBlockchain); + return this.doCall(query, Params.encodeBlockchain, Responses.decodeBlockchain); } /** @@ -173,7 +169,7 @@ export class Tendermint34Client { params: requests.BroadcastTxParams, ): Promise { const query: requests.BroadcastTxRequest = { params: params, method: requests.Method.BroadcastTxSync }; - return this.doCall(query, this.p.encodeBroadcastTx, this.r.decodeBroadcastTxSync); + return this.doCall(query, Params.encodeBroadcastTx, Responses.decodeBroadcastTxSync); } /** @@ -185,7 +181,7 @@ export class Tendermint34Client { params: requests.BroadcastTxParams, ): Promise { const query: requests.BroadcastTxRequest = { params: params, method: requests.Method.BroadcastTxAsync }; - return this.doCall(query, this.p.encodeBroadcastTx, this.r.decodeBroadcastTxAsync); + return this.doCall(query, Params.encodeBroadcastTx, Responses.decodeBroadcastTxAsync); } /** @@ -197,32 +193,32 @@ export class Tendermint34Client { params: requests.BroadcastTxParams, ): Promise { const query: requests.BroadcastTxRequest = { params: params, method: requests.Method.BroadcastTxCommit }; - return this.doCall(query, this.p.encodeBroadcastTx, this.r.decodeBroadcastTxCommit); + return this.doCall(query, Params.encodeBroadcastTx, Responses.decodeBroadcastTxCommit); } public async commit(height?: number): Promise { const query: requests.CommitRequest = { method: requests.Method.Commit, params: { height: height } }; - return this.doCall(query, this.p.encodeCommit, this.r.decodeCommit); + return this.doCall(query, Params.encodeCommit, Responses.decodeCommit); } public async genesis(): Promise { const query: requests.GenesisRequest = { method: requests.Method.Genesis }; - return this.doCall(query, this.p.encodeGenesis, this.r.decodeGenesis); + return this.doCall(query, Params.encodeGenesis, Responses.decodeGenesis); } public async health(): Promise { const query: requests.HealthRequest = { method: requests.Method.Health }; - return this.doCall(query, this.p.encodeHealth, this.r.decodeHealth); + return this.doCall(query, Params.encodeHealth, Responses.decodeHealth); } public async numUnconfirmedTxs(): Promise { const query: requests.NumUnconfirmedTxsRequest = { method: requests.Method.NumUnconfirmedTxs }; - return this.doCall(query, this.p.encodeNumUnconfirmedTxs, this.r.decodeNumUnconfirmedTxs); + return this.doCall(query, Params.encodeNumUnconfirmedTxs, Responses.decodeNumUnconfirmedTxs); } public async status(): Promise { const query: requests.StatusRequest = { method: requests.Method.Status }; - return this.doCall(query, this.p.encodeStatus, this.r.decodeStatus); + return this.doCall(query, Params.encodeStatus, Responses.decodeStatus); } public subscribeNewBlock(): Stream { @@ -230,7 +226,7 @@ export class Tendermint34Client { method: requests.Method.Subscribe, query: { type: requests.SubscriptionEventType.NewBlock }, }; - return this.subscribe(request, this.r.decodeNewBlockEvent); + return this.subscribe(request, Responses.decodeNewBlockEvent); } public subscribeNewBlockHeader(): Stream { @@ -238,7 +234,7 @@ export class Tendermint34Client { method: requests.Method.Subscribe, query: { type: requests.SubscriptionEventType.NewBlockHeader }, }; - return this.subscribe(request, this.r.decodeNewBlockHeaderEvent); + return this.subscribe(request, Responses.decodeNewBlockHeaderEvent); } public subscribeTx(query?: string): Stream { @@ -249,7 +245,7 @@ export class Tendermint34Client { raw: query, }, }; - return this.subscribe(request, this.r.decodeTxEvent); + return this.subscribe(request, Responses.decodeTxEvent); } /** @@ -259,7 +255,7 @@ export class Tendermint34Client { */ public async tx(params: requests.TxParams): Promise { const query: requests.TxRequest = { params: params, method: requests.Method.Tx }; - return this.doCall(query, this.p.encodeTx, this.r.decodeTx); + return this.doCall(query, Params.encodeTx, Responses.decodeTx); } /** @@ -269,7 +265,7 @@ export class Tendermint34Client { */ public async txSearch(params: requests.TxSearchParams): Promise { const query: requests.TxSearchRequest = { params: params, method: requests.Method.TxSearch }; - return this.doCall(query, this.p.encodeTxSearch, this.r.decodeTxSearch); + return this.doCall(query, Params.encodeTxSearch, Responses.decodeTxSearch); } // this should paginate through all txSearch options to ensure it returns all results. @@ -300,7 +296,7 @@ export class Tendermint34Client { method: requests.Method.Validators, params: params, }; - return this.doCall(query, this.p.encodeValidators, this.r.decodeValidators); + return this.doCall(query, Params.encodeValidators, Responses.decodeValidators); } public async validatorsAll(height?: number): Promise { @@ -349,7 +345,7 @@ export class Tendermint34Client { throw new Error("This RPC client type cannot subscribe to events"); } - const req = this.p.encodeSubscribe(request); + const req = Params.encodeSubscribe(request); const eventStream = this.client.listen(req); return eventStream.map((event) => { return decode(event); diff --git a/packages/tendermint-rpc/src/tendermint37/adaptor/index.ts b/packages/tendermint-rpc/src/tendermint37/adaptor/index.ts index 86739c6611..d592b38acb 100644 --- a/packages/tendermint-rpc/src/tendermint37/adaptor/index.ts +++ b/packages/tendermint-rpc/src/tendermint37/adaptor/index.ts @@ -1,10 +1,3 @@ -import { Params } from "./requests"; -import { Responses } from "./responses"; -import { Adaptor } from "./types"; - -export { Decoder, Encoder, Params, Responses } from "./types"; - -export const adaptor37: Adaptor = { - params: Params, - responses: Responses, -}; +export { Params } from "./requests"; +export { Responses } from "./responses"; +export { Decoder, Encoder } from "./types"; diff --git a/packages/tendermint-rpc/src/tendermint37/adaptor/responses.spec.ts b/packages/tendermint-rpc/src/tendermint37/adaptor/responses.spec.ts index c85b753328..38baf246c9 100644 --- a/packages/tendermint-rpc/src/tendermint37/adaptor/responses.spec.ts +++ b/packages/tendermint-rpc/src/tendermint37/adaptor/responses.spec.ts @@ -3,7 +3,7 @@ import { fromBase64, fromHex } from "@cosmjs/encoding"; import { decodeEvent, decodeValidatorGenesis, decodeValidatorInfo, decodeValidatorUpdate } from "./responses"; -describe("Adaptor Responses", () => { +describe("Responses", () => { describe("decodeEvent", () => { it("works with attributes", () => { // from https://rpc.mainnet-1.tgrade.confio.run/tx?hash=0x2C44715748022DB2FB5F40105383719BFCFCEE51DBC02FF4088BE3F5924CD7BF diff --git a/packages/tendermint-rpc/src/tendermint37/adaptor/types.ts b/packages/tendermint-rpc/src/tendermint37/adaptor/types.ts index 1ce9ea1c17..f9bf1d11ab 100644 --- a/packages/tendermint-rpc/src/tendermint37/adaptor/types.ts +++ b/packages/tendermint-rpc/src/tendermint37/adaptor/types.ts @@ -1,60 +1,10 @@ import { JsonRpcRequest, JsonRpcSuccessResponse } from "@cosmjs/json-rpc"; -import { SubscriptionEvent } from "../../rpcclients"; import * as requests from "../requests"; import * as responses from "../responses"; -export interface Adaptor { - readonly params: Params; - readonly responses: Responses; -} - // Encoder is a generic that matches all methods of Params export type Encoder = (req: T) => JsonRpcRequest; // Decoder is a generic that matches all methods of Responses export type Decoder = (res: JsonRpcSuccessResponse) => T; - -export interface Params { - readonly encodeAbciInfo: (req: requests.AbciInfoRequest) => JsonRpcRequest; - readonly encodeAbciQuery: (req: requests.AbciQueryRequest) => JsonRpcRequest; - readonly encodeBlock: (req: requests.BlockRequest) => JsonRpcRequest; - readonly encodeBlockchain: (req: requests.BlockchainRequest) => JsonRpcRequest; - readonly encodeBlockResults: (req: requests.BlockResultsRequest) => JsonRpcRequest; - readonly encodeBlockSearch: (req: requests.BlockSearchRequest) => JsonRpcRequest; - readonly encodeBroadcastTx: (req: requests.BroadcastTxRequest) => JsonRpcRequest; - readonly encodeCommit: (req: requests.CommitRequest) => JsonRpcRequest; - readonly encodeGenesis: (req: requests.GenesisRequest) => JsonRpcRequest; - readonly encodeHealth: (req: requests.HealthRequest) => JsonRpcRequest; - readonly encodeNumUnconfirmedTxs: (req: requests.NumUnconfirmedTxsRequest) => JsonRpcRequest; - readonly encodeStatus: (req: requests.StatusRequest) => JsonRpcRequest; - readonly encodeSubscribe: (req: requests.SubscribeRequest) => JsonRpcRequest; - readonly encodeTx: (req: requests.TxRequest) => JsonRpcRequest; - readonly encodeTxSearch: (req: requests.TxSearchRequest) => JsonRpcRequest; - readonly encodeValidators: (req: requests.ValidatorsRequest) => JsonRpcRequest; -} - -export interface Responses { - readonly decodeAbciInfo: (response: JsonRpcSuccessResponse) => responses.AbciInfoResponse; - readonly decodeAbciQuery: (response: JsonRpcSuccessResponse) => responses.AbciQueryResponse; - readonly decodeBlock: (response: JsonRpcSuccessResponse) => responses.BlockResponse; - readonly decodeBlockResults: (response: JsonRpcSuccessResponse) => responses.BlockResultsResponse; - readonly decodeBlockSearch: (response: JsonRpcSuccessResponse) => responses.BlockSearchResponse; - readonly decodeBlockchain: (response: JsonRpcSuccessResponse) => responses.BlockchainResponse; - readonly decodeBroadcastTxSync: (response: JsonRpcSuccessResponse) => responses.BroadcastTxSyncResponse; - readonly decodeBroadcastTxAsync: (response: JsonRpcSuccessResponse) => responses.BroadcastTxAsyncResponse; - readonly decodeBroadcastTxCommit: (response: JsonRpcSuccessResponse) => responses.BroadcastTxCommitResponse; - readonly decodeCommit: (response: JsonRpcSuccessResponse) => responses.CommitResponse; - readonly decodeGenesis: (response: JsonRpcSuccessResponse) => responses.GenesisResponse; - readonly decodeHealth: (response: JsonRpcSuccessResponse) => responses.HealthResponse; - readonly decodeNumUnconfirmedTxs: (response: JsonRpcSuccessResponse) => responses.NumUnconfirmedTxsResponse; - readonly decodeStatus: (response: JsonRpcSuccessResponse) => responses.StatusResponse; - readonly decodeTx: (response: JsonRpcSuccessResponse) => responses.TxResponse; - readonly decodeTxSearch: (response: JsonRpcSuccessResponse) => responses.TxSearchResponse; - readonly decodeValidators: (response: JsonRpcSuccessResponse) => responses.ValidatorsResponse; - - // events - readonly decodeNewBlockEvent: (response: SubscriptionEvent) => responses.NewBlockEvent; - readonly decodeNewBlockHeaderEvent: (response: SubscriptionEvent) => responses.NewBlockHeaderEvent; - readonly decodeTxEvent: (response: SubscriptionEvent) => responses.TxEvent; -} diff --git a/packages/tendermint-rpc/src/tendermint37/tendermint37client.ts b/packages/tendermint-rpc/src/tendermint37/tendermint37client.ts index 9bbbc3117c..9d89708e2a 100644 --- a/packages/tendermint-rpc/src/tendermint37/tendermint37client.ts +++ b/packages/tendermint-rpc/src/tendermint37/tendermint37client.ts @@ -9,7 +9,7 @@ import { SubscriptionEvent, WebsocketClient, } from "../rpcclients"; -import { adaptor37, Decoder, Encoder, Params, Responses } from "./adaptor"; +import { Decoder, Encoder, Params, Responses } from "./adaptor"; import * as requests from "./requests"; import * as responses from "./responses"; @@ -61,16 +61,12 @@ export class Tendermint37Client { } private readonly client: RpcClient; - private readonly p: Params; - private readonly r: Responses; /** * Use `Tendermint37Client.connect` or `Tendermint37Client.create` to create an instance. */ private constructor(client: RpcClient) { this.client = client; - this.p = adaptor37.params; - this.r = adaptor37.responses; } public disconnect(): void { @@ -79,17 +75,17 @@ export class Tendermint37Client { public async abciInfo(): Promise { const query: requests.AbciInfoRequest = { method: requests.Method.AbciInfo }; - return this.doCall(query, this.p.encodeAbciInfo, this.r.decodeAbciInfo); + return this.doCall(query, Params.encodeAbciInfo, Responses.decodeAbciInfo); } public async abciQuery(params: requests.AbciQueryParams): Promise { const query: requests.AbciQueryRequest = { params: params, method: requests.Method.AbciQuery }; - return this.doCall(query, this.p.encodeAbciQuery, this.r.decodeAbciQuery); + return this.doCall(query, Params.encodeAbciQuery, Responses.decodeAbciQuery); } public async block(height?: number): Promise { const query: requests.BlockRequest = { method: requests.Method.Block, params: { height: height } }; - return this.doCall(query, this.p.encodeBlock, this.r.decodeBlock); + return this.doCall(query, Params.encodeBlock, Responses.decodeBlock); } public async blockResults(height?: number): Promise { @@ -97,7 +93,7 @@ export class Tendermint37Client { method: requests.Method.BlockResults, params: { height: height }, }; - return this.doCall(query, this.p.encodeBlockResults, this.r.decodeBlockResults); + return this.doCall(query, Params.encodeBlockResults, Responses.decodeBlockResults); } /** @@ -110,7 +106,7 @@ export class Tendermint37Client { */ public async blockSearch(params: requests.BlockSearchParams): Promise { const query: requests.BlockSearchRequest = { params: params, method: requests.Method.BlockSearch }; - const resp = await this.doCall(query, this.p.encodeBlockSearch, this.r.decodeBlockSearch); + const resp = await this.doCall(query, Params.encodeBlockSearch, Responses.decodeBlockSearch); return { ...resp, // make sure we sort by height, as tendermint may be sorting by string value of the height @@ -161,7 +157,7 @@ export class Tendermint37Client { maxHeight: maxHeight, }, }; - return this.doCall(query, this.p.encodeBlockchain, this.r.decodeBlockchain); + return this.doCall(query, Params.encodeBlockchain, Responses.decodeBlockchain); } /** @@ -173,7 +169,7 @@ export class Tendermint37Client { params: requests.BroadcastTxParams, ): Promise { const query: requests.BroadcastTxRequest = { params: params, method: requests.Method.BroadcastTxSync }; - return this.doCall(query, this.p.encodeBroadcastTx, this.r.decodeBroadcastTxSync); + return this.doCall(query, Params.encodeBroadcastTx, Responses.decodeBroadcastTxSync); } /** @@ -185,7 +181,7 @@ export class Tendermint37Client { params: requests.BroadcastTxParams, ): Promise { const query: requests.BroadcastTxRequest = { params: params, method: requests.Method.BroadcastTxAsync }; - return this.doCall(query, this.p.encodeBroadcastTx, this.r.decodeBroadcastTxAsync); + return this.doCall(query, Params.encodeBroadcastTx, Responses.decodeBroadcastTxAsync); } /** @@ -197,32 +193,32 @@ export class Tendermint37Client { params: requests.BroadcastTxParams, ): Promise { const query: requests.BroadcastTxRequest = { params: params, method: requests.Method.BroadcastTxCommit }; - return this.doCall(query, this.p.encodeBroadcastTx, this.r.decodeBroadcastTxCommit); + return this.doCall(query, Params.encodeBroadcastTx, Responses.decodeBroadcastTxCommit); } public async commit(height?: number): Promise { const query: requests.CommitRequest = { method: requests.Method.Commit, params: { height: height } }; - return this.doCall(query, this.p.encodeCommit, this.r.decodeCommit); + return this.doCall(query, Params.encodeCommit, Responses.decodeCommit); } public async genesis(): Promise { const query: requests.GenesisRequest = { method: requests.Method.Genesis }; - return this.doCall(query, this.p.encodeGenesis, this.r.decodeGenesis); + return this.doCall(query, Params.encodeGenesis, Responses.decodeGenesis); } public async health(): Promise { const query: requests.HealthRequest = { method: requests.Method.Health }; - return this.doCall(query, this.p.encodeHealth, this.r.decodeHealth); + return this.doCall(query, Params.encodeHealth, Responses.decodeHealth); } public async numUnconfirmedTxs(): Promise { const query: requests.NumUnconfirmedTxsRequest = { method: requests.Method.NumUnconfirmedTxs }; - return this.doCall(query, this.p.encodeNumUnconfirmedTxs, this.r.decodeNumUnconfirmedTxs); + return this.doCall(query, Params.encodeNumUnconfirmedTxs, Responses.decodeNumUnconfirmedTxs); } public async status(): Promise { const query: requests.StatusRequest = { method: requests.Method.Status }; - return this.doCall(query, this.p.encodeStatus, this.r.decodeStatus); + return this.doCall(query, Params.encodeStatus, Responses.decodeStatus); } public subscribeNewBlock(): Stream { @@ -230,7 +226,7 @@ export class Tendermint37Client { method: requests.Method.Subscribe, query: { type: requests.SubscriptionEventType.NewBlock }, }; - return this.subscribe(request, this.r.decodeNewBlockEvent); + return this.subscribe(request, Responses.decodeNewBlockEvent); } public subscribeNewBlockHeader(): Stream { @@ -238,7 +234,7 @@ export class Tendermint37Client { method: requests.Method.Subscribe, query: { type: requests.SubscriptionEventType.NewBlockHeader }, }; - return this.subscribe(request, this.r.decodeNewBlockHeaderEvent); + return this.subscribe(request, Responses.decodeNewBlockHeaderEvent); } public subscribeTx(query?: string): Stream { @@ -249,7 +245,7 @@ export class Tendermint37Client { raw: query, }, }; - return this.subscribe(request, this.r.decodeTxEvent); + return this.subscribe(request, Responses.decodeTxEvent); } /** @@ -259,7 +255,7 @@ export class Tendermint37Client { */ public async tx(params: requests.TxParams): Promise { const query: requests.TxRequest = { params: params, method: requests.Method.Tx }; - return this.doCall(query, this.p.encodeTx, this.r.decodeTx); + return this.doCall(query, Params.encodeTx, Responses.decodeTx); } /** @@ -269,7 +265,7 @@ export class Tendermint37Client { */ public async txSearch(params: requests.TxSearchParams): Promise { const query: requests.TxSearchRequest = { params: params, method: requests.Method.TxSearch }; - return this.doCall(query, this.p.encodeTxSearch, this.r.decodeTxSearch); + return this.doCall(query, Params.encodeTxSearch, Responses.decodeTxSearch); } // this should paginate through all txSearch options to ensure it returns all results. @@ -300,7 +296,7 @@ export class Tendermint37Client { method: requests.Method.Validators, params: params, }; - return this.doCall(query, this.p.encodeValidators, this.r.decodeValidators); + return this.doCall(query, Params.encodeValidators, Responses.decodeValidators); } public async validatorsAll(height?: number): Promise { @@ -349,7 +345,7 @@ export class Tendermint37Client { throw new Error("This RPC client type cannot subscribe to events"); } - const req = this.p.encodeSubscribe(request); + const req = Params.encodeSubscribe(request); const eventStream = this.client.listen(req); return eventStream.map((event) => { return decode(event);