Skip to content

Commit

Permalink
refactor: create dedicated composable for vote voting power (#938)
Browse files Browse the repository at this point in the history
* 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
wa0x6e authored Dec 10, 2024
1 parent 96d9bfa commit c4e071a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 51 deletions.
8 changes: 6 additions & 2 deletions apps/ui/src/components/Modal/Vote.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const emit = defineEmits<{
const { vote } = useActions();
const { web3 } = useWeb3();
const {
votingPower,
get: getVotingPower,
fetch: fetchVotingPower,
reset: resetVotingPower
} = useVotingPower();
Expand Down Expand Up @@ -55,6 +55,10 @@ const formValidator = getValidator({
}
});
const votingPower = computed(() =>
getVotingPower(props.proposal.space, props.proposal)
);
const formattedVotingPower = computed(() =>
getFormattedVotingPower(votingPower.value)
);
Expand Down Expand Up @@ -124,7 +128,7 @@ async function handleConfirmed(tx?: string | null) {
}
function handleFetchVotingPower() {
fetchVotingPower(props.proposal);
fetchVotingPower(props.proposal.space, props.proposal);
}
watch(
Expand Down
6 changes: 3 additions & 3 deletions apps/ui/src/composables/usePropositionPower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ export function usePropositionPower() {
const { web3 } = useWeb3();
const { getCurrent } = useMetaStore();

function latestBlock(network: NetworkID) {
function getLatestBlock(network: NetworkID): number | null {
return supportsNullCurrent(network) ? null : getCurrent(network) ?? 0;
}

function fetch(space: Space) {
votingPowersStore.fetch(
space,
web3.value.account,
latestBlock(space.network)
getLatestBlock(space.network)
);
}

function get(space: Space) {
return votingPowersStore.votingPowers.get(
getIndex(space, latestBlock(space.network))
getIndex(space, getLatestBlock(space.network))
);
}

Expand Down
75 changes: 32 additions & 43 deletions apps/ui/src/composables/useVotingPower.ts
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 };
}
10 changes: 8 additions & 2 deletions apps/ui/src/views/Proposal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const props = defineProps<{
const route = useRoute();
const proposalsStore = useProposalsStore();
const {
votingPower,
get: getVotingPower,
fetch: fetchVotingPower,
reset: resetVotingPower
} = useVotingPower();
Expand Down Expand Up @@ -44,6 +44,12 @@ const discussion = computed(() => {
return sanitizeUrl(proposal.value.discussion);
});
const votingPower = computed(() => {
if (!proposal.value) return;
return getVotingPower(props.space, proposal.value);
});
const votingPowerDecimals = computed(() => {
if (!proposal.value) return 0;
return Math.max(
Expand Down Expand Up @@ -93,7 +99,7 @@ async function handleVoteSubmitted() {
function handleFetchVotingPower() {
if (!proposal.value) return;
fetchVotingPower(proposal.value);
fetchVotingPower(props.space, proposal.value);
}
watch(
Expand Down
4 changes: 3 additions & 1 deletion apps/ui/src/views/Space/Proposals.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const props = defineProps<{ space: Space }>();
const { setTitle } = useTitle();
const {
votingPower,
get: getVotingPower,
fetch: fetchVotingPower,
reset: resetVotingPower
} = useVotingPower();
Expand All @@ -28,6 +28,8 @@ const proposalsRecord = computed(
() => proposalsStore.proposals[`${props.space.network}:${props.space.id}`]
);
const votingPower = computed(() => getVotingPower(props.space));
const spaceLabels = computed(() => {
if (!props.space.labels) return {};
Expand Down

0 comments on commit c4e071a

Please sign in to comment.