Merge pull request #27 from tornadocash/TC-1_add_message_to_vote
TC-1 | TC-98 | Add message for Vote Casting
This commit is contained in:
commit
a83fae0772
9
app/router.scrollBehavior.js
Normal file
9
app/router.scrollBehavior.js
Normal file
@ -0,0 +1,9 @@
|
||||
const routerScrollBehavior = (to, from, savedPosition) => {
|
||||
if (to.name === 'governance-id') {
|
||||
return { x: 0, y: 0 }
|
||||
}
|
||||
|
||||
return savedPosition || { x: 0, y: 0 }
|
||||
}
|
||||
|
||||
export default routerScrollBehavior
|
@ -73,13 +73,17 @@
|
||||
padding: 1.429rem;
|
||||
background: #1f1f1f;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
|
||||
.title {
|
||||
&.is-link {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
&--title {
|
||||
color: #fff;
|
||||
font-size: 1.143rem;
|
||||
margin-bottom: 1rem;
|
||||
line-height: 1.286;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
&--info {
|
||||
@ -105,6 +109,7 @@
|
||||
|
||||
margin-right: 0.714rem;
|
||||
|
||||
&.tag,
|
||||
.tag {
|
||||
color: #fff;
|
||||
background: #363636;
|
||||
|
139
components/ProposalCommentFormModal.vue
Normal file
139
components/ProposalCommentFormModal.vue
Normal file
@ -0,0 +1,139 @@
|
||||
<template>
|
||||
<div class="modal-card box box-modal">
|
||||
<header class="box-modal-header is-spaced">
|
||||
<div class="box-modal-title">{{ $t('proposalComment.modal-title', { id: proposal.id }) }}</div>
|
||||
<button type="button" class="delete" @click="$emit('close')" />
|
||||
</header>
|
||||
|
||||
<p class="detail" v-text="$t('proposalComment.modal-subtitle')" />
|
||||
|
||||
<div class="columns is-multiline">
|
||||
<div class="column is-12">
|
||||
<b-field>
|
||||
<template #label>
|
||||
{{ $t('proposalComment.form-contact') }}
|
||||
<b-tooltip
|
||||
:label="$t('proposalComment.form-contact-tooltip')"
|
||||
size="is-medium"
|
||||
position="is-top"
|
||||
multilined
|
||||
>
|
||||
<button class="button is-primary has-icon">
|
||||
<span class="icon icon-info"></span>
|
||||
</button>
|
||||
</b-tooltip>
|
||||
</template>
|
||||
|
||||
<b-input
|
||||
v-model.trim="form.contact"
|
||||
:maxlength="limit / 2"
|
||||
:has-counter="false"
|
||||
:placeholder="$t('proposalComment.form-contact-placeholder')"
|
||||
/>
|
||||
</b-field>
|
||||
</div>
|
||||
<div class="column is-12">
|
||||
<b-field
|
||||
:message="fields.message ? '' : $t('proposalComment.form-message-required')"
|
||||
:type="{ 'is-warning': !fields.message && !support }"
|
||||
:label="$t('proposalComment.form-message')"
|
||||
>
|
||||
<b-input
|
||||
v-model="form.message"
|
||||
:maxlength="limit"
|
||||
type="textarea"
|
||||
:placeholder="
|
||||
support
|
||||
? $t('proposalComment.form-message-opt-placeholder')
|
||||
: $t('proposalComment.form-message-placeholder')
|
||||
"
|
||||
/>
|
||||
</b-field>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<b-button
|
||||
v-if="support"
|
||||
:disabled="!isValid"
|
||||
type="is-primary"
|
||||
icon-left="check"
|
||||
outlined
|
||||
@click="onCastVote(true)"
|
||||
>
|
||||
{{ $t('for') }}
|
||||
</b-button>
|
||||
|
||||
<b-button
|
||||
v-else
|
||||
:disabled="!isValid"
|
||||
type="is-danger"
|
||||
icon-left="close"
|
||||
outlined
|
||||
@click="onCastVote(false)"
|
||||
>
|
||||
{{ $t('against') }}
|
||||
</b-button>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
const MESSAGE_LIMIT = 100
|
||||
|
||||
export default {
|
||||
props: {
|
||||
support: {
|
||||
type: Boolean,
|
||||
required: true
|
||||
},
|
||||
proposal: {
|
||||
type: Object,
|
||||
required: true,
|
||||
validator: (prop) => 'id' in prop
|
||||
}
|
||||
},
|
||||
data: () => ({
|
||||
limit: MESSAGE_LIMIT,
|
||||
fields: {
|
||||
contact: true,
|
||||
message: true
|
||||
},
|
||||
form: {
|
||||
contact: '',
|
||||
message: ''
|
||||
}
|
||||
}),
|
||||
computed: {
|
||||
isValid() {
|
||||
return this.validate()
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
validate() {
|
||||
const { form, fields, support } = this
|
||||
fields.contact = form.contact.length <= this.limit
|
||||
|
||||
fields.message = support
|
||||
? form.message.length <= this.limit
|
||||
: form.message.length > 2 && form.message.length <= this.limit
|
||||
|
||||
return fields.contact && fields.message
|
||||
},
|
||||
onCastVote() {
|
||||
if (this.isValid) {
|
||||
this.$emit('castVote', this.form)
|
||||
this.$emit('close')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.box-modal {
|
||||
overflow: initial !important;
|
||||
}
|
||||
|
||||
.detail {
|
||||
margin-bottom: 1.25rem;
|
||||
}
|
||||
</style>
|
@ -4,150 +4,159 @@
|
||||
<div class="column is-7-tablet is-8-desktop">
|
||||
<h1 class="title">{{ data.title }}</h1>
|
||||
<div class="description">
|
||||
<p>{{ data.description }}</p>
|
||||
<p class="proposal--description">{{ data.description }}</p>
|
||||
</div>
|
||||
<ProposalComments
|
||||
v-if="isEnabledGovernance && quorumPercent"
|
||||
:proposal="data"
|
||||
:is-initialized="isInitialized"
|
||||
class="proposal--comments"
|
||||
/>
|
||||
</div>
|
||||
<div class="column is-5-tablet is-4-desktop">
|
||||
<div v-if="data.status === 'active'" class="proposal-block">
|
||||
<div class="title">{{ $t('castYourVote') }}</div>
|
||||
<b-tooltip
|
||||
class="fit-content"
|
||||
:label="tooltipMessage"
|
||||
position="is-top"
|
||||
:active="readyForAction"
|
||||
multilined
|
||||
>
|
||||
<div class="buttons buttons__halfwidth">
|
||||
<b-button
|
||||
:disabled="readyForAction"
|
||||
type="is-primary"
|
||||
:icon-left="isFetchingBalances ? '' : 'check'"
|
||||
outlined
|
||||
:loading="isFetchingBalances"
|
||||
@click="onCastVote(true)"
|
||||
>{{ $t('for') }}</b-button
|
||||
>
|
||||
<b-button
|
||||
:disabled="readyForAction"
|
||||
type="is-danger"
|
||||
:icon-left="isFetchingBalances ? '' : 'close'"
|
||||
outlined
|
||||
:loading="isFetchingBalances"
|
||||
@click="onCastVote(false)"
|
||||
>{{ $t('against') }}</b-button
|
||||
>
|
||||
</div>
|
||||
</b-tooltip>
|
||||
<i18n
|
||||
v-if="voterReceipts[data.id] && voterReceipts[data.id].hasVoted"
|
||||
tag="div"
|
||||
path="yourCurrentVote"
|
||||
>
|
||||
<template v-slot:vote>
|
||||
<span
|
||||
:class="{
|
||||
'has-text-primary': voterReceipts[data.id].support,
|
||||
'has-text-danger': !voterReceipts[data.id].support
|
||||
}"
|
||||
>{{ $n(fromWeiToTorn(voterReceipts[data.id].balance)) }} TORN</span
|
||||
>
|
||||
</template>
|
||||
</i18n>
|
||||
</div>
|
||||
<div v-else-if="data.status === 'awaitingExecution'" class="proposal-block">
|
||||
<div class="title">{{ $t('executeProposal') }}</div>
|
||||
<b-tooltip
|
||||
class="fit-content"
|
||||
:label="$t('connectYourWalletFirst')"
|
||||
position="is-top"
|
||||
:active="!ethAccount"
|
||||
multilined
|
||||
>
|
||||
<b-button
|
||||
type="is-primary"
|
||||
icon-left="check"
|
||||
outlined
|
||||
:disabled="!ethAccount"
|
||||
expanded
|
||||
@click="onExecute"
|
||||
>{{ $t('execute') }}</b-button
|
||||
>
|
||||
</b-tooltip>
|
||||
</div>
|
||||
<div class="proposal-block">
|
||||
<div class="title">{{ $t('currentResults') }}</div>
|
||||
<div class="label">
|
||||
{{ $t('for') }}
|
||||
<span class="percent"
|
||||
><number-format :value="data.results.for" /> TORN / {{ calculatePercent('for') }}%</span
|
||||
>
|
||||
</div>
|
||||
<b-progress :value="calculatePercent('for')" type="is-primary"></b-progress>
|
||||
<div class="label">
|
||||
{{ $t('against') }}
|
||||
<span class="percent"
|
||||
><number-format :value="data.results.against" class="value" /> TORN /
|
||||
{{ calculatePercent('against') }}%</span
|
||||
>
|
||||
</div>
|
||||
<b-progress :value="calculatePercent('against')" type="is-danger"></b-progress>
|
||||
<div class="label">
|
||||
{{ $t('quorum') }}
|
||||
<div class="proposal--blocks">
|
||||
<div v-if="data.status === 'active'" class="proposal-block">
|
||||
<div class="title">{{ $t('castYourVote') }}</div>
|
||||
<b-tooltip
|
||||
:label="
|
||||
$t('quorumTooltip', {
|
||||
days: $tc('dayPlural', votingPeriod),
|
||||
votes: $n(quorumVotes, 'compact')
|
||||
})
|
||||
"
|
||||
size="is-medium"
|
||||
class="fit-content"
|
||||
:label="tooltipMessage"
|
||||
position="is-top"
|
||||
:active="readyForAction"
|
||||
multilined
|
||||
>
|
||||
<button class="button is-primary has-icon">
|
||||
<span class="icon icon-info"></span>
|
||||
</button>
|
||||
<div class="buttons buttons__halfwidth">
|
||||
<b-button
|
||||
:disabled="readyForAction"
|
||||
type="is-primary"
|
||||
:icon-left="isFetchingBalances || isCastingVote ? '' : 'check'"
|
||||
outlined
|
||||
:loading="isFetchingBalances || isCastingVote"
|
||||
@click="onCastVote(true)"
|
||||
>{{ $t('for') }}</b-button
|
||||
>
|
||||
<b-button
|
||||
:disabled="readyForAction"
|
||||
type="is-danger"
|
||||
:icon-left="isFetchingBalances || isCastingVote ? '' : 'close'"
|
||||
outlined
|
||||
:loading="isFetchingBalances || isCastingVote"
|
||||
@click="onCastVote(false)"
|
||||
>{{ $t('against') }}</b-button
|
||||
>
|
||||
</div>
|
||||
</b-tooltip>
|
||||
<span class="percent"
|
||||
><number-format :value="isQuorumCompleted ? quorumVotes : quorumResult" class="value" /> TORN /
|
||||
{{ quorumPercent }}%</span
|
||||
<i18n
|
||||
v-if="voterReceipts[data.id] && voterReceipts[data.id].hasVoted"
|
||||
tag="div"
|
||||
path="yourCurrentVote"
|
||||
>
|
||||
<template v-slot:vote>
|
||||
<span
|
||||
:class="{
|
||||
'has-text-primary': voterReceipts[data.id].support,
|
||||
'has-text-danger': !voterReceipts[data.id].support
|
||||
}"
|
||||
>{{ $n(fromWeiToTorn(voterReceipts[data.id].balance)) }} TORN</span
|
||||
>
|
||||
</template>
|
||||
</i18n>
|
||||
</div>
|
||||
<b-progress :value="quorumPercent" type="is-violet"></b-progress>
|
||||
</div>
|
||||
<div class="proposal-block">
|
||||
<div class="title">{{ $t('information') }}</div>
|
||||
<div class="columns is-multiline is-small" :class="{ 'has-countdown': countdown }">
|
||||
<div class="column is-full-small">
|
||||
<strong>{{ $t('proposalAddress') }}</strong>
|
||||
<div class="value">
|
||||
<a :href="contractUrl" class="address" target="_blank" rel="noopener noreferrer">
|
||||
{{ data.target }}
|
||||
</a>
|
||||
<div v-else-if="data.status === 'awaitingExecution'" class="proposal-block">
|
||||
<div class="title">{{ $t('executeProposal') }}</div>
|
||||
<b-tooltip
|
||||
class="fit-content"
|
||||
:label="$t('connectYourWalletFirst')"
|
||||
position="is-top"
|
||||
:active="!ethAccount"
|
||||
multilined
|
||||
>
|
||||
<b-button
|
||||
type="is-primary"
|
||||
icon-left="check"
|
||||
outlined
|
||||
:disabled="!ethAccount"
|
||||
expanded
|
||||
@click="onExecute"
|
||||
>{{ $t('execute') }}</b-button
|
||||
>
|
||||
</b-tooltip>
|
||||
</div>
|
||||
|
||||
<div class="proposal-block">
|
||||
<div class="title">{{ $t('currentResults') }}</div>
|
||||
<div class="label">
|
||||
{{ $t('for') }}
|
||||
<span class="percent"
|
||||
><number-format :value="data.results.for" /> TORN / {{ calculatePercent('for') }}%</span
|
||||
>
|
||||
</div>
|
||||
<b-progress :value="calculatePercent('for')" type="is-primary"></b-progress>
|
||||
<div class="label">
|
||||
{{ $t('against') }}
|
||||
<span class="percent"
|
||||
><number-format :value="data.results.against" class="value" /> TORN /
|
||||
{{ calculatePercent('against') }}%</span
|
||||
>
|
||||
</div>
|
||||
<b-progress :value="calculatePercent('against')" type="is-danger"></b-progress>
|
||||
<div class="label">
|
||||
{{ $t('quorum') }}
|
||||
<b-tooltip
|
||||
:label="
|
||||
$t('quorumTooltip', {
|
||||
days: $tc('dayPlural', votingPeriod),
|
||||
votes: $n(quorumVotes, 'compact')
|
||||
})
|
||||
"
|
||||
size="is-medium"
|
||||
position="is-top"
|
||||
multilined
|
||||
>
|
||||
<button class="button is-primary has-icon">
|
||||
<span class="icon icon-info"></span>
|
||||
</button>
|
||||
</b-tooltip>
|
||||
<span class="percent"
|
||||
><number-format :value="isQuorumCompleted ? quorumVotes : quorumResult" class="value" /> TORN
|
||||
/ {{ quorumPercent }}%</span
|
||||
>
|
||||
</div>
|
||||
<b-progress :value="quorumPercent" type="is-violet"></b-progress>
|
||||
</div>
|
||||
<div class="proposal-block">
|
||||
<div class="title">{{ $t('information') }}</div>
|
||||
<div class="columns is-multiline is-small" :class="{ 'has-countdown': countdown }">
|
||||
<div class="column is-full-small">
|
||||
<strong>{{ $t('proposalAddress') }}</strong>
|
||||
<div class="value">
|
||||
<a :href="contractUrl" class="address" target="_blank" rel="noopener noreferrer">
|
||||
{{ data.target }}
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="column is-half-small">
|
||||
<strong>{{ $t('id') }}</strong>
|
||||
<div class="value">{{ data.id }}</div>
|
||||
</div>
|
||||
<div class="column is-half-small">
|
||||
<strong>{{ $t('status') }}</strong>
|
||||
<div class="value">
|
||||
<b-tag :type="getStatusType(data.status)">{{ $t(data.status) }}</b-tag>
|
||||
<div class="column is-half-small">
|
||||
<strong>{{ $t('id') }}</strong>
|
||||
<div class="value">{{ data.id }}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="column is-half-small">
|
||||
<strong>{{ $t('startDate') }}</strong>
|
||||
<div class="value">{{ $moment.unix(data.startTime).format('llll') }}</div>
|
||||
</div>
|
||||
<div class="column is-half-small">
|
||||
<strong>{{ $t('endDate') }}</strong>
|
||||
<div class="value">{{ $moment.unix(data.endTime).format('llll') }}</div>
|
||||
</div>
|
||||
<div v-if="countdown" class="column is-full-small">
|
||||
<strong>{{ $t(timerLabel) }}</strong>
|
||||
<div class="value">
|
||||
{{ countdown }}
|
||||
<div class="column is-half-small">
|
||||
<strong>{{ $t('status') }}</strong>
|
||||
<div class="value">
|
||||
<b-tag :type="getStatusType(data.status)">{{ $t(data.status) }}</b-tag>
|
||||
</div>
|
||||
</div>
|
||||
<div class="column is-half-small">
|
||||
<strong>{{ $t('startDate') }}</strong>
|
||||
<div class="value">{{ $moment.unix(data.startTime).format('llll') }}</div>
|
||||
</div>
|
||||
<div class="column is-half-small">
|
||||
<strong>{{ $t('endDate') }}</strong>
|
||||
<div class="value">{{ $moment.unix(data.endTime).format('llll') }}</div>
|
||||
</div>
|
||||
<div v-if="countdown" class="column is-full-small">
|
||||
<strong>{{ $t(timerLabel) }}</strong>
|
||||
<div class="value">
|
||||
{{ countdown }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -160,11 +169,15 @@
|
||||
<script>
|
||||
import { mapState, mapActions, mapGetters } from 'vuex'
|
||||
import quorum from './mixins/quorum'
|
||||
import ProposalComments from './ProposalComments.vue'
|
||||
import NumberFormat from '@/components/NumberFormat'
|
||||
import ProposalCommentFormModal from '@/components/ProposalCommentFormModal.vue'
|
||||
|
||||
const { toBN, fromWei, toWei } = require('web3-utils')
|
||||
|
||||
export default {
|
||||
components: {
|
||||
ProposalComments,
|
||||
NumberFormat
|
||||
},
|
||||
mixins: [quorum],
|
||||
@ -182,7 +195,7 @@ export default {
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
...mapState('governance/gov', ['proposals', 'voterReceipts']),
|
||||
...mapState('governance/gov', ['proposals', 'voterReceipts', 'isCastingVote']),
|
||||
...mapState('metamask', ['ethAccount', 'isInitialized']),
|
||||
...mapGetters('txHashKeeper', ['addressExplorerUrl']),
|
||||
...mapGetters('metamask', ['networkConfig']),
|
||||
@ -299,7 +312,24 @@ export default {
|
||||
.toNumber()
|
||||
},
|
||||
onCastVote(support) {
|
||||
this.castVote({ id: this.data.id, support })
|
||||
const { id } = this.data
|
||||
|
||||
this.$buefy.modal.open({
|
||||
parent: this,
|
||||
component: ProposalCommentFormModal,
|
||||
hasModalCard: true,
|
||||
width: 440,
|
||||
customClass: 'is-pinned',
|
||||
props: {
|
||||
support,
|
||||
proposal: this.data
|
||||
},
|
||||
events: {
|
||||
castVote: ({ contact, message }) => {
|
||||
this.castVote({ id, support, contact, message })
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
onExecute() {
|
||||
this.executeProposal({ id: this.data.id })
|
||||
@ -353,3 +383,20 @@ export default {
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.proposal {
|
||||
&--description {
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
&--comments {
|
||||
margin-top: 2rem;
|
||||
}
|
||||
|
||||
&--blocks {
|
||||
position: sticky;
|
||||
top: 1rem;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
203
components/governance/ProposalComment.vue
Normal file
203
components/governance/ProposalComment.vue
Normal file
@ -0,0 +1,203 @@
|
||||
<template>
|
||||
<div class="proposals-box">
|
||||
<div class="columns is-gapless">
|
||||
<div class="column proposals-box--tags">
|
||||
<div class="proposals-box--tag-item">
|
||||
<div class="tag proposals-box--id">
|
||||
<span><number-format :value="votes" /> TORN</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="proposals-box--tag-item">
|
||||
<b-tooltip
|
||||
:label="ens.voter || voter"
|
||||
position="is-top"
|
||||
:multilined="ens.voter && ens.voter.length > 50"
|
||||
>
|
||||
<a
|
||||
target="_blank"
|
||||
:href="addressExplorerUrl(voter)"
|
||||
rel="noopener noreferrer"
|
||||
class="tag proposals-box--id is-link"
|
||||
v-text="shortVoter"
|
||||
/>
|
||||
</b-tooltip>
|
||||
</div>
|
||||
|
||||
<div v-if="delegator" class="proposals-box--tag-item">
|
||||
<b-tooltip
|
||||
:label="ens.delegator || delegator"
|
||||
position="is-top"
|
||||
:multilined="ens.delegator && ens.delegator.length > 50"
|
||||
>
|
||||
<a
|
||||
target="_blank"
|
||||
:href="addressExplorerUrl(delegator)"
|
||||
rel="noopener noreferrer"
|
||||
class="tag proposals-box--id is-link"
|
||||
v-text="$t('delegate')"
|
||||
/>
|
||||
</b-tooltip>
|
||||
</div>
|
||||
|
||||
<div class="proposals-box--tag-item is-percentage">
|
||||
<div class="tag proposals-box--id is-percentage">{{ percentage || '~0.1' }}%</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="proposals-box--comment">
|
||||
<b-icon
|
||||
:icon="support ? 'check' : 'close'"
|
||||
:type="support ? 'is-primary' : 'is-danger'"
|
||||
class="proposals-box--status-icon"
|
||||
/>
|
||||
<span v-if="loading" class="proposals-box--skeleton">
|
||||
<b-skeleton height="21" width="260" style="width: auto;" />
|
||||
</span>
|
||||
<template v-else>
|
||||
<span v-if="contact" class="proposals-box--title">{{ contact }}</span>
|
||||
<span v-if="message" class="proposals-box--info">{{ message }}</span>
|
||||
<span v-if="!contact && !message">-</span>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { mapGetters } from 'vuex'
|
||||
import { sliceAddress, sliceEnsName } from '@/utils'
|
||||
import NumberFormat from '@/components/NumberFormat'
|
||||
|
||||
export default {
|
||||
components: {
|
||||
NumberFormat
|
||||
},
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
loading: {
|
||||
type: Boolean,
|
||||
required: true
|
||||
},
|
||||
contact: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
message: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
support: {
|
||||
type: Boolean,
|
||||
required: true
|
||||
},
|
||||
votes: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
voter: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
percentage: {
|
||||
type: Number,
|
||||
required: true
|
||||
},
|
||||
delegator: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
ens: {
|
||||
type: Object,
|
||||
required: true,
|
||||
validator: (props) => 'delegator' in props && 'voter' in props
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
...mapGetters('txHashKeeper', ['addressExplorerUrl']),
|
||||
|
||||
shortVoter() {
|
||||
return sliceEnsName(this.ens.voter || '') || sliceAddress(this.voter)
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
$margin: 0.714rem;
|
||||
|
||||
.proposals-box {
|
||||
cursor: default;
|
||||
|
||||
.tag {
|
||||
width: 100%;
|
||||
margin: 0;
|
||||
|
||||
&.is-link {
|
||||
text-decoration: none;
|
||||
background-color: #363636;
|
||||
transition: background-color 0.15s ease-in-out;
|
||||
|
||||
&:hover {
|
||||
background-color: rgba(#363636, 0.5);
|
||||
}
|
||||
}
|
||||
|
||||
&.is-percentage {
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
background: transparent;
|
||||
text-align: right;
|
||||
}
|
||||
}
|
||||
|
||||
.columns {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
&--tags {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
margin: calc(#{-$margin * 0.5}) !important;
|
||||
}
|
||||
|
||||
&--tag-item {
|
||||
margin: calc(#{$margin * 0.5});
|
||||
width: auto;
|
||||
min-width: 110px;
|
||||
|
||||
@media screen and (max-width: 600px) {
|
||||
width: calc(50% - #{$margin});
|
||||
}
|
||||
|
||||
& > * {
|
||||
display: flex;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
&.is-percentage {
|
||||
min-width: auto;
|
||||
margin-left: auto;
|
||||
}
|
||||
}
|
||||
|
||||
&--title,
|
||||
&--info {
|
||||
word-break: break-word;
|
||||
display: inline;
|
||||
}
|
||||
|
||||
&--status-icon {
|
||||
vertical-align: middle;
|
||||
margin-bottom: 0.2rem;
|
||||
}
|
||||
|
||||
&--comment {
|
||||
margin-top: 1.5rem;
|
||||
}
|
||||
|
||||
&--skeleton {
|
||||
display: inline-block;
|
||||
}
|
||||
}
|
||||
</style>
|
91
components/governance/ProposalComments.vue
Normal file
91
components/governance/ProposalComments.vue
Normal file
@ -0,0 +1,91 @@
|
||||
<template>
|
||||
<div class="proposal-comments">
|
||||
<div class="proposals-list--header proposal-comments--header">
|
||||
<b-field class="field-tabs">
|
||||
<template v-for="item in tabs">
|
||||
<b-radio-button :key="item.id" v-model="currentTab" :native-value="item.id" type="is-primary">
|
||||
<span>{{ $t(item.label) }}</span>
|
||||
</b-radio-button>
|
||||
</template>
|
||||
</b-field>
|
||||
</div>
|
||||
|
||||
<ProposalCommentsSkeleton v-if="isFetchingComments" :size="comments.length ? 1 : 3" />
|
||||
<ProposalComment
|
||||
v-for="item in commentsFiltered"
|
||||
:key="item.id"
|
||||
v-bind="item"
|
||||
:loading="isFetchingMessages"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { mapActions, mapGetters, mapState } from 'vuex'
|
||||
import ProposalCommentsSkeleton from './ProposalCommentsSkeleton.vue'
|
||||
import ProposalComment from './ProposalComment.vue'
|
||||
|
||||
const TabTypes = {
|
||||
all: 'all',
|
||||
for: 'for',
|
||||
against: 'against'
|
||||
}
|
||||
|
||||
const TAB_LIST = [
|
||||
{ id: TabTypes.all, label: 'all' },
|
||||
{ id: TabTypes.for, label: 'for' },
|
||||
{ id: TabTypes.against, label: 'against' }
|
||||
]
|
||||
|
||||
export default {
|
||||
components: {
|
||||
ProposalCommentsSkeleton,
|
||||
ProposalComment
|
||||
},
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
proposal: {
|
||||
type: Object,
|
||||
required: true
|
||||
}
|
||||
},
|
||||
data: () => ({
|
||||
tabs: TAB_LIST,
|
||||
currentTab: TAB_LIST[0].id
|
||||
}),
|
||||
computed: {
|
||||
...mapState('governance/proposal', ['isFetchingComments', 'isFetchingMessages']),
|
||||
...mapGetters('governance/proposal', ['comments']),
|
||||
|
||||
commentsFiltered() {
|
||||
const { comments } = this
|
||||
|
||||
switch (this.currentTab) {
|
||||
case TabTypes.for:
|
||||
return comments.filter((_) => _.support === true)
|
||||
|
||||
case TabTypes.against:
|
||||
return comments.filter((_) => _.support === false)
|
||||
|
||||
case TabTypes.all:
|
||||
default:
|
||||
return comments
|
||||
}
|
||||
}
|
||||
},
|
||||
created() {
|
||||
this.fetchComments(this.proposal)
|
||||
},
|
||||
methods: {
|
||||
...mapActions('governance/proposal', ['fetchComments'])
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.proposal-comments {
|
||||
&--header {
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
}
|
||||
</style>
|
65
components/governance/ProposalCommentsSkeleton.vue
Normal file
65
components/governance/ProposalCommentsSkeleton.vue
Normal file
@ -0,0 +1,65 @@
|
||||
<template>
|
||||
<div>
|
||||
<div v-for="index in size" :key="index" class="proposals-box">
|
||||
<div class="columns is-gapless">
|
||||
<div class="column">
|
||||
<div class="proposals-box--title">
|
||||
<div class="proposals-box--skeleton">
|
||||
<b-skeleton height="30" width="110" />
|
||||
</div>
|
||||
<div class="proposals-box--skeleton">
|
||||
<b-skeleton height="30" width="110" />
|
||||
</div>
|
||||
|
||||
<div class="proposals-box--skeleton is-percentage">
|
||||
<b-skeleton height="30" width="36" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="proposals-box--comment">
|
||||
<div class="proposals-box--skeleton">
|
||||
<b-skeleton height="21" width="21" />
|
||||
</div>
|
||||
<div class="proposals-box--skeleton">
|
||||
<b-skeleton height="21" width="260" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
props: {
|
||||
size: {
|
||||
type: Number,
|
||||
default: 3
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
$margin: 0.714rem;
|
||||
|
||||
.proposals-box {
|
||||
&--title {
|
||||
width: 100%;
|
||||
margin: 0;
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
&--skeleton {
|
||||
display: inline-block;
|
||||
|
||||
&.is-percentage {
|
||||
float: right;
|
||||
}
|
||||
}
|
||||
|
||||
&--comment {
|
||||
margin-top: 1.5rem;
|
||||
}
|
||||
}
|
||||
</style>
|
@ -10,6 +10,7 @@
|
||||
<b-skeleton width="60%"></b-skeleton>
|
||||
<b-skeleton width="60%"></b-skeleton>
|
||||
</div>
|
||||
<ProposalCommentsSkeleton />
|
||||
</div>
|
||||
<div class="column is-5-tablet is-4-desktop">
|
||||
<div class="proposal-block">
|
||||
@ -77,3 +78,13 @@
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import ProposalCommentsSkeleton from './ProposalCommentsSkeleton.vue'
|
||||
|
||||
export default {
|
||||
components: {
|
||||
ProposalCommentsSkeleton
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
@ -1,8 +1,8 @@
|
||||
<template>
|
||||
<div class="proposals-box" @click="onClick">
|
||||
<div class="proposals-box is-link" @click="onClick">
|
||||
<div class="columns is-gapless">
|
||||
<div class="column is-8-tablet is-9-desktop">
|
||||
<div class="title">
|
||||
<div class="proposals-box--title">
|
||||
{{ data.title }}
|
||||
</div>
|
||||
<div class="proposals-box--info">
|
||||
|
@ -1,9 +1,9 @@
|
||||
<template>
|
||||
<div>
|
||||
<div v-for="(item, index) in emptyArray" :key="index" class="proposals-box">
|
||||
<div v-for="index in size" :key="index" class="proposals-box">
|
||||
<div class="columns is-gapless">
|
||||
<div class="column is-8-tablet is-9-desktop">
|
||||
<div class="title">
|
||||
<div class="proposals-box--title">
|
||||
<b-skeleton height="28" width="210"></b-skeleton>
|
||||
</div>
|
||||
<div class="proposals-box--info">
|
||||
@ -39,17 +39,11 @@
|
||||
|
||||
<script>
|
||||
export default {
|
||||
components: {},
|
||||
props: {
|
||||
size: {
|
||||
type: Number,
|
||||
default: 5
|
||||
}
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
emptyArray: Array(this.size).fill('')
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
@ -95,3 +95,5 @@ export const DUMMY_NONCE = '0x11111111111111111111111111111111111111111111111111
|
||||
|
||||
export const DUMMY_WITHDRAW_DATA =
|
||||
'0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111'
|
||||
|
||||
export const CHUNK_COUNT_PER_BATCH_REQUEST = 200
|
||||
|
@ -283,6 +283,17 @@
|
||||
"description": "Description is required"
|
||||
}
|
||||
},
|
||||
"proposalComment": {
|
||||
"modal-title": "Title: Proposal #{id}",
|
||||
"modal-subtitle": "Please explain: Why are you for or against this proposal?",
|
||||
"form-contact": "Contact",
|
||||
"form-contact-placeholder": "Enter contact (optional)",
|
||||
"form-contact-tooltip": "Contact (optional) may be a nickname in forum, email, telegram, twitter or other",
|
||||
"form-message": "Message",
|
||||
"form-message-placeholder": "Enter message",
|
||||
"form-message-opt-placeholder": "Enter message (optional)",
|
||||
"form-message-required": "Message required"
|
||||
},
|
||||
"executed": "Executed",
|
||||
"proposalDoesNotExist": "The proposal doesn't exist. Please go back to the list.",
|
||||
"errorPage": {
|
||||
|
@ -116,6 +116,7 @@ export default {
|
||||
ensSubdomainKey: 'mainnet-tornado',
|
||||
pollInterval: 15,
|
||||
constants: {
|
||||
GOVERNANCE_BLOCK: 11474695,
|
||||
NOTE_ACCOUNT_BLOCK: 11842486,
|
||||
ENCRYPTED_NOTES_BLOCK: 14248730,
|
||||
MINING_BLOCK_TIME: 15
|
||||
@ -534,6 +535,7 @@ export default {
|
||||
ensSubdomainKey: 'goerli-tornado',
|
||||
pollInterval: 15,
|
||||
constants: {
|
||||
GOVERNANCE_BLOCK: 3945171,
|
||||
NOTE_ACCOUNT_BLOCK: 4131375,
|
||||
ENCRYPTED_NOTES_BLOCK: 4131375,
|
||||
MINING_BLOCK_TIME: 15
|
||||
|
@ -9,6 +9,7 @@ export * from './events'
|
||||
export { default as graph } from './graph'
|
||||
export { default as schema } from './schema'
|
||||
export { default as walletConnectConnector } from './walletConnect'
|
||||
export * from './lookupAddress'
|
||||
|
||||
// eslint-disable-next-line no-undef
|
||||
window.graph = graph
|
||||
|
114
services/lookupAddress.js
Normal file
114
services/lookupAddress.js
Normal file
@ -0,0 +1,114 @@
|
||||
// from https://github.com/ChainSafe/web3.js/issues/2683#issuecomment-547348416
|
||||
|
||||
import namehash from 'eth-ens-namehash'
|
||||
import { BigNumber, utils } from 'ethers'
|
||||
import ABI from 'web3-eth-ens/lib/resources/ABI/Resolver'
|
||||
import uniq from 'lodash/uniq'
|
||||
import chunk from 'lodash/chunk'
|
||||
import { CHUNK_COUNT_PER_BATCH_REQUEST } from '@/constants'
|
||||
|
||||
export const createBatchRequestCallback = (resolve, reject) => (error, data) => {
|
||||
if (error) {
|
||||
reject(error)
|
||||
} else {
|
||||
resolve(data)
|
||||
}
|
||||
}
|
||||
|
||||
const CACHE = {}
|
||||
|
||||
const createFetchNodeAddresses = (registryContract, batch) => async (address) => {
|
||||
const addressLower = address.toLowerCase()
|
||||
|
||||
const node = addressLower.substr(2) + '.addr.reverse'
|
||||
const nodeHash = namehash.hash(node)
|
||||
let nodeAddress = null
|
||||
|
||||
if (!CACHE[addressLower]) {
|
||||
try {
|
||||
nodeAddress = await new Promise((resolve, reject) => {
|
||||
const callback = createBatchRequestCallback(resolve, reject)
|
||||
const requestData = registryContract.methods.resolver(nodeHash).call.request(callback)
|
||||
batch.add(requestData)
|
||||
})
|
||||
|
||||
if (+nodeAddress === 0) nodeAddress = null
|
||||
} catch (error) {
|
||||
console.error(`Error resolve ens for "${address}"`, error.message)
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
addressLower,
|
||||
address,
|
||||
nodeHash,
|
||||
nodeAddress
|
||||
}
|
||||
}
|
||||
|
||||
const createFetchEnsNames = (web3, batch, results) => async (data) => {
|
||||
const { address, addressLower, nodeHash, nodeAddress } = data
|
||||
if (!nodeAddress) return results
|
||||
|
||||
if (CACHE[addressLower]) {
|
||||
results[address] = CACHE[addressLower]
|
||||
return results
|
||||
}
|
||||
|
||||
const nodeContract = new web3.eth.Contract(ABI, nodeAddress)
|
||||
|
||||
try {
|
||||
const ensName = await new Promise((resolve, reject) => {
|
||||
const callback = createBatchRequestCallback(resolve, reject)
|
||||
const requestData = nodeContract.methods.name(nodeHash).call.request(callback)
|
||||
batch.add(requestData)
|
||||
})
|
||||
|
||||
const isZeroAddress =
|
||||
ensName.trim().length && utils.isAddress(ensName) && BigNumber.from(ensName).isZero()
|
||||
|
||||
if (isZeroAddress) return results
|
||||
|
||||
CACHE[addressLower] = ensName
|
||||
results[address] = ensName
|
||||
|
||||
return results
|
||||
} catch (error) {
|
||||
console.error(`Error lookupAddress ens for "${address}"`, error.message)
|
||||
return results
|
||||
}
|
||||
}
|
||||
|
||||
export const lookupAddressesRequest = async (addressList, web3, registryContract) => {
|
||||
const fetchNodeAddressesBatch = new web3.BatchRequest()
|
||||
const fetchNodeAddresses = createFetchNodeAddresses(registryContract, fetchNodeAddressesBatch)
|
||||
const fetchNodeAddressesPromises = uniq(addressList).map(fetchNodeAddresses)
|
||||
fetchNodeAddressesBatch.execute()
|
||||
|
||||
const nodeAddresses = await Promise.all(fetchNodeAddressesPromises)
|
||||
|
||||
const results = {}
|
||||
const fetchEnsNamesBatch = new web3.BatchRequest()
|
||||
const fetchEnsNames = createFetchEnsNames(web3, fetchEnsNamesBatch, results)
|
||||
const fetchEnsNamesPromises = nodeAddresses.map(fetchEnsNames)
|
||||
fetchEnsNamesBatch.execute()
|
||||
|
||||
await Promise.all(fetchEnsNamesPromises)
|
||||
return results
|
||||
}
|
||||
|
||||
export const lookupAddresses = async (addressList, web3) => {
|
||||
const registryContract = await web3.eth.ens.registry.contract
|
||||
// web3.eth.ens._lastSyncCheck = Date.now() // - need for test in fork
|
||||
|
||||
const addressListChunks = chunk(addressList, CHUNK_COUNT_PER_BATCH_REQUEST)
|
||||
let results = {}
|
||||
|
||||
for await (const list of addressListChunks) {
|
||||
const result = await lookupAddressesRequest(list, web3, registryContract)
|
||||
results = { ...results, ...result }
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
@ -1,11 +1,13 @@
|
||||
/* eslint-disable no-console */
|
||||
/* eslint-disable import/order */
|
||||
|
||||
import Web3 from 'web3'
|
||||
import { utils } from 'ethers'
|
||||
import { ToastProgrammatic as Toast } from 'buefy'
|
||||
|
||||
import networkConfig from '@/networkConfig'
|
||||
|
||||
import ERC20ABI from '@/abis/Governance.abi.json'
|
||||
import GovernanceABI from '@/abis/Governance.abi.json'
|
||||
import AggregatorABI from '@/abis/Aggregator.abi.json'
|
||||
|
||||
const { numberToHex, toWei, fromWei, toBN, hexToNumber, hexToNumberString } = require('web3-utils')
|
||||
@ -25,6 +27,7 @@ const state = () => {
|
||||
status: null
|
||||
},
|
||||
isFetchingProposals: true,
|
||||
isCastingVote: false,
|
||||
proposals: [],
|
||||
voterReceipts: [],
|
||||
hasActiveProposals: false,
|
||||
@ -39,13 +42,20 @@ const state = () => {
|
||||
}
|
||||
|
||||
const getters = {
|
||||
govContract: (state, getters, rootState) => ({ netId }) => {
|
||||
const config = networkConfig[`netId${netId}`]
|
||||
getConfig: (state, getters, rootState) => ({ netId }) => {
|
||||
return networkConfig[`netId${netId}`]
|
||||
},
|
||||
getWeb3: (state, getters, rootState) => ({ netId }) => {
|
||||
const { url } = rootState.settings[`netId${netId}`].rpc
|
||||
return new Web3(url)
|
||||
},
|
||||
govContract: (state, getters, rootState) => ({ netId }) => {
|
||||
const config = getters.getConfig({ netId })
|
||||
const address = config['governance.contract.tornadocash.eth']
|
||||
if (address) {
|
||||
const web3 = new Web3(url)
|
||||
return new web3.eth.Contract(ERC20ABI, address)
|
||||
const web3 = getters.getWeb3({ netId })
|
||||
const contract = new web3.eth.Contract(GovernanceABI, address)
|
||||
return contract
|
||||
}
|
||||
|
||||
return null
|
||||
@ -94,6 +104,9 @@ const mutations = {
|
||||
SAVE_FETCHING_PROPOSALS(state, status) {
|
||||
this._vm.$set(state, 'isFetchingProposals', status)
|
||||
},
|
||||
SAVE_CASTING_VOTE(state, status) {
|
||||
this._vm.$set(state, 'isCastingVote', status)
|
||||
},
|
||||
SAVE_LOCKED_BALANCE(state, { balance }) {
|
||||
this._vm.$set(state, 'lockedBalance', balance)
|
||||
},
|
||||
@ -152,6 +165,7 @@ const proposalIntervalConstants = [
|
||||
// 'VOTING_DELAY',
|
||||
'VOTING_PERIOD'
|
||||
]
|
||||
|
||||
const govConstants = ['PROPOSAL_THRESHOLD', 'QUORUM_VOTES']
|
||||
|
||||
const actions = {
|
||||
@ -331,28 +345,45 @@ const actions = {
|
||||
})
|
||||
}
|
||||
},
|
||||
async castVote({ getters, rootGetters, commit, rootState, dispatch, state }, { id, support }) {
|
||||
async castVote(context, payload) {
|
||||
const { getters, rootGetters, commit, rootState, dispatch, state } = context
|
||||
const { id, support, contact = '', message = '' } = payload
|
||||
|
||||
commit('SAVE_CASTING_VOTE', true)
|
||||
|
||||
try {
|
||||
const { ethAccount } = rootState.metamask
|
||||
const netId = rootGetters['metamask/netId']
|
||||
const govInstance = getters.govContract({ netId })
|
||||
const delegators = [...state.delegators]
|
||||
const web3 = getters.getWeb3({ netId })
|
||||
|
||||
if (toBN(state.lockedBalance).gt(toBN('0'))) {
|
||||
delegators.push(ethAccount)
|
||||
}
|
||||
|
||||
const gas = await govInstance.methods
|
||||
.castDelegatedVote(delegators, id, support)
|
||||
.estimateGas({ from: ethAccount, value: 0 })
|
||||
const data = await govInstance.methods.castDelegatedVote(delegators, id, support).encodeABI()
|
||||
const data = govInstance.methods.castDelegatedVote(delegators, id, support).encodeABI()
|
||||
let dataWithTail = data
|
||||
|
||||
if (contact || message) {
|
||||
const value = JSON.stringify([contact, message])
|
||||
const tail = utils.defaultAbiCoder.encode(['string'], [value])
|
||||
dataWithTail = utils.hexConcat([data, tail])
|
||||
}
|
||||
|
||||
const gas = await web3.eth.estimateGas({
|
||||
from: ethAccount,
|
||||
to: govInstance._address,
|
||||
value: 0,
|
||||
data: dataWithTail
|
||||
})
|
||||
|
||||
const callParams = {
|
||||
method: 'eth_sendTransaction',
|
||||
params: {
|
||||
to: govInstance._address,
|
||||
gas: numberToHex(gas + 30000),
|
||||
data
|
||||
data: dataWithTail
|
||||
},
|
||||
watcherParams: {
|
||||
title: support ? 'votingFor' : 'votingAgainst',
|
||||
@ -392,6 +423,7 @@ const actions = {
|
||||
)
|
||||
} finally {
|
||||
dispatch('loading/disable', {}, { root: true })
|
||||
commit('SAVE_CASTING_VOTE', false)
|
||||
}
|
||||
},
|
||||
async executeProposal({ getters, rootGetters, commit, rootState, dispatch }, { id }) {
|
||||
@ -619,6 +651,7 @@ const actions = {
|
||||
const netId = rootGetters['metamask/netId']
|
||||
const aggregatorContract = getters.aggregatorContract
|
||||
const govInstance = getters.govContract({ netId })
|
||||
const config = getters.getConfig({ netId })
|
||||
|
||||
if (!govInstance) {
|
||||
return
|
||||
@ -626,7 +659,7 @@ const actions = {
|
||||
|
||||
const [events, statuses] = await Promise.all([
|
||||
govInstance.getPastEvents('ProposalCreated', {
|
||||
fromBlock: 0,
|
||||
fromBlock: config.constants.GOVERNANCE_BLOCK,
|
||||
toBlock: 'latest'
|
||||
}),
|
||||
aggregatorContract.methods.getAllProposals(govInstance._address).call()
|
||||
@ -663,7 +696,7 @@ const actions = {
|
||||
}
|
||||
|
||||
proposals = events
|
||||
.map(({ returnValues }, index) => {
|
||||
.map(({ returnValues, blockNumber }, index) => {
|
||||
const id = Number(returnValues.id)
|
||||
const { state, startTime, endTime, forVotes, againstVotes } = statuses[index]
|
||||
const { title, description } = parseDescription({ id, text: returnValues.description })
|
||||
@ -677,6 +710,7 @@ const actions = {
|
||||
endTime: Number(endTime),
|
||||
startTime: Number(startTime),
|
||||
status: ProposalState[Number(state)],
|
||||
blockNumber,
|
||||
results: {
|
||||
for: fromWei(forVotes),
|
||||
against: fromWei(againstVotes)
|
||||
@ -767,6 +801,7 @@ const actions = {
|
||||
}
|
||||
|
||||
const netId = rootGetters['metamask/netId']
|
||||
const config = getters.getConfig({ netId })
|
||||
|
||||
const aggregatorContract = getters.aggregatorContract
|
||||
const govInstance = getters.govContract({ netId })
|
||||
@ -774,14 +809,14 @@ const actions = {
|
||||
filter: {
|
||||
to: ethAccount
|
||||
},
|
||||
fromBlock: 0,
|
||||
fromBlock: config.constants.GOVERNANCE_BLOCK,
|
||||
toBlock: 'latest'
|
||||
})
|
||||
let undelegatedAccs = await govInstance.getPastEvents('Undelegated', {
|
||||
filter: {
|
||||
from: ethAccount
|
||||
},
|
||||
fromBlock: 0,
|
||||
fromBlock: config.constants.GOVERNANCE_BLOCK,
|
||||
toBlock: 'latest'
|
||||
})
|
||||
delegatedAccs = delegatedAccs.map((acc) => acc.returnValues.account)
|
||||
|
269
store/governance/proposal.js
Normal file
269
store/governance/proposal.js
Normal file
@ -0,0 +1,269 @@
|
||||
/* eslint-disable no-console */
|
||||
/* eslint-disable import/order */
|
||||
|
||||
import { utils } from 'ethers'
|
||||
import uniqBy from 'lodash/uniqBy'
|
||||
import chunk from 'lodash/chunk'
|
||||
|
||||
import { lookupAddresses, createBatchRequestCallback } from '@/services'
|
||||
import { CHUNK_COUNT_PER_BATCH_REQUEST } from '@/constants'
|
||||
|
||||
const { toWei, fromWei, toBN } = require('web3-utils')
|
||||
|
||||
const CACHE_TX = {}
|
||||
const CACHE_BLOCK = {}
|
||||
|
||||
const parseComment = (calldata, govInstance) => {
|
||||
const empty = { contact: '', message: '' }
|
||||
if (!calldata || !govInstance) return empty
|
||||
|
||||
const methodLength = 4 // length of castDelegatedVote method
|
||||
const result = utils.defaultAbiCoder.decode(
|
||||
['address[]', 'uint256', 'bool'],
|
||||
utils.hexDataSlice(calldata, methodLength)
|
||||
)
|
||||
const data = govInstance.methods.castDelegatedVote(...result).encodeABI()
|
||||
const dataLength = utils.hexDataLength(data)
|
||||
|
||||
try {
|
||||
const str = utils.defaultAbiCoder.decode(['string'], utils.hexDataSlice(calldata, dataLength))
|
||||
const [contact, message] = JSON.parse(str)
|
||||
return { contact, message }
|
||||
} catch {
|
||||
return empty
|
||||
}
|
||||
}
|
||||
|
||||
const createProposalComment = (resultAll, votedEvent) => {
|
||||
const { transactionHash, returnValues, blockNumber } = votedEvent
|
||||
const { voter } = returnValues
|
||||
|
||||
const comment = parseComment()
|
||||
|
||||
const percentage =
|
||||
toBN(votedEvent.returnValues.votes)
|
||||
.mul(toBN(10000))
|
||||
.divRound(resultAll)
|
||||
.toNumber() / 100
|
||||
|
||||
return {
|
||||
id: `${transactionHash}-${voter}`,
|
||||
percentage,
|
||||
...returnValues,
|
||||
votes: fromWei(returnValues.votes),
|
||||
transactionHash,
|
||||
blockNumber,
|
||||
|
||||
...comment,
|
||||
|
||||
ens: {
|
||||
delegator: null,
|
||||
voter: null
|
||||
},
|
||||
delegator: null,
|
||||
timestamp: null
|
||||
}
|
||||
}
|
||||
|
||||
const createFetchCommentWithMessage = (web3, batch, govInstance) => async (proposalComment) => {
|
||||
const { transactionHash, voter, blockNumber } = proposalComment
|
||||
|
||||
if (!CACHE_TX[transactionHash]) {
|
||||
CACHE_TX[transactionHash] = new Promise((resolve, reject) => {
|
||||
const callback = createBatchRequestCallback(resolve, reject)
|
||||
batch.add(web3.eth.getTransaction.request(transactionHash, callback))
|
||||
})
|
||||
}
|
||||
|
||||
if (!CACHE_BLOCK[blockNumber]) {
|
||||
CACHE_BLOCK[blockNumber] = new Promise((resolve, reject) => {
|
||||
const callback = createBatchRequestCallback(resolve, reject)
|
||||
batch.add(web3.eth.getBlock.request(blockNumber, callback))
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
const [tx, blockInfo] = await Promise.all([CACHE_TX[transactionHash], CACHE_BLOCK[blockNumber]])
|
||||
|
||||
const isMaybeHasComment = voter === tx.from
|
||||
const comment = parseComment(isMaybeHasComment ? tx.input : null, govInstance)
|
||||
|
||||
return {
|
||||
...proposalComment,
|
||||
...comment,
|
||||
|
||||
delegator: voter === tx.from ? null : tx.from,
|
||||
timestamp: blockInfo.timestamp
|
||||
}
|
||||
} catch (error) {
|
||||
CACHE_TX[transactionHash] = null
|
||||
CACHE_BLOCK[blockNumber] = null
|
||||
return proposalComment
|
||||
}
|
||||
}
|
||||
|
||||
const state = () => {
|
||||
return {
|
||||
isFetchingComments: false,
|
||||
isFetchingMessages: false,
|
||||
ensNames: {},
|
||||
comments: []
|
||||
}
|
||||
}
|
||||
|
||||
const getters = {
|
||||
comments: (state) => {
|
||||
const { ensNames } = state
|
||||
let comments = state.comments.slice()
|
||||
|
||||
comments.sort((a, b) => b.blockNumber - a.blockNumber)
|
||||
comments = uniqBy(comments, 'voter')
|
||||
comments.sort((a, b) => b.percentage - a.percentage)
|
||||
|
||||
comments = comments.map((data) => ({
|
||||
...data,
|
||||
ens: {
|
||||
delegator: ensNames[data.delegator],
|
||||
voter: ensNames[data.voter]
|
||||
}
|
||||
}))
|
||||
|
||||
return comments
|
||||
}
|
||||
}
|
||||
|
||||
const mutations = {
|
||||
SAVE_FETCHING_COMMENTS(state, status) {
|
||||
state.isFetchingComments = status
|
||||
},
|
||||
SAVE_FETCHING_MESSAGES(state, status) {
|
||||
state.isFetchingMessages = status
|
||||
},
|
||||
SAVE_ENS_NAMES(state, ensNames) {
|
||||
state.ensNames = { ...state.ensNames, ...ensNames }
|
||||
},
|
||||
SAVE_COMMENTS(state, comments) {
|
||||
state.comments = comments
|
||||
}
|
||||
}
|
||||
|
||||
const actions = {
|
||||
async fetchComments(context, proposal) {
|
||||
const { commit, dispatch, state } = context
|
||||
let { comments } = state
|
||||
let newComments = []
|
||||
|
||||
if (comments[0]?.id !== proposal.id) {
|
||||
commit('SAVE_COMMENTS', [])
|
||||
comments = []
|
||||
}
|
||||
|
||||
commit('SAVE_FETCHING_COMMENTS', true)
|
||||
newComments = await dispatch('fetchVotedEvents', { proposal, comments })
|
||||
commit('SAVE_FETCHING_COMMENTS', false)
|
||||
|
||||
if (!newComments) return
|
||||
commit('SAVE_COMMENTS', newComments.concat(comments))
|
||||
dispatch('fetchEnsNames', { comments: newComments })
|
||||
|
||||
commit('SAVE_FETCHING_MESSAGES', true)
|
||||
// TODO: TC-163 - add pagination
|
||||
newComments = await dispatch('fetchCommentsMessages', { comments: newComments })
|
||||
commit('SAVE_FETCHING_MESSAGES', false)
|
||||
|
||||
if (!newComments) return
|
||||
commit('SAVE_COMMENTS', newComments.concat(comments))
|
||||
},
|
||||
async fetchVotedEvents(context, { proposal, comments }) {
|
||||
const { rootGetters } = context
|
||||
let { blockNumber: fromBlock } = proposal
|
||||
|
||||
const netId = rootGetters['metamask/netId']
|
||||
const govInstance = rootGetters['governance/gov/govContract']({ netId })
|
||||
|
||||
if (comments[0]?.id === proposal.id) {
|
||||
fromBlock = comments[0].blockNumber + 1
|
||||
}
|
||||
|
||||
try {
|
||||
let votedEvents = await govInstance.getPastEvents('Voted', {
|
||||
filter: {
|
||||
// support: [false],
|
||||
proposalId: proposal.id
|
||||
},
|
||||
fromBlock,
|
||||
toBlock: 'latest'
|
||||
})
|
||||
|
||||
console.log('fetchVotedEvents', votedEvents.length)
|
||||
|
||||
votedEvents = votedEvents.sort((a, b) => b.blockNumber - a.blockNumber)
|
||||
votedEvents = uniqBy(votedEvents, 'returnValues.voter')
|
||||
|
||||
console.log('fetchVotedEvents uniq', votedEvents.length)
|
||||
|
||||
const resultAll = toBN(toWei(proposal.results.for)).add(toBN(toWei(proposal.results.against)))
|
||||
let newComments = votedEvents.map((votedEvent) => createProposalComment(resultAll, votedEvent))
|
||||
newComments = newComments.concat(comments)
|
||||
return newComments
|
||||
} catch (e) {
|
||||
console.error('fetchVotedEvents', e.message)
|
||||
return null
|
||||
}
|
||||
},
|
||||
async fetchCommentsMessages(context, { comments }) {
|
||||
const { rootGetters } = context
|
||||
|
||||
const netId = rootGetters['metamask/netId']
|
||||
const govInstance = rootGetters['governance/gov/govContract']({ netId })
|
||||
const web3 = rootGetters['governance/gov/getWeb3']({ netId })
|
||||
const commentListChunks = chunk(comments, CHUNK_COUNT_PER_BATCH_REQUEST)
|
||||
|
||||
let results = []
|
||||
|
||||
try {
|
||||
for await (const list of commentListChunks) {
|
||||
const batch = new web3.BatchRequest()
|
||||
const fetchCommentsWithMessages = createFetchCommentWithMessage(web3, batch, govInstance)
|
||||
const promises = list.map(fetchCommentsWithMessages)
|
||||
batch.execute()
|
||||
const result = await Promise.all(promises)
|
||||
|
||||
results = results.concat(result)
|
||||
}
|
||||
|
||||
return results
|
||||
} catch (e) {
|
||||
console.error('fetchCommentsMessages', e.message)
|
||||
}
|
||||
},
|
||||
async fetchEnsNames(context, { comments }) {
|
||||
const { rootGetters, commit } = context
|
||||
|
||||
const netId = rootGetters['metamask/netId']
|
||||
const web3 = rootGetters['governance/gov/getWeb3']({ netId })
|
||||
|
||||
try {
|
||||
const addresses = comments
|
||||
.map((_) => _.voter)
|
||||
.flat()
|
||||
.filter(Boolean)
|
||||
|
||||
console.log('fetchEnsNames', addresses.length)
|
||||
|
||||
const ensNames = await lookupAddresses(addresses, web3)
|
||||
|
||||
commit('SAVE_ENS_NAMES', ensNames)
|
||||
} catch (e) {
|
||||
console.error('fetchEnsNames', e.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
state,
|
||||
getters,
|
||||
mutations,
|
||||
actions
|
||||
}
|
@ -10,6 +10,30 @@ export const sliceAddress = (address) => {
|
||||
return '0x' + hashRender(address.slice(2))
|
||||
}
|
||||
|
||||
export const sliceEnsName = (name, size = 4, separator = '...') => {
|
||||
const chars = [...name]
|
||||
|
||||
const last = name
|
||||
.split('.')
|
||||
.pop()
|
||||
.slice(-size)
|
||||
|
||||
if (chars[0]?.length === 2 && last) {
|
||||
// 🐵🍆💦.eth -> 🐵🍆💦.eth
|
||||
if (chars.length - 4 <= 4) return name
|
||||
|
||||
// 🦍🦍🦍🦍🦍🦍🦍.eth -> 🦍🦍🦍...eth
|
||||
return [].concat(chars.slice(0, 3), separator, last).join('')
|
||||
}
|
||||
|
||||
if (chars.length <= 2 * size + 2 + separator.length) return name
|
||||
if (!name.includes('.')) return sliceAddress(name, size, separator)
|
||||
|
||||
return last.length
|
||||
? [].concat(chars.slice(0, 2 * size - last.length), separator, last).join('')
|
||||
: [].concat(chars.slice(0, size), separator, chars.slice(-size)).join('')
|
||||
}
|
||||
|
||||
const semVerRegex = /^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/
|
||||
|
||||
export const parseSemanticVersion = (version) => {
|
||||
|
Loading…
Reference in New Issue
Block a user