From 0bdb34851ffeccc53f1848723a3b448af6355868 Mon Sep 17 00:00:00 2001 From: Michael Wang <44713145+mzywang@users.noreply.github.com> Date: Mon, 9 Dec 2024 17:08:47 -0500 Subject: [PATCH] pricing --- schema.graphql | 2 ++ src/mappings/factory.ts | 9 ++++++++- src/mappings/pair.ts | 16 ++++++++++++++++ src/mappings/pricing.ts | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/mappings/pricing.ts diff --git a/schema.graphql b/schema.graphql index ae86b774..ace70367 100644 --- a/schema.graphql +++ b/schema.graphql @@ -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") diff --git a/src/mappings/factory.ts b/src/mappings/factory.ts index 2d05129a..8d21f823 100644 --- a/src/mappings/factory.ts +++ b/src/mappings/factory.ts @@ -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)) { @@ -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() } @@ -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() } diff --git a/src/mappings/pair.ts b/src/mappings/pair.ts index feba8892..7cd4bebd 100644 --- a/src/mappings/pair.ts +++ b/src/mappings/pair.ts @@ -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 @@ -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() @@ -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() @@ -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() diff --git a/src/mappings/pricing.ts b/src/mappings/pricing.ts new file mode 100644 index 00000000..e94d3343 --- /dev/null +++ b/src/mappings/pricing.ts @@ -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) + } +}