Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null checks and Optimizations #4

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,15 @@
"scripts": {
"codegen": "graph codegen --output-dir src/types/",
"build": "graph build",
"deploy-hosted": "graph deploy --product hosted-service gundamdweeb/spookyswap",
"create-local": "graph create eerieeight/spooky-swap-exchange --node http://127.0.0.1:8020",
"deploy-local": "graph deploy eerieeight/spookyswap --debug --ipfs http://localhost:5001 --node http://127.0.0.1:8020",
"deploy": "graph deploy eerieeight/spookyswap --ipfs https://api.thegraph.com/ipfs/ --node https://api.thegraph.com/deploy/ --debug",
"deploy-staging": "graph deploy eerieeight/spooky-swap-exchange /SpookySwap --ipfs https://api.staging.thegraph.com/ipfs/ --node https://api.staging.thegraph.com/deploy/",
"watch-local": "graph deploy graphprotocol/spookyswap --watch --debug --node http://127.0.0.1:8020/ --ipfs http://localhost:5001"
},
"devDependencies": {
"@graphprotocol/graph-cli": "^0.16.0",
"@graphprotocol/graph-ts": "^0.16.0",
"@typescript-eslint/eslint-plugin": "^2.0.0",
"@typescript-eslint/parser": "^2.0.0",
"eslint": "^6.2.2",
"eslint-config-prettier": "^6.1.0",
"prettier": "^1.18.2",
"typescript": "^3.5.2"
"@graphprotocol/graph-cli": "^0.67.2",
"@graphprotocol/graph-ts": "^0.32.0"
}
}
}
11 changes: 8 additions & 3 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ type Token @entity {
pairQuote: [Pair!]! @derivedFrom(field: "token1")
}

type PairTokenLookup @entity(immutable: true) {
id: ID!
pair: Pair!
}

type Pair @entity {
# pair address
id: ID!
Expand Down Expand Up @@ -131,9 +136,9 @@ type Transaction @entity {
timestamp: BigInt!
# This is not the reverse of Mint.transaction; it is only used to
# track incomplete mints (similar for burns and swaps)
mints: [Mint]!
burns: [Burn]!
swaps: [Swap]!
mints: [Mint!]
burns: [Burn!]
swaps: [Swap!]
}

type Mint @entity {
Expand Down
147 changes: 117 additions & 30 deletions src/mappings/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ import {
} from './helpers'

function isCompleteMint(mintId: string): boolean {
return MintEvent.load(mintId).sender !== null // sufficient checks
let mintEvent = MintEvent.load(mintId);
if(!mintEvent){
return false
}
if(!mintEvent.sender){
return false
}
return true // sufficient checks
}

export function handleTransfer(event: Transfer): void {
Expand All @@ -36,6 +43,9 @@ export function handleTransfer(event: Transfer): void {
}

let factory = UniswapFactory.load(FACTORY_ADDRESS)
if(!factory){
return
}
let transactionHash = event.transaction.hash.toHexString()

// user stats
Expand All @@ -46,14 +56,17 @@ export function handleTransfer(event: Transfer): void {

// get pair and load contract
let pair = Pair.load(event.address.toHexString())
if(!pair){
return
}
let pairContract = PairContract.bind(event.address)

// liquidity token amount being transfered
let value = convertTokenToDecimal(event.params.value, BI_18)

// get or create transaction
let transaction = Transaction.load(transactionHash)
if (transaction === null) {
if (!transaction) {
transaction = new Transaction(transactionHash)
transaction.blockNumber = event.block.number
transaction.timestamp = event.block.timestamp
Expand All @@ -63,14 +76,14 @@ export function handleTransfer(event: Transfer): void {
}

// mints
let mints = transaction.mints
let mints = transaction.mints!
if (from.toHexString() == ADDRESS_ZERO) {
// update total supply
pair.totalSupply = pair.totalSupply.plus(value)
pair.save()

// create new mint if no mints so far or if last one is done already
if (mints.length === 0 || isCompleteMint(mints[mints.length - 1])) {
if (mints.length == 0 || isCompleteMint(mints[mints.length - 1])) {
let mint = new MintEvent(
event.transaction.hash
.toHexString()
Expand All @@ -96,7 +109,7 @@ export function handleTransfer(event: Transfer): void {

// case where direct send first on ETH withdrawls
if (event.params.to.toHexString() == pair.id) {
let burns = transaction.burns
let burns = transaction.burns!
let burn = new BurnEvent(
event.transaction.hash
.toHexString()
Expand Down Expand Up @@ -126,10 +139,10 @@ export function handleTransfer(event: Transfer): void {
pair.save()

// this is a new instance of a logical burn
let burns = transaction.burns
let burns = transaction.burns!
let burn: BurnEvent
if (burns.length > 0) {
let currentBurn = BurnEvent.load(burns[burns.length - 1])
let currentBurn = BurnEvent.load(burns[burns.length - 1])!
if (currentBurn.needsComplete) {
burn = currentBurn as BurnEvent
} else {
Expand Down Expand Up @@ -162,8 +175,8 @@ export function handleTransfer(event: Transfer): void {
}

// if this logical burn included a fee mint, account for this
if (mints.length !== 0 && !isCompleteMint(mints[mints.length - 1])) {
let mint = MintEvent.load(mints[mints.length - 1])
if (mints.length != 0 && !isCompleteMint(mints[mints.length - 1])) {
let mint = MintEvent.load(mints[mints.length - 1])!
burn.feeTo = mint.to
burn.feeLiquidity = mint.liquidity
// remove the logical mint
Expand Down Expand Up @@ -211,11 +224,18 @@ export function handleTransfer(event: Transfer): void {
}

export function handleSync(event: Sync): void {
let pair = Pair.load(event.address.toHex())
let pair = Pair.load(event.address.toHexString())
if(!pair){
return
}
let token0 = Token.load(pair.token0)
let token1 = Token.load(pair.token1)
let uniswap = UniswapFactory.load(FACTORY_ADDRESS)

if(!token0 || !token1 || !uniswap){
return
}

// reset factory liquidity by subtracting onluy tarcked liquidity
uniswap.totalLiquidityETH = uniswap.totalLiquidityETH.minus(pair.trackedReserveETH as BigDecimal)

Expand All @@ -235,9 +255,12 @@ export function handleSync(event: Sync): void {

// update ETH price now that reserves could have changed
let bundle = Bundle.load('1')
if(!bundle){
return
}
bundle.ethPrice = getEthPriceInUSD()
bundle.save()

//TODO CHANGES KENT
token0.derivedETH = findEthPerToken(token0 as Token)
token1.derivedETH = findEthPerToken(token1 as Token)
// token0.save()
Expand All @@ -246,9 +269,13 @@ export function handleSync(event: Sync): void {
// get tracked liquidity - will be 0 if neither is in whitelist
let trackedLiquidityETH: BigDecimal
if (bundle.ethPrice.notEqual(ZERO_BD)) {
trackedLiquidityETH = getTrackedLiquidityUSD(pair.reserve0, token0 as Token, pair.reserve1, token1 as Token, bundle as Bundle).div(
bundle.ethPrice
)
trackedLiquidityETH = getTrackedLiquidityUSD(
pair.reserve0,
token0 as Token,
pair.reserve1,
token1 as Token,
bundle as Bundle
).div(bundle.ethPrice)
} else {
trackedLiquidityETH = ZERO_BD
}
Expand Down Expand Up @@ -277,14 +304,39 @@ export function handleSync(event: Sync): void {

export function handleMint(event: Mint): void {
let transaction = Transaction.load(event.transaction.hash.toHexString())
if (!transaction) {
transaction = new Transaction(event.transaction.hash.toHexString())
transaction.blockNumber = event.block.number
transaction.timestamp = event.block.timestamp
transaction.mints = []
transaction.burns = []
transaction.swaps = []
}

let mints = transaction.mints
if(!mints){
return
}
let mint = MintEvent.load(mints[mints.length - 1])
if(!mint){
return
}

let pair = Pair.load(event.address.toHex())
if(!pair){
return
}
let uniswap = UniswapFactory.load(FACTORY_ADDRESS)
if(!uniswap){
return
}

//update token info
let token0 = Token.load(pair.token0)
let token1 = Token.load(pair.token1)
if(!token0 || !token1){
return
}

// update exchange info (except balances, sync will cover that)
let token0Amount = convertTokenToDecimal(event.params.amount0, token0.decimals)
Expand All @@ -296,9 +348,12 @@ export function handleMint(event: Mint): void {

// get new amounts of USD and ETH for tracking
let bundle = Bundle.load('1')
let amountTotalUSD = token1.derivedETH
.times(token1Amount)
.plus(token0.derivedETH.times(token0Amount))
if(!bundle){
return
}
let amountTotalUSD = token1
.derivedETH!.times(token1Amount)
.plus(token0.derivedETH!.times(token0Amount))
.times(bundle.ethPrice)

// update txn counts
Expand Down Expand Up @@ -339,19 +394,31 @@ export function handleBurn(event: Burn): void {
let transaction = Transaction.load(event.transaction.hash.toHexString())

// safety check
if (transaction === null) {
if (!transaction) {
return
}

let burns = transaction.burns
let burns = transaction.burns!
let burn = BurnEvent.load(burns[burns.length - 1])
if(!burn){
return
}

let pair = Pair.load(event.address.toHex())
if(!pair){
return
}
let uniswap = UniswapFactory.load(FACTORY_ADDRESS)
if(!uniswap){
return
}

//update token info
let token0 = Token.load(pair.token0)
let token1 = Token.load(pair.token1)
if(!token0 || !token1){
return
}
let token0Amount = convertTokenToDecimal(event.params.amount0, token0.decimals)
let token1Amount = convertTokenToDecimal(event.params.amount1, token1.decimals)

Expand All @@ -361,9 +428,12 @@ export function handleBurn(event: Burn): void {

// get new amounts of USD and ETH for tracking
let bundle = Bundle.load('1')
let amountTotalUSD = token1.derivedETH
.times(token1Amount)
.plus(token0.derivedETH.times(token0Amount))
if(!bundle){
return
}
let amountTotalUSD = token1
.derivedETH!.times(token1Amount)
.plus(token0.derivedETH!.times(token0Amount))
.times(bundle.ethPrice)

// update txn counts
Expand Down Expand Up @@ -404,8 +474,14 @@ export function handleBurn(event: Burn): void {

export function handleSwap(event: Swap): void {
let pair = Pair.load(event.address.toHexString())
if(!pair){
return
}
let token0 = Token.load(pair.token0)
let token1 = Token.load(pair.token1)
if(!token0 || !token1){
return
}
let amount0In = convertTokenToDecimal(event.params.amount0In, token0.decimals)
let amount1In = convertTokenToDecimal(event.params.amount1In, token1.decimals)
let amount0Out = convertTokenToDecimal(event.params.amount0Out, token0.decimals)
Expand All @@ -417,16 +493,24 @@ export function handleSwap(event: Swap): void {

// ETH/USD prices
let bundle = Bundle.load('1')

if(!bundle){
return
}
// get total amounts of derived USD and ETH for tracking
let derivedAmountETH = token1.derivedETH
.times(amount1Total)
.plus(token0.derivedETH.times(amount0Total))
let derivedAmountETH = token1
.derivedETH!.times(amount1Total)
.plus(token0.derivedETH!.times(amount0Total))
.div(BigDecimal.fromString('2'))
let derivedAmountUSD = derivedAmountETH.times(bundle.ethPrice)

// only accounts for volume through white listed tokens
let trackedAmountUSD = getTrackedVolumeUSD(amount0Total, token0 as Token, amount1Total, token1 as Token, bundle as Bundle)
let trackedAmountUSD = getTrackedVolumeUSD(
amount0Total,
token0 as Token,
amount1Total,
token1 as Token,
bundle as Bundle
)

let trackedAmountETH: BigDecimal
if (bundle.ethPrice.equals(ZERO_BD)) {
Expand Down Expand Up @@ -459,6 +543,9 @@ export function handleSwap(event: Swap): void {

// update global values, only used tracked amounts for volume
let uniswap = UniswapFactory.load(FACTORY_ADDRESS)
if(!uniswap){
return
}
uniswap.totalVolumeUSD = uniswap.totalVolumeUSD.plus(trackedAmountUSD)
uniswap.totalVolumeETH = uniswap.totalVolumeETH.plus(trackedAmountETH)
uniswap.untrackedVolumeUSD = uniswap.untrackedVolumeUSD.plus(derivedAmountUSD)
Expand All @@ -471,15 +558,15 @@ export function handleSwap(event: Swap): void {
uniswap.save()

let transaction = Transaction.load(event.transaction.hash.toHexString())
if (transaction === null) {
if (!transaction) {
transaction = new Transaction(event.transaction.hash.toHexString())
transaction.blockNumber = event.block.number
transaction.timestamp = event.block.timestamp
transaction.mints = []
transaction.swaps = []
transaction.burns = []
}
let swaps = transaction.swaps
let swaps = transaction.swaps!
let swap = new SwapEvent(
event.transaction.hash
.toHexString()
Expand All @@ -501,7 +588,7 @@ export function handleSwap(event: Swap): void {
swap.from = event.transaction.from
swap.logIndex = event.logIndex
// use the tracked amount if we have it
swap.amountUSD = trackedAmountUSD === ZERO_BD ? derivedAmountUSD : trackedAmountUSD
swap.amountUSD = trackedAmountUSD == ZERO_BD ? derivedAmountUSD : trackedAmountUSD
swap.save()

// update the transaction
Expand Down
Loading