Skip to content

Commit 299d196

Browse files
authored
Template id detection (#705)
* handle templateIds * lint * fixes * fixes
1 parent fa58272 commit 299d196

8 files changed

+97
-33
lines changed

schema.graphql

+15-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type Token @entity {
3030
publishMarketFeeAmount: BigDecimal
3131

3232
"template ID of the datatoken"
33-
templateId: Int
33+
templateId: BigInt
3434

3535
"number of addresses holding a balance of datatoken , TODO: can we actually calculate this? what happens when users trade the dts"
3636
holderCount: BigInt!
@@ -630,3 +630,17 @@ type NftTransferHistory @entity {
630630
timestamp: Int!
631631
block: Int!
632632
}
633+
634+
type Erc721Template @entity {
635+
#ID = template address
636+
id: ID!
637+
templateId: BigInt!
638+
}
639+
640+
type Erc20Template @entity {
641+
#ID = template address
642+
id: ID!
643+
templateId: BigInt!
644+
}
645+
646+

src/mappings/erc721Factory.ts

+25-21
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import {
22
NFTCreated,
33
TokenCreated,
4-
ERC721Factory
4+
Template721Added,
5+
Template20Added
56
} from '../@types/ERC721Factory/ERC721Factory'
7+
import { Erc721Template, Erc20Template } from '../@types/schema'
68
import { decimal } from './utils/constants'
79
import { weiToDecimal } from './utils/generic'
810

911
import { getUser } from './utils/userUtils'
10-
import { getToken, getNftToken } from './utils/tokenUtils'
12+
import { getToken, getNftToken, getErc20TemplateId } from './utils/tokenUtils'
1113
import { addDatatoken } from './utils/globalUtils'
12-
import { BigInt } from '@graphprotocol/graph-ts'
1314

1415
export function handleNftCreated(event: NFTCreated): void {
1516
// const nft = new Nft(event.params.newTokenAddress.toHexString())
@@ -27,6 +28,7 @@ export function handleNftCreated(event: NFTCreated): void {
2728
nft.block = event.block.number.toI32()
2829
nft.eventIndex = event.logIndex.toI32()
2930
nft.transferable = event.params.transferable
31+
nft.template = event.params.templateAddress.toHexString()
3032

3133
nft.save()
3234
}
@@ -49,25 +51,27 @@ export function handleNewToken(event: TokenCreated): void {
4951
token.decimals = 18
5052
token.supply = decimal.ZERO
5153
token.cap = weiToDecimal(event.params.cap.toBigDecimal(), 18)
52-
const eventTemplateAddress = event.params.templateAddress
53-
.toHexString()
54-
.toLowerCase()
55-
const contract = ERC721Factory.bind(event.address)
56-
const templateCount = contract.try_getCurrentTemplateCount()
57-
if (templateCount.reverted) return
58-
const templateCountNum = templateCount.value.toI32()
54+
token.templateId = getErc20TemplateId(event.params.templateAddress)
55+
token.save()
56+
addDatatoken()
57+
}
5958

60-
for (let i = 0; i < templateCountNum; i++) {
61-
const template = contract.try_getTokenTemplate(BigInt.fromI32(1 + i))
62-
if (template.reverted) return
63-
const templateAddress = template.value.templateAddress
64-
.toHexString()
65-
.toLowerCase()
66-
if (templateAddress == eventTemplateAddress) {
67-
token.templateId = 1 + i
68-
}
59+
export function handleNew721Template(event: Template721Added): void {
60+
let template = Erc721Template.load(
61+
event.params._templateAddress.toHexString()
62+
)
63+
if (template === null) {
64+
template = new Erc721Template(event.params._templateAddress.toHexString())
65+
template.templateId = event.params.nftTemplateCount
66+
template.save()
6967
}
68+
}
7069

71-
token.save()
72-
addDatatoken()
70+
export function handleNew20Template(event: Template20Added): void {
71+
let template = Erc20Template.load(event.params._templateAddress.toHexString())
72+
if (template === null) {
73+
template = new Erc20Template(event.params._templateAddress.toHexString())
74+
template.templateId = event.params.nftTemplateCount
75+
template.save()
76+
}
7377
}

src/mappings/utils/tokenUtils.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Address, log, BigDecimal, BigInt } from '@graphprotocol/graph-ts'
2-
import { Nft, Token } from '../../@types/schema'
2+
import { Nft, Token, Erc721Template, Erc20Template } from '../../@types/schema'
33
import { ERC20 } from '../../@types/templates/ERC20Template/ERC20'
44
import { ERC20Template, ERC721Template } from '../../@types/templates'
55
import { addNft } from './globalUtils'
@@ -109,3 +109,19 @@ export function getUSDValue(
109109
): BigDecimal {
110110
return BigDecimal.zero()
111111
}
112+
113+
export function getErc721TemplateId(address: Address): BigInt {
114+
const template = Erc721Template.load(address.toHexString())
115+
if (template) {
116+
return template.templateId
117+
}
118+
return BigInt.zero()
119+
}
120+
121+
export function getErc20TemplateId(address: Address): BigInt {
122+
const template = Erc20Template.load(address.toHexString())
123+
if (template) {
124+
return template.templateId
125+
}
126+
return BigInt.zero()
127+
}

subgraph.template.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ dataSources:
198198
handler: handleNftCreated
199199
- event: TokenCreated(indexed address,indexed address,string,string,uint256,address)
200200
handler: handleNewToken
201+
- event: Template721Added(indexed address,indexed uint256)
202+
handler: handleNew721Template
203+
- event: Template20Added(indexed address,indexed uint256)
204+
handler: handleNew20Template
201205

202206
- kind: ethereum/contract
203207
name: FactoryRouter

test/integration/Datatoken.test.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,10 @@ describe('Datatoken tests', async () => {
197197
dt.publishMarketFeeAmount === publishMarketFeeAmount,
198198
'incorrect value for: publishMarketFeeAmount'
199199
)
200-
201-
assert(dt.templateId === templateIndex, 'incorrect value for: templateId')
200+
assert(
201+
parseInt(dt.templateId) === templateIndex,
202+
'incorrect value for: templateId'
203+
)
202204
assert(dt.holderCount === '0', 'incorrect value for: holderCount')
203205
assert(dt.orderCount === '0', 'incorrect value for: orderCount')
204206
assert(dt.orders, 'incorrect value for: orders')
@@ -314,7 +316,10 @@ describe('Datatoken tests', async () => {
314316
dt.publishMarketFeeAmount === publishMarketFeeAmount,
315317
'incorrect value for: publishMarketFeeAmount'
316318
)
317-
assert(dt.templateId === templateIndex, 'incorrect value for: templateId')
319+
assert(
320+
parseInt(dt.templateId) === templateIndex,
321+
'incorrect value for: templateId'
322+
)
318323
assert(dt.holderCount === '0', 'incorrect value for: holderCount')
319324
assert(dt.orderCount === '0', 'incorrect value for: orderCount')
320325
assert(dt.orders, 'incorrect value for: orders')

test/integration/Dispenser.test.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ describe('Dispenser tests', async () => {
110110
nftParams,
111111
erc20Params
112112
)
113+
const nftTemplate = await Factory.getNFTTemplate(nftParams.templateIndex)
113114
assert(tx.events.NFTCreated.event === 'NFTCreated')
114115
assert(tx.events.TokenCreated.event === 'TokenCreated')
115116
nftAddress = tx.events.NFTCreated.returnValues.newTokenAddress.toLowerCase()
@@ -163,7 +164,10 @@ describe('Dispenser tests', async () => {
163164
)
164165
assert(nft.storeUpdateRole === null, 'incorrect value for: storeUpdateRole')
165166
assert(nft.metadataRole === null, 'incorrect value for: metadataRole')
166-
assert(nft.template === '', 'incorrect value for: template')
167+
assert(
168+
nft.template === nftTemplate.templateAddress.toLowerCase(),
169+
'incorrect value for: template'
170+
)
167171
assert(nft.transferable === true, 'incorrect value for: transferable')
168172
assert(nft.createdTimestamp >= time, 'incorrect value: createdTimestamp')
169173
assert(nft.createdTimestamp < time + 5, 'incorrect value: createdTimestamp')
@@ -243,7 +247,10 @@ describe('Dispenser tests', async () => {
243247
'incorrect value for: publishMarketFeeAmount'
244248
)
245249

246-
assert(dt.templateId === templateIndex, 'incorrect value for: templateId')
250+
assert(
251+
parseInt(dt.templateId) === templateIndex,
252+
'incorrect value for: templateId'
253+
)
247254
assert(dt.holderCount === '0', 'incorrect value for: holderCount')
248255
assert(dt.orderCount === '0', 'incorrect value for: orderCount')
249256
assert(dt.orders, 'incorrect value for: orders')

test/integration/FixedRateExchange.test.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ describe('Fixed Rate Exchange tests', async () => {
127127
result.events.NewFixedRate.returnValues.exchangeId.toLowerCase()
128128

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

131132
// Check NFT values
132133
await sleep(sleepMs)
@@ -176,7 +177,10 @@ describe('Fixed Rate Exchange tests', async () => {
176177
)
177178
assert(nft.storeUpdateRole === null, 'incorrect value for: storeUpdateRole')
178179
assert(nft.metadataRole === null, 'incorrect value for: metadataRole')
179-
assert(nft.template === '', 'incorrect value for: template')
180+
assert(
181+
nft.template === nftTemplate.templateAddress.toLowerCase(),
182+
'incorrect value for: template'
183+
)
180184
assert(nft.transferable === true, 'incorrect value for: transferable')
181185
assert(
182186
nft.createdTimestamp >= time,
@@ -263,7 +267,10 @@ describe('Fixed Rate Exchange tests', async () => {
263267
'incorrect value for: publishMarketFeeAmount'
264268
)
265269

266-
assert(dt.templateId === templateIndex, 'incorrect value for: templateId')
270+
assert(
271+
parseInt(dt.templateId) === templateIndex,
272+
'incorrect value for: templateId'
273+
)
267274
assert(dt.holderCount === '0', 'incorrect value for: holderCount')
268275
assert(dt.orderCount === '0', 'incorrect value for: orderCount')
269276
assert(dt.orders, 'incorrect value for: orders')

test/integration/Nft.test.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const ddo = {
6666
}
6767
]
6868
}
69+
let nftTemplate
6970

7071
describe('NFT tests', async () => {
7172
const nftName = 'testNFT'
@@ -119,7 +120,7 @@ describe('NFT tests', async () => {
119120
)
120121
erc721Address = result.events.NFTCreated.returnValues[0]
121122
datatokenAddress = result.events.TokenCreated.returnValues[0]
122-
123+
nftTemplate = await Factory.getNFTTemplate(nftParams.templateIndex)
123124
// Check values before updating metadata
124125
await sleep(3000)
125126
nftAddress = erc721Address.toLowerCase()
@@ -167,7 +168,10 @@ describe('NFT tests', async () => {
167168
)
168169
assert(nft.storeUpdateRole === null, 'incorrect value for: storeUpdateRole')
169170
assert(nft.metadataRole === null, 'incorrect value for: metadataRole')
170-
assert(nft.template === '', 'incorrect value for: template')
171+
assert(
172+
nft.template === nftTemplate.templateAddress.toLowerCase(),
173+
'incorrect value for: template'
174+
)
171175
assert(nft.transferable === true, 'incorrect value for: transferable')
172176
assert(
173177
nft.createdTimestamp >= time,
@@ -276,7 +280,10 @@ describe('NFT tests', async () => {
276280
updatedNft.metadataRole === null,
277281
'incorrect value for: metadataRole'
278282
)
279-
assert(updatedNft.template === '', 'incorrect value for: template')
283+
assert(
284+
updatedNft.template === nftTemplate.templateAddress.toLowerCase(),
285+
'incorrect value for: template'
286+
)
280287
assert(
281288
updatedNft.transferable === true,
282289
'incorrect value for: transferable'

0 commit comments

Comments
 (0)