Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Price Tokens on Swap #229

Merged
merged 5 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"dev": "envio dev",
"codegen": "envio codegen",
"start": "ts-node generated/src/Index.bs.js",
"test": "mocha --recursive --require ts-node/register \"test/**/*.ts\" --timeout 30000",
"dottest": "mocha -w --recursive -R dot --require ts-node/register \"test/**/*.ts\" --timeout 30000",
"test": "mocha --recursive --R spec --require ts-node/register \"test/**/*.ts\" --timeout 30000",
"test-watch": "mocha -w --recursive --R dot --require ts-node/register \"test/**/*.ts\" --timeout 30000",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update test runners for easier dev.

"enable-hydra": "./hack/hydra-mode/hydra-mode.sh"
},
"devDependencies": {
Expand Down
38 changes: 16 additions & 22 deletions src/EventHandlers/CLPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
LiquidityPoolAggregator,
Token,
} from "generated";
import { set_whitelisted_prices } from "../PriceOracle";
import { refreshTokenPrice, set_whitelisted_prices } from "../PriceOracle";
import { normalizeTokenAmountTo1e18 } from "../Helpers";
import { multiplyBase1e18, abs } from "../Maths";
import { updateLiquidityPoolAggregator } from "../Aggregators/LiquidityPoolAggregator";
Expand Down Expand Up @@ -472,6 +472,7 @@ CLPool.Swap.handlerWithLoader({
return { liquidityPoolAggregator, token0Instance, token1Instance };
},
handler: async ({ event, context, loaderReturn }) => {
const blockDatetime = new Date(event.block.timestamp * 1000);
const entity: CLPool_Swap = {
id: `${event.chainId}_${event.block.number}_${event.logIndex}`,
sender: event.params.sender,
Expand All @@ -482,14 +483,16 @@ CLPool.Swap.handlerWithLoader({
liquidity: event.params.liquidity,
tick: event.params.tick,
sourceAddress: event.srcAddress,
timestamp: new Date(event.block.timestamp * 1000),
timestamp: blockDatetime,
chainId: event.chainId,
};

context.CLPool_Swap.set(entity);

if (loaderReturn && loaderReturn.liquidityPoolAggregator) {
const { liquidityPoolAggregator, token0Instance, token1Instance } = loaderReturn;
let token0 = token0Instance;
let token1 = token1Instance;

// Delta that will be added to the liquidity pool aggregator
let tokenUpdateData = {
Expand All @@ -503,25 +506,27 @@ CLPool.Swap.handlerWithLoader({
tokenUpdateData.netAmount0 = abs(event.params.amount0);
tokenUpdateData.netAmount1 = abs(event.params.amount1);

if (token0Instance) {
if (token0) {
token0 = await refreshTokenPrice(token0, event.block.number, event.block.timestamp, event.chainId, context);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calls refreshTokenPrice, forcing price fetch on swaps.

const normalizedAmount0 = normalizeTokenAmountTo1e18(
abs(event.params.amount0),
Number(token0Instance.decimals)
Number(token0.decimals)
);
tokenUpdateData.netVolumeToken0USD = multiplyBase1e18(
normalizedAmount0,
token0Instance.pricePerUSDNew
token0.pricePerUSDNew
);
}

if (token1Instance) {
if (token1) {
token1 = await refreshTokenPrice(token1, event.block.number, event.block.timestamp, event.chainId, context);
const normalizedAmount1 = normalizeTokenAmountTo1e18(
abs(event.params.amount1),
Number(token1Instance.decimals)
Number(token1.decimals)
);
tokenUpdateData.netVolumeToken1USD = multiplyBase1e18(
normalizedAmount1,
token1Instance.pricePerUSDNew
token1.pricePerUSDNew
);
}

Expand All @@ -534,8 +539,8 @@ CLPool.Swap.handlerWithLoader({
const reserveResult = updateCLPoolLiquidity(
liquidityPoolAggregator,
event,
token0Instance,
token1Instance
token0,
token1
);

const liquidityPoolAggregatorDiff: Partial<LiquidityPoolAggregator> = {
Expand All @@ -558,21 +563,10 @@ CLPool.Swap.handlerWithLoader({
updateLiquidityPoolAggregator(
liquidityPoolAggregatorDiff,
liquidityPoolAggregator,
new Date(event.block.timestamp * 1000),
context
);
}

const blockDatetime = new Date(event.block.timestamp * 1000);
try {
await set_whitelisted_prices(
event.chainId,
event.block.number,
blockDatetime,
context
);
} catch (error) {
console.log(`Error updating token prices on CLPool swap on chain ${event.chainId}:`, error);
}

},
});
53 changes: 24 additions & 29 deletions src/EventHandlers/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Pool, Pool_Swap, Pool_Sync, Pool_Mint, Pool_Burn } from "generated";
import { LiquidityPoolAggregator, User } from "./../src/Types.gen";
import { normalizeTokenAmountTo1e18 } from "./../Helpers";
import { abs, multiplyBase1e18 } from "./../Maths";
import { set_whitelisted_prices } from "../PriceOracle";
import { refreshTokenPrice } from "../PriceOracle";
import { updateLiquidityPoolAggregator } from "../Aggregators/LiquidityPoolAggregator";
import { TokenIdByChain } from "../Constants";

Expand Down Expand Up @@ -143,6 +143,8 @@ Pool.Swap.handlerWithLoader({
};
},
handler: async ({ event, context, loaderReturn }) => {
const blockDatetime = new Date(event.block.timestamp * 1000);

const entity: Pool_Swap = {
id: `${event.chainId}_${event.block.number}_${event.logIndex}`,
sender: event.params.sender,
Expand All @@ -152,7 +154,7 @@ Pool.Swap.handlerWithLoader({
amount0Out: event.params.amount0Out,
amount1Out: event.params.amount1Out,
sourceAddress: event.srcAddress, // Add sourceAddress
timestamp: new Date(event.block.timestamp * 1000), // Convert to Date
timestamp: blockDatetime, // Convert to Date
chainId: event.chainId,
};

Expand All @@ -161,6 +163,9 @@ Pool.Swap.handlerWithLoader({
const { liquidityPoolAggregator, token0Instance, token1Instance, to_address, user } =
loaderReturn;

let token0 = token0Instance;
let token1 = token1Instance;

let tokenUpdateData = {
netAmount0: 0n,
netAmount1: 0n,
Expand All @@ -170,26 +175,28 @@ Pool.Swap.handlerWithLoader({
};

tokenUpdateData.netAmount0 = event.params.amount0In + event.params.amount0Out;
if (token0Instance) {
if (token0) {
token0 = await refreshTokenPrice(token0, event.block.number, event.block.timestamp, event.chainId, context);
const normalizedAmount0 = normalizeTokenAmountTo1e18(
event.params.amount0In + event.params.amount0Out,
Number(token0Instance.decimals)
Number(token0.decimals)
);
tokenUpdateData.netVolumeToken0USD = multiplyBase1e18(
normalizedAmount0,
token0Instance.pricePerUSDNew
token0.pricePerUSDNew
);
}

tokenUpdateData.netAmount1 = event.params.amount1In + event.params.amount1Out;
if (token1Instance) {
if (token1) {
token1 = await refreshTokenPrice(token1, event.block.number, event.block.timestamp, event.chainId, context);
const normalizedAmount1 = normalizeTokenAmountTo1e18(
event.params.amount1In + event.params.amount1Out,
Number(token1Instance.decimals)
Number(token1.decimals)
);
tokenUpdateData.netVolumeToken1USD = multiplyBase1e18(
normalizedAmount1,
token1Instance.pricePerUSDNew
token1.pricePerUSDNew
);
}

Expand Down Expand Up @@ -220,14 +227,13 @@ Pool.Swap.handlerWithLoader({
context
);

const blockDatetime = new Date(event.block.timestamp * 1000);

// Update user and create if they don't exist
if (!user) {
let newUser: User = {
id: to_address,
numberOfSwaps: 1n,
joined_at_timestamp: new Date(event.block.timestamp * 1000),
joined_at_timestamp: blockDatetime,
};
context.User.set(newUser);
} else {
Expand All @@ -236,28 +242,16 @@ Pool.Swap.handlerWithLoader({
numberOfSwaps: user.numberOfSwaps + 1n,
joined_at_timestamp:
user.joined_at_timestamp <
new Date(event.block.timestamp * 1000)
blockDatetime
? user.joined_at_timestamp
: new Date(event.block.timestamp * 1000),
: blockDatetime,
}; // for unordered head mode this correctly categorizes base users who may have joined early on optimism.
try {
context.User.set(existingUser);
} catch (error) {
console.log("Error updating user:", error);
}
}

// Try to set prices
try {
await set_whitelisted_prices(
event.chainId,
event.block.number,
blockDatetime,
context
);
} catch (error) {
console.log("Error updating token prices on pool sync:", error);
}
}
},
});
Expand Down Expand Up @@ -286,6 +280,7 @@ Pool.Sync.handlerWithLoader({
},
handler: async ({ event, context, loaderReturn }) => {
if (!loaderReturn) return;
const blockDatetime = new Date(event.block.timestamp * 1000);

const { liquidityPoolAggregator, token0Instance, token1Instance } = loaderReturn;

Expand All @@ -294,7 +289,7 @@ Pool.Sync.handlerWithLoader({
reserve0: event.params.reserve0,
reserve1: event.params.reserve1,
sourceAddress: event.srcAddress,
timestamp: new Date(event.block.timestamp * 1000),
timestamp: blockDatetime,
chainId: event.chainId,
};

Expand All @@ -313,7 +308,7 @@ Pool.Sync.handlerWithLoader({
if (token0Instance) {
tokenUpdateData.normalizedReserve0 += normalizeTokenAmountTo1e18(
event.params.reserve0,
Number(token0Instance?.decimals || 18)
Number(token0Instance.decimals)
);
tokenUpdateData.token0PricePerUSDNew = token0Instance.pricePerUSDNew;
tokenUpdateData.totalLiquidityUSD += multiplyBase1e18(
Expand All @@ -325,8 +320,8 @@ Pool.Sync.handlerWithLoader({
if (token1Instance) {
tokenUpdateData.normalizedReserve1 += normalizeTokenAmountTo1e18(
event.params.reserve1,
Number(token1Instance?.decimals || 18)
);
Number(token1Instance.decimals)
);

tokenUpdateData.token1PricePerUSDNew = token1Instance.pricePerUSDNew;
tokenUpdateData.totalLiquidityUSD += multiplyBase1e18(
Expand All @@ -341,7 +336,7 @@ Pool.Sync.handlerWithLoader({
totalLiquidityUSD: tokenUpdateData.totalLiquidityUSD || liquidityPoolAggregator.totalLiquidityUSD,
token0Price: tokenUpdateData.token0PricePerUSDNew,
token1Price: tokenUpdateData.token1PricePerUSDNew,
lastUpdatedTimestamp: new Date(event.block.timestamp * 1000),
lastUpdatedTimestamp: blockDatetime,
};

updateLiquidityPoolAggregator(
Expand Down
24 changes: 24 additions & 0 deletions src/PriceOracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ export interface TokenPriceData {
decimals: bigint;
}

const ONE_HOUR_MS = 60 * 60 * 1000; // 1 hour in milliseconds

export async function refreshTokenPrice(
token: Token,
blockNumber: number,
blockTimestamp: number,
chainId: number,
context: any
): Promise<Token> {
if (blockTimestamp - token.lastUpdatedTimestamp.getTime() < ONE_HOUR_MS) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only actually updates if the last token price is older than 1 hour. Might update this at some point it it seems this should work for now.

return token;
}

const tokenPriceData = await getTokenPriceData(token.address, blockNumber, chainId);
const updatedToken: Token = {
...token,
pricePerUSDNew: tokenPriceData.pricePerUSDNew,
decimals: tokenPriceData.decimals,
lastUpdatedTimestamp: new Date(blockTimestamp * 1000)
};
context.Token.set(updatedToken);
return updatedToken;
}

export async function getTokenPriceData(tokenAddress: string, blockNumber: number, chainId: number): Promise<TokenPriceData> {
const tokenDetails = await getErc20TokenDetails(
tokenAddress,
Expand Down
26 changes: 13 additions & 13 deletions test/EventHandlers/CLPool/CLPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { setupCommon } from "../Pool/common";
describe("CLPool Event Handlers", () => {
let mockDb: any;
let updateLiquidityPoolAggregatorStub: sinon.SinonStub;
let setPricesStub: sinon.SinonStub;
let mockPriceOracle: sinon.SinonStub;

beforeEach(() => {
mockDb = MockDb.createMockDb();
Expand All @@ -23,9 +23,11 @@ describe("CLPool Event Handlers", () => {
LiquidityPoolAggregatorFunctions,
"updateLiquidityPoolAggregator"
);
setPricesStub = sinon
.stub(PriceOracle, "set_whitelisted_prices")
.resolves();
mockPriceOracle = sinon
.stub(PriceOracle, "refreshTokenPrice")
.callsFake(async (...args) => {
return args[0]; // Return the token that was passed in
});

});

Expand Down Expand Up @@ -480,15 +482,13 @@ describe("CLPool Event Handlers", () => {
const [diff] = aggregatorCalls;
expect(diff.totalLiquidityUSD).to.equal(expectations.totalLiquidityUSD);
});

it("should call set_whitelisted_prices", async () => {
expect(setPricesStub.calledOnce).to.be.true;
const [chainId, blockNumber, blockDatetime] =
setPricesStub.firstCall.args;

expect(chainId).to.equal(1);
expect(blockNumber).to.equal(123456);
expect(blockDatetime).to.deep.equal(new Date(1000000 * 1000));
it("should call refreshTokenPrice on token0", () => {
const calledToken = mockPriceOracle.firstCall.args[0];
expect(calledToken.address).to.equal(mockToken0Data.address);
});
it("should call refreshTokenPrice on token1", () => {
const calledToken = mockPriceOracle.secondCall.args[0];
expect(calledToken.address).to.equal(mockToken1Data.address);
});
});

Expand Down
Loading
Loading