-
Notifications
You must be signed in to change notification settings - Fork 4
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
Changes from 3 commits
1948d8d
aa654c8
e1c7ab5
824b441
5c3184b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
@@ -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, | ||
|
@@ -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 = { | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
); | ||
} | ||
|
||
|
@@ -534,8 +539,8 @@ CLPool.Swap.handlerWithLoader({ | |
const reserveResult = updateCLPoolLiquidity( | ||
liquidityPoolAggregator, | ||
event, | ||
token0Instance, | ||
token1Instance | ||
token0, | ||
token1 | ||
); | ||
|
||
const liquidityPoolAggregatorDiff: Partial<LiquidityPoolAggregator> = { | ||
|
@@ -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); | ||
} | ||
|
||
}, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
There was a problem hiding this comment.
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.