Swap
Uniswap v4's swap event improves token exchanges with enhanced efficiency and customization, allowing seamless ERC-20 trades with features like concentrated liquidity and adjustable fees, strengthening its DeFi role. The standard event emitted on swap is:
event Swap(
PoolId indexed id,
address indexed sender,
int128 amount0,
int128 amount1,
uint160 sqrtPriceX96,
uint128 liquidity,
int24 tick,
uint24 fee
);Where amount0 and amount1 are amounts of tokens that took part in swap, sqrtPriceX96 is:
liquidity is a liquidity in pool after the swap, tick, fee is a percentage of protocol fee.
To effectively monitor swap events on a pool, HookRank utilizes The Graph to continuously track them on-chain and modify entities provided in Tracked Data.
On every Swap event emitted standard flow is executed in following order:
calculate
abs()for both amounts to be used in volume calculation further:amountAbs = abs(amountDec);calculate amount of each token in ETH and USD:
amountETH = amountAbs * tokenPriceETH; amountUSD = amountETH * ethPriceUSD;calculate the total swap amount in ETH and USD by accounting for only non-zero prices and dividing result by 2 as both input and output can`t be counted as volume:
const totalSwapAmount = 0;
if (price0USD > 0 && price1USD > 0) {
totalSwapAmount = (price0USD * amount0Abs + price1USD * amount1Abs) / 2;
}
if (price0USD == 0 && price1USD > 0) {
totalSwapAmount = price1USD * amount1Abs;
}
if (price0USD > 0 && price1USD == 0) {
totalSwapAmount = price0USD * amount0Abs;
}calculate the fees in ETH and USD by multiplying the fee values from the event with the totalSwapAmount in their respective currencies, then divide the result by 10^6 to with Uniswap percent denominator:
fees = totalSwapAmount * fee / 10^6;update values of PoolManager
increment
txCountby 1;increment
totalVolumeETHbytotalSwapAmountin ETH;increment
totalVolumeUSD by totalSwapAmountin USD;increment
totalFeesETHbyfeesin ETH;increment
totalFeesUSDbyfeesin USD;
decrement PoolManager
totalValueLockedETHby old PooltotalValueLockedETH(it is needed to add new TVL later);update values of Pool:
increment token volumes (
volumeToken0,volumeToken1) byamount0Abs,amount1Absrespectively;increment
volumeUSDbytotalSwapAmountUSD;increment
feesUSDby calculatedfeesUSD;increment
txCountby 1;update
liquiditywith value from event;update
tickwith value from event;update
sqrtPricewith value from event;increment
totalValueLockedfor both tokens by amount respectively;
update for both tokens values of Token
increment
volumebyamountAbs;increment
totalValueLockedbyamount;increment
volumeUSDbytotalSwapAmountUSD;increment
feesUSDby calculatedfeesUSD;increment
txCountby 1;
update Pool rates:
update token prices to new prices calculated by standard AMM formula;
save
lastTxvalue;
update Bundle
ethPriceUSDby new value fetched from native token pool;update Token
derivedETH(token price in native currency) for both tokens;update Pool values affected by new USD rates:
totalValueLockedETH: totalValueLockedETH = totalValueLockedToken0 * token0DerivedETH + totalValueLockedToken1 * token1DerivedETH;totalValueLockedUSD: totalValueLockedUSD = totalValueLockedETH * ethPriceUSD;
update Pool gas values:
increment
gasAccumulatedBySwapsby gas value from event;increment
gasAccumulatedBySwapsInEthby gas value from event times transaction gas price;increment
swapsCountby 1;
update Hook values:
update
totalValueLockedin ETH and USD by subtracting the pool's oldtotalValueLockedand adding new one (calculated with new prices);increment
volumeUSDbytotalSwapAmountUSD;increment
feesUSDby calculatedfeesUSD;increment
gasAccumulatedBySwapsby gas value from event;increment
gasAccumulatedBySwapsInEthby gas value from event times transaction gas price;increment
swapsCountby 1;
increment PoolManager
totalValueLockedby updated pool values;increment Token
totalValueLockedby updated pool values;update UniswapDayData, PoolDayData, PoolHourData, TokenDayData TokenHourData with values calculated earlier;
Last updated
