import { Rpc } from "../../../../helpers"; import { BinaryReader } from "../../../../binary"; import { QueryClient, createProtobufRpcClient } from "@cosmjs/stargate"; import { GetNodeInfoRequest, GetNodeInfoResponse, GetSyncingRequest, GetSyncingResponse, GetLatestBlockRequest, GetLatestBlockResponse, GetBlockByHeightRequest, GetBlockByHeightResponse, GetLatestValidatorSetRequest, GetLatestValidatorSetResponse, GetValidatorSetByHeightRequest, GetValidatorSetByHeightResponse } from "./query"; /** Service defines the gRPC querier service for tendermint queries. */ export interface Service { /** GetNodeInfo queries the current node info. */ getNodeInfo(request?: GetNodeInfoRequest): Promise; /** GetSyncing queries node syncing. */ getSyncing(request?: GetSyncingRequest): Promise; /** GetLatestBlock returns the latest block. */ getLatestBlock(request?: GetLatestBlockRequest): Promise; /** GetBlockByHeight queries block for given height. */ getBlockByHeight(request: GetBlockByHeightRequest): Promise; /** GetLatestValidatorSet queries latest validator-set. */ getLatestValidatorSet(request?: GetLatestValidatorSetRequest): Promise; /** GetValidatorSetByHeight queries validator-set at a given height. */ getValidatorSetByHeight(request: GetValidatorSetByHeightRequest): Promise; } export class ServiceClientImpl implements Service { private readonly rpc: Rpc; constructor(rpc: Rpc) { this.rpc = rpc; this.getNodeInfo = this.getNodeInfo.bind(this); this.getSyncing = this.getSyncing.bind(this); this.getLatestBlock = this.getLatestBlock.bind(this); this.getBlockByHeight = this.getBlockByHeight.bind(this); this.getLatestValidatorSet = this.getLatestValidatorSet.bind(this); this.getValidatorSetByHeight = this.getValidatorSetByHeight.bind(this); } getNodeInfo(request: GetNodeInfoRequest = {}): Promise { const data = GetNodeInfoRequest.encode(request).finish(); const promise = this.rpc.request("cosmos.base.tendermint.v1beta1.Service", "GetNodeInfo", data); return promise.then(data => GetNodeInfoResponse.decode(new BinaryReader(data))); } getSyncing(request: GetSyncingRequest = {}): Promise { const data = GetSyncingRequest.encode(request).finish(); const promise = this.rpc.request("cosmos.base.tendermint.v1beta1.Service", "GetSyncing", data); return promise.then(data => GetSyncingResponse.decode(new BinaryReader(data))); } getLatestBlock(request: GetLatestBlockRequest = {}): Promise { const data = GetLatestBlockRequest.encode(request).finish(); const promise = this.rpc.request("cosmos.base.tendermint.v1beta1.Service", "GetLatestBlock", data); return promise.then(data => GetLatestBlockResponse.decode(new BinaryReader(data))); } getBlockByHeight(request: GetBlockByHeightRequest): Promise { const data = GetBlockByHeightRequest.encode(request).finish(); const promise = this.rpc.request("cosmos.base.tendermint.v1beta1.Service", "GetBlockByHeight", data); return promise.then(data => GetBlockByHeightResponse.decode(new BinaryReader(data))); } getLatestValidatorSet(request: GetLatestValidatorSetRequest = { pagination: undefined }): Promise { const data = GetLatestValidatorSetRequest.encode(request).finish(); const promise = this.rpc.request("cosmos.base.tendermint.v1beta1.Service", "GetLatestValidatorSet", data); return promise.then(data => GetLatestValidatorSetResponse.decode(new BinaryReader(data))); } getValidatorSetByHeight(request: GetValidatorSetByHeightRequest): Promise { const data = GetValidatorSetByHeightRequest.encode(request).finish(); const promise = this.rpc.request("cosmos.base.tendermint.v1beta1.Service", "GetValidatorSetByHeight", data); return promise.then(data => GetValidatorSetByHeightResponse.decode(new BinaryReader(data))); } } export const createRpcQueryExtension = (base: QueryClient) => { const rpc = createProtobufRpcClient(base); const queryService = new ServiceClientImpl(rpc); return { getNodeInfo(request?: GetNodeInfoRequest): Promise { return queryService.getNodeInfo(request); }, getSyncing(request?: GetSyncingRequest): Promise { return queryService.getSyncing(request); }, getLatestBlock(request?: GetLatestBlockRequest): Promise { return queryService.getLatestBlock(request); }, getBlockByHeight(request: GetBlockByHeightRequest): Promise { return queryService.getBlockByHeight(request); }, getLatestValidatorSet(request?: GetLatestValidatorSetRequest): Promise { return queryService.getLatestValidatorSet(request); }, getValidatorSetByHeight(request: GetValidatorSetByHeightRequest): Promise { return queryService.getValidatorSetByHeight(request); } }; };