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 {
p {
line-height: 1.7;
white-space: pre-wrap;
&:not(:last-child) {
margin-bottom: 2rem;

View File

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

View File

@ -612,7 +612,7 @@ const actions = {
}
},
async fetchProposals({ rootGetters, getters, commit }, { requestId }) {
let proposals
let proposals = []
try {
commit('SAVE_FETCHING_PROPOSALS', true)
@ -624,68 +624,70 @@ const actions = {
return
}
proposals = await govInstance.getPastEvents('ProposalCreated', {
const [events, statuses] = await Promise.all([
govInstance.getPastEvents('ProposalCreated', {
fromBlock: 0,
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
proposals = proposals.map((e) => {
try {
;({ title, description } = JSON.parse(e.returnValues.description))
} catch (err) {
;[title, ...rest] = e.returnValues.description.split('\n', 2)
;({ title, description } = JSON.parse(text))
} catch {
;[title, ...rest] = text.split('\n', 2)
description = rest.join('\n')
}
const netId = rootGetters['metamask/netId']
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 { title, 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 {
id: Number(e.returnValues.id),
id,
title,
description,
endTime: Number(e.returnValues.endTime),
startTime: Number(e.returnValues.startTime),
proposer: e.returnValues.proposer,
target: e.returnValues.target,
target: returnValues.target,
proposer: returnValues.proposer,
endTime: Number(endTime),
startTime: Number(startTime),
status: ProposalState[Number(state)],
results: {
for: '0',
against: '0'
for: fromWei(forVotes),
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) => {
return a.id - b.id
})
if (requestId) {
return proposals[requestId]
}