From 6d0ae77d3c2b11bbabc2c05f447576164c9eedc1 Mon Sep 17 00:00:00 2001 From: yuetloo Date: Wed, 10 Jul 2024 19:31:32 -0400 Subject: [PATCH] refactor static round recipient finding logic --- vue-app/src/api/leaderboard.ts | 31 ----------- vue-app/src/api/projects.ts | 22 ++------ .../src/api/recipient-registry-optimistic.ts | 54 +++++++++++++++++-- vue-app/src/api/round.ts | 40 +++++++++++++- vue-app/src/views/Leaderboard.vue | 5 +- vue-app/src/views/Profile.vue | 2 +- vue-app/src/views/RecipientProfile.vue | 6 +-- 7 files changed, 96 insertions(+), 64 deletions(-) delete mode 100644 vue-app/src/api/leaderboard.ts diff --git a/vue-app/src/api/leaderboard.ts b/vue-app/src/api/leaderboard.ts deleted file mode 100644 index 1678029dd..000000000 --- a/vue-app/src/api/leaderboard.ts +++ /dev/null @@ -1,31 +0,0 @@ -import leaderboardRounds from '@/rounds/rounds.json' -import { isSameAddress } from '@/utils/accounts' - -type LeaderboardRecord = { - address: string - network: string -} - -function isSameNetwork(network1 = '', network2 = ''): boolean { - return network1.toLowerCase() === network2.toLowerCase() -} - -export async function getLeaderboardData(roundAddress: string, network?: string) { - const rounds = leaderboardRounds as LeaderboardRecord[] - const checkNetwork = Boolean(network) - - const found = rounds.find((r: LeaderboardRecord) => { - return isSameAddress(r.address, roundAddress) && (!checkNetwork || isSameNetwork(network, r.network)) - }) - - if (!found) { - return null - } - - const data = await import(`../rounds/${found.network}/${found.address}.json`) - if (!data.round) { - data.round = {} - } - data.round.network = found.network - return data -} diff --git a/vue-app/src/api/projects.ts b/vue-app/src/api/projects.ts index db3144d21..22b4a3089 100644 --- a/vue-app/src/api/projects.ts +++ b/vue-app/src/api/projects.ts @@ -7,7 +7,7 @@ import SimpleRegistry from './recipient-registry-simple' import OptimisticRegistry from './recipient-registry-optimistic' import KlerosRegistry from './recipient-registry-kleros' import sdk from '@/graphql/sdk' -import { getLeaderboardData } from '@/api/leaderboard' +import { findStaticRound } from '@/api/round' import type { RecipientApplicationData } from '@/api/types' import type { GetRecipientByIndexQuery } from '@/graphql/API' @@ -205,7 +205,7 @@ export async function getLeaderboardProject( projectId: string, network: string, ): Promise { - const data = await getLeaderboardData(roundAddress, network) + const data = await findStaticRound(roundAddress, network) if (!data) { return null } @@ -277,7 +277,7 @@ export function staticDataToProjectInterface(project: any): Project { return { id: project.id, address: project.recipientAddress, - name: project.metadata.name, + name: project.metadata.name || project.name, tagline: project.metadata.tagline, description: project.metadata.description, category: project.metadata.category, @@ -298,19 +298,3 @@ export function staticDataToProjectInterface(project: any): Project { isLocked: false, } } - -/** - * Get the list of projects for a static round - * @param roundAddress The funding round contract address - * @param network The network - * @returns Array of projects - */ -export async function getProjectsForStaticRound(roundAddress: string, network: string): Promise { - const data = await getLeaderboardData(roundAddress, network) - if (!data) { - return [] - } - - const projects = data.projects.map(staticDataToProjectInterface) - return projects -} diff --git a/vue-app/src/api/recipient-registry-optimistic.ts b/vue-app/src/api/recipient-registry-optimistic.ts index d09f1dafb..9963d9f77 100644 --- a/vue-app/src/api/recipient-registry-optimistic.ts +++ b/vue-app/src/api/recipient-registry-optimistic.ts @@ -5,14 +5,14 @@ import { chain, clrFundContract } from '@/api/core' import { OptimisticRecipientRegistry } from './abi' import { provider, ipfsGatewayUrl } from './core' -import type { Project } from './projects' +import { staticDataToProjectInterface, type Project } from './projects' import sdk from '@/graphql/sdk' import type { GetProjectQuery, GetRecipientsQuery, Recipient } from '@/graphql/API' import { hasDateElapsed } from '@/utils/dates' import type { RegistryInfo, RecipientApplicationData } from './types' import { formToRecipientData } from './recipient' import { isSameAddress } from '@/utils/accounts' -import { getLeaderboardData } from './leaderboard' +import { findStaticRound } from './round' async function getRegistryInfo(registryAddress: string): Promise { const registry = new Contract(registryAddress, OptimisticRecipientRegistry, provider) @@ -131,6 +131,22 @@ function mapRequestStatus(request: RecipientRequestData): RequestStatus { return status } +/** + * Map the recipient state from static round data to request status + * @param state Recipient state: Active, Rejected, Removed + * @returns Request status + */ +function staticStateToRequestStatus(state: string): RequestStatus { + switch (state) { + case 'Accepted': + return RequestStatus.Executed + case 'Rejected': + return RequestStatus.Rejected + default: + return RequestStatus.Removed + } +} + /** * Try to get the recipients from the static round data * @param registryAddress The recipient registry address @@ -141,7 +157,7 @@ async function tryGetRecipientsStatically(registryAddress: string): Promise { @@ -156,7 +172,7 @@ async function tryGetRecipientsStatically(registryAddress: string): Promise { + let project: Project | null = null + try { + const fundingRoundAddress = await clrFundContract.getCurrentRound() + const network = chain.label.toLowerCase() + const round = await findStaticRound(fundingRoundAddress, network) + if (round?.projects) { + const staticProject = round.projects.find(project => project.id === projectId) + if (staticProject) { + project = staticDataToProjectInterface(staticProject) + if (filter && project.isHidden) { + project = null + } + } + } + } catch { + // return not found on error + return null + } + + return project +} + /** * Get project information * @@ -388,7 +432,7 @@ export async function getProject(recipientId: string, filter = true): Promise { + return isSameAddress(r.address, roundAddress) && (!checkNetwork || isSameNetwork(network, r.network)) + }) + + if (!found) { + return null + } + + const data = await import(`../rounds/${found.network}/${found.address}.json`) + if (!data.round) { + data.round = {} + } + data.round.network = found.network + return data +} + //TODO: update to take ClrFund address as a parameter, default to env. variable export async function getCurrentRound(): Promise { const fundingRoundAddress = await clrFundContract.getCurrentRound() @@ -116,7 +152,7 @@ export function toRoundInfo(data: any): RoundInfo { } export async function getStaticRoundInfo(fundingRoundAddress: string, network?: string): Promise { - const data = await getLeaderboardData(fundingRoundAddress, network) + const data = await findStaticRound(fundingRoundAddress, network) if (!data) { return null } diff --git a/vue-app/src/views/Leaderboard.vue b/vue-app/src/views/Leaderboard.vue index 03db7c637..c7d338ffb 100644 --- a/vue-app/src/views/Leaderboard.vue +++ b/vue-app/src/views/Leaderboard.vue @@ -46,10 +46,9 @@ import { useAppStore } from '@/stores' import { useRouter, useRoute } from 'vue-router' import type { RoundInfo } from '@/api/round' -import { toRoundInfo } from '@/api/round' +import { toRoundInfo, findStaticRound } from '@/api/round' import type { LeaderboardProject } from '@/api/projects' import { toLeaderboardProject } from '@/api/projects' -import { getLeaderboardData } from '@/api/leaderboard' import { getRouteParamValue } from '@/utils/route' const router = useRouter() @@ -63,7 +62,7 @@ const appStore = useAppStore() const { showSimpleLeaderboard } = storeToRefs(appStore) async function loadLeaderboard(address: string, network: string) { - const data = await getLeaderboardData(address, network) + const data = await findStaticRound(address, network) return data } diff --git a/vue-app/src/views/Profile.vue b/vue-app/src/views/Profile.vue index 71a74ee7e..b004e544d 100644 --- a/vue-app/src/views/Profile.vue +++ b/vue-app/src/views/Profile.vue @@ -104,7 +104,7 @@ import Loader from '@/components/Loader.vue' import FundsNeededWarning from '@/components/FundsNeededWarning.vue' import { userRegistryType, UserRegistryType, chain } from '@/api/core' -import { type Project, getProjects, getProjectsForStaticRound } from '@/api/projects' +import { type Project, getProjects } from '@/api/projects' import { isSameAddress } from '@/utils/accounts' import { getTokenLogo } from '@/utils/tokens' import { useAppStore, useUserStore, useRecipientStore, useWalletStore } from '@/stores' diff --git a/vue-app/src/views/RecipientProfile.vue b/vue-app/src/views/RecipientProfile.vue index cb2c548eb..3b0245067 100644 --- a/vue-app/src/views/RecipientProfile.vue +++ b/vue-app/src/views/RecipientProfile.vue @@ -22,15 +22,15 @@

{{ $t('projectProfile.h2_1') }}

- +

{{ $t('projectProfile.h2_2') }}

- +

{{ $t('projectProfile.h2_3') }}

- +