Skip to content

Commit

Permalink
update network to optimism
Browse files Browse the repository at this point in the history
  • Loading branch information
mzywang committed Apr 25, 2024
1 parent 2646b75 commit 3b4a84e
Show file tree
Hide file tree
Showing 7 changed files with 806 additions and 18 deletions.
14 changes: 12 additions & 2 deletions src/mappings/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ import { Pool as PoolTemplate } from '../types/templates'
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'

export function handlePoolCreated(event: PoolCreated): 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 @@ -35,6 +43,8 @@ export function handlePoolCreated(event: PoolCreated): void {
const bundle = new Bundle('1')
bundle.ethPriceUSD = ZERO_BD
bundle.save()

populateEmptyPools(event)
}

factory.poolCount = factory.poolCount.plus(ONE_BI)
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
33 changes: 23 additions & 10 deletions src/utils/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,30 @@ import { Bundle, Pool, Token } from './../types/schema'
import { ONE_BD, ZERO_BD, ZERO_BI } from './constants'

const WETH_ADDRESS = '0x4200000000000000000000000000000000000006'
const USDC_WETH_05_POOL = '0x4c36388be6f416a29c8d8eee81c771ce6be14b18'
const DAI_WETH_03_POOL = '0x03af20bdaaffb4cc0a521796a223f7d85e2aac31'

const DAI_ADDRESS = '0xda10009cbd5d07dd0cecc66161fc93d7c9000da1' // DAI
const USDC_ADDRESS = '0x7f5c764cbc14f9669b88837ca1490cca17c31607' // USDC
const USDT_ADDRESS = '0x94b008aa00579c1307b0ef2c499ad98a8ce58e58' // USDT

const USDC_ADDRESS = '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913'

// 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]

const STABLE_COINS: string[] = [USDC_ADDRESS]

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
]

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

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[] {
Expand All @@ -29,9 +42,9 @@ export function sqrtPriceX96ToTokenPrices(sqrtPriceX96: BigInt, token0: Token, t

export function getEthPriceInUSD(): BigDecimal {
// fetch eth prices for each stablecoin
const usdcPool = Pool.load(USDC_WETH_05_POOL) // usdc is token1
if (usdcPool !== null) {
return usdcPool.token1Price
const daiPool = Pool.load(DAI_WETH_03_POOL) // dai is token1
if (daiPool !== null) {
return daiPool.token1Price
} else {
return ZERO_BD
}
Expand Down
9 changes: 8 additions & 1 deletion src/utils/staticTokenDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ export class StaticTokenDefinition {

// Get all tokens with a static defintion
static getStaticDefinitions(): Array<StaticTokenDefinition> {
const staticDefinitions: Array<StaticTokenDefinition> = []
const staticDefinitions: Array<StaticTokenDefinition> = [
{
address: Address.fromString('0x82af49447d8a07e3bd95bd0d56f35241523fbab1'),
symbol: 'WETH',
name: 'Wrapped Ethereum',
decimals: BigInt.fromI32(18)
}
]
return staticDefinitions
}

Expand Down
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

0 comments on commit 3b4a84e

Please sign in to comment.