* add gulp

* fix transaction values
This commit is contained in:
Alex Coseru 2021-05-13 09:17:54 +03:00 committed by GitHub
parent 3f3f2a09a7
commit 07b02c2b5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 135 additions and 33 deletions

View File

@ -218,10 +218,9 @@ export function updatePoolTransactionToken(
log.warning('ptxTokenValues {} saved', [ptxTokenValues.id]) log.warning('ptxTokenValues {} saved', [ptxTokenValues.id])
if (ptxTokenValues.tokenAddress == OCEAN) { if (ptxTokenValues.tokenAddress == OCEAN) {
const factory = PoolFactory.load('1') const factory = PoolFactory.load('1')
factory.totalOceanLiquidity = factory.totalOceanLiquidity = factory.totalOceanLiquidity
factory.totalOceanLiquidity + .plus(ptxTokenValues.tokenReserve)
ptxTokenValues.tokenReserve - .minus(pool.oceanReserve)
pool.oceanReserve
if (factory.totalOceanLiquidity < ZERO_BD || pool.oceanReserve < ZERO_BD) { if (factory.totalOceanLiquidity < ZERO_BD || pool.oceanReserve < ZERO_BD) {
log.warning( log.warning(
'EEEEEEEEEEEEEEEEE totalOceanLiquidity or oceanReserve < Zero: pool={}, totOcnLiq={}, ocnRes={}', 'EEEEEEEEEEEEEEEEE totalOceanLiquidity or oceanReserve < Zero: pool={}, totOcnLiq={}, ocnRes={}',
@ -324,8 +323,24 @@ export function createPoolTransaction(
// Initial reserve values, will be updated in `updatePoolTransactionToken` // Initial reserve values, will be updated in `updatePoolTransactionToken`
poolTx.datatokenReserve = dtToken.balance poolTx.datatokenReserve = dtToken.balance
poolTx.oceanReserve = ocnToken.balance poolTx.oceanReserve = ocnToken.balance
debuglog('poolTX reserves:(dt, ocean)', null, [
poolTx.datatokenReserve.toString(),
poolTx.oceanReserve.toString()
])
const p = Pool.bind(Address.fromString(poolId)) 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( const priceResult = p.try_calcInGivenOut(
decimalToBigInt(ocnToken.balance), decimalToBigInt(ocnToken.balance),
decimalToBigInt(ocnToken.denormWeight), decimalToBigInt(ocnToken.denormWeight),
@ -334,35 +349,35 @@ export function createPoolTransaction(
ONE_BASE_18, ONE_BASE_18,
decimalToBigInt(pool.swapFee) decimalToBigInt(pool.swapFee)
) )
debuglog( debuglog('got results', null, [])
'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()
]
)
poolTx.consumePrice = priceResult.reverted poolTx.consumePrice = priceResult.reverted
? MINUS_1_BD ? MINUS_1_BD
: bigIntToDecimal(priceResult.value, 18) : bigIntToDecimal(priceResult.value, 18)
poolTx.spotPrice = calcSpotPrice( debuglog('calcInGivenOut:', null, [
ocnToken.balance, priceResult.reverted ? 'failed' : priceResult.value.toString()
ocnToken.denormWeight, ])
dtToken.balance,
dtToken.denormWeight, const priceSpot = p.try_calcSpotPrice(
pool.swapFee 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.consumePrice = poolTx.consumePrice
pool.spotPrice = poolTx.spotPrice pool.spotPrice = poolTx.spotPrice
const oldValueLocked = pool.valueLocked const oldValueLocked = pool.valueLocked
const spotPrice = pool.spotPrice >= ZERO_BD ? pool.spotPrice : ZERO_BD 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') const factory = PoolFactory.load('1')
if (oldValueLocked < ZERO_BD || pool.valueLocked < ZERO_BD) { if (oldValueLocked < ZERO_BD || pool.valueLocked < ZERO_BD) {
log.warning( log.warning(
@ -377,8 +392,9 @@ export function createPoolTransaction(
] ]
) )
} }
factory.totalValueLocked = factory.totalValueLocked = factory.totalValueLocked
factory.totalValueLocked - oldValueLocked + pool.valueLocked .minus(oldValueLocked)
.plus(pool.valueLocked)
pool.transactionCount = pool.transactionCount.plus(BigInt.fromI32(1)) pool.transactionCount = pool.transactionCount.plus(BigInt.fromI32(1))

View File

@ -4,7 +4,8 @@ import {
LOG_JOIN, LOG_JOIN,
LOG_EXIT, LOG_EXIT,
LOG_SWAP, LOG_SWAP,
Transfer Transfer,
Pool as PoolEntity
} from '../@types/templates/Pool/Pool' } from '../@types/templates/Pool/Pool'
import { import {
@ -27,7 +28,8 @@ import {
createPoolTransaction, createPoolTransaction,
OCEAN, OCEAN,
debuglog, debuglog,
updatePoolTokenBalance updatePoolTokenBalance,
bigIntToDecimal
} from '../helpers' } from '../helpers'
/************************************ /************************************
@ -397,7 +399,6 @@ export function handleSwap(event: LOG_SWAP): void {
pool.active = false pool.active = false
} }
pool.save() pool.save()
createPoolTransaction(event, 'swap', event.params.caller.toHexString()) createPoolTransaction(event, 'swap', event.params.caller.toHexString())
updatePoolTransactionToken( updatePoolTransactionToken(
ptx, ptx,
@ -496,14 +497,14 @@ export function handleTransfer(event: Transfer): void {
createPoolShareEntity(poolShareToId, poolId, event.params.to.toHex()) createPoolShareEntity(poolShareToId, poolId, event.params.to.toHex())
poolShareTo = PoolShare.load(poolShareToId) poolShareTo = PoolShare.load(poolShareToId)
} }
poolShareTo.balance += value poolShareTo.balance.plus(value)
poolShareTo.save() poolShareTo.save()
if (poolShareFrom == null) { if (poolShareFrom == null) {
createPoolShareEntity(poolShareFromId, poolId, event.params.from.toHex()) createPoolShareEntity(poolShareFromId, poolId, event.params.from.toHex())
poolShareFrom = PoolShare.load(poolShareFromId) poolShareFrom = PoolShare.load(poolShareFromId)
} }
poolShareFrom.balance -= value poolShareFrom.balance.minus(value)
poolShareFrom.save() poolShareFrom.save()
debuglog( debuglog(
'pool shares transfer: ' + 'pool shares transfer: ' +
@ -526,7 +527,7 @@ export function handleTransfer(event: Transfer): void {
poolShareTo.balance.notEqual(ZERO_BD) && poolShareTo.balance.notEqual(ZERO_BD) &&
poolShareToBalance.equals(ZERO_BD) poolShareToBalance.equals(ZERO_BD)
) { ) {
pool.holderCount += BigInt.fromI32(1) pool.holderCount.plus(BigInt.fromI32(1))
} }
if ( if (
@ -534,7 +535,7 @@ export function handleTransfer(event: Transfer): void {
poolShareFrom.balance.equals(ZERO_BD) && poolShareFrom.balance.equals(ZERO_BD) &&
poolShareFromBalance.notEqual(ZERO_BD) poolShareFromBalance.notEqual(ZERO_BD)
) { ) {
pool.holderCount -= BigInt.fromI32(1) pool.holderCount.plus(BigInt.fromI32(1))
} }
if (poolTx != null) { if (poolTx != null) {
@ -543,3 +544,73 @@ export function handleTransfer(event: Transfer): void {
pool.save() 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
)
}
}
}
}

View File

@ -136,6 +136,9 @@ templates:
- event: LOG_CALL(indexed bytes4,indexed address,bytes) - event: LOG_CALL(indexed bytes4,indexed address,bytes)
topic0: '0xe4e1e53800000000000000000000000000000000000000000000000000000000' topic0: '0xe4e1e53800000000000000000000000000000000000000000000000000000000'
handler: handleRebind handler: handleRebind
- event: LOG_CALL(indexed bytes4,indexed address,bytes)
topic0: '0x8c28cbe800000000000000000000000000000000000000000000000000000000'
handler: handleGulp
- event: LOG_JOIN(indexed address,indexed address,uint256) - event: LOG_JOIN(indexed address,indexed address,uint256)
handler: handleJoinPool handler: handleJoinPool
- event: LOG_EXIT(indexed address,indexed address,uint256) - event: LOG_EXIT(indexed address,indexed address,uint256)

View File

@ -136,6 +136,9 @@ templates:
- event: LOG_CALL(indexed bytes4,indexed address,bytes) - event: LOG_CALL(indexed bytes4,indexed address,bytes)
topic0: '0xe4e1e53800000000000000000000000000000000000000000000000000000000' topic0: '0xe4e1e53800000000000000000000000000000000000000000000000000000000'
handler: handleRebind handler: handleRebind
- event: LOG_CALL(indexed bytes4,indexed address,bytes)
topic0: '0x8c28cbe800000000000000000000000000000000000000000000000000000000'
handler: handleGulp
- event: LOG_JOIN(indexed address,indexed address,uint256) - event: LOG_JOIN(indexed address,indexed address,uint256)
handler: handleJoinPool handler: handleJoinPool
- event: LOG_EXIT(indexed address,indexed address,uint256) - event: LOG_EXIT(indexed address,indexed address,uint256)

View File

@ -136,6 +136,9 @@ templates:
- event: LOG_CALL(indexed bytes4,indexed address,bytes) - event: LOG_CALL(indexed bytes4,indexed address,bytes)
topic0: '0xe4e1e53800000000000000000000000000000000000000000000000000000000' topic0: '0xe4e1e53800000000000000000000000000000000000000000000000000000000'
handler: handleRebind handler: handleRebind
- event: LOG_CALL(indexed bytes4,indexed address,bytes)
topic0: '0x8c28cbe800000000000000000000000000000000000000000000000000000000'
handler: handleGulp
- event: LOG_JOIN(indexed address,indexed address,uint256) - event: LOG_JOIN(indexed address,indexed address,uint256)
handler: handleJoinPool handler: handleJoinPool
- event: LOG_EXIT(indexed address,indexed address,uint256) - event: LOG_EXIT(indexed address,indexed address,uint256)

View File

@ -136,6 +136,9 @@ templates:
- event: LOG_CALL(indexed bytes4,indexed address,bytes) - event: LOG_CALL(indexed bytes4,indexed address,bytes)
topic0: '0xe4e1e53800000000000000000000000000000000000000000000000000000000' topic0: '0xe4e1e53800000000000000000000000000000000000000000000000000000000'
handler: handleRebind handler: handleRebind
- event: LOG_CALL(indexed bytes4,indexed address,bytes)
topic0: '0x8c28cbe800000000000000000000000000000000000000000000000000000000'
handler: handleGulp
- event: LOG_JOIN(indexed address,indexed address,uint256) - event: LOG_JOIN(indexed address,indexed address,uint256)
handler: handleJoinPool handler: handleJoinPool
- event: LOG_EXIT(indexed address,indexed address,uint256) - event: LOG_EXIT(indexed address,indexed address,uint256)

View File

@ -136,6 +136,9 @@ templates:
- event: LOG_CALL(indexed bytes4,indexed address,bytes) - event: LOG_CALL(indexed bytes4,indexed address,bytes)
topic0: '0xe4e1e53800000000000000000000000000000000000000000000000000000000' topic0: '0xe4e1e53800000000000000000000000000000000000000000000000000000000'
handler: handleRebind handler: handleRebind
- event: LOG_CALL(indexed bytes4,indexed address,bytes)
topic0: '0x8c28cbe800000000000000000000000000000000000000000000000000000000'
handler: handleGulp
- event: LOG_JOIN(indexed address,indexed address,uint256) - event: LOG_JOIN(indexed address,indexed address,uint256)
handler: handleJoinPool handler: handleJoinPool
- event: LOG_EXIT(indexed address,indexed address,uint256) - event: LOG_EXIT(indexed address,indexed address,uint256)