merge template

This commit is contained in:
alexcos20 2023-07-29 19:50:40 +03:00
parent d9d420990c
commit 1434cab3b0
4 changed files with 99 additions and 27 deletions

View File

@ -30,7 +30,7 @@ type Token @entity {
publishMarketFeeAmount: BigDecimal
"template ID of the datatoken"
templateId: Int
templateId: BigInt
"number of addresses holding a balance of datatoken , TODO: can we actually calculate this? what happens when users trade the dts"
holderCount: BigInt!
@ -634,6 +634,20 @@ type NftTransferHistory @entity {
block: Int!
}
type Erc721Template @entity {
#ID = template address
id: ID!
templateId: BigInt!
}
type Erc20Template @entity {
#ID = template address
id: ID!
templateId: BigInt!
}
type PredictSubscription @entity{
"id = {contract address}-{txid}-{eventIndex}"
id: ID!

View File

@ -1,6 +1,11 @@
import { NFTCreated, TokenCreated } from '../@types/ERC721Factory/ERC721Factory'
import { ERC721Template } from '../@types/templates/ERC721Template/ERC721Template'
import { ERC20Template } from '../@types/templates/ERC20Template/ERC20Template'
import {
NFTCreated,
TokenCreated,
ERC721Factory,
Template721Added,
Template20Added
} from '../@types/ERC721Factory/ERC721Factory'
import { Erc721Template, Erc20Template } from '../@types/schema'
import { decimal } from './utils/constants'
import { weiToDecimal } from './utils/generic'
import {
@ -8,7 +13,14 @@ import {
ERC20Template3 as factoryERC20Template3
} from '../@types/templates'
import { getUser } from './utils/userUtils'
import { getToken, getNftToken, getPredictContract } from './utils/tokenUtils'
import {
getToken,
getNftToken,
getPredictContract,
getErc721TemplateId,
getErc20TemplateId
} from './utils/tokenUtils'
import { BigInt } from '@graphprotocol/graph-ts'
import { addDatatoken } from './utils/globalUtils'
export function handleNftCreated(event: NFTCreated): void {
// const nft = new Nft(event.params.newTokenAddress.toHexString())
@ -26,12 +38,8 @@ export function handleNftCreated(event: NFTCreated): void {
nft.block = event.block.number.toI32()
nft.eventIndex = event.logIndex.toI32()
nft.transferable = event.params.transferable
// get token id
const contract = ERC721Template.bind(event.params.newTokenAddress)
const contractTemplate = contract.try_getId()
if (!contractTemplate.reverted) {
nft.templateId = contractTemplate.value
}
nft.template = event.params.templateAddress.toHexString()
nft.save()
}
@ -53,21 +61,10 @@ export function handleNewToken(event: TokenCreated): void {
token.decimals = 18
token.supply = decimal.ZERO
token.cap = weiToDecimal(event.params.cap.toBigDecimal(), 18)
// get token id
const contract = ERC20Template.bind(event.params.newTokenAddress)
let tries = 0
// protect against crappy rpc
do {
const contractTemplate = contract.try_getId()
if (!contractTemplate.reverted) {
token.templateId = contractTemplate.value
break
}
tries++
} while (tries < 300)
token.templateId = getErc20TemplateId(event.params.templateAddress)
token.save()
addDatatoken()
if (token.templateId == 3) {
if (token.templateId == BigInt.fromString('3')) {
factoryERC20Template3.create(event.params.newTokenAddress)
const predictContract = getPredictContract(event.params.newTokenAddress)
predictContract.timestamp = event.block.timestamp.toI32()
@ -78,3 +75,39 @@ export function handleNewToken(event: TokenCreated): void {
}
factoryERC20Template.create(event.params.newTokenAddress)
}
export function handleNew721Template(event: Template721Added): void {
const dbId = getErc721TemplateId(event.params._templateAddress)
if (dbId === BigInt.zero()) {
const template = new Erc721Template(event.params._templateAddress)
template.templateId = event.params.nftTemplateCount
template.save()
}
}
export function handleNew20Template(event: Template20Added): void {
const dbId = getErc20TemplateId(event.params._templateAddress)
if (dbId === BigInt.zero()) {
const template = new Erc20Template(event.params._templateAddress)
template.templateId = event.params.nftTemplateCount
template.save()
}
}
export function handleNew721Template(event: Template721Added): void {
const dbId = getErc721TemplateId(event.params._templateAddress)
if (dbId === BigInt.zero()) {
const template = new Erc721Template(event.params._templateAddress)
template.templateId = event.params.nftTemplateCount
template.save()
}
}
export function handleNew20Template(event: Template20Added): void {
const dbId = getErc20TemplateId(event.params._templateAddress)
if (dbId === BigInt.zero()) {
const template = new Erc20Template(event.params._templateAddress)
template.templateId = event.params.nftTemplateCount
template.save()
}
}

View File

@ -1,5 +1,11 @@
import { Address, log, BigDecimal, BigInt } from '@graphprotocol/graph-ts'
import { Nft, Token, PredictContract } from '../../@types/schema'
import {
Nft,
Token,
PredictContract,
Erc721Template,
Erc20Template
} from '../../@types/schema'
import { ERC20 } from '../../@types/templates/ERC20Template/ERC20'
import { ERC721Template } from '../../@types/templates'
import { addNft } from './globalUtils'
@ -28,7 +34,7 @@ export function createToken(address: Address, isDatatoken: boolean): Token {
token.block = 0
token.tx = ''
token.eventIndex = 0
token.templateId = 0
token.templateId = BigInt.zero()
token.save()
return token
}
@ -53,7 +59,6 @@ export function createNftToken(address: Address): Nft {
token.creator = ''
token.assetState = 0
token.template = ''
token.templateId = 0
token.transferable = true
token.createdTimestamp = 0
token.block = 0
@ -132,3 +137,19 @@ export function getPredictContract(address: Address): PredictContract {
}
return newPredictContract
}
export function getErc721TemplateId(address: Address): BigInt {
const template = Erc721Template.load(address)
if (template) {
return template.templateId
}
return BigInt.zero()
}
export function getErc20TemplateId(address: Address): BigInt {
const template = Erc20Template.load(address)
if (template) {
return template.templateId
}
return BigInt.zero()
}

View File

@ -235,6 +235,10 @@ dataSources:
handler: handleNftCreated
- event: TokenCreated(indexed address,indexed address,string,string,uint256,address)
handler: handleNewToken
- event: Template721Added(indexed address,indexed uint256)
handler: handleNew721Template
- event: Template20Added(indexed address,indexed uint256)
handler: handleNew20Template
- kind: ethereum/contract
name: FactoryRouter