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) +}