feat: gov vote

This commit is contained in:
Alisa | Side.one 2023-06-05 11:19:52 +08:00
parent 57657fe627
commit a4b2fd2e23
3 changed files with 311 additions and 328 deletions

View File

@ -1,9 +1,9 @@
<script lang="ts" setup> <script lang="ts" setup>
import { import {
useBlockchain, useBlockchain,
useFormatter, useFormatter,
useStakingStore, useStakingStore,
useTxDialog, useTxDialog,
} from '@/stores'; } from '@/stores';
import { select } from '@/components/dynamic/index'; import { select } from '@/components/dynamic/index';
import type { PaginatedProposals } from '@/types'; import type { PaginatedProposals } from '@/types';
@ -12,263 +12,234 @@ import type { PropType } from 'vue';
import { ref } from 'vue'; import { ref } from 'vue';
const dialog = useTxDialog(); const dialog = useTxDialog();
defineProps({ defineProps({
proposals: { type: Object as PropType<PaginatedProposals> }, proposals: { type: Object as PropType<PaginatedProposals> },
}); });
const format = useFormatter(); const format = useFormatter();
const staking = useStakingStore(); const staking = useStakingStore();
const chain = useBlockchain(); const chain = useBlockchain();
function showType(v: string) { function showType(v: string) {
if (v) { if (v) {
return v.substring(v.lastIndexOf('.') + 1); return v.substring(v.lastIndexOf('.') + 1);
} }
return v; return v;
} }
const statusMap: Record<string, string> = { const statusMap: Record<string, string> = {
PROPOSAL_STATUS_VOTING_PERIOD: 'VOTING', PROPOSAL_STATUS_VOTING_PERIOD: 'VOTING',
PROPOSAL_STATUS_PASSED: 'PASSED', PROPOSAL_STATUS_PASSED: 'PASSED',
PROPOSAL_STATUS_REJECTED: 'REJECTED', PROPOSAL_STATUS_REJECTED: 'REJECTED',
}; };
const voterStatusMap: Record<string, string> = { const voterStatusMap: Record<string, string> = {
No_With_Veto: '', VOTE_OPTION_NO_WITH_VETO: '',
VOTE_OPTION_YES: 'success', VOTE_OPTION_YES: 'success',
VOTE_OPTION_NO: 'error', VOTE_OPTION_NO: 'error',
VOTE_OPTION_ABSTAIN: 'warning', VOTE_OPTION_ABSTAIN: 'warning',
}; };
const proposalInfo = ref(); const proposalInfo = ref();
</script> </script>
<template> <template>
<div class="bg-white dark:bg-[#28334e] rounded text-sm"> <div class="bg-white dark:bg-[#28334e] rounded text-sm">
<table class="table-compact w-full table-fixed hidden lg:!table"> <table class="table-compact w-full table-fixed hidden lg:!table">
<tbody> <tbody>
<tr v-for="(item, index) in proposals?.proposals" :key="index"> <tr v-for="(item, index) in proposals?.proposals" :key="index">
<td class="px-4 w-20"> <td class="px-4 w-20">
<label <label
for="proposal-detail-modal" for="proposal-detail-modal"
class="text-main text-base hover:text-indigo-400 cursor-pointer" class="text-main text-base hover:text-indigo-400 cursor-pointer"
@click="proposalInfo = item" @click="proposalInfo = item"
>
#{{ item?.proposal_id }}</label
>
</td>
<td class="w-full">
<div>
<RouterLink
:to="`/${chain.chainName}/gov/${item?.proposal_id}`"
class="text-main text-base mb-1 block hover:text-indigo-400 truncate"
>
{{ item?.content?.title }}
</RouterLink>
<div
class="bg-[#f6f2ff] text-[#9c6cff] dark:bg-gray-600 dark:text-gray-300 inline-block rounded-full px-2 py-[1px] text-xs mb-1"
>
{{ showType(item.content['@type']) }}
</div>
</div>
</td>
<td class="w-60">
<ProposalProcess
:pool="staking.pool"
:tally="item.final_tally_result"
></ProposalProcess>
</td>
<td class="w-36">
<div class="pl-4">
<div
class="flex items-center"
:class="
statusMap?.[item?.status] === 'PASSED'
? 'text-yes'
: statusMap?.[item?.status] ===
'REJECTED'
? 'text-no'
: 'text-info'
"
>
<div
class="w-1 h-1 rounded-full mr-2"
:class="
statusMap?.[item?.status] === 'PASSED'
? 'bg-yes'
: statusMap?.[item?.status] ===
'REJECTED'
? 'bg-no'
: 'bg-info'
"
></div>
<div class="text-xs">
{{
statusMap?.[item?.status] ||
item?.status
}}
</div>
</div>
<div
class="truncate col-span-2 md:!col-span-1 text-xs text-gray-500 dark:text-gray-400 text-right md:!flex md:!justify-start"
>
{{ format.toDay(item.voting_end_time, 'from') }}
</div>
</div>
</td>
<td
v-if="statusMap?.[item?.status] === 'VOTING'"
class="w-40"
>
<div class="">
<label
for="vote"
class="btn btn-xs btn-primary rounded-sm"
@click="
dialog.open('vote', {
proposal_id: item?.proposal_id,
})
"
>
<span v-if="item?.voterStatus">{{
item?.voterStatus.replace(
'VOTE_OPTION_',
''
)
}}</span>
<span v-else>Vote</span>
</label>
</div>
</td>
</tr>
</tbody>
</table>
<div class="lg:!hidden">
<div
v-for="(item, index) in proposals?.proposals"
:key="index"
class="px-4 py-4"
> >
<div #{{ item?.proposal_id }}</label
class="text-main text-base mb-1 flex justify-between hover:text-indigo-400" >
> </td>
<RouterLink <td class="w-full">
:to="`/${chain.chainName}/gov/${item?.proposal_id}`" <div>
class="flex-1 w-0 truncate mr-4" <RouterLink
>{{ item?.content?.title }}</RouterLink :to="`/${chain.chainName}/gov/${item?.proposal_id}`"
> class="text-main text-base mb-1 block hover:text-indigo-400 truncate"
<label >
for="proposal-detail-modal" {{ item?.content?.title }}
class="text-main text-base hover:text-indigo-400 cursor-pointer" </RouterLink>
@click="proposalInfo = item" <div
> class="bg-[#f6f2ff] text-[#9c6cff] dark:bg-gray-600 dark:text-gray-300 inline-block rounded-full px-2 py-[1px] text-xs mb-1"
#{{ item?.proposal_id }}</label >
> {{ showType(item.content['@type']) }}
</div> </div>
<div class="grid grid-cols-4 mt-2 mb-2">
<div class="col-span-2">
<div
class="bg-[#f6f2ff] text-[#9c6cff] dark:bg-gray-600 dark:text-gray-300 inline-block rounded-full px-2 py-[1px] text-xs mb-1"
>
{{ showType(item.content['@type']) }}
</div>
</div>
<div
class="flex items-center"
:class="
statusMap?.[item?.status] === 'PASSED'
? 'text-yes'
: statusMap?.[item?.status] === 'REJECTED'
? 'text-no'
: 'text-info'
"
>
<div
class="w-1 h-1 rounded-full mr-2"
:class="
statusMap?.[item?.status] === 'PASSED'
? 'bg-yes'
: statusMap?.[item?.status] === 'REJECTED'
? 'bg-no'
: 'bg-info'
"
></div>
<div class="text-xs flex items-center">
{{ statusMap?.[item?.status] || item?.status }}
</div>
</div>
<div
class="truncate text-xs text-gray-500 dark:text-gray-400 flex items-center justify-end"
>
{{ format.toDay(item.voting_end_time, 'from') }}
</div>
</div>
<div>
<ProposalProcess
:pool="staking.pool"
:tally="item.final_tally_result"
></ProposalProcess>
</div>
<div class="mt-4" v-if="statusMap?.[item?.status] === 'VOTING'">
<div class="" v-show="item?.voterStatus === 'No With Veto'">
<label
for="vote"
class="btn btn-xs btn-primary rounded-sm"
@click="
dialog.open('vote', {
proposal_id: item?.proposal_id,
})
"
>Vote</label
>
<div
class="text-xs truncate relative py-1 px-3 rounded-full w-fit"
:class="`text-${
voterStatusMap?.[item?.voterStatus]
}`"
v-show="item?.voterStatus !== 'No With Veto'"
>
<span
class="inset-x-0 inset-y-0 opacity-10 absolute"
:class="`bg-${
voterStatusMap?.[item?.voterStatus]
}`"
></span>
{{ item?.voterStatus }}
</div>
</div>
</div>
</div> </div>
</td>
<td class="w-60">
<ProposalProcess
:pool="staking.pool"
:tally="item.final_tally_result"
></ProposalProcess>
</td>
<td class="w-36">
<div class="pl-4">
<div
class="flex items-center"
:class="
statusMap?.[item?.status] === 'PASSED'
? 'text-yes'
: statusMap?.[item?.status] === 'REJECTED'
? 'text-no'
: 'text-info'
"
>
<div
class="w-1 h-1 rounded-full mr-2"
:class="
statusMap?.[item?.status] === 'PASSED'
? 'bg-yes'
: statusMap?.[item?.status] === 'REJECTED'
? 'bg-no'
: 'bg-info'
"
></div>
<div class="text-xs">
{{ statusMap?.[item?.status] || item?.status }}
</div>
</div>
<div
class="truncate col-span-2 md:!col-span-1 text-xs text-gray-500 dark:text-gray-400 text-right md:!flex md:!justify-start"
>
{{ format.toDay(item.voting_end_time, 'from') }}
</div>
</div>
</td>
<td v-if="statusMap?.[item?.status] === 'VOTING'" class="w-40">
<div class="">
<label
for="vote"
class="btn btn-xs btn-primary rounded-sm"
@click="
dialog.open('vote', {
proposal_id: item?.proposal_id,
})
"
>
<span v-if="item?.voterStatus !== 'VOTE_OPTION_NO_WITH_VETO'">{{
item?.voterStatus?.replace('VOTE_OPTION_', '')
}}</span>
<span v-else>Vote</span>
</label>
</div>
</td>
</tr>
</tbody>
</table>
<div class="lg:!hidden">
<div
v-for="(item, index) in proposals?.proposals"
:key="index"
class="px-4 py-4"
>
<div
class="text-main text-base mb-1 flex justify-between hover:text-indigo-400"
>
<RouterLink
:to="`/${chain.chainName}/gov/${item?.proposal_id}`"
class="flex-1 w-0 truncate mr-4"
>{{ item?.content?.title }}</RouterLink
>
<label
for="proposal-detail-modal"
class="text-main text-base hover:text-indigo-400 cursor-pointer"
@click="proposalInfo = item"
>
#{{ item?.proposal_id }}</label
>
</div> </div>
<input <div class="grid grid-cols-4 mt-2 mb-2">
type="checkbox" <div class="col-span-2">
id="proposal-detail-modal" <div
class="modal-toggle" class="bg-[#f6f2ff] text-[#9c6cff] dark:bg-gray-600 dark:text-gray-300 inline-block rounded-full px-2 py-[1px] text-xs mb-1"
/> >
<label for="proposal-detail-modal" class="modal"> {{ showType(item.content['@type']) }}
<label class="modal-box w-11/12 max-w-5xl" for=""> </div>
<label </div>
for="proposal-detail-modal" <div
class="btn btn-sm btn-circle absolute right-2 top-2" class="flex items-center"
></label :class="
> statusMap?.[item?.status] === 'PASSED'
<h3 class="font-bold text-lg">Description</h3> ? 'text-yes'
<p class="py-4"> : statusMap?.[item?.status] === 'REJECTED'
<Component ? 'text-no'
v-if="proposalInfo?.content?.description" : 'text-info'
:is=" "
select( >
proposalInfo?.content?.description, <div
'horizontal' class="w-1 h-1 rounded-full mr-2"
) :class="
" statusMap?.[item?.status] === 'PASSED'
:value="proposalInfo?.content?.description" ? 'bg-yes'
> : statusMap?.[item?.status] === 'REJECTED'
</Component> ? 'bg-no'
</p> : 'bg-info'
</label> "
</label> ></div>
<div class="text-xs flex items-center">
{{ statusMap?.[item?.status] || item?.status }}
</div>
</div>
<div
class="truncate text-xs text-gray-500 dark:text-gray-400 flex items-center justify-end"
>
{{ format.toDay(item.voting_end_time, 'from') }}
</div>
</div>
<div>
<ProposalProcess
:pool="staking.pool"
:tally="item.final_tally_result"
></ProposalProcess>
</div>
<div class="mt-4" v-if="statusMap?.[item?.status] === 'VOTING'">
<div class="">
<label
for="vote"
class="btn btn-xs btn-primary rounded-sm"
@click="
dialog.open('vote', {
proposal_id: item?.proposal_id,
})
"
>
<span v-if="item?.voterStatus !== 'VOTE_OPTION_NO_WITH_VETO'">{{
item?.voterStatus?.replace('VOTE_OPTION_', '')
}}</span>
<span v-else>Vote</span></label
>
</div>
</div>
</div>
</div> </div>
<input type="checkbox" id="proposal-detail-modal" class="modal-toggle" />
<label for="proposal-detail-modal" class="modal">
<label class="modal-box w-11/12 max-w-5xl" for="">
<label
for="proposal-detail-modal"
class="btn btn-sm btn-circle absolute right-2 top-2"
></label
>
<h3 class="font-bold text-lg">Description</h3>
<p class="py-4">
<Component
v-if="proposalInfo?.content?.description"
:is="select(proposalInfo?.content?.description, 'horizontal')"
:value="proposalInfo?.content?.description"
>
</Component>
</p>
</label>
</label>
</div>
</template> </template>

View File

@ -2,7 +2,7 @@ import { defineStore } from 'pinia';
import { useBlockchain } from './useBlockchain'; import { useBlockchain } from './useBlockchain';
import type { PageRequest, PaginatedProposals } from '@/types'; import type { PageRequest, PaginatedProposals } from '@/types';
import { LoadingStatus } from './useDashboard'; import { LoadingStatus } from './useDashboard';
import { useWalletStore } from './useWalletStore' import { useWalletStore } from './useWalletStore';
import { reactive } from 'vue'; import { reactive } from 'vue';
export const useGovStore = defineStore('govStore', { export const useGovStore = defineStore('govStore', {
@ -23,41 +23,49 @@ export const useGovStore = defineStore('govStore', {
}, },
walletstore() { walletstore() {
return useWalletStore(); return useWalletStore();
} },
}, },
actions: { actions: {
initial() { initial() {
this.$reset() this.$reset();
this.fetchParams(); this.fetchParams();
this.fetchProposals("2"); this.fetchProposals('2');
}, },
async fetchProposals(status: string, pagination?: PageRequest) { async fetchProposals(status: string, pagination?: PageRequest) {
//if (!this.loading[status]) { //if (!this.loading[status]) {
this.loading[status] = LoadingStatus.Loading; this.loading[status] = LoadingStatus.Loading;
const proposals = reactive( const proposals = reactive(
await this.blockchain.rpc?.getGovProposals(status, pagination) await this.blockchain.rpc?.getGovProposals(status, pagination)
); );
if (status === '2') { if (status === '2') {
proposals?.proposals?.forEach((item) => { proposals?.proposals?.forEach((item) => {
this.fetchTally(item.proposal_id).then((res) => { this.fetchTally(item.proposal_id).then((res) => {
item.final_tally_result = res?.tally; 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'
}
}); });
} 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.loading[status] = LoadingStatus.Loaded;
this.proposals[status] = proposals; this.proposals[status] = proposals;
//} //}
return this.proposals[status]; return this.proposals[status];
}, },

View File

@ -1,79 +1,83 @@
import type { Coin, PaginatedResponse } from './common';
import type { Coin, PaginatedResponse } from "./common"
export interface GovParams { export interface GovParams {
"voting_params": { voting_params: {
"voting_period": string, voting_period: string;
"proposal_voting_periods": any[], proposal_voting_periods: any[];
"expedited_voting_period": string expedited_voting_period: string;
}, };
"deposit_params": { deposit_params: {
"min_deposit": Coin[], min_deposit: Coin[];
"max_deposit_period": string, max_deposit_period: string;
"min_expedited_deposit": Coin[], min_expedited_deposit: Coin[];
"min_initial_deposit_ratio": string min_initial_deposit_ratio: string;
}, };
"tally_params": { tally_params: {
"quorum": string, quorum: string;
"threshold": string, threshold: string;
"veto_threshold": string, veto_threshold: string;
"expedited_threshold": string expedited_threshold: string;
} };
} }
export interface GovProposal { export interface GovProposal {
"proposal_id": string, proposal_id: string;
"content": { content: {
"@type": string, '@type': string;
"title": string, title: string;
"description": string, description: string;
"plan"?: { plan?: {
'height'?: string | number, height?: string | number;
'time'?: string | number, time?: string | number;
} };
}, };
"status": string, status: string;
"final_tally_result": { final_tally_result: {
"yes": string, yes: string;
"abstain": string, abstain: string;
"no": string, no: string;
"no_with_veto": string, no_with_veto: string;
}, };
"submit_time": string, submit_time: string;
"deposit_end_time": string, deposit_end_time: string;
"total_deposit": Coin[], total_deposit: Coin[];
"voting_start_time": string, voting_start_time: string;
"voting_end_time": string, voting_end_time: string;
"is_expedited": boolean, is_expedited: boolean;
"voterStatus"?: string, voterStatus?: string
// VoteOption[];
} }
export interface VoteOption {
option: string;
weight: string;
}
export interface Tally { export interface Tally {
yes: string, yes: string;
abstain: string, abstain: string;
no: string, no: string;
no_with_veto: string no_with_veto: string;
} }
export interface GovVote { export interface GovVote {
proposal_id: string, proposal_id: string;
voter: string, voter: string;
option: string, option: string;
options: { "option": string, "weight": string }[] options: { option: string; weight: string }[];
} }
export interface PaginatedProposals extends PaginatedResponse { export interface PaginatedProposals extends PaginatedResponse {
proposals: GovProposal[] proposals: GovProposal[];
} }
export interface PaginatedProposalDeposit extends PaginatedResponse { export interface PaginatedProposalDeposit extends PaginatedResponse {
deposits: { deposits: {
amount: Coin[], amount: Coin[];
proposal_id: string, proposal_id: string;
depositor: string depositor: string;
} };
} }
export interface PaginatedProposalVotes extends PaginatedResponse { export interface PaginatedProposalVotes extends PaginatedResponse {
votes: GovVote[] votes: GovVote[];
} }