Skip to content

Commit

Permalink
refactor static round recipient finding logic
Browse files Browse the repository at this point in the history
  • Loading branch information
yuetloo committed Jul 10, 2024
1 parent 0ddf280 commit 6d0ae77
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 64 deletions.
31 changes: 0 additions & 31 deletions vue-app/src/api/leaderboard.ts

This file was deleted.

22 changes: 3 additions & 19 deletions vue-app/src/api/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -205,7 +205,7 @@ export async function getLeaderboardProject(
projectId: string,
network: string,
): Promise<Project | null> {
const data = await getLeaderboardData(roundAddress, network)
const data = await findStaticRound(roundAddress, network)
if (!data) {
return null
}
Expand Down Expand Up @@ -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,
Expand All @@ -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<Project[]> {
const data = await getLeaderboardData(roundAddress, network)
if (!data) {
return []
}

const projects = data.projects.map(staticDataToProjectInterface)
return projects
}
54 changes: 49 additions & 5 deletions vue-app/src/api/recipient-registry-optimistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RegistryInfo> {
const registry = new Contract(registryAddress, OptimisticRecipientRegistry, provider)
Expand Down Expand Up @@ -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
Expand All @@ -141,7 +157,7 @@ async function tryGetRecipientsStatically(registryAddress: string): Promise<Requ

try {
const fundingRoundAddress = await clrFundContract.getCurrentRound()
const fundingRoundInfo = await getLeaderboardData(fundingRoundAddress)
const fundingRoundInfo = await findStaticRound(fundingRoundAddress)
if (isSameAddress(fundingRoundInfo?.round?.recipientRegistryAddress, registryAddress)) {
if (fundingRoundInfo?.projects) {
requests = fundingRoundInfo.projects.map(project => {
Expand All @@ -156,7 +172,7 @@ async function tryGetRecipientsStatically(registryAddress: string): Promise<Requ
return {
transactionHash: '', // transaction hash not available in the static data
type: RequestType.Registration,
status: project.state as RequestStatus,
status: staticStateToRequestStatus(project.state),
acceptanceDate: DateTime.fromISO(project.createdAt),
recipientId: project.id,
recipient: project.recipientAddress,
Expand Down Expand Up @@ -370,6 +386,34 @@ export async function getProjects(registryAddress: string, startTime?: number, e
return projects
}

/**
* Find the project from the static round file
* @param projectId The project id
* @param filter Filter the project if it's deleted
*/
async function findStaticProject(projectId: string, filter: boolean): Promise<Project | null> {
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
*
Expand All @@ -388,7 +432,7 @@ export async function getProject(recipientId: string, filter = true): Promise<Pr
recipientId,
})
} catch {
return null
return findStaticProject(recipientId, filter)
}

if (!data.recipients.length) {
Expand Down
40 changes: 38 additions & 2 deletions vue-app/src/api/round.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import sdk from '@/graphql/sdk'

import { isSameAddress } from '@/utils/accounts'
import { Keypair } from '@clrfund/common'
import { getLeaderboardData } from '@/api/leaderboard'
import { staticDataToProjectInterface } from './projects'
import staticRounds from '@/rounds/rounds.json'

type StaticRoundRecord = {
address: string
network: string
}

export interface RoundInfo {
fundingRoundAddress: string
Expand Down Expand Up @@ -57,6 +62,37 @@ export enum RoundStatus {
Finalized = 'Finalized',
Cancelled = 'Cancelled',
}

function isSameNetwork(network1 = '', network2 = ''): boolean {
return network1.toLowerCase() === network2.toLowerCase()
}

/**
* Find the funding round address from the static round index file
* @param roundAddress The funding round address
* @param network The network name
* @returns The static round data
*/
export async function findStaticRound(roundAddress: string, network?: string) {
const rounds = staticRounds as StaticRoundRecord[]
const checkNetwork = Boolean(network)

const found = rounds.find((r: StaticRoundRecord) => {
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<string | null> {
const fundingRoundAddress = await clrFundContract.getCurrentRound()
Expand Down Expand Up @@ -116,7 +152,7 @@ export function toRoundInfo(data: any): RoundInfo {
}

export async function getStaticRoundInfo(fundingRoundAddress: string, network?: string): Promise<RoundInfo | null> {
const data = await getLeaderboardData(fundingRoundAddress, network)
const data = await findStaticRound(fundingRoundAddress, network)
if (!data) {
return null
}
Expand Down
5 changes: 2 additions & 3 deletions vue-app/src/views/Leaderboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion vue-app/src/views/Profile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 3 additions & 3 deletions vue-app/src/views/RecipientProfile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
</div>
<div class="project-section">
<h2>{{ $t('projectProfile.h2_1') }}</h2>
<markdown :raw="recipient.description" />
<markdown v-if="recipient.description" :raw="recipient.description" />
</div>
<div v-if="recipient.problemSpace" class="project-section">
<h2>{{ $t('projectProfile.h2_2') }}</h2>
<markdown :raw="recipient.problemSpace" />
<markdown v-if="recipient.problemSpace" :raw="recipient.problemSpace" />
</div>
<div v-if="recipient.plans" class="project-section">
<h2>{{ $t('projectProfile.h2_3') }}</h2>
<markdown :raw="recipient.plans" />
<markdown v-if="recipient.plans" :raw="recipient.plans" />
</div>
<div
:class="{
Expand Down

0 comments on commit 6d0ae77

Please sign in to comment.