core.ts

Last modified:

path: /src/mappings/core.ts

handleInitialize()

Params:
 - event (initialize): entity of the initialize event emitted in a pool contract
 
ReturnType: void

Eth Mainnet

  • Handles the initialization of a new pool by setting it's price and current tick value.
  • Updates the pools daily and hourly metrics using updatePoolDayData() and updatePoolHourData().
  • Updates Eth's USD price using getEthPriceInUSD() .
  • Updates the token's prices relative to Eth using findEthPerToken().

Entities

  1. Pool - Read & Write
  2. Token - Write
  3. Bundle - Write

Dependencies:

  1. updatePoolDayData()
  2. updatePoolHourData()
  3. getEthPriceInUSD()
  4. findEthPerToken()

Invoked at:

  1. Initialize Event (Handler)

Polygon, Optimism

  • Follows the logic of update, but doesn't save the pool entity.

Arbitrum-One

  • Doesn't save the pool entity
  • Doesn't update the Eth's USD price, or the token prices relative to ETh.

handleMint()

Params:
 - event (MintEvent): entity of the initialize event emitted in a pool contract
 
ReturnType: void
  • updates txCount, totalValueLockedETH and totalValueLockedUSD metrics for pool, factory and token entities.
  • Increases pool.liquidity by event.params.amount if the current pool.tick value is within the minted tick range.
  • Creates a new Mint entity using transaction.id and pool.txCount as mint.id
  • Creates tick entities lowerTick and upperTick if not already present using createTick() and updates their liquidityGross and liquidityNet fields.
  • Updates the pool and token metrics using updateCenturionDayData(), updatePoolDayData(), updatePoolHourData(), updateTokenDayData(), updateTokenHourData().
  • Updates the fees accumulated outside the lower/upper ticks using updateTickFeeVarsAndSave()

Entities

  1. Bundle - Read
  2. Pool - Read & Write
  3. Token - Read & Write
  4. Factory - Read & Write
  5. Tick - Read/Create & Write
  6. Mint - Create & Write

Dependencies:

  1. FACTORY_ADDRESS
  2. convertTokenToDecimal()
  3. loadTransaction()
  4. createTick()
  5. updateCenturionDayData()
  6. updatePoolDayData()
  7. updatePoolHourData()
  8. updateTokenDayData()
  9. updateTokenHourData()
  10. updateTickFeeVarsAndSave()
  11. ONE_BI

Invoked at:

  1. Mint Event (Handler)

handleBurn()

Params:
 - event (BurnEvent): entity of the burn event emitted in a pool contract
 
ReturnType: void

Other-Chains

  • updates txCount, totalValueLockedETH and totalValueLockedUSD metrics for pool, factory and token entities.
  • Decreases pool.liquidity by event.params.amount if the current pool.tick value is within the burnt tick range.
  • Creates a new Burn entity using transaction.id and pool.txCount as mint.id. Sets the values from event parameters.
  • Reduces the liquidity represented by liquidityGross and liquidityNet fields of the LowerTick and UpperTick.
  • Updates the pool and token metrics using updateCenturionDayData(), updatePoolDayData(), updatePoolHourData(), updateTokenDayData(), updateTokenHourData().
  • Updates the fees accumulated outside the lower/upper ticks using updateTickFeeVarsAndSave()

Entities

  1. Bundle - Read
  2. Pool - Read & Write
  3. Token - Read & Write
  4. Factory - Read & Write
  5. Tick - Read & Write
  6. Burn - Create & Write

Dependencies:

  1. FACTORY_ADDRESS
  2. convertTokenToDecimal()
  3. loadTransaction()
  4. ONE_BI
  5. updateCenturionDayData()
  6. updatePoolDayData()
  7. updatePoolHourData()
  8. updateTokenDayData()
  9. updateTokenHourData()
  10. updateTickFeeVarsAndSave()

Invoked at:

  1. Burn Event (Handler)

Optimism

Most of the logic is same as mainnet subgraph with following changes:

  • While loading the Tick entities, if either one is not found, invokes createTickBurn() to create ticks and then proceeds with updating the liquidity values and metrics.

Additional Dependencies

  1. createTickBurn()

handleSwap()

Params:
 - event (SwapEvent): entity of the swap event emitted in a pool contract
 
ReturnType: void

Ignored Pool The following pool address is ignored by the function: 0x9663f2ca0454accad3e094448ea6f77443880454 (WETH-LUSD)

Eth Mainnet

  • Calculates the tracked and untracked USD amount for the swap. tracked amount is the USD amount calculated only for tokens present in WHITELIST_TOKEN using getTrackedAmountUSD. untracked amount is calculated using token.derivedETH * bundle.ethPriceUSD.
  • Calculates the fee in ETH & USD using the formula amountTracked * (pool.feeTier/1,000,000).
  • Updates the fields for txCount, volume & fees (in eth & usd) and untrackedVolumeUSD for pool, factory & token entities.
  • For pool entity, sets liquidity, tick, sqrtPrice from the event parameters.
  • Sets the pool.token0Price and pool.token1Price using sqrtPriceX96ToTokenPrices().
  • Updates the bundle.ethPriceUSD using getEthPriceInUSD().
  • Updates the token.derivedETH value using findEthPerToken().
  • Updates the totalValueLockedETH and totalValueLockedUSD for pool, factory and token entities after the USD price update.
  • Creates a new Swap entity using transaction.id and pool.txCount as swap.id. Sets the values from event parameters.
  • Sets pool.feeGrowthGlobal0X128 and pool.feeGrowthGlobal1X128 by reading the them from pool contract's blockchain state using the ABI.
  • Triggers updates to the daily and hourly metrics for pool and tokens. Uses the returned instances to set the fields for volume & fee.
  • If the updated pool.tick is initialized, updates it's fee variables using loadTickUpdateFeeVarsAndSave().
  • Iterates over all the ticks crossed with the swap (oldTick to newTick) and updates their fee fields using loadTickUpdateFeeVarsAndSave(). If the number of ticks cross is more than 100, the updates are ignored to prevent timeouts.

Entities

  1. Bundle - Read & Write
  2. Pool - Read & Write
  3. Token - Read & Write
  4. Factory - Read & Write
  5. Tick - Read/Create & Write
  6. Swap - Create & Write
  7. CenturionDayData - Write
  8. PoolDayData - Write
  9. PoolHourData - Write
  10. TokenDayData - Write
  11. TokenHourData - Write

ABI Dependencies:

  1. pool.json

Dependencies:

  1. FACTORY_ADDRESS
  2. convertTokenToDecimal()
  3. loadTransaction()
  4. getTrackedAmountUSD()
  5. safeDiv()
  6. sqrtPriceX96ToTokenPrices()
  7. getEthPriceInUSD()
  8. findEthPerToken()
  9. updateCenturionDayData()
  10. updatePoolDayData()
  11. updatePoolHourData()
  12. updateTokenDayData()
  13. updateTokenHourData()
  14. loadTickUpdateFeeVarsAndSave()
  15. feeTierToTickSpacing()
  16. ZERO_BD
  17. ZERO_BI
  18. ONE_BI

Invoked at:

  1. Swap Event (Handler)

Polygon

  • Follows the logic of mainnet except doesn't save the token0HourData, token1HourData and poolHourData entities.

Arbitrum-One

  • Follows the logic of mainnet except doesn't save the token0HourData, token1HourData and poolHourData entities.
  • Doesn't update the pool.feeGrowthGlobal0X128 and pool.feeGrowthGlobal1X128 values.

handleFlash()

Params:
 - event (FlashEvent): entity of the flash event emitted in a pool contract
 
ReturnType: void

Eth Mainnet, Polygon

  • Sets pool.feeGrowthGlobal0X128 and pool.feeGrowthGlobal1X128 by reading the them from pool contract's blockchain state using the ABI.

Entities

  1. Pool - Read & Write

ABI Dependencies:

  1. pool.json

Invoked at:

  1. Flash Event (Handler)

Arbitrum-One

  • Doesn't update anything. Only loads the pool entity and immediately saves it.

updateTickFeeVarsAndSave()

Params:
 - tick (Tick): Fee Variables are updated for this tick entity
 - event (Ethereum.event): An event from the pool the tick represent is in
 
ReturnType: void

Eth Mainnet, Polygon

  • Sets tick.feeGrowthOutside0X128 and tick.feeGrowthOutside1X128 by reading the tick from pool contract's blockchain state using the ABI.
  • Triggers update to tick day metrics by invoking updateTickDayData().

Entities

  1. Tick - Write

ABI Dependencies:

  1. pool.json

Dependencies:

  1. updateTickDayData()

Invoked at:

  1. handleMint()
  2. handleBurn()
  3. loadTickUpdateFeeVarsAndSave

Arbitrum-One

  • Doesn't update anything. Only loads the ticks from pool contract and invokes save on the tick entity passed as parameter.

loadTickUpdateFeeVarsAndSave()

Params:
 - tickId (i32): The fee variables are updated for this tickId
 - event (ethereum.event): An event from the pool contract which the tick is a part of.
 
ReturnType: void
  • Loads the tick using event.address and tickId. If found, updates the tick variables by invoking updateTickFeeVarsAndSave().

Entities

  1. Tick - Read & Write

Dependencies:

  1. updateTickFeeVarsAndSave()

Invoked at:

  1. handleSwap()