From b7b5a959ddf6d2177707a98c1105974d3d55e85c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Galiano?= Date: Wed, 4 Dec 2019 14:35:54 -0300 Subject: [PATCH] Extract decimal manipulation helpers to it own module --- src/helpers/decimal.ts | 12 ++++++++++++ src/helpers/{tokens.ts => token.ts} | 0 src/mappings/account.ts | 6 ++++-- src/mappings/registry.ts | 2 +- src/mappings/token.ts | 16 +++++----------- 5 files changed, 22 insertions(+), 14 deletions(-) create mode 100644 src/helpers/decimal.ts rename src/helpers/{tokens.ts => token.ts} (100%) diff --git a/src/helpers/decimal.ts b/src/helpers/decimal.ts new file mode 100644 index 0000000..fbe1839 --- /dev/null +++ b/src/helpers/decimal.ts @@ -0,0 +1,12 @@ +import { BigDecimal } from '@graphprotocol/graph-ts' +import { BigInt } from '@graphprotocol/graph-ts/index' + +export let ZERO = BigDecimal.fromString('0') + +export function toDecimal(value: BigInt, decimals: u32): BigDecimal { + let precision = BigInt.fromI32(10) + .pow(decimals) + .toBigDecimal() + + return value.divDecimal(precision) +} diff --git a/src/helpers/tokens.ts b/src/helpers/token.ts similarity index 100% rename from src/helpers/tokens.ts rename to src/helpers/token.ts diff --git a/src/mappings/account.ts b/src/mappings/account.ts index 8dd4271..f051b82 100644 --- a/src/mappings/account.ts +++ b/src/mappings/account.ts @@ -1,7 +1,9 @@ -import { BigDecimal, BigInt, Bytes, EthereumEvent } from '@graphprotocol/graph-ts' +import { BigDecimal, Bytes, EthereumEvent } from '@graphprotocol/graph-ts' import { Account, AccountBalance, AccountBalanceSnapshot, Token } from '../../generated/schema' +import { ZERO } from '../helpers/decimal' + export function getOrCreateAccount(accountAddress: Bytes): Account { let accountId = accountAddress.toHex() let existingAccount = Account.load(accountId) @@ -27,7 +29,7 @@ function getOrCreateAccountBalance(account: Account, token: Token): AccountBalan let newBalance = new AccountBalance(balanceId) newBalance.account = account.id newBalance.token = token.id - newBalance.amount = BigInt.fromI32(0).toBigDecimal() + newBalance.amount = ZERO return newBalance } diff --git a/src/mappings/registry.ts b/src/mappings/registry.ts index 219b76a..a7998d4 100644 --- a/src/mappings/registry.ts +++ b/src/mappings/registry.ts @@ -5,7 +5,7 @@ import { Unknown } from '../../generated/TokenRegistry/TokenRegistry' import { BurnableToken, MintableToken, StandardToken } from '../../generated/templates' import { REGISTRY_HASH } from '../config' -import { decodeFlags, DEFAULT_DECIMALS, isBurnable, isMintable } from '../helpers/tokens' +import { decodeFlags, DEFAULT_DECIMALS, isBurnable, isMintable } from '../helpers/token' export function initRegistry(event: Unknown): void { log.debug('Initializing token registry, block={}', [event.block.number.toString()]) diff --git a/src/mappings/token.ts b/src/mappings/token.ts index b6a7560..1deba5e 100644 --- a/src/mappings/token.ts +++ b/src/mappings/token.ts @@ -1,4 +1,4 @@ -import { BigInt, BigDecimal, Bytes, EthereumEvent } from '@graphprotocol/graph-ts' +import { BigDecimal, Bytes, EthereumEvent } from '@graphprotocol/graph-ts' import { Transfer } from '../../generated/templates/StandardToken/ERC20' import { Burn } from '../../generated/templates/BurnableToken/Burnable' @@ -6,11 +6,13 @@ import { Mint } from '../../generated/templates/MintableToken/Mintable' import { BurnEvent, MintEvent, Token, TransferEvent } from '../../generated/schema' +import { toDecimal } from '../helpers/decimal' + import { decreaseAccountBalance, getOrCreateAccount, increaseAccountBalance, - saveAccountBalanceSnapshot + saveAccountBalanceSnapshot, } from './account' const GENESIS_ADDRESS = '0x0000000000000000000000000000000000000000' @@ -163,7 +165,7 @@ function createTransferEvent( event: EthereumEvent, amount: BigDecimal, source: Bytes, - destination: Bytes + destination: Bytes, ): TransferEvent { let eventEntity = new TransferEvent(event.transaction.hash.toHex() + '-' + event.logIndex.toString()) eventEntity.token = event.address.toHex() @@ -178,11 +180,3 @@ function createTransferEvent( return eventEntity } - -function toDecimal(value: BigInt, decimals: u32): BigDecimal { - let precision = BigInt.fromI32(10) - .pow(decimals) - .toBigDecimal() - - return value.divDecimal(precision) -}