Extract decimal manipulation helpers to it own module
This commit is contained in:
parent
e788e22e4d
commit
b7b5a959dd
|
@ -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)
|
||||||
|
}
|
|
@ -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 { Account, AccountBalance, AccountBalanceSnapshot, Token } from '../../generated/schema'
|
||||||
|
|
||||||
|
import { ZERO } from '../helpers/decimal'
|
||||||
|
|
||||||
export function getOrCreateAccount(accountAddress: Bytes): Account {
|
export function getOrCreateAccount(accountAddress: Bytes): Account {
|
||||||
let accountId = accountAddress.toHex()
|
let accountId = accountAddress.toHex()
|
||||||
let existingAccount = Account.load(accountId)
|
let existingAccount = Account.load(accountId)
|
||||||
|
@ -27,7 +29,7 @@ function getOrCreateAccountBalance(account: Account, token: Token): AccountBalan
|
||||||
let newBalance = new AccountBalance(balanceId)
|
let newBalance = new AccountBalance(balanceId)
|
||||||
newBalance.account = account.id
|
newBalance.account = account.id
|
||||||
newBalance.token = token.id
|
newBalance.token = token.id
|
||||||
newBalance.amount = BigInt.fromI32(0).toBigDecimal()
|
newBalance.amount = ZERO
|
||||||
|
|
||||||
return newBalance
|
return newBalance
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { Unknown } from '../../generated/TokenRegistry/TokenRegistry'
|
||||||
import { BurnableToken, MintableToken, StandardToken } from '../../generated/templates'
|
import { BurnableToken, MintableToken, StandardToken } from '../../generated/templates'
|
||||||
|
|
||||||
import { REGISTRY_HASH } from '../config'
|
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 {
|
export function initRegistry(event: Unknown): void {
|
||||||
log.debug('Initializing token registry, block={}', [event.block.number.toString()])
|
log.debug('Initializing token registry, block={}', [event.block.number.toString()])
|
||||||
|
|
|
@ -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 { Transfer } from '../../generated/templates/StandardToken/ERC20'
|
||||||
import { Burn } from '../../generated/templates/BurnableToken/Burnable'
|
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 { BurnEvent, MintEvent, Token, TransferEvent } from '../../generated/schema'
|
||||||
|
|
||||||
|
import { toDecimal } from '../helpers/decimal'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
decreaseAccountBalance,
|
decreaseAccountBalance,
|
||||||
getOrCreateAccount,
|
getOrCreateAccount,
|
||||||
increaseAccountBalance,
|
increaseAccountBalance,
|
||||||
saveAccountBalanceSnapshot
|
saveAccountBalanceSnapshot,
|
||||||
} from './account'
|
} from './account'
|
||||||
|
|
||||||
const GENESIS_ADDRESS = '0x0000000000000000000000000000000000000000'
|
const GENESIS_ADDRESS = '0x0000000000000000000000000000000000000000'
|
||||||
|
@ -163,7 +165,7 @@ function createTransferEvent(
|
||||||
event: EthereumEvent,
|
event: EthereumEvent,
|
||||||
amount: BigDecimal,
|
amount: BigDecimal,
|
||||||
source: Bytes,
|
source: Bytes,
|
||||||
destination: Bytes
|
destination: Bytes,
|
||||||
): TransferEvent {
|
): TransferEvent {
|
||||||
let eventEntity = new TransferEvent(event.transaction.hash.toHex() + '-' + event.logIndex.toString())
|
let eventEntity = new TransferEvent(event.transaction.hash.toHex() + '-' + event.logIndex.toString())
|
||||||
eventEntity.token = event.address.toHex()
|
eventEntity.token = event.address.toHex()
|
||||||
|
@ -178,11 +180,3 @@ function createTransferEvent(
|
||||||
|
|
||||||
return eventEntity
|
return eventEntity
|
||||||
}
|
}
|
||||||
|
|
||||||
function toDecimal(value: BigInt, decimals: u32): BigDecimal {
|
|
||||||
let precision = BigInt.fromI32(10)
|
|
||||||
.pow(<u8>decimals)
|
|
||||||
.toBigDecimal()
|
|
||||||
|
|
||||||
return value.divDecimal(precision)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue