watcher-ts/packages/uni-info-watcher/docs/analysis/aggregation.md
Ashwin Phatak 7f5229bf2f
Watch pool burn and swap events (#128)
* Remove test scripts, moved to vulcanize/uniswap-v3-core#watcher-ts.

* Move uniswap analysis docs.

* Watch pool burn and swap events.
2021-07-09 12:30:50 +05:30

32 KiB

Aggregation in Entities

  • Pool

    • id (pool address)

      • Factory PoolCreated event
        let pool = new Pool(event.params.pool.toHexString()) as Pool
        
    • feeTier (fee amount)

      • Factory PoolCreated event
        pool.feeTier = BigInt(event.params.fee)
        
    • sqrtPrice (current price tracker)

      • Pool Initialize event, Swap event
        pool.sqrtPrice = event.params.sqrtPriceX96
        
    • tick (current tick)

      • Pool Initialize event, Swap event
        pool.tick = BigInt(event.params.tick)
        
    • token0Price (token0 per token1), token1Price(token1 per token0)

      • Pool Swap event
        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        let token1 = Token.load(pool.token1)
        let prices = sqrtPriceX96ToTokenPrices(pool.sqrtPrice, token0 as Token, token1 as Token)
        pool.token0Price = prices[0]
        pool.token1Price = prices[1]
        
    • totalValueLockedToken0 (total token 0 across all ticks), totalValueLockedToken1 (total token 1 across all ticks)

      • Pool Mint event, Swap event

        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        let token1 = Token.load(pool.token1)
        let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
        let amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals)
        pool.totalValueLockedToken0 = pool.totalValueLockedToken0.plus(amount0)
        pool.totalValueLockedToken1 = pool.totalValueLockedToken1.plus(amount1)
        
      • Pool Burn event

        pool.totalValueLockedToken0 = pool.totalValueLockedToken0.minus(amount0)
        pool.totalValueLockedToken1 = pool.totalValueLockedToken1.minus(amount1)
        
    • totalValueLockedUSD (tvl USD)

      • Pool Initialize event, Burn event, Swap event
        let bundle = Bundle.load('1')
        let pool = Pool.load(event.address.toHexString())
        pool.totalValueLockedUSD = pool.totalValueLockedETH.times(bundle.ethPriceUSD)
        
    • totalValueLockedETH (tvl derived ETH)

      • Pool Mint event, Burn event, Swap event
        let pool = Pool.load(poolAddress)
        let token0 = Token.load(pool.token0)
        let token1 = Token.load(pool.token1)
        
        pool.totalValueLockedETH = pool.totalValueLockedToken0
          .times(token0.derivedETH)
          .plus(pool.totalValueLockedToken1.times(token1.derivedETH))
        
    • txCount (all time number of transactions)

      • Pool Mint event, Burn event, Swap event
        let pool = Pool.load(event.address.toHexString())
        
        // Constant ONE_BI is BigInt.fromI32(1)
        pool.txCount = pool.txCount.plus(ONE_BI)
        
    • volumeUSD (all time USD swapped)

      • Pool Swap event
        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        let token1 = Token.load(pool.token1)
        
        let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
        let amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals)
        
        let amount0Abs = amount0
        // Constant ZERO_BD is BigDecimal.fromString('0')
        if (amount0.lt(ZERO_BD)) {
          amount0Abs = amount0.times(BigDecimal.fromString('-1'))
        }
        let amount1Abs = amount1
        if (amount1.lt(ZERO_BD)) {
          amount1Abs = amount1.times(BigDecimal.fromString('-1'))
        }
        
        let amountTotalUSDTracked = getTrackedAmountUSD(amount0Abs, token0 as Token, amount1Abs, token1 as Token).div(
          BigDecimal.fromString('2')
        )
        
        pool.volumeUSD = pool.volumeUSD.plus(amountTotalUSDTracked)
        
    • liquidity (in range liquidity)

      • Pool Mint event
        let pool = Pool.load(event.address.toHexString())
        
        if (
          pool.tick !== null &&
          BigInt.fromI32(event.params.tickLower).le(pool.tick as BigInt) &&
          BigInt.fromI32(event.params.tickUpper).gt(pool.tick as BigInt)
        ) {
          pool.liquidity = pool.liquidity.plus(event.params.amount)
        }
        
  • Token

    • id (pool address)

      • Factory PoolCreated event
        let token0 = Token.load(event.params.token0.toHexString())
        
    • decimals (token decimals)

    • derivedETH (derived price in ETH)

    • whitelistPools (pools token is in that are white listed for USD pricing)

    • feesUSD (fees in USD)

      • Pool Swap event
        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        let token1 = Token.load(pool.token1)
        let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
        let amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals)
        
        let amount0Abs = amount0
        if (amount0.lt(ZERO_BD)) {
          amount0Abs = amount0.times(BigDecimal.fromString('-1'))
        }
        let amount1Abs = amount1
        if (amount1.lt(ZERO_BD)) {
          amount1Abs = amount1.times(BigDecimal.fromString('-1'))
        }
        
        let amountTotalUSDTracked = getTrackedAmountUSD(amount0Abs, token0 as Token, amount1Abs, token1 as Token).div(
          BigDecimal.fromString('2')
        )
        
        let feesUSD = amountTotalUSDTracked.times(pool.feeTier.toBigDecimal()).div(BigDecimal.fromString('1000000'))
        token0.feesUSD = token0.feesUSD.plus(feesUSD)
        
    • totalValueLocked (liquidity across all pools in token units)

      • Pool Mint event, Swap event

        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
        token0.totalValueLocked = token0.totalValueLocked.plus(amount0)
        
      • Pool Burn event

        token0.totalValueLocked = token0.totalValueLocked.minus(amount0)
        
    • totalValueLockedUSD (liquidity across all pools in derived USD)

      • Pool Mint event, Burn event, Swap event
        let bundle = Bundle.load('1')
        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        token0.totalValueLockedUSD = token0.totalValueLocked.times(token0.derivedETH.times(bundle.ethPriceUSD))
        
    • txCount (transactions across all pools that include this token)

      • Pool Mint event, Burn event, Swap event
        token0.txCount = token0.txCount.plus(ONE_BI)
        
    • volume (volume in token units)

      • Pool Swap event
        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
        let amount0Abs = amount0
        
        if (amount0.lt(ZERO_BD)) {
          amount0Abs = amount0.times(BigDecimal.fromString('-1'))
        }
        
        token0.volume = token0.volume.plus(amount0Abs)
        
    • volumeUSD (volume in derived USD)

      • Pool Swap event
        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        let token1 = Token.load(pool.token1)
        let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
        let amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals)
        
        let amount0Abs = amount0
        if (amount0.lt(ZERO_BD)) {
          amount0Abs = amount0.times(BigDecimal.fromString('-1'))
        }
        let amount1Abs = amount1
        if (amount1.lt(ZERO_BD)) {
          amount1Abs = amount1.times(BigDecimal.fromString('-1'))
        }
        
        let amountTotalUSDTracked = getTrackedAmountUSD(amount0Abs, token0 as Token, amount1Abs, token1 as Token).div(
          BigDecimal.fromString('2')
        )
        
        token0.volumeUSD = token0.volumeUSD.plus(amountTotalUSDTracked)
        
  • Factory

    • id (factory address)

    • totalFeesUSD (total swap fees all time in USD)

      • Pool Swap event
        let factory = Factory.load(FACTORY_ADDRESS)
        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        let token1 = Token.load(pool.token1)
        let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
        let amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals)
        
        let amount0Abs = amount0
        if (amount0.lt(ZERO_BD)) {
          amount0Abs = amount0.times(BigDecimal.fromString('-1'))
        }
        let amount1Abs = amount1
        if (amount1.lt(ZERO_BD)) {
          amount1Abs = amount1.times(BigDecimal.fromString('-1'))
        }
        
        let amountTotalUSDTracked = getTrackedAmountUSD(amount0Abs, token0 as Token, amount1Abs, token1 as Token).div(
          BigDecimal.fromString('2')
        )
        
        let feesUSD = amountTotalUSDTracked.times(pool.feeTier.toBigDecimal()).div(BigDecimal.fromString('1000000'))
        factory.totalFeesUSD = factory.totalFeesUSD.plus(feesUSD)
        
    • totalValueLockedUSD (TVL derived in USD)

      • Pool Mint event, Burn event, Swap event
        let bundle = Bundle.load('1')
        let factory = Factory.load(FACTORY_ADDRESS)
        factory.totalValueLockedUSD = factory.totalValueLockedETH.times(bundle.ethPriceUSD)
        
    • totalValueLockedETH (TVL derived in ETH)

      • Pool Mint event, Burn event, Swap event
        let pool = Pool.load(event.address.toHexString())
        let factory = Factory.load(FACTORY_ADDRESS)
        factory.totalValueLockedETH = factory.totalValueLockedETH.minus(pool.totalValueLockedETH)
        
        // After change in pool.totalValueLockedETH
        factory.totalValueLockedETH = factory.totalValueLockedETH.plus(pool.totalValueLockedETH)
        
    • totalVolumeUSD (total volume all time in derived USD)

      • Pool Swap event
        let factory = Factory.load(FACTORY_ADDRESS)
        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        let token1 = Token.load(pool.token1)
        let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
        let amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals)
        
        let amount0Abs = amount0
        if (amount0.lt(ZERO_BD)) {
          amount0Abs = amount0.times(BigDecimal.fromString('-1'))
        }
        let amount1Abs = amount1
        if (amount1.lt(ZERO_BD)) {
          amount1Abs = amount1.times(BigDecimal.fromString('-1'))
        }
        
        let amountTotalUSDTracked = getTrackedAmountUSD(amount0Abs, token0 as Token, amount1Abs, token1 as Token).div(
          BigDecimal.fromString('2')
        )
        
        factory.totalVolumeUSD = factory.totalVolumeUSD.plus(amountTotalUSDTracked)
        
    • txCount (amount of transactions all time)

      • Pool Mint event, Burn event, Swap event
        let factory = Factory.load(FACTORY_ADDRESS)
        factory.txCount = factory.txCount.plus(ONE_BI)
        
  • Bundle Stores for USD calculations.

  • Tick

    • id (format: <pool address>#<tick index>)

      • Pool Mint event
      let poolAddress = event.address.toHexString()
      let pool = Pool.load(poolAddress)
      let lowerTickIdx = event.params.tickLower
      let lowerTickId = poolAddress + '#' + BigInt.fromI32(event.params.tickLower).toString()
      let lowerTick = Tick.load(lowerTickId)
      
      if (lowerTick === null) {
        lowerTick = createTick(lowerTickId, lowerTickIdx, pool.id, event)
      }
      
    • liquidityGross (total liquidity pool has as tick lower or upper)

      • Pool Mint event

        lowerTick.liquidityGross = lowerTick.liquidityGross.plus(event.params.amount)
        
      • Pool Burn event

        lowerTick.liquidityGross = lowerTick.liquidityGross.minus(event.params.amount)
        
    • liquidityNet (how much liquidity changes when tick crossed)

      • Pool Mint event

        lowerTick.liquidityNet = lowerTick.liquidityNet.plus(event.params.amount)
        
      • Pool Burn event

        lowerTick.liquidityNet = lowerTick.liquidityNet.minus(event.params.amount)
        
    • price0 (calculated price of token0 of tick within this pool - constant)

      • Pool Mint event
        lowerTick = createTick(lowerTickId, lowerTickIdx, pool.id, event)
        
        // Inside createTick
        // tickIdx = lowerTickIdx
        tick.price0 = bigDecimalExponated(BigDecimal.fromString('1.0001'), BigInt.fromI32(tickIdx))
        
    • price1 (calculated price of token0 of tick within this pool - constant)

      • Pool Mint event
        lowerTick = createTick(lowerTickId, lowerTickIdx, pool.id, event)
        
        // Inside createTick
        // tickIdx = lowerTickIdx
        let price0 = bigDecimalExponated(BigDecimal.fromString('1.0001'), BigInt.fromI32(tickIdx))
        tick.price1 = safeDiv(ONE_BD, price0)
        
    • tickIdx (tick index)

      • Pool Mint event
        lowerTick = createTick(lowerTickId, lowerTickIdx, pool.id, event)
        
        // Inside createTick
        // tickIdx = lowerTickIdx
        tick.tickIdx = BigInt.fromI32(tickIdx)
        
  • Mint

    • id (transaction hash + "#" + index in mints Transaction array)

      • Pool Mint event
        let mint = new Mint(transaction.id.toString() + '#' + pool.txCount.toString())
        
    • amount0 (amount of token 0 minted), amount1 (amount of token 1 minted)

      • Pool Mint event
        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        let token1 = Token.load(pool.token1)
        mint.amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
        mint.amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals)
        
    • amountUSD (derived amount based on available prices of tokens)

      • Pool Mint event
        let bundle = Bundle.load('1')
        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        let token1 = Token.load(pool.token1)
        let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
        let amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals)
        
        mint.amountUSD = amount0
          .times(token0.derivedETH.times(bundle.ethPriceUSD))
          .plus(amount1.times(token1.derivedETH.times(bundle.ethPriceUSD)))
        
    • origin (txn origin)

      • Pool Mint event
        mint.origin = event.transaction.from
        
    • owner (owner of position where liquidity minted to)

      • Pool Mint event
        mint.owner = event.params.owner
        
    • pool (pool position is within)

      • Pool Mint event
        let poolAddress = event.address.toHexString()
        let pool = Pool.load(poolAddress)
        mint.pool = pool.id
        
    • sender (the address that minted the liquidity)

      • Pool Mint event
        mint.sender = event.params.sender
        
    • timestamp (time of txn)

    • transaction (which txn the mint was included in)

      • Pool Mint event
        let transaction = loadTransaction(event)
        mint.transaction = transaction.id
        
  • Burn

    • id (transaction hash + "#" + index in mints Transaction array)

      • Pool Burn event
        let pool = Pool.load(event.address.toHexString())
        let transaction = loadTransaction(event)
        let mint = new Burn(transaction.id + '#' + pool.txCount.toString())
        
    • amount0 (amount of token 0 burned), amount1 (amount of token 1 burned)

      • Pool Burn event
        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        let token1 = Token.load(pool.token1)
        burn.amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
        burn.amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals)
        
    • amountUSD (derived amount based on available prices of tokens)

      • Pool Burn event
        let bundle = Bundle.load('1')
        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        let token1 = Token.load(pool.token1)
        let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
        let amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals)
        
        burn.amountUSD = amount0
          .times(token0.derivedETH.times(bundle.ethPriceUSD))
          .plus(amount1.times(token1.derivedETH.times(bundle.ethPriceUSD)))
        
    • origin (txn origin)

      • Pool Burn event
        burn.origin = event.transaction.from
        
    • owner (owner of position where liquidity was burned)

      • Pool Burn event
        burn.owner = event.params.owner
        
    • pool (pool position is within)

      • Pool Burn event
        let poolAddress = event.address.toHexString()
        let pool = Pool.load(poolAddress)
        burn.pool = pool.id
        
    • timestamp (need this to pull recent txns for specific token or pool)

      • Pool Burn event
        let transaction = loadTransaction(event)
        burn.timestamp = transaction.timestamp
        
    • transaction (txn burn was included in)

      • Pool Burn event
        let transaction = loadTransaction(event)
        burn.transaction = transaction.id
        
  • Swap

    • id (transaction hash + "#" + index in swaps Transaction array)

      • Pool Swap event
        let pool = Pool.load(event.address.toHexString())
        let transaction = loadTransaction(event)
        let swap = new Swap(transaction.id + '#' + pool.txCount.toString())
        
    • amount0 (allow indexing by tokens), amount1 (allow indexing by tokens)

      • Pool Swap event
        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        let token1 = Token.load(pool.token1)
        swap.amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
        swap.amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals)
        
    • amountUSD (derived info)

      • Pool Swap event
        let bundle = Bundle.load('1')
        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        let token1 = Token.load(pool.token1)
        
        let amount0Abs = amount0
        if (amount0.lt(ZERO_BD)) {
          amount0Abs = amount0.times(BigDecimal.fromString('-1'))
        }
        let amount1Abs = amount1
        if (amount1.lt(ZERO_BD)) {
          amount1Abs = amount1.times(BigDecimal.fromString('-1'))
        }
        
        let amountTotalUSDTracked = getTrackedAmountUSD(amount0Abs, token0 as Token, amount1Abs, token1 as Token).div(
          BigDecimal.fromString('2')
        )
        
        swap.amountUSD = amountTotalUSDTracked
        
    • origin (txn origin, the EOA that initiated the txn)

      • Pool Swap event
        swap.origin = event.transaction.from
        
    • pool (pool swap occured within)

      • Pool Swap event
        let poolAddress = event.address.toHexString()
        let pool = Pool.load(poolAddress)
        swap.pool = pool.id
        
    • timestamp (timestamp of transaction)

      • Pool Swap event
        let transaction = loadTransaction(event)
        swap.timestamp = transaction.timestamp
        
    • transaction (pointer to transaction)

      • Pool Swap event
        let transaction = loadTransaction(event)
        swap.transaction = transaction.id
        
  • Transaction

    • id (txn hash)

      • Pool Mint event, Burn event, Swap event NonfungiblePositionManager IncreaseLiquidity event, DecreaseLiquidity event, Collect event, Transfer event
        let transaction = loadTransaction(event)
        
        // Inside loadTransaction
        let transaction = Transaction.load(event.transaction.hash.toHexString())
        if (transaction === null) {
          transaction = new Transaction(event.transaction.hash.toHexString())
        }
        
    • timestamp (timestamp txn was confirmed)

      • Pool Mint event, Burn event, Swap event NonfungiblePositionManager IncreaseLiquidity event, DecreaseLiquidity event, Collect event, Transfer event
        // Inside loadTransaction
        let transaction = Transaction.load(event.transaction.hash.toHexString())
        transaction.timestamp = event.block.timestamp
        
    • burns, mints, swaps (derived values)

      These fields are derived from reverse lookups. https://thegraph.com/docs/define-a-subgraph#reverse-lookups

  • UniswapDayData (Data accumulated and condensed into day stats for all of Uniswap)

    • id (timestamp rounded to current day by dividing by 86400)

      • Pool Mint event, Burn event, Swap event
        updateUniswapDayData(event)
        
        // Inside updateUniswapDayData
        let timestamp = event.block.timestamp.toI32()
        let dayID = timestamp / 86400
        let uniswapDayData = UniswapDayData.load(dayID.toString())
        if (uniswapDayData === null) {
          uniswapDayData = new UniswapDayData(dayID.toString())
        }
        
        • Uses Factory entity totalValueLockedUSD, txCount fields
    • date (timestamp rounded to current day by dividing by 86400)

      • Pool Mint event, Burn event, Swap event
        // Inside updateUniswapDayData
        let timestamp = event.block.timestamp.toI32()
        let dayID = timestamp / 86400
        let dayStartTimestamp = dayID * 86400
        let uniswapDayData = UniswapDayData.load(dayID.toString())
        
        if (uniswapDayData === null) {
          uniswapDayData = new UniswapDayData(dayID.toString())
          uniswapDayData.date = dayStartTimestamp
        }
        
    • tvlUSD (tvl in terms of USD)

      • Pool Mint event, Burn event, Swap event
        let uniswap = Factory.load(FACTORY_ADDRESS)
        let uniswapDayData = UniswapDayData.load(dayID.toString())
        uniswapDayData.tvlUSD = uniswap.totalValueLockedUSD
        
    • volumeUSD (total daily volume in Uniswap derived in terms of USD)

      • Pool Swap event
        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        let token1 = Token.load(pool.token1)
        let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
        let amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals)
        
        let amount0Abs = amount0
        if (amount0.lt(ZERO_BD)) {
          amount0Abs = amount0.times(BigDecimal.fromString('-1'))
        }
        let amount1Abs = amount1
        if (amount1.lt(ZERO_BD)) {
          amount1Abs = amount1.times(BigDecimal.fromString('-1'))
        }
        
        let amountTotalUSDTracked = getTrackedAmountUSD(amount0Abs, token0 as Token, amount1Abs, token1 as Token).div(
          BigDecimal.fromString('2')
        )
        
        let uniswapDayData = updateUniswapDayData(event)
        uniswapDayData.volumeUSD = uniswapDayData.volumeUSD.plus(amountTotalUSDTracked)
        
  • PoolDayData (Data accumulated and condensed into day stats for each pool)

    • id (timestamp rounded to current day by dividing by 86400)

      • Pool Initialize event, Mint event, Burn event, Swap event
        let poolDayData = updatePoolDayData(event)
        
        // Inside updatePoolDayData
        let timestamp = event.block.timestamp.toI32()
        let dayID = timestamp / 86400
        let dayPoolID = event.address
          .toHexString()
          .concat('-')
          .concat(dayID.toString())
        
        let poolDayData = PoolDayData.load(dayPoolID)
        if (poolDayData === null) {
          poolDayData = new PoolDayData(dayPoolID)
        }
        
        • Uses Pool entity token0Price, token1Price, liquidity, sqrtPrice, feeGrowthGlobal0X128, feeGrowthGlobal1X128, tick, totalValueLockedUSD fields
    • date (timestamp rounded to current day by dividing by 86400)

      • Pool Initialize event, Mint event, Burn event, Swap event
        // Inside updatePoolDayData
        let timestamp = event.block.timestamp.toI32()
        let dayID = timestamp / 86400
        let dayStartTimestamp = dayID * 86400
        poolDayData.date = dayStartTimestamp
        
    • tvlUSD (tvl derived in USD at end of period)

      • Pool Initialize event, Mint event, Burn event, Swap event
        // Inside updatePoolDayData
        let pool = Pool.load(event.address.toHexString())
        poolDayData.tvlUSD = pool.totalValueLockedUSD
        
    • volumeUSD (volume in USD)

      • Pool Swap event
        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        let token1 = Token.load(pool.token1)
        let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
        let amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals)
        
        let amount0Abs = amount0
        if (amount0.lt(ZERO_BD)) {
          amount0Abs = amount0.times(BigDecimal.fromString('-1'))
        }
        let amount1Abs = amount1
        if (amount1.lt(ZERO_BD)) {
          amount1Abs = amount1.times(BigDecimal.fromString('-1'))
        }
        
        let amountTotalUSDTracked = getTrackedAmountUSD(amount0Abs, token0 as Token, amount1Abs, token1 as Token).div(
          BigDecimal.fromString('2')
        )
        
        poolDayData.volumeUSD = poolDayData.volumeUSD.plus(amountTotalUSDTracked)
        
  • TokenDayData

    • id (token address concatendated with date)

      • Pool Mint event, Burn event, Swap event
        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        let token0DayData = updateTokenDayData(token0 as Token, event)
        
        // Inside updateTokenDayData
        // token = token0
        let timestamp = event.block.timestamp.toI32()
        let dayID = timestamp / 86400
        let tokenDayID = token.id
          .toString()
          .concat('-')
          .concat(dayID.toString())
        
        let tokenDayData = TokenDayData.load(tokenDayID)
        if (tokenDayData === null) {
          tokenDayData = new TokenDayData(tokenDayID)
        }
        
    • date (timestamp rounded to current day by dividing by 86400)

      • Pool Mint event, Burn event, Swap event
        // Inside updateTokenDayData
        let dayStartTimestamp = dayID * 86400
        
        if (tokenDayData === null) {
          tokenDayData = new TokenDayData(tokenDayID)
          tokenDayData.date = dayStartTimestamp
        }
        
    • totalValueLockedUSD (liquidity across all pools in derived USD)

      • Pool Mint event, Burn event, Swap event
        // Inside updateTokenDayData
        let tokenDayData = TokenDayData.load(tokenDayID)
        tokenDayData.totalValueLockedUSD = token.totalValueLockedUSD
        
    • volumeUSD (volume in derived USD)

      • Pool Swap event
        let pool = Pool.load(event.address.toHexString())
        let token0 = Token.load(pool.token0)
        let token1 = Token.load(pool.token1)
        let amount0 = convertTokenToDecimal(event.params.amount0, token0.decimals)
        let amount1 = convertTokenToDecimal(event.params.amount1, token1.decimals)
        
        let amount0Abs = amount0
        if (amount0.lt(ZERO_BD)) {
          amount0Abs = amount0.times(BigDecimal.fromString('-1'))
        }
        let amount1Abs = amount1
        if (amount1.lt(ZERO_BD)) {
          amount1Abs = amount1.times(BigDecimal.fromString('-1'))
        }
        
        let amountTotalUSDTracked = getTrackedAmountUSD(amount0Abs, token0 as Token, amount1Abs, token1 as Token).div(
          BigDecimal.fromString('2')
        )
        
        token0DayData.volumeUSD = token0DayData.volumeUSD.plus(amountTotalUSDTracked)
        
  • TokenHourData

    • id (token address concatendated with date)

      • Pool Mint event, Burn event, Swap event
        let token0HourData = updateTokenHourData(token0 as Token, event)
        
        // Inside updateTokenHourData
        // token = token0
        let tokenHourID = token.id
          .toString()
          .concat('-')
          .concat(hourIndex.toString())
        let tokenHourData = TokenHourData.load(tokenHourID)
        
        if (tokenHourData === null) {
          tokenHourData = new TokenHourData(tokenHourID)
        }
        
    • close (close price USD)

      • Pool Mint event, Burn event, Swap event
        // Inside updateTokenHourData
        let bundle = Bundle.load('1')
        let tokenPrice = token.derivedETH.times(bundle.ethPriceUSD)
        tokenHourData.close = tokenPrice
        
    • high (high price USD)

      • Pool Mint event, Burn event, Swap event
        // Inside updateTokenHourData
        let tokenHourData = TokenHourData.load(tokenHourID)
        if (tokenHourData === null) {
          tokenHourData = new TokenHourData(tokenHourID)
          tokenHourData.high = tokenPrice
        }
        
        if (tokenPrice.gt(tokenHourData.high)) {
          tokenHourData.high = tokenPrice
        }
        
    • low (low price USD)

      • Pool Mint event, Burn event, Swap event
        // Inside updateTokenHourData
        let tokenHourData = TokenHourData.load(tokenHourID)
        if (tokenHourData === null) {
          tokenHourData = new TokenHourData(tokenHourID)
          tokenHourData.low = tokenPrice
        }
        
        if (tokenPrice.lt(tokenHourData.low)) {
          tokenHourData.low = tokenPrice
        }
        
    • open (opening price USD)

      • Pool Mint event, Burn event, Swap event
        // Inside updateTokenHourData
        if (tokenHourData === null) {
          tokenHourData = new TokenHourData(tokenHourID)
          tokenHourData.open = tokenPrice
        }
        
    • periodStartUnix (unix timestamp for start of hour)

      • Pool Mint event, Burn event, Swap event
        // Inside updateTokenHourData
        if (tokenHourData === null) {
          tokenHourData = new TokenHourData(tokenHourID)
          tokenHourData.periodStartUnix = hourStartUnix
        }