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