core.ts
path: /src/mappings/core.ts
handleInitialize()
Params:
- event (initialize): entity of the initialize event emitted in a pool contract
ReturnType: void
- Eth Mainnet
- Polygon, Optimism
- Arbitrum-One
- Handles the initialization of a new pool by setting it's
priceand currenttickvalue. - Updates the pools daily and hourly metrics using
updatePoolDayData()andupdatePoolHourData(). - Updates Eth's USD price using
getEthPriceInUSD(). - Updates the token's prices relative to Eth using
findEthPerToken().
Entities
Dependencies:
Invoked at:
- Follows the logic of update, but doesn't save the
poolentity.
- 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,totalValueLockedETHandtotalValueLockedUSDmetrics forpool,factoryandtokenentities. - Increases
pool.liquiditybyevent.params.amountif the currentpool.tickvalue is within the minted tick range. - Creates a new
Mintentity usingtransaction.idandpool.txCountasmint.id - Creates tick entities
lowerTickandupperTickif not already present usingcreateTick()and updates theirliquidityGrossandliquidityNetfields. - Updates the pool and token metrics using
updateUniswapDayData(),updatePoolDayData(),updatePoolHourData(),updateTokenDayData(),updateTokenHourData(). - Updates the fees accumulated outside the lower/upper ticks using
updateTickFeeVarsAndSave()
Entities
- Bundle - Read
- Pool - Read & Write
- Token - Read & Write
- Factory - Read & Write
- Tick - Read/Create & Write
- Mint - Create & Write
Dependencies:
- FACTORY_ADDRESS
- convertTokenToDecimal()
- loadTransaction()
- createTick()
- updateUniswapDayData()
- updatePoolDayData()
- updatePoolHourData()
- updateTokenDayData()
- updateTokenHourData()
- updateTickFeeVarsAndSave()
- ONE_BI
Invoked at:
handleBurn()
Params:
- event (BurnEvent): entity of the burn event emitted in a pool contract
ReturnType: void
- Other-Chains
- Optimism
- updates
txCount,totalValueLockedETHandtotalValueLockedUSDmetrics forpool,factoryandtokenentities. - Decreases
pool.liquiditybyevent.params.amountif the currentpool.tickvalue is within the burnt tick range. - Creates a new
Burnentity usingtransaction.idandpool.txCountasmint.id. Sets the values fromeventparameters. - Reduces the liquidity represented by
liquidityGrossandliquidityNetfields of the LowerTick and UpperTick. - Updates the pool and token metrics using
updateUniswapDayData(),updatePoolDayData(),updatePoolHourData(),updateTokenDayData(),updateTokenHourData(). - Updates the fees accumulated outside the lower/upper ticks using
updateTickFeeVarsAndSave()
Entities
- Bundle - Read
- Pool - Read & Write
- Token - Read & Write
- Factory - Read & Write
- Tick - Read & Write
- Burn - Create & Write
Dependencies:
- FACTORY_ADDRESS
- convertTokenToDecimal()
- loadTransaction()
- ONE_BI
- updateUniswapDayData()
- updatePoolDayData()
- updatePoolHourData()
- updateTokenDayData()
- updateTokenHourData()
- updateTickFeeVarsAndSave()
Invoked at:
Most of the logic is same as mainnet subgraph with following changes:
- While loading the
Tickentities, if either one is not found, invokescreateTickBurn()to create ticks and then proceeds with updating the liquidity values and metrics.
Additional Dependencies
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
- Polygon
- Arbitrum-One
- Calculates the tracked and untracked USD amount for the swap.
trackedamount is the USD amount calculated only for tokens present inWHITELIST_TOKENusinggetTrackedAmountUSD.untrackedamount is calculated usingtoken.derivedETH * bundle.ethPriceUSD. - Calculates the fee in
ETH&USDusing the formulaamountTracked * (pool.feeTier/1,000,000). - Updates the fields for
txCount, volume & fees (in eth & usd) anduntrackedVolumeUSDforpool,factory&tokenentities. - For
poolentity, setsliquidity,tick,sqrtPricefrom theeventparameters. - Sets the
pool.token0Priceandpool.token1PriceusingsqrtPriceX96ToTokenPrices(). - Updates the
bundle.ethPriceUSDusinggetEthPriceInUSD(). - Updates the
token.derivedETHvalue usingfindEthPerToken(). - Updates the
totalValueLockedETHandtotalValueLockedUSDforpool,factoryandtokenentities after the USD price update. - Creates a new
Swapentity usingtransaction.idandpool.txCountasswap.id. Sets the values fromeventparameters. - Sets
pool.feeGrowthGlobal0X128andpool.feeGrowthGlobal1X128by 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.tickis initialized, updates it's fee variables usingloadTickUpdateFeeVarsAndSave(). - 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
- Bundle - Read & Write
- Pool - Read & Write
- Token - Read & Write
- Factory - Read & Write
- Tick - Read/Create & Write
- Swap - Create & Write
- UniswapDayData - Write
- PoolDayData - Write
- PoolHourData - Write
- TokenDayData - Write
- TokenHourData - Write
ABI Dependencies:
- pool.json
Dependencies:
- FACTORY_ADDRESS
- convertTokenToDecimal()
- loadTransaction()
- getTrackedAmountUSD()
- safeDiv()
- sqrtPriceX96ToTokenPrices()
- getEthPriceInUSD()
- findEthPerToken()
- updateUniswapDayData()
- updatePoolDayData()
- updatePoolHourData()
- updateTokenDayData()
- updateTokenHourData()
- loadTickUpdateFeeVarsAndSave()
- feeTierToTickSpacing()
- ZERO_BD
- ZERO_BI
- ONE_BI
Invoked at:
- Follows the logic of mainnet except doesn't save the
token0HourData,token1HourDataandpoolHourDataentities.
- Follows the logic of mainnet except doesn't save the
token0HourData,token1HourDataandpoolHourDataentities. - Doesn't update the
pool.feeGrowthGlobal0X128andpool.feeGrowthGlobal1X128values.
handleFlash()
Params:
- event (FlashEvent): entity of the flash event emitted in a pool contract
ReturnType: void
- Eth Mainnet, Polygon
- Arbitrum-One
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
- Arbitrum-One
- Sets
tick.feeGrowthOutside0X128andtick.feeGrowthOutside1X128by reading the tick from pool contract's blockchain state using the ABI. - Triggers update to tick day metrics by invoking
updateTickDayData().
Entities
- Tick - Write
ABI Dependencies:
- pool.json
Dependencies:
Invoked at:
- 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.addressandtickId. If found, updates the tick variables by invokingupdateTickFeeVarsAndSave().
Entities
- Tick - Read & Write