refactor fetchProposals

This commit is contained in:
Danil Kovtonyuk 2022-06-29 22:17:24 +10:00
parent 7f1f1f4750
commit e1e74c7dc0
3 changed files with 56 additions and 55 deletions

@ -207,6 +207,7 @@
.description { .description {
p { p {
line-height: 1.7; line-height: 1.7;
white-space: pre-wrap;
&:not(:last-child) { &:not(:last-child) {
margin-bottom: 2rem; margin-bottom: 2rem;

@ -4,9 +4,7 @@
<div class="column is-7-tablet is-8-desktop"> <div class="column is-7-tablet is-8-desktop">
<h1 class="title">{{ data.title }}</h1> <h1 class="title">{{ data.title }}</h1>
<div class="description"> <div class="description">
<p> <p>{{ data.description }}</p>
{{ data.description }}
</p>
</div> </div>
</div> </div>
<div class="column is-5-tablet is-4-desktop"> <div class="column is-5-tablet is-4-desktop">

@ -612,7 +612,7 @@ const actions = {
} }
}, },
async fetchProposals({ rootGetters, getters, commit }, { requestId }) { async fetchProposals({ rootGetters, getters, commit }, { requestId }) {
let proposals let proposals = []
try { try {
commit('SAVE_FETCHING_PROPOSALS', true) commit('SAVE_FETCHING_PROPOSALS', true)
@ -624,68 +624,70 @@ const actions = {
return return
} }
proposals = await govInstance.getPastEvents('ProposalCreated', { const [events, statuses] = await Promise.all([
govInstance.getPastEvents('ProposalCreated', {
fromBlock: 0, fromBlock: 0,
toBlock: 'latest' toBlock: 'latest'
}) }),
aggregatorContract.methods.getAllProposals(govInstance._address).call()
])
const parseDescription = ({ id, text }) => {
console.log('text', text)
if (netId === 1) {
switch (id) {
case 1:
return {
title: text,
description: 'See: https://torn.community/t/proposal-1-enable-torn-transfers/38'
}
case 10:
text = text.replace('\n', '\\n\\n')
break
case 11:
text = text.replace('"description"', ',"description"')
break
case 13:
text = text.replace(/\\\\n\\\\n(\s)?(\\n)?/g, '\\n')
}
}
let title, description, rest let title, description, rest
proposals = proposals.map((e) => {
try { try {
;({ title, description } = JSON.parse(e.returnValues.description)) ;({ title, description } = JSON.parse(text))
} catch (err) { } catch {
;[title, ...rest] = e.returnValues.description.split('\n', 2) ;[title, ...rest] = text.split('\n', 2)
description = rest.join('\n') description = rest.join('\n')
} }
const netId = rootGetters['metamask/netId'] return { title, description }
if (netId === 1) {
switch (Number(e.returnValues.id)) {
case 1:
description = 'See: https://torn.community/t/proposal-1-enable-torn-transfers/38'
break
case 10:
;({ title, description } = JSON.parse(e.returnValues.description.replaceAll('\n', '')))
break
case 11:
;({ title, description } = JSON.parse(
e.returnValues.description.replace('"description"', ',"description"')
))
}
} }
proposals = events
.map(({ returnValues }, index) => {
const id = Number(returnValues.id)
const { state, startTime, endTime, forVotes, againstVotes } = statuses[index]
const { title, description } = parseDescription({ id, text: returnValues.description })
return { return {
id: Number(e.returnValues.id), id,
title, title,
description, description,
endTime: Number(e.returnValues.endTime), target: returnValues.target,
startTime: Number(e.returnValues.startTime), proposer: returnValues.proposer,
proposer: e.returnValues.proposer, endTime: Number(endTime),
target: e.returnValues.target, startTime: Number(startTime),
status: ProposalState[Number(state)],
results: { results: {
for: '0', for: fromWei(forVotes),
against: '0' against: fromWei(againstVotes)
} }
} }
}) })
const statuses = await aggregatorContract.methods.getAllProposals(govInstance._address).call()
proposals = proposals
.map((p) => {
const proposal = statuses[Number(p.id) - 1]
if (proposal.extended) {
p.endTime = Number(proposal.endTime)
}
p.status = ProposalState[Number(proposal.state)]
p.results = {
for: fromWei(proposal.forVotes),
against: fromWei(proposal.againstVotes)
}
return p
})
.sort((a, b) => { .sort((a, b) => {
return a.id - b.id return a.id - b.id
}) })
if (requestId) { if (requestId) {
return proposals[requestId] return proposals[requestId]
} }