From 9223013d93cbf362996699d7701917a1f39a170c Mon Sep 17 00:00:00 2001 From: nigiri <168690269+0xnigir1@users.noreply.github.com> Date: Fri, 13 Dec 2024 19:56:12 -0300 Subject: [PATCH] feat: improve logic on saving strategy id --- .../unit/sharedDependencies.service.spec.ts | 2 +- packages/data-flow/src/orchestrator.ts | 11 +--- .../data-flow/test/unit/orchestrator.spec.ts | 65 +++++++------------ 3 files changed, 28 insertions(+), 50 deletions(-) diff --git a/apps/processing/test/unit/sharedDependencies.service.spec.ts b/apps/processing/test/unit/sharedDependencies.service.spec.ts index 437c27e..9d309b9 100644 --- a/apps/processing/test/unit/sharedDependencies.service.spec.ts +++ b/apps/processing/test/unit/sharedDependencies.service.spec.ts @@ -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(), diff --git a/packages/data-flow/src/orchestrator.ts b/packages/data-flow/src/orchestrator.ts index b648cbd..7b41b3c 100644 --- a/packages/data-flow/src/orchestrator.ts +++ b/packages/data-flow/src/orchestrator.ts @@ -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, - ); } } diff --git a/packages/data-flow/test/unit/orchestrator.spec.ts b/packages/data-flow/test/unit/orchestrator.spec.ts index a0c4a08..0412e5a 100644 --- a/packages/data-flow/test/unit/orchestrator.spec.ts +++ b/packages/data-flow/test/unit/orchestrator.spec.ts @@ -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([]); @@ -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); @@ -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, { @@ -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); @@ -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, ); }); @@ -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([]); @@ -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);