diff --git a/packages/sdk38/src/lcdapi/lcdclient.ts b/packages/sdk38/src/lcdapi/lcdclient.ts index 3ea8d844..f62b14a1 100644 --- a/packages/sdk38/src/lcdapi/lcdclient.ts +++ b/packages/sdk38/src/lcdapi/lcdclient.ts @@ -219,8 +219,8 @@ export class LcdClient { this.broadcastMode = broadcastMode; } - public async get(path: string): Promise { - const { data } = await this.client.get(path).catch(parseAxiosError); + public async get(path: string, params?: Record): Promise { + const { data } = await this.client.get(path, { params }).catch(parseAxiosError); if (data === null) { throw new Error("Received null response from server"); } diff --git a/packages/sdk38/src/lcdapi/staking.spec.ts b/packages/sdk38/src/lcdapi/staking.spec.ts index 8810277e..0a516d49 100644 --- a/packages/sdk38/src/lcdapi/staking.spec.ts +++ b/packages/sdk38/src/lcdapi/staking.spec.ts @@ -8,6 +8,56 @@ function makeStakingClient(apiUrl: string): LcdClient & StakingExtension { } describe("StakingExtension", () => { + describe("validators", () => { + it("works", async () => { + pendingWithoutWasmd(); + const client = makeStakingClient(wasmd.endpoint); + const response = await client.staking.validators(); + expect(response).toEqual({ + height: jasmine.stringMatching(nonNegativeIntegerMatcher), + result: [ + { + operator_address: "cosmosvaloper1gjvanqxc774u6ed9thj4gpn9gj5zus5u32enqn", + consensus_pubkey: + "cosmosvalconspub1zcjduepqau36ht2r742jh230pxlu4wjmwcmkwpeqava80acphsu87vt5xlpqx6g7qh", + jailed: false, + status: 2, + tokens: "250000000", + delegator_shares: "250000000.000000000000000000", + description: { + moniker: "testing", + identity: "", + website: "", + security_contact: "", + details: "", + }, + unbonding_height: "0", + unbonding_time: "1970-01-01T00:00:00Z", + commission: { + commission_rates: { + rate: "0.100000000000000000", + max_rate: "0.200000000000000000", + max_change_rate: "0.010000000000000000", + }, + update_time: "2020-06-03T06:01:17.4747987Z", + }, + min_self_delegation: "1", + }, + ], + }); + }); + + it("can filter by status", async () => { + pendingWithoutWasmd(); + const client = makeStakingClient(wasmd.endpoint); + const response = await client.staking.validators({ status: "unbonded" }); + expect(response).toEqual({ + height: jasmine.stringMatching(nonNegativeIntegerMatcher), + result: [], + }); + }); + }); + describe("pool", () => { it("works", async () => { pendingWithoutWasmd(); diff --git a/packages/sdk38/src/lcdapi/staking.ts b/packages/sdk38/src/lcdapi/staking.ts index ad83dd7d..365a5653 100644 --- a/packages/sdk38/src/lcdapi/staking.ts +++ b/packages/sdk38/src/lcdapi/staking.ts @@ -1,5 +1,49 @@ import { LcdClient } from "./lcdclient"; +export interface StakingValidatorsParams { + /** @see https://github.com/cosmos/cosmos-sdk/blob/v0.38.5/types/staking.go#L43-L49 */ + readonly status?: "bonded" | "unbonded" | "unbonding"; + readonly page?: number; + readonly limit?: number; +} + +interface Validator { + readonly operator_address: string; + readonly consensus_pubkey: string; + readonly jailed: boolean; + /** + * Numeric bonding status + * + * @see https://github.com/cosmos/cosmos-sdk/blob/v0.38.5/types/staking.go#L43-L49 + */ + readonly status: number; + readonly tokens: string; + readonly delegator_shares: string; + readonly description: { + readonly moniker: string; + readonly identity: string; + readonly website: string; + readonly security_contact: string; + readonly details: string; + }; + readonly unbonding_height: string; + readonly unbonding_time: string; + readonly commission: { + readonly commission_rates: { + readonly rate: string; + readonly max_rate: string; + readonly max_change_rate: string; + }; + readonly update_time: string; + }; + readonly min_self_delegation: string; +} + +export interface StakingValidatorsResponse { + readonly height: string; + readonly result: readonly Validator[]; +} + export interface StakingPoolResponse { readonly height: string; readonly result: { @@ -45,8 +89,8 @@ export interface StakingExtension { // Query redelegations (filters in query params) // /staking/redelegations - // Get all validators - // /staking/validators + /** Get all validators */ + readonly validators: (options?: StakingValidatorsParams) => Promise; // Get a single validator info // /staking/validators/{validatorAddr} @@ -70,6 +114,7 @@ export interface StakingExtension { export function setupStakingExtension(base: LcdClient): StakingExtension { return { staking: { + validators: async (params?: StakingValidatorsParams) => base.get(`/staking/validators`, params), pool: async () => base.get(`/staking/pool`), parameters: async () => base.get(`/staking/parameters`), }, diff --git a/packages/sdk38/types/lcdapi/lcdclient.d.ts b/packages/sdk38/types/lcdapi/lcdclient.d.ts index ce67cc38..7b275b02 100644 --- a/packages/sdk38/types/lcdapi/lcdclient.d.ts +++ b/packages/sdk38/types/lcdapi/lcdclient.d.ts @@ -143,7 +143,7 @@ export declare class LcdClient { * @param broadcastMode Defines at which point of the transaction processing the postTx method (i.e. transaction broadcasting) returns */ constructor(apiUrl: string, broadcastMode?: BroadcastMode); - get(path: string): Promise; + get(path: string, params?: Record): Promise; post(path: string, params: any): Promise; blocksLatest(): Promise; blocks(height: number): Promise; diff --git a/packages/sdk38/types/lcdapi/staking.d.ts b/packages/sdk38/types/lcdapi/staking.d.ts index acebf092..bc0ca5b4 100644 --- a/packages/sdk38/types/lcdapi/staking.d.ts +++ b/packages/sdk38/types/lcdapi/staking.d.ts @@ -1,4 +1,45 @@ import { LcdClient } from "./lcdclient"; +export interface StakingValidatorsParams { + /** @see https://github.com/cosmos/cosmos-sdk/blob/v0.38.5/types/staking.go#L43-L49 */ + readonly status?: "bonded" | "unbonded" | "unbonding"; + readonly page?: number; + readonly limit?: number; +} +interface Validator { + readonly operator_address: string; + readonly consensus_pubkey: string; + readonly jailed: boolean; + /** + * Numeric bonding status + * + * @see https://github.com/cosmos/cosmos-sdk/blob/v0.38.5/types/staking.go#L43-L49 + */ + readonly status: number; + readonly tokens: string; + readonly delegator_shares: string; + readonly description: { + readonly moniker: string; + readonly identity: string; + readonly website: string; + readonly security_contact: string; + readonly details: string; + }; + readonly unbonding_height: string; + readonly unbonding_time: string; + readonly commission: { + readonly commission_rates: { + readonly rate: string; + readonly max_rate: string; + readonly max_change_rate: string; + }; + readonly update_time: string; + }; + readonly min_self_delegation: string; +} +export interface StakingValidatorsResponse { + readonly height: string; + readonly result: readonly Validator[]; +} export interface StakingPoolResponse { readonly height: string; readonly result: { @@ -18,6 +59,8 @@ export interface StakingParametersResponse { } export interface StakingExtension { readonly staking: { + /** Get all validators */ + readonly validators: (options?: StakingValidatorsParams) => Promise; /** Get the current state of the staking pool */ readonly pool: () => Promise; /** Get the current staking parameter values */ @@ -25,3 +68,4 @@ export interface StakingExtension { }; } export declare function setupStakingExtension(base: LcdClient): StakingExtension; +export {};