From 89d2a63d8d0e15ee0ab49db85fa5bc9ac8ddeead Mon Sep 17 00:00:00 2001 From: nigiri <168690269+0xnigir1@users.noreply.github.com> Date: Thu, 17 Oct 2024 18:39:24 -0300 Subject: [PATCH] fix: add specific exceptions --- packages/processors/src/allo/allo.processor.ts | 3 ++- packages/processors/src/exceptions/index.ts | 2 ++ .../src/exceptions/invalidArgument.exception.ts | 5 +++++ .../src/exceptions/unsupportedEvent.exception.ts | 10 ++++++++++ packages/processors/src/helpers/strategy.ts | 7 +++++-- packages/processors/src/helpers/tokenMath.ts | 5 ++++- packages/processors/test/allo/allo.processor.spec.ts | 5 ++--- packages/processors/test/helpers/tokenMath.spec.ts | 9 +++++++-- 8 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 packages/processors/src/exceptions/invalidArgument.exception.ts create mode 100644 packages/processors/src/exceptions/unsupportedEvent.exception.ts diff --git a/packages/processors/src/allo/allo.processor.ts b/packages/processors/src/allo/allo.processor.ts index 76f3e41..2a5ffed 100644 --- a/packages/processors/src/allo/allo.processor.ts +++ b/packages/processors/src/allo/allo.processor.ts @@ -2,6 +2,7 @@ import { Changeset } from "@grants-stack-indexer/repository"; import { AlloEvent, ChainId, ProtocolEvent } from "@grants-stack-indexer/shared"; import type { IProcessor, ProcessorDependencies } from "../internal.js"; +import { UnsupportedEventException } from "../internal.js"; import { PoolCreatedHandler } from "./handlers/index.js"; export class AlloProcessor implements IProcessor<"Allo", AlloEvent> { @@ -15,7 +16,7 @@ export class AlloProcessor implements IProcessor<"Allo", AlloEvent> { case "PoolCreated": return new PoolCreatedHandler(event, this.chainId, this.dependencies).handle(); default: - throw new Error(`Unknown event name: ${event.eventName}`); + throw new UnsupportedEventException("Allo", event.eventName); } } } diff --git a/packages/processors/src/exceptions/index.ts b/packages/processors/src/exceptions/index.ts index 4bb65ad..746c664 100644 --- a/packages/processors/src/exceptions/index.ts +++ b/packages/processors/src/exceptions/index.ts @@ -1 +1,3 @@ export * from "./tokenPriceNotFound.exception.js"; +export * from "./unsupportedEvent.exception.js"; +export * from "./invalidArgument.exception.js"; diff --git a/packages/processors/src/exceptions/invalidArgument.exception.ts b/packages/processors/src/exceptions/invalidArgument.exception.ts new file mode 100644 index 0000000..80ff703 --- /dev/null +++ b/packages/processors/src/exceptions/invalidArgument.exception.ts @@ -0,0 +1,5 @@ +export class InvalidArgument extends Error { + constructor(message: string) { + super(message); + } +} diff --git a/packages/processors/src/exceptions/unsupportedEvent.exception.ts b/packages/processors/src/exceptions/unsupportedEvent.exception.ts new file mode 100644 index 0000000..4760f12 --- /dev/null +++ b/packages/processors/src/exceptions/unsupportedEvent.exception.ts @@ -0,0 +1,10 @@ +import { ContractName } from "@grants-stack-indexer/shared"; + +export class UnsupportedEventException extends Error { + constructor( + contract: ContractName, + public readonly eventName: string, + ) { + super(`Event ${eventName} unsupported for ${contract} processor`); + } +} diff --git a/packages/processors/src/helpers/strategy.ts b/packages/processors/src/helpers/strategy.ts index 75bdd68..a71937c 100644 --- a/packages/processors/src/helpers/strategy.ts +++ b/packages/processors/src/helpers/strategy.ts @@ -143,6 +143,9 @@ export function extractStrategyFromId(_id: Address): Strategy | undefined { } //TODO: refactor this into the StrategyHandler when implemented +// see if we can use a common interface or abstract class for all strategies +// so we don't have to do this switch statement +// most of the strategies don't need to fetch anything and just return null for all the times export const getStrategyTimings = async ( evmProvider: EvmProvider, strategy: Strategy, @@ -169,7 +172,7 @@ export const getStrategyTimings = async ( /** * Gets the strategy data for the DonationVotingMerkleDistributionDirectTransferStrategy - * @param viemClient - The viem client + * @param evmProvider - The evm provider * @param strategyId - The address of the strategy * @returns The strategy data */ @@ -225,7 +228,7 @@ export const getDonationVotingMerkleDistributionDirectTransferStrategyTimings = /** * Gets the strategy data for the DirectGrantsStrategy - * @param viemClient - The viem client + * @param evmProvider - The evm provider * @param strategyAddress - The address of the strategy * @returns The strategy data */ diff --git a/packages/processors/src/helpers/tokenMath.ts b/packages/processors/src/helpers/tokenMath.ts index 037e1c2..e48da9b 100644 --- a/packages/processors/src/helpers/tokenMath.ts +++ b/packages/processors/src/helpers/tokenMath.ts @@ -1,11 +1,14 @@ import { BigNumber } from "@grants-stack-indexer/shared"; +import { InvalidArgument } from "../internal.js"; + /** * Calculates the amount in USD * @param amount - The amount to convert to USD * @param tokenPrice - The price of the token in USD * @param tokenDecimals - The number of decimals the token has * @param truncateDecimals (optional) - The number of decimals to truncate the final result to. Must be between 0 and 18. + * @throws InvalidArgumentException if truncateDecimals is not between 0 and 18 * @returns The amount in USD */ export const calculateAmountInUsd = ( @@ -22,7 +25,7 @@ export const calculateAmountInUsd = ( if (truncateDecimals !== undefined) { if (truncateDecimals < 0 || truncateDecimals > 18) { - throw new Error("Truncate decimals must be between 0 and 18"); + throw new InvalidArgument("Truncate decimals must be between 0 and 18"); } amountInUsd = amountInUsd.decimalPlaces(truncateDecimals); } diff --git a/packages/processors/test/allo/allo.processor.spec.ts b/packages/processors/test/allo/allo.processor.spec.ts index 5650eeb..173a2b3 100644 --- a/packages/processors/test/allo/allo.processor.spec.ts +++ b/packages/processors/test/allo/allo.processor.spec.ts @@ -11,6 +11,7 @@ import type { AlloEvent, ChainId, ProtocolEvent } from "@grants-stack-indexer/sh import { AlloProcessor } from "../../src/allo/allo.processor.js"; import { PoolCreatedHandler } from "../../src/allo/handlers/poolCreated.handler.js"; +import { UnsupportedEventException } from "../../src/internal.js"; // Mock the handlers vi.mock("../../src/allo/handlers/poolCreated.handler.js", () => { @@ -71,8 +72,6 @@ describe("AlloProcessor", () => { eventName: "UnknownEvent", } as unknown as ProtocolEvent<"Allo", AlloEvent>; - await expect(() => processor.process(mockEvent)).rejects.toThrow( - "Unknown event name: UnknownEvent", - ); + await expect(() => processor.process(mockEvent)).rejects.toThrow(UnsupportedEventException); }); }); diff --git a/packages/processors/test/helpers/tokenMath.spec.ts b/packages/processors/test/helpers/tokenMath.spec.ts index f15500d..3aee76c 100644 --- a/packages/processors/test/helpers/tokenMath.spec.ts +++ b/packages/processors/test/helpers/tokenMath.spec.ts @@ -2,6 +2,7 @@ import { parseGwei } from "viem"; import { describe, expect, it, test } from "vitest"; import { calculateAmountInUsd } from "../../src/helpers/tokenMath.js"; +import { InvalidArgument } from "../../src/internal.js"; describe("calculateAmountInUsd", () => { it("calculate USD amount for 18 decimal token with integer price", () => { @@ -102,8 +103,12 @@ describe("calculateAmountInUsd", () => { const tokenPriceInUsd = 100; const tokenDecimals = 18; - expect(() => calculateAmountInUsd(amount, tokenPriceInUsd, tokenDecimals, -1)).toThrow(); - expect(() => calculateAmountInUsd(amount, tokenPriceInUsd, tokenDecimals, 19)).toThrow(); + expect(() => calculateAmountInUsd(amount, tokenPriceInUsd, tokenDecimals, -1)).toThrow( + InvalidArgument, + ); + expect(() => calculateAmountInUsd(amount, tokenPriceInUsd, tokenDecimals, 19)).toThrow( + InvalidArgument, + ); }); test("migrated cases", () => {