From 3f2bdd34f871667dd743fe9b5d76efd351190554 Mon Sep 17 00:00:00 2001 From: nabarun Date: Wed, 30 Mar 2022 10:51:03 +0530 Subject: [PATCH] Send vote message in gov module --- README.md | 22 +++++++++++++++++++ src/index.test.ts | 11 +++++++++- src/index.ts | 56 ++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 82 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a83ba0f..176de83 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,18 @@ Follow these steps to run the tests: - Copy the private key and assign it to variable `SENDER_PRIVATE_KEY` in the [test file](./src/index.test.ts). +- Perform the following steps for testing gov module: + + - Submit a proposal + ```bash + ethermintd tx gov submit-proposal --title="Test Proposal" --description="My awesome proposal" --type="Text" --deposit="10000000aphoton" --from mykey --fees 20aphoton + ``` + + - Query for proposal + ```bash + ethermintd query gov proposals + ``` + - Run the test in chiba-clonk-client repo: ```bash @@ -50,3 +62,13 @@ Follow these steps to run the tests: # Example ethermintd query bank balances ethm1ayxjyxxa3z9z0rjff7rpr67h8aqfgn2t9009zc ``` + +- Check votes for proposal id 1 + + ```bash + # Query votes + ethermintd query gov votes 1 + + # Check votes tally + ethermintd query gov tally 1 + ``` diff --git a/src/index.test.ts b/src/index.test.ts index 592b822..c9f23cc 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1,4 +1,4 @@ -import { sendTokens } from './index' +import { sendTokens, sendVote } from './index' const SENDER_ADDRESS = 'ethm1ayxjyxxa3z9z0rjff7rpr67h8aqfgn2t9009zc'; const SENDER_PRIVATE_KEY = '5041b1ace7ea207794f4c5c1c5f987ff8a9d782f194ef5b24bcffaafaf4a019f'; @@ -7,3 +7,12 @@ const TO_ADDRESS = 'ethm12x63cgg82ek97cf8ew9hf6r7je75s5w2smejqv'; test('Send tokens', async () => { await sendTokens(SENDER_PRIVATE_KEY, SENDER_ADDRESS, TO_ADDRESS) }); + +test('Send vote', async () => { + const voteParams = { + proposalId: 1, + option: 1 + } + + await sendVote(SENDER_PRIVATE_KEY, SENDER_ADDRESS, voteParams) +}) diff --git a/src/index.ts b/src/index.ts index 078ea8f..62e1999 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,14 @@ import axios from "axios"; -import { generateEndpointAccount, generateEndpointBroadcast, generatePostBodyBroadcast } from '@tharsis/provider'; -import { createMessageSend, createTxRawEIP712, signatureToWeb3Extension } from '@tharsis/transactions' import { MessageTypes, signTypedData, SignTypedDataVersion } from "@metamask/eth-sig-util"; +import { generateEndpointAccount, generateEndpointBroadcast, generatePostBodyBroadcast } from '@tharsis/provider'; +import { + createMessageSend, + createTxRawEIP712, + signatureToWeb3Extension, + createTxMsgVote, + Chain, + Sender +} from '@tharsis/transactions' const ETHERMINT_REST_ENDPOINT = 'http://127.0.0.1:1317' @@ -13,6 +20,11 @@ interface TypedMessageDomain { salt?: ArrayBuffer; } +interface VoteParams { + proposalId: number; + option: number; +} + export const sendTokens = async (senderPrivateKey: string, senderAddress: string, destinationAddress: string) => { let { data: addrData} = await axios.get(`${ETHERMINT_REST_ENDPOINT}${generateEndpointAccount(senderAddress)}`) @@ -44,6 +56,38 @@ export const sendTokens = async (senderPrivateKey: string, senderAddress: string // Create a MsgSend transaction. const msg = createMessageSend(chain, sender, fee, memo, params) + + await signAndSendMessage(senderPrivateKey, chain, sender, msg) +} + +export const sendVote = async (senderPrivateKey: string, senderAddress: string, params: VoteParams) => { + let { data: addrData} = await axios.get(`${ETHERMINT_REST_ENDPOINT}${generateEndpointAccount(senderAddress)}`) + + const chain = { + chainId: 9000, + cosmosChainId: 'ethermint_9000-1', + } + + const sender = { + accountAddress: addrData.account.base_account.address, + sequence: addrData.account.base_account.sequence, + accountNumber: addrData.account.base_account.account_number, + pubkey: addrData.account.base_account.pub_key.key, + } + + const fee = { + amount: '20', + denom: 'aphoton', + gas: '200000', + } + + const memo = '' + + const msg = createTxMsgVote(chain, sender, fee, memo, params) + await signAndSendMessage(senderPrivateKey, chain, sender, msg) +} + +const signAndSendMessage = async (senderPrivateKey: string, chain: Chain, sender: Sender, msg: any) => { const eipMessageDomain: any = msg.eipToSign.domain; // Sign transaction. @@ -60,16 +104,16 @@ export const sendTokens = async (senderPrivateKey: string, senderAddress: string let extension = signatureToWeb3Extension(chain, sender, signature) - // Create the txRaw + // Create the txRaw. let rawTx = createTxRawEIP712(msg.legacyAmino.body, msg.legacyAmino.authInfo, extension) const body = generatePostBodyBroadcast(rawTx) - // Broadcast it - await axios.post( + // Broadcast transaction. + return axios.post( `${ETHERMINT_REST_ENDPOINT}${generateEndpointBroadcast()}`, JSON.parse(body) ) - // TODO: Check for successful broadcast + // TODO: Check for successful broadcast. }