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 import import problem of attestation #2

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/components/SpaceProposalPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ onMounted(() => setMessageVisibility(props.proposal.flagged));
v-if="proposal?.state === 'active'"
v-model="selectedChoices"
:proposal="proposal"
:space="space"
@open="modalOpen = true"
@clickVote="clickVote"
/>
Expand Down
1 change: 0 additions & 1 deletion src/components/SpaceProposalResults.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const props = defineProps<{
}>();

const emit = defineEmits(['reload']);

const ts = Number((Date.now() / 1e3).toFixed());

const isInvalidScore = computed(
Expand Down
9 changes: 7 additions & 2 deletions src/components/SpaceProposalVote.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
<script setup lang="ts">
import { Proposal, Choice } from '@/helpers/interfaces';
import { Proposal, Choice, ExtendedSpace } from '@/helpers/interfaces';
import voting from '@snapshot-labs/snapshot.js/src/voting';

const props = defineProps<{
space: ExtendedSpace;
proposal: Proposal;
modelValue: Choice;
}>();

const emit = defineEmits(['update:modelValue', 'clickVote']);

const { web3, web3Account } = useWeb3();
const { userVote, loadUserVote } = useProposalVotes(props.proposal);
const { userVote, loadUserVote } = useProposalVotes(
props.proposal,
undefined,
props.space
);

const key = ref(0);

Expand Down
11 changes: 6 additions & 5 deletions src/components/SpaceProposalVotesList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ const { web3Account } = useWeb3();
const {
profiles,
userPrioritizedVotes,
attestations,
loadingVotes,
loadVotes,
loadUserVote
} = useProposalVotes(props.proposal, 6);
} = useProposalVotes(props.proposal, 6, props.space);

const modalVotesmOpen = ref(false);

const voteCount = computed(
() => props.proposal.votes + (attestations.value.length ? 1 : 0)
const voteCount = computed(() =>
props.proposal.type === 'weighted'
? userPrioritizedVotes.value.length
: props.proposal.votes
);

const showModalDownloadMessage = ref(false);
Expand Down Expand Up @@ -53,7 +54,7 @@ watch(web3Account, loadUserVote, { immediate: true });

<template>
<BaseBlock
v-if="proposal.votes > 0"
v-if="voteCount"
:title="$t('votes')"
:counter="voteCount"
:loading="loadingVotes"
Expand Down
2 changes: 1 addition & 1 deletion src/components/SpaceProposalVotesModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const {
loadVotes,
loadMoreVotes,
loadSingleVote
} = useProposalVotes(props.proposal, 20);
} = useProposalVotes(props.proposal, 20, props.space);

const votesEndEl = ref<HTMLElement | null>(null);
const filterOptions = ref<VoteFilters>(clone(VOTES_FILTERS_DEFAULT));
Expand Down
123 changes: 81 additions & 42 deletions src/composables/useProposalVotes.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import {
Attestation,
Attestations,
ExtendedSpace,
Proposal,
Vote,
VoteFilters
} from '@/helpers/interfaces';
import {
ATTESTAIONS_QUERY,
SINGLE_ATTESTAION_QUERY,
VOTES_QUERY
VOTES_QUERY,
VP_QUERY
} from '@/helpers/queries';
import { clone } from '@snapshot-labs/snapshot.js/src/utils';
import { WEIGHTED_VOTING_PROPOSAL_SCHEMA_UID } from '@/helpers/attest';
Expand All @@ -17,8 +19,15 @@ import { getAddress } from '@ethersproject/address';
type QueryParams = {
voter?: string;
} & Partial<VoteFilters>;

export function useProposalVotes(proposal: Proposal, loadBy = 6) {
interface VP {
vp: number;
vp_by_strategy: number[];
}
export function useProposalVotes(
proposal: Proposal,
loadBy = 6,
space: ExtendedSpace
) {
const { profiles, loadProfiles } = useProfiles();
const { apolloQuery } = useApolloQuery();
const { resolveName } = useResolveName();
Expand All @@ -28,7 +37,7 @@ export function useProposalVotes(proposal: Proposal, loadBy = 6) {
const loadingMoreVotes = ref(false);
const votes = ref<Vote[]>([]);
const userVote = ref<Vote | null>(null);
const attestations = ref<Vote[]>([]);
const isWeighted = proposal.type === 'weighted';

const userPrioritizedVotes = computed(() => {
const votesClone = clone(votes.value);
Expand All @@ -41,7 +50,6 @@ export function useProposalVotes(proposal: Proposal, loadBy = 6) {
}
votesClone.unshift(userVote.value);
}

return votesClone;
});

Expand Down Expand Up @@ -107,6 +115,19 @@ export function useProposalVotes(proposal: Proposal, loadBy = 6) {
'attestations'
);
}
async function _fetchVP(queryParams: QueryParams) {
return apolloQuery(
{
query: VP_QUERY,
variables: {
space: space.id,
proposal: proposal.id,
voter: queryParams.voter
}
},
'vp'
);
}

function formatProposalVotes(votes: Vote[]) {
if (!votes?.length) return [];
Expand All @@ -117,15 +138,17 @@ export function useProposalVotes(proposal: Proposal, loadBy = 6) {
});
}

function formatAttestations(attestations: Attestations): Vote[] {
async function formatAttestations(
attestations: Attestations
): Promise<Vote[]> {
if (!attestations?.length) return [];

const VisitedAttester = new Set<string>();

const result: Vote[] = [];

attestations.forEach(attestation => {
if (VisitedAttester.has(attestation.attester)) return;
for (const attestation of attestations) {
if (VisitedAttester.has(attestation.attester)) continue;
VisitedAttester.add(attestation.attester);

const data = JSON.parse(
Expand All @@ -138,38 +161,38 @@ export function useProposalVotes(proposal: Proposal, loadBy = 6) {
choices.forEach((choiceId, index) => {
choice[choiceId] = percentages[index];
});
const { vp, vp_by_strategy } = ((await _fetchVP({
voter: attestation.attester
})) as VP) || { vp: 1, vp_by_strategy: 1 };
result.push({
ipfs: attestation.id,
voter: attestation.attester,
choice: choice,
reason: '',
scores: [1],
balance: 1,
vp: 1,
vp_by_strategy: [1],
scores: vp_by_strategy,
balance: vp,
vp,
vp_by_strategy,
created: attestation.time,
isAttestation: true
});
});
}

return result;
}
async function loadVotes(filter: Partial<VoteFilters> = {}) {
if (loadingVotes.value) return;

loadingVotes.value = true;

try {
const [response, attestationsResponse] = await Promise.all([
_fetchVotes(filter),
_fetchAttestations()
]);

const formattedAttestations = formatAttestations(attestationsResponse);
attestations.value = formattedAttestations;
votes.value = [
...formattedAttestations
// ...formatProposalVotes(response)
];
const response = await (isWeighted
? _fetchAttestations()
: _fetchVotes(filter));

const formattedVotes = isWeighted
? await formatAttestations(response)
: formatProposalVotes(response);
votes.value = formattedVotes;
} catch (e) {
console.log(e);
} finally {
Expand All @@ -184,7 +207,9 @@ export function useProposalVotes(proposal: Proposal, loadBy = 6) {
const voter = response || search;
try {
const attestationsResponse = await _fetchSingleAttestation(voter);
const formattedAttestations = formatAttestations(attestationsResponse);
const formattedAttestations = await formatAttestations(
attestationsResponse
);
return formattedAttestations;
} catch (e) {
console.log(e);
Expand All @@ -193,26 +218,41 @@ export function useProposalVotes(proposal: Proposal, loadBy = 6) {
}
return [];
}
async function loadSingleVote(search: string) {
loadingVotes.value = true;

const response = await resolveName(search);
const voter = response || search;
try {
const response = await _fetchVote({ voter });
votes.value = formatProposalVotes(response);
} catch (e) {
console.log(e);
} finally {
loadingVotes.value = false;
}
}

async function loadMoreVotes(filter: Partial<VoteFilters> = {}) {
// if (loadingMoreVotes.value || loadingVotes.value) return;
//
// loadingMoreVotes.value = true;
// try {
// const response = await _fetchVotes(filter, votes.value.length);
//
// votes.value = votes.value.concat(formatProposalVotes(response));
// } catch (e) {
// console.log(e);
// } finally {
// loadingMoreVotes.value = false;
// }
if (loadingMoreVotes.value || loadingVotes.value) return;

loadingMoreVotes.value = true;
try {
const response = await _fetchVotes(filter, votes.value.length);

votes.value = votes.value.concat(formatProposalVotes(response));
} catch (e) {
console.log(e);
} finally {
loadingMoreVotes.value = false;
}
}

async function loadUserVote(voter: string) {
try {
// const response = await _fetchVote({ voter });
const response = await _loadUserAttestation(voter);
const response = await (isWeighted
? _loadUserAttestation(voter)
: _fetchVote({ voter }));
userVote.value =
response.length > 0 ? formatProposalVotes(response)[0] : null;
} catch (e) {
Expand All @@ -226,7 +266,6 @@ export function useProposalVotes(proposal: Proposal, loadBy = 6) {

return {
votes,
attestations,
userPrioritizedVotes,
profiles,
loadingVotes,
Expand All @@ -235,7 +274,7 @@ export function useProposalVotes(proposal: Proposal, loadBy = 6) {
formatProposalVotes,
loadVotes,
loadMoreVotes,
loadSingleVote: _loadUserAttestation,
loadSingleVote: isWeighted ? _loadUserAttestation : loadSingleVote,
loadUserVote
};
}
Loading