update schema, fix bugs, prevent processing tx multiple times.

This commit is contained in:
ssallam 2020-12-02 17:20:29 +01:00
parent 9a74178a62
commit 0b53e70fc2
3 changed files with 54 additions and 10 deletions

View File

@ -47,6 +47,7 @@ type Pool @entity {
tokens: [PoolToken!] @derivedFrom(field: "poolId")
shares: [PoolShare!] @derivedFrom(field: "poolId")
transactions: [PoolTransaction!] @derivedFrom(field: "poolAddress")
transactionsTokenValues: [PoolTransactionTokenValues!] @derivedFrom(field: "poolAddress")
}
type PoolToken @entity {
@ -69,6 +70,10 @@ type PoolTransactionTokenValues @entity {
id: ID! # pool tx + tokenAddress
txId: PoolTransaction!
poolToken: PoolToken!
poolAddress: Pool!
userAddress: User!
tokenAddress: String!
value: BigDecimal!
tokenReserve: BigDecimal!
feeValue: BigDecimal! # Swap fee value in OCEAN
@ -79,6 +84,8 @@ type PoolTransaction @entity {
id: ID! # pool tx
poolAddress: Pool
userAddress: User # User address that initiates the swap
poolAddressStr: String!
userAddressStr: String!
sharesTransferAmount: BigDecimal! #
sharesBalance: BigDecimal!
@ -171,6 +178,7 @@ type User @entity {
tokenBalancesOwned: [TokenBalance!] @derivedFrom(field: "userAddress")
tokensOwned: [Datatoken!] @derivedFrom(field: "minter")
poolTransactions: [PoolTransaction!] @derivedFrom(field: "userAddress")
poolTransactionsTokenValues: [PoolTransactionTokenValues!] @derivedFrom(field: "userAddress")
tokenTransactions: [TokenTransaction!] @derivedFrom(field: "userAddress")
orders: [TokenOrder!] @derivedFrom(field: "payer")
}

View File

@ -26,7 +26,7 @@ export let ONE = BigDecimal.fromString('1.0')
let network = dataSource.network()
export let OCEAN: string = (network == 'mainnet')
? '0x967da4048cD07aB37855c090aAF366e4ce1b9F48'
? '0x967da4048cd07ab37855c090aaf366e4ce1b9f48'
: '0x8967BCF84170c91B0d24D4302C2376283b0B3a07'
export function hexToDecimal(hexString: String, decimals: i32): BigDecimal {
@ -74,6 +74,7 @@ export function updatePoolTransactionToken(
balance: BigDecimal, feeValue: BigDecimal
): void {
let ptx = PoolTransaction.load(poolTx)
let poolToken = PoolToken.load(poolTokenId)
let ptxTokenValuesId = poolTx.concat('-').concat(poolToken.tokenAddress)
let ptxTokenValues = PoolTransactionTokenValues.load(ptxTokenValuesId)
@ -82,8 +83,12 @@ export function updatePoolTransactionToken(
}
ptxTokenValues.txId = poolTx
ptxTokenValues.poolToken = poolTokenId
ptxTokenValues.poolAddress = poolToken.poolId
ptxTokenValues.userAddress = ptx.userAddress
ptxTokenValues.tokenAddress = PoolToken.load(poolTokenId).tokenAddress
ptxTokenValues.value = amount
ptxTokenValues.tokenReserve = balance.plus(amount)
ptxTokenValues.tokenReserve = balance
ptxTokenValues.feeValue = feeValue
if (amount.lt(ZERO_BD)) {
ptxTokenValues.type = 'out'
@ -97,23 +102,28 @@ export function updatePoolTransactionToken(
export function createPoolTransaction(event: ethereum.Event, event_type: string, userAddress: string): void {
let poolId = event.address.toHex()
let pool = Pool.load(poolId)
let ptx = event.transaction.hash.toHexString()
let ocnToken = PoolToken.load(poolId.concat('-').concat(OCEAN))
let dtToken = PoolToken.load(poolId.concat('-').concat(pool.datatokenAddress))
if (ocnToken == null || dtToken == null) return
if (ocnToken == null || dtToken == null) {
return
}
let poolTx = PoolTransaction.load(ptx)
if (poolTx == null) {
poolTx = new PoolTransaction(ptx)
if (poolTx != null) {
return
}
poolTx = new PoolTransaction(ptx)
poolTx.poolAddress = poolId
poolTx.userAddress = userAddress
poolTx.poolAddressStr = poolId
poolTx.userAddressStr = userAddress
poolTx.sharesTransferAmount = ZERO_BD
poolTx.sharesBalance = ZERO_BD
// poolTx.spotPrice = ZERO_BD
poolTx.spotPrice = calcSpotPrice(
ocnToken.denormWeight, dtToken.denormWeight, ocnToken.balance, dtToken.balance, pool.swapFee)
// poolTx.consumePrice = calcInGivenOut(

View File

@ -84,11 +84,32 @@ export function handleSetup(event: LOG_CALL): void {
let baseTokenWeight = data.slice(330,394) // (74+(4*64),74+(5*64))
let swapFee = data.slice(394) // (74+(5*64), END)
_handleRebind(event, poolId, dataTokenAddress, dataTokenAmount, dataTokenWeight)
let poolTokenId = poolId.concat('-').concat(baseTokenAddress)
let poolToken = PoolToken.load(poolTokenId)
if (poolToken != null) return
_handleRebind(event, poolId, dataTokenAddress, dataTokenAmount, dataTokenWeight)
_handleRebind(event, poolId, baseTokenAddress, baseTokenAmount, baseTokenWeight)
handleSetSwapFee(event, swapFee)
handleFinalize(event)
createPoolTransaction(event, 'setup', event.transaction.from.toHex())
let pool = Pool.load(poolId)
// update base token
let amount = hexToDecimal(baseTokenAmount, 18)
updatePoolTransactionToken(
event.transaction.hash.toHexString(), poolTokenId,
amount, PoolToken.load(poolTokenId).balance,
ZERO_BD
)
// update the datatoken
amount = hexToDecimal(dataTokenAmount, 18)
updatePoolTransactionToken(
event.transaction.hash.toHexString(), poolId.concat('-').concat(dataTokenAddress),
amount, PoolToken.load(poolId.concat('-').concat(dataTokenAddress)).balance,
ZERO_BD
)
}
export function _handleRebind(event: LOG_CALL, poolId: string, tokenAddress: string, balanceStr: string, denormWeightStr: string): void {
@ -98,7 +119,7 @@ export function _handleRebind(event: LOG_CALL, poolId: string, tokenAddress: str
if (tokenAddress != OCEAN ) {
pool.datatokenAddress = tokenAddress
}
pool.tokenCount = BigInt.fromI32(2)
pool.tokenCount += BigInt.fromI32(1)
let address = Address.fromString(tokenAddress)
let denormWeight = hexToDecimal(denormWeightStr, decimals)
let poolTokenId = poolId.concat('-').concat(address.toHexString())
@ -129,6 +150,7 @@ export function _handleRebind(event: LOG_CALL, poolId: string, tokenAddress: str
}
export function handleRebind(event: LOG_CALL): void {
return
let poolId = event.address.toHex()
_handleRebind(
event,
@ -153,6 +175,11 @@ export function handleJoinPool(event: LOG_JOIN): void {
pool.joinCount = pool.joinCount.plus(BigInt.fromI32(1))
pool.save()
let ptx = event.transaction.hash.toHexString()
let poolTx = PoolTransaction.load(ptx)
if (poolTx != null) {
return
}
let address = event.params.tokenIn.toHex()
let poolTokenId = poolId.concat('-').concat(address)
@ -167,7 +194,6 @@ export function handleJoinPool(event: LOG_JOIN): void {
let tokenAmountIn = tokenToDecimal(event.params.tokenAmountIn.toBigDecimal(), decimals)
poolToken.balance = poolToken.balance.plus(tokenAmountIn)
poolToken.save()
createPoolTransaction(event, 'join', event.params.caller.toHexString())
updatePoolTransactionToken(
event.transaction.hash.toHexString(), poolTokenId,