Skip to content

Commit a0c35a1

Browse files
authored
Merge pull request #181 from capsule-corp-ternoa/dev
Implement CollectionOffchainDataSet event + creationBlokId added to RentEntity
2 parents 1a6a944 + 2dcc986 commit a0c35a1

File tree

6 files changed

+62
-48
lines changed

6 files changed

+62
-48
lines changed

project.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ dataSources:
141141
filter:
142142
module: marketplace
143143
method: NFTSold
144+
- handler: handleEvent
145+
kind: substrate/EventHandler
146+
filter:
147+
module: nft
148+
method: NFTAddedToCollection
144149

145150
# Secret NFT
146151
- handler: handleEvent
@@ -238,7 +243,7 @@ dataSources:
238243
kind: substrate/EventHandler
239244
filter:
240245
module: nft
241-
method: NFTAddedToCollection
246+
method: CollectionOffchainDataSet
242247

243248
# Marketplace
244249
- handler: handleEvent

schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ type RentEntity @entity {
116116
renter: String! @index
117117
rentee: String @index
118118
startBlockId: Int
119+
creationBlockId: Int!
119120
durationType: String!
120121
blockDuration: Int
121122
maxSubscriptionBlockDuration: Int

src/eventHandlers/nfts.ts

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,37 @@ export const nftCreatedHandler = async (event: SubstrateEvent): Promise<void> =>
1212
const commonEventData = getCommonEventData(event)
1313
if (!commonEventData.isSuccess) throw new Error("NFT created error, extrinsic isSuccess : false")
1414
const [nftId, owner, offchainData, royalty, collectionId, isSoulbound, mintFee] = event.event.data
15-
let record = await NftEntity.get(nftId.toString())
16-
if (record === undefined) {
17-
record = new NftEntity(nftId.toString())
18-
record.nftId = nftId.toString()
19-
record.collectionId = collectionId?.toString() || null
20-
record.owner = owner.toString()
21-
record.creator = owner.toString()
22-
record.offchainData = formatString(offchainData.toString())
23-
record.royalty = Number(royalty.toString()) / 10000
24-
record.isCapsule = false
25-
record.isListed = false
26-
record.typeOfListing = null
27-
record.isSecret = false
28-
record.isRented = false
29-
record.isDelegated = false
30-
record.isSoulbound = isSoulbound.toString() === "true"
31-
record.isSecretSynced = false
32-
record.isCapsuleSynced = false
33-
record.isTransmission = false
34-
record.createdAt = commonEventData.timestamp
35-
record.updatedAt = commonEventData.timestamp
36-
record.timestampCreated = commonEventData.timestamp
37-
await record.save()
38-
if (record.collectionId) {
39-
let collectionRecord = await CollectionEntity.get(record.collectionId)
40-
if (collectionRecord === undefined) throw new Error("Collection where nft is added not found in db")
41-
collectionRecord.nfts.push(record.nftId)
42-
collectionRecord.nbNfts = collectionRecord.nbNfts + 1
43-
if (collectionRecord.nfts.length === collectionRecord.limit) collectionRecord.hasReachedLimit = true
44-
await collectionRecord.save()
45-
}
46-
await nftOperationEntityHandler(record, null, commonEventData, NFTOperation.Created, [mintFee.toString()])
47-
await genericTransferHandler(owner, "Treasury", mintFee, commonEventData)
15+
const record = new NftEntity(nftId.toString())
16+
record.nftId = nftId.toString()
17+
record.collectionId = collectionId?.toString() || null
18+
record.owner = owner.toString()
19+
record.creator = owner.toString()
20+
record.offchainData = formatString(offchainData.toString())
21+
record.royalty = Number(royalty.toString()) / 10000
22+
record.isCapsule = false
23+
record.isListed = false
24+
record.typeOfListing = null
25+
record.isSecret = false
26+
record.isRented = false
27+
record.isDelegated = false
28+
record.isSoulbound = isSoulbound.toString() === "true"
29+
record.isSecretSynced = false
30+
record.isCapsuleSynced = false
31+
record.isTransmission = false
32+
record.createdAt = commonEventData.timestamp
33+
record.updatedAt = commonEventData.timestamp
34+
record.timestampCreated = commonEventData.timestamp
35+
await record.save()
36+
if (record.collectionId) {
37+
let collectionRecord = await CollectionEntity.get(record.collectionId)
38+
if (collectionRecord === undefined) throw new Error("Collection where nft is added not found in db")
39+
const newLength = collectionRecord.nfts.push(record.nftId)
40+
collectionRecord.nbNfts = newLength
41+
if (newLength === collectionRecord.limit) collectionRecord.hasReachedLimit = true
42+
await collectionRecord.save()
4843
}
44+
await nftOperationEntityHandler(record, null, commonEventData, NFTOperation.Created, [mintFee.toString()])
45+
await genericTransferHandler(owner, "Treasury", mintFee, commonEventData)
4946
}
5047

5148
export const secretAddedToNFTHandler = async (event: SubstrateEvent): Promise<void> => {
@@ -141,20 +138,17 @@ export const nftCollectionCreatedHandler = async (event: SubstrateEvent): Promis
141138
const commonEventData = getCommonEventData(event)
142139
if (!commonEventData.isSuccess) throw new Error("NFT collection creation error, extrinsic isSuccess : false")
143140
const [collectionId, owner, offchainData, limit] = event.event.data
144-
let record = await CollectionEntity.get(collectionId.toString())
145-
if (record === undefined) {
146-
record = new CollectionEntity(collectionId.toString())
147-
record.owner = owner.toString()
148-
record.offchainData = formatString(offchainData.toString())
149-
record.collectionId = collectionId.toString()
150-
record.nfts = []
151-
record.nbNfts = 0
152-
record.hasReachedLimit = false
153-
record.isClosed = false
154-
record.limit = limit?.toString() ? Number(limit?.toString()) : null
155-
record.timestampCreated = commonEventData.timestamp
156-
await record.save()
157-
}
141+
const record = new CollectionEntity(collectionId.toString())
142+
record.owner = owner.toString()
143+
record.offchainData = formatString(offchainData.toString())
144+
record.collectionId = collectionId.toString()
145+
record.nfts = []
146+
record.nbNfts = 0
147+
record.hasReachedLimit = false
148+
record.isClosed = false
149+
record.limit = limit?.toString() ? Number(limit?.toString()) : null
150+
record.timestampCreated = commonEventData.timestamp
151+
await record.save()
158152
}
159153

160154
export const nftCollectionBurnedHandler = async (event: SubstrateEvent): Promise<void> => {
@@ -190,6 +184,14 @@ export const nftCollectionLimitedHandler = async (event: SubstrateEvent): Promis
190184
await record.save()
191185
}
192186

187+
export const nftCollectionOffchainDataSetHandler = async (event: SubstrateEvent): Promise<void> => {
188+
const [collectionId, offchainData] = event.event.data
189+
const record = await CollectionEntity.get(collectionId.toString())
190+
if (record === undefined) throw new Error("NFT collection to set offchain data not found in db")
191+
record.offchainData = formatString(offchainData.toString())
192+
await record.save()
193+
}
194+
193195
export const nftAddedToCollectionHandler = async (event: SubstrateEvent): Promise<void> => {
194196
const commonEventData = getCommonEventData(event)
195197
if (!commonEventData.isSuccess) throw new Error("NFT add to collection error, extrinsic isSuccess : false")

src/eventHandlers/rents.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const rentContractCreatedHandler = async (event: SubstrateEvent): Promise
3535

3636
let record = new RentEntity(`${commonEventData.extrinsicId}-${nftId.toString()}`)
3737
record.nftId = nftId.toString()
38+
record.creationBlockId = Number(commonEventData.blockId)
3839
record.hasStarted = false
3940
record.hasEnded = false
4041
record.hasBeenCanceled = false

src/mappings/mappingHandlers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ export async function handleEvent(event: SubstrateEvent): Promise<void> {
6565
case "nft.CollectionLimited":
6666
await eventHandlers.nftCollectionLimitedHandler(event)
6767
break
68+
case "nft.CollectionOffchainDataSet":
69+
await eventHandlers.nftCollectionOffchainDataSetHandler(event)
70+
break
6871
case "nft.NFTAddedToCollection":
6972
await eventHandlers.nftAddedToCollectionHandler(event)
7073
break

src/types/models/RentEntity.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export class RentEntity implements Entity {
3232

3333
public startBlockId?: number;
3434

35+
public creationBlockId: number;
36+
3537
public durationType: string;
3638

3739
public blockDuration?: number;

0 commit comments

Comments
 (0)