Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template id detection #705

Merged
merged 4 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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!
}


46 changes: 25 additions & 21 deletions src/mappings/erc721Factory.ts
Original file line number Diff line number Diff line change
@@ -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())
Expand All @@ -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()
}
Expand All @@ -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()
token.templateId = getErc20TemplateId(event.params.templateAddress)
token.save()
addDatatoken()
}

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
}
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()
}
}

token.save()
addDatatoken()
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()
}
}
18 changes: 17 additions & 1 deletion src/mappings/utils/tokenUtils.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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()
}
4 changes: 4 additions & 0 deletions subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions test/integration/Datatoken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { TransactionReceipt } from 'web3-core'

const data = JSON.parse(
fs.readFileSync(

Check warning on line 23 in test/integration/Datatoken.test.ts

View workflow job for this annotation

GitHub Actions / test

Found fs.readFileSync with non literal argument at index 0

Check warning on line 23 in test/integration/Datatoken.test.ts

View workflow job for this annotation

GitHub Actions / test

Found fs.readFileSync with non literal argument at index 0
process.env.ADDRESS_FILE ||
`${homedir}/.ocean/ocean-contracts/artifacts/address.json`,
'utf8'
Expand Down Expand Up @@ -197,8 +197,10 @@
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')
Expand Down Expand Up @@ -314,7 +316,10 @@
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')
Expand Down
11 changes: 9 additions & 2 deletions test/integration/Dispenser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
const sleepMs = 1800

const data = JSON.parse(
fs.readFileSync(

Check warning on line 24 in test/integration/Dispenser.test.ts

View workflow job for this annotation

GitHub Actions / test

Found fs.readFileSync with non literal argument at index 0

Check warning on line 24 in test/integration/Dispenser.test.ts

View workflow job for this annotation

GitHub Actions / test

Found fs.readFileSync with non literal argument at index 0
process.env.ADDRESS_FILE ||
`${homedir}/.ocean/ocean-contracts/artifacts/address.json`,
'utf8'
Expand Down Expand Up @@ -110,6 +110,7 @@
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()
Expand Down Expand Up @@ -163,7 +164,10 @@
)
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')
Expand Down Expand Up @@ -243,7 +247,10 @@
'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')
Expand Down
11 changes: 9 additions & 2 deletions test/integration/FixedRateExchange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
const sleepMs = 1700

const data = JSON.parse(
fs.readFileSync(

Check warning on line 24 in test/integration/FixedRateExchange.test.ts

View workflow job for this annotation

GitHub Actions / test

Found fs.readFileSync with non literal argument at index 0

Check warning on line 24 in test/integration/FixedRateExchange.test.ts

View workflow job for this annotation

GitHub Actions / test

Found fs.readFileSync with non literal argument at index 0
process.env.ADDRESS_FILE ||
`${homedir}/.ocean/ocean-contracts/artifacts/address.json`,
'utf8'
Expand Down Expand Up @@ -127,6 +127,7 @@
result.events.NewFixedRate.returnValues.exchangeId.toLowerCase()

fixedRateId = `${exchangeContract}-${exchangeId}`
const nftTemplate = await Factory.getNFTTemplate(nftParams.templateIndex)

// Check NFT values
await sleep(sleepMs)
Expand Down Expand Up @@ -176,7 +177,10 @@
)
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,
Expand Down Expand Up @@ -263,7 +267,10 @@
'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')
Expand Down
13 changes: 10 additions & 3 deletions test/integration/Nft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import { TransactionReceipt } from 'web3-core'

const data = JSON.parse(
fs.readFileSync(

Check warning on line 19 in test/integration/Nft.test.ts

View workflow job for this annotation

GitHub Actions / test

Found fs.readFileSync with non literal argument at index 0

Check warning on line 19 in test/integration/Nft.test.ts

View workflow job for this annotation

GitHub Actions / test

Found fs.readFileSync with non literal argument at index 0
process.env.ADDRESS_FILE ||
`${homedir}/.ocean/ocean-contracts/artifacts/address.json`,
'utf8'
Expand Down Expand Up @@ -66,6 +66,7 @@
}
]
}
let nftTemplate

describe('NFT tests', async () => {
const nftName = 'testNFT'
Expand Down Expand Up @@ -119,7 +120,7 @@
)
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()
Expand Down Expand Up @@ -167,7 +168,10 @@
)
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,
Expand Down Expand Up @@ -276,7 +280,10 @@
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'
Expand Down
Loading