Skip to content

Commit

Permalink
Merge pull request #482 from ethereum/refactor-tally-fetch
Browse files Browse the repository at this point in the history
Tally fetch refactor
  • Loading branch information
samajammin authored Jan 6, 2022
2 parents 0fab2a5 + 2b62eed commit e24b0d7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 32 deletions.
85 changes: 55 additions & 30 deletions vue-app/src/components/ClaimButton.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<template>
<div>
<loader v-if="isLoading" />
<p v-if="claimed">
✔️ {{ formatAmount(allocatedAmount) }} {{ tokenSymbol }} claimed
</p>
Expand All @@ -15,64 +16,92 @@
</template>

<script lang="ts">
import { Vue, Component, Prop } from 'vue-property-decorator'
import { Vue, Component, Prop, Watch } from 'vue-property-decorator'
import { FixedNumber } from 'ethers'
import { getAllocatedAmount, isFundsClaimed } from '@/api/claims'
import { Project } from '@/api/projects'
import { RoundStatus } from '@/api/round'
import { RoundStatus, RoundInfo } from '@/api/round'
import { Tally } from '@/api/tally'
import ClaimModal from '@/components/ClaimModal.vue'
import { markdown } from '@/utils/markdown'
import { LOAD_TALLY } from '@/store/action-types'
import { formatAmount } from '@/utils/amounts'
import { markdown } from '@/utils/markdown'
@Component
import ClaimModal from '@/components/ClaimModal.vue'
import Loader from '@/components/Loader.vue'
@Component({
components: { Loader },
})
export default class ClaimButton extends Vue {
@Prop() project!: Project
allocatedAmount: FixedNumber | null = null
claimed: boolean | null = null
isLoading = true
get currentRound(): RoundInfo | null {
return this.$store.state.currentRound
}
get tally(): Tally | null {
return this.$store.state.tally
}
get descriptionHtml(): string {
return markdown.render(this.project?.description || '')
}
get tokenSymbol(): string {
const { nativeTokenSymbol } = this.$store.state.currentRound
return nativeTokenSymbol ?? ''
}
created() {
// Wait for tally to load and get claim status
this.$store.watch((state) => state.tally, this.checkAllocation)
this.checkAllocation(this.$store.state.tally)
this.isLoading = false
this.checkAllocation()
}
private async checkAllocation(tally: Tally | null) {
const currentRound = this.$store.state.currentRound
@Watch('currentRound')
async checkAllocation() {
// If the current round is not finalized or the allocated amount was already
// fetched then don't do anything
if (
!this.project ||
!currentRound ||
currentRound.status !== RoundStatus.Finalized ||
!tally
!this.currentRound ||
!this.$store.getters.isRoundFinalized ||
this.allocatedAmount
) {
this.isLoading = false
return
}
this.isLoading = true
if (!this.tally) {
await this.loadTally()
}
this.allocatedAmount = await getAllocatedAmount(
currentRound.fundingRoundAddress,
currentRound.nativeTokenDecimals,
tally.results.tally[this.project.index],
tally.totalVoiceCreditsPerVoteOption.tally[this.project.index]
this.currentRound.fundingRoundAddress,
this.currentRound.nativeTokenDecimals,
this.tally!.results.tally[this.project.index],
this.tally!.totalVoiceCreditsPerVoteOption.tally[this.project.index]
)
this.claimed = await isFundsClaimed(
currentRound.fundingRoundAddress,
this.currentRound.fundingRoundAddress,
this.project.address
)
this.isLoading = false
}
get tokenSymbol(): string {
const { nativeTokenSymbol } = this.$store.state.currentRound
return nativeTokenSymbol ?? ''
async loadTally() {
await this.$store.dispatch(LOAD_TALLY)
}
hasClaimBtn(): boolean {
const { currentRound } = this.$store.state
return (
currentRound &&
currentRound.status === RoundStatus.Finalized &&
!!this.currentRound &&
this.currentRound.status === RoundStatus.Finalized &&
this.project !== null &&
this.project.index !== 0 &&
!this.project.isHidden &&
Expand All @@ -87,7 +116,7 @@ export default class ClaimButton extends Vue {
formatAmount(value: FixedNumber): string {
const maxDecimals = 6
const { nativeTokenDecimals } = this.$store.state.currentRound
const { nativeTokenDecimals } = this.currentRound!
return formatAmount(value, nativeTokenDecimals, null, maxDecimals)
}
Expand All @@ -104,10 +133,6 @@ export default class ClaimButton extends Vue {
{}
)
}
get descriptionHtml(): string {
return markdown.render(this.project?.description || '')
}
}
</script>

Expand Down
1 change: 1 addition & 0 deletions vue-app/src/store/action-types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const SELECT_ROUND = 'SELECT_ROUND'
export const LOAD_TALLY = 'LOAD_TALLY'
export const LOAD_FACTORY_INFO = 'LOAD_FACTORY_INFO'
export const LOAD_ROUND_INFO = 'LOAD_ROUND_INFO'
export const LOAD_RECIPIENT_REGISTRY_INFO = 'LOAD_RECIPIENT_REGISTRY_INFO'
Expand Down
8 changes: 6 additions & 2 deletions vue-app/src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
LOAD_FACTORY_INFO,
LOAD_RECIPIENT_REGISTRY_INFO,
LOAD_ROUND_INFO,
LOAD_TALLY,
LOAD_USER_INFO,
LOGIN_USER,
LOGOUT_USER,
Expand Down Expand Up @@ -92,8 +93,11 @@ const actions = {
//TODO: update to take factory address as a parameter, default to env. variable
const round = await getRoundInfo(roundAddress)
commit(SET_CURRENT_ROUND, round)
if (round && round.status === RoundStatus.Finalized) {
const tally = await getTally(roundAddress)
},
async [LOAD_TALLY]({ commit, state }) {
const currentRound = state.currentRound
if (currentRound && currentRound.status === RoundStatus.Finalized) {
const tally = await getTally(state.currentRoundAddress)
commit(SET_TALLY, tally)
}
},
Expand Down

0 comments on commit e24b0d7

Please sign in to comment.