LogoLogo
  • HOOKRANK
    • Introduction
  • CORE FEATURES
    • Dashboard
      • Hook List
      • Hook Details
    • Earnings Analytics
    • Gas Spendings
    • Total Value Locked
    • Transaction Volume
  • HOOKS KNOWLEDGE
    • NoOp Hooks
  • HookRank indexing
    • Events
      • Swap
      • Initialize
      • ModifyLiquidity
    • Tracked Data
  • PUBLIC API
    • Introduction
    • Endpoints
      • /api/public/v1/uniswap/hooks/networks
        • GET
      • /api/public/v1/uniswap/hooks/currencies
        • GET
      • /api/public/v1/uniswap/hooks
        • GET
      • /api/public/v1/uniswap/hooks/{{chainId}}/{{hookAddress}}
        • GET
      • /api/public/v1/uniswap/hooks/{{chainId}}/{{hookAddress}}/contract-metadata
        • GET
  • GUIDES
    • FAQs
    • How To Provide Custom Hook Integration Data
  • EXTERNAL LINKS
    • Twitter / X
    • GitHub
    • Discord
    • Brand Kit
Powered by GitBook
On this page
  1. HookRank indexing
  2. Events

Swap

PreviousEventsNextInitialize

Last updated 2 months ago

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:

sqrtPriceX96=price of token1price of token0×296\text{sqrtPriceX96} = \sqrt{\frac{\text{price of token1}}{\text{price of token0}}} \times 2^{96}sqrtPriceX96=price of token0price of token1​​×296

liquidity is a liquidity in pool after the swap, , fee is a percentage of protocol fee.

To effectively monitor swap events on a pool, HookRank utilizes 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:

  1. convert each token amount from decimal to integer value using number of decimals specified in and revert a sign as specified in official Uniswap : amountDec = -(amount / decimals);

  2. calculate abs()for both amounts to be used in volume calculation further: amountAbs = abs(amountDec);

  3. calculate amount of each token in ETH and USD: amountETH = amountAbs * tokenPriceETH; amountUSD = amountETH * ethPriceUSD;

  4. 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;
}
    • 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;

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 : fees = totalSwapAmount * fee / 10^6;

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;

denominator
tick
The Graph
article
Token
PoolManager
PoolManager
Pool
Pool
Token
Pool
Bundle
Token
Pool
Pool
Hook
PoolManager
Token
UniswapDayData
PoolDayData
PoolHourData
TokenDayData
TokenHourData