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!
gasPrice: BigDecimal!
oceanReserve: BigDecimal!
datatokenReserve: BigDecimal!
tokens: [PoolTransactionTokenValues!] @derivedFrom(field: "txId")
}

View File

@ -31,6 +31,7 @@ export let BONE = BigDecimal.fromString('1000000000000000000')
export let ENABLE_DEBUG = false
export let DEBUG_POOL_ADDRESS = '0x64269ce81a07f21cb3566afa752810c4ca0bcb6f'
let network = dataSource.network()
@ -38,9 +39,7 @@ export let OCEAN: string = (network == 'mainnet')
? '0x967da4048cd07ab37855c090aaf366e4ce1b9f48'
: '0x8967BCF84170c91B0d24D4302C2376283b0B3a07'
export function debuglog(message: string, event: ethereum.Event, args: Array<string>): void {
if (!ENABLE_DEBUG) return
export function _debuglog(message: string, event: ethereum.Event, args: Array<string>): void {
if (event != null) {
args.push(event.transaction.hash.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++) {
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 {
@ -76,8 +80,10 @@ export function decimalToBigInt(value: BigDecimal): BigInt {
}
export function updatePoolTokenBalance(poolToken: PoolToken, balance: BigDecimal, source: string): void {
debuglog('updating poolToken balance (source, oldBalance, newBalance, poolId) ', null,
[source, poolToken.balance.toString(), balance.toString(), poolToken.poolId])
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])
}
poolToken.balance = balance
}
@ -132,6 +138,25 @@ export function updatePoolTransactionToken(
}
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 {
@ -164,27 +189,35 @@ export function createPoolTransaction(event: ethereum.Event, event_type: string,
pool.datatokenReserve = dtToken.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 result = p.try_calcInGivenOut(
decimalToBigInt(ocnToken.balance), decimalToBigInt(ocnToken.denormWeight),
decimalToBigInt(dtToken.balance), decimalToBigInt(dtToken.denormWeight),
ONE_INT, decimalToBigInt(pool.swapFee)
)
let result = p.try_getSpotPrice(
Address.fromString(ocnToken.tokenAddress),
Address.fromString(dtToken.tokenAddress))
// pool.spotPrice = poolTx.spotPrice
pool.consumePrice = result.reverted ? BigDecimal.fromString('-1') : bigIntToDecimal(result.value, 18)
debuglog(
'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_INT.toString(),
decimalToBigInt(pool.swapFee).toString(),
result.reverted ? 'failed' : result.value.toString()
]
)
// let result = p.try_calcInGivenOut(
// decimalToBigInt(ocnToken.balance), decimalToBigInt(ocnToken.denormWeight),
// decimalToBigInt(dtToken.balance), decimalToBigInt(dtToken.denormWeight),
// ONE_INT, decimalToBigInt(pool.swapFee)
// )
// pool.consumePrice = BigDecimal.fromString('-1')//result.reverted ? BigDecimal.fromString('-1') : bigIntToDecimal(result.value, 18)
// debuglog(
// '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_INT.toString(),
// decimalToBigInt(pool.swapFee).toString(),
// result.reverted ? 'failed' : result.value.toString()
// ]
// )
pool.save()
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.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()
}

View File

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