This commit is contained in:
mihaisc 2022-04-05 09:20:56 -07:00 committed by GitHub
parent d8751fcb99
commit c531773136
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,7 +8,12 @@ import {
SwapFeeChanged SwapFeeChanged
} from '../@types/templates/BPool/BPool' } from '../@types/templates/BPool/BPool'
import { Transfer } from '../@types/templates/BPool/BToken' import { Transfer } from '../@types/templates/BPool/BToken'
import { integer, PoolTransactionType, ZERO_ADDRESS } from './utils/constants' import {
decimal,
integer,
PoolTransactionType,
ZERO_ADDRESS
} from './utils/constants'
import { weiToDecimal } from './utils/generic' import { weiToDecimal } from './utils/generic'
import { import {
addLiquidity, addLiquidity,
@ -115,14 +120,14 @@ export function handleSwap(event: LOG_SWAP): void {
pool.joinCount = pool.joinCount.plus(integer.ONE) pool.joinCount = pool.joinCount.plus(integer.ONE)
const poolSnapshot = getPoolSnapshot(pool.id, event.block.timestamp.toI32()) const poolSnapshot = getPoolSnapshot(pool.id, event.block.timestamp.toI32())
// get token out and update pool transaction, value is negative
const tokenOut = getToken(event.params.tokenOut, false) const tokenOut = getToken(event.params.tokenOut, false)
const tokenIn = getToken(event.params.tokenIn, false) const tokenIn = getToken(event.params.tokenIn, false)
let spotPrice = decimal.ZERO
const ammountOut = weiToDecimal( const ammountOut = weiToDecimal(
event.params.tokenAmountOut.toBigDecimal(), event.params.tokenAmountOut.toBigDecimal(),
tokenOut.decimals tokenOut.decimals
) )
let baseTokenDecimals = 18
const tokenOutNewBalance = weiToDecimal( const tokenOutNewBalance = weiToDecimal(
event.params.outBalance.toBigDecimal(), event.params.outBalance.toBigDecimal(),
tokenOut.decimals tokenOut.decimals
@ -140,7 +145,12 @@ export function handleSwap(event: LOG_SWAP): void {
} else { } else {
poolTx.baseToken = tokenOut.id poolTx.baseToken = tokenOut.id
poolTx.baseTokenValue = ammountOut.neg() poolTx.baseTokenValue = ammountOut.neg()
baseTokenDecimals = tokenOut.decimals
spotPrice = weiToDecimal(
event.params.newSpotPrice.toBigDecimal(),
tokenOut.decimals
)
pool.baseTokenLiquidity = tokenOutNewBalance pool.baseTokenLiquidity = tokenOutNewBalance
poolSnapshot.swapVolume = poolSnapshot.swapVolume.plus(ammountOut) poolSnapshot.swapVolume = poolSnapshot.swapVolume.plus(ammountOut)
@ -161,7 +171,10 @@ export function handleSwap(event: LOG_SWAP): void {
} else { } else {
poolTx.baseToken = tokenIn.id poolTx.baseToken = tokenIn.id
poolTx.baseTokenValue = ammountIn poolTx.baseTokenValue = ammountIn
baseTokenDecimals = tokenIn.decimals
spotPrice = decimal.ONE.div(
weiToDecimal(event.params.newSpotPrice.toBigDecimal(), tokenOut.decimals)
)
pool.baseTokenLiquidity = tokenInNewBalance pool.baseTokenLiquidity = tokenInNewBalance
poolSnapshot.swapVolume = poolSnapshot.swapVolume.plus(ammountIn) poolSnapshot.swapVolume = poolSnapshot.swapVolume.plus(ammountIn)
addLiquidity(tokenIn.id, ammountIn) addLiquidity(tokenIn.id, ammountIn)
@ -169,10 +182,6 @@ export function handleSwap(event: LOG_SWAP): void {
} }
// update spot price // update spot price
const spotPrice = weiToDecimal(
event.params.newSpotPrice.toBigDecimal(),
baseTokenDecimals
)
pool.spotPrice = spotPrice pool.spotPrice = spotPrice
poolSnapshot.spotPrice = spotPrice poolSnapshot.spotPrice = spotPrice
poolSnapshot.baseTokenLiquidity = pool.baseTokenLiquidity poolSnapshot.baseTokenLiquidity = pool.baseTokenLiquidity