Skip to content

Commit

Permalink
fix: add specific exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnigir1 committed Oct 17, 2024
1 parent 11c9bbe commit 89d2a63
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/processors/src/allo/allo.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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);
}
}
}
2 changes: 2 additions & 0 deletions packages/processors/src/exceptions/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from "./tokenPriceNotFound.exception.js";
export * from "./unsupportedEvent.exception.js";
export * from "./invalidArgument.exception.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class InvalidArgument extends Error {
constructor(message: string) {
super(message);
}
}
10 changes: 10 additions & 0 deletions packages/processors/src/exceptions/unsupportedEvent.exception.ts
Original file line number Diff line number Diff line change
@@ -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`);
}
}
7 changes: 5 additions & 2 deletions packages/processors/src/helpers/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down
5 changes: 4 additions & 1 deletion packages/processors/src/helpers/tokenMath.ts
Original file line number Diff line number Diff line change
@@ -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 = (
Expand All @@ -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);
}
Expand Down
5 changes: 2 additions & 3 deletions packages/processors/test/allo/allo.processor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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);
});
});
9 changes: 7 additions & 2 deletions packages/processors/test/helpers/tokenMath.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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", () => {
Expand Down

0 comments on commit 89d2a63

Please sign in to comment.