From 35c3e7f538b29e073ec4913c7d858590b0ebf6ae Mon Sep 17 00:00:00 2001 From: Michael Wang <44713145+mzywang@users.noreply.github.com> Date: Mon, 9 Dec 2024 12:59:04 -0500 Subject: [PATCH] v2-events --- schema.graphql | 17 +++++++++++----- src/mappings/factory.ts | 4 ---- src/mappings/pair.ts | 44 ++++++++++++++++++++--------------------- subgraph.yaml | 4 ++-- 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/schema.graphql b/schema.graphql index ae86b774..0ad0a44e 100644 --- a/schema.graphql +++ b/schema.graphql @@ -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") @@ -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") @@ -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! +} \ No newline at end of file diff --git a/src/mappings/factory.ts b/src/mappings/factory.ts index 2d05129a..b52a63d3 100644 --- a/src/mappings/factory.ts +++ b/src/mappings/factory.ts @@ -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() } @@ -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() } @@ -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) diff --git a/src/mappings/pair.ts b/src/mappings/pair.ts index dc072702..8b057e17 100644 --- a/src/mappings/pair.ts +++ b/src/mappings/pair.ts @@ -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 @@ -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() -} diff --git a/subgraph.yaml b/subgraph.yaml index 83365a97..9562b7f2 100644 --- a/subgraph.yaml +++ b/subgraph.yaml @@ -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 \ No newline at end of file + - event: Transfer(indexed address,indexed address,uint256) + handler: handleTransfer \ No newline at end of file