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

View File

@ -26,7 +26,7 @@ export let ONE = BigDecimal.fromString('1.0')
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 hexToDecimal(hexString: String, decimals: i32): BigDecimal { export function hexToDecimal(hexString: String, decimals: i32): BigDecimal {
@ -74,6 +74,7 @@ export function updatePoolTransactionToken(
balance: BigDecimal, feeValue: BigDecimal balance: BigDecimal, feeValue: BigDecimal
): void { ): void {
let ptx = PoolTransaction.load(poolTx)
let poolToken = PoolToken.load(poolTokenId) let poolToken = PoolToken.load(poolTokenId)
let ptxTokenValuesId = poolTx.concat('-').concat(poolToken.tokenAddress) let ptxTokenValuesId = poolTx.concat('-').concat(poolToken.tokenAddress)
let ptxTokenValues = PoolTransactionTokenValues.load(ptxTokenValuesId) let ptxTokenValues = PoolTransactionTokenValues.load(ptxTokenValuesId)
@ -82,8 +83,12 @@ export function updatePoolTransactionToken(
} }
ptxTokenValues.txId = poolTx ptxTokenValues.txId = poolTx
ptxTokenValues.poolToken = poolTokenId ptxTokenValues.poolToken = poolTokenId
ptxTokenValues.poolAddress = poolToken.poolId
ptxTokenValues.userAddress = ptx.userAddress
ptxTokenValues.tokenAddress = PoolToken.load(poolTokenId).tokenAddress
ptxTokenValues.value = amount ptxTokenValues.value = amount
ptxTokenValues.tokenReserve = balance.plus(amount) ptxTokenValues.tokenReserve = balance
ptxTokenValues.feeValue = feeValue ptxTokenValues.feeValue = feeValue
if (amount.lt(ZERO_BD)) { if (amount.lt(ZERO_BD)) {
ptxTokenValues.type = 'out' ptxTokenValues.type = 'out'
@ -97,23 +102,28 @@ export function updatePoolTransactionToken(
export function createPoolTransaction(event: ethereum.Event, event_type: string, userAddress: string): void { export function createPoolTransaction(event: ethereum.Event, event_type: string, userAddress: string): void {
let poolId = event.address.toHex() let poolId = event.address.toHex()
let pool = Pool.load(poolId) let pool = Pool.load(poolId)
let ptx = event.transaction.hash.toHexString() let ptx = event.transaction.hash.toHexString()
let ocnToken = PoolToken.load(poolId.concat('-').concat(OCEAN)) let ocnToken = PoolToken.load(poolId.concat('-').concat(OCEAN))
let dtToken = PoolToken.load(poolId.concat('-').concat(pool.datatokenAddress)) 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) let poolTx = PoolTransaction.load(ptx)
if (poolTx == null) { if (poolTx != null) {
poolTx = new PoolTransaction(ptx) return
} }
poolTx = new PoolTransaction(ptx)
poolTx.poolAddress = poolId poolTx.poolAddress = poolId
poolTx.userAddress = userAddress poolTx.userAddress = userAddress
poolTx.poolAddressStr = poolId
poolTx.userAddressStr = userAddress
poolTx.sharesTransferAmount = ZERO_BD poolTx.sharesTransferAmount = ZERO_BD
poolTx.sharesBalance = ZERO_BD poolTx.sharesBalance = ZERO_BD
// poolTx.spotPrice = ZERO_BD
poolTx.spotPrice = calcSpotPrice( poolTx.spotPrice = calcSpotPrice(
ocnToken.denormWeight, dtToken.denormWeight, ocnToken.balance, dtToken.balance, pool.swapFee) ocnToken.denormWeight, dtToken.denormWeight, ocnToken.balance, dtToken.balance, pool.swapFee)
// poolTx.consumePrice = calcInGivenOut( // 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 baseTokenWeight = data.slice(330,394) // (74+(4*64),74+(5*64))
let swapFee = data.slice(394) // (74+(5*64), END) 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) _handleRebind(event, poolId, baseTokenAddress, baseTokenAmount, baseTokenWeight)
handleSetSwapFee(event, swapFee) handleSetSwapFee(event, swapFee)
handleFinalize(event) 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 { 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 ) { if (tokenAddress != OCEAN ) {
pool.datatokenAddress = tokenAddress pool.datatokenAddress = tokenAddress
} }
pool.tokenCount = BigInt.fromI32(2) pool.tokenCount += BigInt.fromI32(1)
let address = Address.fromString(tokenAddress) let address = Address.fromString(tokenAddress)
let denormWeight = hexToDecimal(denormWeightStr, decimals) let denormWeight = hexToDecimal(denormWeightStr, decimals)
let poolTokenId = poolId.concat('-').concat(address.toHexString()) 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 { export function handleRebind(event: LOG_CALL): void {
return
let poolId = event.address.toHex() let poolId = event.address.toHex()
_handleRebind( _handleRebind(
event, event,
@ -153,6 +175,11 @@ export function handleJoinPool(event: LOG_JOIN): void {
pool.joinCount = pool.joinCount.plus(BigInt.fromI32(1)) pool.joinCount = pool.joinCount.plus(BigInt.fromI32(1))
pool.save() pool.save()
let ptx = event.transaction.hash.toHexString()
let poolTx = PoolTransaction.load(ptx)
if (poolTx != null) {
return
}
let address = event.params.tokenIn.toHex() let address = event.params.tokenIn.toHex()
let poolTokenId = poolId.concat('-').concat(address) 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) let tokenAmountIn = tokenToDecimal(event.params.tokenAmountIn.toBigDecimal(), decimals)
poolToken.balance = poolToken.balance.plus(tokenAmountIn) poolToken.balance = poolToken.balance.plus(tokenAmountIn)
poolToken.save() poolToken.save()
createPoolTransaction(event, 'join', event.params.caller.toHexString()) createPoolTransaction(event, 'join', event.params.caller.toHexString())
updatePoolTransactionToken( updatePoolTransactionToken(
event.transaction.hash.toHexString(), poolTokenId, event.transaction.hash.toHexString(), poolTokenId,