Skip to content

Commit

Permalink
pricing
Browse files Browse the repository at this point in the history
  • Loading branch information
mzywang committed Dec 10, 2024
1 parent 802e1f8 commit 0bdb348
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
2 changes: 2 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type Token @entity {
decimals: BigInt!

totalLiquidity: BigDecimal!
primaryUsdPrice: BigDecimal!
secondaryUsdPrice: BigDecimal!

pairsAsToken0: [Pair!]! @derivedFrom(field: "token0")
pairsAsToken1: [Pair!]! @derivedFrom(field: "token1")
Expand Down
9 changes: 8 additions & 1 deletion src/mappings/factory.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* eslint-disable prefer-const */
import { log } from '@graphprotocol/graph-ts'
import { BigDecimal, log } from '@graphprotocol/graph-ts'

import { PairCreated } from '../types/Factory/Factory'
import { Pair, Token, UniswapFactory } from '../types/schema'
import { Pair as PairTemplate } from '../types/templates'
import { FACTORY_ADDRESS, fetchTokenDecimals, ONE_BI, ZERO_BD } from './helpers'
import { isStablecoin } from './pricing'

export function handleNewPair(event: PairCreated): void {
if (event.params.param3.equals(ONE_BI)) {
Expand All @@ -25,6 +26,9 @@ export function handleNewPair(event: PairCreated): void {
const token0 = new Token(event.params.token0.toHexString())
token0.decimals = decimals
token0.totalLiquidity = ZERO_BD
if (isStablecoin(event.params.token0.toHexString())) {
token0.primaryUsdPrice = BigDecimal.fromString('1.0')
}
token0.save()
}

Expand All @@ -41,6 +45,9 @@ export function handleNewPair(event: PairCreated): void {
const token1 = new Token(event.params.token1.toHexString())
token1.decimals = decimals
token1.totalLiquidity = ZERO_BD
if (isStablecoin(event.params.token1.toHexString())) {
token1.primaryUsdPrice = BigDecimal.fromString('1.0')
}
token1.save()
}

Expand Down
16 changes: 16 additions & 0 deletions src/mappings/pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 } from '../types/templates/Pair/Pair'
import { convertTokenToDecimal } from './helpers'
import { updatePrimaryUsdPrices } from './pricing'

export function handleMint(event: Mint): void {
const block = event.block
Expand Down Expand Up @@ -36,6 +37,11 @@ export function handleMint(event: Mint): void {
mint.amount1 = token1Amount as BigDecimal
mint.save()

pair.reserve0 = pair.reserve0.plus(token0Amount)
pair.reserve1 = pair.reserve1.plus(token1Amount)
pair.save()

updatePrimaryUsdPrices(pair, token0, token1)
token0.totalLiquidity = token0.totalLiquidity.plus(token0Amount)
token1.totalLiquidity = token1.totalLiquidity.plus(token1Amount)
token0.save()
Expand Down Expand Up @@ -66,6 +72,11 @@ export function handleBurn(event: Burn): void {
burn.to = event.params.to
burn.save()

pair.reserve0 = pair.reserve0.minus(token0Amount)
pair.reserve1 = pair.reserve1.minus(token1Amount)
pair.save()

updatePrimaryUsdPrices(pair, token0, token1)
token0.totalLiquidity = token0.totalLiquidity.minus(token0Amount)
token1.totalLiquidity = token1.totalLiquidity.minus(token1Amount)
token0.save()
Expand Down Expand Up @@ -100,6 +111,11 @@ export function handleSwap(event: Swap): void {
swap.to = event.params.to
swap.save()

pair.reserve0 = pair.reserve0.minus(token0AmountOut).plus(token0AmountIn)
pair.reserve1 = pair.reserve1.minus(token1AmountOut).plus(token1AmountIn)
pair.save()

updatePrimaryUsdPrices(pair, token0, token1)
token0.totalLiquidity = token0.totalLiquidity.minus(token0AmountOut).plus(token0AmountIn)
token1.totalLiquidity = token1.totalLiquidity.minus(token1AmountOut).plus(token1AmountIn)
token0.save()
Expand Down
33 changes: 33 additions & 0 deletions src/mappings/pricing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { BigDecimal } from '@graphprotocol/graph-ts'

import { Pair, Token } from '../types/schema'

const STABLECOINS: string[] = [
'0x6b175474e89094c44da98b954eedeac495271d0f', // DAI
'0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', // USDC
'0xdac17f958d2ee523a2206206994597c13d831ec7', // USDT
]

export function isStablecoin(token: string): boolean {
return STABLECOINS.includes(token)
}

export function getPrimaryUsdPrice(pair: Pair, token: Token): BigDecimal {
if (isStablecoin(token)) {
return BigDecimal.fromString('1.0')
} else if (isStablecoin(pair.token0)) {
return pair.reserve0.div(pair.reserve1)
} else if (isStablecoin(pair.token1)) {
return pair.reserve1.div(pair.reserve0)
} else {
return BigDecimal.fromString('0.0')
}
}

export function updatePrimaryUsdPrices(pair: Pair, token0: Token, token1: Token): void {
if (isStablecoin(pair.token0)) {
token1.primaryUsdPrice = pair.reserve0.div(pair.reserve1)
} else if (isStablecoin(pair.token1)) {
token0.primaryUsdPrice = pair.reserve1.div(pair.reserve0)
}
}

0 comments on commit 0bdb348

Please sign in to comment.