Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ERF Distribution Schema #701

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions src/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ export const MatchingDistributionSchema = z.object({
usdPriceTimestampAt: z.date().optional(),
});

export const ERFDistributionSchema = z.object({
matchingDistribution: z.array(
z.object({
applicationId: z.string(),
projectPayoutAddress: z.string(),
projectId: z.string(),
projectName: z.string(),
matchPoolPercentage: z.coerce.number(),
})
),
blockNumber: z.number().optional(),
blockTimestamp: z.date().optional(),
});

export type RoundTable = {
id: Address | string;
chainId: ChainId;
Expand Down
87 changes: 65 additions & 22 deletions src/indexer/allo/v2/handleEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Changeset } from "../../../database/index.js";
import {
ApplicationTable,
Donation,
ERFDistributionSchema,
MatchingDistributionSchema,
NewApplication,
NewRound,
Expand Down Expand Up @@ -274,22 +275,28 @@ export async function handleEvent(

case "PoolCreated": {
const { pointer: metadataPointer } = event.params.metadata;

const { roundMetadata, applicationMetadata } = await fetchPoolMetadata(
ipfsGet,
metadataPointer
);

const parsedMetadata = RoundMetadataSchema.safeParse(roundMetadata);

const poolId = event.params.poolId;

const { managerRole, adminRole } = generateRoundRoles(poolId);

const strategyAddress = event.params.strategy;

const strategyId = await readContract({
contract: "AlloV2/IStrategy/V1",
address: strategyAddress,
functionName: "getStrategyId",
});

const strategy = extractStrategyFromId(strategyId);

let matchAmount = 0n;
let matchAmountInUsd = 0;

Expand Down Expand Up @@ -353,6 +360,7 @@ export async function handleEvent(
) {
const contract =
"AlloV2/DonationVotingMerkleDistributionDirectTransferStrategy/V1";

const [
registrationStartTimeResolved,
registrationEndTimeResolved,
Expand Down Expand Up @@ -380,6 +388,7 @@ export async function handleEvent(
functionName: "allocationEndTime",
}),
]);

applicationsStartTime = getDateFromTimestamp(
registrationStartTimeResolved
);
Expand All @@ -392,6 +401,7 @@ export async function handleEvent(
parsedMetadata.data.quadraticFundingConfig.matchingFundsAvailable.toString(),
token.decimals
);

matchAmountInUsd = (
await convertToUSD(
priceProvider,
Expand All @@ -407,11 +417,11 @@ export async function handleEvent(
(strategy.name === "allov2.DirectGrantsSimpleStrategy" ||
strategy.name === "allov2.DirectGrantsLiteStrategy")
) {
// const contract = "AlloV2/DirectGrantsSimpleStrategy/V1";
const contract =
strategy.name === "allov2.DirectGrantsSimpleStrategy"
? "AlloV2/DirectGrantsSimpleStrategy/V1"
: "AlloV2/DirectGrantsLiteStrategy/V1";

const [registrationStartTimeResolved, registrationEndTimeResolved] =
await Promise.all([
await readContract({
Expand All @@ -425,6 +435,7 @@ export async function handleEvent(
functionName: "registrationEndTime",
}),
]);

applicationsStartTime = getDateFromTimestamp(
registrationStartTimeResolved
);
Expand All @@ -434,6 +445,7 @@ export async function handleEvent(
strategy.name === "allov2.EasyRetroFundingStrategy"
) {
const contract = "AlloV2/EasyRetroFundingStrategy/V1";

const [
registrationStartTimeResolved,
registrationEndTimeResolved,
Expand Down Expand Up @@ -597,7 +609,6 @@ export async function handleEvent(
},
});
}

changes.push({
type: "DeletePendingRoundRoles",
ids: pendingManagerRoundRoles.map((r) => r.id!),
Expand Down Expand Up @@ -1088,31 +1099,63 @@ export async function handleEvent(
event.params.metadata.pointer
)) as Record<string, unknown>;

const usdAmount = await convertToUSD(
priceProvider,
chainId,
round?.matchTokenAddress,
BigInt(1),
event.blockNumber
);
const blockNumber = Number(event.blockNumber);
const blockTimestamp =
getDateFromTimestamp(
BigInt((await blockTimestampInMs(chainId, event.blockNumber)) / 1000)
) || undefined;
let distribution;
switch (round.strategyName) {
case "allov2.EasyRetroFundingStrategy": {
rawDistribution["blockNumber"] = blockNumber;
rawDistribution["blockTimestamp"] = blockTimestamp;
const parsedDistribution =
ERFDistributionSchema.safeParse(rawDistribution);

const enhancedMatchingDistribution = parsedDistribution.success
? parsedDistribution.data?.matchingDistribution.map((entry) => ({
...entry,
contributionsCount: 0,
originalMatchAmountInToken: "0",
matchAmountInToken: "0",
}))
: [];

distribution = {
data: {
matchingDistribution: enhancedMatchingDistribution,
blockNumber: blockNumber,
blockTimestamp: blockTimestamp,
usdPrice: 0,
usdPriceTimestampAt: undefined,
},
success: parsedDistribution.success,
};
break;
}
default: {
const usdAmount = await convertToUSD(
priceProvider,
chainId,
round?.matchTokenAddress,
BigInt(1),
event.blockNumber
);

const blockTimestamp = getDateFromTimestamp(
BigInt((await blockTimestampInMs(chainId, event.blockNumber)) / 1000)
);
rawDistribution["blockNumber"] = Number(event.blockNumber);
if (blockTimestamp) {
rawDistribution["blockTimestamp"] = blockTimestamp;
}
rawDistribution["usdPrice"] = usdAmount.price;
rawDistribution["usdPriceTimestampAt"] = usdAmount.timestamp;
rawDistribution["blockNumber"] = blockNumber;
rawDistribution["blockTimestamp"] = blockTimestamp;
rawDistribution["usdPrice"] = usdAmount.price;
rawDistribution["usdPriceTimestampAt"] = usdAmount.timestamp;

const distribution =
MatchingDistributionSchema.safeParse(rawDistribution);
distribution = MatchingDistributionSchema.safeParse(rawDistribution);
break;
}
}

if (!distribution.success) {
if (!distribution || !distribution.success) {
logger.warn({
msg: "Failed to parse distribution",
error: distribution.error,
error: distribution?.error,
event,
rawDistribution,
});
Expand Down