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

Add updates to handle usdc #2

Open
wants to merge 2 commits into
base: 03-06-Update_subgraph_for_zora_network
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ type UniswapFactory @entity {
txCount: BigInt!
}

type Block @entity {
id: ID!
number: BigInt!
timestamp: BigInt!
parentHash: String
author: String
difficulty: BigInt
totalDifficulty: BigInt
gasUsed: BigInt
gasLimit: BigInt
receiptsRoot: String
transactionsRoot: String
stateRoot: String
size: BigInt
unclesHash: String
}

type Token @entity {
# token address
id: ID!
Expand Down
2 changes: 1 addition & 1 deletion src/mappings/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ export function handleMint(event: Mint): void {
mint.save()

// update the LP position
const liquidityPosition = createLiquidityPosition(event.address, Address.fromBytes(mint.to!))
const liquidityPosition = createLiquidityPosition(event.address, Address.fromBytes(mint.to))
if (!liquidityPosition) {
return;
}
Expand Down
23 changes: 21 additions & 2 deletions src/mappings/factory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable prefer-const */
import { log } from '@graphprotocol/graph-ts'
import { log, ethereum } from '@graphprotocol/graph-ts'
import { PairCreated } from '../types/Factory/Factory'
import { Bundle, Pair, Token, UniswapFactory } from '../types/schema'
import { Bundle, Pair, Token, UniswapFactory, Block } from '../types/schema'
import { Pair as PairTemplate } from '../types/templates'
import {
FACTORY_ADDRESS,
Expand All @@ -13,6 +13,25 @@ import {
ZERO_BI
} from './helpers'

export function handleBlock(block: ethereum.Block): void {
let id = block.hash.toHex()
let blockEntity = new Block(id);
blockEntity.number = block.number;
blockEntity.timestamp = block.timestamp;
blockEntity.parentHash = block.parentHash.toHex();
blockEntity.author = block.author.toHex();
blockEntity.difficulty = block.difficulty;
blockEntity.totalDifficulty = block.totalDifficulty;
blockEntity.gasUsed = block.gasUsed;
blockEntity.gasLimit = block.gasLimit;
blockEntity.receiptsRoot = block.receiptsRoot.toHex();
blockEntity.transactionsRoot = block.transactionsRoot.toHex();
blockEntity.stateRoot = block.stateRoot.toHex();
blockEntity.size = block.size;
blockEntity.unclesHash = block.unclesHash.toHex();
blockEntity.save();
}

export function handleNewPair(event: PairCreated): void {
// load factory (create if first exchange)
let factory = UniswapFactory.load(FACTORY_ADDRESS)
Expand Down
26 changes: 26 additions & 0 deletions src/mappings/handleBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
EthereumBlock
} from "@graphprotocol/graph-ts"

import {
Block
} from "../../generated/schema"

export function handleBlock(block: EthereumBlock): void {
let id = block.hash.toHex()
let blockEntity = new Block(id);
blockEntity.number = block.number;
blockEntity.timestamp = block.timestamp;
blockEntity.parentHash = block.parentHash.toHex();
blockEntity.author = block.author.toHex();
blockEntity.difficulty = block.difficulty;
blockEntity.totalDifficulty = block.totalDifficulty;
blockEntity.gasUsed = block.gasUsed;
blockEntity.gasLimit = block.gasLimit;
blockEntity.receiptsRoot = block.receiptsRoot.toHex();
blockEntity.transactionsRoot = block.transactionsRoot.toHex();
blockEntity.stateRoot = block.stateRoot.toHex();
blockEntity.size = block.size;
blockEntity.unclesHash = block.unclesHash.toHex();
blockEntity.save();
}
48 changes: 16 additions & 32 deletions src/mappings/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import { BigDecimal, Address, BigInt } from '@graphprotocol/graph-ts/index'
import { ZERO_BD, factoryContract, ADDRESS_ZERO, ONE_BD, UNTRACKED_PAIRS } from './helpers'

const WETH_ADDRESS = '0x4200000000000000000000000000000000000006'
// const USDC_WETH_PAIR = '0xb4e16d0168e52d35cacd2c6185b44281ec28c9dc' // created 10008355
const USDC_WETH_PAIR = '0x88ac3948338b624b0a66015d8a9c9d1d7ed9fdad' // created block 11616039
// const DAI_WETH_PAIR = '0xa478c2975ab1ea89e8196811f51a7b7ade33eb11' // created block 10042267
// const USDT_WETH_PAIR = '0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852' // created block 10093341

export function getEthPriceInUSD(): BigDecimal {
return ZERO_BD;
// fetch eth prices for each stablecoin
let daiPair = Pair.load(DAI_WETH_PAIR) // dai is token0
let usdcPair = Pair.load(USDC_WETH_PAIR) // usdc is token0
let usdtPair = Pair.load(USDT_WETH_PAIR) // usdt is token1
// let daiPair = Pair.load(DAI_WETH_PAIR) // dai is token0
let usdcPair = Pair.load(USDC_WETH_PAIR) // usdc is token1
// let usdtPair = Pair.load(USDT_WETH_PAIR) // usdt is token1

// all 3 have been created
/*
if (daiPair !== null && usdcPair !== null && usdtPair !== null) {
let totalLiquidityETH = daiPair.reserve1.plus(usdcPair.reserve1).plus(usdtPair.reserve0)
let daiWeight = daiPair.reserve1.div(totalLiquidityETH)
Expand All @@ -32,8 +32,9 @@ export function getEthPriceInUSD(): BigDecimal {
let usdcWeight = usdcPair.reserve1.div(totalLiquidityETH)
return daiPair.token0Price.times(daiWeight).plus(usdcPair.token0Price.times(usdcWeight))
// USDC is the only pair so far
} else if (usdcPair !== null) {
return usdcPair.token0Price
} else*/
if (usdcPair !== null) {
return usdcPair.token1Price;
} else {
return ZERO_BD
}
Expand All @@ -42,30 +43,11 @@ export function getEthPriceInUSD(): BigDecimal {
// token where amounts should contribute to tracked volume and liquidity
let WHITELIST: string[] = [
'0x4200000000000000000000000000000000000006', // WETH
// '0x6b175474e89094c44da98b954eedeac495271d0f', // DAI
// '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', // USDC
// '0xdac17f958d2ee523a2206206994597c13d831ec7', // USDT
// '0x0000000000085d4780b73119b644ae5ecd22b376', // TUSD
// '0x5d3a536e4d6dbd6114cc1ead35777bab948e3643', // cDAI
// '0x39aa39c021dfbae8fac545936693ac917d5e7563', // cUSDC
// '0x86fadb80d8d2cff3c3680819e4da99c10232ba0f', // EBASE
// '0x57ab1ec28d129707052df4df418d58a2d46d5f51', // sUSD
// '0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2', // MKR
// '0xc00e94cb662c3520282e6f5717214004a7f26888', // COMP
// '0x514910771af9ca656af840dff83e8264ecf986ca', //LINK
// '0x960b236a07cf122663c4303350609a66a7b288c0', //ANT
// '0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f', //SNX
// '0x0bc529c00c6401aef6d220be8c6ea1667f6ad93e', //YFI
// '0xdf5e0e81dff6faf3a7e52ba697820c5e32d806a8', // yCurv
// '0x853d955acef822db058eb8505911ed77f175b99e', // FRAX
// '0xa47c8bf37f92abed4a126bda807a7b7498661acd', // WUST
// '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984', // UNI
// '0x2260fac5e5542a773aa44fbcfedf7c193bc2c599', // WBTC
// '0x956f47f50a910163d8bf957cf5846d573e7f87ca' // FEI
'0xCccCCccc7021b32EBb4e8C08314bD62F7c653EC4', // USDC
]

// minimum liquidity required to count towards tracked volume for pairs with small # of Lps
let MINIMUM_USD_THRESHOLD_NEW_PAIRS = BigDecimal.fromString('400000')
let MINIMUM_USD_THRESHOLD_NEW_PAIRS = BigDecimal.fromString('30000')

// minimum liquidity for price to get tracked
let MINIMUM_LIQUIDITY_THRESHOLD_ETH = BigDecimal.fromString('2')
Expand All @@ -78,12 +60,14 @@ export function findEthPerToken(token: Token): BigDecimal {
if (token.id == WETH_ADDRESS) {
return ONE_BD
}
return ZERO_BD;
// loop through whitelist and check if paired with any
for (let i = 0; i < WHITELIST.length; ++i) {
let pairAddress = factoryContract.getPair(Address.fromString(token.id), Address.fromString(WHITELIST[i]))
if (pairAddress.toHexString() != ADDRESS_ZERO) {
let pair = Pair.load(pairAddress.toHexString())
let pairAddress = factoryContract.try_getPair(Address.fromString(token.id), Address.fromString(WHITELIST[i]))
if (pairAddress.reverted) {
continue;
}
if (!pairAddress.reverted && pairAddress.value.toHexString() != ADDRESS_ZERO) {
let pair = Pair.load(pairAddress.value.toHexString())
if (pair!.token0 == token.id && pair!.reserveETH.gt(MINIMUM_LIQUIDITY_THRESHOLD_ETH)) {
let token1 = Token.load(pair!.token1)
return pair!.token1Price.times(token1!.derivedETH as BigDecimal) // return token1 per our token * Eth per token 1
Expand Down
2 changes: 2 additions & 0 deletions subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ dataSources:
eventHandlers:
- event: PairCreated(indexed address,indexed address,address,uint256)
handler: handleNewPair
blockHandlers:
- handler: handleBlock
templates:
- kind: ethereum/contract
name: Pair
Expand Down