mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
Remove Adaptor abstractions
This commit is contained in:
parent
4adfcb0f6a
commit
3124187177
@ -6,6 +6,11 @@ and this project adheres to
|
|||||||
|
|
||||||
## [Unreleased]
|
## [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
|
## [0.31.1] - 2023-08-21
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
@ -1,10 +1,3 @@
|
|||||||
import { Params } from "./requests";
|
export { Params } from "./requests";
|
||||||
import { Responses } from "./responses";
|
export { Responses } from "./responses";
|
||||||
import { Adaptor } from "./types";
|
export { Decoder, Encoder } from "./types";
|
||||||
|
|
||||||
export { Decoder, Encoder, Params, Responses } from "./types";
|
|
||||||
|
|
||||||
export const adaptor38: Adaptor = {
|
|
||||||
params: Params,
|
|
||||||
responses: Responses,
|
|
||||||
};
|
|
||||||
|
@ -3,7 +3,7 @@ import { fromBase64, fromHex } from "@cosmjs/encoding";
|
|||||||
|
|
||||||
import { decodeEvent, decodeValidatorGenesis, decodeValidatorInfo, decodeValidatorUpdate } from "./responses";
|
import { decodeEvent, decodeValidatorGenesis, decodeValidatorInfo, decodeValidatorUpdate } from "./responses";
|
||||||
|
|
||||||
describe("Adaptor Responses", () => {
|
describe("Responses", () => {
|
||||||
describe("decodeEvent", () => {
|
describe("decodeEvent", () => {
|
||||||
it("works with attributes", () => {
|
it("works with attributes", () => {
|
||||||
// from https://rpc.mainnet-1.tgrade.confio.run/tx?hash=0x2C44715748022DB2FB5F40105383719BFCFCEE51DBC02FF4088BE3F5924CD7BF
|
// from https://rpc.mainnet-1.tgrade.confio.run/tx?hash=0x2C44715748022DB2FB5F40105383719BFCFCEE51DBC02FF4088BE3F5924CD7BF
|
||||||
|
@ -1,60 +1,10 @@
|
|||||||
import { JsonRpcRequest, JsonRpcSuccessResponse } from "@cosmjs/json-rpc";
|
import { JsonRpcRequest, JsonRpcSuccessResponse } from "@cosmjs/json-rpc";
|
||||||
|
|
||||||
import { SubscriptionEvent } from "../../rpcclients";
|
|
||||||
import * as requests from "../requests";
|
import * as requests from "../requests";
|
||||||
import * as responses from "../responses";
|
import * as responses from "../responses";
|
||||||
|
|
||||||
export interface Adaptor {
|
|
||||||
readonly params: Params;
|
|
||||||
readonly responses: Responses;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encoder is a generic that matches all methods of Params
|
// Encoder is a generic that matches all methods of Params
|
||||||
export type Encoder<T extends requests.Request> = (req: T) => JsonRpcRequest;
|
export type Encoder<T extends requests.Request> = (req: T) => JsonRpcRequest;
|
||||||
|
|
||||||
// Decoder is a generic that matches all methods of Responses
|
// Decoder is a generic that matches all methods of Responses
|
||||||
export type Decoder<T extends responses.Response> = (res: JsonRpcSuccessResponse) => T;
|
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;
|
|
||||||
}
|
|
||||||
|
@ -9,7 +9,7 @@ import {
|
|||||||
SubscriptionEvent,
|
SubscriptionEvent,
|
||||||
WebsocketClient,
|
WebsocketClient,
|
||||||
} from "../rpcclients";
|
} from "../rpcclients";
|
||||||
import { adaptor38, Decoder, Encoder, Params, Responses } from "./adaptor";
|
import { Decoder, Encoder, Params, Responses } from "./adaptor";
|
||||||
import * as requests from "./requests";
|
import * as requests from "./requests";
|
||||||
import * as responses from "./responses";
|
import * as responses from "./responses";
|
||||||
|
|
||||||
@ -61,16 +61,12 @@ export class Comet38Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private readonly client: RpcClient;
|
private readonly client: RpcClient;
|
||||||
private readonly p: Params;
|
|
||||||
private readonly r: Responses;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use `Tendermint37Client.connect` or `Tendermint37Client.create` to create an instance.
|
* Use `Tendermint37Client.connect` or `Tendermint37Client.create` to create an instance.
|
||||||
*/
|
*/
|
||||||
private constructor(client: RpcClient) {
|
private constructor(client: RpcClient) {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.p = adaptor38.params;
|
|
||||||
this.r = adaptor38.responses;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public disconnect(): void {
|
public disconnect(): void {
|
||||||
@ -79,17 +75,17 @@ export class Comet38Client {
|
|||||||
|
|
||||||
public async abciInfo(): Promise<responses.AbciInfoResponse> {
|
public async abciInfo(): Promise<responses.AbciInfoResponse> {
|
||||||
const query: requests.AbciInfoRequest = { method: requests.Method.AbciInfo };
|
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> {
|
public async abciQuery(params: requests.AbciQueryParams): Promise<responses.AbciQueryResponse> {
|
||||||
const query: requests.AbciQueryRequest = { params: params, method: requests.Method.AbciQuery };
|
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> {
|
public async block(height?: number): Promise<responses.BlockResponse> {
|
||||||
const query: requests.BlockRequest = { method: requests.Method.Block, params: { height: height } };
|
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> {
|
public async blockResults(height?: number): Promise<responses.BlockResultsResponse> {
|
||||||
@ -97,7 +93,7 @@ export class Comet38Client {
|
|||||||
method: requests.Method.BlockResults,
|
method: requests.Method.BlockResults,
|
||||||
params: { height: height },
|
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> {
|
public async blockSearch(params: requests.BlockSearchParams): Promise<responses.BlockSearchResponse> {
|
||||||
const query: requests.BlockSearchRequest = { params: params, method: requests.Method.BlockSearch };
|
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 {
|
return {
|
||||||
...resp,
|
...resp,
|
||||||
// make sure we sort by height, as tendermint may be sorting by string value of the height
|
// 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,
|
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,
|
params: requests.BroadcastTxParams,
|
||||||
): Promise<responses.BroadcastTxSyncResponse> {
|
): Promise<responses.BroadcastTxSyncResponse> {
|
||||||
const query: requests.BroadcastTxRequest = { params: params, method: requests.Method.BroadcastTxSync };
|
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,
|
params: requests.BroadcastTxParams,
|
||||||
): Promise<responses.BroadcastTxAsyncResponse> {
|
): Promise<responses.BroadcastTxAsyncResponse> {
|
||||||
const query: requests.BroadcastTxRequest = { params: params, method: requests.Method.BroadcastTxAsync };
|
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,
|
params: requests.BroadcastTxParams,
|
||||||
): Promise<responses.BroadcastTxCommitResponse> {
|
): Promise<responses.BroadcastTxCommitResponse> {
|
||||||
const query: requests.BroadcastTxRequest = { params: params, method: requests.Method.BroadcastTxCommit };
|
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> {
|
public async commit(height?: number): Promise<responses.CommitResponse> {
|
||||||
const query: requests.CommitRequest = { method: requests.Method.Commit, params: { height: height } };
|
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> {
|
public async genesis(): Promise<responses.GenesisResponse> {
|
||||||
const query: requests.GenesisRequest = { method: requests.Method.Genesis };
|
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> {
|
public async health(): Promise<responses.HealthResponse> {
|
||||||
const query: requests.HealthRequest = { method: requests.Method.Health };
|
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> {
|
public async numUnconfirmedTxs(): Promise<responses.NumUnconfirmedTxsResponse> {
|
||||||
const query: requests.NumUnconfirmedTxsRequest = { method: requests.Method.NumUnconfirmedTxs };
|
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> {
|
public async status(): Promise<responses.StatusResponse> {
|
||||||
const query: requests.StatusRequest = { method: requests.Method.Status };
|
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> {
|
public subscribeNewBlock(): Stream<responses.NewBlockEvent> {
|
||||||
@ -230,7 +226,7 @@ export class Comet38Client {
|
|||||||
method: requests.Method.Subscribe,
|
method: requests.Method.Subscribe,
|
||||||
query: { type: requests.SubscriptionEventType.NewBlock },
|
query: { type: requests.SubscriptionEventType.NewBlock },
|
||||||
};
|
};
|
||||||
return this.subscribe(request, this.r.decodeNewBlockEvent);
|
return this.subscribe(request, Responses.decodeNewBlockEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public subscribeNewBlockHeader(): Stream<responses.NewBlockHeaderEvent> {
|
public subscribeNewBlockHeader(): Stream<responses.NewBlockHeaderEvent> {
|
||||||
@ -238,7 +234,7 @@ export class Comet38Client {
|
|||||||
method: requests.Method.Subscribe,
|
method: requests.Method.Subscribe,
|
||||||
query: { type: requests.SubscriptionEventType.NewBlockHeader },
|
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> {
|
public subscribeTx(query?: string): Stream<responses.TxEvent> {
|
||||||
@ -249,7 +245,7 @@ export class Comet38Client {
|
|||||||
raw: query,
|
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> {
|
public async tx(params: requests.TxParams): Promise<responses.TxResponse> {
|
||||||
const query: requests.TxRequest = { params: params, method: requests.Method.Tx };
|
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> {
|
public async txSearch(params: requests.TxSearchParams): Promise<responses.TxSearchResponse> {
|
||||||
const query: requests.TxSearchRequest = { params: params, method: requests.Method.TxSearch };
|
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.
|
// this should paginate through all txSearch options to ensure it returns all results.
|
||||||
@ -300,7 +296,7 @@ export class Comet38Client {
|
|||||||
method: requests.Method.Validators,
|
method: requests.Method.Validators,
|
||||||
params: params,
|
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> {
|
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");
|
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);
|
const eventStream = this.client.listen(req);
|
||||||
return eventStream.map<T>((event) => {
|
return eventStream.map<T>((event) => {
|
||||||
return decode(event);
|
return decode(event);
|
||||||
|
@ -1,10 +1,3 @@
|
|||||||
import { Params } from "./requests";
|
export { Params } from "./requests";
|
||||||
import { Responses } from "./responses";
|
export { Responses } from "./responses";
|
||||||
import { Adaptor } from "./types";
|
export { Decoder, Encoder } from "./types";
|
||||||
|
|
||||||
export { Decoder, Encoder, Params, Responses } from "./types";
|
|
||||||
|
|
||||||
export const adaptor34: Adaptor = {
|
|
||||||
params: Params,
|
|
||||||
responses: Responses,
|
|
||||||
};
|
|
||||||
|
@ -3,7 +3,7 @@ import { fromBase64, fromHex, toUtf8 } from "@cosmjs/encoding";
|
|||||||
|
|
||||||
import { decodeEvent, decodeValidatorGenesis, decodeValidatorInfo, decodeValidatorUpdate } from "./responses";
|
import { decodeEvent, decodeValidatorGenesis, decodeValidatorInfo, decodeValidatorUpdate } from "./responses";
|
||||||
|
|
||||||
describe("Adaptor Responses", () => {
|
describe("Responses", () => {
|
||||||
describe("decodeEvent", () => {
|
describe("decodeEvent", () => {
|
||||||
it("works with attributes", () => {
|
it("works with attributes", () => {
|
||||||
// from https://rpc.mainnet-1.tgrade.confio.run/tx?hash=0x2C44715748022DB2FB5F40105383719BFCFCEE51DBC02FF4088BE3F5924CD7BF
|
// from https://rpc.mainnet-1.tgrade.confio.run/tx?hash=0x2C44715748022DB2FB5F40105383719BFCFCEE51DBC02FF4088BE3F5924CD7BF
|
||||||
|
@ -1,60 +1,10 @@
|
|||||||
import { JsonRpcRequest, JsonRpcSuccessResponse } from "@cosmjs/json-rpc";
|
import { JsonRpcRequest, JsonRpcSuccessResponse } from "@cosmjs/json-rpc";
|
||||||
|
|
||||||
import { SubscriptionEvent } from "../../rpcclients";
|
|
||||||
import * as requests from "../requests";
|
import * as requests from "../requests";
|
||||||
import * as responses from "../responses";
|
import * as responses from "../responses";
|
||||||
|
|
||||||
export interface Adaptor {
|
|
||||||
readonly params: Params;
|
|
||||||
readonly responses: Responses;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encoder is a generic that matches all methods of Params
|
// Encoder is a generic that matches all methods of Params
|
||||||
export type Encoder<T extends requests.Request> = (req: T) => JsonRpcRequest;
|
export type Encoder<T extends requests.Request> = (req: T) => JsonRpcRequest;
|
||||||
|
|
||||||
// Decoder is a generic that matches all methods of Responses
|
// Decoder is a generic that matches all methods of Responses
|
||||||
export type Decoder<T extends responses.Response> = (res: JsonRpcSuccessResponse) => T;
|
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;
|
|
||||||
}
|
|
||||||
|
@ -9,7 +9,7 @@ import {
|
|||||||
SubscriptionEvent,
|
SubscriptionEvent,
|
||||||
WebsocketClient,
|
WebsocketClient,
|
||||||
} from "../rpcclients";
|
} from "../rpcclients";
|
||||||
import { adaptor34, Decoder, Encoder, Params, Responses } from "./adaptor";
|
import { Decoder, Encoder, Params, Responses } from "./adaptor";
|
||||||
import * as requests from "./requests";
|
import * as requests from "./requests";
|
||||||
import * as responses from "./responses";
|
import * as responses from "./responses";
|
||||||
|
|
||||||
@ -61,16 +61,12 @@ export class Tendermint34Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private readonly client: RpcClient;
|
private readonly client: RpcClient;
|
||||||
private readonly p: Params;
|
|
||||||
private readonly r: Responses;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use `Tendermint34Client.connect` or `Tendermint34Client.create` to create an instance.
|
* Use `Tendermint34Client.connect` or `Tendermint34Client.create` to create an instance.
|
||||||
*/
|
*/
|
||||||
private constructor(client: RpcClient) {
|
private constructor(client: RpcClient) {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.p = adaptor34.params;
|
|
||||||
this.r = adaptor34.responses;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public disconnect(): void {
|
public disconnect(): void {
|
||||||
@ -79,17 +75,17 @@ export class Tendermint34Client {
|
|||||||
|
|
||||||
public async abciInfo(): Promise<responses.AbciInfoResponse> {
|
public async abciInfo(): Promise<responses.AbciInfoResponse> {
|
||||||
const query: requests.AbciInfoRequest = { method: requests.Method.AbciInfo };
|
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> {
|
public async abciQuery(params: requests.AbciQueryParams): Promise<responses.AbciQueryResponse> {
|
||||||
const query: requests.AbciQueryRequest = { params: params, method: requests.Method.AbciQuery };
|
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> {
|
public async block(height?: number): Promise<responses.BlockResponse> {
|
||||||
const query: requests.BlockRequest = { method: requests.Method.Block, params: { height: height } };
|
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> {
|
public async blockResults(height?: number): Promise<responses.BlockResultsResponse> {
|
||||||
@ -97,7 +93,7 @@ export class Tendermint34Client {
|
|||||||
method: requests.Method.BlockResults,
|
method: requests.Method.BlockResults,
|
||||||
params: { height: height },
|
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> {
|
public async blockSearch(params: requests.BlockSearchParams): Promise<responses.BlockSearchResponse> {
|
||||||
const query: requests.BlockSearchRequest = { params: params, method: requests.Method.BlockSearch };
|
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 {
|
return {
|
||||||
...resp,
|
...resp,
|
||||||
// make sure we sort by height, as tendermint may be sorting by string value of the height
|
// 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,
|
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,
|
params: requests.BroadcastTxParams,
|
||||||
): Promise<responses.BroadcastTxSyncResponse> {
|
): Promise<responses.BroadcastTxSyncResponse> {
|
||||||
const query: requests.BroadcastTxRequest = { params: params, method: requests.Method.BroadcastTxSync };
|
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,
|
params: requests.BroadcastTxParams,
|
||||||
): Promise<responses.BroadcastTxAsyncResponse> {
|
): Promise<responses.BroadcastTxAsyncResponse> {
|
||||||
const query: requests.BroadcastTxRequest = { params: params, method: requests.Method.BroadcastTxAsync };
|
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,
|
params: requests.BroadcastTxParams,
|
||||||
): Promise<responses.BroadcastTxCommitResponse> {
|
): Promise<responses.BroadcastTxCommitResponse> {
|
||||||
const query: requests.BroadcastTxRequest = { params: params, method: requests.Method.BroadcastTxCommit };
|
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> {
|
public async commit(height?: number): Promise<responses.CommitResponse> {
|
||||||
const query: requests.CommitRequest = { method: requests.Method.Commit, params: { height: height } };
|
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> {
|
public async genesis(): Promise<responses.GenesisResponse> {
|
||||||
const query: requests.GenesisRequest = { method: requests.Method.Genesis };
|
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> {
|
public async health(): Promise<responses.HealthResponse> {
|
||||||
const query: requests.HealthRequest = { method: requests.Method.Health };
|
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> {
|
public async numUnconfirmedTxs(): Promise<responses.NumUnconfirmedTxsResponse> {
|
||||||
const query: requests.NumUnconfirmedTxsRequest = { method: requests.Method.NumUnconfirmedTxs };
|
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> {
|
public async status(): Promise<responses.StatusResponse> {
|
||||||
const query: requests.StatusRequest = { method: requests.Method.Status };
|
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> {
|
public subscribeNewBlock(): Stream<responses.NewBlockEvent> {
|
||||||
@ -230,7 +226,7 @@ export class Tendermint34Client {
|
|||||||
method: requests.Method.Subscribe,
|
method: requests.Method.Subscribe,
|
||||||
query: { type: requests.SubscriptionEventType.NewBlock },
|
query: { type: requests.SubscriptionEventType.NewBlock },
|
||||||
};
|
};
|
||||||
return this.subscribe(request, this.r.decodeNewBlockEvent);
|
return this.subscribe(request, Responses.decodeNewBlockEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public subscribeNewBlockHeader(): Stream<responses.NewBlockHeaderEvent> {
|
public subscribeNewBlockHeader(): Stream<responses.NewBlockHeaderEvent> {
|
||||||
@ -238,7 +234,7 @@ export class Tendermint34Client {
|
|||||||
method: requests.Method.Subscribe,
|
method: requests.Method.Subscribe,
|
||||||
query: { type: requests.SubscriptionEventType.NewBlockHeader },
|
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> {
|
public subscribeTx(query?: string): Stream<responses.TxEvent> {
|
||||||
@ -249,7 +245,7 @@ export class Tendermint34Client {
|
|||||||
raw: query,
|
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> {
|
public async tx(params: requests.TxParams): Promise<responses.TxResponse> {
|
||||||
const query: requests.TxRequest = { params: params, method: requests.Method.Tx };
|
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> {
|
public async txSearch(params: requests.TxSearchParams): Promise<responses.TxSearchResponse> {
|
||||||
const query: requests.TxSearchRequest = { params: params, method: requests.Method.TxSearch };
|
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.
|
// this should paginate through all txSearch options to ensure it returns all results.
|
||||||
@ -300,7 +296,7 @@ export class Tendermint34Client {
|
|||||||
method: requests.Method.Validators,
|
method: requests.Method.Validators,
|
||||||
params: params,
|
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> {
|
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");
|
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);
|
const eventStream = this.client.listen(req);
|
||||||
return eventStream.map<T>((event) => {
|
return eventStream.map<T>((event) => {
|
||||||
return decode(event);
|
return decode(event);
|
||||||
|
@ -1,10 +1,3 @@
|
|||||||
import { Params } from "./requests";
|
export { Params } from "./requests";
|
||||||
import { Responses } from "./responses";
|
export { Responses } from "./responses";
|
||||||
import { Adaptor } from "./types";
|
export { Decoder, Encoder } from "./types";
|
||||||
|
|
||||||
export { Decoder, Encoder, Params, Responses } from "./types";
|
|
||||||
|
|
||||||
export const adaptor37: Adaptor = {
|
|
||||||
params: Params,
|
|
||||||
responses: Responses,
|
|
||||||
};
|
|
||||||
|
@ -3,7 +3,7 @@ import { fromBase64, fromHex } from "@cosmjs/encoding";
|
|||||||
|
|
||||||
import { decodeEvent, decodeValidatorGenesis, decodeValidatorInfo, decodeValidatorUpdate } from "./responses";
|
import { decodeEvent, decodeValidatorGenesis, decodeValidatorInfo, decodeValidatorUpdate } from "./responses";
|
||||||
|
|
||||||
describe("Adaptor Responses", () => {
|
describe("Responses", () => {
|
||||||
describe("decodeEvent", () => {
|
describe("decodeEvent", () => {
|
||||||
it("works with attributes", () => {
|
it("works with attributes", () => {
|
||||||
// from https://rpc.mainnet-1.tgrade.confio.run/tx?hash=0x2C44715748022DB2FB5F40105383719BFCFCEE51DBC02FF4088BE3F5924CD7BF
|
// from https://rpc.mainnet-1.tgrade.confio.run/tx?hash=0x2C44715748022DB2FB5F40105383719BFCFCEE51DBC02FF4088BE3F5924CD7BF
|
||||||
|
@ -1,60 +1,10 @@
|
|||||||
import { JsonRpcRequest, JsonRpcSuccessResponse } from "@cosmjs/json-rpc";
|
import { JsonRpcRequest, JsonRpcSuccessResponse } from "@cosmjs/json-rpc";
|
||||||
|
|
||||||
import { SubscriptionEvent } from "../../rpcclients";
|
|
||||||
import * as requests from "../requests";
|
import * as requests from "../requests";
|
||||||
import * as responses from "../responses";
|
import * as responses from "../responses";
|
||||||
|
|
||||||
export interface Adaptor {
|
|
||||||
readonly params: Params;
|
|
||||||
readonly responses: Responses;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encoder is a generic that matches all methods of Params
|
// Encoder is a generic that matches all methods of Params
|
||||||
export type Encoder<T extends requests.Request> = (req: T) => JsonRpcRequest;
|
export type Encoder<T extends requests.Request> = (req: T) => JsonRpcRequest;
|
||||||
|
|
||||||
// Decoder is a generic that matches all methods of Responses
|
// Decoder is a generic that matches all methods of Responses
|
||||||
export type Decoder<T extends responses.Response> = (res: JsonRpcSuccessResponse) => T;
|
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;
|
|
||||||
}
|
|
||||||
|
@ -9,7 +9,7 @@ import {
|
|||||||
SubscriptionEvent,
|
SubscriptionEvent,
|
||||||
WebsocketClient,
|
WebsocketClient,
|
||||||
} from "../rpcclients";
|
} from "../rpcclients";
|
||||||
import { adaptor37, Decoder, Encoder, Params, Responses } from "./adaptor";
|
import { Decoder, Encoder, Params, Responses } from "./adaptor";
|
||||||
import * as requests from "./requests";
|
import * as requests from "./requests";
|
||||||
import * as responses from "./responses";
|
import * as responses from "./responses";
|
||||||
|
|
||||||
@ -61,16 +61,12 @@ export class Tendermint37Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private readonly client: RpcClient;
|
private readonly client: RpcClient;
|
||||||
private readonly p: Params;
|
|
||||||
private readonly r: Responses;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use `Tendermint37Client.connect` or `Tendermint37Client.create` to create an instance.
|
* Use `Tendermint37Client.connect` or `Tendermint37Client.create` to create an instance.
|
||||||
*/
|
*/
|
||||||
private constructor(client: RpcClient) {
|
private constructor(client: RpcClient) {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.p = adaptor37.params;
|
|
||||||
this.r = adaptor37.responses;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public disconnect(): void {
|
public disconnect(): void {
|
||||||
@ -79,17 +75,17 @@ export class Tendermint37Client {
|
|||||||
|
|
||||||
public async abciInfo(): Promise<responses.AbciInfoResponse> {
|
public async abciInfo(): Promise<responses.AbciInfoResponse> {
|
||||||
const query: requests.AbciInfoRequest = { method: requests.Method.AbciInfo };
|
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> {
|
public async abciQuery(params: requests.AbciQueryParams): Promise<responses.AbciQueryResponse> {
|
||||||
const query: requests.AbciQueryRequest = { params: params, method: requests.Method.AbciQuery };
|
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> {
|
public async block(height?: number): Promise<responses.BlockResponse> {
|
||||||
const query: requests.BlockRequest = { method: requests.Method.Block, params: { height: height } };
|
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> {
|
public async blockResults(height?: number): Promise<responses.BlockResultsResponse> {
|
||||||
@ -97,7 +93,7 @@ export class Tendermint37Client {
|
|||||||
method: requests.Method.BlockResults,
|
method: requests.Method.BlockResults,
|
||||||
params: { height: height },
|
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> {
|
public async blockSearch(params: requests.BlockSearchParams): Promise<responses.BlockSearchResponse> {
|
||||||
const query: requests.BlockSearchRequest = { params: params, method: requests.Method.BlockSearch };
|
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 {
|
return {
|
||||||
...resp,
|
...resp,
|
||||||
// make sure we sort by height, as tendermint may be sorting by string value of the height
|
// 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,
|
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,
|
params: requests.BroadcastTxParams,
|
||||||
): Promise<responses.BroadcastTxSyncResponse> {
|
): Promise<responses.BroadcastTxSyncResponse> {
|
||||||
const query: requests.BroadcastTxRequest = { params: params, method: requests.Method.BroadcastTxSync };
|
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,
|
params: requests.BroadcastTxParams,
|
||||||
): Promise<responses.BroadcastTxAsyncResponse> {
|
): Promise<responses.BroadcastTxAsyncResponse> {
|
||||||
const query: requests.BroadcastTxRequest = { params: params, method: requests.Method.BroadcastTxAsync };
|
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,
|
params: requests.BroadcastTxParams,
|
||||||
): Promise<responses.BroadcastTxCommitResponse> {
|
): Promise<responses.BroadcastTxCommitResponse> {
|
||||||
const query: requests.BroadcastTxRequest = { params: params, method: requests.Method.BroadcastTxCommit };
|
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> {
|
public async commit(height?: number): Promise<responses.CommitResponse> {
|
||||||
const query: requests.CommitRequest = { method: requests.Method.Commit, params: { height: height } };
|
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> {
|
public async genesis(): Promise<responses.GenesisResponse> {
|
||||||
const query: requests.GenesisRequest = { method: requests.Method.Genesis };
|
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> {
|
public async health(): Promise<responses.HealthResponse> {
|
||||||
const query: requests.HealthRequest = { method: requests.Method.Health };
|
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> {
|
public async numUnconfirmedTxs(): Promise<responses.NumUnconfirmedTxsResponse> {
|
||||||
const query: requests.NumUnconfirmedTxsRequest = { method: requests.Method.NumUnconfirmedTxs };
|
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> {
|
public async status(): Promise<responses.StatusResponse> {
|
||||||
const query: requests.StatusRequest = { method: requests.Method.Status };
|
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> {
|
public subscribeNewBlock(): Stream<responses.NewBlockEvent> {
|
||||||
@ -230,7 +226,7 @@ export class Tendermint37Client {
|
|||||||
method: requests.Method.Subscribe,
|
method: requests.Method.Subscribe,
|
||||||
query: { type: requests.SubscriptionEventType.NewBlock },
|
query: { type: requests.SubscriptionEventType.NewBlock },
|
||||||
};
|
};
|
||||||
return this.subscribe(request, this.r.decodeNewBlockEvent);
|
return this.subscribe(request, Responses.decodeNewBlockEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public subscribeNewBlockHeader(): Stream<responses.NewBlockHeaderEvent> {
|
public subscribeNewBlockHeader(): Stream<responses.NewBlockHeaderEvent> {
|
||||||
@ -238,7 +234,7 @@ export class Tendermint37Client {
|
|||||||
method: requests.Method.Subscribe,
|
method: requests.Method.Subscribe,
|
||||||
query: { type: requests.SubscriptionEventType.NewBlockHeader },
|
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> {
|
public subscribeTx(query?: string): Stream<responses.TxEvent> {
|
||||||
@ -249,7 +245,7 @@ export class Tendermint37Client {
|
|||||||
raw: query,
|
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> {
|
public async tx(params: requests.TxParams): Promise<responses.TxResponse> {
|
||||||
const query: requests.TxRequest = { params: params, method: requests.Method.Tx };
|
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> {
|
public async txSearch(params: requests.TxSearchParams): Promise<responses.TxSearchResponse> {
|
||||||
const query: requests.TxSearchRequest = { params: params, method: requests.Method.TxSearch };
|
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.
|
// this should paginate through all txSearch options to ensure it returns all results.
|
||||||
@ -300,7 +296,7 @@ export class Tendermint37Client {
|
|||||||
method: requests.Method.Validators,
|
method: requests.Method.Validators,
|
||||||
params: params,
|
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> {
|
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");
|
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);
|
const eventStream = this.client.listen(req);
|
||||||
return eventStream.map<T>((event) => {
|
return eventStream.map<T>((event) => {
|
||||||
return decode(event);
|
return decode(event);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user