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
txCount
by 1;increment
totalVolumeETH
bytotalSwapAmount
in ETH;increment
totalVolumeUSD by totalSwapAmount
in USD;increment
totalFeesETH
byfees
in ETH;increment
totalFeesUSD
byfees
in USD;
decrement PoolManager
totalValueLockedETH
by old PooltotalValueLockedETH
(it is needed to add new TVL later);update values of Pool:
increment token volumes (
volumeToken0
,volumeToken1
) byamount0Abs,
amount1Abs
respectively;increment
volumeUSD
bytotalSwapAmountUSD;
increment
feesUSD
by calculatedfeesUSD;
increment
txCount
by 1;update
liquidity
with value from event;update
tick
with value from event;update
sqrtPrice
with value from event;increment
totalValueLocked
for both tokens by amount respectively;
update for both tokens values of Token
increment
volume
byamountAbs;
increment
totalValueLocked
byamount
;increment
volumeUSD
bytotalSwapAmountUSD;
increment
feesUSD
by calculatedfeesUSD;
increment
txCount
by 1;
update Pool rates:
update token prices to new prices calculated by standard AMM formula;
save
lastTx
value;
update Bundle
ethPriceUSD
by 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
gasAccumulatedBySwaps
by gas value from event;increment
gasAccumulatedBySwapsInEth
by gas value from event times transaction gas price;increment
swapsCount
by 1;
update Hook values:
update
totalValueLocked
in ETH and USD by subtracting the pool's oldtotalValueLocked
and adding new one (calculated with new prices);increment
volumeUSD
bytotalSwapAmountUSD;
increment
feesUSD
by calculatedfeesUSD;
increment
gasAccumulatedBySwaps
by gas value from event;increment
gasAccumulatedBySwapsInEth
by gas value from event times transaction gas price;increment
swapsCount
by 1;
increment PoolManager
totalValueLocked
by updated pool values;increment Token
totalValueLocked
by updated pool values;update UniswapDayData, PoolDayData, PoolHourData, TokenDayData TokenHourData with values calculated earlier;
Last updated