stargate: Update queries to use generic RPC

This commit is contained in:
willclarktech 2021-02-03 14:57:06 +00:00
parent 4834c00d92
commit 4b992fc81a
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
11 changed files with 48 additions and 55 deletions

View File

@ -3,6 +3,7 @@ export { parseRawLog } from "./logs";
export {
AuthExtension,
BankExtension,
createRpc,
DistributionExtension,
IbcExtension,
QueryClient,

View File

@ -4,3 +4,4 @@ export { BankExtension, setupBankExtension } from "./bank";
export { DistributionExtension, setupDistributionExtension } from "./distribution";
export { IbcExtension, setupIbcExtension } from "./ibc";
export { setupStakingExtension, StakingExtension } from "./staking";
export { createRpc } from "./utils";

View File

@ -1,4 +1,5 @@
import Long from "long";
import { QueryClient } from "./queryclient";
/**
* Takes a bech32 encoded address and returns the data part. The prefix is ignored and discarded.
* This is called AccAddress in Cosmos SDK, which is basically an alias for raw binary data.
@ -18,3 +19,8 @@ export declare function createPagination(
readonly limit: Long;
readonly countTotal: boolean;
};
interface Rpc {
request(service: string, method: string, data: Uint8Array): Promise<Uint8Array>;
}
export declare function createRpc(base: QueryClient): Rpc;
export {};

View File

@ -3,6 +3,7 @@ export { parseRawLog } from "./logs";
export {
AuthExtension,
BankExtension,
createRpc,
DistributionExtension,
IbcExtension,
QueryClient,

View File

@ -5,7 +5,7 @@ import { BaseAccount } from "../codec/cosmos/auth/v1beta1/auth";
import { QueryClientImpl } from "../codec/cosmos/auth/v1beta1/query";
import { Any } from "../codec/google/protobuf/any";
import { QueryClient } from "./queryclient";
import { toAccAddress, toObject } from "./utils";
import { createRpc, toAccAddress, toObject } from "./utils";
export interface AuthExtension {
readonly auth: {
@ -17,15 +17,10 @@ export interface AuthExtension {
}
export function setupAuthExtension(base: QueryClient): AuthExtension {
const rpc = createRpc(base);
// Use this service to get easy typed access to query methods
// This cannot be used to for proof verification
const queryService = new QueryClientImpl({
request: (service: string, method: string, data: Uint8Array): Promise<Uint8Array> => {
// Parts of the path are unavailable, so we hardcode them here. See https://github.com/protobufjs/protobuf.js/issues/1229
const path = `/cosmos.auth.v1beta1.Query/${method}`;
return base.queryUnverified(path, data);
},
});
// This cannot be used for proof verification
const queryService = new QueryClientImpl(rpc);
return {
auth: {

View File

@ -5,7 +5,7 @@ import { assert } from "@cosmjs/utils";
import { QueryClientImpl } from "../codec/cosmos/bank/v1beta1/query";
import { Coin } from "../codec/cosmos/base/v1beta1/coin";
import { QueryClient } from "./queryclient";
import { toAccAddress, toObject } from "./utils";
import { createRpc, toAccAddress, toObject } from "./utils";
export interface BankExtension {
readonly bank: {
@ -20,15 +20,10 @@ export interface BankExtension {
}
export function setupBankExtension(base: QueryClient): BankExtension {
const rpc = createRpc(base);
// Use this service to get easy typed access to query methods
// This cannot be used to for proof verification
const queryService = new QueryClientImpl({
request: (service: string, method: string, data: Uint8Array): Promise<Uint8Array> => {
// Parts of the path are unavailable, so we hardcode them here. See https://github.com/protobufjs/protobuf.js/issues/1229
const path = `/cosmos.bank.v1beta1.Query/${method}`;
return base.queryUnverified(path, data);
},
});
// This cannot be used for proof verification
const queryService = new QueryClientImpl(rpc);
return {
bank: {

View File

@ -14,7 +14,7 @@ import {
QueryValidatorSlashesResponse,
} from "../codec/cosmos/distribution/v1beta1/query";
import { QueryClient } from "./queryclient";
import { createPagination, toObject } from "./utils";
import { createPagination, createRpc, toObject } from "./utils";
export interface DistributionExtension {
readonly distribution: {
@ -43,15 +43,11 @@ export interface DistributionExtension {
}
export function setupDistributionExtension(base: QueryClient): DistributionExtension {
const rpc = createRpc(base);
// Use this service to get easy typed access to query methods
// This cannot be used to for proof verification
const queryService = new QueryClientImpl({
request: (service: string, method: string, data: Uint8Array): Promise<Uint8Array> => {
// Parts of the path are unavailable, so we hardcode them here. See https://github.com/protobufjs/protobuf.js/issues/1229
const path = `/cosmos.distribution.v1beta1.Query/${method}`;
return base.queryUnverified(path, data);
},
});
// This cannot be used for proof verification
const queryService = new QueryClientImpl(rpc);
return {
distribution: {
unverified: {

View File

@ -24,7 +24,7 @@ import {
QueryConnectionsResponse,
} from "../codec/ibc/core/connection/v1/query";
import { QueryClient } from "./queryclient";
import { createPagination, toObject } from "./utils";
import { createPagination, createRpc, toObject } from "./utils";
export interface IbcExtension {
readonly ibc: {
@ -89,24 +89,11 @@ export interface IbcExtension {
}
export function setupIbcExtension(base: QueryClient): IbcExtension {
// Use this service to get easy typed access to query methods
// This cannot be used to for proof verification
const channelQueryService = new ChannelQuery({
request: (service: string, method: string, data: Uint8Array): Promise<Uint8Array> => {
// Parts of the path are unavailable, so we hardcode them here. See https://github.com/protobufjs/protobuf.js/issues/1229
const path = `/ibc.core.channel.v1.Query/${method}`;
return base.queryUnverified(path, data);
},
});
const connectionQueryService = new ConnectionQuery({
request: (service: string, method: string, data: Uint8Array): Promise<Uint8Array> => {
// Parts of the path are unavailable, so we hardcode them here. See https://github.com/protobufjs/protobuf.js/issues/1229
const path = `/ibc.core.connection.v1.Query/${method}`;
return base.queryUnverified(path, data);
},
});
const rpc = createRpc(base);
// Use these services to get easy typed access to query methods
// These cannot be used for proof verification
const channelQueryService = new ChannelQuery(rpc);
const connectionQueryService = new ConnectionQuery(rpc);
return {
ibc: {

View File

@ -9,3 +9,4 @@ export { BankExtension, setupBankExtension } from "./bank";
export { DistributionExtension, setupDistributionExtension } from "./distribution";
export { IbcExtension, setupIbcExtension } from "./ibc";
export { setupStakingExtension, StakingExtension } from "./staking";
export { createRpc } from "./utils";

View File

@ -20,7 +20,7 @@ import {
} from "../codec/cosmos/staking/v1beta1/query";
import { BondStatus } from "../codec/cosmos/staking/v1beta1/staking";
import { QueryClient } from "./queryclient";
import { createPagination, toObject } from "./utils";
import { createPagination, createRpc, toObject } from "./utils";
export type BondStatusString = Exclude<keyof typeof BondStatus, "BOND_STATUS_UNSPECIFIED">;
@ -73,14 +73,9 @@ export interface StakingExtension {
export function setupStakingExtension(base: QueryClient): StakingExtension {
// Use this service to get easy typed access to query methods
// This cannot be used to for proof verification
const queryService = new QueryClientImpl({
request: (service: "string", method: string, data: Uint8Array): Promise<Uint8Array> => {
// Parts of the path are unavailable, so we hardcode them here. See https://github.com/protobufjs/protobuf.js/issues/1229
const path = `/cosmos.staking.v1beta1.Query/${method}`;
return base.queryUnverified(path, data);
},
});
// This cannot be used for proof verification
const rpc = createRpc(base);
const queryService = new QueryClientImpl(rpc);
return {
staking: {

View File

@ -1,6 +1,8 @@
import { Bech32 } from "@cosmjs/encoding";
import Long from "long";
import { QueryClient } from "./queryclient";
/**
* Takes a bech32 encoded address and returns the data part. The prefix is ignored and discarded.
* This is called AccAddress in Cosmos SDK, which is basically an alias for raw binary data.
@ -34,3 +36,16 @@ export function createPagination(
countTotal: false,
};
}
interface Rpc {
request(service: string, method: string, data: Uint8Array): Promise<Uint8Array>;
}
export function createRpc(base: QueryClient): Rpc {
return {
request: (service: string, method: string, data: Uint8Array): Promise<Uint8Array> => {
const path = `/${service}/${method}`;
return base.queryUnverified(path, data);
},
};
}