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: events orchestrator #21
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
000032e
feat: events orchestrator
0xkenj1 deccdf3
Merge branch 'dev' into feat/events-orchestrator
0xnigir1 efa1446
feat: remaining events orchestrator impl
0xnigir1 39c885a
test: add unit tests and natspec
0xnigir1 99b75b9
fix: hanging tests
0xnigir1 0000ae6
fix: hanging tests
0xnigir1 1d4d9fe
fix: use native AbortController
0xnigir1 d88a2d3
fix: hanging tests
0xnigir1 d594c3e
fix: pr comments
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./strategy.abi.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,225 @@ | ||
export const iStrategyAbi = [ | ||
{ | ||
type: "function", | ||
name: "allocate", | ||
inputs: [ | ||
{ name: "_data", type: "bytes", internalType: "bytes" }, | ||
{ name: "_sender", type: "address", internalType: "address" }, | ||
], | ||
outputs: [], | ||
stateMutability: "payable", | ||
}, | ||
{ | ||
type: "function", | ||
name: "distribute", | ||
inputs: [ | ||
{ name: "_recipientIds", type: "address[]", internalType: "address[]" }, | ||
{ name: "_data", type: "bytes", internalType: "bytes" }, | ||
{ name: "_sender", type: "address", internalType: "address" }, | ||
], | ||
outputs: [], | ||
stateMutability: "nonpayable", | ||
}, | ||
{ | ||
type: "function", | ||
name: "getAllo", | ||
inputs: [], | ||
outputs: [{ name: "", type: "address", internalType: "contract IAllo" }], | ||
stateMutability: "view", | ||
}, | ||
{ | ||
type: "function", | ||
name: "getPayouts", | ||
inputs: [ | ||
{ name: "_recipientIds", type: "address[]", internalType: "address[]" }, | ||
{ name: "_data", type: "bytes[]", internalType: "bytes[]" }, | ||
], | ||
outputs: [ | ||
{ | ||
name: "", | ||
type: "tuple[]", | ||
internalType: "struct IStrategy.PayoutSummary[]", | ||
components: [ | ||
{ | ||
name: "recipientAddress", | ||
type: "address", | ||
internalType: "address", | ||
}, | ||
{ name: "amount", type: "uint256", internalType: "uint256" }, | ||
], | ||
}, | ||
], | ||
stateMutability: "view", | ||
}, | ||
{ | ||
type: "function", | ||
name: "getPoolAmount", | ||
inputs: [], | ||
outputs: [{ name: "", type: "uint256", internalType: "uint256" }], | ||
stateMutability: "view", | ||
}, | ||
{ | ||
type: "function", | ||
name: "getPoolId", | ||
inputs: [], | ||
outputs: [{ name: "", type: "uint256", internalType: "uint256" }], | ||
stateMutability: "view", | ||
}, | ||
{ | ||
type: "function", | ||
name: "getRecipientStatus", | ||
inputs: [{ name: "_recipientId", type: "address", internalType: "address" }], | ||
outputs: [{ name: "", type: "uint8", internalType: "enum IStrategy.Status" }], | ||
stateMutability: "view", | ||
}, | ||
{ | ||
type: "function", | ||
name: "getStrategyId", | ||
inputs: [], | ||
outputs: [{ name: "", type: "bytes32", internalType: "bytes32" }], | ||
stateMutability: "view", | ||
}, | ||
{ | ||
type: "function", | ||
name: "increasePoolAmount", | ||
inputs: [{ name: "_amount", type: "uint256", internalType: "uint256" }], | ||
outputs: [], | ||
stateMutability: "nonpayable", | ||
}, | ||
{ | ||
type: "function", | ||
name: "initialize", | ||
inputs: [ | ||
{ name: "_poolId", type: "uint256", internalType: "uint256" }, | ||
{ name: "_data", type: "bytes", internalType: "bytes" }, | ||
], | ||
outputs: [], | ||
stateMutability: "nonpayable", | ||
}, | ||
{ | ||
type: "function", | ||
name: "isPoolActive", | ||
inputs: [], | ||
outputs: [{ name: "", type: "bool", internalType: "bool" }], | ||
stateMutability: "nonpayable", | ||
}, | ||
{ | ||
type: "function", | ||
name: "isValidAllocator", | ||
inputs: [{ name: "_allocator", type: "address", internalType: "address" }], | ||
outputs: [{ name: "", type: "bool", internalType: "bool" }], | ||
stateMutability: "view", | ||
}, | ||
{ | ||
type: "function", | ||
name: "registerRecipient", | ||
inputs: [ | ||
{ name: "_data", type: "bytes", internalType: "bytes" }, | ||
{ name: "_sender", type: "address", internalType: "address" }, | ||
], | ||
outputs: [{ name: "", type: "address", internalType: "address" }], | ||
stateMutability: "payable", | ||
}, | ||
{ | ||
type: "event", | ||
name: "Allocated", | ||
inputs: [ | ||
{ | ||
name: "recipientId", | ||
type: "address", | ||
indexed: true, | ||
internalType: "address", | ||
}, | ||
{ | ||
name: "amount", | ||
type: "uint256", | ||
indexed: false, | ||
internalType: "uint256", | ||
}, | ||
{ | ||
name: "token", | ||
type: "address", | ||
indexed: false, | ||
internalType: "address", | ||
}, | ||
{ | ||
name: "sender", | ||
type: "address", | ||
indexed: false, | ||
internalType: "address", | ||
}, | ||
], | ||
anonymous: false, | ||
}, | ||
{ | ||
type: "event", | ||
name: "Distributed", | ||
inputs: [ | ||
{ | ||
name: "recipientId", | ||
type: "address", | ||
indexed: true, | ||
internalType: "address", | ||
}, | ||
{ | ||
name: "recipientAddress", | ||
type: "address", | ||
indexed: false, | ||
internalType: "address", | ||
}, | ||
{ | ||
name: "amount", | ||
type: "uint256", | ||
indexed: false, | ||
internalType: "uint256", | ||
}, | ||
{ | ||
name: "sender", | ||
type: "address", | ||
indexed: false, | ||
internalType: "address", | ||
}, | ||
], | ||
anonymous: false, | ||
}, | ||
{ | ||
type: "event", | ||
name: "Initialized", | ||
inputs: [ | ||
{ | ||
name: "poolId", | ||
type: "uint256", | ||
indexed: false, | ||
internalType: "uint256", | ||
}, | ||
{ name: "data", type: "bytes", indexed: false, internalType: "bytes" }, | ||
], | ||
anonymous: false, | ||
}, | ||
{ | ||
type: "event", | ||
name: "PoolActive", | ||
inputs: [{ name: "active", type: "bool", indexed: false, internalType: "bool" }], | ||
anonymous: false, | ||
}, | ||
{ | ||
type: "event", | ||
name: "Registered", | ||
inputs: [ | ||
{ | ||
name: "recipientId", | ||
type: "address", | ||
indexed: true, | ||
internalType: "address", | ||
}, | ||
{ name: "data", type: "bytes", indexed: false, internalType: "bytes" }, | ||
{ | ||
name: "sender", | ||
type: "address", | ||
indexed: false, | ||
internalType: "address", | ||
}, | ||
], | ||
anonymous: false, | ||
}, | ||
] as const; |
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,55 @@ | ||
import type { Changeset } from "@grants-stack-indexer/repository"; | ||
import { | ||
AlloProcessor, | ||
ProcessorDependencies, | ||
RegistryProcessor, | ||
StrategyProcessor, | ||
} from "@grants-stack-indexer/processors"; | ||
import { | ||
AnyEvent, | ||
ChainId, | ||
ContractName, | ||
isAlloEvent, | ||
isRegistryEvent, | ||
isStrategyEvent, | ||
ProcessorEvent, | ||
} from "@grants-stack-indexer/shared"; | ||
|
||
import { InvalidEvent } from "./exceptions/index.js"; | ||
|
||
/** | ||
* EventsProcessor handles the processing of Allo V2 events by delegating them to the appropriate processor | ||
* (Allo, Registry, or Strategy) based on the event type. Each processor generates changesets that represent | ||
* the required database updates. | ||
*/ | ||
export class EventsProcessor { | ||
alloProcessor: AlloProcessor; | ||
registryProcessor: RegistryProcessor; | ||
strategyProcessor: StrategyProcessor; | ||
|
||
constructor(chainId: ChainId, dependencies: Readonly<ProcessorDependencies>) { | ||
this.alloProcessor = new AlloProcessor(chainId, dependencies); | ||
this.registryProcessor = new RegistryProcessor(chainId, dependencies); | ||
this.strategyProcessor = new StrategyProcessor(chainId, dependencies); | ||
} | ||
|
||
/** | ||
* Process an Allo V2 event and return the changesets | ||
* @param event - The event to process | ||
* @returns The changesets | ||
* @throws InvalidEvent if the event is not a valid Allo V2 event (Allo, Registry or Strategy) | ||
*/ | ||
public async processEvent(event: ProcessorEvent<ContractName, AnyEvent>): Promise<Changeset[]> { | ||
if (isAlloEvent(event)) { | ||
return await this.alloProcessor.process(event); | ||
} | ||
if (isRegistryEvent(event)) { | ||
return await this.registryProcessor.process(event); | ||
} | ||
if (isStrategyEvent(event)) { | ||
return await this.strategyProcessor.process(event); | ||
} | ||
|
||
throw new InvalidEvent(event); | ||
} | ||
} |
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,25 @@ | ||
import type { AnyEvent, ContractName, ProcessorEvent } from "@grants-stack-indexer/shared"; | ||
|
||
import type { IEventsRegistry } from "./internal.js"; | ||
|
||
/** | ||
* Class to store the last processed event in memory | ||
*/ | ||
//TODO: Implement storage version to persist the last processed event. we need to store it by chainId | ||
export class InMemoryEventsRegistry implements IEventsRegistry { | ||
private lastProcessedEvent: ProcessorEvent<ContractName, AnyEvent> | undefined; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
async getLastProcessedEvent(): Promise<ProcessorEvent<ContractName, AnyEvent> | undefined> { | ||
return this.lastProcessedEvent; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
async saveLastProcessedEvent(event: ProcessorEvent<ContractName, AnyEvent>): Promise<void> { | ||
this.lastProcessedEvent = event; | ||
} | ||
} |
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,2 @@ | ||
export * from "./invalidEvent.js"; | ||
export * from "./invalidChangeset.exception.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,9 @@ | ||
import { AnyEvent, ContractName, ProcessorEvent, stringify } from "@grants-stack-indexer/shared"; | ||
|
||
export class InvalidEvent extends Error { | ||
constructor(event: ProcessorEvent<ContractName, AnyEvent>) { | ||
super(`Event couldn't be processed: ${stringify(event)}`); | ||
|
||
this.name = "InvalidEvent"; | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
packages/data-flow/src/interfaces/eventsRegistry.interface.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,18 @@ | ||
import { AnyEvent, ContractName, ProcessorEvent } from "@grants-stack-indexer/shared"; | ||
|
||
/** | ||
* The events registry saves as a checkpoint to the last processed event by the system. | ||
* This is used to resume the indexing from the last processed event in case of an error or temporary interruption. | ||
*/ | ||
export interface IEventsRegistry { | ||
/** | ||
* Get the last processed event by the system | ||
* @returns The last processed event or undefined if no event has been processed yet. | ||
*/ | ||
getLastProcessedEvent(): Promise<ProcessorEvent<ContractName, AnyEvent> | undefined>; | ||
/** | ||
* Save the last processed event by the system | ||
* @param event - The event to save. | ||
*/ | ||
saveLastProcessedEvent(event: ProcessorEvent<ContractName, AnyEvent>): Promise<void>; | ||
} |
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,4 @@ | ||
export * from "./eventsFetcher.interface.js"; | ||
export * from "./dataLoader.interface.js"; | ||
export * from "./eventsRegistry.interface.js"; | ||
export * from "./strategyRegistry.interface.js"; |
Oops, something went wrong.
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.
it saves the last processed event as a checkpoint? or the registry itself is a checkpoint for the last processed event?
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.
the registry itself is the checkpoint, you query the registry for getting the last processed event (however at current MVP stage, since it's in memory on shutdown you lose tracking of course)