Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: offchain pending proposal voting power should not use latest block #941

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions apps/ui/src/components/Modal/Vote.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ const emit = defineEmits<{
const { vote } = useActions();
const { web3 } = useWeb3();
const {
votingPower,
fetch: fetchVotingPower,
getProposalVp,
fetchProposalVp,
reset: resetVotingPower
} = useVotingPower();
} = useVoteVotingPower();
const proposalsStore = useProposalsStore();
const { loadVotes, votes } = useAccount();
const route = useRoute();
Expand All @@ -55,6 +55,8 @@ const formValidator = getValidator({
}
});
const votingPower = computed(() => getProposalVp(props.proposal));
const formattedVotingPower = computed(() =>
getFormattedVotingPower(votingPower.value)
);
Expand Down Expand Up @@ -124,7 +126,7 @@ async function handleConfirmed(tx?: string | null) {
}
function handleFetchVotingPower() {
fetchVotingPower(props.proposal);
fetchProposalVp(props.proposal);
}
watch(
Expand Down
67 changes: 67 additions & 0 deletions apps/ui/src/composables/useVoteVotingPower.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { offchainNetworks, supportsNullCurrent } from '@/networks';
import { getIndex } from '@/stores/votingPowers';
import { NetworkID, Proposal, Space } from '@/types';

export function useVoteVotingPower() {
const votingPowersStore = useVotingPowersStore();
const { web3 } = useWeb3();
const { getCurrent } = useMetaStore();

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

function proposalSnapshot(proposal: Proposal) {
return (
(proposal.state === 'pending' &&
!offchainNetworks.includes(proposal.network)
? null
: proposal.snapshot) || latestBlock(proposal.network)
);
}

function fetchProposalVp(proposal: Proposal) {
if (!web3.value.account) return;

votingPowersStore.fetch(
proposal,
web3.value.account,
proposalSnapshot(proposal)
);
}

function fetchSpaceVp(space: Space) {
if (!web3.value.account) return;

votingPowersStore.fetch(
space,
web3.value.account,
latestBlock(space.network)
);
}

function getProposalVp(proposal: Proposal) {
return votingPowersStore.votingPowers.get(
getIndex(proposal.space, proposalSnapshot(proposal))
);
}

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

function reset() {
votingPowersStore.reset();
}

watch(
() => web3.value.account,
account => {
if (!account) reset();
}
);

return { getProposalVp, getSpaceVp, fetchProposalVp, fetchSpaceVp, reset };
}
14 changes: 10 additions & 4 deletions apps/ui/src/views/Proposal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ const props = defineProps<{
const route = useRoute();
const proposalsStore = useProposalsStore();
const {
votingPower,
fetch: fetchVotingPower,
getProposalVp,
fetchProposalVp,
reset: resetVotingPower
} = useVotingPower();
} = useVoteVotingPower();
const { setTitle } = useTitle();
const { web3 } = useWeb3();
const { modalAccountOpen } = useModal();
Expand Down Expand Up @@ -42,6 +42,12 @@ const discussion = computed(() => {
return sanitizeUrl(proposal.value.discussion);
});

const votingPower = computed(() => {
if (!proposal.value) return;

return getProposalVp(proposal.value);
});

const votingPowerDecimals = computed(() => {
if (!proposal.value) return 0;
return Math.max(
Expand Down Expand Up @@ -87,7 +93,7 @@ async function handleVoteSubmitted() {
function handleFetchVotingPower() {
if (!proposal.value) return;

fetchVotingPower(proposal.value);
fetchProposalVp(proposal.value);
}

watch(
Expand Down
10 changes: 6 additions & 4 deletions apps/ui/src/views/Space/Proposals.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const props = defineProps<{ space: Space }>();

const { setTitle } = useTitle();
const {
votingPower,
fetch: fetchVotingPower,
getSpaceVp,
fetchSpaceVp,
reset: resetVotingPower
} = useVotingPower();
} = useVoteVotingPower();
const { web3 } = useWeb3();
const router = useRouter();
const route = useRoute();
Expand All @@ -28,6 +28,8 @@ const proposalsRecord = computed(
() => proposalsStore.proposals[`${props.space.network}:${props.space.id}`]
);

const votingPower = computed(() => getSpaceVp(props.space));

const spaceLabels = computed(() => {
if (!props.space.labels) return {};

Expand All @@ -46,7 +48,7 @@ async function handleEndReached() {
}

function handleFetchVotingPower() {
fetchVotingPower(props.space);
fetchSpaceVp(props.space);
}

watchThrottled(
Expand Down