Process burnable/mintable tokens that use Transfer event instead of Burn/Mint

This commit is contained in:
Sebastián Galiano 2019-07-26 10:58:01 -03:00
parent 3dab66fba7
commit 22bd63fd50
2 changed files with 73 additions and 28 deletions

View File

@ -1,6 +1,6 @@
import { Address, JSONValue, Value, log, ipfs } from '@graphprotocol/graph-ts'
import { Token } from '../../generated/schema'
import * as schema from '../../generated/schema'
import { Unknown } from '../../generated/TokenRegistry/TokenRegistry'
import { BurnableToken, MintableToken, StandardToken } from '../../generated/TokenRegistry/templates'
@ -27,10 +27,10 @@ export function createToken(value: JSONValue, userData: Value): void {
if (address) {
let contractAddress = Address.fromString(address)
let token = Token.load(contractAddress.toHex())
let token = schema.Token.load(contractAddress.toHex())
if (!token) {
token = new Token(contractAddress.toHex())
token = new schema.Token(contractAddress.toHex())
token.address = contractAddress
token.name = name
token.symbol = symbol
@ -83,14 +83,17 @@ function decodeFlags(value: u64): string[] {
return flags
}
// If token implements optional ERC20 fields
function isDetailed(flags: u64): boolean {
return (flags & (2 << 0)) != 0
}
// If token can be irreversibly destroyed
function isBurnable(flags: u64): boolean {
return (flags & (2 << 1)) != 0
}
// If token can be created or minted
function isMintable(flags: u64): boolean {
return (flags & (2 << 3)) != 0
}

View File

@ -1,4 +1,4 @@
import { BigInt, BigDecimal, log } from '@graphprotocol/graph-ts'
import { BigInt, BigDecimal, Bytes, EthereumEvent } from '@graphprotocol/graph-ts'
import * as schema from '../../generated/schema'
@ -6,54 +6,96 @@ import { Transfer } from '../../generated/TokenRegistry/templates/StandardToken/
import { Burn } from '../../generated/TokenRegistry/templates/BurnableToken/Burnable'
import { Mint } from '../../generated/TokenRegistry/templates/MintableToken/Mintable'
const GENESIS_ADDRESS = '0x0000000000000000000000000000000000000000'
export function handleTransfer(event: Transfer): void {
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 = toDecimal(event.params.value, token.decimals)
entity.sender = event.params.from
entity.source = event.params.from
entity.destination = event.params.to
if (token) {
let amount = toDecimal(event.params.value, token.decimals)
entity.block = event.block.number
entity.transaction = event.transaction.hash
entity.timestamp = event.block.timestamp
entity.save()
if (event.params.from.toHex() === GENESIS_ADDRESS) {
let entity = mintEvent(event, amount, event.params.to)
entity.save()
} else if (event.params.to.toHex() === GENESIS_ADDRESS) {
let entity = burnEvent(event, amount, event.params.from)
entity.save()
} else {
let entity = transferEvent(event, amount, event.params.from, event.params.to)
entity.save()
}
}
}
export function handleBurn(event: Burn): void {
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 = toDecimal(event.params.value, token.decimals)
entity.sender = event.transaction.from
entity.burner = event.params.burner
if (token) {
let amount = toDecimal(event.params.value, token.decimals)
let entity = burnEvent(event, amount, event.params.burner)
entity.block = event.block.number
entity.transaction = event.transaction.hash
entity.timestamp = event.block.timestamp
entity.save()
entity.save()
}
}
export function handleMint(event: Mint): void {
let token = schema.Token.load(event.address.toHex())
if (token) {
let amount = toDecimal(event.params.amount, token.decimals)
let entity = mintEvent(event, amount, event.params.to)
entity.save()
}
}
function burnEvent(event: EthereumEvent, amount: BigDecimal, burner: Bytes): schema.BurnEvent {
let entity = new schema.BurnEvent(event.transaction.hash.toHex() + '-' + event.logIndex.toString())
entity.token = event.address.toHex()
entity.amount = amount
entity.sender = event.transaction.from
entity.burner = burner
entity.block = event.block.number
entity.transaction = event.transaction.hash
entity.timestamp = event.block.timestamp
return entity
}
function mintEvent(event: EthereumEvent, amount: BigDecimal, destination: Bytes): schema.MintEvent {
let entity = new schema.MintEvent(event.transaction.hash.toHex() + '-' + event.logIndex.toString())
entity.token = event.address.toHex()
entity.amount = toDecimal(event.params.amount, token.decimals)
entity.amount = amount
entity.sender = event.transaction.from
entity.destination = event.params.to
entity.destination = destination
entity.minter = event.transaction.from
entity.block = event.block.number
entity.transaction = event.transaction.hash
entity.timestamp = event.block.timestamp
entity.save()
return entity
}
function transferEvent(
event: EthereumEvent,
amount: BigDecimal,
source: Bytes,
destination: Bytes
): schema.TransferEvent {
let entity = new schema.TransferEvent(event.transaction.hash.toHex() + '-' + event.logIndex.toString())
entity.token = event.address.toHex()
entity.amount = amount
entity.sender = source
entity.source = source
entity.destination = destination
entity.block = event.block.number
entity.transaction = event.transaction.hash
entity.timestamp = event.block.timestamp
return entity
}
function toDecimal(value: BigInt, decimals: u32): BigDecimal {