Extract decimal manipulation helpers to it own module

This commit is contained in:
Sebastián Galiano 2019-12-04 14:35:54 -03:00
parent e788e22e4d
commit b7b5a959dd
5 changed files with 22 additions and 14 deletions

12
src/helpers/decimal.ts Normal file
View File

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

View File

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

View File

@ -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()])

View File

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