Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update network to optimism #220

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/mappings/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { STATIC_TOKEN_DEFINITIONS, StaticTokenDefinition } from '../utils/static
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 { populateEmptyPools } from '../utils/backfill'

// 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 @@ -22,8 +23,15 @@ export function handlePoolCreatedHelper(
whitelistTokens: string[] = WHITELIST_TOKENS,
staticTokenDefinitions: StaticTokenDefinition[] = STATIC_TOKEN_DEFINITIONS,
): void {
// temp fix
if (event.params.pool == Address.fromHexString('0x8fe8d9bb8eeba3ed688069c3d6b556c9ca258248')) {
// fix for pool overflow - this pool has a token that overflows on one of its values, but theres no way in
// assembly ts to error catch for this.
// Transaction - https://optimistic.etherscan.io/tx/0x16312a52237ce08e4bb7534648f4c8da6cd4c192f0b955cf6770b2d347f19d2b
if (
event.params.pool === Address.fromHexString('0x282b7d6bef6c78927f394330dca297eca2bd18cd') ||
event.params.pool === Address.fromHexString('0x5738de8d0b864d5ef5d65b9e05b421b71f2c2eb4') ||
event.params.pool === Address.fromHexString('0x5500721e5a063f0396c5e025a640e8491eb89aac') ||
event.params.pool === Address.fromHexString('0x1ffd370f9d01f75de2cc701956886acec9749e80')
) {
return
}

Expand All @@ -48,6 +56,9 @@ export function handlePoolCreatedHelper(
const bundle = new Bundle('1')
bundle.ethPriceUSD = ZERO_BD
bundle.save()

// on factory creation, create the inital pools from pre-regenesis
populateEmptyPools(event)
}

factory.poolCount = factory.poolCount.plus(ONE_BI)
Expand Down
4 changes: 2 additions & 2 deletions src/mappings/pool/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { Bundle, Pool, Token } from '../../types/schema'
import { Initialize } from '../../types/templates/Pool/Pool'
import { updatePoolDayData, updatePoolHourData } from '../../utils/intervalUpdates'
import {
DAI_WETH_03_POOL,
findEthPerToken,
getEthPriceInUSD,
MINIMUM_ETH_LOCKED,
STABLE_COINS,
STABLECOIN_IS_TOKEN0,
USDC_WETH_05_POOL,
WETH_ADDRESS,
} from '../../utils/pricing'

Expand All @@ -19,7 +19,7 @@ export function handleInitialize(event: Initialize): void {

export function handleInitializeHelper(
event: Initialize,
stablecoinWrappedNativePoolAddress: string = USDC_WETH_05_POOL,
stablecoinWrappedNativePoolAddress: string = DAI_WETH_03_POOL,
stablecoinIsToken0: boolean = STABLECOIN_IS_TOKEN0,
wrappedNativeAddress: string = WETH_ADDRESS,
stablecoinAddresses: string[] = STABLE_COINS,
Expand Down
635 changes: 635 additions & 0 deletions src/poolMappings.ts

Large diffs are not rendered by default.

123 changes: 123 additions & 0 deletions src/utils/backfill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/* eslint-disable prefer-const */
import { Address, BigInt, ethereum } from '@graphprotocol/graph-ts'
import { POOL_MAPPINGS } from '../poolMappings'
import { Pool, Token } from '../types/schema'
import { Pool as PoolABI } from '../types/Factory/Pool'
import { ZERO_BD, ZERO_BI } from './constants'
import { fetchTokenDecimals, fetchTokenName, fetchTokenSymbol, fetchTokenTotalSupply } from './token'
import { WHITELIST_TOKENS } from './pricing'
import { Pool as PoolTemplate } from '../types/templates'
import { ERC20 } from '../types/Factory/ERC20'
import { convertTokenToDecimal } from '.'

function populateToken(tokenAddress: string): void {
let token = Token.load(tokenAddress)
if (token != null) {
return
}
token = new Token(tokenAddress)
token.symbol = fetchTokenSymbol(Address.fromString(tokenAddress))
token.name = fetchTokenName(Address.fromString(tokenAddress))
token.totalSupply = fetchTokenTotalSupply(Address.fromString(tokenAddress))
let decimals = fetchTokenDecimals(Address.fromString(tokenAddress))
if (decimals === null) {
return
}
token.decimals = decimals
token.derivedETH = ZERO_BD
token.volume = ZERO_BD
token.volumeUSD = ZERO_BD
token.feesUSD = ZERO_BD
token.untrackedVolumeUSD = ZERO_BD
token.totalValueLocked = ZERO_BD
token.totalValueLockedUSD = ZERO_BD
token.totalValueLockedUSDUntracked = ZERO_BD
token.txCount = ZERO_BI
token.poolCount = ZERO_BI
token.whitelistPools = []
token.save()
}

/**
* Create entries in store for each pool and token
* before regenesis.
*/
export function populateEmptyPools(event: ethereum.Event): void {
let length = POOL_MAPPINGS.length
for (let i = 0; i < length; ++i) {
let poolMapping = POOL_MAPPINGS[i]
let newAddress = poolMapping[1]
let token0Address = poolMapping[2]
let token1Address = poolMapping[3]

let poolContract = PoolABI.bind(newAddress)
let pool = new Pool(newAddress.toHexString()) as Pool
pool.createdAtBlockNumber = event.block.number
pool.createdAtTimestamp = event.block.timestamp
pool.token0 = token0Address.toHexString()
pool.token1 = token1Address.toHexString()
pool.liquidity = poolContract.liquidity()
pool.sqrtPrice = ZERO_BI
pool.token0Price = ZERO_BD
pool.token1Price = ZERO_BD
pool.observationIndex = ZERO_BI
pool.liquidityProviderCount = ZERO_BI
pool.txCount = ZERO_BI
pool.totalValueLockedToken0 = ZERO_BD
pool.totalValueLockedToken1 = ZERO_BD
pool.totalValueLockedETH = ZERO_BD
pool.totalValueLockedUSD = ZERO_BD
pool.totalValueLockedUSDUntracked = ZERO_BD
pool.volumeToken0 = ZERO_BD
pool.volumeToken1 = ZERO_BD
pool.volumeUSD = ZERO_BD
pool.untrackedVolumeUSD = ZERO_BD
pool.feesUSD = ZERO_BD
pool.collectedFeesToken0 = ZERO_BD
pool.collectedFeesToken1 = ZERO_BD
pool.collectedFeesUSD = ZERO_BD

// need fee tier
let feeTier = poolContract.fee()
pool.feeTier = BigInt.fromI32(feeTier)

// create token entities if needed
populateToken(token0Address.toHexString())
populateToken(token1Address.toHexString())
let token0 = Token.load(token0Address.toHexString())
let token1 = Token.load(token1Address.toHexString())

if (token0 && token1) {
if (WHITELIST_TOKENS.includes(pool.token0)) {
let newPools = token1.whitelistPools
newPools.push(pool.id)
token1.whitelistPools = newPools
}

if (WHITELIST_TOKENS.includes(token1.id)) {
let newPools = token0.whitelistPools
newPools.push(pool.id)
token0.whitelistPools = newPools
}

// populate the TVL by call contract balanceOf
let token0Contract = ERC20.bind(Address.fromString(pool.token0))
let tvlToken0Raw = token0Contract.balanceOf(Address.fromString(pool.id))
let tvlToken0Adjusted = convertTokenToDecimal(tvlToken0Raw, token0.decimals)
pool.totalValueLockedToken0 = tvlToken0Adjusted
token0.totalValueLocked = tvlToken0Adjusted

let token1Contract = ERC20.bind(Address.fromString(pool.token1))
let tvlToken1Raw = token1Contract.balanceOf(Address.fromString(pool.id))
let tvlToken1Adjusted = convertTokenToDecimal(tvlToken1Raw, token1.decimals)
pool.totalValueLockedToken1 = tvlToken1Adjusted
token1.totalValueLocked = tvlToken1Adjusted

// add pool to tracked address and store entities
PoolTemplate.create(Address.fromString(pool.id))
token0.save()
token1.save()
pool.save()
}
}
}
2 changes: 1 addition & 1 deletion src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Address, BigDecimal, BigInt } from '@graphprotocol/graph-ts'
import { Factory as FactoryContract } from '../types/templates/Pool/Factory'

export const ADDRESS_ZERO = '0x0000000000000000000000000000000000000000'
export const FACTORY_ADDRESS = '0x33128a8fC17869897dcE68Ed026d694621f6FDfD'
export const FACTORY_ADDRESS = '0x1F98431c8aD98523631AE4a59f267346ea31F984'

export const ZERO_BI = BigInt.fromI32(0)
export const ONE_BI = BigInt.fromI32(1)
Expand Down
34 changes: 24 additions & 10 deletions src/utils/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,45 @@ import { Bundle, Pool, Token } from './../types/schema'
import { ONE_BD, ZERO_BD, ZERO_BI } from './constants'

export const WETH_ADDRESS = '0x4200000000000000000000000000000000000006'
export const USDC_WETH_05_POOL = '0x4c36388be6f416a29c8d8eee81c771ce6be14b18'
export const DAI_WETH_03_POOL = '0x03af20bdaaffb4cc0a521796a223f7d85e2aac31'
export const STABLECOIN_IS_TOKEN0 = false

const USDC_ADDRESS = '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913'
const DAI_ADDRESS = '0xda10009cbd5d07dd0cecc66161fc93d7c9000da1'
const USDC_ADDRESS = '0x7f5c764cbc14f9669b88837ca1490cca17c31607'
const USDT_ADDRESS = '0x94b008aa00579c1307b0ef2c499ad98a8ce58e58'

// token where amounts should contribute to tracked volume and liquidity
// usually tokens that many tokens are paired with s
export const WHITELIST_TOKENS: string[] = [WETH_ADDRESS, USDC_ADDRESS]

export const STABLE_COINS: string[] = [USDC_ADDRESS]

export const MINIMUM_ETH_LOCKED = BigDecimal.fromString('1')
// usually tokens that many tokens are paired with
export const WHITELIST_TOKENS: string[] = [
WETH_ADDRESS, // WETH,
DAI_ADDRESS,
USDC_ADDRESS,
USDT_ADDRESS,
'0x4200000000000000000000000000000000000042', // OP
'0x9e1028f5f1d5ede59748ffcee5532509976840e0', // PERP
'0x50c5725949a6f0c72e6c4a641f24049a917db0cb', // LYRA
'0x68f180fcce6836688e9084f035309e29bf0a2095', // WBTC
]

export const STABLE_COINS: string[] = [DAI_ADDRESS, USDC_ADDRESS, USDT_ADDRESS]

export const MINIMUM_ETH_LOCKED = BigDecimal.fromString('10')

const Q192 = BigInt.fromI32(2).pow(192 as u8)
export function sqrtPriceX96ToTokenPrices(sqrtPriceX96: BigInt, token0: Token, token1: Token): BigDecimal[] {
const num = sqrtPriceX96.times(sqrtPriceX96).toBigDecimal()
const denom = BigDecimal.fromString(Q192.toString())
const price1 = num.div(denom).times(exponentToBigDecimal(token0.decimals)).div(exponentToBigDecimal(token1.decimals))
const price1 = num
.div(denom)
.times(exponentToBigDecimal(token0.decimals))
.div(exponentToBigDecimal(token1.decimals))

const price0 = safeDiv(BigDecimal.fromString('1'), price1)
return [price0, price1]
}

export function getEthPriceInUSD(
stablecoinWrappedNativePoolAddress: string = USDC_WETH_05_POOL,
stablecoinWrappedNativePoolAddress: string = DAI_WETH_03_POOL,
stablecoinIsToken0: boolean = STABLECOIN_IS_TOKEN0, // true is stablecoin is token0, false if stablecoin is token1
): BigDecimal {
const stablecoinWrappedNativePool = Pool.load(stablecoinWrappedNativePoolAddress)
Expand Down
9 changes: 8 additions & 1 deletion src/utils/staticTokenDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ export const getStaticDefinition = (
return null
}

export const STATIC_TOKEN_DEFINITIONS: Array<StaticTokenDefinition> = []
export const STATIC_TOKEN_DEFINITIONS: Array<StaticTokenDefinition> = [
{
address: Address.fromString('0x82af49447d8a07e3bd95bd0d56f35241523fbab1'),
symbol: 'WETH',
name: 'Wrapped Ethereum',
decimals: BigInt.fromI32(18)
}
]
8 changes: 4 additions & 4 deletions subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ features:
dataSources:
- kind: ethereum/contract
name: Factory
network: base
network: optimism
source:
address: '0x33128a8fC17869897dcE68Ed026d694621f6FDfD'
address: '0x1F98431c8aD98523631AE4a59f267346ea31F984'
abi: Factory
startBlock: 2009445
startBlock: 0
mapping:
kind: ethereum/events
apiVersion: 0.0.7
Expand All @@ -39,7 +39,7 @@ dataSources:
templates:
- kind: ethereum/contract
name: Pool
network: base
network: optimism
source:
abi: Pool
mapping:
Expand Down