generated from defi-wonderland/ts-turborepo-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: all event handlers for allo contract (#39)
# 🤖 Linear Closes GIT-137 GIT-149 GIT-150 GIT-151 GIT-152 ## Description Added all the remaining handlers for Allo contract events. ## Checklist before requesting a review - [ ] I have conducted a self-review of my code. - [ ] I have conducted a QA. - [ ] If it is a core feature, I have included comprehensive tests.
- Loading branch information
Showing
24 changed files
with
1,231 additions
and
132 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
export * from "./poolCreated.handler.js"; | ||
export * from "./poolFunded.handler.js"; | ||
export * from "./poolMetadataUpdated.handler.js"; | ||
export * from "./roleGranted.handler.js"; | ||
export * from "./roleRevoked.handler.js"; |
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
55 changes: 55 additions & 0 deletions
55
packages/processors/src/processors/allo/handlers/poolFunded.handler.ts
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,55 @@ | ||
import type { Changeset } from "@grants-stack-indexer/repository"; | ||
import type { ChainId, ProcessorEvent } from "@grants-stack-indexer/shared"; | ||
import { getToken, UnknownToken } from "@grants-stack-indexer/shared"; | ||
|
||
import type { IEventHandler, ProcessorDependencies } from "../../../internal.js"; | ||
import { getTokenAmountInUsd } from "../../../helpers/index.js"; | ||
|
||
type Dependencies = Pick<ProcessorDependencies, "roundRepository" | "logger" | "pricingProvider">; | ||
|
||
/** | ||
* Handles the PoolFunded event for the Allo protocol. | ||
* | ||
* This handler performs the following core actions when a pool is funded: | ||
* - Fetches the round metadata from the metadata provider. | ||
* - Returns the changeset to update the round with the new metadata. | ||
*/ | ||
export class PoolFundedHandler implements IEventHandler<"Allo", "PoolFunded"> { | ||
constructor( | ||
readonly event: ProcessorEvent<"Allo", "PoolFunded">, | ||
private readonly chainId: ChainId, | ||
private readonly dependencies: Dependencies, | ||
) {} | ||
/* @inheritdoc */ | ||
async handle(): Promise<Changeset[]> { | ||
const poolId = this.event.params.poolId.toString(); | ||
const fundedAmount = BigInt(this.event.params.amount); | ||
const { roundRepository, pricingProvider } = this.dependencies; | ||
|
||
const round = await roundRepository.getRoundByIdOrThrow(this.chainId, poolId); | ||
|
||
const token = getToken(this.chainId, round.matchTokenAddress); | ||
|
||
//TODO: Review this on Advace Recovery Milestone | ||
if (!token) throw new UnknownToken(round.matchTokenAddress, this.chainId); | ||
|
||
const { amountInUsd } = await getTokenAmountInUsd( | ||
pricingProvider, | ||
token, | ||
fundedAmount, | ||
this.event.blockTimestamp, | ||
); | ||
|
||
return [ | ||
{ | ||
type: "IncrementRoundFundedAmount", | ||
args: { | ||
chainId: this.chainId, | ||
roundId: round.id, | ||
fundedAmount, | ||
fundedAmountInUsd: amountInUsd, | ||
}, | ||
}, | ||
]; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
packages/processors/src/processors/allo/handlers/poolMetadataUpdated.handler.ts
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,83 @@ | ||
import { parseUnits } from "viem"; | ||
|
||
import type { Changeset } from "@grants-stack-indexer/repository"; | ||
import type { ChainId, ProcessorEvent } from "@grants-stack-indexer/shared"; | ||
import { getToken } from "@grants-stack-indexer/shared"; | ||
|
||
import type { IEventHandler, ProcessorDependencies } from "../../../internal.js"; | ||
import { getTokenAmountInUsd } from "../../../helpers/index.js"; | ||
import { RoundMetadataSchema } from "../../../schemas/index.js"; | ||
|
||
type Dependencies = Pick< | ||
ProcessorDependencies, | ||
"metadataProvider" | "roundRepository" | "pricingProvider" | ||
>; | ||
|
||
/** | ||
* Handles the PoolMetadataUpdated event for the Allo protocol. | ||
* | ||
* This handler performs the following core actions when a pool metadata is updated: | ||
* - Fetches the round metadata from the metadata provider. | ||
* - Returns the changeset to update the round with the new metadata. | ||
*/ | ||
export class PoolMetadataUpdatedHandler implements IEventHandler<"Allo", "PoolMetadataUpdated"> { | ||
constructor( | ||
readonly event: ProcessorEvent<"Allo", "PoolMetadataUpdated">, | ||
private readonly chainId: ChainId, | ||
private readonly dependencies: Dependencies, | ||
) {} | ||
/* @inheritdoc */ | ||
async handle(): Promise<Changeset[]> { | ||
const [_protocol, metadataPointer] = this.event.params.metadata; | ||
const { metadataProvider, pricingProvider, roundRepository } = this.dependencies; | ||
|
||
const metadata = await metadataProvider.getMetadata<{ | ||
round?: unknown; | ||
application?: unknown; | ||
}>(metadataPointer); | ||
|
||
const round = await roundRepository.getRoundByIdOrThrow( | ||
this.chainId, | ||
this.event.params.poolId.toString(), | ||
); | ||
|
||
let matchAmount = round.matchAmount; | ||
let matchAmountInUsd = round.matchAmountInUsd; | ||
|
||
const parsedRoundMetadata = RoundMetadataSchema.safeParse(metadata?.round); | ||
const token = getToken(this.chainId, round.matchTokenAddress); | ||
|
||
if (parsedRoundMetadata.success && token) { | ||
matchAmount = parseUnits( | ||
parsedRoundMetadata.data.quadraticFundingConfig.matchingFundsAvailable.toString(), | ||
token.decimals, | ||
); | ||
matchAmountInUsd = ( | ||
await getTokenAmountInUsd( | ||
pricingProvider, | ||
token, | ||
matchAmount, | ||
this.event.blockTimestamp, | ||
) | ||
).amountInUsd; | ||
} | ||
|
||
return [ | ||
{ | ||
type: "UpdateRound", | ||
args: { | ||
chainId: this.chainId, | ||
roundId: this.event.params.poolId.toString(), | ||
round: { | ||
matchAmount, | ||
matchAmountInUsd, | ||
applicationMetadataCid: metadataPointer, | ||
applicationMetadata: metadata?.application ?? {}, | ||
roundMetadataCid: metadataPointer, | ||
roundMetadata: metadata?.round ?? {}, | ||
}, | ||
}, | ||
}, | ||
]; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
packages/processors/src/processors/allo/handlers/roleGranted.handler.ts
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,84 @@ | ||
import { getAddress } from "viem"; | ||
|
||
import type { Changeset, Round } from "@grants-stack-indexer/repository"; | ||
import type { ChainId, ProcessorEvent } from "@grants-stack-indexer/shared"; | ||
|
||
import type { IEventHandler, ProcessorDependencies } from "../../../internal.js"; | ||
|
||
type Dependencies = Pick<ProcessorDependencies, "roundRepository">; | ||
|
||
/** | ||
* Handles the RoleGranted event for the Allo protocol. | ||
* | ||
* This handler performs the following core actions when a new role is granted: | ||
* - Insert a new round role if the role granted is admin or manager. | ||
* - Insert a new pending round role if the role granted is not admin or manager. | ||
* - Return the changeset. | ||
*/ | ||
export class RoleGrantedHandler implements IEventHandler<"Allo", "RoleGranted"> { | ||
constructor( | ||
readonly event: ProcessorEvent<"Allo", "RoleGranted">, | ||
private readonly chainId: ChainId, | ||
private readonly dependencies: Dependencies, | ||
) {} | ||
/* @inheritdoc */ | ||
async handle(): Promise<Changeset[]> { | ||
const role = this.event.params.role.toLowerCase(); | ||
const account = getAddress(this.event.params.account); | ||
const { roundRepository } = this.dependencies; | ||
|
||
let round: Round | undefined = undefined; | ||
|
||
// search for a round where the admin role is the role granted | ||
round = await roundRepository.getRoundByRole(this.chainId, "admin", role); | ||
if (round) { | ||
return [ | ||
{ | ||
type: "InsertRoundRole", | ||
args: { | ||
roundRole: { | ||
chainId: this.chainId, | ||
roundId: round.id, | ||
role: "admin", | ||
address: account, | ||
createdAtBlock: BigInt(this.event.blockNumber), | ||
}, | ||
}, | ||
}, | ||
]; | ||
} | ||
|
||
// search for a round where the manager role is the role granted | ||
round = await roundRepository.getRoundByRole(this.chainId, "manager", role); | ||
if (round) { | ||
return [ | ||
{ | ||
type: "InsertRoundRole", | ||
args: { | ||
roundRole: { | ||
chainId: this.chainId, | ||
roundId: round.id, | ||
role: "manager", | ||
address: account, | ||
createdAtBlock: BigInt(this.event.blockNumber), | ||
}, | ||
}, | ||
}, | ||
]; | ||
} | ||
|
||
return [ | ||
{ | ||
type: "InsertPendingRoundRole", | ||
args: { | ||
pendingRoundRole: { | ||
chainId: this.chainId, | ||
role: role, | ||
address: account, | ||
createdAtBlock: BigInt(this.event.blockNumber), | ||
}, | ||
}, | ||
}, | ||
]; | ||
} | ||
} |
Oops, something went wrong.