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: direct grants lite strategy #34
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8eee37b
feat: application payout repository
0xnigir1 7533c07
Merge branch 'dev' into feat/direct-grants-lite-strategy
0xnigir1 2ebd4cf
feat: direct grants lite event handlers
0xnigir1 180333a
fix: lint errors
0xnigir1 41a449d
test: unify createMockEvent util function
0xnigir1 11d422b
fix: typos, update natspec and import path
0xnigir1 7ad91df
feat: don't call pricing provider if amount is 0
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
28 changes: 28 additions & 0 deletions
28
packages/data-flow/src/data-loader/handlers/applicationPayout.handlers.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,28 @@ | ||
import { | ||
ApplicationPayoutChangeset, | ||
IApplicationPayoutRepository, | ||
} from "@grants-stack-indexer/repository"; | ||
|
||
import { ChangesetHandler } from "../types/index.js"; | ||
|
||
/** | ||
* Collection of handlers for application-related operations. | ||
* Each handler corresponds to a specific Application changeset type. | ||
*/ | ||
export type ApplicationPayoutHandlers = { | ||
[K in ApplicationPayoutChangeset["type"]]: ChangesetHandler<K>; | ||
}; | ||
|
||
/** | ||
* Creates handlers for managing application-related operations. | ||
* | ||
* @param repository - The application repository instance used for database operations | ||
* @returns An object containing all application-related handlers | ||
*/ | ||
export const createApplicationPayoutHandlers = ( | ||
repository: IApplicationPayoutRepository, | ||
): ApplicationPayoutHandlers => ({ | ||
InsertApplicationPayout: (async (changeset): Promise<void> => { | ||
await repository.insertApplicationPayout(changeset.args.applicationPayout); | ||
}) satisfies ChangesetHandler<"InsertApplicationPayout">, | ||
}); |
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
118 changes: 118 additions & 0 deletions
118
packages/processors/src/processors/strategy/directGrantsLite/directGrantsLite.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,118 @@ | ||
import { Changeset } from "@grants-stack-indexer/repository"; | ||
import { Address, ChainId, ProcessorEvent, StrategyEvent } from "@grants-stack-indexer/shared"; | ||
|
||
import DirectGrantsLiteStrategy from "../../../abis/allo-v2/v1/DirectGrantsLiteStrategy.js"; | ||
import { getDateFromTimestamp } from "../../../helpers/index.js"; | ||
import { | ||
BaseRecipientStatusUpdatedHandler, | ||
ProcessorDependencies, | ||
StrategyTimings, | ||
UnsupportedEventException, | ||
} from "../../../internal.js"; | ||
import { BaseStrategyHandler } from "../index.js"; | ||
import { | ||
DGLiteAllocatedHandler, | ||
DGLiteRegisteredHandler, | ||
DGLiteTimestampsUpdatedHandler, | ||
DGLiteUpdatedRegistrationHandler, | ||
} from "./handlers/index.js"; | ||
|
||
const STRATEGY_NAME = "allov2.DirectGrantsLiteStrategy"; | ||
|
||
/** | ||
* This handler is responsible for processing events related to the | ||
* Direct Grants Lite strategy. | ||
* | ||
* The following events are currently handled by this strategy: | ||
* - Registered | ||
* - UpdatedRegistrationWithStatus | ||
* - TimestampsUpdated | ||
* - AllocatedWithToken | ||
* - RecipientStatusUpdatedWithFullRow | ||
*/ | ||
export class DirectGrantsLiteStrategyHandler extends BaseStrategyHandler { | ||
constructor( | ||
private readonly chainId: ChainId, | ||
private readonly dependencies: ProcessorDependencies, | ||
) { | ||
super(STRATEGY_NAME); | ||
} | ||
|
||
/** @inheritdoc */ | ||
async handle(event: ProcessorEvent<"Strategy", StrategyEvent>): Promise<Changeset[]> { | ||
switch (event.eventName) { | ||
case "RecipientStatusUpdatedWithFullRow": | ||
return new BaseRecipientStatusUpdatedHandler( | ||
event as ProcessorEvent<"Strategy", "RecipientStatusUpdatedWithFullRow">, | ||
this.chainId, | ||
this.dependencies, | ||
).handle(); | ||
case "RegisteredWithSender": | ||
return new DGLiteRegisteredHandler( | ||
event as ProcessorEvent<"Strategy", "RegisteredWithSender">, | ||
this.chainId, | ||
this.dependencies, | ||
).handle(); | ||
case "UpdatedRegistrationWithStatus": | ||
return new DGLiteUpdatedRegistrationHandler( | ||
event as ProcessorEvent<"Strategy", "UpdatedRegistrationWithStatus">, | ||
this.chainId, | ||
this.dependencies, | ||
).handle(); | ||
case "TimestampsUpdated": | ||
return new DGLiteTimestampsUpdatedHandler( | ||
event as ProcessorEvent<"Strategy", "TimestampsUpdated">, | ||
this.chainId, | ||
this.dependencies, | ||
).handle(); | ||
case "AllocatedWithToken": | ||
return new DGLiteAllocatedHandler( | ||
event as ProcessorEvent<"Strategy", "AllocatedWithToken">, | ||
this.chainId, | ||
this.dependencies, | ||
).handle(); | ||
default: | ||
throw new UnsupportedEventException("Strategy", event.eventName, this.name); | ||
} | ||
} | ||
|
||
/** @inheritdoc */ | ||
override async fetchStrategyTimings(strategyId: Address): Promise<StrategyTimings> { | ||
const { evmProvider } = this.dependencies; | ||
let results: [bigint, bigint] = [0n, 0n]; | ||
|
||
const contractCalls = [ | ||
{ | ||
abi: DirectGrantsLiteStrategy, | ||
functionName: "registrationStartTime", | ||
address: strategyId, | ||
}, | ||
{ | ||
abi: DirectGrantsLiteStrategy, | ||
functionName: "registrationEndTime", | ||
address: strategyId, | ||
}, | ||
] as const; | ||
|
||
// TODO: refactor when evmProvider implements this natively | ||
if (evmProvider.getMulticall3Address()) { | ||
results = await evmProvider.multicall({ | ||
contracts: contractCalls, | ||
allowFailure: false, | ||
}); | ||
} else { | ||
results = (await Promise.all( | ||
contractCalls.map((call) => | ||
evmProvider.readContract(call.address, call.abi, call.functionName), | ||
), | ||
)) as [bigint, bigint]; | ||
} | ||
|
||
return { | ||
applicationsStartTime: getDateFromTimestamp(results[0]), | ||
applicationsEndTime: getDateFromTimestamp(results[1]), | ||
donationsStartTime: null, | ||
donationsEndTime: null, | ||
}; | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
packages/processors/src/processors/strategy/directGrantsLite/handlers/allocated.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,109 @@ | ||
import { getAddress } from "viem"; | ||
|
||
import { Changeset } from "@grants-stack-indexer/repository"; | ||
import { ChainId, getTokenOrThrow, ProcessorEvent } from "@grants-stack-indexer/shared"; | ||
|
||
import { getTokenAmountInUsd, getUsdInTokenAmount } from "../../../../helpers/index.js"; | ||
import { IEventHandler, ProcessorDependencies } from "../../../../internal.js"; | ||
|
||
type Dependencies = Pick< | ||
ProcessorDependencies, | ||
"roundRepository" | "applicationRepository" | "pricingProvider" | ||
>; | ||
|
||
/** | ||
* Handler for processing AllocatedWithToken events from the DirectGrantsLite strategy. | ||
* | ||
* When a round operator allocates funds to a recipient, this handler: | ||
* 1. Retrieves the round and application based on the strategy address and recipient | ||
* 2. Converts the allocated token amount to USD value | ||
* 3. Calculates the equivalent amount in the round's match token | ||
* 4. Updates the application with the allocation details | ||
*/ | ||
|
||
export class DGLiteAllocatedHandler implements IEventHandler<"Strategy", "AllocatedWithToken"> { | ||
constructor( | ||
readonly event: ProcessorEvent<"Strategy", "AllocatedWithToken">, | ||
private readonly chainId: ChainId, | ||
private readonly dependencies: Dependencies, | ||
) {} | ||
|
||
/** | ||
* Handles the AllocatedWithToken event for the Direct Grants Lite strategy. | ||
* @returns The changeset with an InsertApplicationPayout operation. | ||
* @throws RoundNotFound if the round is not found. | ||
* @throws ApplicationNotFound if the application is not found. | ||
* @throws TokenNotFound if the token is not found. | ||
* @throws TokenPriceNotFound if the token price is not found. | ||
*/ | ||
async handle(): Promise<Changeset[]> { | ||
const { roundRepository, applicationRepository } = this.dependencies; | ||
const { srcAddress } = this.event; | ||
const { recipientId: _recipientId, amount: strAmount, token: _token } = this.event.params; | ||
|
||
const amount = BigInt(strAmount); | ||
|
||
const round = await roundRepository.getRoundByStrategyAddressOrThrow( | ||
this.chainId, | ||
getAddress(srcAddress), | ||
); | ||
|
||
const recipientId = getAddress(_recipientId); | ||
const tokenAddress = getAddress(_token); | ||
const application = await applicationRepository.getApplicationByAnchorAddressOrThrow( | ||
this.chainId, | ||
round.id, | ||
recipientId, | ||
); | ||
|
||
const token = getTokenOrThrow(this.chainId, tokenAddress); | ||
const matchToken = getTokenOrThrow(this.chainId, round.matchTokenAddress); | ||
|
||
let amountInUsd = "0"; | ||
let amountInRoundMatchToken = 0n; | ||
|
||
if (amount > 0) { | ||
const { amountInUsd: amountInUsdString } = await getTokenAmountInUsd( | ||
this.dependencies.pricingProvider, | ||
token, | ||
amount, | ||
this.event.blockTimestamp, | ||
); | ||
amountInUsd = amountInUsdString; | ||
|
||
amountInRoundMatchToken = | ||
matchToken.address === token.address | ||
? amount | ||
: ( | ||
await getUsdInTokenAmount( | ||
this.dependencies.pricingProvider, | ||
matchToken, | ||
amountInUsd, | ||
this.event.blockTimestamp, | ||
) | ||
).amount; | ||
} | ||
|
||
const timestamp = this.event.blockTimestamp; | ||
|
||
return [ | ||
{ | ||
type: "InsertApplicationPayout", | ||
args: { | ||
applicationPayout: { | ||
amount, | ||
applicationId: application.id, | ||
roundId: round.id, | ||
chainId: this.chainId, | ||
tokenAddress, | ||
amountInRoundMatchToken, | ||
amountInUsd, | ||
transactionHash: this.event.transactionFields.hash, | ||
sender: getAddress(this.event.params.sender), | ||
timestamp: new Date(timestamp), | ||
}, | ||
}, | ||
}, | ||
]; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/processors/src/processors/strategy/directGrantsLite/handlers/index.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,4 @@ | ||
export * from "./registered.handler.js"; | ||
export * from "./updatedRegistration.handler.js"; | ||
export * from "./timestampsUpdated.handler.js"; | ||
export * from "./allocated.handler.js"; |
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.
if amount is zero maybe we should return early to avoid unnecessary calls that'll just return 0
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.
i wouldn't return early but very nice catch, i will improve code to avoid querying prices wen amount is 0 🤝
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.
7ad91df