fix errors, add order handler.

This commit is contained in:
ssallam 2020-11-26 12:10:45 +01:00
parent 316dbeb9ad
commit 6470cb1c2c
5 changed files with 123 additions and 48 deletions

View File

@ -128,11 +128,26 @@ type Datatoken @entity {
minter: User!
publisher: User!
balances: [TokenBalance!] @derivedFrom(field: "datatokenId")
orders: [TokenOrder!] @derivedFrom(field: "datatokenId")
createTime: Int! # Block time datatoken was created
holdersCount: BigInt! # Number of addresses holding a balance of datatoken
orderCount: BigInt! # Number of orders executed for this dataset
tx: Bytes # Datatoken creation transaction id
}
type TokenOrder @entity {
id: ID! # datatokenId + userAddress + tx
datatokenId: Datatoken!
consumer: User!
payer: User!
amount: BigDecimal!
serviceId: Int!
timestamp: Int!
marketFeeCollector: User
marketFee: BigDecimal!
tx: Bytes
}
type TokenBalance @entity {
id: ID! # datatokenId + userAddress
userAddress: User!

View File

@ -6,7 +6,7 @@ import { log } from '@graphprotocol/graph-ts'
import {
OceanDatatokens,
Datatoken, PoolShare, Pool, User, TokenBalance
Datatoken, PoolShare, Pool, User, TokenBalance, TokenOrder
} from '../types/schema'
import {
hexToDecimal,
@ -14,7 +14,7 @@ import {
tokenToDecimal,
createTokenBalanceEntity,
updateDatatokenBalance,
saveTransaction,
saveTokenTransaction,
ZERO_BD, createPoolShareEntity, createUserEntity
} from './helpers'
@ -37,8 +37,8 @@ export function handleTransfer(event: Transfer): void {
let tokenBalanceToId = tokenId.concat('-').concat(event.params.to.toHex())
let tokenBalanceFrom = TokenBalance.load(tokenBalanceFromId)
let tokenBalanceTo = TokenBalance.load(tokenBalanceToId)
let oldBalanceFrom = 0
let oldBalanceTo = 0
let oldBalanceFrom = BigDecimal.fromString('0.0')
let oldBalanceTo = BigDecimal.fromString('0.0')
let datatoken = Datatoken.load(tokenId)
@ -95,5 +95,34 @@ export function handleTransfer(event: Transfer): void {
}
export function handleOrderStarted(event: OrderStarted): void {
let ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
let tokenId = event.address.toHex()
let datatoken = Datatoken.load(tokenId)
if (datatoken == null) return
let payer = event.params.payer.toHex()
// let feeCollector = event.params.mrktFeeCollector
// let marketFee = event.params.marketFee
let tx = event.transaction.hash
let orderId = tokenId.concat('-').concat(payer).concat('-').concat(tx.toHexString())
let order = TokenOrder.load(orderId)
if (order == null) {
order = new TokenOrder(orderId)
}
order.datatokenId = tokenId
order.amount = tokenToDecimal(event.params.amount.toBigDecimal(), 18)
order.consumer = event.params.consumer.toHex()
order.payer = payer
order.serviceId = event.params.serviceId.toI32()
order.timestamp = event.params.timestamp.toI32()
if (event.params.mrktFeeCollector != null && event.params.mrktFeeCollector.toHex() != ZERO_ADDRESS) {
order.marketFeeCollector = event.params.mrktFeeCollector.toHexString()
}
order.marketFee = tokenToDecimal(event.params.marketFee.toBigDecimal(), 18)
order.tx = tx
order.save()
datatoken.orderCount = datatoken.orderCount + BigInt.fromI32(1)
datatoken.save()
}

View File

@ -3,6 +3,7 @@ import { TokenRegistered } from '../types/DTFactory/DTFactory'
import { OceanDatatokens, Datatoken } from '../types/schema'
import { Datatoken as DatatokenContract } from '../types/templates'
import {
tokenToDecimal,
ZERO_BD,
} from './helpers'
import { log } from '@graphprotocol/graph-ts'
@ -19,12 +20,19 @@ export function handleNewToken(event: TokenRegistered): void {
let datatoken = new Datatoken(event.params.tokenAddress.toHexString())
log.error('************************ handleNewToken: datatokenId {}', [datatoken.id.toString()])
datatoken.minter = event.params.registeredBy
datatoken.publisher = event.params.registeredBy
datatoken.factoryID = event.address.toHexString()
datatoken.symbol = event.params.tokenSymbol
datatoken.name = event.params.tokenName
datatoken.decimals = 18
datatoken.address = event.params.tokenAddress.toHexString()
datatoken.cap = tokenToDecimal(event.params.tokenCap.toBigDecimal(), 18)
datatoken.supply = ZERO_BD
datatoken.minter = event.params.registeredBy.toHex()
datatoken.publisher = event.params.registeredBy.toHex()
datatoken.createTime = event.block.timestamp.toI32()
datatoken.holdersCount = BigInt.fromI32(0)
datatoken.factoryID = event.address.toHexString()
datatoken.orderCount = BigInt.fromI32(0)
datatoken.tx = event.transaction.hash
datatoken.save()

View File

@ -7,13 +7,13 @@ import {
ethereum
} from '@graphprotocol/graph-ts'
import {
Pool,
User,
PoolToken,
PoolShare,
TokenPrice,
Transaction,
OceanPools, Datatoken, TokenBalance
Pool,
User,
PoolToken,
PoolShare,
TokenPrice,
PoolTransaction,
OceanPools, Datatoken, TokenBalance, TokenTransaction
} from '../types/schema'
import { BToken } from '../types/templates/Pool/BToken'
import { log } from '@graphprotocol/graph-ts'
@ -27,7 +27,7 @@ export let OCEAN: string = (network == 'mainnet')
: '0x8967BCF84170c91B0d24D4302C2376283b0B3a07'
export function hexToDecimal(hexString: String, decimals: i32): BigDecimal {
let bytes = Bytes.fromHexString(hexString).reverse() as Bytes
let bytes = Bytes.fromHexString(hexString.toString()).reverse() as Bytes
let bi = BigInt.fromUnsignedBytes(bytes)
let scale = BigInt.fromI32(10).pow(decimals as u8).toBigDecimal()
return bi.divDecimal(scale)
@ -59,7 +59,7 @@ export function createPoolTokenEntity(id: string, pool: string, address: string)
let poolToken = new PoolToken(id)
poolToken.poolId = pool
poolToken.tokenId = datatoken ? datatoken.id: null
poolToken.tokenId = datatoken ? datatoken.id: ''
poolToken.address = address
poolToken.balance = ZERO_BD
poolToken.denormWeight = ZERO_BD
@ -79,6 +79,7 @@ export function updatePoolLiquidity(id: string): void {
let hasOceanPrice = false
let poolOcnLiquidity = ZERO_BD
let poolDTLiquidity = ZERO_BD
return
let oceanPoolTokenId = id.concat('-').concat(OCEAN)
let oceanPoolToken = PoolToken.load(oceanPoolTokenId)
@ -88,7 +89,7 @@ export function updatePoolLiquidity(id: string): void {
if (dtTokenPrice !== null) {
let poolTokenId = id.concat('-').concat(DT)
let poolToken = PoolToken.load(poolTokenId)
poolDTLiquidity = wethTokenPrice.price.times(poolToken.balance).div(poolToken.denormWeight).times(pool.totalWeight)
poolDTLiquidity = TokenPrice.price.times(poolToken.balance).div(poolToken.denormWeight).times(pool.totalWeight)
hasPrice = true
}
@ -160,12 +161,12 @@ export function decrPoolCount(finalized: boolean): void {
factory.save()
}
export function saveTransaction(event: ethereum.Event, eventName: string): void {
export function savePoolTransaction(event: ethereum.Event, eventName: string): void {
let tx = event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString())
let userAddress = event.transaction.from.toHex()
let transaction = Transaction.load(tx)
let transaction = PoolTransaction.load(tx)
if (transaction == null) {
transaction = new Transaction(tx)
transaction = new PoolTransaction(tx)
}
transaction.event = eventName
transaction.poolAddress = event.address.toHex()
@ -180,6 +181,26 @@ export function saveTransaction(event: ethereum.Event, eventName: string): void
createUserEntity(userAddress)
}
export function saveTokenTransaction(event: ethereum.Event, eventName: string): void {
let tx = event.transaction.hash.toHexString().concat('-').concat(event.logIndex.toString())
let userAddress = event.transaction.from.toHex()
let transaction = TokenTransaction.load(tx)
if (transaction == null) {
transaction = new TokenTransaction(tx)
}
transaction.event = eventName
transaction.datatokenAddress = event.address.toHex()
transaction.userAddress = userAddress
transaction.gasUsed = event.transaction.gasUsed.toBigDecimal()
transaction.gasPrice = event.transaction.gasPrice.toBigDecimal()
transaction.tx = event.transaction.hash
transaction.timestamp = event.block.timestamp.toI32()
transaction.block = event.block.number.toI32()
transaction.save()
createUserEntity(userAddress)
}
export function createUserEntity(address: string): void {
if (User.load(address) == null) {
let user = new User(address)

View File

@ -19,7 +19,7 @@ import {
createPoolShareEntity,
createPoolTokenEntity,
updatePoolLiquidity,
saveTransaction,
savePoolTransaction,
ZERO_BD,
decrPoolCount
} from './helpers'
@ -37,7 +37,7 @@ export function handleSetSwapFee(event: LOG_CALL, swapFeeStr: string=null): void
pool.swapFee = hexToDecimal(swapFeeStr, 18)
pool.save()
saveTransaction(event, 'setSwapFee')
// savePoolTransaction(event, 'setSwapFee')
}
export function handleSetController(event: LOG_CALL): void {
@ -47,17 +47,16 @@ export function handleSetController(event: LOG_CALL): void {
pool.controller = controller
pool.save()
saveTransaction(event, 'setController')
// savePoolTransaction(event, 'setController')
}
export function handleSetPublicSwap(event: LOG_CALL): void {
let poolId = event.address.toHex()
let pool = Pool.load(poolId)
let publicSwap = event.params.data.toHexString().slice(-1) == '1'
pool.publicSwap = publicSwap
pool.publicSwap = event.params.data.toHexString().slice(-1) == '1'
pool.save()
saveTransaction(event, 'setPublicSwap')
// savePoolTransaction(event, 'setPublicSwap')
}
export function handleFinalize(event: LOG_CALL): void {
@ -85,31 +84,32 @@ export function handleFinalize(event: LOG_CALL): void {
factory.finalizedPoolCount = factory.finalizedPoolCount + 1
factory.save()
saveTransaction(event, 'finalize')
// savePoolTransaction(event, 'finalize')
}
export function handleSetup(event: LOG_CALL): void {
let poolId = event.address.toHex()
let data = event.params.data.toHexString()
let dataTokenAddress = data.slice(34,74)
let dataTokenAddress = Address.fromString(data.slice(34,74)).toHexString()
let dataTokenAmount = data.slice(98,162)
let dataTokenWeight = data.slice(162,226)
let baseTokenAddress = data.slice(226, 290-24)
let baseTokenAddress = Address.fromString(data.slice(226, 290-24)).toHexString()
let baseTokenAmount = data.slice(290,354)
let baseTokenWeight = data.slice(354,418)
let swapFee = data.slice(418)
_handleRebind(event, poolId, dataTokenAddress, dataTokenAmount, dataTokenWeight)
_handleRebind(event, poolId, baseTokenAddress, baseTokenAmount, baseTokenWeight)
handleSetSwapFee(event, swapFee)
handleFinalize(event)
saveTransaction(event, 'setup')
savePoolTransaction(event, 'setup')
}
export function _handleRebind(event: LOG_CALL, poolId: string, tokenAddress: string, balanceStr: string, denormWeightStr: string): void {
let pool = Pool.load(poolId)
let decimals = BigInt.fromI32(18).toI32()
let tokenBytes = Bytes.fromHexString(tokenAddress) as Bytes
let tokensList = pool.tokensList || []
if (tokensList.indexOf(tokenBytes) == -1 ) {
@ -117,9 +117,8 @@ export function _handleRebind(event: LOG_CALL, poolId: string, tokenAddress: str
}
pool.tokensList = tokensList
pool.tokensCount = BigInt.fromI32(tokensList.length)
let address = Address.fromString(tokenAddress)
let denormWeight = hexToDecimal(denormWeightStr, 18)
let denormWeight = hexToDecimal(denormWeightStr, decimals)
let poolTokenId = poolId.concat('-').concat(address.toHexString())
let poolToken = PoolToken.load(poolTokenId)
@ -136,14 +135,11 @@ export function _handleRebind(event: LOG_CALL, poolId: string, tokenAddress: str
}
}
let datatoken = poolToken.tokenId != null ? Datatoken.load(poolToken.tokenId) : null
let decimals = datatoken == null ? BigInt.fromI32(18).toI32() : datatoken.decimals
let balance = hexToDecimal(balanceStr, decimals)
let balance = hexToDecimal(balanceStr, decimals)
poolToken.balance = balance
poolToken.denormWeight = denormWeight
poolToken.save()
if (balance.equals(ZERO_BD)) {
decrPoolCount(pool.finalized)
pool.active = false
@ -163,7 +159,7 @@ export function handleRebind(event: LOG_CALL): void {
event.params.data.toHexString().slice(138)
)
saveTransaction(event, 'rebind')
savePoolTransaction(event, 'rebind')
}
@ -184,14 +180,16 @@ export function handleJoinPool(event: LOG_JOIN): void {
if (!poolToken) {
return
}
let datatoken = poolToken.tokenId != null ? Datatoken.load(poolToken.tokenId) : null
let datatoken: Datatoken | null
datatoken = poolToken.tokenId != null ? Datatoken.load(poolToken.tokenId) : null
let decimals = datatoken == null ? BigInt.fromI32(18).toI32() : datatoken.decimals
let tokenAmountIn = tokenToDecimal(event.params.tokenAmountIn.toBigDecimal(), decimals)
poolToken.balance = poolToken.balance.plus(tokenAmountIn)
poolToken.save()
updatePoolLiquidity(poolId)
saveTransaction(event, 'join')
savePoolTransaction(event, 'join')
}
export function handleExitPool(event: LOG_EXIT): void {
@ -203,7 +201,9 @@ export function handleExitPool(event: LOG_EXIT): void {
if (!poolToken) {
return
}
let datatoken = poolToken.tokenId != null ? Datatoken.load(poolToken.tokenId) : null
let datatoken: Datatoken | null
datatoken = poolToken.tokenId != null ? Datatoken.load(poolToken.tokenId) : null
let decimals = datatoken == null ? BigInt.fromI32(18).toI32() : datatoken.decimals
let tokenAmountOut = tokenToDecimal(event.params.tokenAmountOut.toBigDecimal(), decimals)
let newAmount = poolToken.balance.minus(tokenAmountOut)
@ -219,7 +219,7 @@ export function handleExitPool(event: LOG_EXIT): void {
pool.save()
updatePoolLiquidity(poolId)
saveTransaction(event, 'exit')
savePoolTransaction(event, 'exit')
}
/************************************
@ -235,7 +235,8 @@ export function handleSwap(event: LOG_SWAP): void {
if (!poolTokenIn) {
return
}
let tokenAmountIn = tokenToDecimal(event.params.tokenAmountIn.toBigDecimal(), poolTokenIn.decimals)
let dtIn = Datatoken.load(tokenIn)
let tokenAmountIn = tokenToDecimal(event.params.tokenAmountIn.toBigDecimal(), (dtIn == null) ? 18 : dtIn.decimals)
let newAmountIn = poolTokenIn.balance.plus(tokenAmountIn)
poolTokenIn.balance = newAmountIn
poolTokenIn.save()
@ -243,7 +244,8 @@ export function handleSwap(event: LOG_SWAP): void {
let tokenOut = event.params.tokenOut.toHex()
let poolTokenOutId = poolId.concat('-').concat(tokenOut.toString())
let poolTokenOut = PoolToken.load(poolTokenOutId)
let tokenAmountOut = tokenToDecimal(event.params.tokenAmountOut.toBigDecimal(), poolTokenOut.decimals)
let dtOut = Datatoken.load(tokenOut)
let tokenAmountOut = tokenToDecimal(event.params.tokenAmountOut.toBigDecimal(), (dtOut == null) ? 18 : dtOut.decimals)
let newAmountOut = poolTokenOut.balance.minus(tokenAmountOut)
poolTokenOut.balance = newAmountOut
poolTokenOut.save()
@ -310,9 +312,9 @@ export function handleSwap(event: LOG_SWAP): void {
swap.caller = event.params.caller
swap.tokenIn = event.params.tokenIn
swap.tokenInSym = poolTokenIn.symbol
swap.tokenInSym = (dtIn == null) ? 'OCEAN' : dtIn.symbol
swap.tokenOut = event.params.tokenOut
swap.tokenOutSym = poolTokenOut.symbol
swap.tokenOutSym = (dtOut == null) ? 'OCEAN' : dtOut.symbol
swap.tokenAmountIn = tokenAmountIn
swap.tokenAmountOut = tokenAmountOut
swap.poolAddress = event.address.toHex()
@ -325,7 +327,7 @@ export function handleSwap(event: LOG_SWAP): void {
swap.timestamp = event.block.timestamp.toI32()
swap.save()
saveTransaction(event, 'swap')
savePoolTransaction(event, 'swap')
}
/************************************