Skip to content

Commit

Permalink
Governance breaking changes for Columbus-5 (#125)
Browse files Browse the repository at this point in the history
* build: v2.0.5
* feat(gov): add MsgWeightedVote
* feat(gov): fix breaking changes (Vote option and Proposal status became number)
* dep: update
  • Loading branch information
hanjukim authored Sep 8, 2021
1 parent b77db95 commit 9021bcd
Show file tree
Hide file tree
Showing 10 changed files with 1,972 additions and 2,288 deletions.
4,001 changes: 1,786 additions & 2,215 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@terra-money/terra.js",
"version": "2.0.4",
"version": "2.0.5",
"description": "The JavaScript SDK for Terra",
"license": "MIT",
"author": "Terraform Labs, PTE.",
Expand Down Expand Up @@ -54,7 +54,7 @@
"arrowParens": "avoid"
},
"devDependencies": {
"@types/jest": "^26.0.14",
"@types/jest": "^27.0.1",
"@types/node": "^15.12.4",
"@types/readable-stream": "^2.3.9",
"@types/secp256k1": "^4.0.1",
Expand All @@ -70,7 +70,7 @@
"lodash.memoize": "^4.1.2",
"prettier": "^2.2.1",
"stream-browserify": "^3.0.0",
"ts-jest": "^27.0.3",
"ts-jest": "^27.0.5",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths-webpack-plugin": "^3.3.0",
Expand Down
43 changes: 0 additions & 43 deletions src/core/Vote.ts

This file was deleted.

29 changes: 20 additions & 9 deletions src/core/Proposal.ts → src/core/gov/Proposal.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Coins } from './Coins';
import { Int } from './numeric';
import { JSONSerializable } from '../util/json';
import { CommunityPoolSpendProposal } from './distribution/proposals';
import { ParameterChangeProposal } from './params/proposals';
import { TextProposal } from './gov/proposals';
import { Coins } from '../Coins';
import { Int } from '../numeric';
import { JSONSerializable } from '../../util/json';
import { CommunityPoolSpendProposal } from '../distribution/proposals';
import { ParameterChangeProposal } from '../params/proposals';
import { TextProposal } from './proposals';

/**
* Stores information pertaining to a submitted proposal, such as its status and time of
Expand Down Expand Up @@ -70,7 +70,7 @@ export class Proposal extends JSONSerializable<Proposal.Data> {
return new Proposal(
Number.parseInt(id),
Proposal.Content.fromData(content),
proposal_status,
Proposal.StatusMapping[proposal_status],
ftr,
new Date(submit_time),
new Date(deposit_end_time),
Expand Down Expand Up @@ -104,7 +104,9 @@ export class Proposal extends JSONSerializable<Proposal.Data> {
return {
id: this.id.toFixed(),
content: this.content.toData(),
proposal_status: proposal_status,
proposal_status: Object.keys(Proposal.StatusMapping).indexOf(
proposal_status
),
final_tally_result: ftr,
submit_time: this.submit_time.toISOString(),
deposit_end_time: this.deposit_end_time.toISOString(),
Expand Down Expand Up @@ -157,10 +159,19 @@ export namespace Proposal {
FAILED = 'Failed',
}

export const StatusMapping: { [key: number]: Status } = {
0: Status.NIL,
1: Status.DEPOSIT_PERIOD,
2: Status.VOTING_PERIOD,
3: Status.PASSED,
4: Status.REJECTED,
5: Status.FAILED,
};

export interface Data {
content: Content.Data;
id: string;
proposal_status: Status;
proposal_status: number;
final_tally_result?: {
yes: string;
abstain: string;
Expand Down
97 changes: 97 additions & 0 deletions src/core/gov/Vote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { JSONSerializable } from '../../util/json';
import { AccAddress } from '../bech32';

/**
* 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 options: Vote.Options,
public option?: Vote.Option // undefined except proposals in voting status
) {
super();
}

public static fromData(data: Vote.Data): Vote {
const { proposal_id, voter, options, option } = data;

return new Vote(
parseInt(proposal_id),
voter,
options.map(({ option, weight }) => ({
option: Vote.OptionMapping[option],
weight,
})),
option ? Vote.OptionMapping[option] : undefined
);
}

public toData(): Vote.Data {
const { proposal_id, voter, options, option } = this;

const res: Vote.Data = {
proposal_id: proposal_id.toFixed(),
voter,
options: options.map(({ option, weight }) => ({
option: Object.keys(Vote.OptionMapping).indexOf(option),
weight,
})),
};

if (option) {
res.option = Object.keys(Vote.OptionMapping).indexOf(option);
}

return res;
}
}

export namespace Vote {
export type Options = {
option: Option;
weight: string;
}[];

/** Voting options */
export enum Option {
/** - */
EMPTY = 'Empty',

/** Vote yes */
YES = 'Yes',

/** Do not vote */
ABSTAIN = 'Abstain',

/** Vote no */
NO = 'No',

/** Vote No with the option to veto if passed */
NO_WITH_VETO = 'NoWithVeto',
}

export const OptionMapping: { [key: number]: Option } = {
0: Option.EMPTY,
1: Option.YES,
2: Option.ABSTAIN,
3: Option.NO,
4: Option.NO_WITH_VETO,
};

export interface Data {
proposal_id: string;
voter: AccAddress;
option?: number; // undefined except proposals in voting status
options: {
option: number;
weight: string; // Dec
}[];
}
}
2 changes: 1 addition & 1 deletion src/core/gov/msgs/MsgSubmitProposal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Coins } from '../../Coins';
import { Proposal } from '../../Proposal';
import { Proposal } from '../Proposal';
import { JSONSerializable } from '../../../util/json';
import { AccAddress } from '../../bech32';

Expand Down
21 changes: 6 additions & 15 deletions src/core/gov/msgs/MsgVote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,14 @@ export class MsgVote extends JSONSerializable<MsgVote.Data> {
}

export namespace MsgVote {
/** Voting options */
export enum Option {
/** - */
EMPTY = 'Empty',

/** Vote yes */
YES = 'Yes',

/** Do not vote */
ABSTAIN = 'Abstain',

/** Vote no */
NO = 'No',

/** Vote No with the option to veto if passed */
NO_WITH_VETO = 'NoWithVeto',
EMPTY = 0,
YES = 1,
ABSTAIN = 2,
NO = 3,
NO_WITH_VETO = 4,
}

export interface Data {
type: 'gov/MsgVote';
value: {
Expand Down
56 changes: 56 additions & 0 deletions src/core/gov/msgs/MsgVoteWeighted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { JSONSerializable } from '../../../util/json';
import { AccAddress } from '../../bech32';
import { MsgVote } from './MsgVote';

/**
* Weighted vote for a proposal
*/
export class MsgVoteWeighted extends JSONSerializable<MsgVoteWeighted.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 options: MsgVoteWeighted.Options
) {
super();
}

public static fromData(data: MsgVoteWeighted.Data): MsgVoteWeighted {
const {
value: { proposal_id, voter, options },
} = data;
return new MsgVoteWeighted(Number.parseInt(proposal_id), voter, options);
}

public toData(): MsgVoteWeighted.Data {
const { proposal_id, voter, options } = this;
return {
type: 'gov/MsgVoteWeighted',
value: {
proposal_id: proposal_id.toFixed(),
voter,
options,
},
};
}
}

export namespace MsgVoteWeighted {
export type Options = {
option: MsgVote.Option;
weight: string;
}[];

export interface Data {
type: 'gov/MsgVoteWeighted';
value: {
proposal_id: string;
voter: AccAddress;
options: Options;
};
}
}
1 change: 1 addition & 0 deletions src/core/gov/msgs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MsgDeposit } from './MsgDeposit';
export * from './MsgDeposit';
export * from './MsgSubmitProposal';
export * from './MsgVote';
export * from './MsgVoteWeighted';

export type GovMsg = MsgDeposit | MsgSubmitProposal | MsgVote;

Expand Down
4 changes: 2 additions & 2 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export * from './Coins';
export * from './Denom';
export * from './Msg';
export * from './numeric';
export * from './Proposal';
export * from './PublicKey';
export * from './StdFee';
export * from './StdSignature';
Expand All @@ -13,7 +12,6 @@ export * from './StdTx';
export * from './TxInfo';
export * from './ValidatorSet';
export * from './Deposit';
export * from './Vote';

// Auth
export * from './auth/Account';
Expand All @@ -29,6 +27,8 @@ export * from './distribution/proposals';
// Governance
export * from './gov/msgs';
export * from './gov/proposals';
export * from './gov/Proposal';
export * from './gov/Vote';

// Market
export * from './market/msgs';
Expand Down

0 comments on commit 9021bcd

Please sign in to comment.