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

Min hour ohlc to testnet #1

Merged
merged 2 commits into from
Oct 18, 2023
Merged
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
60 changes: 60 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,37 @@ type PairMinOHLC @entity {
# close price USD
close: BigDecimal!
}
type Pair5MinOHLC @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!
# timestamp rounded to current day by dividing by 3600
periodStartUnix: Int!
token0: Bytes!
token1: Bytes!
open: BigDecimal!
# high price USD
high: BigDecimal!
# low price USD
low: BigDecimal!
# close price USD
close: BigDecimal!
}
type PairDayOHLC @entity {
# concat-token0-token1-mintimestamp address
id: ID!
# timestamp rounded to current day by dividing by 86400
Expand All @@ -680,4 +710,34 @@ type PairHourOHLC @entity {
low: BigDecimal!
# close price USD
close: BigDecimal!
}
type PairMonthOHLC @entity {
# concat-token0-token1-mintimestamp address
id: ID!
# timestamp rounded to current day by dividing by 2592000
periodStartUnix: Int!
token0: Bytes!
token1: Bytes!
open: BigDecimal!
# high price USD
high: BigDecimal!
# low price USD
low: BigDecimal!
# close price USD
close: BigDecimal!
}
type PairYearOHLC @entity {
# concat-token0-token1-mintimestamp address
id: ID!
# timestamp rounded to current day by dividing by 31536000
periodStartUnix: Int!
token0: Bytes!
token1: Bytes!
open: BigDecimal!
# high price USD
high: BigDecimal!
# low price USD
low: BigDecimal!
# close price USD
close: BigDecimal!
}
13 changes: 13 additions & 0 deletions src/mappings/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ 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 {
updatePair5MinData,
updatePairDayData,
updatePairHourData,
updatePairMinData,
updatePairMonthData,
updatePairYearData,
updatePoolDayData,
updatePoolHourData,
updateTickDayData,
Expand Down Expand Up @@ -417,8 +421,17 @@ export function handleSwap(event: SwapEvent): void {
let token1HourData = updateTokenHourData(token1 as Token, event)
updatePairMinData(Address.fromString(pool.token0), Address.fromString(pool.token1), amount0Abs, amount1Abs, event)
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)
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)
updatePairDayData(Address.fromString(pool.token1), Address.fromString(pool.token0), amount1Abs, amount0Abs, event)
updatePairMonthData(Address.fromString(pool.token0), Address.fromString(pool.token1), amount0Abs, amount1Abs, event)
updatePairMonthData(Address.fromString(pool.token1), Address.fromString(pool.token0), amount1Abs, amount0Abs, event)
updatePairYearData(Address.fromString(pool.token0), Address.fromString(pool.token1), amount0Abs, amount1Abs, event)
updatePairYearData(Address.fromString(pool.token1), Address.fromString(pool.token0), amount1Abs, amount0Abs, event)

// update volume metrics
uniswapDayData.volumeETH = uniswapDayData.volumeETH.plus(amountTotalETHTracked)
uniswapDayData.volumeUSD = uniswapDayData.volumeUSD.plus(amountTotalUSDTracked)
Expand Down
188 changes: 182 additions & 6 deletions src/utils/intervalUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import {
TickDayData,
Tick,
PairMinOHLC,
PairHourOHLC
PairHourOHLC,
PairDayOHLC,
PairMonthOHLC,
PairYearOHLC,
Pair5MinOHLC
} from './../types/schema'
import { FACTORY_ADDRESS } from './constants'
import { Address, BigDecimal, ethereum } from '@graphprotocol/graph-ts'
Expand Down Expand Up @@ -264,9 +268,9 @@ export function updatePairMinData(
let minIndex = timestamp / 60 // get unique hour within unix history
let minStartUnix = minIndex * 60 // want the rounded effect
let tokenMinID = token0
.toString()
.toHexString()
.concat('-')
.concat(token1.toString())
.concat(token1.toHexString())
.concat('-')
.concat(minIndex.toString())
let pairMinData = PairMinOHLC.load(tokenMinID)
Expand Down Expand Up @@ -296,6 +300,49 @@ export function updatePairMinData(

return pairMinData as PairMinOHLC
}
export function updatePair5MinData(
token0: Address,
token1: Address,
amount0: BigDecimal,
amount1: BigDecimal,
event: ethereum.Event
): Pair5MinOHLC {
let timestamp = event.block.timestamp.toI32()
let min5Index = timestamp / 300 // get unique hour within unix history
let min5StartUnix = min5Index * 300 // want the rounded effect
let token5MinID = token0
.toHexString()
.concat('-')
.concat(token1.toHexString())
.concat('-')
.concat(min5Index.toString())
let pair5MinData = Pair5MinOHLC.load(token5MinID)
let price = amount0.div(amount1)
if (pair5MinData === null) {
pair5MinData = new Pair5MinOHLC(token5MinID)
pair5MinData.token0 = token0
pair5MinData.token1 = token1
pair5MinData.periodStartUnix = min5StartUnix
pair5MinData.open = price
pair5MinData.high = price
pair5MinData.low = price
pair5MinData.close = price
}

if (price.gt(pair5MinData.high)) {
pair5MinData.high = price
}

if (price.lt(pair5MinData.low)) {
pair5MinData.low = price
}

pair5MinData.close = price

pair5MinData.save()

return pair5MinData as Pair5MinOHLC
}
export function updatePairHourData(
token0: Address,
token1: Address,
Expand All @@ -307,9 +354,9 @@ export function updatePairHourData(
let minIndex = timestamp / 3600 // get unique hour within unix history
let minStartUnix = minIndex * 3600 // want the rounded effect
let tokenHourID = token0
.toString()
.toHexString()
.concat('-')
.concat(token1.toString())
.concat(token1.toHexString())
.concat('-')
.concat(minIndex.toString())
let pairHourData = PairHourOHLC.load(tokenHourID)
Expand Down Expand Up @@ -338,4 +385,133 @@ export function updatePairHourData(
pairHourData.save()

return pairHourData as PairHourOHLC
}
}
export function updatePairDayData(
token0: Address,
token1: Address,
amount0: BigDecimal,
amount1: BigDecimal,
event: ethereum.Event
): PairDayOHLC {
let timestamp = event.block.timestamp.toI32()
let minIndex = timestamp / 86400 // get unique hour within unix history
let minStartUnix = minIndex * 86400 // want the rounded effect
let tokenDayID = token0
.toHexString()
.concat('-')
.concat(token1.toHexString())
.concat('-')
.concat(minIndex.toString())
let pairDayData = PairDayOHLC.load(tokenDayID)
let price = amount0.div(amount1)
if (pairDayData === null) {
pairDayData = new PairDayOHLC(tokenDayID)
pairDayData.token0 = token0
pairDayData.token1 = token1
pairDayData.periodStartUnix = minStartUnix
pairDayData.open = price
pairDayData.high = price
pairDayData.low = price
pairDayData.close = price
}

if (price.gt(pairDayData.high)) {
pairDayData.high = price
}

if (price.lt(pairDayData.low)) {
pairDayData.low = price
}

pairDayData.close = price

pairDayData.save()

return pairDayData as PairDayOHLC
}
export function updatePairMonthData(
token0: Address,
token1: Address,
amount0: BigDecimal,
amount1: BigDecimal,
event: ethereum.Event
): PairMonthOHLC {
let timestamp = event.block.timestamp.toI32()
let minIndex = timestamp / 2592000 // get unique hour within unix history
let minStartUnix = minIndex * 2592000 // want the rounded effect
let tokenMonthID = token0
.toHexString()
.concat('-')
.concat(token1.toHexString())
.concat('-')
.concat(minIndex.toString())
let pairMonthData = PairMonthOHLC.load(tokenMonthID)
let price = amount0.div(amount1)
if (pairMonthData === null) {
pairMonthData = new PairMonthOHLC(tokenMonthID)
pairMonthData.token0 = token0
pairMonthData.token1 = token1
pairMonthData.periodStartUnix = minStartUnix
pairMonthData.open = price
pairMonthData.high = price
pairMonthData.low = price
pairMonthData.close = price
}

if (price.gt(pairMonthData.high)) {
pairMonthData.high = price
}

if (price.lt(pairMonthData.low)) {
pairMonthData.low = price
}

pairMonthData.close = price

pairMonthData.save()

return pairMonthData as PairMonthOHLC
}
export function updatePairYearData(
token0: Address,
token1: Address,
amount0: BigDecimal,
amount1: BigDecimal,
event: ethereum.Event
): PairYearOHLC {
let timestamp = event.block.timestamp.toI32()
let minIndex = timestamp / 31536000 // get unique hour within unix history
let minStartUnix = minIndex * 31536000 // want the rounded effect
let tokenYearID = token0
.toHexString()
.concat('-')
.concat(token1.toHexString())
.concat('-')
.concat(minIndex.toString())
let pairYearData = PairYearOHLC.load(tokenYearID)
let price = amount0.div(amount1)
if (pairYearData === null) {
pairYearData = new PairYearOHLC(tokenYearID)
pairYearData.token0 = token0
pairYearData.token1 = token1
pairYearData.periodStartUnix = minStartUnix
pairYearData.open = price
pairYearData.high = price
pairYearData.low = price
pairYearData.close = price
}

if (price.gt(pairYearData.high)) {
pairYearData.high = price
}

if (price.lt(pairYearData.low)) {
pairYearData.low = price
}

pairYearData.close = price

pairYearData.save()

return pairYearData as PairYearOHLC
}
Loading