Skip to content

Commit

Permalink
add ERF Distribution Schema
Browse files Browse the repository at this point in the history
remove logging

remove debugging things

format
  • Loading branch information
0xKurt committed Jan 30, 2025
1 parent 278b488 commit 4c2fb60
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 22 deletions.
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

0 comments on commit 4c2fb60

Please sign in to comment.