From 3dab66fba7124537073f3cb62ea1767697f7d4db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Galiano?= Date: Fri, 19 Jul 2019 01:32:06 -0300 Subject: [PATCH] Using the right token amount precision for events --- schema.graphql | 10 +++++----- src/mappings/token.ts | 22 +++++++++++++++------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/schema.graphql b/schema.graphql index f72df65..7d9c373 100644 --- a/schema.graphql +++ b/schema.graphql @@ -50,7 +50,7 @@ interface TokenEvent @entity { # Information about to event itself token: Token! - amount: BigInt! + amount: BigDecimal! sender: Bytes! # Information about block and transaction @@ -65,8 +65,8 @@ type BurnEvent implements TokenEvent @entity { " Token address " token: Token! - " Quantity of tokens nurned " - amount: BigInt! + " Quantity of tokens burned " + amount: BigDecimal! " Transaction sender address " sender: Bytes! @@ -91,7 +91,7 @@ type MintEvent implements TokenEvent @entity { token: Token! " Quantity of tokens minted " - amount: BigInt! + amount: BigDecimal! " Transaction sender address " sender: Bytes! @@ -119,7 +119,7 @@ type TransferEvent implements TokenEvent @entity { token: Token! " Quantity of tokens transferred " - amount: BigInt! + amount: BigDecimal! " Transaction sender address " sender: Bytes! diff --git a/src/mappings/token.ts b/src/mappings/token.ts index a3a2c0b..8912999 100644 --- a/src/mappings/token.ts +++ b/src/mappings/token.ts @@ -1,4 +1,4 @@ -import { log } from '@graphprotocol/graph-ts' +import { BigInt, BigDecimal, log } from '@graphprotocol/graph-ts' import * as schema from '../../generated/schema' @@ -7,11 +7,11 @@ import { Burn } from '../../generated/TokenRegistry/templates/BurnableToken/Burn import { Mint } from '../../generated/TokenRegistry/templates/MintableToken/Mintable' export function handleTransfer(event: Transfer): void { - log.debug('Handling token transfer, address={}', [event.address.toHex()]) + let token = schema.Token.load(event.address.toHex()) let entity = new schema.TransferEvent(event.transaction.hash.toHex() + '-' + event.logIndex.toString()) entity.token = event.address.toHex() - entity.amount = event.params.value + entity.amount = toDecimal(event.params.value, token.decimals) entity.sender = event.params.from entity.source = event.params.from entity.destination = event.params.to @@ -24,11 +24,11 @@ export function handleTransfer(event: Transfer): void { } export function handleBurn(event: Burn): void { - log.debug('Handling token burn, address={}', [event.address.toHex()]) + let token = schema.Token.load(event.address.toHex()) let entity = new schema.BurnEvent(event.transaction.hash.toHex() + '-' + event.logIndex.toString()) entity.token = event.address.toHex() - entity.amount = event.params.value + entity.amount = toDecimal(event.params.value, token.decimals) entity.sender = event.transaction.from entity.burner = event.params.burner @@ -40,11 +40,11 @@ export function handleBurn(event: Burn): void { } export function handleMint(event: Mint): void { - log.debug('Handling token mint, address={}', [event.address.toHex()]) + let token = schema.Token.load(event.address.toHex()) let entity = new schema.MintEvent(event.transaction.hash.toHex() + '-' + event.logIndex.toString()) entity.token = event.address.toHex() - entity.amount = event.params.amount + entity.amount = toDecimal(event.params.amount, token.decimals) entity.sender = event.transaction.from entity.destination = event.params.to entity.minter = event.transaction.from @@ -55,3 +55,11 @@ export function handleMint(event: Mint): void { entity.save() } + +function toDecimal(value: BigInt, decimals: u32): BigDecimal { + let precision = BigInt.fromI32(10) + .pow(decimals) + .toBigDecimal() + + return value.divDecimal(precision) +}