Skip to content

Commit

Permalink
Merge branch 'master' into fix-wrong-address-case-when-resolved-from-ens
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Sep 30, 2024
2 parents 0d09459 + 9e2e636 commit c4c82ed
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 29 deletions.
2 changes: 1 addition & 1 deletion snapshot-spaces
16 changes: 13 additions & 3 deletions src/components/AvatarUser.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<script setup lang="ts">
import { getAddress } from '@ethersproject/address';
const props = withDefaults(
defineProps<{
address: string;
Expand All @@ -13,15 +15,23 @@ const props = withDefaults(
const { profilesCreated } = useProfiles();
const normalizedAddress = computed(() => getAddress(props.address));
const timestamp = computed(() => {
if (!props?.address || !profilesCreated.value?.[props.address]) return '';
return `&ts=${profilesCreated.value[props.address]}`;
if (
!normalizedAddress.value ||
!profilesCreated.value?.[normalizedAddress.value]
) {
return '';
}
return `&ts=${profilesCreated.value[normalizedAddress.value]}`;
});
</script>

<template>
<BaseAvatar
:src="`https://cdn.stamp.fyi/avatar/eth:${address}?s=${
:src="`https://cdn.stamp.fyi/avatar/eth:${normalizedAddress}?s=${
Number(size) * 2
}${timestamp}`"
:preview-file="previewFile"
Expand Down
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,26 +1,20 @@
import { LEADERBOARD_QUERY } from '@/helpers/queries';
import { getInstance } from '@snapshot-labs/lock/plugins/vue3';
import { getAddress } from '@ethersproject/address';
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 @@ -55,6 +49,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 @@ -74,6 +69,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 @@ -94,6 +90,7 @@ export function useDelegates(space: ExtendedSpace) {
const resolvedAddress = await resolveName(addressOrEns);
if (!resolvedAddress) return;
const response = await reader.getDelegate(getAddress(resolvedAddress));
loadStats([response.id]);
delegate.value = response;
} catch (e) {
console.error(e);
Expand Down Expand Up @@ -141,6 +138,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
4 changes: 1 addition & 3 deletions src/composables/useStrategies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ export function useStrategies() {
});

const filterStrategies = (q = '') =>
strategies.value
.filter(s => s.id.toLowerCase().includes(q.toLowerCase()))
.sort((a, b) => b.spacesCount - a.spacesCount);
strategies.value.filter(s => s.id.toLowerCase().includes(q.toLowerCase()));

const { apolloQuery } = useApolloQuery();

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
4 changes: 2 additions & 2 deletions src/views/SpaceDelegates.vue
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ useInfiniteScroll(
{ distance: 500 }
);
watch(searchInputDebounced, () => {
loadDelegate(searchInput.value);
watch(searchInputDebounced, async () => {
await loadDelegate(searchInput.value);
});
watch(matchFilter, () => {
Expand Down

0 comments on commit c4c82ed

Please sign in to comment.