From f4f23e30de838fa43ad491beb5772905ac8886d4 Mon Sep 17 00:00:00 2001 From: fly33499 Date: Mon, 18 Oct 2021 22:07:20 +0900 Subject: [PATCH] refactor: gov module param optimization --- sdk/FirmaGovService.ts | 51 +++++++++++++++++++++++++----------------- test/16.gov_tx.test.ts | 24 ++++++++------------ 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/sdk/FirmaGovService.ts b/sdk/FirmaGovService.ts index ba9e700..2a67aa6 100644 --- a/sdk/FirmaGovService.ts +++ b/sdk/FirmaGovService.ts @@ -28,7 +28,7 @@ export class FirmaGovService { } } - private async getSignedTxSubmitTextProposal(wallet: FirmaWalletService, title: string, description: string, initialDepositFCT: number, proposer: string, txMisc: TxMisc = DefaultTxMisc): Promise { + private async getSignedTxSubmitTextProposal(wallet: FirmaWalletService, title: string, description: string, initialDepositFCT: number, txMisc: TxMisc = DefaultTxMisc): Promise { try { let txClient = new GovTxClient(wallet.getRawWallet(), this._config.rpcAddress); @@ -45,6 +45,7 @@ export class FirmaGovService { value: Uint8Array.from(TextProposal.encode(proposal).finish()), }); + let proposer = await wallet.getAddress(); let message = txClient.msgSubmitProposal({ content: content, initialDeposit: [sendAmount], proposer: proposer }); return await txClient.sign([message], getSignAndBroadcastOption(this._config.denom, txMisc)); @@ -54,7 +55,7 @@ export class FirmaGovService { } } - private async getSignedTxCancelSoftwareUpgradeProposal(wallet: FirmaWalletService, title: string, description: string, initialDepositFCT: number, proposer: string, txMisc: TxMisc = DefaultTxMisc): Promise { + private async getSignedTxCancelSoftwareUpgradeProposal(wallet: FirmaWalletService, title: string, description: string, initialDepositFCT: number, txMisc: TxMisc = DefaultTxMisc): Promise { try { let txClient = new GovTxClient(wallet.getRawWallet(), this._config.rpcAddress); @@ -71,7 +72,9 @@ export class FirmaGovService { value: Uint8Array.from(SoftwareUpgradeProposal.encode(proposal).finish()), }); + let proposer = await wallet.getAddress(); let message = txClient.msgSubmitProposal({ content: content, initialDeposit: [initialDepositAmount], proposer: proposer }); + return await txClient.sign([message], getSignAndBroadcastOption(this._config.denom, txMisc)); } catch (error) { @@ -80,7 +83,7 @@ export class FirmaGovService { } } - private async getSignedTxSoftwareUpgradeProposal(wallet: FirmaWalletService, title: string, description: string, initialDepositFCT: number, proposer: string, plan: SoftwareUpgradePlan, txMisc: TxMisc = DefaultTxMisc): Promise { + private async getSignedTxSoftwareUpgradeProposal(wallet: FirmaWalletService, title: string, description: string, initialDepositFCT: number, plan: SoftwareUpgradePlan, txMisc: TxMisc = DefaultTxMisc): Promise { try { let txClient = new GovTxClient(wallet.getRawWallet(), this._config.rpcAddress); @@ -98,6 +101,7 @@ export class FirmaGovService { value: Uint8Array.from(SoftwareUpgradeProposal.encode(proposal).finish()), }); + let proposer = await wallet.getAddress(); let message = txClient.msgSubmitProposal({ content: content, initialDeposit: [initialDepositAmount], proposer: proposer }); return await txClient.sign([message], getSignAndBroadcastOption(this._config.denom, txMisc)); @@ -107,7 +111,7 @@ export class FirmaGovService { } } - private async getSignedTxParameterChangeProposal(wallet: FirmaWalletService, title: string, description: string, initialDepositFCT: number, proposer: string, paramList: ParamChangeOption[], txMisc: TxMisc = DefaultTxMisc): Promise { + private async getSignedTxParameterChangeProposal(wallet: FirmaWalletService, title: string, description: string, initialDepositFCT: number, paramList: ParamChangeOption[], txMisc: TxMisc = DefaultTxMisc): Promise { try { let txClient = new GovTxClient(wallet.getRawWallet(), this._config.rpcAddress); @@ -125,6 +129,7 @@ export class FirmaGovService { value: Uint8Array.from(ParameterChangeProposal.encode(proposal).finish()), }); + let proposer = await wallet.getAddress(); let message = txClient.msgSubmitProposal({ content: content, initialDeposit: [initialDepositAmount], proposer: proposer }); return await txClient.sign([message], getSignAndBroadcastOption(this._config.denom, txMisc)); @@ -134,7 +139,7 @@ export class FirmaGovService { } } - private async getSignedTxCommunityPoolSpendProposal(wallet: FirmaWalletService, title: string, description: string, initialDepositFCT: number, proposer: string, amount: number, recipient: string, txMisc: TxMisc = DefaultTxMisc): Promise { + private async getSignedTxCommunityPoolSpendProposal(wallet: FirmaWalletService, title: string, description: string, initialDepositFCT: number, amount: number, recipient: string, txMisc: TxMisc = DefaultTxMisc): Promise { try { let txClient = new GovTxClient(wallet.getRawWallet(), this._config.rpcAddress); @@ -154,6 +159,7 @@ export class FirmaGovService { value: Uint8Array.from(CommunityPoolSpendProposal.encode(proposal).finish()), }); + let proposer = await wallet.getAddress(); let message = txClient.msgSubmitProposal({ content: content, initialDeposit: [initialDepositAmount], proposer: proposer }); return await txClient.sign([message], getSignAndBroadcastOption(this._config.denom, txMisc)); @@ -163,10 +169,10 @@ export class FirmaGovService { } } - public async submitCancelSoftwareUpgradeProposal(wallet: FirmaWalletService, title: string, description: string, initialDeposit: number, proposer: string, txMisc: TxMisc = DefaultTxMisc): Promise { + public async submitCancelSoftwareUpgradeProposal(wallet: FirmaWalletService, title: string, description: string, initialDeposit: number, txMisc: TxMisc = DefaultTxMisc): Promise { try { - let txRaw = await this.getSignedTxCancelSoftwareUpgradeProposal(wallet, title, description, initialDeposit, proposer, txMisc); + let txRaw = await this.getSignedTxCancelSoftwareUpgradeProposal(wallet, title, description, initialDeposit, txMisc); let txClient = new GovTxClient(wallet.getRawWallet(), this._config.rpcAddress); return await txClient.broadcast(txRaw); @@ -177,7 +183,7 @@ export class FirmaGovService { } } - public async submitSoftwareUpgradeProposalByHeight(wallet: FirmaWalletService, title: string, description: string, initialDeposit: number, proposer: string, upgradeName: string, height: Long, txMisc: TxMisc = DefaultTxMisc): Promise { + public async submitSoftwareUpgradeProposalByHeight(wallet: FirmaWalletService, title: string, description: string, initialDeposit: number, upgradeName: string, height: Long, txMisc: TxMisc = DefaultTxMisc): Promise { try { let plan = { name: upgradeName, @@ -187,7 +193,7 @@ export class FirmaGovService { upgradedClientState: undefined }; - let txRaw = await this.getSignedTxSoftwareUpgradeProposal(wallet, title, description, initialDeposit, proposer, plan, txMisc); + let txRaw = await this.getSignedTxSoftwareUpgradeProposal(wallet, title, description, initialDeposit, plan, txMisc); let txClient = new GovTxClient(wallet.getRawWallet(), this._config.rpcAddress); return await txClient.broadcast(txRaw); @@ -198,7 +204,7 @@ export class FirmaGovService { } } - public async submitSoftwareUpgradeProposalByTime(wallet: FirmaWalletService, title: string, description: string, initialDeposit: number, proposer: string, upgradeName: string, upgradeTime: Date, txMisc: TxMisc = DefaultTxMisc): Promise { + public async submitSoftwareUpgradeProposalByTime(wallet: FirmaWalletService, title: string, description: string, initialDeposit: number, upgradeName: string, upgradeTime: Date, txMisc: TxMisc = DefaultTxMisc): Promise { try { let plan = { name: upgradeName, @@ -208,7 +214,7 @@ export class FirmaGovService { upgradedClientState: undefined }; - let txRaw = await this.getSignedTxSoftwareUpgradeProposal(wallet, title, description, initialDeposit, proposer, plan, txMisc); + let txRaw = await this.getSignedTxSoftwareUpgradeProposal(wallet, title, description, initialDeposit, plan, txMisc); let txClient = new GovTxClient(wallet.getRawWallet(), this._config.rpcAddress); return await txClient.broadcast(txRaw); @@ -220,10 +226,10 @@ export class FirmaGovService { } - public async submitParameterChangeProposal(wallet: FirmaWalletService, title: string, description: string, initialDeposit: number, proposer: string, paramList: ParamChangeOption[], txMisc: TxMisc = DefaultTxMisc): Promise { + public async submitParameterChangeProposal(wallet: FirmaWalletService, title: string, description: string, initialDeposit: number, paramList: ParamChangeOption[], txMisc: TxMisc = DefaultTxMisc): Promise { try { - let txRaw = await this.getSignedTxParameterChangeProposal(wallet, title, description, initialDeposit, proposer, paramList, txMisc); + let txRaw = await this.getSignedTxParameterChangeProposal(wallet, title, description, initialDeposit, paramList, txMisc); let txClient = new GovTxClient(wallet.getRawWallet(), this._config.rpcAddress); return await txClient.broadcast(txRaw); @@ -234,10 +240,10 @@ export class FirmaGovService { } } - public async submitCommunityPoolSpendProposal(wallet: FirmaWalletService, title: string, description: string, initialDeposit: number, proposer: string, amount: number, recipient: string, txMisc: TxMisc = DefaultTxMisc): Promise { + public async submitCommunityPoolSpendProposal(wallet: FirmaWalletService, title: string, description: string, initialDeposit: number, amount: number, recipient: string, txMisc: TxMisc = DefaultTxMisc): Promise { try { - let txRaw = await this.getSignedTxCommunityPoolSpendProposal(wallet, title, description, initialDeposit, proposer, amount, recipient, txMisc); + let txRaw = await this.getSignedTxCommunityPoolSpendProposal(wallet, title, description, initialDeposit, amount, recipient, txMisc); let txClient = new GovTxClient(wallet.getRawWallet(), this._config.rpcAddress); return await txClient.broadcast(txRaw); @@ -249,10 +255,10 @@ export class FirmaGovService { } - public async submitTextProposal(wallet: FirmaWalletService, title: string, description: string, initialDeposit: number, proposer: string, txMisc: TxMisc = DefaultTxMisc): Promise { + public async submitTextProposal(wallet: FirmaWalletService, title: string, description: string, initialDeposit: number, txMisc: TxMisc = DefaultTxMisc): Promise { try { - let txRaw = await this.getSignedTxSubmitTextProposal(wallet, title, description, initialDeposit, proposer, txMisc); + let txRaw = await this.getSignedTxSubmitTextProposal(wallet, title, description, initialDeposit, txMisc); let txClient = new GovTxClient(wallet.getRawWallet(), this._config.rpcAddress); return await txClient.broadcast(txRaw); @@ -278,10 +284,11 @@ export class FirmaGovService { } } - public async vote(wallet: FirmaWalletService, proposalId: Long, option: VotingOption, txMisc: TxMisc = DefaultTxMisc): Promise { + public async vote(wallet: FirmaWalletService, proposalId: number, option: VotingOption, txMisc: TxMisc = DefaultTxMisc): Promise { try { - let txRaw = await this.getSignedTxVote(wallet, proposalId, option, txMisc); + const longId = Long.fromInt(proposalId); + let txRaw = await this.getSignedTxVote(wallet, longId, option, txMisc); let txClient = new GovTxClient(wallet.getRawWallet(), this._config.rpcAddress); return await txClient.broadcast(txRaw); @@ -310,9 +317,11 @@ export class FirmaGovService { } } - public async deposit(wallet: FirmaWalletService, proposalId: Long, amount: number, txMisc: TxMisc = DefaultTxMisc): Promise { + public async deposit(wallet: FirmaWalletService, proposalId: number, amount: number, txMisc: TxMisc = DefaultTxMisc): Promise { try { - let txRaw = await this.getSignedTxDeposit(wallet, proposalId, amount, txMisc); + + const longId = Long.fromInt(proposalId); + let txRaw = await this.getSignedTxDeposit(wallet, longId, amount, txMisc); let txClient = new GovTxClient(wallet.getRawWallet(), this._config.rpcAddress); return await txClient.broadcast(txRaw); diff --git a/test/16.gov_tx.test.ts b/test/16.gov_tx.test.ts index ea27d97..1c27d97 100644 --- a/test/16.gov_tx.test.ts +++ b/test/16.gov_tx.test.ts @@ -19,12 +19,11 @@ describe.skip('[16. Gov Tx Test]', () => { const wallet = await firma.Wallet.fromMnemonic(aliceMnemonic); - const proposer = await wallet.getAddress(); const initialDepositFCT = 10; const title = "test submit proposal"; const description = "test description"; - var result = await firma.Gov.submitTextProposal(wallet, title, description, initialDepositFCT, proposer); + var result = await firma.Gov.submitTextProposal(wallet, title, description, initialDepositFCT); console.log(result); expect(result.code).to.equal(0); @@ -35,14 +34,13 @@ describe.skip('[16. Gov Tx Test]', () => { const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); const bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); - const proposer = await aliceWallet.getAddress(); const initialDepositFCT = 10; const title = "Community spend proposal1"; const description = "This is a community spend proposal"; const amount = 1000; const recipient = await bobWallet.getAddress(); - var result = await firma.Gov.submitCommunityPoolSpendProposal(aliceWallet, title, description, initialDepositFCT, proposer, amount, recipient); + var result = await firma.Gov.submitCommunityPoolSpendProposal(aliceWallet, title, description, initialDepositFCT, amount, recipient); console.log(result); expect(result.code).to.equal(0); @@ -52,7 +50,6 @@ describe.skip('[16. Gov Tx Test]', () => { const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); - const proposer = await aliceWallet.getAddress(); const initialDepositFCT = 10; const title = "Parameter Change proposal1"; const description = "This is a Parameter change proposal"; @@ -63,7 +60,7 @@ describe.skip('[16. Gov Tx Test]', () => { value: "100", }]; - var result = await firma.Gov.submitParameterChangeProposal(aliceWallet, title, description, initialDepositFCT, proposer, changeParamList); + var result = await firma.Gov.submitParameterChangeProposal(aliceWallet, title, description, initialDepositFCT, changeParamList); console.log(result); expect(result.code).to.equal(0); @@ -73,7 +70,6 @@ describe.skip('[16. Gov Tx Test]', () => { const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); - const proposer = await aliceWallet.getAddress(); const initialDepositFCT = 10000; const title = "Software Upgrade proposal1"; const description = "This is a software upgrade proposal"; @@ -81,7 +77,7 @@ describe.skip('[16. Gov Tx Test]', () => { const upgradeName = "v0.2.7"; const upgradeHeight = Long.fromInt(20000000); - var result = await firma.Gov.submitSoftwareUpgradeProposalByHeight(aliceWallet, title, description, initialDepositFCT, proposer, upgradeName, upgradeHeight); + var result = await firma.Gov.submitSoftwareUpgradeProposalByHeight(aliceWallet, title, description, initialDepositFCT, upgradeName, upgradeHeight); console.log(result); expect(result.code).to.equal(0); @@ -91,12 +87,11 @@ describe.skip('[16. Gov Tx Test]', () => { const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); - const proposer = await aliceWallet.getAddress(); const initialDepositFCT = 1000; const title = "Software Upgrade proposal1"; const description = "This is a software upgrade proposal"; - var result = await firma.Gov.submitCancelSoftwareUpgradeProposal(aliceWallet, title, description, initialDepositFCT, proposer); + var result = await firma.Gov.submitCancelSoftwareUpgradeProposal(aliceWallet, title, description, initialDepositFCT); console.log(result); expect(result.code).to.equal(0); @@ -108,7 +103,6 @@ describe.skip('[16. Gov Tx Test]', () => { const aliceWallet = await firma.Wallet.fromMnemonic(aliceMnemonic); const bobWallet = await firma.Wallet.fromMnemonic(bobMnemonic); - const proposer = await aliceWallet.getAddress(); const initialDepositFCT = 8; const title = "Software Upgrade proposal2"; const description = "This is a software upgrade proposal"; @@ -120,7 +114,7 @@ describe.skip('[16. Gov Tx Test]', () => { const upgradeTime = expirationDate; const upgradeInfo = "info?"; - var result = await firma.Gov.SubmitSoftwareUpgradeProposalByTime(aliceWallet, title, description, initialDepositFCT, proposer, upgradeName, upgradeTime); + var result = await firma.Gov.SubmitSoftwareUpgradeProposalByTime(aliceWallet, title, description, initialDepositFCT, upgradeName, upgradeTime); console.log(result); expect(result.code).to.equal(0); @@ -134,7 +128,7 @@ describe.skip('[16. Gov Tx Test]', () => { const wallet = await firma.Wallet.fromMnemonic(aliceMnemonic); - const proposalId = Long.fromInt(tempProposalId); + const proposalId = tempProposalId; const amount = 1000; var result = await firma.Gov.deposit(wallet, proposalId, amount); //console.log(result); @@ -144,7 +138,7 @@ describe.skip('[16. Gov Tx Test]', () => { it('Vote - alice YES', async () => { const wallet = await firma.Wallet.fromMnemonic(aliceMnemonic); - const proposalId = Long.fromInt(tempProposalId); + const proposalId = tempProposalId; var result = await firma.Gov.vote(wallet, proposalId, VotingOption.VOTE_OPTION_YES); //console.log(result); @@ -154,7 +148,7 @@ describe.skip('[16. Gov Tx Test]', () => { it('Vote - bob NO', async () => { const wallet = await firma.Wallet.fromMnemonic(bobMnemonic); - const proposalId = Long.fromInt(tempProposalId); + const proposalId = tempProposalId; var result = await firma.Gov.vote(wallet, proposalId, VotingOption.VOTE_OPTION_NO); //console.log(result);