-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: create dedicated composable for vote voting power (#938)
* refactor: refactor: create dedicated composable for vote voting power * refactor: use single fetch and get functions * fix: lint fix * refactor: better variable name * fix: avoid type casting * refactor: rename function for consistency
- Loading branch information
Showing
5 changed files
with
52 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,58 @@ | ||
import { supportsNullCurrent } from '@/networks'; | ||
import { getIndex as getVotingPowerIndex } from '@/stores/votingPowers'; | ||
import { Proposal, Space } from '@/types'; | ||
import { getIndex } from '@/stores/votingPowers'; | ||
import { NetworkID, Proposal, Space } from '@/types'; | ||
|
||
export function useVotingPower() { | ||
const votingPowersStore = useVotingPowersStore(); | ||
const { web3 } = useWeb3(); | ||
const { getCurrent } = useMetaStore(); | ||
|
||
const item = ref<Space | Proposal | undefined>(); | ||
const block = ref<number | null>(null); | ||
|
||
const space = computed(() => | ||
item.value && 'space' in item.value | ||
? (item.value?.space as Space) | ||
: item.value | ||
); | ||
function getLatestBlock(network: NetworkID): number | null { | ||
return supportsNullCurrent(network) ? null : getCurrent(network) || 0; | ||
} | ||
|
||
const proposal = computed(() => | ||
item.value && 'snapshot' in item.value | ||
? (item.value as Proposal) | ||
: undefined | ||
); | ||
function getProposalSnapshot(proposal: Proposal): number | null { | ||
return ( | ||
(proposal.state === 'pending' ? null : proposal.snapshot) || | ||
getLatestBlock(proposal.network) | ||
); | ||
} | ||
|
||
const proposalSnapshot = computed<number | null>(() => { | ||
if (!proposal.value) return null; | ||
function getSnapshot( | ||
space: Space | Proposal['space'], | ||
proposal?: Proposal | ||
): number | null { | ||
return proposal | ||
? getProposalSnapshot(proposal) | ||
: getLatestBlock((space as Space).network); | ||
} | ||
|
||
return proposal.value.state === 'pending' | ||
? latestBlock(proposal.value) | ||
: proposal.value.snapshot; | ||
}); | ||
function fetch(space: Space | Proposal['space'], proposal?: Proposal) { | ||
if (!web3.value.account) return; | ||
|
||
const votingPower = computed( | ||
() => | ||
space.value && | ||
votingPowersStore.votingPowers.get( | ||
getVotingPowerIndex(space.value, block.value) | ||
) | ||
); | ||
votingPowersStore.fetch( | ||
proposal || (space as Space), | ||
web3.value.account, | ||
getSnapshot(space, proposal) | ||
); | ||
} | ||
|
||
function latestBlock(spaceOrProposal: Space | Proposal) { | ||
return supportsNullCurrent(spaceOrProposal.network) | ||
? null | ||
: getCurrent(spaceOrProposal.network) ?? 0; | ||
function get(space: Space | Proposal['space'], proposal?: Proposal) { | ||
return votingPowersStore.votingPowers.get( | ||
getIndex(proposal?.space || space, getSnapshot(space, proposal)) | ||
); | ||
} | ||
|
||
function reset() { | ||
votingPowersStore.reset(); | ||
} | ||
|
||
function fetch(spaceOrProposal: Space | Proposal) { | ||
if (!web3.value.account) return; | ||
item.value = spaceOrProposal; | ||
block.value = proposal.value | ||
? proposalSnapshot.value | ||
: latestBlock(space.value as Space); | ||
|
||
votingPowersStore.fetch(item.value, web3.value.account, block.value); | ||
} | ||
|
||
watch( | ||
() => web3.value.account, | ||
account => { | ||
if (!account) reset(); | ||
} | ||
); | ||
|
||
return { votingPower, fetch, reset }; | ||
return { get, fetch, reset }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters