sdk38: Move proposal submission into test script

This commit is contained in:
willclarktech 2020-07-15 15:11:24 +02:00
parent 55917c6cbf
commit a00c5e8f8d
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
3 changed files with 121 additions and 144 deletions

View File

@ -1,20 +1,114 @@
/* eslint-disable @typescript-eslint/camelcase */
import { sleep } from "@cosmjs/utils";
import { coins } from "../coins";
import { isPostTxFailure } from "../cosmosclient";
import { makeSignBytes } from "../encoding";
import { SigningCosmosClient } from "../signingcosmosclient";
import {
dateTimeStampMatcher,
nonNegativeIntegerMatcher,
pendingWithoutWasmd,
wasmd,
wasmdEnabled,
} from "../testutils.spec";
import { Secp256k1Wallet } from "../wallet";
import { GovExtension, GovParametersType, setupGovExtension } from "./gov";
import { LcdClient } from "./lcdclient";
const governorAddress = "cosmos14qemq0vw6y3gc3u3e0aty2e764u4gs5le3hada";
function makeGovClient(apiUrl: string): LcdClient & GovExtension {
return LcdClient.withExtensions({ apiUrl }, setupGovExtension);
}
describe("GovExtension", () => {
const httpUrl = "http://localhost:1317";
const alice = {
mnemonic: "enlist hip relief stomach skate base shallow young switch frequent cry park",
address0: "cosmos14qemq0vw6y3gc3u3e0aty2e764u4gs5le3hada",
};
const defaultFee = {
amount: coins(25000, "ucosm"),
gas: "1500000", // 1.5 million
};
let proposalId: string;
beforeAll(async () => {
if (wasmdEnabled()) {
const wallet = await Secp256k1Wallet.fromMnemonic(alice.mnemonic);
const client = new SigningCosmosClient(httpUrl, alice.address0, wallet, {});
const chainId = await client.getChainId();
const proposalMsg = {
type: "cosmos-sdk/MsgSubmitProposal",
value: {
content: {
type: "cosmos-sdk/TextProposal",
value: {
description: "This proposal proposes to test whether this proposal passes",
title: "Test Proposal",
},
},
proposer: alice.address0,
initial_deposit: coins(25000000, "ustake"),
},
};
const proposalMemo = "Test proposal for wasmd";
const { accountNumber: proposalAccountNumber, sequence: proposalSequence } = await client.getNonce();
const proposalSignBytes = makeSignBytes(
[proposalMsg],
defaultFee,
chainId,
proposalMemo,
proposalAccountNumber,
proposalSequence,
);
const proposalSignature = await wallet.sign(alice.address0, proposalSignBytes);
const proposalTx = {
msg: [proposalMsg],
fee: defaultFee,
memo: proposalMemo,
signatures: [proposalSignature],
};
const proposalReceipt = await client.postTx(proposalTx);
if (isPostTxFailure(proposalReceipt)) {
throw new Error("Proposal submission failed");
}
proposalId = proposalReceipt.logs[0].events
.find(({ type }) => type === "submit_proposal")!
.attributes.find(({ key }) => key === "proposal_id")!.value;
const voteMsg = {
type: "cosmos-sdk/MsgVote",
value: {
proposal_id: proposalId,
voter: alice.address0,
option: "Yes",
},
};
const voteMemo = "Test vote for wasmd";
const { accountNumber: voteAccountNumber, sequence: voteSequence } = await client.getNonce();
const voteSignBytes = makeSignBytes(
[voteMsg],
defaultFee,
chainId,
voteMemo,
voteAccountNumber,
voteSequence,
);
const voteSignature = await wallet.sign(alice.address0, voteSignBytes);
const voteTx = {
msg: [voteMsg],
fee: defaultFee,
memo: voteMemo,
signatures: [voteSignature],
};
await client.postTx(voteTx);
await sleep(75); // wait until transactions are indexed
}
});
describe("parametersByType", () => {
it("works for deposit", async () => {
pendingWithoutWasmd();
@ -64,27 +158,24 @@ describe("GovExtension", () => {
pendingWithoutWasmd();
const client = makeGovClient(wasmd.endpoint);
const response = await client.gov.proposals();
expect(response).toEqual({
height: jasmine.stringMatching(nonNegativeIntegerMatcher),
result: [
{
content: {
type: "cosmos-sdk/TextProposal",
value: {
title: "Test Proposal",
description: "This proposal proposes to test whether this proposal passes",
},
},
id: "1",
proposal_status: "VotingPeriod",
final_tally_result: Object({ yes: "0", abstain: "0", no: "0", no_with_veto: "0" }),
submit_time: jasmine.stringMatching(dateTimeStampMatcher),
deposit_end_time: jasmine.stringMatching(dateTimeStampMatcher),
total_deposit: [{ denom: "ustake", amount: "25000000" }],
voting_start_time: jasmine.stringMatching(dateTimeStampMatcher),
voting_end_time: jasmine.stringMatching(dateTimeStampMatcher),
expect(response.height).toMatch(nonNegativeIntegerMatcher);
expect(response.result.length).toBeGreaterThanOrEqual(1);
expect(response.result[response.result.length - 1]).toEqual({
content: {
type: "cosmos-sdk/TextProposal",
value: {
title: "Test Proposal",
description: "This proposal proposes to test whether this proposal passes",
},
],
},
id: proposalId,
proposal_status: "VotingPeriod",
final_tally_result: { yes: "0", abstain: "0", no: "0", no_with_veto: "0" },
submit_time: jasmine.stringMatching(dateTimeStampMatcher),
deposit_end_time: jasmine.stringMatching(dateTimeStampMatcher),
total_deposit: [{ denom: "ustake", amount: "25000000" }],
voting_start_time: jasmine.stringMatching(dateTimeStampMatcher),
voting_end_time: jasmine.stringMatching(dateTimeStampMatcher),
});
});
});
@ -93,7 +184,6 @@ describe("GovExtension", () => {
it("works", async () => {
pendingWithoutWasmd();
const client = makeGovClient(wasmd.endpoint);
const proposalId = "1";
const response = await client.gov.proposal(proposalId);
expect(response).toEqual({
height: jasmine.stringMatching(nonNegativeIntegerMatcher),
@ -107,7 +197,7 @@ describe("GovExtension", () => {
},
id: proposalId,
proposal_status: "VotingPeriod",
final_tally_result: Object({ yes: "0", abstain: "0", no: "0", no_with_veto: "0" }),
final_tally_result: { yes: "0", abstain: "0", no: "0", no_with_veto: "0" },
submit_time: jasmine.stringMatching(dateTimeStampMatcher),
deposit_end_time: jasmine.stringMatching(dateTimeStampMatcher),
total_deposit: [{ denom: "ustake", amount: "25000000" }],
@ -122,13 +212,12 @@ describe("GovExtension", () => {
it("works", async () => {
pendingWithoutWasmd();
const client = makeGovClient(wasmd.endpoint);
const proposalId = "1";
const response = await client.gov.proposer(proposalId);
expect(response).toEqual({
height: jasmine.stringMatching(nonNegativeIntegerMatcher),
result: {
proposal_id: proposalId,
proposer: governorAddress,
proposer: alice.address0,
},
});
});
@ -138,14 +227,13 @@ describe("GovExtension", () => {
it("works", async () => {
pendingWithoutWasmd();
const client = makeGovClient(wasmd.endpoint);
const proposalId = "1";
const response = await client.gov.deposits(proposalId);
expect(response).toEqual({
height: jasmine.stringMatching(nonNegativeIntegerMatcher),
result: [
{
proposal_id: proposalId,
depositor: governorAddress,
depositor: alice.address0,
amount: [{ denom: "ustake", amount: "25000000" }],
},
],
@ -157,13 +245,12 @@ describe("GovExtension", () => {
it("works", async () => {
pendingWithoutWasmd();
const client = makeGovClient(wasmd.endpoint);
const proposalId = "1";
const response = await client.gov.deposit(proposalId, governorAddress);
const response = await client.gov.deposit(proposalId, alice.address0);
expect(response).toEqual({
height: jasmine.stringMatching(nonNegativeIntegerMatcher),
result: {
proposal_id: proposalId,
depositor: governorAddress,
depositor: alice.address0,
amount: [{ denom: "ustake", amount: "25000000" }],
},
});
@ -174,7 +261,6 @@ describe("GovExtension", () => {
it("works", async () => {
pendingWithoutWasmd();
const client = makeGovClient(wasmd.endpoint);
const proposalId = "1";
const response = await client.gov.tally(proposalId);
expect(response).toEqual({
height: jasmine.stringMatching(nonNegativeIntegerMatcher),
@ -192,14 +278,13 @@ describe("GovExtension", () => {
it("works", async () => {
pendingWithoutWasmd();
const client = makeGovClient(wasmd.endpoint);
const proposalId = "1";
const response = await client.gov.votes(proposalId);
expect(response).toEqual({
height: jasmine.stringMatching(nonNegativeIntegerMatcher),
result: [
{
proposal_id: proposalId,
voter: governorAddress,
voter: alice.address0,
option: "Yes",
},
],
@ -211,12 +296,11 @@ describe("GovExtension", () => {
it("works", async () => {
pendingWithoutWasmd();
const client = makeGovClient(wasmd.endpoint);
const proposalId = "1";
const response = await client.gov.vote(proposalId, governorAddress);
const response = await client.gov.vote(proposalId, alice.address0);
expect(response).toEqual({
height: jasmine.stringMatching(nonNegativeIntegerMatcher),
result: {
voter: governorAddress,
voter: alice.address0,
proposal_id: proposalId,
option: "Yes",
},

View File

@ -19,4 +19,3 @@ SCRIPT_DIR="$(realpath "$(dirname "$0")")"
"$SCRIPT_DIR/deploy_erc20.js"
"$SCRIPT_DIR/deploy_nameservice.js"
"$SCRIPT_DIR/deploy_staking.js"
"$SCRIPT_DIR/submit_proposal.js"

View File

@ -1,106 +0,0 @@
#!/usr/bin/env node
/* eslint-disable @typescript-eslint/camelcase */
const { coins, Secp256k1Wallet, SigningCosmosClient, makeSignBytes } = require("@cosmjs/sdk38");
const httpUrl = "http://localhost:1317";
const alice = {
mnemonic: "enlist hip relief stomach skate base shallow young switch frequent cry park",
address0: "cosmos14qemq0vw6y3gc3u3e0aty2e764u4gs5le3hada",
};
const defaultFee = {
amount: coins(25000, "ucosm"),
gas: "1500000", // 1.5 million
};
async function submitProposal(client, wallet) {
const chainId = await client.getChainId();
const proposalMsg = {
type: "cosmos-sdk/MsgSubmitProposal",
value: {
content: {
type: "cosmos-sdk/TextProposal",
value: {
description: "This proposal proposes to test whether this proposal passes",
title: "Test Proposal",
},
},
proposer: alice.address0,
initial_deposit: coins(25000000, "ustake"),
},
};
const proposalMemo = "Test proposal for wasmd";
const { accountNumber: proposalAccountNumber, sequence: proposalSequence } = await client.getNonce();
const proposalSignBytes = makeSignBytes(
[proposalMsg],
defaultFee,
chainId,
proposalMemo,
proposalAccountNumber,
proposalSequence,
);
const proposalSignature = await wallet.sign(alice.address0, proposalSignBytes);
const proposalTx = {
msg: [proposalMsg],
fee: defaultFee,
memo: proposalMemo,
signatures: [proposalSignature],
};
return client.postTx(proposalTx);
}
async function submitVote(client, wallet, proposalId) {
const chainId = await client.getChainId();
const voteMsg = {
type: "cosmos-sdk/MsgVote",
value: {
proposal_id: proposalId,
voter: alice.address0,
option: "Yes",
},
};
const voteMemo = "Test vote for wasmd";
const { accountNumber: voteAccountNumber, sequence: voteSequence } = await client.getNonce();
const voteSignBytes = makeSignBytes(
[voteMsg],
defaultFee,
chainId,
voteMemo,
voteAccountNumber,
voteSequence,
);
const voteSignature = await wallet.sign(alice.address0, voteSignBytes);
const voteTx = {
msg: [voteMsg],
fee: defaultFee,
memo: voteMemo,
signatures: [voteSignature],
};
return client.postTx(voteTx);
}
async function main() {
const wallet = await Secp256k1Wallet.fromMnemonic(alice.mnemonic);
const client = new SigningCosmosClient(httpUrl, alice.address0, wallet, {});
const proposalReceipt = await submitProposal(client, wallet);
console.info(`Proposal submission succeeded. Receipt: ${JSON.stringify(proposalReceipt)}`);
const proposalId = proposalReceipt.logs[0].events
.find(({ type }) => type === "submit_proposal")
.attributes.find(({ key }) => key === "proposal_id").value;
const voteReceipt = await submitVote(client, wallet, proposalId);
console.info(`Vote succeeded. Receipt: ${JSON.stringify(voteReceipt)}`);
}
main().then(
() => {
console.info("Done submitting proposal.");
process.exit(0);
},
(error) => {
console.error(error);
process.exit(1);
},
);