Skip to content

Commit

Permalink
test: add pool created handler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnigir1 committed Oct 16, 2024
1 parent a89704c commit 40efd36
Show file tree
Hide file tree
Showing 11 changed files with 718 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/processors/src/allo/allo.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class AlloProcessor implements IProcessor<"Allo", AlloEvent> {
private readonly roundRepository: IRoundReadRepository,
) {}

process(event: ProtocolEvent<"Allo", AlloEvent>): Promise<Changeset[]> {
async process(event: ProtocolEvent<"Allo", AlloEvent>): Promise<Changeset[]> {
switch (event.eventName) {
case "PoolCreated":
return new PoolCreatedHandler(event, this.chainId, {
Expand All @@ -28,7 +28,7 @@ export class AlloProcessor implements IProcessor<"Allo", AlloEvent> {
roundRepository: this.roundRepository,
}).handle();
default:
throw new Error("Unknown event name");
throw new Error(`Unknown event name: ${event.eventName}`);
}
}
}
24 changes: 16 additions & 8 deletions packages/processors/src/allo/handlers/poolCreated.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import {

import type { IMetadataProvider } from "@grants-stack-indexer/metadata";
import type { IPricingProvider } from "@grants-stack-indexer/pricing";
import type { Changeset, IRoundReadRepository, NewRound } from "@grants-stack-indexer/repository";
import type {
Changeset,
IRoundReadRepository,
NewRound,
PendingRoundRole,
} from "@grants-stack-indexer/repository";
import type { ChainId, ProtocolEvent } from "@grants-stack-indexer/shared";
import { isAlloNativeToken } from "@grants-stack-indexer/shared/";

Expand Down Expand Up @@ -222,6 +227,7 @@ export class PoolCreatedHandler implements IEventHandler<"Allo", "PoolCreated">
*/
private async handlePendingRoles(chainId: ChainId, roundId: string): Promise<Changeset[]> {
const changes: Changeset[] = [];
const allPendingRoles: PendingRoundRole[] = [];

for (const roleName of ["admin", "manager"] as const) {
const pendingRoles = await this.roundRepository.getPendingRoundRoles(chainId, roleName);
Expand All @@ -239,13 +245,15 @@ export class PoolCreatedHandler implements IEventHandler<"Allo", "PoolCreated">
},
});
}
allPendingRoles.push(...pendingRoles);
}

if (pendingRoles.length > 0) {
changes.push({
type: "DeletePendingRoundRoles",
args: { ids: pendingRoles.map((r) => r.id!) },
});
}
const pendingRoleIds = [...new Set(allPendingRoles.map((r) => r.id!))];
if (pendingRoleIds.length > 0) {
changes.push({
type: "DeletePendingRoundRoles",
args: { ids: pendingRoleIds },
});
}

return changes;
Expand All @@ -267,6 +275,6 @@ export class PoolCreatedHandler implements IEventHandler<"Allo", "PoolCreated">
throw new Error("Token price not found");
}

return calculateAmountInUsd(amount, tokenPrice, token.decimals);
return calculateAmountInUsd(amount, tokenPrice.priceUsd, token.decimals);
}
}
6 changes: 2 additions & 4 deletions packages/processors/src/helpers/tokenMath.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { formatUnits, parseUnits } from "viem";

import { TokenPrice } from "@grants-stack-indexer/pricing";

/**
* Calculates the amount in USD
* @param amount - The amount to convert to USD
Expand All @@ -12,13 +10,13 @@ import { TokenPrice } from "@grants-stack-indexer/pricing";
*/
export const calculateAmountInUsd = (
amount: bigint,
tokenPrice: TokenPrice,
tokenPriceInUsd: number,
tokenDecimals: number,
truncateDecimals?: number,
): number => {
const amountInUsd = Number(
formatUnits(
amount * parseUnits(tokenPrice.priceUsd.toString(), tokenDecimals),
amount * parseUnits(tokenPriceInUsd.toString(), tokenDecimals),
tokenDecimals * 2,
),
);
Expand Down
76 changes: 76 additions & 0 deletions packages/processors/test/allo/allo.processor.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Chain, PublicClient, Transport } from "viem";
import { beforeEach, describe, expect, it, vi } from "vitest";

import type { IMetadataProvider } from "@grants-stack-indexer/metadata";
import type { IPricingProvider } from "@grants-stack-indexer/pricing";
import type { IRoundReadRepository } from "@grants-stack-indexer/repository";
import type { AlloEvent, ChainId, ProtocolEvent } from "@grants-stack-indexer/shared";

import { AlloProcessor } from "../../src/allo/allo.processor.js";
import { PoolCreatedHandler } from "../../src/allo/handlers/poolCreated.handler.js";

// Mock the handlers
vi.mock("../../src/allo/handlers/poolCreated.handler.js", () => {
const PoolCreatedHandler = vi.fn();
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
PoolCreatedHandler.prototype.handle = vi.fn();
return {
PoolCreatedHandler,
};
});

describe("AlloProcessor", () => {
const mockChainId = 10 as ChainId;
let processor: AlloProcessor;
let mockViemClient: PublicClient<Transport, Chain>;
let mockPricingProvider: IPricingProvider;
let mockMetadataProvider: IMetadataProvider;
let mockRoundRepository: IRoundReadRepository;

beforeEach(() => {
mockViemClient = {} as PublicClient<Transport, Chain>;
mockPricingProvider = {} as IPricingProvider;
mockMetadataProvider = {} as IMetadataProvider;
mockRoundRepository = {} as IRoundReadRepository;

processor = new AlloProcessor(
mockChainId,
mockViemClient,
mockPricingProvider,
mockMetadataProvider,
mockRoundRepository,
);

// Reset mocks before each test
vi.clearAllMocks();
});

it("call PoolCreatedHandler for PoolCreated event", async () => {
const mockEvent: ProtocolEvent<"Allo", "PoolCreated"> = {
eventName: "PoolCreated",
// Add other necessary event properties here
} as ProtocolEvent<"Allo", "PoolCreated">;

vi.spyOn(PoolCreatedHandler.prototype, "handle").mockResolvedValue([]);

await processor.process(mockEvent);

expect(PoolCreatedHandler).toHaveBeenCalledWith(mockEvent, mockChainId, {
viemClient: mockViemClient,
pricingProvider: mockPricingProvider,
metadataProvider: mockMetadataProvider,
roundRepository: mockRoundRepository,
});
expect(PoolCreatedHandler.prototype.handle).toHaveBeenCalled();
});

it("throw an error for unknown event names", async () => {
const mockEvent = {
eventName: "UnknownEvent",
} as unknown as ProtocolEvent<"Allo", AlloEvent>;

await expect(() => processor.process(mockEvent)).rejects.toThrow(
"Unknown event name: UnknownEvent",
);
});
});
Loading

0 comments on commit 40efd36

Please sign in to comment.