Skip to content

Commit

Permalink
feat: don't call pricing provider if amount is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnigir1 committed Nov 19, 2024
1 parent 11d422b commit 7ad91df
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,30 @@ export class DGLiteAllocatedHandler implements IEventHandler<"Strategy", "Alloca
const token = getTokenOrThrow(this.chainId, tokenAddress);
const matchToken = getTokenOrThrow(this.chainId, round.matchTokenAddress);

const { amountInUsd } = await getTokenAmountInUsd(
this.dependencies.pricingProvider,
token,
amount,
this.event.blockTimestamp,
);
let amountInUsd = "0";
let amountInRoundMatchToken = 0n;

if (amount > 0) {
const { amountInUsd: amountInUsdString } = await getTokenAmountInUsd(
this.dependencies.pricingProvider,
token,
amount,
this.event.blockTimestamp,
);
amountInUsd = amountInUsdString;

let amountInRoundMatchToken: bigint | null = null;
amountInRoundMatchToken =
matchToken.address === token.address
? amount
: (
await getUsdInTokenAmount(
this.dependencies.pricingProvider,
matchToken,
amountInUsd,
this.event.blockTimestamp,
)
).amount;
amountInRoundMatchToken =
matchToken.address === token.address
? amount
: (
await getUsdInTokenAmount(
this.dependencies.pricingProvider,
matchToken,
amountInUsd,
this.event.blockTimestamp,
)
).amount;
}

const timestamp = this.event.blockTimestamp;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,60 @@ describe("DGLiteAllocatedHandler", () => {
});
});

it("doesn't fetch token price if amount is 0", async () => {
mockEvent = createMockEvent(eventName, defaultParams, defaultStrategyId, {
params: { amount: "0" },
});
const mockRound = {
id: "round1",
matchTokenAddress: "0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1",
matchAmount: BigInt(0),
matchAmountInUsd: "0",
fundedAmount: BigInt(0),
fundedAmountInUsd: "0",
totalAmountDonatedInUsd: "0",
totalDonationsCount: 0,
uniqueDonorsCount: 0,
tags: [],
} as unknown as Round;

const mockApplication = {
id: "app1",
projectId: "project1",
} as unknown as Application;

vi.spyOn(mockRoundRepository, "getRoundByStrategyAddressOrThrow").mockResolvedValue(
mockRound,
);
vi.spyOn(
mockApplicationRepository,
"getApplicationByAnchorAddressOrThrow",
).mockResolvedValue(mockApplication);

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

const result = await handler.handle();
const changeset = result[0] as {
type: "InsertApplicationPayout";
args: {
applicationPayout: {
amount: bigint;
amountInUsd: string;
amountInRoundMatchToken: bigint;
};
};
};

expect(mockPricingProvider.getTokenPrice).not.toHaveBeenCalled();
expect(changeset.args.applicationPayout.amount).toBe(0n);
expect(changeset.args.applicationPayout.amountInUsd).toBe("0");
expect(changeset.args.applicationPayout.amountInRoundMatchToken).toBe(0n);
});

it("throws RoundNotFound if round is not found", async () => {
mockEvent = createMockEvent(eventName, defaultParams, defaultStrategyId);
vi.spyOn(mockRoundRepository, "getRoundByStrategyAddressOrThrow").mockRejectedValue(
Expand Down

0 comments on commit 7ad91df

Please sign in to comment.