Skip to content

Commit

Permalink
get project from static round if subgraph is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
yuetloo committed Jul 11, 2024
1 parent 6e94ec6 commit 74658e6
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 40 deletions.
34 changes: 30 additions & 4 deletions vue-app/src/api/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,27 @@ export async function getRecipientRegistryAddress(roundAddress: string | null):
}
}

export async function getProjects(registryAddress: string, startTime?: number, endTime?: number): Promise<Project[]> {
/**
* Get all the projects added between the start and end time
* @returns List of projects
*/
export async function getProjects({
registryAddress,
fundingRoundAddress,
network,
startTime,
endTime,
}: {
registryAddress: string
fundingRoundAddress?: string
network?: string
startTime?: number
endTime?: number
}): Promise<Project[]> {
if (recipientRegistryType === 'simple') {
return await SimpleRegistry.getProjects(registryAddress, startTime, endTime)
} else if (recipientRegistryType === 'optimistic') {
return await OptimisticRegistry.getProjects(registryAddress, startTime, endTime)
return await OptimisticRegistry.getProjects({ registryAddress, fundingRoundAddress, network, startTime, endTime })
} else if (recipientRegistryType === 'kleros') {
return await KlerosRegistry.getProjects(registryAddress, startTime, endTime)
} else {
Expand All @@ -80,11 +96,21 @@ export async function getProjects(registryAddress: string, startTime?: number, e
* @param filter filter result by locked or verified status
* @returns project information
*/
export async function getProject(registryAddress: string, recipientId: string, filter = true): Promise<Project | null> {
export async function getProject({
registryAddress,
fundingRoundAddress,
recipientId,
filter = true,
}: {
registryAddress: string
fundingRoundAddress?: string
recipientId: string
filter: boolean
}): Promise<Project | null> {
if (recipientRegistryType === 'simple') {
return await SimpleRegistry.getProject(registryAddress, recipientId)
} else if (recipientRegistryType === 'optimistic') {
return await OptimisticRegistry.getProject(recipientId, filter)
return await OptimisticRegistry.getProject({ fundingRoundAddress, recipientId, filter })
} else if (recipientRegistryType === 'kleros') {
return await KlerosRegistry.getProject(registryAddress, recipientId)
} else {
Expand Down
57 changes: 48 additions & 9 deletions vue-app/src/api/recipient-registry-optimistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { hasDateElapsed } from '@/utils/dates'
import type { RegistryInfo, RecipientApplicationData } from './types'
import { formToRecipientData } from './recipient'
import { isSameAddress } from '@/utils/accounts'
import { findStaticRound } from './round'
import { findStaticRound, getStaticRoundInfo } from './round'

async function getRegistryInfo(registryAddress: string): Promise<RegistryInfo> {
const registry = new Contract(registryAddress, OptimisticRecipientRegistry, provider)
Expand Down Expand Up @@ -310,14 +310,36 @@ function decodeProject(recipient: Partial<Recipient>): Project {
}
}

export async function getProjects(registryAddress: string, startTime?: number, endTime?: number): Promise<Project[]> {
/**
* Get a list of projects created between the start time and end time
* @param registryAddress The recipient registry address
* @param fundingRoundAddress The funding round address to search in the static rounds
* @returns List of projects
*/
export async function getProjects({
registryAddress,
fundingRoundAddress,
network,
startTime,
endTime,
}: {
registryAddress: string
fundingRoundAddress?: string
network?: string
startTime?: number
endTime?: number
}): Promise<Project[]> {
let data: GetRecipientsQuery
try {
data = await sdk.GetRecipients({
registryAddress: registryAddress.toLowerCase(),
})
} catch {
return []
if (!fundingRoundAddress) {
return []
}
const _round = await getStaticRoundInfo(fundingRoundAddress, network)
return _round?.projects || []
}

if (!data.recipients.length) {
Expand Down Expand Up @@ -383,14 +405,22 @@ export async function getProjects(registryAddress: string, startTime?: number, e
/**
* Find the project from the static round file
* @param projectId The project id
* @param fundingRoundAddress The funding round address
* @param filter Filter the project if it's deleted
*/
async function findStaticProject(projectId: string, filter: boolean): Promise<Project | null> {
async function findStaticProject({
projectId,
fundingRoundAddress,
filter,
}: {
fundingRoundAddress?: string
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)
const roundAddress = fundingRoundAddress ?? (await clrFundContract.getCurrentRound())
const round = await findStaticRound(roundAddress)
if (round?.projects) {
const staticProject = round.projects.find(project => project.id === projectId)
if (staticProject) {
Expand All @@ -412,10 +442,19 @@ async function findStaticProject(projectId: string, filter: boolean): Promise<Pr
* Get project information
*
* @param recipientId recipient id
* @param fundingRoundAddress The funding round address
* @param filter default to always filter result by locked or verified status
* @returns project
*/
export async function getProject(recipientId: string, filter = true): Promise<Project | null> {
export async function getProject({
recipientId,
fundingRoundAddress,
filter = true,
}: {
recipientId: string
fundingRoundAddress?: string
filter: boolean
}): Promise<Project | null> {
if (!isHexString(recipientId, 32)) {
return null
}
Expand All @@ -426,7 +465,7 @@ export async function getProject(recipientId: string, filter = true): Promise<Pr
recipientId,
})
} catch {
return findStaticProject(recipientId, filter)
return findStaticProject({ projectId: recipientId, fundingRoundAddress, filter })
}

if (!data.recipients.length) {
Expand Down
22 changes: 7 additions & 15 deletions vue-app/src/views/Profile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,13 @@ async function loadProjects(): Promise<void> {
if (!recipientRegistryAddress.value) return
const currentRoundAddress = currentRound.value?.fundingRoundAddress
if (currentRoundAddress) {
const round = await getRoundInfo(currentRoundAddress, currentRound.value)
if (round?.projects) {
_projects = round.projects
}
}
if (!_projects) {
_projects = await getProjects(
recipientRegistryAddress.value,
currentRound.value?.startTime.toSeconds(),
currentRound.value?.votingDeadline.toSeconds(),
)
}
_projects = await getProjects({
registryAddress: recipientRegistryAddress.value,
fundingRoundAddress: currentRound.value?.fundingRoundAddress,
network: currentRound.value?.network,
startTime: currentRound.value?.startTime.toSeconds(),
endTime: currentRound.value?.votingDeadline.toSeconds(),
})
const userProjects: Project[] = _projects.filter(
({ address, requester }) =>
Expand Down
8 changes: 7 additions & 1 deletion vue-app/src/views/Project.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,14 @@ onMounted(async () => {
roundAddress.value = (route.params.address as string) || currentRoundAddress || ''
const recipientId = route.params.id as string
const registryAddress = await getRecipientRegistryAddress(roundAddress.value || null)
const _project = await getProject(registryAddress, route.params.id as string)
const _project = await getProject({
registryAddress,
fundingRoundAddress: roundAddress.value || undefined,
recipientId,
filter: false,
})
if (_project === null || _project.isHidden) {
// Project not found
router.push({ name: 'projects' })
Expand Down
19 changes: 10 additions & 9 deletions vue-app/src/views/ProjectList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ async function loadProjectRoundInfo(roundAddress: string) {
let startTime = 0
let votingDeadline = DateTime.local().toSeconds()
let network = ''
let roundProjects: Project[] | undefined = undefined
let fundingRoundAddress = ''
if (roundAddress) {
const round = await getRoundInfo(roundAddress, currentRound.value)
Expand All @@ -146,9 +146,7 @@ async function loadProjectRoundInfo(roundAddress: string) {
startTime = round.startTime.toSeconds()
votingDeadline = round.votingDeadline.toSeconds()
network = round.network || ''
if (round.projects) {
roundProjects = round.projects
}
fundingRoundAddress = round.fundingRoundAddress
}
}
Expand All @@ -157,11 +155,14 @@ async function loadProjectRoundInfo(roundAddress: string) {
recipientRegistryAddress = await getRecipientRegistryAddress(null)
}
if (!roundProjects) {
roundProjects = await getProjects(recipientRegistryAddress, startTime, votingDeadline)
}
const visibleProjects = roundProjects.filter(project => {
const _projects = await getProjects({
registryAddress: recipientRegistryAddress,
fundingRoundAddress,
network,
startTime,
endTime: votingDeadline,
})
const visibleProjects = _projects.filter(project => {
return !project.isHidden && !project.isLocked
})
shuffleArray(visibleProjects)
Expand Down
8 changes: 6 additions & 2 deletions vue-app/src/views/RecipientProfile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ onMounted(async () => {
const recipientRegistryAddress = await getRecipientRegistryAddress(currentRoundAddress)
// retrieve the project information without filtering by the locked or verified status
const filter = false
recipient.value = await getProject(recipientRegistryAddress, recipientId, filter)
recipient.value = await getProject({
registryAddress: recipientRegistryAddress,
fundingRoundAddress: currentRoundAddress || undefined,
recipientId,
filter: false,
})
if (recipient.value?.address) {
ens.value = await ensLookup(recipient.value.address)
}
Expand Down

0 comments on commit 74658e6

Please sign in to comment.