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.
Merge branch 'dev' into feat/direct-grants-lite-strategy
- Loading branch information
Showing
20 changed files
with
486 additions
and
15 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
38 changes: 38 additions & 0 deletions
38
packages/processors/src/processors/strategy/directAllocation/directAllocation.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,38 @@ | ||
import { Changeset } from "@grants-stack-indexer/repository"; | ||
import { ChainId, ProcessorEvent, StrategyEvent } from "@grants-stack-indexer/shared"; | ||
|
||
import { ProcessorDependencies, UnsupportedEventException } from "../../../internal.js"; | ||
import { BaseStrategyHandler } from "../index.js"; | ||
import { DirectAllocatedHandler } from "./handlers/index.js"; | ||
|
||
const STRATEGY_NAME = "allov2.DirectAllocationStrategy"; | ||
|
||
/** | ||
* This handler is responsible for processing events related to the | ||
* Direct Allocation strategy. | ||
* | ||
* The following events are currently handled by this strategy: | ||
* - DirectAllocated | ||
*/ | ||
export class DirectAllocationStrategyHandler 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 "DirectAllocated": | ||
return new DirectAllocatedHandler( | ||
event as ProcessorEvent<"Strategy", "DirectAllocated">, | ||
this.chainId, | ||
this.dependencies, | ||
).handle(); | ||
default: | ||
throw new UnsupportedEventException("Strategy", event.eventName, this.name); | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
...s/processors/src/processors/strategy/directAllocation/handlers/directAllocated.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,91 @@ | ||
import { getAddress, zeroAddress } from "viem"; | ||
|
||
import { Changeset, Donation } from "@grants-stack-indexer/repository"; | ||
import { ChainId, getTokenOrThrow, ProcessorEvent } from "@grants-stack-indexer/shared"; | ||
|
||
import { getTokenAmountInUsd } from "../../../../helpers/index.js"; | ||
import { IEventHandler, ProcessorDependencies } from "../../../../internal.js"; | ||
import { getDonationId } from "../../helpers/index.js"; | ||
|
||
type Dependencies = Pick< | ||
ProcessorDependencies, | ||
"projectRepository" | "roundRepository" | "pricingProvider" | "logger" | ||
>; | ||
|
||
/** | ||
* Handles the DirectAllocated event for the Direct Allocation strategy. | ||
* | ||
* This handler processes direct allocations of funds to a project by: | ||
* - Validating that both the round and project exist | ||
* - Retrieving token price data to calculate USD amounts | ||
* - Creating a new donation record with the allocated amount | ||
* | ||
* Unlike other allocation handlers, this one does not require an application | ||
* since funds are allocated directly to projects. | ||
*/ | ||
export class DirectAllocatedHandler implements IEventHandler<"Strategy", "DirectAllocated"> { | ||
constructor( | ||
readonly event: ProcessorEvent<"Strategy", "DirectAllocated">, | ||
private readonly chainId: ChainId, | ||
private readonly dependencies: Dependencies, | ||
) {} | ||
|
||
/** | ||
* Handles the DirectAllocated event for the Direct Allocation strategy. | ||
* @returns {Changeset[]} The changeset containing an InsertDonation change | ||
* @throws {ProjectNotFound} if the project does not exist | ||
* @throws {RoundNotFound} if the round does not exist | ||
* @throws {UnknownToken} if the token does not exist | ||
* @throws {TokenPriceNotFoundError} if the token price is not found | ||
*/ | ||
async handle(): Promise<Changeset[]> { | ||
const { projectRepository, roundRepository, pricingProvider } = this.dependencies; | ||
const strategyAddress = getAddress(this.event.srcAddress); | ||
|
||
const round = await roundRepository.getRoundByStrategyAddressOrThrow( | ||
this.chainId, | ||
strategyAddress, | ||
); | ||
const project = await projectRepository.getProjectByIdOrThrow( | ||
this.chainId, | ||
this.event.params.profileId, | ||
); | ||
|
||
const donationId = getDonationId(this.event.blockNumber, this.event.logIndex); | ||
|
||
const amount = BigInt(this.event.params.amount); | ||
const token = getTokenOrThrow(this.chainId, this.event.params.token); | ||
const sender = getAddress(this.event.params.sender); | ||
|
||
const { amountInUsd, timestamp: priceTimestamp } = await getTokenAmountInUsd( | ||
pricingProvider, | ||
token, | ||
amount, | ||
this.event.blockTimestamp, | ||
); | ||
|
||
const donation: Donation = { | ||
id: donationId, | ||
chainId: this.chainId, | ||
roundId: round.id, | ||
applicationId: zeroAddress, | ||
donorAddress: sender, | ||
recipientAddress: getAddress(this.event.params.profileOwner), | ||
projectId: project.id, | ||
transactionHash: this.event.transactionFields.hash, | ||
blockNumber: BigInt(this.event.blockNumber), | ||
tokenAddress: token.address, | ||
amount: amount, | ||
amountInUsd, | ||
amountInRoundMatchToken: 0n, | ||
timestamp: new Date(priceTimestamp), | ||
}; | ||
|
||
return [ | ||
{ | ||
type: "InsertDonation", | ||
args: { donation }, | ||
}, | ||
]; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/processors/src/processors/strategy/directAllocation/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 @@ | ||
export * from "./directAllocated.handler.js"; |
2 changes: 2 additions & 0 deletions
2
packages/processors/src/processors/strategy/directAllocation/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,2 @@ | ||
export * from "./handlers/index.js"; | ||
export * from "./directAllocation.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
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
8 changes: 8 additions & 0 deletions
8
packages/processors/src/processors/strategy/helpers/allocated.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,8 @@ | ||
import { encodePacked, keccak256 } from "viem/utils"; | ||
|
||
/** | ||
* DONATION_ID = keccak256(abi.encodePacked(blockNumber, "-", logIndex)); | ||
*/ | ||
export const getDonationId = (blockNumber: number, logIndex: number): string => { | ||
return keccak256(encodePacked(["string"], [`${blockNumber}-${logIndex}`])); | ||
}; |
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,2 +1,3 @@ | ||
export * from "./decoder.js"; | ||
export * from "./applicationStatus.js"; | ||
export * from "./allocated.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
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
96 changes: 96 additions & 0 deletions
96
packages/processors/test/strategy/directAllocation/directAllocation.handler.spec.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,96 @@ | ||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
||
import { EvmProvider } from "@grants-stack-indexer/chain-providers"; | ||
import { IMetadataProvider } from "@grants-stack-indexer/metadata"; | ||
import { IPricingProvider } from "@grants-stack-indexer/pricing"; | ||
import { | ||
IApplicationReadRepository, | ||
IProjectReadRepository, | ||
IRoundReadRepository, | ||
} from "@grants-stack-indexer/repository"; | ||
import { ChainId, ILogger, ProcessorEvent, StrategyEvent } from "@grants-stack-indexer/shared"; | ||
|
||
import { UnsupportedEventException } from "../../../src/internal.js"; | ||
import { DirectAllocationStrategyHandler } from "../../../src/processors/strategy/directAllocation/directAllocation.handler.js"; | ||
import { DirectAllocatedHandler } from "../../../src/processors/strategy/directAllocation/handlers/directAllocated.handler.js"; | ||
|
||
vi.mock( | ||
"../../../src/processors/strategy/directAllocation/handlers/directAllocated.handler.js", | ||
() => { | ||
const DirectAllocatedHandler = vi.fn(); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
DirectAllocatedHandler.prototype.handle = vi.fn(); | ||
return { DirectAllocatedHandler }; | ||
}, | ||
); | ||
|
||
describe("DirectAllocationStrategyHandler", () => { | ||
let handler: DirectAllocationStrategyHandler; | ||
let mockMetadataProvider: IMetadataProvider; | ||
let mockRoundRepository: IRoundReadRepository; | ||
let mockProjectRepository: IProjectReadRepository; | ||
let mockEVMProvider: EvmProvider; | ||
let mockPricingProvider: IPricingProvider; | ||
let mockApplicationRepository: IApplicationReadRepository; | ||
let mockLogger: ILogger; | ||
const chainId = 10 as ChainId; | ||
|
||
beforeEach(() => { | ||
mockMetadataProvider = {} as IMetadataProvider; | ||
mockRoundRepository = {} as IRoundReadRepository; | ||
mockProjectRepository = {} as IProjectReadRepository; | ||
mockEVMProvider = {} as unknown as EvmProvider; | ||
mockPricingProvider = {} as IPricingProvider; | ||
mockApplicationRepository = {} as IApplicationReadRepository; | ||
mockLogger = {} as ILogger; | ||
|
||
handler = new DirectAllocationStrategyHandler(chainId, { | ||
metadataProvider: mockMetadataProvider, | ||
roundRepository: mockRoundRepository, | ||
projectRepository: mockProjectRepository, | ||
evmProvider: mockEVMProvider, | ||
pricingProvider: mockPricingProvider, | ||
applicationRepository: mockApplicationRepository, | ||
logger: mockLogger, | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it("returns correct name", () => { | ||
expect(handler.name).toBe("allov2.DirectAllocationStrategy"); | ||
}); | ||
|
||
it("calls DirectAllocatedHandler for DirectAllocated event", async () => { | ||
const mockEvent = { | ||
eventName: "DirectAllocated", | ||
} as ProcessorEvent<"Strategy", "DirectAllocated">; | ||
|
||
vi.spyOn(DirectAllocatedHandler.prototype, "handle").mockResolvedValue([]); | ||
|
||
await handler.handle(mockEvent); | ||
|
||
expect(DirectAllocatedHandler).toHaveBeenCalledWith(mockEvent, chainId, { | ||
metadataProvider: mockMetadataProvider, | ||
roundRepository: mockRoundRepository, | ||
projectRepository: mockProjectRepository, | ||
evmProvider: mockEVMProvider, | ||
pricingProvider: mockPricingProvider, | ||
applicationRepository: mockApplicationRepository, | ||
logger: mockLogger, | ||
}); | ||
expect(DirectAllocatedHandler.prototype.handle).toHaveBeenCalled(); | ||
}); | ||
|
||
it("throws UnsupportedEventException for unknown events", async () => { | ||
const mockEvent = { | ||
eventName: "UnknownEvent", | ||
} as unknown as ProcessorEvent<"Strategy", StrategyEvent>; | ||
|
||
await expect(handler.handle(mockEvent)).rejects.toThrow( | ||
new UnsupportedEventException("Strategy", "UnknownEvent", handler.name), | ||
); | ||
}); | ||
}); |
Oops, something went wrong.