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 ce011ec commit 35c3e7f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 33 deletions.
17 changes: 12 additions & 5 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,9 +23,6 @@ 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")
Expand Down Expand Up @@ -73,3 +68,15 @@ type Swap @entity {
amount1Out: BigDecimal!
to: Bytes!
}

type Transfer @entity {
id: ID! # transactionhash#logIndex
timestamp: BigInt!
blockNumber: BigInt!
pair: Pair!
token0: Token!
token1: Token!
from: Bytes!
to: Bytes!
value: BigDecimal!
}
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
44 changes: 22 additions & 22 deletions src/mappings/pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,30 @@ 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, 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 +111,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()
}
4 changes: 2 additions & 2 deletions subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 35c3e7f

Please sign in to comment.