stargate: Remove toObject util

This commit is contained in:
willclarktech 2021-02-03 15:08:26 +00:00
parent 176cea7fa5
commit 7c3db5fd64
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
7 changed files with 49 additions and 63 deletions

View File

@ -6,11 +6,6 @@ import { QueryClient } from "./queryclient";
* The result is typically 20 bytes long but not restricted to that.
*/
export declare function toAccAddress(address: string): Uint8Array;
/**
* Use this to convert a protobuf.js class to the interface (e.g. Coin to ICoin)
* in a ways that makes Jasmine's toEqual happy.
*/
export declare function toObject<I extends object>(thing: I): Omit<I, never>;
export declare function createPagination(
paginationKey?: Uint8Array,
): {

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 { createRpc, toAccAddress, toObject } from "./utils";
import { createRpc, toAccAddress } from "./utils";
export interface AuthExtension {
readonly auth: {
@ -32,7 +32,7 @@ export function setupAuthExtension(base: QueryClient): AuthExtension {
const account = Any.decode(responseData);
switch (account.typeUrl) {
case "/cosmos.auth.v1beta1.BaseAccount": {
return toObject(BaseAccount.decode(account.value));
return BaseAccount.decode(account.value);
}
default:
throw new Error(`Unsupported type: '${account.typeUrl}'`);
@ -45,7 +45,7 @@ export function setupAuthExtension(base: QueryClient): AuthExtension {
switch (account.typeUrl) {
case "/cosmos.auth.v1beta1.BaseAccount": {
assert(account.value);
return toObject(BaseAccount.decode(account.value));
return BaseAccount.decode(account.value);
}
default:
throw new Error(`Unsupported type: '${account.typeUrl}'`);

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 { createRpc, toAccAddress, toObject } from "./utils";
import { createRpc, toAccAddress } from "./utils";
export interface BankExtension {
readonly bank: {
@ -36,26 +36,26 @@ export function setupBankExtension(base: QueryClient): BankExtension {
// https://github.com/cosmos/cosmos-sdk/blob/2879c0702c87dc9dd828a8c42b9224dc054e28ad/store/prefix/store.go#L37-L43
const key = Uint8Array.from([...toAscii("balances"), ...toAccAddress(address), ...toAscii(denom)]);
const responseData = await base.queryVerified("bank", key);
return responseData.length ? toObject(Coin.decode(responseData)) : null;
return responseData.length ? Coin.decode(responseData) : null;
},
unverified: {
balance: async (address: string, denom: string) => {
const { balance } = await queryService.Balance({ address: address, denom: denom });
assert(balance);
return toObject(balance);
return balance;
},
allBalances: async (address: string) => {
const { balances } = await queryService.AllBalances({ address: address });
return balances.map(toObject);
return balances;
},
totalSupply: async () => {
const { supply } = await queryService.TotalSupply({});
return supply.map(toObject);
return supply;
},
supplyOf: async (denom: string) => {
const { amount } = await queryService.SupplyOf({ denom: denom });
assert(amount);
return toObject(amount);
return amount;
},
},
},

View File

@ -14,7 +14,7 @@ import {
QueryValidatorSlashesResponse,
} from "../codec/cosmos/distribution/v1beta1/query";
import { QueryClient } from "./queryclient";
import { createPagination, createRpc, toObject } from "./utils";
import { createPagination, createRpc } from "./utils";
export interface DistributionExtension {
readonly distribution: {
@ -53,48 +53,48 @@ export function setupDistributionExtension(base: QueryClient): DistributionExten
unverified: {
communityPool: async () => {
const response = await queryService.CommunityPool({});
return toObject(response);
return response;
},
delegationRewards: async (delegatorAddress: string, validatorAddress: string) => {
const response = await queryService.DelegationRewards({
delegatorAddress: delegatorAddress,
validatorAddress: validatorAddress,
});
return toObject(response);
return response;
},
delegationTotalRewards: async (delegatorAddress: string) => {
const response = await queryService.DelegationTotalRewards({
delegatorAddress: delegatorAddress,
});
return toObject(response);
return response;
},
delegatorValidators: async (delegatorAddress: string) => {
const response = await queryService.DelegatorValidators({
delegatorAddress: delegatorAddress,
});
return toObject(response);
return response;
},
delegatorWithdrawAddress: async (delegatorAddress: string) => {
const response = await queryService.DelegatorWithdrawAddress({
delegatorAddress: delegatorAddress,
});
return toObject(response);
return response;
},
params: async () => {
const response = await queryService.Params({});
return toObject(response);
return response;
},
validatorCommission: async (validatorAddress: string) => {
const response = await queryService.ValidatorCommission({
validatorAddress: validatorAddress,
});
return toObject(response);
return response;
},
validatorOutstandingRewards: async (validatorAddress: string) => {
const response = await queryService.ValidatorOutstandingRewards({
validatorAddress: validatorAddress,
});
return toObject(response);
return response;
},
validatorSlashes: async (
validatorAddress: string,
@ -108,7 +108,7 @@ export function setupDistributionExtension(base: QueryClient): DistributionExten
endingHeight: Long.fromNumber(endingHeight),
pagination: createPagination(paginationKey),
});
return toObject(response);
return response;
},
},
},

View File

@ -24,7 +24,7 @@ import {
QueryConnectionsResponse,
} from "../codec/ibc/core/connection/v1/query";
import { QueryClient } from "./queryclient";
import { createPagination, createRpc, toObject } from "./utils";
import { createPagination, createRpc } from "./utils";
export interface IbcExtension {
readonly ibc: {
@ -102,7 +102,7 @@ export function setupIbcExtension(base: QueryClient): IbcExtension {
// key: https://github.com/cosmos/cosmos-sdk/blob/ef0a7344af345882729598bc2958a21143930a6b/x/ibc/24-host/keys.go#L117-L120
const key = toAscii(`channelEnds/ports/${portId}/channels/${channelId}`);
const responseData = await base.queryVerified("ibc", key);
return responseData.length ? toObject(Channel.decode(responseData)) : null;
return responseData.length ? Channel.decode(responseData) : null;
},
packetCommitment: async (portId: string, channelId: string, sequence: number) => {
// keeper: https://github.com/cosmos/cosmos-sdk/blob/3bafd8255a502e5a9cee07391cf8261538245dfd/x/ibc/04-channel/keeper/keeper.go#L128-L133
@ -132,14 +132,14 @@ export function setupIbcExtension(base: QueryClient): IbcExtension {
// Queries for ibc.core.channel.v1
channel: async (portId: string, channelId: string) => {
const response = await channelQueryService.Channel({ portId: portId, channelId: channelId });
return toObject(response);
return response;
},
channels: async (paginationKey?: Uint8Array) => {
const request = {
pagination: createPagination(paginationKey),
};
const response = await channelQueryService.Channels(request);
return toObject(response);
return response;
},
connectionChannels: async (connection: string, paginationKey?: Uint8Array) => {
const request = {
@ -147,7 +147,7 @@ export function setupIbcExtension(base: QueryClient): IbcExtension {
pagination: createPagination(paginationKey),
};
const response = await channelQueryService.ConnectionChannels(request);
return toObject(response);
return response;
},
packetCommitment: async (portId: string, channelId: string, sequence: number) => {
const response = await channelQueryService.PacketCommitment({
@ -155,7 +155,7 @@ export function setupIbcExtension(base: QueryClient): IbcExtension {
channelId: channelId,
sequence: Long.fromNumber(sequence),
});
return toObject(response);
return response;
},
packetCommitments: async (portId: string, channelId: string, paginationKey?: Uint8Array) => {
const request = {
@ -164,7 +164,7 @@ export function setupIbcExtension(base: QueryClient): IbcExtension {
pagination: createPagination(paginationKey),
};
const response = await channelQueryService.PacketCommitments(request);
return toObject(response);
return response;
},
packetAcknowledgement: async (portId: string, channelId: string, sequence: number) => {
const response = await channelQueryService.PacketAcknowledgement({
@ -172,7 +172,7 @@ export function setupIbcExtension(base: QueryClient): IbcExtension {
channelId: channelId,
sequence: Long.fromNumber(sequence),
});
return toObject(response);
return response;
},
packetAcknowledgements: async (portId: string, channelId: string, paginationKey?: Uint8Array) => {
const response = await channelQueryService.PacketAcknowledgements({
@ -180,7 +180,7 @@ export function setupIbcExtension(base: QueryClient): IbcExtension {
channelId: channelId,
pagination: createPagination(paginationKey),
});
return toObject(response);
return response;
},
unreceivedPackets: async (
portId: string,
@ -192,7 +192,7 @@ export function setupIbcExtension(base: QueryClient): IbcExtension {
channelId: channelId,
packetCommitmentSequences: packetCommitmentSequences.map((s) => Long.fromNumber(s)),
});
return toObject(response);
return response;
},
unreceivedAcks: async (portId: string, channelId: string, packetAckSequences: readonly number[]) => {
const response = await channelQueryService.UnreceivedAcks({
@ -200,32 +200,32 @@ export function setupIbcExtension(base: QueryClient): IbcExtension {
channelId: channelId,
packetAckSequences: packetAckSequences.map((s) => Long.fromNumber(s)),
});
return toObject(response);
return response;
},
nextSequenceReceive: async (portId: string, channelId: string) => {
const response = await channelQueryService.NextSequenceReceive({
portId: portId,
channelId: channelId,
});
return toObject(response);
return response;
},
// Queries for ibc.core.connection.v1
connection: async (connectionId: string) => {
const response = await connectionQueryService.Connection({ connectionId: connectionId });
return toObject(response);
return response;
},
connections: async (paginationKey?: Uint8Array) => {
const request = {
pagination: createPagination(paginationKey),
};
const response = await connectionQueryService.Connections(request);
return toObject(response);
return response;
},
clientConnections: async (clientId: string) => {
const response = await connectionQueryService.ClientConnections({ clientId: clientId });
return toObject(response);
return response;
},
},
},

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, createRpc, toObject } from "./utils";
import { createPagination, createRpc } from "./utils";
export type BondStatusString = Exclude<keyof typeof BondStatus, "BOND_STATUS_UNSPECIFIED">;
@ -85,49 +85,49 @@ export function setupStakingExtension(base: QueryClient): StakingExtension {
delegatorAddr: delegatorAddress,
validatorAddr: validatorAddress,
});
return toObject(response);
return response;
},
delegatorDelegations: async (delegatorAddress: string, paginationKey?: Uint8Array) => {
const response = await queryService.DelegatorDelegations({
delegatorAddr: delegatorAddress,
pagination: createPagination(paginationKey),
});
return toObject(response);
return response;
},
delegatorUnbondingDelegations: async (delegatorAddress: string, paginationKey?: Uint8Array) => {
const response = await queryService.DelegatorUnbondingDelegations({
delegatorAddr: delegatorAddress,
pagination: createPagination(paginationKey),
});
return toObject(response);
return response;
},
delegatorValidator: async (delegatorAddress: string, validatorAddress: string) => {
const response = await queryService.DelegatorValidator({
delegatorAddr: delegatorAddress,
validatorAddr: validatorAddress,
});
return toObject(response);
return response;
},
delegatorValidators: async (delegatorAddress: string, paginationKey?: Uint8Array) => {
const response = await queryService.DelegatorValidators({
delegatorAddr: delegatorAddress,
pagination: createPagination(paginationKey),
});
return toObject(response);
return response;
},
historicalInfo: async (height: number) => {
const response = await queryService.HistoricalInfo({
height: Long.fromNumber(height),
});
return toObject(response);
return response;
},
params: async () => {
const response = await queryService.Params({});
return toObject(response);
return response;
},
pool: async () => {
const response = await queryService.Pool({});
return toObject(response);
return response;
},
redelegations: async (
delegatorAddress: string,
@ -141,39 +141,39 @@ export function setupStakingExtension(base: QueryClient): StakingExtension {
dstValidatorAddr: destinationValidatorAddress,
pagination: createPagination(paginationKey),
});
return toObject(response);
return response;
},
unbondingDelegation: async (delegatorAddress: string, validatorAddress: string) => {
const response = await queryService.UnbondingDelegation({
delegatorAddr: delegatorAddress,
validatorAddr: validatorAddress,
});
return toObject(response);
return response;
},
validator: async (validatorAddress: string) => {
const response = await queryService.Validator({ validatorAddr: validatorAddress });
return toObject(response);
return response;
},
validatorDelegations: async (validatorAddress: string, paginationKey?: Uint8Array) => {
const response = await queryService.ValidatorDelegations({
validatorAddr: validatorAddress,
pagination: createPagination(paginationKey),
});
return toObject(response);
return response;
},
validators: async (status: BondStatusString, paginationKey?: Uint8Array) => {
const response = await queryService.Validators({
status: status,
pagination: createPagination(paginationKey),
});
return toObject(response);
return response;
},
validatorUnbondingDelegations: async (validatorAddress: string, paginationKey?: Uint8Array) => {
const response = await queryService.ValidatorUnbondingDelegations({
validatorAddr: validatorAddress,
pagination: createPagination(paginationKey),
});
return toObject(response);
return response;
},
},
},

View File

@ -12,15 +12,6 @@ export function toAccAddress(address: string): Uint8Array {
return Bech32.decode(address).data;
}
/**
* Use this to convert a protobuf.js class to the interface (e.g. Coin to ICoin)
* in a ways that makes Jasmine's toEqual happy.
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function toObject<I extends object>(thing: I): Omit<I, never> {
return { ...thing };
}
export function createPagination(
paginationKey?: Uint8Array,
): {