Remove Adaptor abstractions

This commit is contained in:
Simon Warta 2023-08-24 16:46:19 +02:00
parent 4adfcb0f6a
commit 3124187177
13 changed files with 83 additions and 261 deletions

View File

@ -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

View File

@ -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";

View File

@ -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

View File

@ -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<T extends requests.Request> = (req: T) => JsonRpcRequest;
// Decoder is a generic that matches all methods of Responses
export type Decoder<T extends responses.Response> = (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;
}

View File

@ -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<responses.AbciInfoResponse> {
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<responses.AbciQueryResponse> {
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<responses.BlockResponse> {
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<responses.BlockResultsResponse> {
@ -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<responses.BlockSearchResponse> {
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<responses.BroadcastTxSyncResponse> {
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<responses.BroadcastTxAsyncResponse> {
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<responses.BroadcastTxCommitResponse> {
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<responses.CommitResponse> {
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<responses.GenesisResponse> {
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<responses.HealthResponse> {
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<responses.NumUnconfirmedTxsResponse> {
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<responses.StatusResponse> {
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<responses.NewBlockEvent> {
@ -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<responses.NewBlockHeaderEvent> {
@ -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<responses.TxEvent> {
@ -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<responses.TxResponse> {
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<responses.TxSearchResponse> {
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<responses.ValidatorsResponse> {
@ -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<T>((event) => {
return decode(event);

View File

@ -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";

View File

@ -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

View File

@ -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<T extends requests.Request> = (req: T) => JsonRpcRequest;
// Decoder is a generic that matches all methods of Responses
export type Decoder<T extends responses.Response> = (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;
}

View File

@ -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<responses.AbciInfoResponse> {
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<responses.AbciQueryResponse> {
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<responses.BlockResponse> {
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<responses.BlockResultsResponse> {
@ -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<responses.BlockSearchResponse> {
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<responses.BroadcastTxSyncResponse> {
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<responses.BroadcastTxAsyncResponse> {
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<responses.BroadcastTxCommitResponse> {
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<responses.CommitResponse> {
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<responses.GenesisResponse> {
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<responses.HealthResponse> {
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<responses.NumUnconfirmedTxsResponse> {
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<responses.StatusResponse> {
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<responses.NewBlockEvent> {
@ -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<responses.NewBlockHeaderEvent> {
@ -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<responses.TxEvent> {
@ -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<responses.TxResponse> {
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<responses.TxSearchResponse> {
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<responses.ValidatorsResponse> {
@ -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<T>((event) => {
return decode(event);

View File

@ -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";

View File

@ -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

View File

@ -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<T extends requests.Request> = (req: T) => JsonRpcRequest;
// Decoder is a generic that matches all methods of Responses
export type Decoder<T extends responses.Response> = (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;
}

View File

@ -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<responses.AbciInfoResponse> {
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<responses.AbciQueryResponse> {
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<responses.BlockResponse> {
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<responses.BlockResultsResponse> {
@ -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<responses.BlockSearchResponse> {
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<responses.BroadcastTxSyncResponse> {
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<responses.BroadcastTxAsyncResponse> {
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<responses.BroadcastTxCommitResponse> {
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<responses.CommitResponse> {
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<responses.GenesisResponse> {
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<responses.HealthResponse> {
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<responses.NumUnconfirmedTxsResponse> {
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<responses.StatusResponse> {
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<responses.NewBlockEvent> {
@ -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<responses.NewBlockHeaderEvent> {
@ -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<responses.TxEvent> {
@ -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<responses.TxResponse> {
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<responses.TxSearchResponse> {
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<responses.ValidatorsResponse> {
@ -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<T>((event) => {
return decode(event);