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

View File

@ -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;

View File

@ -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">

View File

@ -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([
fromBlock: 0, govInstance.getPastEvents('ProposalCreated', {
toBlock: 'latest' fromBlock: 0,
}) toBlock: 'latest'
let title, description, rest }),
proposals = proposals.map((e) => { 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
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"')
))
}
}
return { proposals = events
id: Number(e.returnValues.id), .map(({ returnValues }, index) => {
title, const id = Number(returnValues.id)
description, const { state, startTime, endTime, forVotes, againstVotes } = statuses[index]
endTime: Number(e.returnValues.endTime), const { title, description } = parseDescription({ id, text: returnValues.description })
startTime: Number(e.returnValues.startTime),
proposer: e.returnValues.proposer,
target: e.returnValues.target,
results: {
for: '0',
against: '0'
}
}
})
const statuses = await aggregatorContract.methods.getAllProposals(govInstance._address).call()
proposals = proposals
.map((p) => {
const proposal = statuses[Number(p.id) - 1]
if (proposal.extended) { return {
p.endTime = Number(proposal.endTime) id,
title,
description,
target: returnValues.target,
proposer: returnValues.proposer,
endTime: Number(endTime),
startTime: Number(startTime),
status: ProposalState[Number(state)],
results: {
for: fromWei(forVotes),
against: fromWei(againstVotes)
}
} }
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]
} }