Skip to content

Commit

Permalink
fix: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnigir1 committed Nov 12, 2024
1 parent 108470d commit 4c46e0c
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 47 deletions.
1 change: 1 addition & 0 deletions packages/processors/src/exceptions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ 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";
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class MetadataParsingFailed extends Error {
constructor(additionalInfo?: string) {
super(`Failed to parse application metadata: ${additionalInfo}`);
}
}
2 changes: 1 addition & 1 deletion packages/processors/src/helpers/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IPricingProvider } from "@grants-stack-indexer/pricing";
import { Token } from "@grants-stack-indexer/shared";

import { TokenPriceNotFoundError } from "../internal.js";
import { calculateAmountInToken, calculateAmountInUsd } from "./tokenMath.js";
import { calculateAmountInToken, calculateAmountInUsd } from "./index.js";

// sometimes coingecko returns no prices for 1 hour range, 2 hours works better
const TIMESTAMP_DELTA_RANGE = 2 * 60 * 60 * 1000;
Expand Down
8 changes: 7 additions & 1 deletion packages/processors/src/helpers/tokenMath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const calculateAmountInUsd = (
* @param tokenPriceInUsd - The price of the token in USD
* @param tokenDecimals - The number of decimals the token has
* @returns The amount in token
* @throws Error if tokenPriceInUsd is 0 (division by zero)
*/
export const calculateAmountInToken = (
amountInUSD: string,
Expand All @@ -49,5 +50,10 @@ export const calculateAmountInToken = (
const tokenPriceInUsdBN = new BigNumber(tokenPriceInUsd);
const scaleFactor = new BigNumber(10).pow(tokenDecimals);

return BigInt(amountInUsdBN.multipliedBy(scaleFactor).dividedBy(tokenPriceInUsdBN).toFixed(0));
return BigInt(
amountInUsdBN
.multipliedBy(scaleFactor)
.dividedBy(tokenPriceInUsdBN)
.toFixed(0, BigNumber.ROUND_FLOOR),
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getTokenAmountInUsd, getUsdInTokenAmount } from "../../../helpers/index
import {
ApplicationNotFound,
IEventHandler,
MetadataParsingFailed,
ProcessorDependencies,
RoundNotFound,
UnknownToken,
Expand All @@ -18,18 +19,6 @@ type Dependencies = Pick<
"roundRepository" | "applicationRepository" | "pricingProvider"
>;

export class OriginMissing extends Error {
constructor() {
super("Origin field is required");
}
}

export class MetadataParsingFailed extends Error {
constructor(additionalInfo?: string) {
super(`Failed to parse application metadata: ${additionalInfo}`);
}
}

/**
* Handles the Allocated event for the Donation Voting Merkle Distribution Direct Transfer strategy.
*
Expand Down Expand Up @@ -60,10 +49,6 @@ export class DVMDAllocatedHandler implements IEventHandler<"Strategy", "Allocate
const { srcAddress } = this.event;
const { recipientId: _recipientId, amount, token: _token } = this.event.params;

if (!this.event.params.origin) {
throw new OriginMissing();
}

const round = await this.getRoundOrThrow(srcAddress);
const application = await this.getApplicationOrThrow(round.id, _recipientId);

Expand Down
10 changes: 10 additions & 0 deletions packages/processors/test/helpers/tokenMath.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,14 @@ describe("calculateAmountInToken", () => {
it("handle scientific notation for USD amount", () => {
expect(calculateAmountInToken("1e3", "1", 18)).toBe(1000000000000000000000n);
});

it("truncates decimals to floor", () => {
// For 6 decimal token
expect(calculateAmountInToken("1.123456", "1", 6)).toBe(1123456n);
expect(calculateAmountInToken("1", "0.123456", 6)).toBe(8100051n); // 8100051.840331778 before truncation

// For 8 decimal token
expect(calculateAmountInToken("1.12345678", "1", 8)).toBe(112345678n);
expect(calculateAmountInToken("1", "0.12345678", 8)).toBe(810000066n); // 810000066.4200054 before truncation
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ import { ChainId, DeepPartial, mergeDeep, ProcessorEvent } from "@grants-stack-i

import {
ApplicationNotFound,
MetadataParsingFailed,
RoundNotFound,
TokenPriceNotFoundError,
UnknownToken,
} from "../../../../src/exceptions/index.js";
import {
DVMDAllocatedHandler,
MetadataParsingFailed,
OriginMissing,
} from "../../../../src/strategy/donationVotingMerkleDistributionDirectTransfer/handlers/allocated.handler.js";
import { DVMDAllocatedHandler } from "../../../../src/strategy/donationVotingMerkleDistributionDirectTransfer/handlers/allocated.handler.js";

function createMockEvent(
overrides: DeepPartial<ProcessorEvent<"Strategy", "AllocatedWithOrigin">> = {},
Expand Down Expand Up @@ -189,20 +186,6 @@ describe("DVMDAllocatedHandler", () => {
]);
});

it("throws OriginMissing if origin is undefined", async () => {
mockEvent = createMockEvent();
//forcefully remove origin from mockEvent
mockEvent.params.origin = undefined as unknown as `0x${string}`;

handler = new DVMDAllocatedHandler(mockEvent, chainId, {
roundRepository: mockRoundRepository,
applicationRepository: mockApplicationRepository,
pricingProvider: mockPricingProvider,
});

await expect(handler.handle()).rejects.toThrow(OriginMissing);
});

it("throws RoundNotFound if round is not found", async () => {
mockEvent = createMockEvent();
vi.spyOn(mockRoundRepository, "getRoundByStrategyAddress").mockResolvedValue(undefined);
Expand Down
14 changes: 4 additions & 10 deletions packages/repository/src/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,19 @@ export type {
PartialProject,
ProjectRole,
PendingProjectRole,
} from "./types/project.types.js";
} from "./types/index.js";

export type {
Round,
NewRound,
PartialRound,
RoundRole,
PendingRoundRole,
} from "./types/round.types.js";
export type { Round, NewRound, PartialRound, RoundRole, PendingRoundRole } from "./types/index.js";

export type {
ApplicationStatus,
StatusSnapshot,
Application,
NewApplication,
PartialApplication,
} from "./types/application.types.js";
} from "./types/index.js";

export type { Donation, NewDonation } from "./types/donation.types.js";
export type { Donation, NewDonation } from "./types/index.js";

export type {
Changeset,
Expand Down

0 comments on commit 4c46e0c

Please sign in to comment.