Skip to content

Commit

Permalink
Stores standard pool reserves as nominal token values, and cleans up …
Browse files Browse the repository at this point in the history
…tests. (#199)
  • Loading branch information
jfarid27 authored Nov 19, 2024
1 parent 92361f5 commit e691c35
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 42 deletions.
14 changes: 8 additions & 6 deletions src/EventHandlers/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,33 @@ Pool.Fees.handlerWithLoader({
const { liquidityPool, token0Instance, token1Instance } = loaderReturn;

let tokenUpdateData = {
totalFees0: 0n,
totalFees1: 0n,
totalFees0: event.params.amount0,
totalFees1: event.params.amount1,
totalFeesNormalized0: 0n,
totalFeesNormalized1: 0n,
totalFeesUSD: 0n,
};

tokenUpdateData.totalFees0 = event.params.amount0
if (token0Instance) {
tokenUpdateData.totalFees0 = normalizeTokenAmountTo1e18(
tokenUpdateData.totalFeesNormalized0 = normalizeTokenAmountTo1e18(
event.params.amount0,
Number(token0Instance.decimals)
);
tokenUpdateData.totalFeesUSD += multiplyBase1e18(
tokenUpdateData.totalFees0,
tokenUpdateData.totalFeesNormalized0,
token0Instance.pricePerUSDNew
);
}

tokenUpdateData.totalFees1 = event.params.amount1
if (token1Instance) {
tokenUpdateData.totalFees1 = normalizeTokenAmountTo1e18(
tokenUpdateData.totalFeesNormalized1 = normalizeTokenAmountTo1e18(
event.params.amount1,
Number(token1Instance.decimals)
);
tokenUpdateData.totalFeesUSD += multiplyBase1e18(
tokenUpdateData.totalFees1,
tokenUpdateData.totalFeesNormalized1,
token1Instance.pricePerUSDNew
);
}
Expand Down
13 changes: 7 additions & 6 deletions test/EventHandlers/CLPool/CLPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ describe("CLPool Event Handlers", () => {
expectations.totalLiquidityUSD =
(mockLiquidityPoolData.reserve0 - expectations.amount0In) * mockToken0Data.pricePerUSDNew / ( 10n ** (mockToken0Data.decimals) ) +
(mockLiquidityPoolData.reserve1 - expectations.amount1In) * mockToken1Data.pricePerUSDNew / ( 10n ** (mockToken1Data.decimals) );

expectations.totalFeesUSD =
mockLiquidityPoolData.totalFeesUSD +
(expectations.amount0In / 10n ** (mockToken0Data.decimals) ) * mockToken0Data.pricePerUSDNew +
(expectations.amount1In / 10n ** (mockToken1Data.decimals) ) * mockToken1Data.pricePerUSDNew;

beforeEach(() => {
mockEventData = {
Expand Down Expand Up @@ -336,12 +341,8 @@ describe("CLPool Event Handlers", () => {
});

it("should correctly update total fees in USD", async () => {
expect(diff.totalFeesUSD).to.equal(
mockLiquidityPoolData.totalFeesUSD +
(expectations.amount0In * mockToken0Data.pricePerUSDNew) / 10n ** 18n +
(expectations.amount1In / 10n ** 6n) * mockToken1Data.pricePerUSDNew,
"It should correctly update total fees in USD"
);
expect(diff.totalFeesUSD).to.equal(expectations.totalFeesUSD,
"It should correctly update total fees in USD");
});
describe("CLPool Aggregator", () => {
let diff: any;
Expand Down
76 changes: 46 additions & 30 deletions test/EventHandlers/Pool/Fees.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,71 @@ import { setupCommon } from "./common";
import { TEN_TO_THE_18_BI, TEN_TO_THE_6_BI, toChecksumAddress, TokenIdByChain } from "../../../src/Constants";

describe("Pool Fees Event", () => {
const { mockToken0Data, mockToken1Data } = setupCommon();
const { mockToken0Data, mockToken1Data, mockLiquidityPoolData } = setupCommon();
const poolId = mockLiquidityPoolData.id;

let mockDb: any;
beforeEach(() => {
mockDb = MockDb.createMockDb();
});
let updatedDB: any;
let expectations: any = {
amount0In: 3n * 10n ** 18n,
amount1In: 2n * 10n ** 6n,
totalLiquidityUSD: 0n,
};

it("should update LiquidityPool entity with new fees", async () => {
const poolId = toChecksumAddress("0x3333333333333333333333333333333333333333");
const mockLiquidityPool: LiquidityPoolAggregator = {
id: poolId,
chainId: 10,
token0_id: TokenIdByChain("0x1111111111111111111111111111111111111111", 10),
token1_id: TokenIdByChain("0x2222222222222222222222222222222222222222", 10),
token0_address: "0x1111111111111111111111111111111111111111",
token1_address: "0x2222222222222222222222222222222222222222",
totalFees0: 0n,
totalFees1: 0n,
totalFeesUSD: 0n,
} as LiquidityPoolAggregator;

const updatedDB1 = mockDb.entities.Token.set(mockToken0Data as Token);
const updatedDB2 = updatedDB1.entities.Token.set(mockToken1Data as Token);
const updatedDB3 = updatedDB2.entities.LiquidityPoolAggregator.set(mockLiquidityPool);
expectations.totalLiquidityUSD =
(mockLiquidityPoolData.reserve0 - expectations.amount0In) * mockToken0Data.pricePerUSDNew / ( 10n ** (mockToken0Data.decimals) ) +
(mockLiquidityPoolData.reserve1 - expectations.amount1In) * mockToken1Data.pricePerUSDNew / ( 10n ** (mockToken1Data.decimals) );

expectations.totalFeesUSD =
mockLiquidityPoolData.totalFeesUSD +
(expectations.amount0In / 10n ** (mockToken0Data.decimals) ) * mockToken0Data.pricePerUSDNew +
(expectations.amount1In / 10n ** (mockToken1Data.decimals) ) * mockToken1Data.pricePerUSDNew;

let updatedPool: any;

beforeEach(async () => {
mockDb = MockDb.createMockDb();
updatedDB = mockDb.entities.Token.set(mockToken0Data as Token);
updatedDB = updatedDB.entities.Token.set(mockToken1Data as Token);
updatedDB = updatedDB.entities.LiquidityPoolAggregator.set(mockLiquidityPoolData);

const mockEvent = Pool.Fees.createMockEvent({
amount0: 100n * TEN_TO_THE_18_BI,
amount1: 200n * TEN_TO_THE_6_BI,
amount0: expectations.amount0In,
amount1: expectations.amount1In,
mockEventData: {
block: {
number: 123456,
timestamp: 1000000,
hash: "0x1234567890123456789012345678901234567890123456789012345678901234",
},
chainId: 10,
srcAddress: toChecksumAddress("0x3333333333333333333333333333333333333333"),
srcAddress: poolId,
},
});

const result = await Pool.Fees.processEvent({
event: mockEvent,
mockDb: updatedDB3,
mockDb: updatedDB,
});

const updatedPool = result.entities.LiquidityPoolAggregator.get(poolId);
updatedPool = result.entities.LiquidityPoolAggregator.get(poolId);
});

it("should update LiquidityPoolAggregator", async () => {
expect(updatedPool).to.not.be.undefined;
expect(updatedPool?.totalFees0).to.equal(100n * TEN_TO_THE_18_BI);
expect(updatedPool?.totalFees1).to.equal(200n * TEN_TO_THE_18_BI);
expect(updatedPool?.totalFeesUSD).to.equal(300n * TEN_TO_THE_18_BI); // 100 + 200
expect(updatedPool?.lastUpdatedTimestamp).to.deep.equal(new Date(1000000 * 1000));
expect(updatedPool.lastUpdatedTimestamp).to.deep.equal(new Date(1000000 * 1000));
});

it("should update LiquidityPoolAggregator nominal fees", async () => {
expect(updatedPool.totalFees0).to.equal(
mockLiquidityPoolData.totalFees0 + expectations.amount0In
);
expect(updatedPool.totalFees1).to.equal(
mockLiquidityPoolData.totalFees1 + expectations.amount1In
);
});

it("should update LiquidityPoolAggregator total fees in USD", async () => {
expect(updatedPool.totalFeesUSD).to.equal(expectations.totalFeesUSD);
});
});

0 comments on commit e691c35

Please sign in to comment.