Skip to content

Commit 30c1be3

Browse files
committed
add pool snapshot
1 parent 9fcdbf6 commit 30c1be3

File tree

4 files changed

+135
-138
lines changed

4 files changed

+135
-138
lines changed

schema.graphql

+9-16
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,13 @@ type Pool @entity {
108108
"maximum supply if any, converted from wei"
109109
cap: BigDecimal
110110

111-
baseToken: PoolToken!
112-
datatoken: PoolToken!
111+
baseToken: Token!
112+
baseTokenLiquidity: BigDecimal!
113+
baseTokenWeight: BigDecimal!
114+
115+
datatoken: Token!
116+
datatokenLiquidity: BigDecimal!
117+
datatokenWeight: BigDecimal!
113118

114119
"pool Fee percent, fee goes to all liquidity providers : SWAP, JOIN , EXIT"
115120
poolFee: BigDecimal!
@@ -166,18 +171,6 @@ type Pool @entity {
166171
transactions: [PoolTransaction!] @derivedFrom(field: "pool")
167172
}
168173

169-
# should not pe @entity
170-
type PoolToken @entity {
171-
"pool address + token address"
172-
id: ID!
173-
pool: Pool!
174-
token: Token!
175-
"balance of the token in this pool"
176-
balance: BigDecimal!
177-
"weight of token in the pool (50% for ocean bpools)"
178-
denormWeight: BigDecimal!
179-
}
180-
181174
# we will need to track pool share tx between users - bpool transfer tx event
182175
type PoolShares @entity {
183176
"poolAddress + userAddress"
@@ -216,7 +209,7 @@ type PoolTransaction @entity {
216209
marketFee: BigDecimal!
217210

218211
"block time when pool was created"
219-
createdTimestamp: Int!
212+
timestamp: Int!
220213
"pool creation transaction id"
221214
tx: Bytes
222215
"block number when it was created"
@@ -383,7 +376,7 @@ type PoolSnapshot @entity {
383376
"swap fee value 24h"
384377
swapFees: BigDecimal!
385378
"date without time"
386-
createdTimestamp: Int!
379+
date: Int!
387380
"last spot price in the 24h interval"
388381
spotPrice: BigDecimal!
389382

src/mappings/pool.ts

+56-24
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import { weiToDecimal } from './utils/generic'
1212
import {
1313
calcSpotPrice,
1414
getPool,
15-
getPoolToken,
1615
getPoolTransaction,
17-
getPoolShares
16+
getPoolShares,
17+
getPoolSnapshot
1818
} from './utils/poolUtils'
1919
import { getToken } from './utils/tokenUtils'
2020
import { getUser } from './utils/userUtils'
@@ -28,7 +28,8 @@ export function handleJoin(event: LOG_JOIN): void {
2828
pool.transactionCount = pool.transactionCount.plus(integer.ONE)
2929
pool.joinCount = pool.joinCount.plus(integer.ONE)
3030

31-
// get token, update pool transaction and update pool user liquidity
31+
// get token, update pool transaction, poolSnapshot
32+
const poolSnapshot = getPoolSnapshot(pool.id, event.block.timestamp.toI32())
3233
const token = getToken(event.params.tokenIn.toHex())
3334
const ammount = weiToDecimal(
3435
event.params.tokenAmountIn.toBigDecimal(),
@@ -37,16 +38,22 @@ export function handleJoin(event: LOG_JOIN): void {
3738
if (token.isDatatoken) {
3839
poolTx.datatoken = token.id
3940
poolTx.datatokenValue = ammount
41+
42+
poolSnapshot.datatokenLiquidity =
43+
poolSnapshot.datatokenLiquidity.plus(ammount)
44+
45+
pool.datatokenLiquidity.plus(ammount)
4046
} else {
4147
poolTx.baseToken = token.id
4248
poolTx.baseTokenValue = ammount
43-
}
4449

45-
// update pool token
46-
const poolToken = getPoolToken(pool.id, token.id)
47-
poolToken.balance.plus(ammount)
50+
poolSnapshot.baseTokenLiquidity =
51+
poolSnapshot.baseTokenLiquidity.plus(ammount)
52+
53+
pool.baseTokenLiquidity.plus(ammount)
54+
}
4855

49-
poolToken.save()
56+
poolSnapshot.save()
5057
poolTx.save()
5158
pool.save()
5259
}
@@ -61,22 +68,30 @@ export function handleExit(event: LOG_EXIT): void {
6168

6269
// get token and update pool transaction, value is negative because this is an exit event.
6370
const token = getToken(event.params.tokenOut.toHex())
71+
const poolSnapshot = getPoolSnapshot(pool.id, event.block.timestamp.toI32())
6472
const ammount = weiToDecimal(
6573
event.params.tokenAmountOut.toBigDecimal(),
6674
token.decimals
6775
)
6876
if (token.isDatatoken) {
6977
poolTx.datatoken = token.id
7078
poolTx.datatokenValue = ammount.neg()
79+
80+
poolSnapshot.datatokenLiquidity =
81+
poolSnapshot.datatokenLiquidity.minus(ammount)
82+
83+
pool.datatokenLiquidity.minus(ammount)
7184
} else {
7285
poolTx.baseToken = token.id
7386
poolTx.baseTokenValue = ammount.neg()
74-
}
7587

76-
const poolToken = getPoolToken(pool.id, token.id)
77-
poolToken.balance.minus(ammount)
88+
poolSnapshot.baseTokenLiquidity =
89+
poolSnapshot.baseTokenLiquidity.minus(ammount)
90+
91+
pool.baseTokenLiquidity.minus(ammount)
92+
}
7893

79-
poolToken.save()
94+
poolSnapshot.save()
8095
poolTx.save()
8196
pool.save()
8297
}
@@ -89,6 +104,7 @@ export function handleSwap(event: LOG_SWAP): void {
89104
pool.transactionCount = pool.transactionCount.plus(integer.ONE)
90105
pool.joinCount = pool.joinCount.plus(integer.ONE)
91106

107+
const poolSnapshot = getPoolSnapshot(pool.id, event.block.timestamp.toI32())
92108
// get token out and update pool transaction, value is negative
93109
const tokenOut = getToken(event.params.tokenOut.toHex())
94110
const ammountOut = weiToDecimal(
@@ -98,12 +114,20 @@ export function handleSwap(event: LOG_SWAP): void {
98114
if (tokenOut.isDatatoken) {
99115
poolTx.datatoken = tokenOut.id
100116
poolTx.datatokenValue = ammountOut.neg()
117+
118+
pool.datatokenLiquidity = pool.datatokenLiquidity.minus(ammountOut)
119+
120+
poolSnapshot.datatokenLiquidity =
121+
poolSnapshot.datatokenLiquidity.minus(ammountOut)
101122
} else {
102123
poolTx.baseToken = tokenOut.id
103124
poolTx.baseTokenValue = ammountOut.neg()
125+
126+
pool.baseTokenLiquidity = pool.baseTokenLiquidity.minus(ammountOut)
127+
128+
poolSnapshot.baseTokenLiquidity =
129+
poolSnapshot.baseTokenLiquidity.minus(ammountOut)
104130
}
105-
const poolTokenOut = getPoolToken(pool.id, tokenOut.id)
106-
poolTokenOut.balance.minus(ammountOut)
107131

108132
// update pool token in
109133
const tokenIn = getToken(event.params.tokenIn.toHex())
@@ -114,12 +138,20 @@ export function handleSwap(event: LOG_SWAP): void {
114138
if (tokenIn.isDatatoken) {
115139
poolTx.datatoken = tokenIn.id
116140
poolTx.datatokenValue = ammountIn
141+
142+
pool.datatokenLiquidity = pool.datatokenLiquidity.plus(ammountIn)
143+
144+
poolSnapshot.datatokenLiquidity =
145+
poolSnapshot.datatokenLiquidity.plus(ammountIn)
117146
} else {
118147
poolTx.baseToken = tokenIn.id
119148
poolTx.baseTokenValue = ammountIn
149+
150+
pool.baseTokenLiquidity = pool.baseTokenLiquidity.plus(ammountIn)
151+
152+
poolSnapshot.baseTokenLiquidity =
153+
poolSnapshot.baseTokenLiquidity.plus(ammountIn)
120154
}
121-
const poolTokenIn = getPoolToken(pool.id, tokenIn.id)
122-
poolTokenIn.balance.plus(ammountIn)
123155

124156
// update spot price
125157
const isTokenInDatatoken = tokenIn.isDatatoken
@@ -130,28 +162,28 @@ export function handleSwap(event: LOG_SWAP): void {
130162
isTokenInDatatoken ? tokenIn.decimals : tokenOut.decimals
131163
)
132164
pool.spotPrice = spotPrice
165+
poolSnapshot.spotPrice = spotPrice
133166

134-
poolTokenIn.save()
135-
poolTokenOut.save()
167+
poolSnapshot.save()
136168
poolTx.save()
137169
pool.save()
138170
}
139171

140-
// setup is just to set token weight and spotPrice , it will mostly be 50:50
172+
// setup is just to set token weight(it will mostly be 50:50) and spotPrice
141173
export function handleSetup(event: LOG_SETUP): void {
142174
const pool = getPool(event.address.toHex())
143175

144176
const token = getToken(event.params.baseToken.toHex())
145-
const baseToken = getPoolToken(pool.id, event.params.baseToken.toHex())
146-
baseToken.denormWeight = weiToDecimal(
177+
pool.baseToken = token.id
178+
pool.baseTokenWeight = weiToDecimal(
147179
event.params.baseTokenWeight.toBigDecimal(),
148180
token.decimals
149181
)
150-
baseToken.save()
151182

152183
// decimals hardcoded because datatokens have 18 decimals
153-
const datatoken = getPoolToken(pool.id, event.params.dataToken.toHex())
154-
datatoken.denormWeight = weiToDecimal(
184+
const datatoken = getToken(event.params.dataToken.toHex())
185+
pool.datatoken = datatoken.id
186+
pool.datatokenWeight = weiToDecimal(
155187
event.params.dataTokenWeight.toBigDecimal(),
156188
18
157189
)

src/mappings/utils/constants.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { BigDecimal, BigInt } from '@graphprotocol/graph-ts'
22

33
export const ENABLE_DEBUG = true
44

5+
export const DAY = 24 * 60 * 60
6+
57
export namespace integer {
68
export const NEGATIVE_ONE = BigInt.fromI32(-1)
79
export const ZERO = BigInt.fromI32(0)

0 commit comments

Comments
 (0)