Using the right token amount precision for events

This commit is contained in:
Sebastián Galiano 2019-07-19 01:32:06 -03:00
parent 82440be5a3
commit 3dab66fba7
2 changed files with 20 additions and 12 deletions

View File

@ -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!

View File

@ -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(<u8>decimals)
.toBigDecimal()
return value.divDecimal(precision)
}