Skip to content

Commit

Permalink
feat: improve logic on saving strategy id
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnigir1 committed Dec 13, 2024
1 parent fe62481 commit 9223013
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ vi.mock("@grants-stack-indexer/repository", () => ({
KyselyApplicationRepository: vi.fn(),
KyselyDonationRepository: vi.fn(),
KyselyApplicationPayoutRepository: vi.fn(),
KyselyStrategyRepository: vi.fn().mockImplementation(() => ({
KyselyStrategyRegistryRepository: vi.fn().mockImplementation(() => ({
getStrategies: vi.fn().mockResolvedValue([]),
getStrategyId: vi.fn(),
saveStrategyId: vi.fn(),
Expand Down
11 changes: 2 additions & 9 deletions packages/data-flow/src/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,17 @@ export class Orchestrator {

event = await this.enhanceStrategyId(event);
if (this.isPoolCreated(event)) {
// we save the strategy id with handled = false because we don't know if the strategy is handled yet
const handleable = existsHandler(event.strategyId);
await this.strategyRegistry.saveStrategyId(
this.chainId,
event.srcAddress,
event.strategyId,
false,
handleable,
);
} else if (event.contractName === "Strategy" && "strategyId" in event) {
if (!existsHandler(event.strategyId)) {
// we skip the event if the strategy id is not handled yet
continue;
} else {
await this.strategyRegistry.saveStrategyId(
this.chainId,
event.srcAddress,
event.strategyId,
true,
);
}
}

Expand Down
65 changes: 25 additions & 40 deletions packages/data-flow/test/unit/orchestrator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ describe("Orchestrator", { sequential: true }, () => {
const eventsProcessorSpy = vi.spyOn(orchestrator["eventsProcessor"], "processEvent");

vi.spyOn(mockEventsRegistry, "getLastProcessedEvent").mockResolvedValue(undefined);
vi.spyOn(mockStrategyRegistry, "getStrategyId").mockResolvedValue({
id: "0x6f9291df02b2664139cec5703c124e4ebce32879c74b6297faa1468aa5ff9ebf",
address: "0x123",
chainId,
handled: false,
});
vi.spyOn(mockIndexerClient, "getEventsAfterBlockNumberAndLogIndex")
.mockResolvedValueOnce(mockEvents)
.mockResolvedValue([]);
Expand All @@ -155,6 +161,9 @@ describe("Orchestrator", { sequential: true }, () => {
vi.spyOn(mockEventsRegistry, "saveLastProcessedEvent").mockImplementation(() => {
return Promise.resolve();
});
vi.spyOn(mockStrategyRegistry, "saveStrategyId").mockImplementation(() => {
return Promise.resolve();
});

runPromise = orchestrator.run(abortController.signal);

Expand Down Expand Up @@ -273,7 +282,8 @@ describe("Orchestrator", { sequential: true }, () => {

it("save strategyId to registry on PoolCreated event", async () => {
const strategyAddress = "0x123" as Address;
const strategyId =
const strategyId = "0xunknown" as Hex;
const existingStrategyId =
"0x6f9291df02b2664139cec5703c124e4ebce32879c74b6297faa1468aa5ff9ebf" as Hex;

const mockEvent = createMockEvent("Allo", "PoolCreated", 1, {
Expand All @@ -287,10 +297,12 @@ describe("Orchestrator", { sequential: true }, () => {

const eventsProcessorSpy = vi.spyOn(orchestrator["eventsProcessor"], "processEvent");
vi.spyOn(mockStrategyRegistry, "getStrategyId").mockResolvedValue(undefined);
vi.spyOn(mockEvmProvider, "readContract").mockResolvedValue(strategyId);
vi.spyOn(mockEvmProvider, "readContract")
.mockResolvedValueOnce(strategyId)
.mockResolvedValueOnce(existingStrategyId);
vi.spyOn(mockEventsRegistry, "getLastProcessedEvent").mockResolvedValue(undefined);
vi.spyOn(mockIndexerClient, "getEventsAfterBlockNumberAndLogIndex")
.mockResolvedValueOnce([mockEvent])
.mockResolvedValueOnce([mockEvent, mockEvent])
.mockResolvedValue([]);

runPromise = orchestrator.run(abortController.signal);
Expand All @@ -299,47 +311,18 @@ describe("Orchestrator", { sequential: true }, () => {
if (eventsProcessorSpy.mock.calls.length < 1) throw new Error("Not yet called");
});

expect(mockStrategyRegistry.saveStrategyId).toHaveBeenCalledWith(
expect(mockStrategyRegistry.saveStrategyId).toHaveBeenNthCalledWith(
1,
chainId,
strategyAddress,
strategyId,
false,
);
});

it("updates strategyId as handled=true on handled Strategy event", async () => {
const strategyAddress = "0x123" as Address;
const strategyId =
"0x6f9291df02b2664139cec5703c124e4ebce32879c74b6297faa1468aa5ff9ebf" as Hex;

const mockEvent = createMockEvent("Strategy", "RegisteredWithSender", 1, {
recipientId: "0x123",
data: "0x123",
sender: "0x123",
});

const eventsProcessorSpy = vi.spyOn(orchestrator["eventsProcessor"], "processEvent");
vi.spyOn(mockEventsRegistry, "getLastProcessedEvent").mockResolvedValue(undefined);
vi.spyOn(mockIndexerClient, "getEventsAfterBlockNumberAndLogIndex")
.mockResolvedValueOnce([mockEvent])
.mockResolvedValue([]);
vi.spyOn(mockStrategyRegistry, "getStrategyId").mockResolvedValue({
id: strategyId,
address: strategyAddress,
chainId,
handled: false,
});

runPromise = orchestrator.run(abortController.signal);

await vi.waitFor(() => {
if (eventsProcessorSpy.mock.calls.length < 1) throw new Error("Not yet called");
});

expect(mockStrategyRegistry.saveStrategyId).toHaveBeenCalledWith(
expect(mockStrategyRegistry.saveStrategyId).toHaveBeenNthCalledWith(
2,
chainId,
strategyAddress,
strategyId,
existingStrategyId,
true,
);
});
Expand Down Expand Up @@ -508,11 +491,10 @@ describe("Orchestrator", { sequential: true }, () => {
chainId,
handled: true,
});
vi.spyOn(mockEvmProvider, "readContract").mockResolvedValue(strategyId);

vi.spyOn(mockIndexerClient, "getEventsAfterBlockNumberAndLogIndex")
.mockResolvedValueOnce([poolCreatedEvent])
.mockResolvedValueOnce([])
.mockResolvedValueOnce([registeredEvent])
.mockResolvedValueOnce([poolCreatedEvent, registeredEvent])
.mockResolvedValue([]);

eventsProcessorSpy.mockResolvedValue([]);
Expand All @@ -526,6 +508,9 @@ describe("Orchestrator", { sequential: true }, () => {
vi.spyOn(mockEventsRegistry, "saveLastProcessedEvent").mockImplementation(() => {
return Promise.resolve();
});
vi.spyOn(mockStrategyRegistry, "saveStrategyId").mockImplementation(() => {
return Promise.resolve();
});

runPromise = orchestrator.run(abortController.signal);

Expand Down

0 comments on commit 9223013

Please sign in to comment.