This commit is contained in:
mihaisc 2021-11-23 11:54:40 +02:00
parent 0ceff45ef2
commit 9fcdbf6ce3
2 changed files with 103 additions and 11 deletions

View File

@ -376,6 +376,7 @@ type DispenserTransaction @entity {
type PoolSnapshot @entity { type PoolSnapshot @entity {
id: ID! id: ID!
pool: Pool! pool: Pool!
"total pool shares at the end of the 24h interval"
totalShares: BigDecimal! totalShares: BigDecimal!
"swap value 24h" "swap value 24h"
swapVolume: BigDecimal! swapVolume: BigDecimal!
@ -383,18 +384,16 @@ type PoolSnapshot @entity {
swapFees: BigDecimal! swapFees: BigDecimal!
"date without time" "date without time"
createdTimestamp: Int! createdTimestamp: Int!
spotPrice: BigDecimal! # TODO: last spot price or first one? "last spot price in the 24h interval"
tokens: [PoolSnapshotTokenValue!] @derivedFrom(field: "poolSnapshot") spotPrice: BigDecimal!
}
type PoolSnapshotTokenValue @entity { baseToken: Token!
id: ID! # pool tx + tokenAddress baseTokenLiquidity: BigDecimal!
token: Token!
value: BigDecimal!
tokenReserve: BigDecimal! # how many tokens are left in pool
poolSnapshot: PoolSnapshot!
}
datatoken: Token!
datatokenLiquidity: BigDecimal!
}
type GlobalStats @entity { type GlobalStats @entity {
id: ID! id: ID!

View File

@ -98,3 +98,96 @@ export function calcSpotPrice(
return price return price
} }
export function getPoolSnapshotId(poolAddress: string, timestamp: i32): string {
const dayTimestamp = timestamp - (timestamp % DAY) // Todays Timestamp
return `${poolAddress}-${dayTimestamp}`
}
export function createPoolSnapshot(poolId: string, timestamp: i32): void {
log.warning('Start create Pool Snapshot: {} {}', [
poolId,
timestamp.toString()
])
const dayTimestamp = timestamp - (timestamp % DAY) // Todays Timestamp
const pool = PoolEntity.load(poolId)
log.warning('got pool {} {}', [pool.id, poolId])
// Save pool snapshot
const snapshotId = poolId + '-' + dayTimestamp.toString()
log.warning('Creatnig Pool Snapshot with id {} {} {}', [
snapshotId,
pool.totalShares.toString(),
pool.totalSwapFee.toString()
])
const snapshot = new PoolSnapshot(snapshotId)
snapshot.pool = poolId
snapshot.totalShares = pool.totalShares
snapshot.swapVolume = ZERO_BD
snapshot.swapFees = pool.totalSwapFee
snapshot.timestamp = dayTimestamp
snapshot.save()
}
export function saveSwapToSnapshot(
poolAddress: string,
timestamp: i32,
volume: BigDecimal,
fees: BigDecimal
): void {
const dayTimestamp = timestamp - (timestamp % DAY) // Todays timestamp
// Save pool snapshot
const snapshotId = poolAddress + '-' + dayTimestamp.toString()
const snapshot = PoolSnapshot.load(snapshotId)
if (!snapshot) {
return
}
snapshot.swapVolume = snapshot.swapVolume.plus(volume)
snapshot.swapFees = snapshot.swapFees.plus(fees)
snapshot.save()
}
export function updatePoolSnapshotToken(
poolAddress: string,
timestamp: i32,
poolTokenId: string,
amount: BigDecimal,
balance: BigDecimal,
feeValue: BigDecimal
): void {
log.warning('Start create Pool Snapshot Token: {} {}', [
poolAddress,
timestamp.toString()
])
const dayTimestamp = timestamp - (timestamp % DAY) // Todays timestamp
const snapshotId = poolAddress + '-' + dayTimestamp.toString()
log.warning('Pool Snapshot Token: {} {} {} {}', [
amount.toString(),
balance.toString(),
feeValue.toString(),
snapshotId + '-' + poolTokenId
])
const token = new PoolSnapshotTokenValue(snapshotId + '-' + poolTokenId)
token.poolSnapshot = snapshotId
token.value = amount
token.tokenReserve = balance
token.tokenAddress = poolTokenId
token.feeValue = feeValue
if (amount.lt(ZERO_BD)) {
token.type = 'out'
} else {
token.type = 'in'
}
log.warning('Snapshot Token ID: {}', [token.id])
token.save()
}