Skip to content

Commit

Permalink
Fix GovAPI endpoints (deposits, votes) and handle LCD API return null…
Browse files Browse the repository at this point in the history
… 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 <[email protected]>
Co-authored-by: Yun Yeo <[email protected]>
Co-authored-by: brycepratt <[email protected]>
Co-authored-by: Jon Lund Steffensen <[email protected]>
  • Loading branch information
5 people authored Aug 13, 2021
1 parent b17807d commit 7bb9179
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 10 deletions.
25 changes: 18 additions & 7 deletions src/client/lcd/api/GovAPI.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -94,20 +102,23 @@ export class GovAPI extends BaseAPI {
public async deposits(
proposalId: number,
params: APIParams = {}
): Promise<any> {
): Promise<Deposit> {
return this.c
.get(`/gov/proposals/${proposalId}/deposits`, params)
.then(d => d.result);
.get<Deposit.Data>(`/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<any> {
public async votes(
proposalId: number,
params: APIParams = {}
): Promise<Vote> {
return this.c
.get(`/gov/proposals/${proposalId}/deposits`, params)
.then(d => d.result);
.get<Vote.Data>(`/gov/proposals/${proposalId}/votes`, params)
.then(d => Vote.fromData(d.result));
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/core/Coins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { Numeric } from './numeric';
*/
export class Coins
extends JSONSerializable<Coins.Data>
implements Numeric<Coins> {
implements Numeric<Coins>
{
private _coins: Coins.ReprDict;

/**
Expand Down Expand Up @@ -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));
}

/**
Expand Down
49 changes: 49 additions & 0 deletions src/core/Deposit.ts
Original file line number Diff line number Diff line change
@@ -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<Deposit.Data> {
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;
}
}
43 changes: 43 additions & 0 deletions src/core/Vote.ts
Original file line number Diff line number Diff line change
@@ -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<Vote.Data> {
/**
* @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;
}
}
2 changes: 2 additions & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 7bb9179

Please sign in to comment.