diff --git a/packages/stargate/src/index.ts b/packages/stargate/src/index.ts index a278448a..a6b93190 100644 --- a/packages/stargate/src/index.ts +++ b/packages/stargate/src/index.ts @@ -9,6 +9,8 @@ export { setupAuthExtension, setupBankExtension, setupIbcExtension, + setupStakingExtension, + StakingExtension, } from "./queries"; export { Account, diff --git a/packages/stargate/src/queries/index.ts b/packages/stargate/src/queries/index.ts index 40ab4c10..c3c817b5 100644 --- a/packages/stargate/src/queries/index.ts +++ b/packages/stargate/src/queries/index.ts @@ -7,3 +7,4 @@ export { QueryClient } from "./queryclient"; export { AuthExtension, setupAuthExtension } from "./auth"; export { BankExtension, setupBankExtension } from "./bank"; export { IbcExtension, setupIbcExtension } from "./ibc"; +export { setupStakingExtension, StakingExtension } from "./staking"; diff --git a/packages/stargate/src/queries/staking.ts b/packages/stargate/src/queries/staking.ts new file mode 100644 index 00000000..471befaf --- /dev/null +++ b/packages/stargate/src/queries/staking.ts @@ -0,0 +1,185 @@ +/* eslint-disable @typescript-eslint/naming-convention */ +import Long from "long"; + +import { cosmos } from "../codec"; +import { QueryClient } from "./queryclient"; +import { toObject } from "./utils"; + +type IQueryDelegationResponse = cosmos.staking.v1beta1.IQueryDelegationResponse; +type IQueryDelegatorDelegationsResponse = cosmos.staking.v1beta1.IQueryDelegatorDelegationsResponse; +type IQueryDelegatorUnbondingDelegationsResponse = cosmos.staking.v1beta1.IQueryDelegatorUnbondingDelegationsResponse; +type IQueryDelegatorValidatorResponse = cosmos.staking.v1beta1.IQueryDelegatorValidatorResponse; +type IQueryDelegatorValidatorsResponse = cosmos.staking.v1beta1.IQueryDelegatorValidatorsResponse; +type IQueryHistoricalInfoResponse = cosmos.staking.v1beta1.IQueryHistoricalInfoResponse; +type IQueryParamsResponse = cosmos.staking.v1beta1.IQueryParamsResponse; +type IQueryPoolResponse = cosmos.staking.v1beta1.IQueryPoolResponse; +type IQueryRedelegationsResponse = cosmos.staking.v1beta1.IQueryRedelegationsResponse; +type IQueryUnbondingDelegationResponse = cosmos.staking.v1beta1.IQueryUnbondingDelegationResponse; +type IQueryValidatorResponse = cosmos.staking.v1beta1.IQueryValidatorResponse; +type IQueryValidatorDelegationsResponse = cosmos.staking.v1beta1.IQueryValidatorDelegationsResponse; +type IQueryValidatorsResponse = cosmos.staking.v1beta1.IQueryValidatorsResponse; +type IQueryValidatorUnbondingDelegationsResponse = cosmos.staking.v1beta1.IQueryValidatorUnbondingDelegationsResponse; + +const { Query } = cosmos.staking.v1beta1; + +export interface StakingExtension { + readonly staking: { + readonly unverified: { + delegation: (delegatorAddress: string, validatorAddress: string) => Promise; + delegatorDelegations: ( + delegatorAddress: string, + paginationKey?: Uint8Array, + ) => Promise; + delegatorUnbondingDelegations: ( + delegatorAddress: string, + paginationKey?: Uint8Array, + ) => Promise; + delegatorValidator: ( + delegatorAddress: string, + validatorAddress: string, + ) => Promise; + delegatorValidators: ( + delegatorAddress: string, + paginationKey?: Uint8Array, + ) => Promise; + historicalInfo: (height: number) => Promise; + params: () => Promise; + pool: () => Promise; + redelegations: ( + delegatorAddress: string, + sourceValidatorAddress: string, + destinationValidatorAddress: string, + paginationKey?: Uint8Array, + ) => Promise; + unbondingDelegation: ( + delegatorAddress: string, + validatorAddress: string, + ) => Promise; + validator: (validatorAddress: string) => Promise; + validatorDelegations: ( + validatorAddress: string, + paginationKey?: Uint8Array, + ) => Promise; + validators: (status: string, paginationKey?: Uint8Array) => Promise; + validatorUnbondingDelegations: ( + validatorAddress: string, + paginationKey?: Uint8Array, + ) => Promise; + }; + }; +} + +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 = 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.staking.v1beta1.Query/${method.name}`; + base + .queryUnverified(path, requestData) + .then((response) => callback(null, response)) + .catch((error) => callback(error)); + }); + + return { + staking: { + unverified: { + delegation: async (delegatorAddress: string, validatorAddress: string) => { + const response = await queryService.delegation({ + delegatorAddr: delegatorAddress, + validatorAddr: validatorAddress, + }); + return toObject(response); + }, + delegatorDelegations: async (delegatorAddress: string, paginationKey?: Uint8Array) => { + const response = await queryService.delegatorDelegations({ + delegatorAddr: delegatorAddress, + pagination: paginationKey ? { key: paginationKey } : undefined, + }); + return toObject(response); + }, + delegatorUnbondingDelegations: async (delegatorAddress: string, paginationKey?: Uint8Array) => { + const response = await queryService.delegatorUnbondingDelegations({ + delegatorAddr: delegatorAddress, + pagination: paginationKey ? { key: paginationKey } : undefined, + }); + return toObject(response); + }, + delegatorValidator: async (delegatorAddress: string, validatorAddress: string) => { + const response = queryService.delegatorValidator({ + delegatorAddr: delegatorAddress, + validatorAddr: validatorAddress, + }); + return toObject(response); + }, + delegatorValidators: async (delegatorAddress: string, paginationKey?: Uint8Array) => { + const response = queryService.delegatorValidators({ + delegatorAddr: delegatorAddress, + pagination: paginationKey ? { key: paginationKey } : undefined, + }); + return toObject(response); + }, + historicalInfo: async (height: number) => { + const response = queryService.historicalInfo({ + height: Long.fromNumber(height), + }); + return toObject(response); + }, + params: async () => { + const response = queryService.params({}); + return toObject(response); + }, + pool: async () => { + const response = queryService.pool({}); + return toObject(response); + }, + redelegations: async ( + delegatorAddress: string, + sourceValidatorAddress: string, + destinationValidatorAddress: string, + paginationKey?: Uint8Array, + ) => { + const response = queryService.redelegations({ + delegatorAddr: delegatorAddress, + srcValidatorAddr: sourceValidatorAddress, + dstValidatorAddr: destinationValidatorAddress, + pagination: paginationKey ? { key: paginationKey } : undefined, + }); + return toObject(response); + }, + unbondingDelegation: async (delegatorAddress: string, validatorAddress: string) => { + const response = queryService.unbondingDelegation({ + delegatorAddr: delegatorAddress, + validatorAddr: validatorAddress, + }); + return toObject(response); + }, + validator: async (validatorAddress: string) => { + const response = queryService.validator({ validatorAddr: validatorAddress }); + return toObject(response); + }, + validatorDelegations: async (validatorAddress: string, paginationKey?: Uint8Array) => { + const response = queryService.validatorDelegations({ + validatorAddr: validatorAddress, + pagination: paginationKey ? { key: paginationKey } : undefined, + }); + return toObject(response); + }, + validators: async (status: string, paginationKey?: Uint8Array) => { + const response = queryService.validators({ + status: status, + pagination: paginationKey ? { key: paginationKey } : undefined, + }); + return toObject(response); + }, + validatorUnbondingDelegations: async (validatorAddress: string, paginationKey?: Uint8Array) => { + const response = queryService.validatorUnbondingDelegations({ + validatorAddr: validatorAddress, + pagination: paginationKey ? { key: paginationKey } : undefined, + }); + return toObject(response); + }, + }, + }, + }; +} diff --git a/packages/stargate/types/index.d.ts b/packages/stargate/types/index.d.ts index a278448a..a6b93190 100644 --- a/packages/stargate/types/index.d.ts +++ b/packages/stargate/types/index.d.ts @@ -9,6 +9,8 @@ export { setupAuthExtension, setupBankExtension, setupIbcExtension, + setupStakingExtension, + StakingExtension, } from "./queries"; export { Account, diff --git a/packages/stargate/types/queries/index.d.ts b/packages/stargate/types/queries/index.d.ts index 2cc0699b..76b082da 100644 --- a/packages/stargate/types/queries/index.d.ts +++ b/packages/stargate/types/queries/index.d.ts @@ -2,3 +2,4 @@ export { QueryClient } from "./queryclient"; export { AuthExtension, setupAuthExtension } from "./auth"; export { BankExtension, setupBankExtension } from "./bank"; export { IbcExtension, setupIbcExtension } from "./ibc"; +export { setupStakingExtension, StakingExtension } from "./staking"; diff --git a/packages/stargate/types/queries/staking.d.ts b/packages/stargate/types/queries/staking.d.ts new file mode 100644 index 00000000..f9ab444c --- /dev/null +++ b/packages/stargate/types/queries/staking.d.ts @@ -0,0 +1,64 @@ +import { cosmos } from "../codec"; +import { QueryClient } from "./queryclient"; +declare type IQueryDelegationResponse = cosmos.staking.v1beta1.IQueryDelegationResponse; +declare type IQueryDelegatorDelegationsResponse = cosmos.staking.v1beta1.IQueryDelegatorDelegationsResponse; +declare type IQueryDelegatorUnbondingDelegationsResponse = cosmos.staking.v1beta1.IQueryDelegatorUnbondingDelegationsResponse; +declare type IQueryDelegatorValidatorResponse = cosmos.staking.v1beta1.IQueryDelegatorValidatorResponse; +declare type IQueryDelegatorValidatorsResponse = cosmos.staking.v1beta1.IQueryDelegatorValidatorsResponse; +declare type IQueryHistoricalInfoResponse = cosmos.staking.v1beta1.IQueryHistoricalInfoResponse; +declare type IQueryParamsResponse = cosmos.staking.v1beta1.IQueryParamsResponse; +declare type IQueryPoolResponse = cosmos.staking.v1beta1.IQueryPoolResponse; +declare type IQueryRedelegationsResponse = cosmos.staking.v1beta1.IQueryRedelegationsResponse; +declare type IQueryUnbondingDelegationResponse = cosmos.staking.v1beta1.IQueryUnbondingDelegationResponse; +declare type IQueryValidatorResponse = cosmos.staking.v1beta1.IQueryValidatorResponse; +declare type IQueryValidatorDelegationsResponse = cosmos.staking.v1beta1.IQueryValidatorDelegationsResponse; +declare type IQueryValidatorsResponse = cosmos.staking.v1beta1.IQueryValidatorsResponse; +declare type IQueryValidatorUnbondingDelegationsResponse = cosmos.staking.v1beta1.IQueryValidatorUnbondingDelegationsResponse; +export interface StakingExtension { + readonly staking: { + readonly unverified: { + delegation: (delegatorAddress: string, validatorAddress: string) => Promise; + delegatorDelegations: ( + delegatorAddress: string, + paginationKey?: Uint8Array, + ) => Promise; + delegatorUnbondingDelegations: ( + delegatorAddress: string, + paginationKey?: Uint8Array, + ) => Promise; + delegatorValidator: ( + delegatorAddress: string, + validatorAddress: string, + ) => Promise; + delegatorValidators: ( + delegatorAddress: string, + paginationKey?: Uint8Array, + ) => Promise; + historicalInfo: (height: number) => Promise; + params: () => Promise; + pool: () => Promise; + redelegations: ( + delegatorAddress: string, + sourceValidatorAddress: string, + destinationValidatorAddress: string, + paginationKey?: Uint8Array, + ) => Promise; + unbondingDelegation: ( + delegatorAddress: string, + validatorAddress: string, + ) => Promise; + validator: (validatorAddress: string) => Promise; + validatorDelegations: ( + validatorAddress: string, + paginationKey?: Uint8Array, + ) => Promise; + validators: (status: string, paginationKey?: Uint8Array) => Promise; + validatorUnbondingDelegations: ( + validatorAddress: string, + paginationKey?: Uint8Array, + ) => Promise; + }; + }; +} +export declare function setupStakingExtension(base: QueryClient): StakingExtension; +export {};