Add totalOceanLiquidity and fix TVL calculation

This commit is contained in:
ssallam 2021-01-29 11:57:05 +01:00
parent 5d829e6fcd
commit b5dd448cec
3 changed files with 26 additions and 3 deletions

View File

@ -2,7 +2,8 @@ type PoolFactory @entity {
id: ID!
totalLockedValue: BigDecimal # total value from all pools expressed in OCEAN
totalLiquidity: BigDecimal! # All the pools liquidity value in Ocean
totalOceanLiquidity: BigDecimal! # Total of OCEAN liquidity from all pools
totalSwapVolume: BigDecimal! # All the swap volume in Ocean
totalSwapFee: BigDecimal! # All the swap fee in Ocean

View File

@ -109,6 +109,12 @@ export function updatePoolTokenBalance(
null,
[source, poolToken.balance.toString(), balance.toString(), poolToken.poolId]
)
if (balance < ZERO_BD || poolToken.balance < ZERO_BD) {
log.warning(
'EEEEEEEEEEEEEEEEE poolToken.balance < Zero: pool={}, poolToken={}, oldBalance={}, newBalance={}',
[poolToken.poolId, poolToken.tokenAddress.toString(), poolToken.balance.toString(), balance.toString()])
}
poolToken.balance = balance
}
@ -183,8 +189,16 @@ export function updatePoolTransactionToken(
ptxTokenValues.save()
if (ptxTokenValues.tokenAddress == OCEAN) {
let factory = PoolFactory.load('1')
factory.totalOceanLiquidity = factory.totalOceanLiquidity + ptxTokenValues.tokenReserve - pool.oceanReserve
if (factory.totalOceanLiquidity < ZERO_BD || pool.oceanReserve < ZERO_BD) {
log.warning(
'EEEEEEEEEEEEEEEEE totalOceanLiquidity or oceanReserve < Zero: pool={}, totOcnLiq={}, ocnRes={}',
[pool.id, factory.totalOceanLiquidity.toString(), pool.oceanReserve.toString()])
}
ptx.oceanReserve = ptxTokenValues.tokenReserve
pool.oceanReserve = ptxTokenValues.tokenReserve
factory.save()
} else {
ptx.datatokenReserve = ptxTokenValues.tokenReserve
pool.datatokenReserve = ptxTokenValues.tokenReserve
@ -311,8 +325,16 @@ export function createPoolTransaction(
pool.consumePrice = poolTx.consumePrice
pool.spotPrice = poolTx.spotPrice
const lockedValue = pool.lockedValue
pool.lockedValue = pool.oceanReserve + (pool.datatokenReserve * pool.spotPrice)
const spotPrice = pool.spotPrice >= ZERO_BD ? pool.spotPrice : ZERO_BD
pool.lockedValue = poolTx.oceanReserve + (poolTx.datatokenReserve * spotPrice)
let factory = PoolFactory.load('1')
if (lockedValue < ZERO_BD || pool.lockedValue < ZERO_BD) {
log.warning(
'EEEEEEEEEEEEEEEEE valueLocked < Zero: pool={}, oldVL={}, newVL={}, OCEAN={}, DT={}, spotPrice={}',
[pool.id, lockedValue.toString(), pool.lockedValue.toString(),
poolTx.oceanReserve.toString(), poolTx.datatokenReserve.toString(),
pool.spotPrice.toString()])
}
factory.totalLockedValue = factory.totalLockedValue - lockedValue + pool.lockedValue
pool.transactionCount = pool.transactionCount.plus(BigInt.fromI32(1))

View File

@ -9,7 +9,7 @@ export function handleNewPool(event: BPoolRegistered): void {
if (factory == null) {
factory = new PoolFactory('1')
factory.totalLiquidity = ZERO_BD
factory.totalOceanLiquidity = ZERO_BD
factory.totalSwapVolume = ZERO_BD
factory.totalSwapFee = ZERO_BD
factory.totalLockedValue = ZERO_BD