-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from Giveth/add-gitcoin-projects
Add gitcoin projects
- Loading branch information
Showing
11 changed files
with
164 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const GITCOIN_API_URL = | ||
process.env.GITCOIN_API_URL || | ||
"https://grants-stack-indexer-v2.gitcoin.co/graphql"; | ||
|
||
export const IPFS_GATEWAY = process.env.IPFS_GATEWAY || "https://ipfs.io/ipfs"; | ||
|
||
export const GITCOIN_API_LIMIT = 100; | ||
|
||
export const gitcoinSourceConfig: SourceConfig = { | ||
source: "gitcoin", | ||
idField: "id", | ||
titleField: "title", | ||
descriptionField: "description", | ||
slugField: "slug", | ||
imageField: "image", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { GitcoinProjectInfo } from "./type"; | ||
import { updateOrCreateProject } from "../helpers"; | ||
import { IPFS_GATEWAY, gitcoinSourceConfig } from "./constants"; | ||
|
||
const generateGitcoinSlug = (project: GitcoinProjectInfo) => { | ||
const application = project.applications[0]; | ||
|
||
if ( | ||
!application || | ||
!application?.roundId || | ||
!application?.chainId || | ||
!application?.id | ||
) | ||
return ""; | ||
|
||
const { chainId, roundId, id } = application; | ||
return `#/round/${chainId}/${roundId}/${id}`; | ||
}; | ||
|
||
const convertIpfsHashToHttps = (hash: string) => { | ||
return `${IPFS_GATEWAY}/${hash}`; | ||
}; | ||
|
||
export const processProjectsBatch = async ( | ||
projectsBatch: GitcoinProjectInfo[] | ||
) => { | ||
for (const project of projectsBatch) { | ||
if (project.metadata?.type !== "project") continue; | ||
const processedProject = { | ||
id: project.id, | ||
title: project.name || project.metadata?.title, | ||
description: project.metadata?.description, | ||
slug: generateGitcoinSlug(project), | ||
image: project.metadata?.bannerImg | ||
? convertIpfsHashToHttps(project.metadata?.bannerImg) | ||
: "", | ||
}; | ||
await updateOrCreateProject(processedProject, gitcoinSourceConfig); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { GITCOIN_API_LIMIT } from "./constants"; | ||
import { processProjectsBatch } from "./helpers"; | ||
import { fetchGitcoinProjectsBatch } from "./service"; | ||
|
||
export const fetchAndProcessGitcoinProjects = async () => { | ||
try { | ||
let hasMoreProjects = true; | ||
let skip = 0; | ||
const limit = GITCOIN_API_LIMIT; | ||
|
||
while (hasMoreProjects) { | ||
const projectsBatch = await fetchGitcoinProjectsBatch(limit, skip); | ||
if (projectsBatch.length > 0) { | ||
await processProjectsBatch(projectsBatch); | ||
skip += limit; | ||
} else { | ||
hasMoreProjects = false; | ||
} | ||
} | ||
} catch (error: any) { | ||
console.log("error on fetchAndProcessGitcoinProjects", error.message); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { graphQLRequest } from "../../../helpers/request"; | ||
import { GITCOIN_API_URL } from "./constants"; | ||
import { GitcoinProjectInfo } from "./type"; | ||
|
||
export const fetchGitcoinProjectsBatch = async ( | ||
first: number, | ||
offset: number | ||
) => { | ||
try { | ||
const res = await graphQLRequest( | ||
GITCOIN_API_URL, | ||
`query fetchProjects($first: Int = 10, $offset: Int = 10) { | ||
projects( | ||
first: $first | ||
offset: $offset | ||
orderBy: ID_DESC | ||
filter: { | ||
tags: { contains: "allo-v2" } | ||
projectType: { equalTo: CANONICAL } | ||
chainId: { in: [1, 137, 10, 324, 42161, 42220, 43114, 534352, 8453] } | ||
not: { tags: { contains: "program" } } | ||
rounds: { every: { applicationsExist: true } } | ||
} | ||
) { | ||
id | ||
name | ||
applications { | ||
roundId | ||
chainId | ||
id | ||
} | ||
metadata | ||
} | ||
} | ||
`, | ||
{ | ||
first, | ||
offset, | ||
} | ||
); | ||
|
||
return res.data.projects; | ||
} catch (error: any) { | ||
console.log("error on fetchGitcoinProjectsBatch", error.message); | ||
return []; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export type GitcoinProjectInfo = { | ||
id: string; | ||
name: string; | ||
applications: GitcoinApplication[]; | ||
metadata: GitcoinMetadata; | ||
}; | ||
|
||
type GitcoinApplication = { | ||
roundId: string; | ||
chainId: number; | ||
id: string; | ||
}; | ||
|
||
type GitcoinMetadata = { | ||
type: string; | ||
title: string; | ||
logoImg: string; | ||
website: string; | ||
bannerImg: string; | ||
createdAt: number; | ||
credentials: {}; | ||
description: string; | ||
logoImgData: {}; | ||
bannerImgData: {}; | ||
projectTwitter: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters