Skip to content

Commit 9fcdbf6

Browse files
committed
snapshot
1 parent 0ceff45 commit 9fcdbf6

File tree

2 files changed

+103
-11
lines changed

2 files changed

+103
-11
lines changed

schema.graphql

+10-11
Original file line numberDiff line numberDiff line change
@@ -376,25 +376,24 @@ type DispenserTransaction @entity {
376376
type PoolSnapshot @entity {
377377
id: ID!
378378
pool: Pool!
379+
"total pool shares at the end of the 24h interval"
379380
totalShares: BigDecimal!
380381
"swap value 24h"
381382
swapVolume: BigDecimal!
382383
"swap fee value 24h"
383384
swapFees: BigDecimal!
384385
"date without time"
385-
createdTimestamp: Int!
386-
spotPrice: BigDecimal! # TODO: last spot price or first one?
387-
tokens: [PoolSnapshotTokenValue!] @derivedFrom(field: "poolSnapshot")
388-
}
386+
createdTimestamp: Int!
387+
"last spot price in the 24h interval"
388+
spotPrice: BigDecimal!
389+
390+
baseToken: Token!
391+
baseTokenLiquidity: BigDecimal!
389392

390-
type PoolSnapshotTokenValue @entity {
391-
id: ID! # pool tx + tokenAddress
392-
token: Token!
393-
value: BigDecimal!
394-
tokenReserve: BigDecimal! # how many tokens are left in pool
395-
poolSnapshot: PoolSnapshot!
396-
}
393+
datatoken: Token!
394+
datatokenLiquidity: BigDecimal!
397395

396+
}
398397
type GlobalStats @entity {
399398
id: ID!
400399

src/mappings/utils/poolUtils.ts

+93
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,96 @@ export function calcSpotPrice(
9898

9999
return price
100100
}
101+
102+
103+
export function getPoolSnapshotId(poolAddress: string, timestamp: i32): string {
104+
const dayTimestamp = timestamp - (timestamp % DAY) // Todays Timestamp
105+
return `${poolAddress}-${dayTimestamp}`
106+
}
107+
108+
export function createPoolSnapshot(poolId: string, timestamp: i32): void {
109+
log.warning('Start create Pool Snapshot: {} {}', [
110+
poolId,
111+
timestamp.toString()
112+
])
113+
const dayTimestamp = timestamp - (timestamp % DAY) // Todays Timestamp
114+
115+
const pool = PoolEntity.load(poolId)
116+
117+
log.warning('got pool {} {}', [pool.id, poolId])
118+
// Save pool snapshot
119+
const snapshotId = poolId + '-' + dayTimestamp.toString()
120+
121+
log.warning('Creatnig Pool Snapshot with id {} {} {}', [
122+
snapshotId,
123+
pool.totalShares.toString(),
124+
pool.totalSwapFee.toString()
125+
])
126+
const snapshot = new PoolSnapshot(snapshotId)
127+
128+
snapshot.pool = poolId
129+
130+
snapshot.totalShares = pool.totalShares
131+
snapshot.swapVolume = ZERO_BD
132+
snapshot.swapFees = pool.totalSwapFee
133+
snapshot.timestamp = dayTimestamp
134+
snapshot.save()
135+
}
136+
137+
export function saveSwapToSnapshot(
138+
poolAddress: string,
139+
timestamp: i32,
140+
volume: BigDecimal,
141+
fees: BigDecimal
142+
): void {
143+
const dayTimestamp = timestamp - (timestamp % DAY) // Todays timestamp
144+
145+
// Save pool snapshot
146+
const snapshotId = poolAddress + '-' + dayTimestamp.toString()
147+
const snapshot = PoolSnapshot.load(snapshotId)
148+
149+
if (!snapshot) {
150+
return
151+
}
152+
153+
snapshot.swapVolume = snapshot.swapVolume.plus(volume)
154+
snapshot.swapFees = snapshot.swapFees.plus(fees)
155+
snapshot.save()
156+
}
157+
158+
export function updatePoolSnapshotToken(
159+
poolAddress: string,
160+
timestamp: i32,
161+
poolTokenId: string,
162+
amount: BigDecimal,
163+
balance: BigDecimal,
164+
feeValue: BigDecimal
165+
): void {
166+
log.warning('Start create Pool Snapshot Token: {} {}', [
167+
poolAddress,
168+
timestamp.toString()
169+
])
170+
const dayTimestamp = timestamp - (timestamp % DAY) // Todays timestamp
171+
172+
const snapshotId = poolAddress + '-' + dayTimestamp.toString()
173+
log.warning('Pool Snapshot Token: {} {} {} {}', [
174+
amount.toString(),
175+
balance.toString(),
176+
feeValue.toString(),
177+
snapshotId + '-' + poolTokenId
178+
])
179+
const token = new PoolSnapshotTokenValue(snapshotId + '-' + poolTokenId)
180+
181+
token.poolSnapshot = snapshotId
182+
token.value = amount
183+
token.tokenReserve = balance
184+
token.tokenAddress = poolTokenId
185+
token.feeValue = feeValue
186+
if (amount.lt(ZERO_BD)) {
187+
token.type = 'out'
188+
} else {
189+
token.type = 'in'
190+
}
191+
log.warning('Snapshot Token ID: {}', [token.id])
192+
token.save()
193+
}

0 commit comments

Comments
 (0)