Skip to content

Commit

Permalink
v2-events
Browse files Browse the repository at this point in the history
  • Loading branch information
mzywang committed Dec 9, 2024
1 parent c51ee7a commit e809280
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 41 deletions.
74 changes: 74 additions & 0 deletions networks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"arbitrum-one": {
"Factory": {
"address": "0xf1D7CC64Fb4452F05c498126312eBE29f30Fbcf9",
"startBlock": 150442611
}
},
"avalanche": {
"Factory": {
"address": "0x9e5A52f57b3038F1B8EeE45F28b3C1967e22799C",
"startBlock": 37767795
}
},
"base": {
"Factory": {
"address": "0x8909Dc15e40173Ff4699343b6eB8132c65e18eC6",
"startBlock": 6601915
}
},
"blast-mainnet": {
"Factory": {
"address": "0x5C346464d33F90bABaf70dB6388507CC889C1070",
"startBlock": 399405
}
},
"bsc": {
"Factory": {
"address": "0x8909Dc15e40173Ff4699343b6eB8132c65e18eC6",
"startBlock": 33496018
}
},
"celo": {
"Factory": {
"address": "0x79a530c8e2fA8748B7B40dd3629C0520c2cCf03f",
"startBlock": 21306953
}
},
"mainnet": {
"Factory": {
"address": "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f",
"startBlock": 10000834
}
},
"matic": {
"Factory": {
"address": "0x9e5A52f57b3038F1B8EeE45F28b3C1967e22799C",
"startBlock": 49948178
}
},
"optimism": {
"Factory": {
"address": "0x0c3c1c532F1e39EdF36BE9Fe0bE1410313E074Bf",
"startBlock": 112197986
}
},
"zora-mainnet": {
"Factory": {
"address": "0x0F797dC7efaEA995bB916f268D919d0a1950eE3C",
"startBlock": 10973308
}
},
"worldchain-mainnet": {
"Factory": {
"address": "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f",
"startBlock": 4063439
}
},
"sepolia": {
"Factory": {
"address": "0xF62c03E08ada871A0bEb309762E260a7a6a880E6",
"startBlock": 6918791
}
}
}
23 changes: 15 additions & 8 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ type Token @entity {
id: ID! # token address, lowercased
decimals: BigInt!

totalLiquidity: BigDecimal!

pairsAsToken0: [Pair!]! @derivedFrom(field: "token0")
pairsAsToken1: [Pair!]! @derivedFrom(field: "token1")
mintsAsToken0: [Mint!]! @derivedFrom(field: "token0")
Expand All @@ -25,15 +23,12 @@ type Pair @entity {
createdAtTimestamp: BigInt!
createdAtBlockNumber: BigInt!

reserve0: BigDecimal!
reserve1: BigDecimal!

mints: [Mint!]! @derivedFrom(field: "pair")
burns: [Burn!]! @derivedFrom(field: "pair")
swaps: [Swap!]! @derivedFrom(field: "pair")
}

type Mint @entity {
type Mint @entity(immutable: true) {
id: ID! # transactionhash#logIndex
timestamp: BigInt!
blockNumber: BigInt!
Expand All @@ -46,7 +41,7 @@ type Mint @entity {
amount1: BigDecimal!
}

type Burn @entity {
type Burn @entity(immutable: true) {
id: ID! # transactionhash#logIndex
timestamp: BigInt!
blockNumber: BigInt!
Expand All @@ -59,7 +54,7 @@ type Burn @entity {
to: Bytes!
}

type Swap @entity {
type Swap @entity(immutable: true) {
id: ID! # transactionhash#logIndex
timestamp: BigInt!
blockNumber: BigInt!
Expand All @@ -73,3 +68,15 @@ type Swap @entity {
amount1Out: BigDecimal!
to: Bytes!
}

type Transfer @entity(immutable: true) {
id: ID! # transactionhash#logIndex
timestamp: BigInt!
blockNumber: BigInt!
pair: Pair!
token0: Token!
token1: Token!
from: Bytes!
to: Bytes!
value: BigInt!
}
4 changes: 0 additions & 4 deletions src/mappings/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export function handleNewPair(event: PairCreated): void {

const token0 = new Token(event.params.token0.toHexString())
token0.decimals = decimals
token0.totalLiquidity = ZERO_BD
token0.save()
}

Expand All @@ -40,7 +39,6 @@ export function handleNewPair(event: PairCreated): void {

const token1 = new Token(event.params.token1.toHexString())
token1.decimals = decimals
token1.totalLiquidity = ZERO_BD
token1.save()
}

Expand All @@ -49,8 +47,6 @@ export function handleNewPair(event: PairCreated): void {
pair.token1 = event.params.token1.toHexString()
pair.createdAtTimestamp = event.block.timestamp
pair.createdAtBlockNumber = event.block.number
pair.reserve0 = ZERO_BD
pair.reserve1 = ZERO_BD
pair.save()

PairTemplate.create(event.params.pair)
Expand Down
53 changes: 30 additions & 23 deletions src/mappings/pair.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
import { BigDecimal } from '@graphprotocol/graph-ts'
import { log } from '@graphprotocol/graph-ts'

import { Burn as BurnEntity, Mint as MintEntity, Pair, Swap as SwapEntity, Token } from '../types/schema'
import { Burn, Mint, Swap, Sync } from '../types/templates/Pair/Pair'
import {
Burn as BurnEntity,
Mint as MintEntity,
Pair,
Swap as SwapEntity,
Token,
Transfer as TransferEntity,
} from '../types/schema'
import { Burn, Mint, Swap, Transfer } from '../types/templates/Pair/Pair'
import { convertTokenToDecimal } from './helpers'

export function handleTransfer(event: Transfer): void {
const block = event.block
const transaction = event.transaction
const transferId = transaction.hash.toHex() + '#' + event.logIndex.toString()

const pair = Pair.load(event.address.toHex())!
const token0 = Token.load(pair.token0)!
const token1 = Token.load(pair.token1)!

const transfer = new TransferEntity(transferId)
transfer.timestamp = block.timestamp
transfer.blockNumber = block.number
transfer.pair = pair.id
transfer.token0 = token0.id
transfer.token1 = token1.id
transfer.from = event.params.from
transfer.to = event.params.to
transfer.value = event.params.value
transfer.save()
}

export function handleMint(event: Mint): void {
const block = event.block
const transaction = event.transaction
Expand Down Expand Up @@ -90,24 +118,3 @@ export function handleSwap(event: Swap): void {
swap.to = event.params.to
swap.save()
}

export function handleSync(event: Sync): void {
const pair = Pair.load(event.address.toHex())!
const token0 = Token.load(pair.token0)!
const token1 = Token.load(pair.token1)!

const reserve0 = convertTokenToDecimal(event.params.reserve0, token0.decimals)
const reserve1 = convertTokenToDecimal(event.params.reserve1, token1.decimals)

const oldReserve0 = pair.reserve0
const oldReserve1 = pair.reserve1

pair.reserve0 = reserve0
pair.reserve1 = reserve1
pair.save()

token0.totalLiquidity = token0.totalLiquidity.plus(reserve0.minus(oldReserve0))
token1.totalLiquidity = token1.totalLiquidity.plus(reserve1.minus(oldReserve1))
token0.save()
token1.save()
}
12 changes: 6 additions & 6 deletions subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ schema:
dataSources:
- kind: ethereum/contract
name: Factory
network: mainnet
network: arbitrum-one
source:
address: '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f'
abi: Factory
startBlock: 10000834
address: "0xf1D7CC64Fb4452F05c498126312eBE29f30Fbcf9"
startBlock: 150442611
mapping:
kind: ethereum/events
apiVersion: 0.0.9
Expand All @@ -31,7 +31,7 @@ dataSources:
templates:
- kind: ethereum/contract
name: Pair
network: mainnet
network: arbitrum-one
source:
abi: Pair
mapping:
Expand All @@ -54,5 +54,5 @@ templates:
handler: handleBurn
- event: Swap(indexed address,uint256,uint256,uint256,uint256,indexed address)
handler: handleSwap
- event: Sync(uint112,uint112)
handler: handleSync
- event: Transfer(indexed address,indexed address,uint256)
handler: handleTransfer

0 comments on commit e809280

Please sign in to comment.