ocean-subgraph/src/mappings/erc721Factory.ts

72 lines
2.4 KiB
TypeScript
Raw Normal View History

import {
NFTCreated,
TokenCreated,
ERC721Factory
} from '../@types/ERC721Factory/ERC721Factory'
import { decimal } from './utils/constants'
import { weiToDecimal } from './utils/generic'
2022-02-18 12:09:18 +01:00
2021-11-10 13:47:44 +01:00
import { getUser } from './utils/userUtils'
2022-02-18 12:09:18 +01:00
import { getToken, getNftToken } from './utils/tokenUtils'
2022-02-20 14:51:01 +01:00
import { addDatatoken } from './utils/globalUtils'
import { BigInt } from '@graphprotocol/graph-ts'
2021-11-04 16:00:43 +01:00
export function handleNftCreated(event: NFTCreated): void {
2022-02-18 12:09:18 +01:00
// const nft = new Nft(event.params.newTokenAddress.toHexString())
const nft = getNftToken(event.params.newTokenAddress)
2021-11-10 13:47:44 +01:00
const user = getUser(event.params.admin.toHexString())
nft.owner = user.id
const creator = getUser(event.params.creator.toHexString())
nft.creator = creator.id
2021-11-04 16:00:43 +01:00
nft.address = event.params.newTokenAddress.toHexString()
2021-11-19 15:42:17 +01:00
nft.name = event.params.tokenName
nft.symbol = event.params.symbol.toString()
nft.tokenUri = event.params.tokenURI.toString()
2021-11-10 13:47:44 +01:00
nft.createdTimestamp = event.block.timestamp.toI32()
2021-12-02 12:08:47 +01:00
nft.tx = event.transaction.hash.toHex()
2021-11-04 16:00:43 +01:00
nft.block = event.block.number.toI32()
nft.transferable = event.params.transferable
2021-11-04 16:00:43 +01:00
nft.save()
}
export function handleNewToken(event: TokenCreated): void {
2022-02-18 12:09:18 +01:00
const token = getToken(event.params.newTokenAddress, true)
// const token = new Token(event.params.newTokenAddress.toHexString())
2021-11-04 16:00:43 +01:00
token.isDatatoken = true
token.address = event.params.newTokenAddress.toHexString()
2021-11-10 13:47:44 +01:00
token.createdTimestamp = event.block.timestamp.toI32()
2021-12-02 12:08:47 +01:00
token.tx = event.transaction.hash.toHex()
2021-11-04 16:00:43 +01:00
token.block = event.block.number.toI32()
token.nft = event.params.creator.toHexString()
2021-11-19 15:42:17 +01:00
token.name = event.params.name
token.symbol = event.params.symbol
2021-11-04 16:00:43 +01:00
token.decimals = 18
2021-11-19 15:42:17 +01:00
token.supply = decimal.ZERO
token.cap = weiToDecimal(event.params.cap.toBigDecimal(), 18)
const eventTemplateAddress = event.params.templateAddress
.toHexString()
.toLowerCase()
const contract = ERC721Factory.bind(event.address)
const templateCount = contract.try_getCurrentTemplateCount()
if (templateCount.reverted) return
const templateCountNum = templateCount.value.toI32()
for (let i = 0; i < templateCountNum; i++) {
const template = contract.try_getTokenTemplate(BigInt.fromI32(1 + i))
if (template.reverted) return
const templateAddress = template.value.templateAddress
.toHexString()
.toLowerCase()
if (templateAddress == eventTemplateAddress) {
token.templateId = 1 + i
}
}
2021-12-07 10:47:58 +01:00
2021-11-04 16:00:43 +01:00
token.save()
2022-02-20 14:51:01 +01:00
addDatatoken()
2021-11-04 16:00:43 +01:00
}