Template id detection (#705)

* handle templateIds

* lint

* fixes

* fixes
This commit is contained in:
Alex Coseru 2023-08-15 15:00:49 +03:00 committed by GitHub
parent fa58272978
commit 299d196162
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 98 additions and 34 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!
@ -630,3 +630,17 @@ type NftTransferHistory @entity {
timestamp: Int!
block: Int!
}
type Erc721Template @entity {
#ID = template address
id: ID!
templateId: BigInt!
}
type Erc20Template @entity {
#ID = template address
id: ID!
templateId: BigInt!
}

View File

@ -1,15 +1,16 @@
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 { getUser } from './utils/userUtils'
import { getToken, getNftToken } from './utils/tokenUtils'
import { getToken, getNftToken, getErc20TemplateId } from './utils/tokenUtils'
import { addDatatoken } from './utils/globalUtils'
import { BigInt } from '@graphprotocol/graph-ts'
export function handleNftCreated(event: NFTCreated): void {
// const nft = new Nft(event.params.newTokenAddress.toHexString())
@ -27,6 +28,7 @@ export function handleNftCreated(event: NFTCreated): void {
nft.block = event.block.number.toI32()
nft.eventIndex = event.logIndex.toI32()
nft.transferable = event.params.transferable
nft.template = event.params.templateAddress.toHexString()
nft.save()
}
@ -49,25 +51,27 @@ export function handleNewToken(event: TokenCreated): void {
token.decimals = 18
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
}
}
token.templateId = getErc20TemplateId(event.params.templateAddress)
token.save()
addDatatoken()
}
export function handleNew721Template(event: Template721Added): void {
let template = Erc721Template.load(
event.params._templateAddress.toHexString()
)
if (template === null) {
template = new Erc721Template(event.params._templateAddress.toHexString())
template.templateId = event.params.nftTemplateCount
template.save()
}
}
export function handleNew20Template(event: Template20Added): void {
let template = Erc20Template.load(event.params._templateAddress.toHexString())
if (template === null) {
template = new Erc20Template(event.params._templateAddress.toHexString())
template.templateId = event.params.nftTemplateCount
template.save()
}
}

View File

@ -1,5 +1,5 @@
import { Address, log, BigDecimal, BigInt } from '@graphprotocol/graph-ts'
import { Nft, Token } from '../../@types/schema'
import { Nft, Token, Erc721Template, Erc20Template } from '../../@types/schema'
import { ERC20 } from '../../@types/templates/ERC20Template/ERC20'
import { ERC20Template, ERC721Template } from '../../@types/templates'
import { addNft } from './globalUtils'
@ -109,3 +109,19 @@ export function getUSDValue(
): BigDecimal {
return BigDecimal.zero()
}
export function getErc721TemplateId(address: Address): BigInt {
const template = Erc721Template.load(address.toHexString())
if (template) {
return template.templateId
}
return BigInt.zero()
}
export function getErc20TemplateId(address: Address): BigInt {
const template = Erc20Template.load(address.toHexString())
if (template) {
return template.templateId
}
return BigInt.zero()
}

View File

@ -198,6 +198,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

View File

@ -197,8 +197,10 @@ describe('Datatoken tests', async () => {
dt.publishMarketFeeAmount === publishMarketFeeAmount,
'incorrect value for: publishMarketFeeAmount'
)
assert(dt.templateId === templateIndex, 'incorrect value for: templateId')
assert(
parseInt(dt.templateId) === templateIndex,
'incorrect value for: templateId'
)
assert(dt.holderCount === '0', 'incorrect value for: holderCount')
assert(dt.orderCount === '0', 'incorrect value for: orderCount')
assert(dt.orders, 'incorrect value for: orders')
@ -314,7 +316,10 @@ describe('Datatoken tests', async () => {
dt.publishMarketFeeAmount === publishMarketFeeAmount,
'incorrect value for: publishMarketFeeAmount'
)
assert(dt.templateId === templateIndex, 'incorrect value for: templateId')
assert(
parseInt(dt.templateId) === templateIndex,
'incorrect value for: templateId'
)
assert(dt.holderCount === '0', 'incorrect value for: holderCount')
assert(dt.orderCount === '0', 'incorrect value for: orderCount')
assert(dt.orders, 'incorrect value for: orders')

View File

@ -110,6 +110,7 @@ describe('Dispenser tests', async () => {
nftParams,
erc20Params
)
const nftTemplate = await Factory.getNFTTemplate(nftParams.templateIndex)
assert(tx.events.NFTCreated.event === 'NFTCreated')
assert(tx.events.TokenCreated.event === 'TokenCreated')
nftAddress = tx.events.NFTCreated.returnValues.newTokenAddress.toLowerCase()
@ -163,7 +164,10 @@ describe('Dispenser tests', async () => {
)
assert(nft.storeUpdateRole === null, 'incorrect value for: storeUpdateRole')
assert(nft.metadataRole === null, 'incorrect value for: metadataRole')
assert(nft.template === '', 'incorrect value for: template')
assert(
nft.template === nftTemplate.templateAddress.toLowerCase(),
'incorrect value for: template'
)
assert(nft.transferable === true, 'incorrect value for: transferable')
assert(nft.createdTimestamp >= time, 'incorrect value: createdTimestamp')
assert(nft.createdTimestamp < time + 5, 'incorrect value: createdTimestamp')
@ -243,7 +247,10 @@ describe('Dispenser tests', async () => {
'incorrect value for: publishMarketFeeAmount'
)
assert(dt.templateId === templateIndex, 'incorrect value for: templateId')
assert(
parseInt(dt.templateId) === templateIndex,
'incorrect value for: templateId'
)
assert(dt.holderCount === '0', 'incorrect value for: holderCount')
assert(dt.orderCount === '0', 'incorrect value for: orderCount')
assert(dt.orders, 'incorrect value for: orders')

View File

@ -127,6 +127,7 @@ describe('Fixed Rate Exchange tests', async () => {
result.events.NewFixedRate.returnValues.exchangeId.toLowerCase()
fixedRateId = `${exchangeContract}-${exchangeId}`
const nftTemplate = await Factory.getNFTTemplate(nftParams.templateIndex)
// Check NFT values
await sleep(sleepMs)
@ -176,7 +177,10 @@ describe('Fixed Rate Exchange tests', async () => {
)
assert(nft.storeUpdateRole === null, 'incorrect value for: storeUpdateRole')
assert(nft.metadataRole === null, 'incorrect value for: metadataRole')
assert(nft.template === '', 'incorrect value for: template')
assert(
nft.template === nftTemplate.templateAddress.toLowerCase(),
'incorrect value for: template'
)
assert(nft.transferable === true, 'incorrect value for: transferable')
assert(
nft.createdTimestamp >= time,
@ -263,7 +267,10 @@ describe('Fixed Rate Exchange tests', async () => {
'incorrect value for: publishMarketFeeAmount'
)
assert(dt.templateId === templateIndex, 'incorrect value for: templateId')
assert(
parseInt(dt.templateId) === templateIndex,
'incorrect value for: templateId'
)
assert(dt.holderCount === '0', 'incorrect value for: holderCount')
assert(dt.orderCount === '0', 'incorrect value for: orderCount')
assert(dt.orders, 'incorrect value for: orders')

View File

@ -66,6 +66,7 @@ const ddo = {
}
]
}
let nftTemplate
describe('NFT tests', async () => {
const nftName = 'testNFT'
@ -119,7 +120,7 @@ describe('NFT tests', async () => {
)
erc721Address = result.events.NFTCreated.returnValues[0]
datatokenAddress = result.events.TokenCreated.returnValues[0]
nftTemplate = await Factory.getNFTTemplate(nftParams.templateIndex)
// Check values before updating metadata
await sleep(3000)
nftAddress = erc721Address.toLowerCase()
@ -167,7 +168,10 @@ describe('NFT tests', async () => {
)
assert(nft.storeUpdateRole === null, 'incorrect value for: storeUpdateRole')
assert(nft.metadataRole === null, 'incorrect value for: metadataRole')
assert(nft.template === '', 'incorrect value for: template')
assert(
nft.template === nftTemplate.templateAddress.toLowerCase(),
'incorrect value for: template'
)
assert(nft.transferable === true, 'incorrect value for: transferable')
assert(
nft.createdTimestamp >= time,
@ -276,7 +280,10 @@ describe('NFT tests', async () => {
updatedNft.metadataRole === null,
'incorrect value for: metadataRole'
)
assert(updatedNft.template === '', 'incorrect value for: template')
assert(
updatedNft.template === nftTemplate.templateAddress.toLowerCase(),
'incorrect value for: template'
)
assert(
updatedNft.transferable === true,
'incorrect value for: transferable'