Skip to content

Commit

Permalink
refactor subgraph to support multiple chains
Browse files Browse the repository at this point in the history
  • Loading branch information
mzywang committed Jun 3, 2024
1 parent cb859f1 commit 493371d
Show file tree
Hide file tree
Showing 22 changed files with 513 additions and 276 deletions.
26 changes: 26 additions & 0 deletions networks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"mainnet": {
"Factory": {
"address": "0x1F98431c8aD98523631AE4a59f267346ea31F984",
"startBlock": 12369621
}
},
"bsc": {
"Factory": {
"address": "0xdB1d10011AD0Ff90774D0C6Bb92e5C5c8b4461F7",
"startBlock": 12369621
}
},
"base": {
"Factory": {
"address": "0x33128a8fC17869897dcE68Ed026d694621f6FDfD",
"startBlock": 2009445
}
},
"arbitrum-one": {
"Factory": {
"address": "0x1F98431c8aD98523631AE4a59f267346ea31F984",
"startBlock": 165
}
}
}
4 changes: 3 additions & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ type Token @entity {
totalValueLockedUSD: BigDecimal!
# TVL derived in USD untracked
totalValueLockedUSDUntracked: BigDecimal!
# derived price in ETH
# NOTE: for chains where ETH is not the native token, this will be the derived
# price of that chain's native token, effectively, this should be renamed
# derivedNative
derivedETH: BigDecimal!
# pools token is in that are white listed for USD pricing
whitelistPools: [Pool!]!
Expand Down
30 changes: 16 additions & 14 deletions src/mappings/factory.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Address, BigInt, log } from '@graphprotocol/graph-ts'
import { BigInt, log } from '@graphprotocol/graph-ts'

import { PoolCreated } from '../types/Factory/Factory'
import { Factory } from '../types/schema'
import { Bundle, Pool, Token } from '../types/schema'
import { Pool as PoolTemplate } from '../types/templates'
import { STATIC_TOKEN_DEFINITIONS, StaticTokenDefinition } from '../utils/staticTokenDefinition'
import { getSubgraphConfig, SubgraphConfig } from '../utils/chains'
import { fetchTokenDecimals, fetchTokenName, fetchTokenSymbol, fetchTokenTotalSupply } from '../utils/token'
import { ADDRESS_ZERO, FACTORY_ADDRESS, ONE_BI, ZERO_BD, ZERO_BI } from './../utils/constants'
import { WHITELIST_TOKENS } from './../utils/pricing'
import { ADDRESS_ZERO, ONE_BI, ZERO_BD, ZERO_BI } from './../utils/constants'

// The subgraph handler must have this signature to be able to handle events,
// however, we invoke a helper in order to inject dependencies for unit tests.
Expand All @@ -18,12 +17,15 @@ export function handlePoolCreated(event: PoolCreated): void {
// Exported for unit tests
export function handlePoolCreatedHelper(
event: PoolCreated,
factoryAddress: string = FACTORY_ADDRESS,
whitelistTokens: string[] = WHITELIST_TOKENS,
staticTokenDefinitions: StaticTokenDefinition[] = STATIC_TOKEN_DEFINITIONS,
subgraphConfig: SubgraphConfig = getSubgraphConfig(),
): void {
const factoryAddress = subgraphConfig.factoryAddress
const whitelistTokens = subgraphConfig.whitelistTokens
const tokenOverrides = subgraphConfig.tokenOverrides
const poolsToSkip = subgraphConfig.poolsToSkip

// temp fix
if (event.params.pool == Address.fromHexString('0x8fe8d9bb8eeba3ed688069c3d6b556c9ca258248')) {
if (poolsToSkip.includes(event.params.pool.toHexString())) {
return
}

Expand Down Expand Up @@ -59,10 +61,10 @@ export function handlePoolCreatedHelper(
// fetch info if null
if (token0 === null) {
token0 = new Token(event.params.token0.toHexString())
token0.symbol = fetchTokenSymbol(event.params.token0, staticTokenDefinitions)
token0.name = fetchTokenName(event.params.token0, staticTokenDefinitions)
token0.symbol = fetchTokenSymbol(event.params.token0, tokenOverrides)
token0.name = fetchTokenName(event.params.token0, tokenOverrides)
token0.totalSupply = fetchTokenTotalSupply(event.params.token0)
const decimals = fetchTokenDecimals(event.params.token0, staticTokenDefinitions)
const decimals = fetchTokenDecimals(event.params.token0, tokenOverrides)

// bail if we couldn't figure out the decimals
if (decimals === null) {
Expand All @@ -86,10 +88,10 @@ export function handlePoolCreatedHelper(

if (token1 === null) {
token1 = new Token(event.params.token1.toHexString())
token1.symbol = fetchTokenSymbol(event.params.token1)
token1.name = fetchTokenName(event.params.token1)
token1.symbol = fetchTokenSymbol(event.params.token1, tokenOverrides)
token1.name = fetchTokenName(event.params.token1, tokenOverrides)
token1.totalSupply = fetchTokenTotalSupply(event.params.token1)
const decimals = fetchTokenDecimals(event.params.token1)
const decimals = fetchTokenDecimals(event.params.token1, tokenOverrides)
// bail if we couldn't figure out the decimals
if (decimals === null) {
log.debug('mybug the decimal on token 0 was null', [])
Expand Down
9 changes: 6 additions & 3 deletions src/mappings/pool/burn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { BigInt } from '@graphprotocol/graph-ts'
import { Bundle, Burn, Factory, Pool, Tick, Token } from '../../types/schema'
import { Burn as BurnEvent } from '../../types/templates/Pool/Pool'
import { convertTokenToDecimal, loadTransaction } from '../../utils'
import { FACTORY_ADDRESS, ONE_BI } from '../../utils/constants'
import { getSubgraphConfig, SubgraphConfig } from '../../utils/chains'
import { ONE_BI } from '../../utils/constants'
import {
updatePoolDayData,
updatePoolHourData,
Expand All @@ -17,7 +18,9 @@ export function handleBurn(event: BurnEvent): void {
}

// Note: this handler need not adjust TVL because that is accounted for in the handleCollect handler
export function handleBurnHelper(event: BurnEvent, factoryAddress: string = FACTORY_ADDRESS): void {
export function handleBurnHelper(event: BurnEvent, subgraphConfig: SubgraphConfig = getSubgraphConfig()): void {
const factoryAddress = subgraphConfig.factoryAddress

const bundle = Bundle.load('1')!
const poolAddress = event.address.toHexString()
const pool = Pool.load(poolAddress)!
Expand Down Expand Up @@ -91,7 +94,7 @@ export function handleBurnHelper(event: BurnEvent, factoryAddress: string = FACT
lowerTick.save()
upperTick.save()
}
updateUniswapDayData(event)
updateUniswapDayData(event, factoryAddress)
updatePoolDayData(event)
updatePoolHourData(event)
updateTokenDayData(token0 as Token, event)
Expand Down
16 changes: 8 additions & 8 deletions src/mappings/pool/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ import { BigInt } from '@graphprotocol/graph-ts'
import { Bundle, Collect, Factory, Pool, Token } from '../../types/schema'
import { Collect as CollectEvent } from '../../types/templates/Pool/Pool'
import { convertTokenToDecimal, loadTransaction } from '../../utils'
import { FACTORY_ADDRESS, ONE_BI } from '../../utils/constants'
import { getSubgraphConfig, SubgraphConfig } from '../../utils/chains'
import { ONE_BI } from '../../utils/constants'
import {
updatePoolDayData,
updatePoolHourData,
updateTokenDayData,
updateTokenHourData,
updateUniswapDayData,
} from '../../utils/intervalUpdates'
import { getTrackedAmountUSD, WHITELIST_TOKENS } from '../../utils/pricing'
import { getTrackedAmountUSD } from '../../utils/pricing'

export function handleCollect(event: CollectEvent): void {
handleCollectHelper(event)
}

export function handleCollectHelper(
event: CollectEvent,
factoryAddress: string = FACTORY_ADDRESS,
whitelistTokens: string[] = WHITELIST_TOKENS,
): void {
export function handleCollectHelper(event: CollectEvent, subgraphConfig: SubgraphConfig = getSubgraphConfig()): void {
const factoryAddress = subgraphConfig.factoryAddress
const whitelistTokens = subgraphConfig.whitelistTokens

const bundle = Bundle.load('1')!
const pool = Pool.load(event.address.toHexString())
if (pool == null) {
Expand Down Expand Up @@ -92,7 +92,7 @@ export function handleCollectHelper(
collect.tickUpper = BigInt.fromI32(event.params.tickUpper)
collect.logIndex = event.logIndex

updateUniswapDayData(event)
updateUniswapDayData(event, factoryAddress)
updatePoolDayData(event)
updatePoolHourData(event)
updateTokenDayData(token0 as Token, event)
Expand Down
44 changes: 23 additions & 21 deletions src/mappings/pool/initialize.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
import { BigDecimal, BigInt } from '@graphprotocol/graph-ts'
import { BigInt } from '@graphprotocol/graph-ts'

import { Bundle, Pool, Token } from '../../types/schema'
import { Initialize } from '../../types/templates/Pool/Pool'
import { getSubgraphConfig, SubgraphConfig } from '../../utils/chains'
import { updatePoolDayData, updatePoolHourData } from '../../utils/intervalUpdates'
import {
findEthPerToken,
getEthPriceInUSD,
MINIMUM_ETH_LOCKED,
STABLE_COINS,
STABLECOIN_IS_TOKEN0,
USDC_WETH_03_POOL,
WETH_ADDRESS,
} from '../../utils/pricing'
import { findNativePerToken, getNativePriceInUSD } from '../../utils/pricing'

export function handleInitialize(event: Initialize): void {
handleInitializeHelper(event)
}

export function handleInitializeHelper(
event: Initialize,
stablecoinWrappedNativePoolAddress: string = USDC_WETH_03_POOL,
stablecoinIsToken0: boolean = STABLECOIN_IS_TOKEN0,
wrappedNativeAddress: string = WETH_ADDRESS,
stablecoinAddresses: string[] = STABLE_COINS,
minimumEthLocked: BigDecimal = MINIMUM_ETH_LOCKED,
): void {
export function handleInitializeHelper(event: Initialize, subgraphConfig: SubgraphConfig = getSubgraphConfig()): void {
const stablecoinWrappedNativePoolAddress = subgraphConfig.stablecoinWrappedNativePoolAddress
const stablecoinIsToken0 = subgraphConfig.stablecoinIsToken0
const wrappedNativeAddress = subgraphConfig.wrappedNativeAddress
const stablecoinAddresses = subgraphConfig.stablecoinAddresses
const minimumNativeLocked = subgraphConfig.minimumNativeLocked

// update pool sqrt price and tick
const pool = Pool.load(event.address.toHexString())!
pool.sqrtPrice = event.params.sqrtPriceX96
Expand All @@ -37,16 +29,26 @@ export function handleInitializeHelper(

// update ETH price now that prices could have changed
const bundle = Bundle.load('1')!
bundle.ethPriceUSD = getEthPriceInUSD(stablecoinWrappedNativePoolAddress, stablecoinIsToken0)
bundle.ethPriceUSD = getNativePriceInUSD(stablecoinWrappedNativePoolAddress, stablecoinIsToken0)
bundle.save()

updatePoolDayData(event)
updatePoolHourData(event)

// update token prices
if (token0 && token1) {
token0.derivedETH = findEthPerToken(token0 as Token, wrappedNativeAddress, stablecoinAddresses, minimumEthLocked)
token1.derivedETH = findEthPerToken(token1 as Token, wrappedNativeAddress, stablecoinAddresses, minimumEthLocked)
token0.derivedETH = findNativePerToken(
token0 as Token,
wrappedNativeAddress,
stablecoinAddresses,
minimumNativeLocked,
)
token1.derivedETH = findNativePerToken(
token1 as Token,
wrappedNativeAddress,
stablecoinAddresses,
minimumNativeLocked,
)
token0.save()
token1.save()
}
Expand Down
9 changes: 6 additions & 3 deletions src/mappings/pool/mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { BigInt } from '@graphprotocol/graph-ts'
import { Bundle, Factory, Mint, Pool, Tick, Token } from '../../types/schema'
import { Mint as MintEvent } from '../../types/templates/Pool/Pool'
import { convertTokenToDecimal, loadTransaction } from '../../utils'
import { FACTORY_ADDRESS, ONE_BI } from '../../utils/constants'
import { getSubgraphConfig, SubgraphConfig } from '../../utils/chains'
import { ONE_BI } from '../../utils/constants'
import {
updatePoolDayData,
updatePoolHourData,
Expand All @@ -17,7 +18,9 @@ export function handleMint(event: MintEvent): void {
handleMintHelper(event)
}

export function handleMintHelper(event: MintEvent, factoryAddress: string = FACTORY_ADDRESS): void {
export function handleMintHelper(event: MintEvent, subgraphConfig: SubgraphConfig = getSubgraphConfig()): void {
const factoryAddress = subgraphConfig.factoryAddress

const bundle = Bundle.load('1')!
const poolAddress = event.address.toHexString()
const pool = Pool.load(poolAddress)!
Expand Down Expand Up @@ -122,7 +125,7 @@ export function handleMintHelper(event: MintEvent, factoryAddress: string = FACT
// TODO: Update Tick's volume, fees, and liquidity provider count. Computing these on the tick
// level requires reimplementing some of the swapping code from v3-core.

updateUniswapDayData(event)
updateUniswapDayData(event, factoryAddress)
updatePoolDayData(event)
updatePoolHourData(event)
updateTokenDayData(token0 as Token, event)
Expand Down
51 changes: 28 additions & 23 deletions src/mappings/pool/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { BigDecimal, BigInt } from '@graphprotocol/graph-ts'
import { Bundle, Factory, Pool, Swap, Token } from '../../types/schema'
import { Swap as SwapEvent } from '../../types/templates/Pool/Pool'
import { convertTokenToDecimal, loadTransaction, safeDiv } from '../../utils'
import { FACTORY_ADDRESS, ONE_BI, ZERO_BD } from '../../utils/constants'
import { getSubgraphConfig, SubgraphConfig } from '../../utils/chains'
import { ONE_BI, ZERO_BD } from '../../utils/constants'
import {
updatePoolDayData,
updatePoolHourData,
Expand All @@ -12,33 +13,27 @@ import {
updateUniswapDayData,
} from '../../utils/intervalUpdates'
import {
findEthPerToken,
getEthPriceInUSD,
findNativePerToken,
getNativePriceInUSD,
getTrackedAmountUSD,
MINIMUM_ETH_LOCKED,
sqrtPriceX96ToTokenPrices,
STABLE_COINS,
STABLECOIN_IS_TOKEN0,
USDC_WETH_03_POOL,
WETH_ADDRESS,
WHITELIST_TOKENS,
} from '../../utils/pricing'

export function handleSwap(event: SwapEvent): void {
handleSwapHelper(event)
}

export function handleSwapHelper(
event: SwapEvent,
stablecoinWrappedNativePoolAddress: string = USDC_WETH_03_POOL,
stablecoinIsToken0: boolean = STABLECOIN_IS_TOKEN0,
wrappedNativeAddress: string = WETH_ADDRESS,
stablecoinAddresses: string[] = STABLE_COINS,
minimumEthLocked: BigDecimal = MINIMUM_ETH_LOCKED,
whitelistTokens: string[] = WHITELIST_TOKENS,
): void {
export function handleSwapHelper(event: SwapEvent, subgraphConfig: SubgraphConfig = getSubgraphConfig()): void {
const factoryAddress = subgraphConfig.factoryAddress
const stablecoinWrappedNativePoolAddress = subgraphConfig.stablecoinWrappedNativePoolAddress
const stablecoinIsToken0 = subgraphConfig.stablecoinIsToken0
const wrappedNativeAddress = subgraphConfig.wrappedNativeAddress
const stablecoinAddresses = subgraphConfig.stablecoinAddresses
const minimumNativeLocked = subgraphConfig.minimumNativeLocked
const whitelistTokens = subgraphConfig.whitelistTokens

const bundle = Bundle.load('1')!
const factory = Factory.load(FACTORY_ADDRESS)!
const factory = Factory.load(factoryAddress)!
const pool = Pool.load(event.address.toHexString())!

// hot fix for bad pricing
Expand Down Expand Up @@ -133,10 +128,20 @@ export function handleSwapHelper(
pool.save()

// update USD pricing
bundle.ethPriceUSD = getEthPriceInUSD(stablecoinWrappedNativePoolAddress, stablecoinIsToken0)
bundle.ethPriceUSD = getNativePriceInUSD(stablecoinWrappedNativePoolAddress, stablecoinIsToken0)
bundle.save()
token0.derivedETH = findEthPerToken(token0 as Token, wrappedNativeAddress, stablecoinAddresses, minimumEthLocked)
token1.derivedETH = findEthPerToken(token1 as Token, wrappedNativeAddress, stablecoinAddresses, minimumEthLocked)
token0.derivedETH = findNativePerToken(
token0 as Token,
wrappedNativeAddress,
stablecoinAddresses,
minimumNativeLocked,
)
token1.derivedETH = findNativePerToken(
token1 as Token,
wrappedNativeAddress,
stablecoinAddresses,
minimumNativeLocked,
)

/**
* Things afffected by new USD rates
Expand Down Expand Up @@ -171,7 +176,7 @@ export function handleSwapHelper(
swap.logIndex = event.logIndex

// interval data
const uniswapDayData = updateUniswapDayData(event)
const uniswapDayData = updateUniswapDayData(event, factoryAddress)
const poolDayData = updatePoolDayData(event)
const poolHourData = updatePoolHourData(event)
const token0DayData = updateTokenDayData(token0 as Token, event)
Expand Down
Loading

0 comments on commit 493371d

Please sign in to comment.