Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MsgVoteWeighted #1224

Merged
merged 5 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@ jobs:
background: true
- run:
name: Start simapp
command: ./scripts/simapp42/start.sh
command: ./scripts/simapp44/start.sh
background: true
- run:
name: Start slow simapp
command: ./scripts/simapp42/slow_start.sh
command: ./scripts/simapp44/slow_start.sh
background: true
- run:
name: Start Tendermint blockchains
Expand Down Expand Up @@ -278,8 +278,8 @@ jobs:
- run:
environment:
HTTPSERVER_ENABLED: 1
SIMAPP42_ENABLED: 1
SLOW_SIMAPP42_ENABLED: 1
SIMAPP44_ENABLED: 1
SLOW_SIMAPP44_ENABLED: 1
TENDERMINT_ENABLED: 1
SOCKETSERVER_ENABLED: 1
SKIP_BUILD: 1
Expand All @@ -297,8 +297,8 @@ jobs:
working_directory: packages/cli
environment:
HTTPSERVER_ENABLED: 1
SIMAPP42_ENABLED: 1
SLOW_SIMAPP42_ENABLED: 1
SIMAPP44_ENABLED: 1
SLOW_SIMAPP44_ENABLED: 1
TENDERMINT_ENABLED: 1
SOCKETSERVER_ENABLED: 1
SKIP_BUILD: 1
Expand All @@ -310,8 +310,8 @@ jobs:
./scripts/httpserver/stop.sh
./scripts/socketserver/stop.sh
./scripts/tendermint/all_stop.sh
./scripts/simapp42/stop.sh
./scripts/simapp42/slow_stop.sh
./scripts/simapp44/stop.sh
./scripts/simapp44/slow_stop.sh
./scripts/wasmd/stop.sh
test-chrome:
machine:
Expand Down Expand Up @@ -339,11 +339,11 @@ jobs:
background: true
- run:
name: Start simapp
command: ./scripts/simapp42/start.sh
command: ./scripts/simapp44/start.sh
background: true
- run:
name: Start slow simapp
command: ./scripts/simapp42/slow_start.sh
command: ./scripts/simapp44/slow_start.sh
background: true
- run:
name: Start Tendermint blockchains
Expand Down Expand Up @@ -396,8 +396,8 @@ jobs:
- run:
environment:
HTTPSERVER_ENABLED: 1
SIMAPP42_ENABLED: 1
SLOW_SIMAPP42_ENABLED: 1
SIMAPP44_ENABLED: 1
SLOW_SIMAPP44_ENABLED: 1
TENDERMINT_ENABLED: 1
SOCKETSERVER_ENABLED: 1
SKIP_BUILD: 1
Expand All @@ -409,8 +409,8 @@ jobs:
./scripts/httpserver/stop.sh
./scripts/socketserver/stop.sh
./scripts/tendermint/all_stop.sh
./scripts/simapp42/stop.sh
./scripts/simapp42/slow_stop.sh
./scripts/simapp44/stop.sh
./scripts/simapp44/slow_stop.sh
./scripts/wasmd/stop.sh
coverage:
machine:
Expand All @@ -437,11 +437,11 @@ jobs:
background: true
- run:
name: Start simapp
command: ./scripts/simapp42/start.sh
command: ./scripts/simapp44/start.sh
background: true
- run:
name: Start slow simapp
command: ./scripts/simapp42/slow_start.sh
command: ./scripts/simapp44/slow_start.sh
background: true
- run:
name: Start Tendermint blockchains
Expand Down Expand Up @@ -494,8 +494,8 @@ jobs:
- run:
environment:
HTTPSERVER_ENABLED: 1
SIMAPP42_ENABLED: 1
SLOW_SIMAPP42_ENABLED: 1
SIMAPP44_ENABLED: 1
SLOW_SIMAPP44_ENABLED: 1
TENDERMINT_ENABLED: 1
SOCKETSERVER_ENABLED: 1
SKIP_BUILD: 1
Expand All @@ -510,8 +510,8 @@ jobs:
./scripts/httpserver/stop.sh
./scripts/socketserver/stop.sh
./scripts/tendermint/all_stop.sh
./scripts/simapp42/stop.sh
./scripts/simapp42/slow_stop.sh
./scripts/simapp44/stop.sh
./scripts/simapp44/slow_stop.sh
./scripts/wasmd/stop.sh
docs-build:
docker:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ and this project adheres to
- @cosmjs/cosmwasm-stargate: Add `SigningCosmWasmClient.executeMultiple`
([#1072]).
- @cosmjs/math: Add `{Uint32,Int53,Uint53,Uint64}.toBigInt` converter methods.
- @cosmjs/stargate: Add missing exports `AminoMsgTransfer`/`isAminoMsgTransfer`.
- @cosmjs/stargate: Add support for `MsgVoteWeighted` (register by default and
create Amino JSON converters) ([#1224]).

[#1072]: https://github.com/cosmos/cosmjs/issues/1072
[#1154]: https://github.com/cosmos/cosmjs/issues/1154
[#1176]: https://github.com/cosmos/cosmjs/pull/1176
[#1224]: https://github.com/cosmos/cosmjs/pull/1224

### Fixed

Expand Down
6 changes: 6 additions & 0 deletions packages/stargate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ export {
AminoMsgSetWithdrawAddress,
AminoMsgSubmitEvidence,
AminoMsgSubmitProposal,
AminoMsgTransfer,
AminoMsgUndelegate,
AminoMsgUnjail,
AminoMsgVerifyInvariant,
AminoMsgVote,
AminoMsgVoteWeighted,
AminoMsgWithdrawDelegatorReward,
AminoMsgWithdrawValidatorCommission,
AuthExtension,
Expand All @@ -38,10 +40,12 @@ export {
isAminoMsgSetWithdrawAddress,
isAminoMsgSubmitEvidence,
isAminoMsgSubmitProposal,
isAminoMsgTransfer,
isAminoMsgUndelegate,
isAminoMsgUnjail,
isAminoMsgVerifyInvariant,
isAminoMsgVote,
isAminoMsgVoteWeighted,
isAminoMsgWithdrawDelegatorReward,
isAminoMsgWithdrawValidatorCommission,
isMsgDelegateEncodeObject,
Expand All @@ -51,6 +55,7 @@ export {
isMsgTransferEncodeObject,
isMsgUndelegateEncodeObject,
isMsgVoteEncodeObject,
isMsgVoteWeightedEncodeObject,
isMsgWithdrawDelegatorRewardEncodeObject,
MintExtension,
MintParams,
Expand All @@ -61,6 +66,7 @@ export {
MsgTransferEncodeObject,
MsgUndelegateEncodeObject,
MsgVoteEncodeObject,
MsgVoteWeightedEncodeObject,
MsgWithdrawDelegatorRewardEncodeObject,
setupAuthExtension,
setupBankExtension,
Expand Down
58 changes: 57 additions & 1 deletion packages/stargate/src/modules/gov/aminomessages.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { TextProposal, VoteOption } from "cosmjs-types/cosmos/gov/v1beta1/gov";
import { MsgDeposit, MsgSubmitProposal, MsgVote } from "cosmjs-types/cosmos/gov/v1beta1/tx";
import { MsgDeposit, MsgSubmitProposal, MsgVote, MsgVoteWeighted } from "cosmjs-types/cosmos/gov/v1beta1/tx";
import Long from "long";

import { AminoTypes } from "../../aminotypes";
import {
AminoMsgDeposit,
AminoMsgSubmitProposal,
AminoMsgVote,
AminoMsgVoteWeighted,
createGovAminoConverters,
} from "./aminomessages";

Expand Down Expand Up @@ -90,6 +91,34 @@ describe("AminoTypes", () => {
};
expect(aminoMsg).toEqual(expected);
});

it("works for MsgVoteWeighted", () => {
const msg: MsgVoteWeighted = {
proposalId: Long.fromNumber(5),
voter: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k",
options: [
{ option: VoteOption.VOTE_OPTION_NO_WITH_VETO, weight: "700000000000000000" /* 0.7 */ },
{ option: VoteOption.VOTE_OPTION_NO, weight: "300000000000000000" /* 0.3 */ },
],
};
const aminoTypes = new AminoTypes(createGovAminoConverters());
const aminoMsg = aminoTypes.toAmino({
typeUrl: "/cosmos.gov.v1beta1.MsgVoteWeighted",
value: msg,
});
const expected: AminoMsgVoteWeighted = {
type: "cosmos-sdk/MsgVoteWeighted",
value: {
proposal_id: "5",
voter: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k",
options: [
{ option: VoteOption.VOTE_OPTION_NO_WITH_VETO, weight: "0.700000000000000000" },
{ option: VoteOption.VOTE_OPTION_NO, weight: "0.300000000000000000" },
],
},
};
expect(aminoMsg).toEqual(expected);
});
});

describe("fromAmino", () => {
Expand Down Expand Up @@ -167,5 +196,32 @@ describe("AminoTypes", () => {
value: expectedValue,
});
});

it("works for MsgVoteWeighted", () => {
const aminoMsg: AminoMsgVoteWeighted = {
type: "cosmos-sdk/MsgVoteWeighted",
value: {
proposal_id: "5",
voter: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k",
options: [
{ option: 4, weight: "0.750000000000000000" },
{ option: 3, weight: "0.250000000000000000" },
],
},
};
const msg = new AminoTypes(createGovAminoConverters()).fromAmino(aminoMsg);
const expectedValue: MsgVoteWeighted = {
proposalId: Long.fromNumber(5),
voter: "cosmos1xy4yqngt0nlkdcenxymg8tenrghmek4nmqm28k",
options: [
{ option: VoteOption.VOTE_OPTION_NO_WITH_VETO, weight: "750000000000000000" },
{ option: VoteOption.VOTE_OPTION_NO, weight: "250000000000000000" },
],
};
expect(msg).toEqual({
typeUrl: "/cosmos.gov.v1beta1.MsgVoteWeighted",
value: expectedValue,
});
});
});
});
55 changes: 54 additions & 1 deletion packages/stargate/src/modules/gov/aminomessages.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { AminoMsg, Coin } from "@cosmjs/amino";
import { Decimal } from "@cosmjs/math";
import { assert, assertDefinedAndNotNull, isNonNullObject } from "@cosmjs/utils";
import { TextProposal, voteOptionFromJSON } from "cosmjs-types/cosmos/gov/v1beta1/gov";
import { MsgDeposit, MsgSubmitProposal, MsgVote } from "cosmjs-types/cosmos/gov/v1beta1/tx";
import { MsgDeposit, MsgSubmitProposal, MsgVote, MsgVoteWeighted } from "cosmjs-types/cosmos/gov/v1beta1/tx";
import { Any } from "cosmjs-types/google/protobuf/any";
import Long from "long";

import { AminoConverters } from "../../aminotypes";
import { decodeCosmosSdkDecFromProto } from "../../queryclient";

/** Supports submitting arbitrary proposal content. */
export interface AminoMsgSubmitProposal extends AminoMsg {
Expand Down Expand Up @@ -59,6 +61,32 @@ export function isAminoMsgVote(msg: AminoMsg): msg is AminoMsgVote {
return msg.type === "cosmos-sdk/MsgVote";
}

/**
* @see https://github.com/cosmos/cosmos-sdk/blob/v0.44.5/x/gov/types/tx.pb.go#L196-L203
* @see https://github.com/cosmos/cosmos-sdk/blob/v0.44.5/x/gov/types/gov.pb.go#L124-L130
*/
export interface AminoMsgVoteWeighted extends AminoMsg {
readonly type: "cosmos-sdk/MsgVoteWeighted";
readonly value: {
readonly proposal_id: string;
/** Bech32 account address */
readonly voter: string;
readonly options: Array<{
/**
* VoteOption as integer from 0 to 4 🤷‍
*
* @see https://github.com/cosmos/cosmos-sdk/blob/v0.44.5/x/gov/types/gov.pb.go#L35-L49
*/
readonly option: number;
readonly weight: string;
}>;
};
}

export function isAminoMsgVoteWeighted(msg: AminoMsg): msg is AminoMsgVoteWeighted {
return (msg as AminoMsgVoteWeighted).type === "cosmos-sdk/MsgVoteWeighted";
}

/** Submits a deposit to an existing proposal */
export interface AminoMsgDeposit extends AminoMsg {
readonly type: "cosmos-sdk/MsgDeposit";
Expand Down Expand Up @@ -110,6 +138,31 @@ export function createGovAminoConverters(): AminoConverters {
};
},
},
"/cosmos.gov.v1beta1.MsgVoteWeighted": {
aminoType: "cosmos-sdk/MsgVoteWeighted",
toAmino: ({ options, proposalId, voter }: MsgVoteWeighted): AminoMsgVoteWeighted["value"] => {
return {
options: options.map((o) => ({
option: o.option,
// Weight is between 0 and 1, so we always have 20 characters when printing all trailing
// zeros (e.g. "0.700000000000000000" or "1.000000000000000000")
weight: decodeCosmosSdkDecFromProto(o.weight).toString().padEnd(20, "0"),
})),
proposal_id: proposalId.toString(),
voter: voter,
};
},
fromAmino: ({ options, proposal_id, voter }: AminoMsgVoteWeighted["value"]): MsgVoteWeighted => {
return {
proposalId: Long.fromString(proposal_id),
voter: voter,
options: options.map((o) => ({
option: voteOptionFromJSON(o.option),
weight: Decimal.fromUserInput(o.weight, 18).atomics,
})),
};
},
},
"/cosmos.gov.v1beta1.MsgSubmitProposal": {
aminoType: "cosmos-sdk/MsgSubmitProposal",
toAmino: ({
Expand Down
Loading