From 361998996eb14084e03ed462a43ff8edaa614b24 Mon Sep 17 00:00:00 2001 From: fly33499 Date: Mon, 18 Oct 2021 21:31:02 +0900 Subject: [PATCH] feat: update gov query module --- sdk/FirmaGovService.ts | 99 +++++++++++++++++++++++++++- sdk/firmachain/gov/GovQueryClient.ts | 85 ++++++++++++++++++++++-- 2 files changed, 176 insertions(+), 8 deletions(-) diff --git a/sdk/FirmaGovService.ts b/sdk/FirmaGovService.ts index bfc7044..ba9e700 100644 --- a/sdk/FirmaGovService.ts +++ b/sdk/FirmaGovService.ts @@ -1,4 +1,4 @@ -import { GovTxClient, GovQueryClient, TxMisc, DefaultTxMisc, getSignAndBroadcastOption, ParamChangeOption, SoftwareUpgradePlan, VotingOption } from './firmachain/gov'; +import { GovTxClient, GovQueryClient, TxMisc, DefaultTxMisc, getSignAndBroadcastOption, ParamChangeOption, SoftwareUpgradePlan, VotingOption, ProposalInfo, ProposalStatus, ProposalParam } from './firmachain/gov'; import { TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx'; import { FirmaWalletService } from "./FirmaWalletService"; @@ -9,7 +9,7 @@ import { Any } from './firmachain/google/protobuf/any'; import { TextProposal } from 'cosmjs-types/cosmos/gov/v1beta1/gov'; import { CommunityPoolSpendProposal } from 'cosmjs-types/cosmos/distribution/v1beta1/distribution'; import { ParameterChangeProposal } from 'cosmjs-types/cosmos/params/v1beta1/params'; -import { SoftwareUpgradeProposal } from 'cosmjs-types/cosmos/upgrade/v1beta1/upgrade'; +import { CancelSoftwareUpgradeProposal, SoftwareUpgradeProposal } from 'cosmjs-types/cosmos/upgrade/v1beta1/upgrade'; import Long from 'long'; export class FirmaGovService { @@ -54,6 +54,32 @@ export class FirmaGovService { } } + private async getSignedTxCancelSoftwareUpgradeProposal(wallet: FirmaWalletService, title: string, description: string, initialDepositFCT: number, proposer: string, txMisc: TxMisc = DefaultTxMisc): Promise { + + try { + let txClient = new GovTxClient(wallet.getRawWallet(), this._config.rpcAddress); + + const initialDepositAmount = { denom: this._config.denom, amount: FirmaUtil.getUFCTStringFromFCT(initialDepositFCT) }; + + const proposal = CancelSoftwareUpgradeProposal.fromPartial({ + title: title, + description: description, + }); + + let content = Any.fromPartial({ + typeUrl: "/cosmos.upgrade.v1beta1.CancelSoftwareUpgradeProposal", + value: Uint8Array.from(SoftwareUpgradeProposal.encode(proposal).finish()), + }); + + let message = txClient.msgSubmitProposal({ content: content, initialDeposit: [initialDepositAmount], proposer: proposer }); + return await txClient.sign([message], getSignAndBroadcastOption(this._config.denom, txMisc)); + + } catch (error) { + FirmaUtil.printLog(error); + throw error; + } + } + private async getSignedTxSoftwareUpgradeProposal(wallet: FirmaWalletService, title: string, description: string, initialDepositFCT: number, proposer: string, plan: SoftwareUpgradePlan, txMisc: TxMisc = DefaultTxMisc): Promise { try { @@ -137,6 +163,20 @@ export class FirmaGovService { } } + public async submitCancelSoftwareUpgradeProposal(wallet: FirmaWalletService, title: string, description: string, initialDeposit: number, proposer: string, txMisc: TxMisc = DefaultTxMisc): Promise { + try { + + let txRaw = await this.getSignedTxCancelSoftwareUpgradeProposal(wallet, title, description, initialDeposit, proposer, txMisc); + + let txClient = new GovTxClient(wallet.getRawWallet(), this._config.rpcAddress); + return await txClient.broadcast(txRaw); + + } catch (error) { + FirmaUtil.printLog(error); + throw error; + } + } + public async submitSoftwareUpgradeProposalByHeight(wallet: FirmaWalletService, title: string, description: string, initialDeposit: number, proposer: string, upgradeName: string, height: Long, txMisc: TxMisc = DefaultTxMisc): Promise { try { let plan = { @@ -282,5 +322,60 @@ export class FirmaGovService { throw error; } } + + //query + + + public async getParam(): Promise { + try { + let queryClient = new GovQueryClient(this._config.restApiAddress); + let result = await queryClient.queryGetParam(); + + return result; + + } catch (error) { + FirmaUtil.printLog(error); + throw error; + } + } + + public async getProposal(id: string): Promise { + try { + let queryClient = new GovQueryClient(this._config.restApiAddress); + let result = await queryClient.queryGetProposal(id); + + return result; + + } catch (error) { + FirmaUtil.printLog(error); + throw error; + } + } + + public async getProposalListByStatus(status: ProposalStatus): Promise { + try { + let queryClient = new GovQueryClient(this._config.restApiAddress); + let result = await queryClient.queryGetProposalListByStatus(status); + + return result; + + } catch (error) { + FirmaUtil.printLog(error); + throw error; + } + } + + public async getProposalList(): Promise { + try { + let queryClient = new GovQueryClient(this._config.restApiAddress); + let result = await queryClient.queryGetProposalList(); + + return result; + + } catch (error) { + FirmaUtil.printLog(error); + throw error; + } + } } diff --git a/sdk/firmachain/gov/GovQueryClient.ts b/sdk/firmachain/gov/GovQueryClient.ts index 79fbfa7..e37a10b 100644 --- a/sdk/firmachain/gov/GovQueryClient.ts +++ b/sdk/firmachain/gov/GovQueryClient.ts @@ -1,5 +1,54 @@ import Axios, { AxiosInstance } from 'axios'; +export enum ProposalStatus { + PROPOSAL_STATUS_UNSPECIFIED = 0, + PROPOSAL_STATUS_DEPOSIT_PERIOD = 1, + PROPOSAL_STATUS_VOTING_PERIOD = 2, + PROPOSAL_STATUS_PASSED = 3, + PROPOSAL_STATUS_REJECTED = 4, + PROPOSAL_STATUS_FAILED = 5, +} + +export interface ProposalParam { + voting_period: string; + deposit_params: { + min_deposit: { + denom: string, + amount: string + }[], + max_deposit_period: string + }; + tally_params: { + quorum: string, + threshold: string, + veto_threshold: string, + }; +} + +export interface ProposalInfo { + proposal_id: string; + content: { + "@type": string, + title: string, + description: string + }; + status: string; + final_tally_result: { + yes: string, + abstain: string, + no: string, + no_with_veto: string + }; + submit_time: string; + deposit_end_time: string; + total_deposit: { + denom: string, + amount: string + }[]; + voting_start_time: string; + voting_end_time: string; +} + export class GovQueryClient { private _axios: AxiosInstance; @@ -13,18 +62,42 @@ export class GovQueryClient { }); } - public async querySupplyOf(denom: string) : Promise { - let path = "/cosmos/bank/v1beta1/supply/" + denom; + public async queryGetParam(): Promise { + + let path = "/cosmos/gov/v1beta1/params/voting"; + var votingResult = await this._axios.get(path); + + path = "/cosmos/gov/v1beta1/params/deposit"; + var depositResult = await this._axios.get(path); + + path = "/cosmos/gov/v1beta1/params/tallying"; + var tallyingResult = await this._axios.get(path); + + return { + voting_period: votingResult.data.voting_params.voting_period, + deposit_params: depositResult.data.deposit_params, + tally_params: tallyingResult.data.tally_params + }; + } + + public async queryGetProposal(id: string): Promise { + let path = "/cosmos/gov/v1beta1/proposals/" + id; var result = await this._axios.get(path); - return result.data.amount.amount; + return result.data.proposal; } - public async queryBalance(address: string, denom: string): Promise { + public async queryGetProposalListByStatus(status: ProposalStatus): Promise { + let path = "/cosmos/gov/v1beta1/proposals"; + + var result = await this._axios.get(path, { params: { proposalStatus: status } }); + return result.data.proposals; + } - let path = "/cosmos/bank/v1beta1/balances/" + address + "/" + denom; + public async queryGetProposalList(): Promise { + let path = "/cosmos/gov/v1beta1/proposals"; var result = await this._axios.get(path); - return result.data.balance.amount; + return result.data.proposals; } } \ No newline at end of file