|
1 | 1 | import { log } from '@graphprotocol/graph-ts'
|
2 |
| -import { PoolTransaction } from '../@types/schema' |
3 | 2 | import {
|
4 |
| - LOG_BPT, |
5 | 3 | LOG_EXIT,
|
6 | 4 | LOG_JOIN,
|
7 | 5 | LOG_SETUP,
|
8 | 6 | LOG_SWAP
|
9 | 7 | } from '../@types/templates/BPool/BPool'
|
10 | 8 | import { Transfer } from '../@types/templates/BPool/BToken'
|
11 |
| -import { integer, PoolTransactionType } from './utils/constants' |
| 9 | +import { integer, PoolTransactionType, ZERO_ADDRESS } from './utils/constants' |
12 | 10 | import { weiToDecimal } from './utils/generic'
|
13 | 11 | import { getGlobalStats } from './utils/globalUtils'
|
14 | 12 | import {
|
15 | 13 | calcSpotPrice,
|
16 | 14 | getPool,
|
17 | 15 | getPoolTransaction,
|
18 |
| - getPoolShares, |
| 16 | + getPoolShare, |
19 | 17 | getPoolSnapshot
|
20 | 18 | } from './utils/poolUtils'
|
21 | 19 | import { getToken } from './utils/tokenUtils'
|
@@ -183,7 +181,7 @@ export function handleSwap(event: LOG_SWAP): void {
|
183 | 181 |
|
184 | 182 | // setup is just to set token weight(it will mostly be 50:50) and spotPrice
|
185 | 183 | export function handleSetup(event: LOG_SETUP): void {
|
186 |
| - log.warning('new Pool ', []) |
| 184 | + log.warning('new Pool from {} ', [event.transaction.from.toHexString()]) |
187 | 185 | const pool = getPool(event.address.toHex())
|
188 | 186 |
|
189 | 187 | pool.controller = event.params.caller.toHexString()
|
@@ -211,55 +209,82 @@ export function handleSetup(event: LOG_SETUP): void {
|
211 | 209 | )
|
212 | 210 | pool.spotPrice = spotPrice
|
213 | 211 | pool.isFinalized = true
|
214 |
| - const poolTx = PoolTransaction.load(event.transaction.hash.toHex()) |
215 |
| - if (poolTx) { |
216 |
| - poolTx.type = PoolTransactionType.SETUP |
217 |
| - poolTx.save() |
218 |
| - } |
| 212 | + // TODO: proper tx , add baseToken, datatoken |
| 213 | + const fromUser = getUser(event.transaction.from.toHexString()) |
| 214 | + const poolTx = getPoolTransaction( |
| 215 | + event, |
| 216 | + fromUser.id, |
| 217 | + PoolTransactionType.SETUP |
| 218 | + ) |
| 219 | + poolTx.type = PoolTransactionType.SETUP |
| 220 | + poolTx.baseToken = token.id |
| 221 | + poolTx.datatoken = datatoken.id |
| 222 | + const poolSnapshot = getPoolSnapshot(pool.id, event.block.timestamp.toI32()) |
| 223 | + poolSnapshot.spotPrice = spotPrice |
219 | 224 |
|
| 225 | + poolTx.save() |
| 226 | + poolSnapshot.save() |
220 | 227 | const globalStats = getGlobalStats()
|
221 | 228 | globalStats.poolCount = globalStats.poolCount + 1
|
222 | 229 | globalStats.save()
|
223 | 230 | pool.save()
|
224 | 231 | datatoken.save()
|
225 | 232 | }
|
226 | 233 |
|
227 |
| -export function handleBpt(event: LOG_BPT): void { |
228 |
| - const pool = getPool(event.address.toHex()) |
229 |
| - const poolShares = getPoolShares(pool.id, event.transaction.from.toHex()) |
230 |
| - const poolTx = PoolTransaction.load(event.transaction.hash.toHex()) |
231 |
| - // TODO: should we return here if null? theoretically this should not be null since LOG_BPT is after the other events |
232 |
| - if (!poolTx) return |
| 234 | +export function handlerBptTransfer(event: Transfer): void { |
| 235 | + const fromAddress = event.params.src.toHexString() |
| 236 | + const toAddress = event.params.dst.toHexString() |
| 237 | + const poolAddress = event.address.toHex() |
| 238 | + const caller = getUser(event.transaction.from.toHex()) |
| 239 | + const poolTx = getPoolTransaction(event, caller.id, PoolTransactionType.SWAP) |
| 240 | + const poolSnapshot = getPoolSnapshot( |
| 241 | + poolAddress, |
| 242 | + event.block.timestamp.toI32() |
| 243 | + ) |
| 244 | + log.warning('bpt transfer tx: {} from: {} | to {} | ammount {} ', [ |
| 245 | + event.transaction.hash.toHex(), |
| 246 | + fromAddress, |
| 247 | + toAddress, |
| 248 | + event.params.amt.toString() |
| 249 | + ]) |
233 | 250 |
|
234 |
| - const decimalBpt = weiToDecimal(event.params.bptAmount.toBigDecimal(), 18) |
| 251 | + // btoken has 18 decimals |
| 252 | + const ammount = weiToDecimal(event.params.amt.toBigDecimal(), 18) |
235 | 253 |
|
236 |
| - // for some reason switch is broken so reverting to good old if |
237 |
| - if (poolTx.type === PoolTransactionType.JOIN) { |
238 |
| - poolShares.shares = poolShares.shares.plus(decimalBpt) |
239 |
| - pool.totalShares.plus(decimalBpt) |
240 |
| - } |
241 |
| - if (poolTx.type === PoolTransactionType.EXIT) { |
242 |
| - poolShares.shares = poolShares.shares.minus(decimalBpt) |
243 |
| - pool.totalShares.minus(decimalBpt) |
| 254 | + if (fromAddress != ZERO_ADDRESS && toAddress != ZERO_ADDRESS) { |
| 255 | + poolTx.sharesTransferAmount = poolTx.sharesTransferAmount.plus(ammount) |
244 | 256 | }
|
245 | 257 |
|
246 |
| - poolShares.shares = weiToDecimal(event.params.bptAmount.toBigDecimal(), 18) |
| 258 | + if (fromAddress == ZERO_ADDRESS) { |
| 259 | + // add total |
| 260 | + const pool = getPool(poolAddress) |
| 261 | + pool.totalShares = pool.totalShares.plus(ammount) |
247 | 262 |
|
248 |
| - pool.save() |
249 |
| - poolShares.save() |
250 |
| -} |
251 |
| - |
252 |
| -export function handlerBptTransfer(event: Transfer): void { |
253 |
| - const fromUser = getPoolShares( |
254 |
| - event.address.toHex(), |
255 |
| - event.params.src.toHex() |
256 |
| - ) |
257 |
| - const toUser = getPoolShares(event.address.toHex(), event.params.dst.toHex()) |
258 |
| - const ammount = weiToDecimal(event.params.amt.toBigDecimal(), 18) |
| 263 | + // check tx? |
| 264 | + poolSnapshot.totalShares = pool.totalShares |
| 265 | + pool.save() |
| 266 | + } else { |
| 267 | + if (poolAddress != fromAddress) { |
| 268 | + const fromUser = getPoolShare(poolAddress, fromAddress) |
| 269 | + fromUser.shares = fromUser.shares.minus(ammount) |
| 270 | + fromUser.save() |
| 271 | + } |
| 272 | + } |
259 | 273 |
|
260 |
| - fromUser.shares = fromUser.shares.minus(ammount) |
261 |
| - toUser.shares = toUser.shares.plus(ammount) |
| 274 | + if (toAddress == ZERO_ADDRESS) { |
| 275 | + // remove |
| 276 | + const pool = getPool(poolAddress) |
| 277 | + pool.totalShares = pool.totalShares.minus(ammount) |
| 278 | + poolSnapshot.totalShares = pool.totalShares |
| 279 | + pool.save() |
| 280 | + } else { |
| 281 | + if (poolAddress != toAddress) { |
| 282 | + const toUser = getPoolShare(poolAddress, toAddress) |
| 283 | + toUser.shares = toUser.shares.plus(ammount) |
| 284 | + toUser.save() |
| 285 | + } |
| 286 | + } |
262 | 287 |
|
263 |
| - fromUser.save() |
264 |
| - toUser.save() |
| 288 | + poolTx.save() |
| 289 | + poolSnapshot.save() |
265 | 290 | }
|
0 commit comments