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
price
and currenttick
value. - 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
pool
entity.
- 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
andtotalValueLockedUSD
metrics forpool
,factory
andtoken
entities. - Increases
pool.liquidity
byevent.params.amount
if the currentpool.tick
value is within the minted tick range. - Creates a new
Mint
entity usingtransaction.id
andpool.txCount
asmint.id
- Creates tick entities
lowerTick
andupperTick
if not already present usingcreateTick()
and updates theirliquidityGross
andliquidityNet
fields. - 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
,totalValueLockedETH
andtotalValueLockedUSD
metrics forpool
,factory
andtoken
entities. - Decreases
pool.liquidity
byevent.params.amount
if the currentpool.tick
value is within the burnt tick range. - Creates a new
Burn
entity usingtransaction.id
andpool.txCount
asmint.id
. Sets the values fromevent
parameters. - Reduces the liquidity represented by
liquidityGross
andliquidityNet
fields 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
Tick
entities, 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.
tracked
amount is the USD amount calculated only for tokens present inWHITELIST_TOKEN
usinggetTrackedAmountUSD
.untracked
amount is calculated usingtoken.derivedETH * bundle.ethPriceUSD
. - Calculates the fee in
ETH
&USD
using the formulaamountTracked * (pool.feeTier/1,000,000)
. - Updates the fields for
txCount
, volume & fees (in eth & usd) anduntrackedVolumeUSD
forpool
,factory
&token
entities. - For
pool
entity, setsliquidity
,tick
,sqrtPrice
from theevent
parameters. - Sets the
pool.token0Price
andpool.token1Price
usingsqrtPriceX96ToTokenPrices()
. - Updates the
bundle.ethPriceUSD
usinggetEthPriceInUSD()
. - Updates the
token.derivedETH
value usingfindEthPerToken()
. - Updates the
totalValueLockedETH
andtotalValueLockedUSD
forpool
,factory
andtoken
entities after the USD price update. - Creates a new
Swap
entity usingtransaction.id
andpool.txCount
asswap.id
. Sets the values fromevent
parameters. - Sets
pool.feeGrowthGlobal0X128
andpool.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 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
,token1HourData
andpoolHourData
entities.
- Follows the logic of mainnet except doesn't save the
token0HourData
,token1HourData
andpoolHourData
entities. - Doesn't update the
pool.feeGrowthGlobal0X128
andpool.feeGrowthGlobal1X128
values.
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.feeGrowthOutside0X128
andtick.feeGrowthOutside1X128
by 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.address
andtickId
. If found, updates the tick variables by invokingupdateTickFeeVarsAndSave()
.
Entities
- Tick - Read & Write