Skip to content

Commit

Permalink
feat: avoiding position creation for 0 address
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Mar 11, 2021
1 parent 49843cc commit bc96a34
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 29 deletions.
58 changes: 44 additions & 14 deletions src/mappings/core.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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'
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 12 additions & 15 deletions src/mappings/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('-')
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bc96a34

Please sign in to comment.