diff --git a/schema.graphql b/schema.graphql index 2d2f548..e5b3471 100644 --- a/schema.graphql +++ b/schema.graphql @@ -114,9 +114,12 @@ type PoolVolume @entity { } type PoolSnapshot @entity { - # `{poolKey}-{timestamp}` + # `{poolKey}-{intervalType}-{timestamp}` id: ID! poolKey: String! + # interval type: 1h + intervalType: String! + # normalized candle timestamp of the block where this trade occurred (second) timestamp: BigInt! price: BigInt! liquidityA: BigInt! diff --git a/src/helpers.ts b/src/helpers.ts index 2c29135..b7e7c43 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -86,7 +86,7 @@ export function buildChartLogId( .concat(timestamp.toString()) } -export function buildPoolVolumeId( +export function buildPoolVolumeAndSnapshotId( poolKey: Bytes, intervalType: string, timestamp: i64, @@ -99,10 +99,6 @@ export function buildPoolVolumeId( .concat(timestamp.toString()) } -export function buildPoolSnapshotId(poolKey: Bytes, timestamp: i64): string { - return poolKey.toHexString().concat('-').concat(timestamp.toString()) -} - export function buildMarketCode(base: Token, quote: Token): string { return base.id.concat('/').concat(quote.id) } diff --git a/src/pool.ts b/src/pool.ts index 2114a64..36178d6 100644 --- a/src/pool.ts +++ b/src/pool.ts @@ -15,8 +15,7 @@ import { } from './addresses' import { baseToQuote, - buildPoolSnapshotId, - buildPoolVolumeId, + buildPoolVolumeAndSnapshotId, CHART_LOG_INTERVALS, } from './helpers' @@ -47,7 +46,11 @@ export function handleClear(event: Clear): void { (event.block.timestamp.toI64() as number) / intervalInNumber, ) * intervalInNumber) as i64 - const poolVolumeId = buildPoolVolumeId(poolKey, intervalType, timestampForAcc) + const poolVolumeId = buildPoolVolumeAndSnapshotId( + poolKey, + intervalType, + timestampForAcc, + ) let poolVolume = PoolVolume.load(poolVolumeId) if (poolVolume === null) { poolVolume = new PoolVolume(poolVolumeId) @@ -94,15 +97,25 @@ export function handleUpdatePrice(event: UpdatePrice): void { BigInt.fromString(poolKey.toHexString()), ) - const poolSnapshotId = buildPoolSnapshotId( + const intervalEntry = CHART_LOG_INTERVALS.getEntry('1h')! // only use 1h interval for now + const intervalType = intervalEntry.key + const intervalInNumber = intervalEntry.value + const timestampForAcc = (Math.floor( + (event.block.timestamp.toI64() as number) / intervalInNumber, + ) * intervalInNumber) as i64 + + const poolSnapshotId = buildPoolVolumeAndSnapshotId( poolKey, - event.block.timestamp.toI64(), + intervalType, + timestampForAcc, ) + let poolSnapshot = PoolSnapshot.load(poolSnapshotId) if (poolSnapshot === null) { poolSnapshot = new PoolSnapshot(poolSnapshotId) poolSnapshot.poolKey = poolKey.toHexString() - poolSnapshot.timestamp = event.block.timestamp + poolSnapshot.intervalType = intervalType + poolSnapshot.timestamp = BigInt.fromI64(timestampForAcc) poolSnapshot.price = event.params.oraclePrice poolSnapshot.liquidityA = totalLiquidityA poolSnapshot.liquidityB = totalLiquidityB