Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@

DATABASE_URL="postgresql://postgres:postgres@localhost:5432/Grants?schema=public"
GRANTS_BASE_URL="https://indexer-production.fly.dev/data"
INDEXER_PASSWORD="your-indexer-password"
52 changes: 19 additions & 33 deletions src/graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ export type GraphQLResponse<K> = {

export const gqlRequest = async (
document: any,
variable: any,
variable: any
): Promise<any> => {
try {
return await request(
`https://grants-stack-indexer-v2.gitcoin.co/graphql`,
`https://beta.indexer.gitcoin.co/v1/graphql`,
document,
variable,
{
"content-type": "application/json",
"x-hasura-admin-secret": process.env.INDEXER_PASSWORD || "",
}
);
} catch (error) {
console.log(`Request failed, refreshing in 2 seconds`);
Expand All @@ -25,7 +29,7 @@ export const getRounds = async (chainId: number) => {
return gqlRequest(
gql`
query getRounds($chainId: Int = 10) {
rounds(condition: { chainId: $chainId }) {
rounds(where: { chainId: { _eq: $chainId } }) {
id
chainId
applicationMetadata
Expand Down Expand Up @@ -53,7 +57,7 @@ export const getRounds = async (chainId: number) => {
}
}
`,
{ chainId },
{ chainId }
);
};

Expand All @@ -67,7 +71,7 @@ export const getApplications = async ({
return gqlRequest(
gql`
query getApplications($roundId: String = "", $chainId: Int = 10) {
round(chainId: $chainId, id: $roundId) {
rounds(where: { chainId: { _eq: $chainId }, id: { _eq: $roundId } }) {
applications {
id
projectId
Expand All @@ -85,7 +89,7 @@ export const getApplications = async ({
}
}
`,
{ chainId, roundId },
{ chainId, roundId }
);
};

Expand All @@ -99,7 +103,7 @@ export const getVotes = async ({
return gqlRequest(
gql`
query getVotes($roundId: String = "", $chainId: Int = 10) {
round(chainId: $chainId, id: $roundId) {
rounds(where: { chainId: { _eq: $chainId }, id: { _eq: $roundId } }) {
donations {
amount
amountInRoundMatchToken
Expand All @@ -118,42 +122,24 @@ export const getVotes = async ({
}
}
`,
{ chainId, roundId },
{ chainId, roundId }
);
};

export const getTokenPrice = async ({
chainId,
tokenAddress,
blockNumber,
}: {
chainId: number;
tokenAddress: string;
blockNumber: string;
}) => {
export const getTokenPrice = async ({ tokenCode }: { tokenCode: string }) => {
return gqlRequest(
gql`
query getTokenPrice(
$tokenAddress: String = ""
$chainId: Int = 10
$blockNumber: BigFloat = ""
) {
prices(
query getTokenPrice($tokenCode: String = "") {
priceCache(
orderBy: BLOCK_NUMBER_DESC
filter: {
tokenAddress: { equalTo: $tokenAddress }
chainId: { equalTo: $chainId }
blockNumber: { lessThanOrEqualTo: $blockNumber }
}
first: 1
filter: { tokenCode: { equalTo: $tokenCode } }
limit: 1
) {
id
chainId
priceInUsd
blockNumber
priceUsd
}
}
`,
{ chainId, tokenAddress, blockNumber },
{ tokenCode }
);
};
16 changes: 10 additions & 6 deletions src/loaders/applications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ const manageApplications = async ({ chainId, prisma, roundId }: Props) => {
const applicationResponse = (await getApplications({
chainId: Number(chainId),
roundId: round.roundId.toLowerCase(),
})) as GraphQLResponse<{ applications: any[] }>;
})) as GraphQLResponse<{ applications: any[] }[]>;

const applicationList = applicationResponse?.round?.applications || [];
const applicationList = applicationResponse?.round[0]?.applications || [];

console.log(
`${applicationList.length} application${
applicationList.length !== 1 ? "s" : ""
} found for active application round (${rIndex + 1}/${rounds.length}) : ${round.roundId}`,
} found for active application round (${rIndex + 1}/${rounds.length}) : ${
round.roundId
}`
);

for (const [index, application] of applicationList.entries()) {
Expand Down Expand Up @@ -116,9 +118,11 @@ const manageApplications = async ({ chainId, prisma, roundId }: Props) => {
});

process.stdout.write(
` => Committed ${index + 1} of ${applicationList.length} applications (${
` => Committed ${index + 1} of ${
applicationList.length
} applications (${
Math.round((currentCount / applicationList.length) * 10000) / 100
}%) ${isLast ? "\n" : "\r"}`,
}%) ${isLast ? "\n" : "\r"}`
);
}

Expand All @@ -137,7 +141,7 @@ const manageApplications = async ({ chainId, prisma, roundId }: Props) => {
});

console.log(
`\r\n Round application period has ended, disabling further indexing\r\n`,
`\r\n Round application period has ended, disabling further indexing\r\n`
);
}
}
Expand Down
86 changes: 42 additions & 44 deletions src/loaders/rounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const manageRounds = async ({ chainId, prisma, roundId }: Props) => {

if (roundsData.length === 0) {
console.error(
"This round does not exist. Please make sure to specify the right chainId for the round",
"This round does not exist. Please make sure to specify the right chainId for the round"
);
process.exit(1);
}
Expand All @@ -32,46 +32,44 @@ const manageRounds = async ({ chainId, prisma, roundId }: Props) => {
for (let i = 0; i < roundsData.length; i++) {
const r = roundsData[i];

if (isAddress(r.id)) {
// default program to address zero
const programContractAddress = isAddress(
r.roundMetadata?.programContractAddress,
)
? getAddress(r.roundMetadata?.programContractAddress)
: ethers.constants.AddressZero;

const [createdAt, updatedAt] = await fetchBlockTimestamp({
chainId: Number(chainId),
blockNumbers: [r.createdAtBlock, r.updatedAtBlock],
});

rounds.push({
amountUSD: r.totalAmountDonatedInUsd,
votes: r.totalDonationsCount,
token: r.matchTokenAddress,
matchAmount: r.matchAmount,
matchAmountUSD: r.matchAmountInUsd,
uniqueContributors: r.uniqueDonorsCount,
applicationMetaPtr: r.applicationMetadataCid,
applicationMetadata: r.applicationMetadata,
metaPtr: r.roundMetadataCid,
metadata: r.roundMetadata,
applicationsStartTime: handleDateString(r.applicationsStartTime),
applicationsEndTime: handleDateString(r.applicationsEndTime),
roundStartTime: handleDateString(r.donationsStartTime),
roundEndTime: handleDateString(r.donationsEndTime),
createdAt,
updatedAt,
createdAtBlock: Number(r.createdAtBlock),
updatedAtBlock: Number(r.updatedAtBlock),
chainId: Number(chainId),
roundId: r.id.toLowerCase(),
programContractAddress,
});

if (programContractAddress) {
programs.add(programContractAddress);
}
// default program to address zero
const programContractAddress = isAddress(
r.roundMetadata?.programContractAddress
)
? getAddress(r.roundMetadata?.programContractAddress)
: ethers.constants.AddressZero;

const [createdAt, updatedAt] = await fetchBlockTimestamp({
chainId: Number(chainId),
blockNumbers: [r.createdAtBlock, r.updatedAtBlock],
});

rounds.push({
amountUSD: r.totalAmountDonatedInUsd,
votes: r.totalDonationsCount,
token: r.matchTokenAddress,
matchAmount: r.matchAmount.toString(),
matchAmountUSD: r.matchAmountInUsd,
uniqueContributors: r.uniqueDonorsCount,
applicationMetaPtr: r.applicationMetadataCid,
applicationMetadata: r.applicationMetadata,
metaPtr: r.roundMetadataCid,
metadata: r.roundMetadata,
applicationsStartTime: handleDateString(r.applicationsStartTime),
applicationsEndTime: handleDateString(r.applicationsEndTime),
roundStartTime: handleDateString(r.donationsStartTime),
roundEndTime: handleDateString(r.donationsEndTime),
createdAt,
updatedAt,
createdAtBlock: Number(r.createdAtBlock),
updatedAtBlock: Number(r.updatedAtBlock),
chainId: Number(chainId),
roundId: r.id.toLowerCase(),
programContractAddress,
});

if (programContractAddress) {
programs.add(programContractAddress);
}
}

Expand All @@ -87,9 +85,9 @@ const manageRounds = async ({ chainId, prisma, roundId }: Props) => {
const programsTotal = programsList.length;

console.log(
`${rounds.length} round${roundsTotal !== 1 ? "s" : ""} found across ${programsTotal} program${
programsTotal !== 1 ? "s" : ""
}`,
`${rounds.length} round${
roundsTotal !== 1 ? "s" : ""
} found across ${programsTotal} program${programsTotal !== 1 ? "s" : ""}`
);

await prisma.program.createMany({
Expand Down
10 changes: 6 additions & 4 deletions src/loaders/votes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ const manageVotes = async ({ chainId, prisma, roundId }: Props) => {
const votesResponse = (await getVotes({
chainId: Number(chainId),
roundId: round.roundId.toLowerCase(),
})) as GraphQLResponse<{ donations: any[] }>;
})) as GraphQLResponse<{ donations: any[] }[]>;

const votesList = votesResponse?.round?.donations || [];
const votesList = votesResponse?.round[0]?.donations || [];

// logger(`Committing vote: ${vote.transaction}`)
if (votesList.length > 0) {
process.stdout.write(`\n`);
}
process.stdout.write(
`${votesList.length} votes found for round (${rIndex + 1}/${rounds.length}): ${round.roundId} \n`,
`${votesList.length} votes found for round (${rIndex + 1}/${
rounds.length
}): ${round.roundId} \n`
);

for (const [index, vote] of votesList.entries()) {
Expand Down Expand Up @@ -98,7 +100,7 @@ const manageVotes = async ({ chainId, prisma, roundId }: Props) => {
process.stdout.write(
` => Committed ${currentCount} of ${votesList.length} votes (${
Math.round((currentCount / votesList.length) * 10000) / 100
}%) ${isLast ? "\n" : "\r"}`,
}%) ${isLast ? "\n" : "\r"}`
);
}
}
Expand Down
31 changes: 15 additions & 16 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ export const grantFetch = async (path: string) => {

export const initialFetch = async (chainId: string) => {
try {
const block =
await clients[Number(chainId) as keyof typeof clients].getBlock();
const block = await clients[
Number(chainId) as keyof typeof clients
].getBlock();

console.log(
`Current block on chainId ${chainId} is ${block.number?.toString()}`,
`Current block on chainId ${chainId} is ${block.number?.toString()}`
);
} catch (error) {}
};
Expand Down Expand Up @@ -144,7 +145,7 @@ export const createExtraParams = (next_page_params: NextPageParams) => {
const prefix = acc ? "&" : "";
return `${acc}${prefix}${curr}=${value}`;
},
"",
""
);
};

Expand All @@ -159,8 +160,8 @@ export const fetchBlockTimestamp = async ({
blockNumbers.map((blockNumber) =>
clients[Number(chainId) as keyof typeof clients]
.getBlock({ blockNumber: BigInt(blockNumber) })
.then((block) => block.timestamp),
),
.then((block) => block.timestamp)
)
);
};

Expand Down Expand Up @@ -204,7 +205,7 @@ export const fetchRoundDistributionData = async ({
address: roundId,
abi: roundABI,
functionName: "payoutStrategy",
}),
})
) as `0x${string}`;

const contractConfig = {
Expand Down Expand Up @@ -263,16 +264,14 @@ export const fetchRoundDistributionData = async ({
}

const targetTokenPrice = (await getTokenPrice({
chainId,
tokenAddress: nTokenAddress,
blockNumber: targetBlockNumber.toString(),
})) as { prices: any[] };
tokenCode: token.code,
})) as { priceCache: any[] };

token.price = targetTokenPrice.prices[0].priceInUsd ?? 0;
token.price = targetTokenPrice.priceCache[0].priceUsd ?? 0;
}

const res = await fetch(
`https://d16c97c2np8a2o.cloudfront.net/ipfs/${metaPtr}`,
`https://d16c97c2np8a2o.cloudfront.net/ipfs/${metaPtr}`
);

const distro = await res.json();
Expand All @@ -287,7 +286,7 @@ export const fetchRoundDistributionData = async ({
console.log(error);

console.log(
`Error occurred while fetching round distro data for ${roundId}`,
`Error occurred while fetching round distro data for ${roundId}`
);
}

Expand All @@ -306,7 +305,7 @@ export const formatDistributionPrice = ({
return Number(
formatUnits(
hexToBigInt(amount) * parseUnits(price.toString(), decimal),
decimal + 18,
),
decimal + 18
)
);
};