From e8092802bd6c4ef480fad17de6580f76c4f3adfc 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 --- networks.json | 74 +++++++++++++++++++++++++++++++++++++++++ schema.graphql | 23 ++++++++----- src/mappings/factory.ts | 4 --- src/mappings/pair.ts | 53 ++++++++++++++++------------- subgraph.yaml | 12 +++---- 5 files changed, 125 insertions(+), 41 deletions(-) create mode 100644 networks.json diff --git a/networks.json b/networks.json new file mode 100644 index 00000000..f0920691 --- /dev/null +++ b/networks.json @@ -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 + } + } +} \ No newline at end of file diff --git a/schema.graphql b/schema.graphql index ae86b774..234bb4d9 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,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! @@ -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! @@ -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! @@ -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! +} \ 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..e9ca5597 100644 --- a/src/mappings/pair.ts +++ b/src/mappings/pair.ts @@ -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 @@ -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() -} diff --git a/subgraph.yaml b/subgraph.yaml index 83365a97..1a855ad2 100644 --- a/subgraph.yaml +++ b/subgraph.yaml @@ -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 @@ -31,7 +31,7 @@ dataSources: templates: - kind: ethereum/contract name: Pair - network: mainnet + network: arbitrum-one source: abi: Pair mapping: @@ -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