Skip to content

Commit

Permalink
feat: write XXOrThrow methods on repository
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnigir1 committed Nov 13, 2024
1 parent b442db8 commit 4187c7c
Show file tree
Hide file tree
Showing 28 changed files with 384 additions and 407 deletions.
4 changes: 0 additions & 4 deletions packages/processors/src/exceptions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@ export * from "./tokenPriceNotFound.exception.js";
export * from "./unsupportedEvent.exception.js";
export * from "./invalidArgument.exception.js";
export * from "./unsupportedStrategy.exception.js";
export * from "./projectNotFound.exception.js";
export * from "./roundNotFound.exception.js";
export * from "./applicationNotFound.exception.js";
export * from "./unknownToken.exception.js";
export * from "./metadataParsingFailed.exception.js";
export * from "./metadataNotFound.exception.js";
7 changes: 0 additions & 7 deletions packages/processors/src/exceptions/unknownToken.exception.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { Address, getAddress } from "viem";
import { getAddress } from "viem";

import { Application, Changeset, Round } from "@grants-stack-indexer/repository";
import { Changeset } from "@grants-stack-indexer/repository";
import { ChainId, ProcessorEvent } from "@grants-stack-indexer/shared";

import {
ApplicationNotFound,
IEventHandler,
ProcessorDependencies,
RoundNotFound,
} from "../../internal.js";
import { IEventHandler, ProcessorDependencies } from "../../internal.js";

type Dependencies = Pick<
ProcessorDependencies,
Expand Down Expand Up @@ -45,12 +40,21 @@ export class BaseFundsDistributedHandler implements IEventHandler<"Strategy", "F
* 2. IncrementRoundTotalDistributed: Increments the total distributed amount for a round.
*/
async handle(): Promise<Changeset[]> {
const { roundRepository, applicationRepository } = this.dependencies;

const strategyAddress = getAddress(this.event.srcAddress);
const round = await this.getRoundOrThrow(strategyAddress);
const round = await roundRepository.getRoundByStrategyAddressOrThrow(
this.chainId,
strategyAddress,
);

const roundId = round.id;
const anchorAddress = getAddress(this.event.params.recipientId);
const application = await this.getApplicationOrThrow(roundId, anchorAddress);
const application = await applicationRepository.getApplicationByAnchorAddressOrThrow(
this.chainId,
roundId,
anchorAddress,
);

return [
{
Expand All @@ -74,49 +78,4 @@ export class BaseFundsDistributedHandler implements IEventHandler<"Strategy", "F
},
];
}

/**
* Retrieves a round by its strategy address.
* @param {Address} strategyAddress - The address of the strategy.
* @returns {Promise<Round>} The round found.
* @throws {RoundNotFound} if the round does not exist.
*/
private async getRoundOrThrow(strategyAddress: Address): Promise<Round> {
const { roundRepository } = this.dependencies;
const round = await roundRepository.getRoundByStrategyAddress(
this.chainId,
strategyAddress,
);

if (!round) {
throw new RoundNotFound(this.chainId, strategyAddress);
}

return round;
}

/**
* Retrieves an application by its round ID and recipient address.
* @param {string} roundId - The ID of the round.
* @param {Address} recipientId - The address of the recipient.
* @returns {Promise<Application>} The application found.
* @throws {ApplicationNotFound} if the application does not exist.
*/
private async getApplicationOrThrow(
roundId: string,
anchorAddress: Address,
): Promise<Application> {
const { applicationRepository } = this.dependencies;
const application = await applicationRepository.getApplicationByAnchorAddress(
this.chainId,
roundId,
anchorAddress,
);

if (!application) {
throw new ApplicationNotFound(this.chainId, roundId, anchorAddress);
}

return application;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import StatusesBitmap from "statuses-bitmap";
import { Address, getAddress } from "viem";
import { getAddress } from "viem";

import { Application, Changeset, Round } from "@grants-stack-indexer/repository";
import { Application, Changeset } from "@grants-stack-indexer/repository";
import { ChainId, ProcessorEvent } from "@grants-stack-indexer/shared";

import {
ApplicationStatus,
IEventHandler,
ProcessorDependencies,
RoundNotFound,
} from "../../internal.js";
import { ApplicationStatus, IEventHandler, ProcessorDependencies } from "../../internal.js";
import { createStatusUpdate, isValidApplicationStatus } from "../helpers/index.js";

type Dependencies = Pick<
Expand Down Expand Up @@ -52,7 +47,13 @@ export class BaseRecipientStatusUpdatedHandler
* @returns An array of changesets to update application statuses.
*/
async handle(): Promise<Changeset[]> {
const round = await this.getRoundOrThrow(this.event.srcAddress);
const { roundRepository } = this.dependencies;

const strategyAddress = getAddress(this.event.srcAddress);
const round = await roundRepository.getRoundByStrategyAddressOrThrow(
this.chainId,
strategyAddress,
);

const applicationsToUpdate = await this.getApplicationsToUpdate(round.id);

Expand Down Expand Up @@ -108,26 +109,4 @@ export class BaseRecipientStatusUpdatedHandler

return applications;
}

/**
* Retrieves a round by its strategy address
* @param address - The address of the strategy
* @throws RoundNotFound if the round is not found
*/
private async getRoundOrThrow(address: Address): Promise<Round> {
const normalizedAddress = getAddress(address);
const round = await this.dependencies.roundRepository.getRoundByStrategyAddress(
this.chainId,
normalizedAddress,
);

if (!round) {
this.dependencies.logger.warn(
`RecipientStatusUpdated: Round not found for strategy address ${normalizedAddress}`,
);
throw new RoundNotFound(this.chainId, normalizedAddress);
}

return round;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { Address, encodePacked, getAddress, keccak256 } from "viem";
import { encodePacked, getAddress, keccak256 } from "viem";

import { Application, Changeset, Donation, Round } from "@grants-stack-indexer/repository";
import { ChainId, getToken, ProcessorEvent, Token } from "@grants-stack-indexer/shared";
import { Changeset, Donation } from "@grants-stack-indexer/repository";
import { ChainId, getTokenOrThrow, ProcessorEvent } from "@grants-stack-indexer/shared";

import { getTokenAmountInUsd, getUsdInTokenAmount } from "../../../helpers/index.js";
import {
ApplicationNotFound,
IEventHandler,
MetadataParsingFailed,
ProcessorDependencies,
RoundNotFound,
UnknownToken,
} from "../../../internal.js";
import { IEventHandler, MetadataParsingFailed, ProcessorDependencies } from "../../../internal.js";
import { ApplicationMetadata, ApplicationMetadataSchema } from "../../../schemas/index.js";

type Dependencies = Pick<
Expand Down Expand Up @@ -46,16 +39,24 @@ export class DVMDAllocatedHandler implements IEventHandler<"Strategy", "Allocate
* @throws {MetadataParsingFailed} if the metadata is invalid
*/
async handle(): Promise<Changeset[]> {
const { roundRepository, applicationRepository } = this.dependencies;
const { srcAddress } = this.event;
const { recipientId: _recipientId, amount, token: _token } = this.event.params;

const round = await this.getRoundOrThrow(srcAddress);
const application = await this.getApplicationOrThrow(round.id, _recipientId);
const round = await roundRepository.getRoundByStrategyAddressOrThrow(
this.chainId,
getAddress(srcAddress),
);
const application = await applicationRepository.getApplicationByAnchorAddressOrThrow(
this.chainId,
round.id,
getAddress(_recipientId),
);

const donationId = this.getDonationId(this.event.blockNumber, this.event.logIndex);

const token = this.getTokenOrThrow(_token);
const matchToken = this.getTokenOrThrow(round.matchTokenAddress);
const token = getTokenOrThrow(this.chainId, _token);
const matchToken = getTokenOrThrow(this.chainId, round.matchTokenAddress);

const { amountInUsd, timestamp: priceTimestamp } = await getTokenAmountInUsd(
this.dependencies.pricingProvider,
Expand Down Expand Up @@ -103,71 +104,13 @@ export class DVMDAllocatedHandler implements IEventHandler<"Strategy", "Allocate
];
}

/**
* Retrieves a round by its strategy address.
* @param {Address} strategyAddress - The address of the strategy.
* @returns {Promise<Round>} The round found.
* @throws {RoundNotFound} if the round does not exist.
*/
private async getRoundOrThrow(strategyAddress: Address): Promise<Round> {
const normalizedStrategyAddress = getAddress(strategyAddress);
const round = await this.dependencies.roundRepository.getRoundByStrategyAddress(
this.chainId,
normalizedStrategyAddress,
);

if (!round) {
throw new RoundNotFound(this.chainId, normalizedStrategyAddress);
}

return round;
}

/**
* Retrieves an application by its round ID and recipient address.
* @param {string} roundId - The ID of the round.
* @param {Address} recipientId - The address of the recipient.
* @returns {Promise<Application>} The application found.
* @throws {ApplicationNotFound} if the application does not exist.
*/
private async getApplicationOrThrow(
roundId: string,
recipientId: Address,
): Promise<Application> {
const normalizedRecipientId = getAddress(recipientId);
const application =
await this.dependencies.applicationRepository.getApplicationByAnchorAddress(
this.chainId,
roundId,
normalizedRecipientId,
);

if (!application) {
throw new ApplicationNotFound(this.chainId, roundId, normalizedRecipientId);
}

return application;
}

/**
* DONATION_ID = keccak256(abi.encodePacked(blockNumber, "-", logIndex));
*/
private getDonationId(blockNumber: number, logIndex: number): string {
return keccak256(encodePacked(["string"], [`${blockNumber}-${logIndex}`]));
}

/**
* Retrieves a token by its address and chain ID.
* @param {Address} tokenAddress - The address of the token.
* @returns {Token} The token found.
* @throws {UnknownToken} if the token does not exist.
*/
private getTokenOrThrow(tokenAddress: Address): Token {
const token = getToken(this.chainId, getAddress(tokenAddress));
if (!token) throw new UnknownToken(tokenAddress, this.chainId);
return token;
}

/**
* Parses the application metadata.
* @param {unknown} metadata - The metadata to parse.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ import { getAddress } from "viem";
import { Changeset, NewApplication } from "@grants-stack-indexer/repository";
import { ChainId, ProcessorEvent } from "@grants-stack-indexer/shared";

import {
IEventHandler,
ProcessorDependencies,
ProjectNotFound,
RoundNotFound,
} from "../../../internal.js";
import { IEventHandler, ProcessorDependencies } from "../../../internal.js";
import { decodeDVMDApplicationData } from "../helpers/index.js";

type Dependencies = Pick<
Expand Down Expand Up @@ -41,22 +36,17 @@ export class DVMDRegisteredHandler implements IEventHandler<"Strategy", "Registe
const { blockNumber, blockTimestamp } = this.event;

const anchorAddress = getAddress(recipientId);
const project = await projectRepository.getProjectByAnchor(this.chainId, anchorAddress);

if (!project) {
throw new ProjectNotFound(this.chainId, anchorAddress);
}
const project = await projectRepository.getProjectByAnchorOrThrow(
this.chainId,
anchorAddress,
);

const strategyAddress = getAddress(this.event.srcAddress);
const round = await roundRepository.getRoundByStrategyAddress(
const round = await roundRepository.getRoundByStrategyAddressOrThrow(
this.chainId,
strategyAddress,
);

if (!round) {
throw new RoundNotFound(this.chainId, strategyAddress);
}

const values = decodeDVMDApplicationData(encodedData);
// ID is defined as recipientsCounter - 1, which is a value emitted by the strategy
const id = (Number(values.recipientsCounter) - 1).toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { ChainId, ProcessorEvent } from "@grants-stack-indexer/shared";

import type { IEventHandler, ProcessorDependencies } from "../../../internal.js";
import { getDateFromTimestamp } from "../../../helpers/index.js";
import { RoundNotFound } from "../../../internal.js";

type Dependencies = Pick<ProcessorDependencies, "roundRepository">;

Expand Down Expand Up @@ -36,15 +35,11 @@ export class DVMDTimestampsUpdatedHandler
*/
async handle(): Promise<Changeset[]> {
const strategyAddress = getAddress(this.event.srcAddress);
const round = await this.dependencies.roundRepository.getRoundByStrategyAddress(
const round = await this.dependencies.roundRepository.getRoundByStrategyAddressOrThrow(
this.chainId,
strategyAddress,
);

if (!round) {
throw new RoundNotFound(this.chainId, strategyAddress);
}

const {
registrationStartTime,
registrationEndTime,
Expand Down
Loading

0 comments on commit 4187c7c

Please sign in to comment.