diff --git a/src/helpers.ts b/src/helpers.ts index 981f20a..8324477 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -218,10 +218,9 @@ export function updatePoolTransactionToken( log.warning('ptxTokenValues {} saved', [ptxTokenValues.id]) if (ptxTokenValues.tokenAddress == OCEAN) { const factory = PoolFactory.load('1') - factory.totalOceanLiquidity = - factory.totalOceanLiquidity + - ptxTokenValues.tokenReserve - - pool.oceanReserve + factory.totalOceanLiquidity = factory.totalOceanLiquidity + .plus(ptxTokenValues.tokenReserve) + .minus(pool.oceanReserve) if (factory.totalOceanLiquidity < ZERO_BD || pool.oceanReserve < ZERO_BD) { log.warning( 'EEEEEEEEEEEEEEEEE totalOceanLiquidity or oceanReserve < Zero: pool={}, totOcnLiq={}, ocnRes={}', @@ -324,8 +323,24 @@ export function createPoolTransaction( // Initial reserve values, will be updated in `updatePoolTransactionToken` poolTx.datatokenReserve = dtToken.balance poolTx.oceanReserve = ocnToken.balance + debuglog('poolTX reserves:(dt, ocean)', null, [ + poolTx.datatokenReserve.toString(), + poolTx.oceanReserve.toString() + ]) const p = Pool.bind(Address.fromString(poolId)) + debuglog( + 'createPoolTransaction args sent to calcInGivenOut (ocnBalance, ocnWeight, dtBalance, dtWeight, dtAmount, swapFee)', + null, + [ + decimalToBigInt(ocnToken.balance).toString(), + decimalToBigInt(ocnToken.denormWeight).toString(), + decimalToBigInt(dtToken.balance).toString(), + decimalToBigInt(dtToken.denormWeight).toString(), + ONE_BASE_18.toString(), + decimalToBigInt(pool.swapFee).toString() + ] + ) const priceResult = p.try_calcInGivenOut( decimalToBigInt(ocnToken.balance), decimalToBigInt(ocnToken.denormWeight), @@ -334,35 +349,35 @@ export function createPoolTransaction( ONE_BASE_18, decimalToBigInt(pool.swapFee) ) - debuglog( - 'createPoolTransaction args to calcInGivenOut (ocnBalance, ocnWeight, dtBalance, dtWeight, dtAmount, swapFee, result)', - null, - [ - decimalToBigInt(ocnToken.balance).toString(), - decimalToBigInt(ocnToken.denormWeight).toString(), - decimalToBigInt(dtToken.balance).toString(), - decimalToBigInt(dtToken.denormWeight).toString(), - ONE_BASE_18.toString(), - decimalToBigInt(pool.swapFee).toString(), - priceResult.reverted ? 'failed' : priceResult.value.toString() - ] - ) + debuglog('got results', null, []) poolTx.consumePrice = priceResult.reverted ? MINUS_1_BD : bigIntToDecimal(priceResult.value, 18) - poolTx.spotPrice = calcSpotPrice( - ocnToken.balance, - ocnToken.denormWeight, - dtToken.balance, - dtToken.denormWeight, - pool.swapFee + debuglog('calcInGivenOut:', null, [ + priceResult.reverted ? 'failed' : priceResult.value.toString() + ]) + + const priceSpot = p.try_calcSpotPrice( + decimalToBigInt(ocnToken.balance), + decimalToBigInt(ocnToken.denormWeight), + decimalToBigInt(dtToken.balance), + decimalToBigInt(dtToken.denormWeight), + decimalToBigInt(pool.swapFee) ) + poolTx.spotPrice = priceSpot.reverted + ? ZERO_BD + : bigIntToDecimal(priceSpot.value, 18) + debuglog('SpotPrice:', null, [ + priceSpot.reverted ? 'failed' : priceSpot.value.toString() + ]) pool.consumePrice = poolTx.consumePrice pool.spotPrice = poolTx.spotPrice const oldValueLocked = pool.valueLocked const spotPrice = pool.spotPrice >= ZERO_BD ? pool.spotPrice : ZERO_BD - pool.valueLocked = poolTx.oceanReserve + poolTx.datatokenReserve * spotPrice + pool.valueLocked = poolTx.oceanReserve.plus( + poolTx.datatokenReserve.times(spotPrice) + ) const factory = PoolFactory.load('1') if (oldValueLocked < ZERO_BD || pool.valueLocked < ZERO_BD) { log.warning( @@ -377,8 +392,9 @@ export function createPoolTransaction( ] ) } - factory.totalValueLocked = - factory.totalValueLocked - oldValueLocked + pool.valueLocked + factory.totalValueLocked = factory.totalValueLocked + .minus(oldValueLocked) + .plus(pool.valueLocked) pool.transactionCount = pool.transactionCount.plus(BigInt.fromI32(1)) diff --git a/src/mappings/pool.ts b/src/mappings/pool.ts index 5302c75..b5b3a32 100644 --- a/src/mappings/pool.ts +++ b/src/mappings/pool.ts @@ -4,7 +4,8 @@ import { LOG_JOIN, LOG_EXIT, LOG_SWAP, - Transfer + Transfer, + Pool as PoolEntity } from '../@types/templates/Pool/Pool' import { @@ -27,7 +28,8 @@ import { createPoolTransaction, OCEAN, debuglog, - updatePoolTokenBalance + updatePoolTokenBalance, + bigIntToDecimal } from '../helpers' /************************************ @@ -397,7 +399,6 @@ export function handleSwap(event: LOG_SWAP): void { pool.active = false } pool.save() - createPoolTransaction(event, 'swap', event.params.caller.toHexString()) updatePoolTransactionToken( ptx, @@ -496,14 +497,14 @@ export function handleTransfer(event: Transfer): void { createPoolShareEntity(poolShareToId, poolId, event.params.to.toHex()) poolShareTo = PoolShare.load(poolShareToId) } - poolShareTo.balance += value + poolShareTo.balance.plus(value) poolShareTo.save() if (poolShareFrom == null) { createPoolShareEntity(poolShareFromId, poolId, event.params.from.toHex()) poolShareFrom = PoolShare.load(poolShareFromId) } - poolShareFrom.balance -= value + poolShareFrom.balance.minus(value) poolShareFrom.save() debuglog( 'pool shares transfer: ' + @@ -526,7 +527,7 @@ export function handleTransfer(event: Transfer): void { poolShareTo.balance.notEqual(ZERO_BD) && poolShareToBalance.equals(ZERO_BD) ) { - pool.holderCount += BigInt.fromI32(1) + pool.holderCount.plus(BigInt.fromI32(1)) } if ( @@ -534,7 +535,7 @@ export function handleTransfer(event: Transfer): void { poolShareFrom.balance.equals(ZERO_BD) && poolShareFromBalance.notEqual(ZERO_BD) ) { - pool.holderCount -= BigInt.fromI32(1) + pool.holderCount.plus(BigInt.fromI32(1)) } if (poolTx != null) { @@ -543,3 +544,73 @@ export function handleTransfer(event: Transfer): void { pool.save() } + +/************************************ + *********** GULP ************ + ************************************/ +export function handleGulp(event: LOG_CALL): void { + const poolId = event.address.toHex() + const ptx = event.transaction.hash.toHexString() + // we need to check the contract balance & compare with our internal balances + const pool = Pool.load(poolId) + const poolEbtity = PoolEntity.bind(Address.fromString(poolId)) + if (!pool) { + log.warning('Gulp called, but cannot load pool {}', [poolId]) + return + } + const ocnToken = PoolToken.load(poolId.concat('-').concat(OCEAN)) + const dtToken = PoolToken.load( + poolId.concat('-').concat(pool.datatokenAddress) + ) + const ocnTokenBalance = ocnToken.balance + const dtTokenBalance = dtToken.balance + // get the balances from the contract + // for ocean + if (ocnToken) { + const balanceAttempt = poolEbtity.try_getBalance(Address.fromString(OCEAN)) + if (!balanceAttempt.reverted) { + const contractBalance = bigIntToDecimal(balanceAttempt.value, 18) + if ( + ocnToken.balance.notEqual(contractBalance) && + contractBalance.ge(ZERO_BD) + ) { + // we have a difference. let's absorb that + createPoolTransaction(event, 'gulp', event.params.caller.toHexString()) + ocnToken.balance = contractBalance + ocnToken.save() + updatePoolTransactionToken( + ptx, + ocnToken.id, + contractBalance.minus(ocnTokenBalance), + contractBalance, + ZERO_BD + ) + } + } + } + // for dt + if (dtToken) { + const balanceAttempt = poolEbtity.try_getBalance( + Address.fromString(pool.datatokenAddress) + ) + if (!balanceAttempt.reverted) { + const contractBalance = bigIntToDecimal(balanceAttempt.value, 18) + if ( + dtToken.balance.notEqual(contractBalance) && + contractBalance.ge(ZERO_BD) + ) { + // we have a difference. let's absorb that + createPoolTransaction(event, 'gulp', event.params.caller.toHexString()) + dtToken.balance = contractBalance + dtToken.save() + updatePoolTransactionToken( + ptx, + dtToken.id, + contractBalance.minus(dtTokenBalance), + contractBalance, + ZERO_BD + ) + } + } + } +} diff --git a/subgraph.moonbeamalpha.yaml b/subgraph.moonbeamalpha.yaml index c204ea4..457e229 100644 --- a/subgraph.moonbeamalpha.yaml +++ b/subgraph.moonbeamalpha.yaml @@ -136,6 +136,9 @@ templates: - event: LOG_CALL(indexed bytes4,indexed address,bytes) topic0: '0xe4e1e53800000000000000000000000000000000000000000000000000000000' handler: handleRebind + - event: LOG_CALL(indexed bytes4,indexed address,bytes) + topic0: '0x8c28cbe800000000000000000000000000000000000000000000000000000000' + handler: handleGulp - event: LOG_JOIN(indexed address,indexed address,uint256) handler: handleJoinPool - event: LOG_EXIT(indexed address,indexed address,uint256) diff --git a/subgraph.polygon.yaml b/subgraph.polygon.yaml index e973724..2e88325 100644 --- a/subgraph.polygon.yaml +++ b/subgraph.polygon.yaml @@ -136,6 +136,9 @@ templates: - event: LOG_CALL(indexed bytes4,indexed address,bytes) topic0: '0xe4e1e53800000000000000000000000000000000000000000000000000000000' handler: handleRebind + - event: LOG_CALL(indexed bytes4,indexed address,bytes) + topic0: '0x8c28cbe800000000000000000000000000000000000000000000000000000000' + handler: handleGulp - event: LOG_JOIN(indexed address,indexed address,uint256) handler: handleJoinPool - event: LOG_EXIT(indexed address,indexed address,uint256) diff --git a/subgraph.rinkeby.yaml b/subgraph.rinkeby.yaml index 27c16d4..494bf69 100644 --- a/subgraph.rinkeby.yaml +++ b/subgraph.rinkeby.yaml @@ -136,6 +136,9 @@ templates: - event: LOG_CALL(indexed bytes4,indexed address,bytes) topic0: '0xe4e1e53800000000000000000000000000000000000000000000000000000000' handler: handleRebind + - event: LOG_CALL(indexed bytes4,indexed address,bytes) + topic0: '0x8c28cbe800000000000000000000000000000000000000000000000000000000' + handler: handleGulp - event: LOG_JOIN(indexed address,indexed address,uint256) handler: handleJoinPool - event: LOG_EXIT(indexed address,indexed address,uint256) diff --git a/subgraph.ropsten.yaml b/subgraph.ropsten.yaml index bebf7a8..00827fc 100644 --- a/subgraph.ropsten.yaml +++ b/subgraph.ropsten.yaml @@ -136,6 +136,9 @@ templates: - event: LOG_CALL(indexed bytes4,indexed address,bytes) topic0: '0xe4e1e53800000000000000000000000000000000000000000000000000000000' handler: handleRebind + - event: LOG_CALL(indexed bytes4,indexed address,bytes) + topic0: '0x8c28cbe800000000000000000000000000000000000000000000000000000000' + handler: handleGulp - event: LOG_JOIN(indexed address,indexed address,uint256) handler: handleJoinPool - event: LOG_EXIT(indexed address,indexed address,uint256) diff --git a/subgraph.yaml b/subgraph.yaml index 4a6f5a7..364f48a 100644 --- a/subgraph.yaml +++ b/subgraph.yaml @@ -136,6 +136,9 @@ templates: - event: LOG_CALL(indexed bytes4,indexed address,bytes) topic0: '0xe4e1e53800000000000000000000000000000000000000000000000000000000' handler: handleRebind + - event: LOG_CALL(indexed bytes4,indexed address,bytes) + topic0: '0x8c28cbe800000000000000000000000000000000000000000000000000000000' + handler: handleGulp - event: LOG_JOIN(indexed address,indexed address,uint256) handler: handleJoinPool - event: LOG_EXIT(indexed address,indexed address,uint256)