diff --git a/schema.graphql b/schema.graphql index d49b166..671e0c3 100644 --- a/schema.graphql +++ b/schema.graphql @@ -681,6 +681,36 @@ type Pair5MinOHLC @entity { # close price USD close: BigDecimal! } +type Pair15MinOHLC @entity { + # concat-token0-token1-mintimestamp address + id: ID! + # timestamp rounded to current day by dividing by 300 + periodStartUnix: Int! + token0: Bytes! + token1: Bytes! + open: BigDecimal! + # high price USD + high: BigDecimal! + # low price USD + low: BigDecimal! + # close price USD + close: BigDecimal! +} +type Pair30MinOHLC @entity { + # concat-token0-token1-mintimestamp address + id: ID! + # timestamp rounded to current day by dividing by 300 + periodStartUnix: Int! + token0: Bytes! + token1: Bytes! + open: BigDecimal! + # high price USD + high: BigDecimal! + # low price USD + low: BigDecimal! + # close price USD + close: BigDecimal! +} type PairHourOHLC @entity { # concat-token0-token1-mintimestamp address id: ID! diff --git a/src/mappings/core.ts b/src/mappings/core.ts index c61aace..8b563f8 100644 --- a/src/mappings/core.ts +++ b/src/mappings/core.ts @@ -13,6 +13,8 @@ import { convertTokenToDecimal, loadTransaction, safeDiv } from '../utils' import { FACTORY_ADDRESS, ONE_BI, ZERO_BD, ZERO_BI } from '../utils/constants' import { findEthPerToken, getEthPriceInUSD, getTrackedAmountUSD, sqrtPriceX96ToTokenPrices } from '../utils/pricing' import { + updatePair15MinData, + updatePair30MinData, updatePair5MinData, updatePairDayData, updatePairHourData, @@ -424,6 +426,11 @@ export function handleSwap(event: SwapEvent): void { updatePairMinData(Address.fromString(pool.token1), Address.fromString(pool.token0), amount1Abs, amount0Abs, event) updatePair5MinData(Address.fromString(pool.token0), Address.fromString(pool.token1), amount0Abs, amount1Abs, event) updatePair5MinData(Address.fromString(pool.token1), Address.fromString(pool.token0), amount1Abs, amount0Abs, event) + updatePair15MinData(Address.fromString(pool.token0), Address.fromString(pool.token1), amount0Abs, amount1Abs, event) + updatePair15MinData(Address.fromString(pool.token1), Address.fromString(pool.token0), amount1Abs, amount0Abs, event) + updatePair30MinData(Address.fromString(pool.token0), Address.fromString(pool.token1), amount0Abs, amount1Abs, event) + updatePair30MinData(Address.fromString(pool.token1), Address.fromString(pool.token0), amount1Abs, amount0Abs, event) + updatePairHourData(Address.fromString(pool.token0), Address.fromString(pool.token1), amount0Abs, amount1Abs, event) updatePairHourData(Address.fromString(pool.token1), Address.fromString(pool.token0), amount1Abs, amount0Abs, event) updatePairDayData(Address.fromString(pool.token0), Address.fromString(pool.token1), amount0Abs, amount1Abs, event) diff --git a/src/utils/intervalUpdates.ts b/src/utils/intervalUpdates.ts index de37b81..cbb3cb0 100644 --- a/src/utils/intervalUpdates.ts +++ b/src/utils/intervalUpdates.ts @@ -17,7 +17,9 @@ import { PairDayOHLC, PairMonthOHLC, PairYearOHLC, - Pair5MinOHLC + Pair5MinOHLC, + Pair15MinOHLC, + Pair30MinOHLC } from './../types/schema' import { FACTORY_ADDRESS } from './constants' import { Address, BigDecimal, ethereum } from '@graphprotocol/graph-ts' @@ -343,6 +345,92 @@ export function updatePair5MinData( return pair5MinData as Pair5MinOHLC } +export function updatePair15MinData( + token0: Address, + token1: Address, + amount0: BigDecimal, + amount1: BigDecimal, + event: ethereum.Event +): Pair15MinOHLC { + let timestamp = event.block.timestamp.toI32() + let min15Index = timestamp / 900 // get unique hour within unix history + let min15StartUnix = min15Index * 900 // want the rounded effect + let token15MinID = token0 + .toHexString() + .concat('-') + .concat(token1.toHexString()) + .concat('-') + .concat(min15Index.toString()) + let pair15MinData = Pair15MinOHLC.load(token15MinID) + let price = amount0.div(amount1) + if (pair15MinData === null) { + pair15MinData = new Pair15MinOHLC(token15MinID) + pair15MinData.token0 = token0 + pair15MinData.token1 = token1 + pair15MinData.periodStartUnix = min15StartUnix + pair15MinData.open = price + pair15MinData.high = price + pair15MinData.low = price + pair15MinData.close = price + } + + if (price.gt(pair15MinData.high)) { + pair15MinData.high = price + } + + if (price.lt(pair15MinData.low)) { + pair15MinData.low = price + } + + pair15MinData.close = price + + pair15MinData.save() + + return pair15MinData as Pair15MinOHLC +} +export function updatePair30MinData( + token0: Address, + token1: Address, + amount0: BigDecimal, + amount1: BigDecimal, + event: ethereum.Event +): Pair30MinOHLC { + let timestamp = event.block.timestamp.toI32() + let min30Index = timestamp / 1800 // get unique hour within unix history + let min30StartUnix = min30Index * 1800 // want the rounded effect + let token30MinID = token0 + .toHexString() + .concat('-') + .concat(token1.toHexString()) + .concat('-') + .concat(min30Index.toString()) + let pair30MinData = Pair30MinOHLC.load(token30MinID) + let price = amount0.div(amount1) + if (pair30MinData === null) { + pair30MinData = new Pair30MinOHLC(token30MinID) + pair30MinData.token0 = token0 + pair30MinData.token1 = token1 + pair30MinData.periodStartUnix = min30StartUnix + pair30MinData.open = price + pair30MinData.high = price + pair30MinData.low = price + pair30MinData.close = price + } + + if (price.gt(pair30MinData.high)) { + pair30MinData.high = price + } + + if (price.lt(pair30MinData.low)) { + pair30MinData.low = price + } + + pair30MinData.close = price + + pair30MinData.save() + + return pair30MinData as Pair30MinOHLC +} export function updatePairHourData( token0: Address, token1: Address,