From a4b2fd2e231dab5b1bb931c356993ca65c62e178 Mon Sep 17 00:00:00 2001 From: "Alisa | Side.one" Date: Mon, 5 Jun 2023 11:19:52 +0800 Subject: [PATCH] feat: gov vote --- src/components/ProposalListItem.vue | 455 +++++++++++++--------------- src/stores/useGovStore.ts | 62 ++-- src/types/gov.ts | 122 ++++---- 3 files changed, 311 insertions(+), 328 deletions(-) diff --git a/src/components/ProposalListItem.vue b/src/components/ProposalListItem.vue index c2ac75fc..dcca7a95 100644 --- a/src/components/ProposalListItem.vue +++ b/src/components/ProposalListItem.vue @@ -1,9 +1,9 @@ diff --git a/src/stores/useGovStore.ts b/src/stores/useGovStore.ts index e415153e..48b07fce 100644 --- a/src/stores/useGovStore.ts +++ b/src/stores/useGovStore.ts @@ -2,7 +2,7 @@ import { defineStore } from 'pinia'; import { useBlockchain } from './useBlockchain'; import type { PageRequest, PaginatedProposals } from '@/types'; import { LoadingStatus } from './useDashboard'; -import { useWalletStore } from './useWalletStore' +import { useWalletStore } from './useWalletStore'; import { reactive } from 'vue'; export const useGovStore = defineStore('govStore', { @@ -23,41 +23,49 @@ export const useGovStore = defineStore('govStore', { }, walletstore() { return useWalletStore(); - } + }, }, actions: { initial() { - this.$reset() + this.$reset(); this.fetchParams(); - this.fetchProposals("2"); + this.fetchProposals('2'); }, async fetchProposals(status: string, pagination?: PageRequest) { //if (!this.loading[status]) { - this.loading[status] = LoadingStatus.Loading; - const proposals = reactive( - await this.blockchain.rpc?.getGovProposals(status, pagination) - ); - if (status === '2') { - proposals?.proposals?.forEach((item) => { - this.fetchTally(item.proposal_id).then((res) => { - item.final_tally_result = res?.tally; - }); - if (this.walletstore.currentAddress) { - try { - this.fetchProposalVotesVoter(item.proposal_id, this.walletstore.currentAddress).then((res) => { - item.voterStatus = res?.vote?.option || 'No With Veto' - }); - } catch (error) { - item.voterStatus = 'No With Veto' - } - } else { - item.voterStatus = 'No With Veto' - } + this.loading[status] = LoadingStatus.Loading; + const proposals = reactive( + await this.blockchain.rpc?.getGovProposals(status, pagination) + ); + if (status === '2') { + proposals?.proposals?.forEach((item) => { + this.fetchTally(item.proposal_id).then((res) => { + item.final_tally_result = res?.tally; }); - } + if (this.walletstore.currentAddress) { + try { + this.fetchProposalVotesVoter( + item.proposal_id, + this.walletstore.currentAddress + ) + .then((res) => { + item.voterStatus = res?.vote?.option || 'VOTE_OPTION_NO_WITH_VETO' + // 'No With Veto'; + }) + .catch((reject) => { + item.voterStatus = 'VOTE_OPTION_NO_WITH_VETO' + }); + } catch (error) { + item.voterStatus = 'VOTE_OPTION_NO_WITH_VETO' + } + } else { + item.voterStatus = 'VOTE_OPTION_NO_WITH_VETO' + } + }); + } - this.loading[status] = LoadingStatus.Loaded; - this.proposals[status] = proposals; + this.loading[status] = LoadingStatus.Loaded; + this.proposals[status] = proposals; //} return this.proposals[status]; }, diff --git a/src/types/gov.ts b/src/types/gov.ts index a0261e5f..c09a380f 100644 --- a/src/types/gov.ts +++ b/src/types/gov.ts @@ -1,79 +1,83 @@ - -import type { Coin, PaginatedResponse } from "./common" +import type { Coin, PaginatedResponse } from './common'; export interface GovParams { - "voting_params": { - "voting_period": string, - "proposal_voting_periods": any[], - "expedited_voting_period": string - }, - "deposit_params": { - "min_deposit": Coin[], - "max_deposit_period": string, - "min_expedited_deposit": Coin[], - "min_initial_deposit_ratio": string - }, - "tally_params": { - "quorum": string, - "threshold": string, - "veto_threshold": string, - "expedited_threshold": string - } + voting_params: { + voting_period: string; + proposal_voting_periods: any[]; + expedited_voting_period: string; + }; + deposit_params: { + min_deposit: Coin[]; + max_deposit_period: string; + min_expedited_deposit: Coin[]; + min_initial_deposit_ratio: string; + }; + tally_params: { + quorum: string; + threshold: string; + veto_threshold: string; + expedited_threshold: string; + }; } export interface GovProposal { - "proposal_id": string, - "content": { - "@type": string, - "title": string, - "description": string, - "plan"?: { - 'height'?: string | number, - 'time'?: string | number, - } - }, - "status": string, - "final_tally_result": { - "yes": string, - "abstain": string, - "no": string, - "no_with_veto": string, - }, - "submit_time": string, - "deposit_end_time": string, - "total_deposit": Coin[], - "voting_start_time": string, - "voting_end_time": string, - "is_expedited": boolean, - "voterStatus"?: string, + proposal_id: string; + content: { + '@type': string; + title: string; + description: string; + plan?: { + height?: string | number; + time?: string | number; + }; + }; + status: string; + final_tally_result: { + yes: string; + abstain: string; + no: string; + no_with_veto: string; + }; + submit_time: string; + deposit_end_time: string; + total_deposit: Coin[]; + voting_start_time: string; + voting_end_time: string; + is_expedited: boolean; + voterStatus?: string +// VoteOption[]; } +export interface VoteOption { + option: string; + weight: string; +} export interface Tally { - yes: string, - abstain: string, - no: string, - no_with_veto: string + yes: string; + abstain: string; + no: string; + no_with_veto: string; } export interface GovVote { - proposal_id: string, - voter: string, - option: string, - options: { "option": string, "weight": string }[] + proposal_id: string; + voter: string; + option: string; + options: { option: string; weight: string }[]; } export interface PaginatedProposals extends PaginatedResponse { - proposals: GovProposal[] + proposals: GovProposal[]; } export interface PaginatedProposalDeposit extends PaginatedResponse { - deposits: { - amount: Coin[], - proposal_id: string, - depositor: string - } + deposits: { + amount: Coin[]; + proposal_id: string; + depositor: string; + }; } export interface PaginatedProposalVotes extends PaginatedResponse { - votes: GovVote[] -} \ No newline at end of file + votes: GovVote[]; +}