update token reserve at the pool level, fix issues.

This commit is contained in:
ssallam 2020-12-03 10:05:34 +01:00
parent 0b53e70fc2
commit bc33ba658e
3 changed files with 41 additions and 9 deletions

View File

@ -91,7 +91,6 @@ type PoolTransaction @entity {
sharesBalance: BigDecimal! sharesBalance: BigDecimal!
spotPrice: BigDecimal! spotPrice: BigDecimal!
consumePrice: BigDecimal!
tx: Bytes! tx: Bytes!
event: String event: String
block: Int! block: Int!

View File

@ -23,12 +23,27 @@ export let ZERO_BD = BigDecimal.fromString('0.0')
export let MINUS_1 = BigDecimal.fromString('-1.0') export let MINUS_1 = BigDecimal.fromString('-1.0')
export let ONE = BigDecimal.fromString('1.0') export let ONE = BigDecimal.fromString('1.0')
export let ENABLE_DEBUG = false
let network = dataSource.network() let network = dataSource.network()
export let OCEAN: string = (network == 'mainnet') export let OCEAN: string = (network == 'mainnet')
? '0x967da4048cd07ab37855c090aaf366e4ce1b9f48' ? '0x967da4048cd07ab37855c090aaf366e4ce1b9f48'
: '0x8967BCF84170c91B0d24D4302C2376283b0B3a07' : '0x8967BCF84170c91B0d24D4302C2376283b0B3a07'
export function debuglog(message: string, event: ethereum.Event, args: Array<string>): void {
if (!ENABLE_DEBUG) return
if (event != null) {
args.push(event.transaction.hash.toHex())
args.push(event.address.toHex())
}
for (let i=0; i < args.length; i++) {
message = message.concat(' {}')
}
log.info(message, args)
}
export function hexToDecimal(hexString: String, decimals: i32): BigDecimal { export function hexToDecimal(hexString: String, decimals: i32): BigDecimal {
let bytes = Bytes.fromHexString(hexString.toString()).reverse() as Bytes let bytes = Bytes.fromHexString(hexString.toString()).reverse() as Bytes
let bi = BigInt.fromUnsignedBytes(bytes) let bi = BigInt.fromUnsignedBytes(bytes)
@ -46,6 +61,12 @@ export function tokenToDecimal(amount: BigDecimal, decimals: i32): BigDecimal {
return amount.div(scale) return amount.div(scale)
} }
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])
poolToken.balance = balance
}
export function createPoolShareEntity(id: string, pool: string, user: string): void { export function createPoolShareEntity(id: string, pool: string, user: string): void {
let poolShare = new PoolShare(id) let poolShare = new PoolShare(id)
@ -129,7 +150,16 @@ export function createPoolTransaction(event: ethereum.Event, event_type: string,
// poolTx.consumePrice = calcInGivenOut( // poolTx.consumePrice = calcInGivenOut(
// ocnToken.denormWeight, dtToken.denormWeight, ocnToken.balance, dtToken.balance, // ocnToken.denormWeight, dtToken.denormWeight, ocnToken.balance, dtToken.balance,
// ONE, pool.swapFee) // ONE, pool.swapFee)
poolTx.consumePrice = ZERO_BD
pool.datatokenReserve = dtToken.balance
pool.oceanReserve = ocnToken.balance
pool.spotPrice = poolTx.spotPrice
pool.save()
debuglog('updated pool reserves (source, dtBalance, ocnBalance, dtReserve, ocnReserve): ',
event,
['createPoolTransaction', dtToken.balance.toString(), ocnToken.balance.toString(),
pool.datatokenReserve.toString(), pool.oceanReserve.toString()])
// pool.consumePrice = ZERO_BD
poolTx.tx = event.transaction.hash poolTx.tx = event.transaction.hash
poolTx.event = event_type poolTx.event = event_type

View File

@ -19,7 +19,9 @@ import {
MINUS_1, MINUS_1,
decrPoolCount, decrPoolCount,
updatePoolTransactionToken, updatePoolTransactionToken,
createPoolTransaction, OCEAN, createPoolTransaction,
OCEAN,
debuglog, updatePoolTokenBalance
} from './helpers' } from './helpers'
/************************************ /************************************
@ -65,6 +67,7 @@ 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, [])
let data = event.params.data.toHexString() let data = event.params.data.toHexString()
// First 2 chars are 0x // First 2 chars are 0x
@ -138,7 +141,7 @@ export function _handleRebind(event: LOG_CALL, poolId: string, tokenAddress: str
} }
let balance = hexToDecimal(balanceStr, decimals) let balance = hexToDecimal(balanceStr, decimals)
poolToken.balance = balance updatePoolTokenBalance(poolToken as PoolToken, balance, '_handleRebind')
poolToken.denormWeight = denormWeight poolToken.denormWeight = denormWeight
poolToken.save() poolToken.save()
@ -150,7 +153,6 @@ export function _handleRebind(event: LOG_CALL, poolId: string, tokenAddress: str
} }
export function handleRebind(event: LOG_CALL): void { export function handleRebind(event: LOG_CALL): void {
return
let poolId = event.address.toHex() let poolId = event.address.toHex()
_handleRebind( _handleRebind(
event, event,
@ -192,7 +194,8 @@ export function handleJoinPool(event: LOG_JOIN): void {
datatoken = poolToken.tokenId != null ? Datatoken.load(poolToken.tokenId) : null datatoken = poolToken.tokenId != null ? Datatoken.load(poolToken.tokenId) : null
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)
poolToken.balance = poolToken.balance.plus(tokenAmountIn) updatePoolTokenBalance(poolToken as PoolToken, poolToken.balance.plus(tokenAmountIn), 'handleJoinPool')
poolToken.save() poolToken.save()
createPoolTransaction(event, 'join', event.params.caller.toHexString()) createPoolTransaction(event, 'join', event.params.caller.toHexString())
updatePoolTransactionToken( updatePoolTransactionToken(
@ -217,7 +220,7 @@ export function handleExitPool(event: LOG_EXIT): void {
let decimals = datatoken == null ? BigInt.fromI32(18).toI32() : datatoken.decimals let decimals = datatoken == null ? BigInt.fromI32(18).toI32() : datatoken.decimals
let tokenAmountOut = tokenToDecimal(event.params.tokenAmountOut.toBigDecimal(), decimals) let tokenAmountOut = tokenToDecimal(event.params.tokenAmountOut.toBigDecimal(), decimals)
let newAmount = poolToken.balance.minus(tokenAmountOut) let newAmount = poolToken.balance.minus(tokenAmountOut)
poolToken.balance = newAmount updatePoolTokenBalance(poolToken as PoolToken, newAmount, 'handleExitPool')
poolToken.save() poolToken.save()
let pool = Pool.load(poolId) let pool = Pool.load(poolId)
@ -253,7 +256,7 @@ export function handleSwap(event: LOG_SWAP): void {
let dtIn = Datatoken.load(tokenIn) let dtIn = Datatoken.load(tokenIn)
let tokenAmountIn = tokenToDecimal(event.params.tokenAmountIn.toBigDecimal(), (dtIn == null) ? 18 : dtIn.decimals) let tokenAmountIn = tokenToDecimal(event.params.tokenAmountIn.toBigDecimal(), (dtIn == null) ? 18 : dtIn.decimals)
let newAmountIn = poolTokenIn.balance.plus(tokenAmountIn) let newAmountIn = poolTokenIn.balance.plus(tokenAmountIn)
poolTokenIn.balance = newAmountIn updatePoolTokenBalance(poolTokenIn as PoolToken, newAmountIn, 'handleSwap.tokenIn')
poolTokenIn.save() poolTokenIn.save()
let tokenOut = event.params.tokenOut.toHex() let tokenOut = event.params.tokenOut.toHex()
@ -262,7 +265,7 @@ export function handleSwap(event: LOG_SWAP): void {
let dtOut = Datatoken.load(tokenOut) let dtOut = Datatoken.load(tokenOut)
let tokenAmountOut = tokenToDecimal(event.params.tokenAmountOut.toBigDecimal(), (dtOut == null) ? 18 : dtOut.decimals) let tokenAmountOut = tokenToDecimal(event.params.tokenAmountOut.toBigDecimal(), (dtOut == null) ? 18 : dtOut.decimals)
let newAmountOut = poolTokenOut.balance.minus(tokenAmountOut) let newAmountOut = poolTokenOut.balance.minus(tokenAmountOut)
poolTokenOut.balance = newAmountOut updatePoolTokenBalance(poolTokenOut as PoolToken, newAmountOut, 'handleSwap.tokenOut')
poolTokenOut.save() poolTokenOut.save()
let pool = Pool.load(poolId) let pool = Pool.load(poolId)