Swap
Last updated
Last updated
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:
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:
convert each token amount from decimal to integer value using number of decimals specified in and revert a sign as specified in official Uniswap article:
amountDec = -(amount / decimals);
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:
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;
increment txCount
by 1;
increment totalVolumeETH
by totalSwapAmount
in ETH;
increment totalVolumeUSD by totalSwapAmount
in USD;
increment totalFeesETH
by fees
in ETH;
increment totalFeesUSD
by fees
in USD;
increment token volumes (volumeToken0
, volumeToken1
) by amount0Abs,
amount1Abs
respectively;
increment volumeUSD
by totalSwapAmountUSD;
increment feesUSD
by calculated feesUSD;
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;
increment volume
by amountAbs;
increment totalValueLocked
by amount
;
increment volumeUSD
by totalSwapAmountUSD;
increment feesUSD
by calculated feesUSD;
increment txCount
by 1;
update token prices to new prices calculated by standard AMM formula;
save lastTx
value;
totalValueLockedETH:
totalValueLockedETH = totalValueLockedToken0 * token0DerivedETH + totalValueLockedToken1 * token1DerivedETH;
totalValueLockedUSD:
totalValueLockedUSD = totalValueLockedETH * ethPriceUSD;
increment gasAccumulatedBySwaps
by gas value from event;
increment gasAccumulatedBySwapsInEth
by gas value from event times transaction gas price;
increment swapsCount
by 1;
update totalValueLocked
in ETH and USD by subtracting the pool's old totalValueLocked
and adding new one (calculated with new prices);
increment volumeUSD
by totalSwapAmountUSD;
increment feesUSD
by calculated feesUSD;
increment gasAccumulatedBySwaps
by gas value from event;
increment gasAccumulatedBySwapsInEth
by gas value from event times transaction gas price;
increment swapsCount
by 1;
update values of
decrement totalValueLockedETH
by old totalValueLockedETH
(it is needed to add new TVL later);
update values of :
update for both tokens values of
update rates:
update ethPriceUSD
by new value fetched from native token pool;
update derivedETH
(token price in native currency) for both tokens;
update values affected by new USD rates:
update gas values:
update values:
increment totalValueLocked
by updated pool values;
increment totalValueLocked
by updated pool values;
update , , , with values calculated earlier;