debug pool history issue.

This commit is contained in:
ssallam 2020-12-08 17:57:09 +01:00
parent 827d51b3e1
commit b47cf07f18
3 changed files with 101 additions and 30 deletions

View File

@ -98,6 +98,9 @@ type PoolTransaction @entity {
gasUsed: BigDecimal! gasUsed: BigDecimal!
gasPrice: BigDecimal! gasPrice: BigDecimal!
oceanReserve: BigDecimal!
datatokenReserve: BigDecimal!
tokens: [PoolTransactionTokenValues!] @derivedFrom(field: "txId") tokens: [PoolTransactionTokenValues!] @derivedFrom(field: "txId")
} }

View File

@ -31,6 +31,7 @@ export let BONE = BigDecimal.fromString('1000000000000000000')
export let ENABLE_DEBUG = false export let ENABLE_DEBUG = false
export let DEBUG_POOL_ADDRESS = '0x64269ce81a07f21cb3566afa752810c4ca0bcb6f'
let network = dataSource.network() let network = dataSource.network()
@ -38,9 +39,7 @@ export let OCEAN: string = (network == 'mainnet')
? '0x967da4048cd07ab37855c090aaf366e4ce1b9f48' ? '0x967da4048cd07ab37855c090aaf366e4ce1b9f48'
: '0x8967BCF84170c91B0d24D4302C2376283b0B3a07' : '0x8967BCF84170c91B0d24D4302C2376283b0B3a07'
export function debuglog(message: string, event: ethereum.Event, args: Array<string>): void { export function _debuglog(message: string, event: ethereum.Event, args: Array<string>): void {
if (!ENABLE_DEBUG) return
if (event != null) { if (event != null) {
args.push(event.transaction.hash.toHex()) args.push(event.transaction.hash.toHex())
args.push(event.address.toHex()) args.push(event.address.toHex())
@ -48,7 +47,12 @@ export function debuglog(message: string, event: ethereum.Event, args: Array<str
for (let i=0; i < args.length; i++) { for (let i=0; i < args.length; i++) {
message = message.concat(' {}') message = message.concat(' {}')
} }
log.error('********** ' + message, args) log.error('@@@@@@@@@@@@@@@@@@@ ' + message, args)
}
export function debuglog(message: string, event: ethereum.Event, args: Array<string>): void {
if (!ENABLE_DEBUG) return
_debuglog(message, event, args)
} }
export function hexToDecimal(hexString: String, decimals: i32): BigDecimal { export function hexToDecimal(hexString: String, decimals: i32): BigDecimal {
@ -76,8 +80,10 @@ export function decimalToBigInt(value: BigDecimal): BigInt {
} }
export function updatePoolTokenBalance(poolToken: PoolToken, balance: BigDecimal, source: string): void { export function updatePoolTokenBalance(poolToken: PoolToken, balance: BigDecimal, source: string): void {
debuglog('updating poolToken balance (source, oldBalance, newBalance, poolId) ', null, if (poolToken.poolId == DEBUG_POOL_ADDRESS && poolToken.tokenAddress == OCEAN) {
_debuglog('########## updating poolToken balance (source, oldBalance, newBalance, poolId) ', null,
[source, poolToken.balance.toString(), balance.toString(), poolToken.poolId]) [source, poolToken.balance.toString(), balance.toString(), poolToken.poolId])
}
poolToken.balance = balance poolToken.balance = balance
} }
@ -132,6 +138,25 @@ export function updatePoolTransactionToken(
} }
ptxTokenValues.save() ptxTokenValues.save()
if (ptxTokenValues.tokenAddress == OCEAN) {
ptx.oceanReserve = ptxTokenValues.tokenReserve
} else {
ptx.datatokenReserve = ptxTokenValues.tokenReserve
}
if (poolToken.poolId == DEBUG_POOL_ADDRESS && poolToken.tokenAddress == OCEAN) {
_debuglog('########## updatePoolTransactionToken: ', null,
[
BigInt.fromI32(ptx.block).toString(),
BigInt.fromI32(ptx.timestamp).toString(),
ptxTokenValues.type,
ptxTokenValues.value.toString(),
ptxTokenValues.tokenReserve.toString(),
poolToken.poolId,
])
}
ptx.save()
} }
export function createPoolTransaction(event: ethereum.Event, event_type: string, userAddress: string): void { export function createPoolTransaction(event: ethereum.Event, event_type: string, userAddress: string): void {
@ -164,27 +189,35 @@ export function createPoolTransaction(event: ethereum.Event, event_type: string,
pool.datatokenReserve = dtToken.balance pool.datatokenReserve = dtToken.balance
pool.oceanReserve = ocnToken.balance pool.oceanReserve = ocnToken.balance
pool.spotPrice = poolTx.spotPrice // Initial reserve values, will be updated in `updatePoolTransactionToken`
poolTx.datatokenReserve = dtToken.balance
poolTx.oceanReserve = ocnToken.balance
let p = Pool.bind(Address.fromString(poolId)) let p = Pool.bind(Address.fromString(poolId))
let result = p.try_calcInGivenOut( let result = p.try_getSpotPrice(
decimalToBigInt(ocnToken.balance), decimalToBigInt(ocnToken.denormWeight), Address.fromString(ocnToken.tokenAddress),
decimalToBigInt(dtToken.balance), decimalToBigInt(dtToken.denormWeight), Address.fromString(dtToken.tokenAddress))
ONE_INT, decimalToBigInt(pool.swapFee) // pool.spotPrice = poolTx.spotPrice
)
pool.consumePrice = result.reverted ? BigDecimal.fromString('-1') : bigIntToDecimal(result.value, 18) pool.consumePrice = result.reverted ? BigDecimal.fromString('-1') : bigIntToDecimal(result.value, 18)
debuglog(
'args to calcInGivenOut (ocnBalance, ocnWeight, dtBalance, dtWeight, dtAmount, swapFee, result)', null, // let result = p.try_calcInGivenOut(
[ // decimalToBigInt(ocnToken.balance), decimalToBigInt(ocnToken.denormWeight),
decimalToBigInt(ocnToken.balance).toString(), // decimalToBigInt(dtToken.balance), decimalToBigInt(dtToken.denormWeight),
decimalToBigInt(ocnToken.denormWeight).toString(), // ONE_INT, decimalToBigInt(pool.swapFee)
decimalToBigInt(dtToken.balance).toString(), // )
decimalToBigInt(dtToken.denormWeight).toString(), // pool.consumePrice = BigDecimal.fromString('-1')//result.reverted ? BigDecimal.fromString('-1') : bigIntToDecimal(result.value, 18)
ONE_INT.toString(), // debuglog(
decimalToBigInt(pool.swapFee).toString(), // 'args to calcInGivenOut (ocnBalance, ocnWeight, dtBalance, dtWeight, dtAmount, swapFee, result)', null,
result.reverted ? 'failed' : result.value.toString() // [
] // decimalToBigInt(ocnToken.balance).toString(),
) // decimalToBigInt(ocnToken.denormWeight).toString(),
// decimalToBigInt(dtToken.balance).toString(),
// decimalToBigInt(dtToken.denormWeight).toString(),
// ONE_INT.toString(),
// decimalToBigInt(pool.swapFee).toString(),
// result.reverted ? 'failed' : result.value.toString()
// ]
// )
pool.save() pool.save()
debuglog('updated pool reserves (source, dtBalance, ocnBalance, dtReserve, ocnReserve): ', debuglog('updated pool reserves (source, dtBalance, ocnBalance, dtReserve, ocnReserve): ',
@ -199,6 +232,16 @@ export function createPoolTransaction(event: ethereum.Event, event_type: string,
poolTx.gasUsed = event.transaction.gasUsed.toBigDecimal() poolTx.gasUsed = event.transaction.gasUsed.toBigDecimal()
poolTx.gasPrice = event.transaction.gasPrice.toBigDecimal() poolTx.gasPrice = event.transaction.gasPrice.toBigDecimal()
if (poolId == DEBUG_POOL_ADDRESS) {
_debuglog('####################### poolTransaction: ', event,
[
BigInt.fromI32(poolTx.block).toString(),
BigInt.fromI32(poolTx.timestamp).toString(),
pool.oceanReserve.toString()
])
}
poolTx.save() poolTx.save()
} }

View File

@ -21,7 +21,7 @@ import {
updatePoolTransactionToken, updatePoolTransactionToken,
createPoolTransaction, createPoolTransaction,
OCEAN, OCEAN,
debuglog, updatePoolTokenBalance debuglog, updatePoolTokenBalance, _debuglog, DEBUG_POOL_ADDRESS
} from './helpers' } from './helpers'
/************************************ /************************************
@ -67,8 +67,9 @@ export function handleFinalize(event: LOG_CALL): void {
export function handleSetup(event: LOG_CALL): void { export function handleSetup(event: LOG_CALL): void {
let poolId = event.address.toHex() let poolId = event.address.toHex()
debuglog('handleSetup: ', event, []) if (poolId == DEBUG_POOL_ADDRESS) {
_debuglog('handleSetup: ', event, [])
}
let data = event.params.data.toHexString() let data = event.params.data.toHexString()
// First 2 chars are 0x // First 2 chars are 0x
// Next there is 8 chars // Next there is 8 chars
@ -180,6 +181,9 @@ export function handleJoinPool(event: LOG_JOIN): void {
let ptx = event.transaction.hash.toHexString() let ptx = event.transaction.hash.toHexString()
let poolTx = PoolTransaction.load(ptx) let poolTx = PoolTransaction.load(ptx)
if (poolTx != null) { if (poolTx != null) {
if (poolId == DEBUG_POOL_ADDRESS && event.params.tokenIn.toHex() == OCEAN) {
_debuglog('!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!! JOIN JOIN JOIN !!!!!!!!!!!! PoolTransaction EXISTS: ', event, [])
}
return return
} }
@ -187,6 +191,9 @@ export function handleJoinPool(event: LOG_JOIN): void {
let poolTokenId = poolId.concat('-').concat(address) let poolTokenId = poolId.concat('-').concat(address)
let poolToken = PoolToken.load(poolTokenId) let poolToken = PoolToken.load(poolTokenId)
if (poolToken == null) { if (poolToken == null) {
if (poolToken.poolId == DEBUG_POOL_ADDRESS && poolToken.tokenAddress == OCEAN) {
_debuglog('!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!! JOIN JOIN JOIN !!!!!!!!!!!! NO PoolToken: ', event, [address, poolTokenId])
}
return return
} }
@ -195,6 +202,10 @@ export function handleJoinPool(event: LOG_JOIN): void {
let decimals = datatoken == null ? BigInt.fromI32(18).toI32() : datatoken.decimals let decimals = datatoken == null ? BigInt.fromI32(18).toI32() : datatoken.decimals
let tokenAmountIn = tokenToDecimal(event.params.tokenAmountIn.toBigDecimal(), decimals) let tokenAmountIn = tokenToDecimal(event.params.tokenAmountIn.toBigDecimal(), decimals)
updatePoolTokenBalance(poolToken as PoolToken, poolToken.balance.plus(tokenAmountIn), 'handleJoinPool') updatePoolTokenBalance(poolToken as PoolToken, poolToken.balance.plus(tokenAmountIn), 'handleJoinPool')
if (poolToken.poolId == DEBUG_POOL_ADDRESS && poolToken.tokenAddress == OCEAN) {
_debuglog('!!!!!!!!!!!!!!!!!! JOIN JOIN JOIN : (token, amountIn, amountIn) ', event,
[address, tokenAmountIn.toString(), event.params.tokenAmountIn.toString()])
}
poolToken.save() poolToken.save()
createPoolTransaction(event, 'join', event.params.caller.toHexString()) createPoolTransaction(event, 'join', event.params.caller.toHexString())
@ -212,6 +223,9 @@ export function handleExitPool(event: LOG_EXIT): void {
let poolTokenId = poolId.concat('-').concat(address.toString()) let poolTokenId = poolId.concat('-').concat(address.toString())
let poolToken = PoolToken.load(poolTokenId) let poolToken = PoolToken.load(poolTokenId)
if (!poolToken) { if (!poolToken) {
if (poolToken.poolId == DEBUG_POOL_ADDRESS && poolToken.tokenAddress == OCEAN) {
_debuglog('!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!! EXIT EXIT EXIT !!!!!!!!!!!! NO PoolToken: ', event, [address, poolTokenId])
}
return return
} }
@ -222,7 +236,10 @@ export function handleExitPool(event: LOG_EXIT): void {
let newAmount = poolToken.balance.minus(tokenAmountOut) let newAmount = poolToken.balance.minus(tokenAmountOut)
updatePoolTokenBalance(poolToken as PoolToken, newAmount, 'handleExitPool') updatePoolTokenBalance(poolToken as PoolToken, newAmount, 'handleExitPool')
poolToken.save() poolToken.save()
if (poolToken.poolId == DEBUG_POOL_ADDRESS && poolToken.tokenAddress == OCEAN) {
_debuglog('!!!!!!!!!!!!!!!!!! EXIT EXIT EXIT : (token, amountOut, amountOut)', event,
[address, tokenAmountOut.toString(), event.params.tokenAmountOut.toString()])
}
let pool = Pool.load(poolId) let pool = Pool.load(poolId)
pool.exitCount = pool.exitCount.plus(BigInt.fromI32(1)) pool.exitCount = pool.exitCount.plus(BigInt.fromI32(1))
if (newAmount.equals(ZERO_BD)) { if (newAmount.equals(ZERO_BD)) {
@ -251,6 +268,10 @@ export function handleSwap(event: LOG_SWAP): void {
let poolTokenInId = poolId.concat('-').concat(tokenIn.toString()) let poolTokenInId = poolId.concat('-').concat(tokenIn.toString())
let poolTokenIn = PoolToken.load(poolTokenInId) let poolTokenIn = PoolToken.load(poolTokenInId)
if (!poolTokenIn) { if (!poolTokenIn) {
if (poolId == DEBUG_POOL_ADDRESS) {
_debuglog('!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!! SWAP SWAP SWAP !!!!!!!!!!!! NO PoolToken: ', event,
[tokenIn, poolTokenInId])
}
return return
} }
let dtIn = Datatoken.load(tokenIn) let dtIn = Datatoken.load(tokenIn)
@ -267,7 +288,11 @@ export function handleSwap(event: LOG_SWAP): void {
let newAmountOut = poolTokenOut.balance.minus(tokenAmountOut) let newAmountOut = poolTokenOut.balance.minus(tokenAmountOut)
updatePoolTokenBalance(poolTokenOut as PoolToken, newAmountOut, 'handleSwap.tokenOut') updatePoolTokenBalance(poolTokenOut as PoolToken, newAmountOut, 'handleSwap.tokenOut')
poolTokenOut.save() poolTokenOut.save()
if (poolId == DEBUG_POOL_ADDRESS) {
_debuglog('!!!!!!!!!!!!!!!!!! SWAP SWAP SWAP : (tokenIn, tokenOut, amountIn, amountIn, amountOut, amountOut)', event,
[tokenIn, tokenOut, tokenAmountIn.toString(), event.params.tokenAmountIn.toString(),
tokenAmountOut.toString(), event.params.tokenAmountOut.toString()])
}
let pool = Pool.load(poolId) let pool = Pool.load(poolId)
pool.swapCount += BigInt.fromI32(1) pool.swapCount += BigInt.fromI32(1)