Skip to content

Commit

Permalink
feat: load delegate stats
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Sep 29, 2024
1 parent db8b63a commit 5913bd0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
12 changes: 5 additions & 7 deletions src/components/SpaceDelegatesCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ import { explorerUrl } from '@/helpers/utils';
import {
DelegateWithPercent,
Profile,
ExtendedSpace,
DelegatesVote,
DelegatesProposal
ExtendedSpace
} from '@/helpers/interfaces';
const props = defineProps<{
delegate: DelegateWithPercent;
profiles: Record<string, Profile>;
space: ExtendedSpace;
stats?: {
votes: DelegatesVote[];
proposals: DelegatesProposal[];
votes: number;
proposals: number;
};
about?: string;
}>();
Expand Down Expand Up @@ -134,12 +132,12 @@ function handleDropdownAction(action: string) {

<div class="mt-3 flex gap-[6px]">
<div>
{{ formatCompactNumber(stats?.votes.length || 0) }}
{{ formatCompactNumber(stats?.votes || 0) }}
votes
</div>
·
<div>
{{ formatCompactNumber(stats?.proposals.length || 0) }}
{{ formatCompactNumber(stats?.proposals || 0) }}
proposals
</div>
</div>
Expand Down
37 changes: 27 additions & 10 deletions src/composables/useDelegates.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import { LEADERBOARD_QUERY } from '@/helpers/queries';
import { getInstance } from '@snapshot-labs/lock/plugins/vue3';
import {
DelegateWithPercent,
DelegatesVote,
DelegatesProposal,
ExtendedSpace
} from '@/helpers/interfaces';
import { DelegateWithPercent, ExtendedSpace } from '@/helpers/interfaces';
import {
DelegationTypes,
setupDelegation as getDelegationAdapter
} from '@/helpers/delegationV2';

type DelegatesStats = Record<
string,
{ votes: DelegatesVote[]; proposals: DelegatesProposal[] }
>;
type DelegatesStats = Record<string, { votes: number; proposals: number }>;

const DELEGATES_LIMIT = 18;

export function useDelegates(space: ExtendedSpace) {
const auth = getInstance();
const { resolveName } = useResolveName();
const { apolloQuery } = useApolloQuery();

const { reader, writer } = getDelegationAdapter(space, auth);

Expand Down Expand Up @@ -54,6 +48,7 @@ export function useDelegates(space: ExtendedSpace) {
try {
const response = await fetchDelegateBatch(orderBy);
delegates.value = response;
loadStats(response.map(d => d.id));
hasMoreDelegates.value = response.length === DELEGATES_LIMIT;
} catch (e) {
console.error(e);
Expand All @@ -73,6 +68,7 @@ export function useDelegates(space: ExtendedSpace) {
orderBy,
delegates.value.length
);
loadStats(response.map(d => d.id));
delegates.value = [...delegates.value, ...response];
hasMoreDelegates.value = response.length === DELEGATES_LIMIT;
} catch (e) {
Expand All @@ -93,6 +89,7 @@ export function useDelegates(space: ExtendedSpace) {
const resolvedAddress = await resolveName(addressOrEns);
if (!resolvedAddress) return;
const response = await reader.getDelegate(resolvedAddress);
loadStats([response.id]);
delegate.value = response;
} catch (e) {
console.error(e);
Expand Down Expand Up @@ -140,6 +137,26 @@ export function useDelegates(space: ExtendedSpace) {
}
}

async function loadStats(addresses: string[]) {
const leaderboards = await apolloQuery(
{
query: LEADERBOARD_QUERY,
variables: {
space: space.id,
user_in: addresses
}
},
'leaderboards'
);

leaderboards.forEach(leaderboard => {
delegatesStats.value[leaderboard.user] = {
votes: leaderboard.votesCount,
proposals: leaderboard.proposalsCount
};
});
}

return {
isLoadingDelegate,
isLoadingDelegates,
Expand Down
12 changes: 12 additions & 0 deletions src/helpers/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,15 @@ export const SPACE_QUERY = gql`
}
}
`;

export const LEADERBOARD_QUERY = gql`
query Leaderboard($space: String!, $user_in: [String]) {
leaderboards(where: { space: $space, user_in: $user_in }) {
space
user
proposalsCount
votesCount
lastVote
}
}
`;
7 changes: 4 additions & 3 deletions src/views/SpaceDelegate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useConfirmDialog } from '@vueuse/core';
import { clone } from '@snapshot-labs/snapshot.js/src/utils';
import { DelegatingTo } from '../helpers/delegationV2/types';
import { DelegationTypes } from '@/helpers/delegationV2';
import { getAddress } from '@ethersproject/address';
const INITIAL_STATEMENT = {
about: '',
Expand Down Expand Up @@ -67,7 +68,7 @@ const showUndelegate = computed(() => {
});
const delegateStats = computed(() => {
return delegatesStats.value?.[address.value];
return delegatesStats.value?.[getAddress(address.value)];
});
const delegatorItems = computed(() => {
Expand All @@ -88,12 +89,12 @@ const delegatorItems = computed(() => {
},
{
label: 'Proposals',
value: formatCompactNumber(delegateStats.value?.proposals.length || 0),
value: formatCompactNumber(delegateStats.value?.proposals || 0),
tooltip: null
},
{
label: 'Votes',
value: formatCompactNumber(delegateStats.value?.votes.length || 0),
value: formatCompactNumber(delegateStats.value?.votes || 0),
tooltip: null
}
];
Expand Down

0 comments on commit 5913bd0

Please sign in to comment.