From bc96a34bbe8c0c56ae87a530b37480ac77c567f4 Mon Sep 17 00:00:00 2001 From: benesjan Date: Thu, 11 Mar 2021 13:38:36 -0600 Subject: [PATCH] feat: avoiding position creation for 0 address --- src/mappings/core.ts | 58 +++++++++++++++++++++++++++++++---------- src/mappings/helpers.ts | 27 +++++++++---------- subgraph.yaml | 2 ++ 3 files changed, 58 insertions(+), 29 deletions(-) diff --git a/src/mappings/core.ts b/src/mappings/core.ts index 47e66538..5b4b356d 100644 --- a/src/mappings/core.ts +++ b/src/mappings/core.ts @@ -1,5 +1,5 @@ /* eslint-disable prefer-const */ -import { BigInt, BigDecimal, store, Address } from '@graphprotocol/graph-ts' +import { BigInt, BigDecimal, store, Address, log } from '@graphprotocol/graph-ts' import { Pair, Token, @@ -8,7 +8,7 @@ import { Mint as MintEvent, Burn as BurnEvent, Swap as SwapEvent, - Bundle + Bundle, LiquidityPosition } from '../types/schema' import { Pair as PairContract, Mint, Burn, Swap, Transfer, Sync } from '../types/templates/Pair/Pair' import { updatePairDayData, updateTokenDayData, updateUniswapDayData, updatePairHourData } from './dayUpdates' @@ -197,16 +197,24 @@ export function handleTransfer(event: Transfer): void { if (from.toHexString() != ADDRESS_ZERO && from.toHexString() != pair.id) { let fromUserLiquidityPosition = createLiquidityPosition(event.address, from) - fromUserLiquidityPosition.liquidityTokenBalance = convertTokenToDecimal(pairContract.balanceOf(from), BI_18) - fromUserLiquidityPosition.save() - createLiquiditySnapshot(fromUserLiquidityPosition, event) + if (fromUserLiquidityPosition !== null) { + fromUserLiquidityPosition.liquidityTokenBalance = convertTokenToDecimal(pairContract.balanceOf(from), BI_18) + fromUserLiquidityPosition.save() + createLiquiditySnapshot(fromUserLiquidityPosition as LiquidityPosition, event) + } } if (event.params.to.toHexString() != ADDRESS_ZERO && to.toHexString() != pair.id) { - let toUserLiquidityPosition = createLiquidityPosition(event.address, to) - toUserLiquidityPosition.liquidityTokenBalance = convertTokenToDecimal(pairContract.balanceOf(to), BI_18) - toUserLiquidityPosition.save() - createLiquiditySnapshot(toUserLiquidityPosition, event) + if (to.toHexString() !== ADDRESS_ZERO) { + let toUserLiquidityPosition = createLiquidityPosition(event.address, to) + if (toUserLiquidityPosition !== null) { + toUserLiquidityPosition.liquidityTokenBalance = convertTokenToDecimal(pairContract.balanceOf(to), BI_18) + toUserLiquidityPosition.save() + createLiquiditySnapshot(toUserLiquidityPosition as LiquidityPosition, event) + } + } else { + log.warning('Detected 0 address in handleTransfer', []) + } } transaction.save() @@ -320,9 +328,17 @@ export function handleMint(event: Mint): void { mint.amountUSD = amountTotalUSD as BigDecimal mint.save() - // update the LP position - let liquidityPosition = createLiquidityPosition(event.address, mint.to as Address) - createLiquiditySnapshot(liquidityPosition, event) + let to = mint.to as Address + if (to.toHexString() !== ADDRESS_ZERO) { + let liquidityPosition = createLiquidityPosition(event.address, to) + if (liquidityPosition !== null) { + createLiquiditySnapshot(liquidityPosition as LiquidityPosition, event) + } else { + log.warning('null position in handleMint', []) + } + } else { + log.warning('Detected 0 address in handleMint', []) + } // update day entities updatePairDayData(event) @@ -383,8 +399,22 @@ export function handleBurn(event: Burn): void { burn.save() // update the LP position - let liquidityPosition = createLiquidityPosition(event.address, burn.sender as Address) - createLiquiditySnapshot(liquidityPosition, event) + let to = burn.sender as Address + if (burn.sender.toHexString() != '0x') { + let liquidityPosition = createLiquidityPosition(event.address, to) + if (liquidityPosition !== null) { + createLiquiditySnapshot(liquidityPosition as LiquidityPosition, event) + } else { + log.warning('null position in handleBurn: ' + .concat(burn.sender.toHexString()).concat(' ') + .concat(ADDRESS_ZERO) + .concat(', tx_hash: ') + .concat(event.transaction.hash.toHexString()), []) + } + } else { + log.warning('Detected 0x address in handleBurn'.concat(', tx_hash: ') + .concat(event.transaction.hash.toHexString()), []) + } // update day entities updatePairDayData(event) diff --git a/src/mappings/helpers.ts b/src/mappings/helpers.ts index 980bd977..76ef1438 100644 --- a/src/mappings/helpers.ts +++ b/src/mappings/helpers.ts @@ -139,7 +139,18 @@ export function fetchTokenDecimals(tokenAddress: Address): BigInt { return BigInt.fromI32(decimalValue as i32) } -export function createLiquidityPosition(exchange: Address, user: Address): LiquidityPosition { +export function createLiquidityPosition(exchange: Address, user: Address): LiquidityPosition | null { + if (User.load(user.toHexString()) === null) { + log.error( + 'Null user during snapshot creation. User address: ' + .concat(user.toHexString()) + .concat(' exchange address: ') + .concat(exchange.toHexString()) + .concat(' --> SKIPPING POSITION CREATION'), + [] + ) + return null + } let id = exchange .toHexString() .concat('-') @@ -169,20 +180,6 @@ export function createUser(address: Address): void { } export function createLiquiditySnapshot(position: LiquidityPosition, event: EthereumEvent): void { - if (User.load(position.user) === null) { - log.error( - 'Null user during snapshot creation. User id: ' - .concat(position.user) - .concat(' position id: ') - .concat(position.id) - .concat(' tx hash: ') - .concat(event.transaction.hash.toHexString()) - .concat(' --> SKIPPING SNAPSHOT CREATION'), - [] - ) - return - } - let bundle = Bundle.load('1') let pair = Pair.load(position.pair) let token0 = Token.load(pair.token0) diff --git a/subgraph.yaml b/subgraph.yaml index a4ba4508..bcabb5db 100644 --- a/subgraph.yaml +++ b/subgraph.yaml @@ -9,8 +9,10 @@ dataSources: network: mainnet source: address: '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f' +# address: '0x71CD6666064C3A1354a3B4dca5fA1E2D3ee7D303' abi: Factory startBlock: 10000834 +# startBlock: 10634502 mapping: kind: ethereum/events apiVersion: 0.0.3