From 7bb91790b9a076b52228b6fc1ef482e06b560f2f Mon Sep 17 00:00:00 2001 From: Jose Ramirez Date: Fri, 13 Aug 2021 19:45:23 -0300 Subject: [PATCH] Fix GovAPI endpoints (deposits, votes) and handle LCD API return null in a Coins.Data value. (#112) * Fix TypeError when LCD API return null in a Coins.Data value * Fix GovAPI votes endpoint and add type to deposits endpoint Co-authored-by: Paul Kim Co-authored-by: Yun Yeo Co-authored-by: brycepratt Co-authored-by: Jon Lund Steffensen --- src/client/lcd/api/GovAPI.ts | 25 ++++++++++++------ src/core/Coins.ts | 7 +++--- src/core/Deposit.ts | 49 ++++++++++++++++++++++++++++++++++++ src/core/Vote.ts | 43 +++++++++++++++++++++++++++++++ src/core/index.ts | 2 ++ 5 files changed, 116 insertions(+), 10 deletions(-) create mode 100644 src/core/Deposit.ts create mode 100644 src/core/Vote.ts diff --git a/src/client/lcd/api/GovAPI.ts b/src/client/lcd/api/GovAPI.ts index b65ecb294..b424ac331 100644 --- a/src/client/lcd/api/GovAPI.ts +++ b/src/client/lcd/api/GovAPI.ts @@ -1,5 +1,13 @@ import { BaseAPI } from './BaseAPI'; -import { Proposal, AccAddress, Coins, Dec, Int } from '../../../core'; +import { + Proposal, + AccAddress, + Coins, + Dec, + Int, + Deposit, + Vote, +} from '../../../core'; import { DepositParams, VotingParams, @@ -94,20 +102,23 @@ export class GovAPI extends BaseAPI { public async deposits( proposalId: number, params: APIParams = {} - ): Promise { + ): Promise { return this.c - .get(`/gov/proposals/${proposalId}/deposits`, params) - .then(d => d.result); + .get(`/gov/proposals/${proposalId}/deposits`, params) + .then(d => Deposit.fromData(d.result)); } /** * Get the current votes for a proposal * @param proposalId proposal's ID */ - public async votes(proposalId: number, params: APIParams = {}): Promise { + public async votes( + proposalId: number, + params: APIParams = {} + ): Promise { return this.c - .get(`/gov/proposals/${proposalId}/deposits`, params) - .then(d => d.result); + .get(`/gov/proposals/${proposalId}/votes`, params) + .then(d => Vote.fromData(d.result)); } /** diff --git a/src/core/Coins.ts b/src/core/Coins.ts index d9b6f805e..de2a1aa27 100644 --- a/src/core/Coins.ts +++ b/src/core/Coins.ts @@ -10,7 +10,8 @@ import { Numeric } from './numeric'; */ export class Coins extends JSONSerializable - implements Numeric { + implements Numeric +{ private _coins: Coins.ReprDict; /** @@ -126,8 +127,8 @@ export class Coins this._coins[denom] = val; } - public static fromData(data: Coins.Data): Coins { - return new Coins(data.map(Coin.fromData)); + public static fromData(data: Coins.Data | null): Coins { + return new Coins((data ?? []).map(Coin.fromData)); } /** diff --git a/src/core/Deposit.ts b/src/core/Deposit.ts new file mode 100644 index 000000000..a60a054d7 --- /dev/null +++ b/src/core/Deposit.ts @@ -0,0 +1,49 @@ +import { Coins } from './Coins'; +import { JSONSerializable } from '../util/json'; +import { AccAddress } from './bech32'; + +/** + * Stores deposit information for a proposal + */ +export class Deposit extends JSONSerializable { + public amount: Coins; + /** + * @param proposal_id Id of porposal to deposit to + * @param depositor depositor's account address + * @param amount amount to deposit + */ + constructor( + public proposal_id: number, + public depositor: AccAddress, + amount: Coins.Input + ) { + super(); + this.amount = new Coins(amount); + } + + public static fromData(data: Deposit.Data): Deposit { + const { proposal_id, depositor, amount } = data; + return new Deposit( + Number.parseInt(proposal_id), + depositor, + Coins.fromData(amount) + ); + } + + public toData(): Deposit.Data { + const { proposal_id, depositor, amount } = this; + return { + proposal_id: proposal_id.toString(), + depositor, + amount: amount.toData(), + }; + } +} + +export namespace Deposit { + export interface Data { + proposal_id: string; + depositor: AccAddress; + amount: Coins.Data; + } +} diff --git a/src/core/Vote.ts b/src/core/Vote.ts new file mode 100644 index 000000000..a174cd2ad --- /dev/null +++ b/src/core/Vote.ts @@ -0,0 +1,43 @@ +import { JSONSerializable } from '../util/json'; +import { AccAddress } from './bech32'; +import { MsgVote } from './'; + +/** + * Stores vote information for a proposal + */ +export class Vote extends JSONSerializable { + /** + * @param proposal_id ID of proposal to vote on + * @param voter voter's account address + * @param option one of voting options + */ + constructor( + public proposal_id: number, + public voter: AccAddress, + public option: MsgVote.Option + ) { + super(); + } + + public static fromData(data: Vote.Data): Vote { + const { proposal_id, voter, option } = data; + return new Vote(parseInt(proposal_id), voter, option); + } + + public toData(): Vote.Data { + const { proposal_id, voter, option } = this; + return { + proposal_id: proposal_id.toFixed(), + voter, + option, + }; + } +} + +export namespace Vote { + export interface Data { + proposal_id: string; + voter: AccAddress; + option: MsgVote.Option; + } +} diff --git a/src/core/index.ts b/src/core/index.ts index 71367b77e..0b08b3cee 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -12,6 +12,8 @@ export * from './StdSignMsg'; export * from './StdTx'; export * from './TxInfo'; export * from './ValidatorSet'; +export * from './Deposit'; +export * from './Vote'; // Auth export * from './auth/Account';