generated from defi-wonderland/ts-turborepo-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: all remaining event handlers for DVMD strategy #30
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b442db8
feat: all remaining event handlers for DVMD strategy
0xnigir1 4187c7c
feat: write XXOrThrow methods on repository
0xnigir1 980f1f6
fix: string type on Envio events & repository jsonb field issue
0xnigir1 2bef2e4
fix: pr comments
0xnigir1 b0d96ab
Merge branch 'dev' into feat/dvmd-remaining-handlers
0xnigir1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,6 @@ | ||
export enum ApplicationStatus { | ||
NONE = 0, | ||
PENDING, | ||
APPROVED, | ||
REJECTED, | ||
} |
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 @@ | ||
export * from "./enums.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
6 changes: 6 additions & 0 deletions
6
packages/processors/src/exceptions/metadataNotFound.exception.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,6 @@ | ||
export class MetadataNotFound extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = "MetadataNotFoundError"; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,3 +1,4 @@ | ||
export * from "./projectMetadata.js"; | ||
export * from "./roundMetadata.js"; | ||
export * from "./applicationMetadata.js"; | ||
export * from "./matchingDistribution.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { z } from "zod"; | ||
|
||
export type MatchingDistribution = z.infer<typeof MatchingDistributionSchema>; | ||
|
||
const BigIntSchema = z.string().or( | ||
z.object({ type: z.literal("BigNumber"), hex: z.string() }).transform((val) => { | ||
return BigInt(val.hex).toString(); | ||
}), | ||
); | ||
|
||
export const MatchingDistributionSchema = z.object({ | ||
matchingDistribution: z.array( | ||
z.object({ | ||
applicationId: z.string(), | ||
projectPayoutAddress: z.string(), | ||
projectId: z.string(), | ||
projectName: z.string(), | ||
matchPoolPercentage: z.number().or(z.string().min(1)).pipe(z.coerce.number()), | ||
contributionsCount: z.number().or(z.string().min(1)).pipe(z.coerce.number()), | ||
originalMatchAmountInToken: BigIntSchema.default("0"), | ||
matchAmountInToken: BigIntSchema.default("0"), | ||
}), | ||
), | ||
}); |
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
78 changes: 78 additions & 0 deletions
78
packages/processors/src/strategy/common/baseDistributionUpdated.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,78 @@ | ||
import { getAddress } from "viem"; | ||
|
||
import { Changeset } from "@grants-stack-indexer/repository"; | ||
import { ChainId, ProcessorEvent } from "@grants-stack-indexer/shared"; | ||
|
||
import { | ||
IEventHandler, | ||
MetadataNotFound, | ||
MetadataParsingFailed, | ||
ProcessorDependencies, | ||
} from "../../internal.js"; | ||
import { MatchingDistribution, MatchingDistributionSchema } from "../../schemas/index.js"; | ||
|
||
type Dependencies = Pick<ProcessorDependencies, "metadataProvider" | "logger">; | ||
|
||
/** | ||
* BaseDistributionUpdatedHandler: Processes 'DistributionUpdated' events | ||
* | ||
* - Decodes the updated distribution metadata | ||
* - Creates a changeset to update the round with the new distribution | ||
* - Serves as a base class as all strategies share the same logic for this event. | ||
* | ||
* @dev: | ||
* - Strategy handlers that want to handle the DistributionUpdated event should create an instance of this class corresponding to the event. | ||
* | ||
*/ | ||
|
||
export class BaseDistributionUpdatedHandler | ||
implements IEventHandler<"Strategy", "DistributionUpdated"> | ||
{ | ||
constructor( | ||
readonly event: ProcessorEvent<"Strategy", "DistributionUpdated">, | ||
private readonly chainId: ChainId, | ||
private readonly dependencies: Dependencies, | ||
) {} | ||
|
||
/* @inheritdoc */ | ||
async handle(): Promise<Changeset[]> { | ||
const { logger, metadataProvider } = this.dependencies; | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const [_, pointer] = this.event.params.metadata; | ||
|
||
const strategyAddress = getAddress(this.event.srcAddress); | ||
const rawDistribution = await metadataProvider.getMetadata< | ||
MatchingDistribution | undefined | ||
>(pointer); | ||
|
||
if (!rawDistribution) { | ||
logger.warn(`No matching distribution found for pointer: ${pointer}`); | ||
|
||
throw new MetadataNotFound(`No matching distribution found for pointer: ${pointer}`); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should MetadataProvider be responsible for throwing a "not found" error instead of having the handler doing it? |
||
|
||
const distribution = MatchingDistributionSchema.safeParse(rawDistribution); | ||
|
||
if (!distribution.success) { | ||
logger.warn(`Failed to parse matching distribution: ${distribution.error.message}`); | ||
|
||
throw new MetadataParsingFailed( | ||
`Failed to parse matching distribution: ${distribution.error.message}`, | ||
); | ||
} | ||
|
||
return [ | ||
{ | ||
type: "UpdateRoundByStrategyAddress", | ||
args: { | ||
chainId: this.chainId, | ||
strategyAddress, | ||
round: { | ||
readyForPayoutTransaction: this.event.transactionFields.hash, | ||
matchingDistribution: distribution.data.matchingDistribution, | ||
}, | ||
}, | ||
}, | ||
]; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
packages/processors/src/strategy/common/baseFundsDistributed.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,81 @@ | ||
import { getAddress } from "viem"; | ||
|
||
import { Changeset } from "@grants-stack-indexer/repository"; | ||
import { ChainId, ProcessorEvent } from "@grants-stack-indexer/shared"; | ||
|
||
import { IEventHandler, ProcessorDependencies } from "../../internal.js"; | ||
|
||
type Dependencies = Pick< | ||
ProcessorDependencies, | ||
"roundRepository" | "applicationRepository" | "logger" | ||
>; | ||
|
||
/** | ||
* BaseFundsDistributedHandler: Processes 'FundsDistributed' events | ||
* | ||
* - Handles funds distributed events across all strategies. | ||
* - Creates two changesets: | ||
* 1. UpdateApplication: Updates the application with the transaction hash. | ||
* 2. IncrementRoundTotalDistributed: Increments the total distributed amount for a round. | ||
* - Serves as a base class as all strategies share the same logic for this event. | ||
* | ||
* @dev: | ||
* - Strategy handlers that want to handle the FundsDistributed event should create an instance of this class corresponding to the event. | ||
* | ||
*/ | ||
|
||
export class BaseFundsDistributedHandler implements IEventHandler<"Strategy", "FundsDistributed"> { | ||
constructor( | ||
readonly event: ProcessorEvent<"Strategy", "FundsDistributed">, | ||
private readonly chainId: ChainId, | ||
private readonly dependencies: Dependencies, | ||
) {} | ||
|
||
/** | ||
* Handles the FundsDistributed event. | ||
* @throws {RoundNotFound} if the round is not found. | ||
* @throws {ApplicationNotFound} if the application is not found. | ||
* @returns An array of changesets with the following: | ||
* 1. UpdateApplication: Updates the application with the transaction hash. | ||
* 2. IncrementRoundTotalDistributed: Increments the total distributed amount for a round. | ||
*/ | ||
async handle(): Promise<Changeset[]> { | ||
const { roundRepository, applicationRepository } = this.dependencies; | ||
|
||
const strategyAddress = getAddress(this.event.srcAddress); | ||
const round = await roundRepository.getRoundByStrategyAddressOrThrow( | ||
this.chainId, | ||
strategyAddress, | ||
); | ||
|
||
const roundId = round.id; | ||
const anchorAddress = getAddress(this.event.params.recipientId); | ||
const application = await applicationRepository.getApplicationByAnchorAddressOrThrow( | ||
this.chainId, | ||
roundId, | ||
anchorAddress, | ||
); | ||
|
||
return [ | ||
{ | ||
type: "UpdateApplication", | ||
args: { | ||
chainId: this.chainId, | ||
roundId, | ||
applicationId: application.id, | ||
application: { | ||
distributionTransaction: this.event.transactionFields.hash, | ||
}, | ||
}, | ||
}, | ||
{ | ||
type: "IncrementRoundTotalDistributed", | ||
args: { | ||
chainId: this.chainId, | ||
roundId: round.id, | ||
amount: BigInt(this.event.params.amount), | ||
}, | ||
}, | ||
]; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inheritdoc?