Skip to content

Commit

Permalink
feat: update schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Dorvin committed Oct 8, 2024
1 parent b34d3fd commit 1846323
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
5 changes: 4 additions & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
6 changes: 1 addition & 5 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function buildChartLogId(
.concat(timestamp.toString())
}

export function buildPoolVolumeId(
export function buildPoolVolumeAndSnapshotId(
poolKey: Bytes,
intervalType: string,
timestamp: i64,
Expand All @@ -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)
}
Expand Down
25 changes: 19 additions & 6 deletions src/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import {
} from './addresses'
import {
baseToQuote,
buildPoolSnapshotId,
buildPoolVolumeId,
buildPoolVolumeAndSnapshotId,
CHART_LOG_INTERVALS,
} from './helpers'

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1846323

Please sign in to comment.