|
| 1 | +import { parseUnits } from "viem"; |
| 2 | + |
| 3 | +import type { Changeset } from "@grants-stack-indexer/repository"; |
| 4 | +import type { ChainId, ProcessorEvent } from "@grants-stack-indexer/shared"; |
| 5 | +import { getToken } from "@grants-stack-indexer/shared"; |
| 6 | + |
| 7 | +import type { IEventHandler, ProcessorDependencies } from "../../../internal.js"; |
| 8 | +import { getTokenAmountInUsd } from "../../../helpers/index.js"; |
| 9 | +import { RoundMetadataSchema } from "../../../schemas/index.js"; |
| 10 | + |
| 11 | +type Dependencies = Pick< |
| 12 | + ProcessorDependencies, |
| 13 | + "metadataProvider" | "roundRepository" | "pricingProvider" |
| 14 | +>; |
| 15 | + |
| 16 | +/** |
| 17 | + * Handles the PoolMetadataUpdated event for the Allo protocol. |
| 18 | + * |
| 19 | + * This handler performs the following core actions when a pool metadata is updated: |
| 20 | + * - Fetches the round metadata from the metadata provider. |
| 21 | + * - Returns the changeset to update the round with the new metadata. |
| 22 | + */ |
| 23 | +export class PoolMetadataUpdatedHandler implements IEventHandler<"Allo", "PoolMetadataUpdated"> { |
| 24 | + constructor( |
| 25 | + readonly event: ProcessorEvent<"Allo", "PoolMetadataUpdated">, |
| 26 | + private readonly chainId: ChainId, |
| 27 | + private readonly dependencies: Dependencies, |
| 28 | + ) {} |
| 29 | + /* @inheritdoc */ |
| 30 | + async handle(): Promise<Changeset[]> { |
| 31 | + const [_protocol, metadataPointer] = this.event.params.metadata; |
| 32 | + const { metadataProvider, pricingProvider, roundRepository } = this.dependencies; |
| 33 | + |
| 34 | + const metadata = await metadataProvider.getMetadata<{ |
| 35 | + round?: unknown; |
| 36 | + application?: unknown; |
| 37 | + }>(metadataPointer); |
| 38 | + |
| 39 | + const round = await roundRepository.getRoundByIdOrThrow( |
| 40 | + this.chainId, |
| 41 | + this.event.params.poolId.toString(), |
| 42 | + ); |
| 43 | + |
| 44 | + let matchAmount = round.matchAmount; |
| 45 | + let matchAmountInUsd = round.matchAmountInUsd; |
| 46 | + |
| 47 | + const parsedRoundMetadata = RoundMetadataSchema.safeParse(metadata?.round); |
| 48 | + const token = getToken(this.chainId, round.matchTokenAddress); |
| 49 | + |
| 50 | + if (parsedRoundMetadata.success && token) { |
| 51 | + matchAmount = parseUnits( |
| 52 | + parsedRoundMetadata.data.quadraticFundingConfig.matchingFundsAvailable.toString(), |
| 53 | + token.decimals, |
| 54 | + ); |
| 55 | + matchAmountInUsd = ( |
| 56 | + await getTokenAmountInUsd( |
| 57 | + pricingProvider, |
| 58 | + token, |
| 59 | + matchAmount, |
| 60 | + this.event.blockTimestamp, |
| 61 | + ) |
| 62 | + ).amountInUsd; |
| 63 | + } |
| 64 | + |
| 65 | + return [ |
| 66 | + { |
| 67 | + type: "UpdateRound", |
| 68 | + args: { |
| 69 | + chainId: this.chainId, |
| 70 | + roundId: this.event.params.poolId.toString(), |
| 71 | + round: { |
| 72 | + matchAmount, |
| 73 | + matchAmountInUsd, |
| 74 | + applicationMetadataCid: metadataPointer, |
| 75 | + applicationMetadata: metadata?.application ?? {}, |
| 76 | + roundMetadataCid: metadataPointer, |
| 77 | + roundMetadata: metadata?.round ?? {}, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + ]; |
| 82 | + } |
| 83 | +} |
0 commit comments