From 486e6aa8c40bdd54e004bc31eaafe46d887cec83 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Tue, 26 Jan 2021 13:03:06 +0000 Subject: [PATCH] stargate: Add distribution queries --- packages/stargate/src/index.ts | 2 + packages/stargate/src/queries/distribution.ts | 122 ++++++++++++++++++ packages/stargate/src/queries/index.ts | 1 + packages/stargate/types/index.d.ts | 2 + .../stargate/types/queries/distribution.d.ts | 38 ++++++ packages/stargate/types/queries/index.d.ts | 1 + 6 files changed, 166 insertions(+) create mode 100644 packages/stargate/src/queries/distribution.ts create mode 100644 packages/stargate/types/queries/distribution.d.ts diff --git a/packages/stargate/src/index.ts b/packages/stargate/src/index.ts index a6b93190..1d72d8f4 100644 --- a/packages/stargate/src/index.ts +++ b/packages/stargate/src/index.ts @@ -4,10 +4,12 @@ export { parseRawLog } from "./logs"; export { AuthExtension, BankExtension, + DistributionExtension, IbcExtension, QueryClient, setupAuthExtension, setupBankExtension, + setupDistributionExtension, setupIbcExtension, setupStakingExtension, StakingExtension, diff --git a/packages/stargate/src/queries/distribution.ts b/packages/stargate/src/queries/distribution.ts new file mode 100644 index 00000000..da3c809c --- /dev/null +++ b/packages/stargate/src/queries/distribution.ts @@ -0,0 +1,122 @@ +/* eslint-disable @typescript-eslint/naming-convention */ +import Long from "long"; + +import { cosmos } from "../codec"; +import { QueryClient } from "./queryclient"; +import { toObject } from "./utils"; + +type IQueryCommunityPoolResponse = cosmos.distribution.v1beta1.IQueryCommunityPoolResponse; +type IQueryDelegationRewardsResponse = cosmos.distribution.v1beta1.IQueryDelegationRewardsResponse; +type IQueryDelegationTotalRewardsResponse = cosmos.distribution.v1beta1.IQueryDelegationTotalRewardsResponse; +type IQueryDelegatorValidatorsResponse = cosmos.distribution.v1beta1.IQueryDelegatorValidatorsResponse; +type IQueryDelegatorWithdrawAddressResponse = cosmos.distribution.v1beta1.IQueryDelegatorWithdrawAddressResponse; +type IQueryParamsResponse = cosmos.distribution.v1beta1.IQueryParamsResponse; +type IQueryValidatorCommissionResponse = cosmos.distribution.v1beta1.IQueryValidatorCommissionResponse; +type IQueryValidatorOutstandingRewardsResponse = cosmos.distribution.v1beta1.IQueryValidatorOutstandingRewardsResponse; +type IQueryValidatorSlashesResponse = cosmos.distribution.v1beta1.IQueryValidatorSlashesResponse; + +const { Query } = cosmos.distribution.v1beta1; + +export interface DistributionExtension { + readonly distribution: { + unverified: { + communityPool: () => Promise; + delegationRewards: ( + delegatorAddress: string, + validatorAddress: string, + ) => Promise; + delegationTotalRewards: (delegatorAddress: string) => Promise; + delegatorValidators: (delegatorAddress: string) => Promise; + delegatorWithdrawAddress: (delegatorAddress: string) => Promise; + params: () => Promise; + validatorCommission: (validatorAddress: string) => Promise; + validatorOutstandingRewards: ( + validatorAddress: string, + ) => Promise; + validatorSlashes: ( + validatorAddress: string, + startingHeight: number, + endingHeight: number, + paginationKey?: Uint8Array, + ) => Promise; + }; + }; +} + +export function setupDistributionExtension(base: QueryClient): DistributionExtension { + // Use this service to get easy typed access to query methods + // This cannot be used to for proof verification + const queryService = Query.create((method: any, requestData, callback) => { + // 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.name}`; + base + .queryUnverified(path, requestData) + .then((response) => callback(null, response)) + .catch((error) => callback(error)); + }); + return { + distribution: { + unverified: { + communityPool: async () => { + const response = await queryService.communityPool({}); + return toObject(response); + }, + delegationRewards: async (delegatorAddress: string, validatorAddress: string) => { + const response = await queryService.delegationRewards({ + delegatorAddress: delegatorAddress, + validatorAddress: validatorAddress, + }); + return toObject(response); + }, + delegationTotalRewards: async (delegatorAddress: string) => { + const response = await queryService.delegationTotalRewards({ + delegatorAddress: delegatorAddress, + }); + return toObject(response); + }, + delegatorValidators: async (delegatorAddress: string) => { + const response = await queryService.delegatorValidators({ + delegatorAddress: delegatorAddress, + }); + return toObject(response); + }, + delegatorWithdrawAddress: async (delegatorAddress: string) => { + const response = await queryService.delegatorWithdrawAddress({ + delegatorAddress: delegatorAddress, + }); + return toObject(response); + }, + params: async () => { + const response = await queryService.params({}); + return toObject(response); + }, + validatorCommission: async (validatorAddress: string) => { + const response = await queryService.validatorCommission({ + validatorAddress: validatorAddress, + }); + return toObject(response); + }, + validatorOutstandingRewards: async (validatorAddress: string) => { + const response = await queryService.validatorOutstandingRewards({ + validatorAddress: validatorAddress, + }); + return toObject(response); + }, + validatorSlashes: async ( + validatorAddress: string, + startingHeight: number, + endingHeight: number, + paginationKey?: Uint8Array, + ) => { + const response = await queryService.validatorSlashes({ + validatorAddress: validatorAddress, + startingHeight: Long.fromNumber(startingHeight), + endingHeight: Long.fromNumber(endingHeight), + pagination: paginationKey ? { key: paginationKey } : undefined, + }); + return toObject(response); + }, + }, + }, + }; +} diff --git a/packages/stargate/src/queries/index.ts b/packages/stargate/src/queries/index.ts index c3c817b5..5161cd5c 100644 --- a/packages/stargate/src/queries/index.ts +++ b/packages/stargate/src/queries/index.ts @@ -6,5 +6,6 @@ export { QueryClient } from "./queryclient"; export { AuthExtension, setupAuthExtension } from "./auth"; export { BankExtension, setupBankExtension } from "./bank"; +export { DistributionExtension, setupDistributionExtension } from "./distribution"; export { IbcExtension, setupIbcExtension } from "./ibc"; export { setupStakingExtension, StakingExtension } from "./staking"; diff --git a/packages/stargate/types/index.d.ts b/packages/stargate/types/index.d.ts index a6b93190..1d72d8f4 100644 --- a/packages/stargate/types/index.d.ts +++ b/packages/stargate/types/index.d.ts @@ -4,10 +4,12 @@ export { parseRawLog } from "./logs"; export { AuthExtension, BankExtension, + DistributionExtension, IbcExtension, QueryClient, setupAuthExtension, setupBankExtension, + setupDistributionExtension, setupIbcExtension, setupStakingExtension, StakingExtension, diff --git a/packages/stargate/types/queries/distribution.d.ts b/packages/stargate/types/queries/distribution.d.ts new file mode 100644 index 00000000..afe453dc --- /dev/null +++ b/packages/stargate/types/queries/distribution.d.ts @@ -0,0 +1,38 @@ +import { cosmos } from "../codec"; +import { QueryClient } from "./queryclient"; +declare type IQueryCommunityPoolResponse = cosmos.distribution.v1beta1.IQueryCommunityPoolResponse; +declare type IQueryDelegationRewardsResponse = cosmos.distribution.v1beta1.IQueryDelegationRewardsResponse; +declare type IQueryDelegationTotalRewardsResponse = cosmos.distribution.v1beta1.IQueryDelegationTotalRewardsResponse; +declare type IQueryDelegatorValidatorsResponse = cosmos.distribution.v1beta1.IQueryDelegatorValidatorsResponse; +declare type IQueryDelegatorWithdrawAddressResponse = cosmos.distribution.v1beta1.IQueryDelegatorWithdrawAddressResponse; +declare type IQueryParamsResponse = cosmos.distribution.v1beta1.IQueryParamsResponse; +declare type IQueryValidatorCommissionResponse = cosmos.distribution.v1beta1.IQueryValidatorCommissionResponse; +declare type IQueryValidatorOutstandingRewardsResponse = cosmos.distribution.v1beta1.IQueryValidatorOutstandingRewardsResponse; +declare type IQueryValidatorSlashesResponse = cosmos.distribution.v1beta1.IQueryValidatorSlashesResponse; +export interface DistributionExtension { + readonly distribution: { + unverified: { + communityPool: () => Promise; + delegationRewards: ( + delegatorAddress: string, + validatorAddress: string, + ) => Promise; + delegationTotalRewards: (delegatorAddress: string) => Promise; + delegatorValidators: (delegatorAddress: string) => Promise; + delegatorWithdrawAddress: (delegatorAddress: string) => Promise; + params: () => Promise; + validatorCommission: (validatorAddress: string) => Promise; + validatorOutstandingRewards: ( + validatorAddress: string, + ) => Promise; + validatorSlashes: ( + validatorAddress: string, + startingHeight: number, + endingHeight: number, + paginationKey?: Uint8Array, + ) => Promise; + }; + }; +} +export declare function setupDistributionExtension(base: QueryClient): DistributionExtension; +export {}; diff --git a/packages/stargate/types/queries/index.d.ts b/packages/stargate/types/queries/index.d.ts index 76b082da..1958b382 100644 --- a/packages/stargate/types/queries/index.d.ts +++ b/packages/stargate/types/queries/index.d.ts @@ -1,5 +1,6 @@ export { QueryClient } from "./queryclient"; export { AuthExtension, setupAuthExtension } from "./auth"; export { BankExtension, setupBankExtension } from "./bank"; +export { DistributionExtension, setupDistributionExtension } from "./distribution"; export { IbcExtension, setupIbcExtension } from "./ibc"; export { setupStakingExtension, StakingExtension } from "./staking";