diff --git a/project.yaml b/project.yaml index 98217f1..ce0e05f 100644 --- a/project.yaml +++ b/project.yaml @@ -141,6 +141,11 @@ dataSources: filter: module: marketplace method: NFTSold + - handler: handleEvent + kind: substrate/EventHandler + filter: + module: nft + method: NFTAddedToCollection # Secret NFT - handler: handleEvent @@ -238,7 +243,7 @@ dataSources: kind: substrate/EventHandler filter: module: nft - method: NFTAddedToCollection + method: CollectionOffchainDataSet # Marketplace - handler: handleEvent diff --git a/schema.graphql b/schema.graphql index 4129692..3231930 100644 --- a/schema.graphql +++ b/schema.graphql @@ -116,6 +116,7 @@ type RentEntity @entity { renter: String! @index rentee: String @index startBlockId: Int + creationBlockId: Int! durationType: String! blockDuration: Int maxSubscriptionBlockDuration: Int diff --git a/src/eventHandlers/nfts.ts b/src/eventHandlers/nfts.ts index 324810e..aa0e9e1 100644 --- a/src/eventHandlers/nfts.ts +++ b/src/eventHandlers/nfts.ts @@ -12,40 +12,37 @@ export const nftCreatedHandler = async (event: SubstrateEvent): Promise => const commonEventData = getCommonEventData(event) if (!commonEventData.isSuccess) throw new Error("NFT created error, extrinsic isSuccess : false") const [nftId, owner, offchainData, royalty, collectionId, isSoulbound, mintFee] = event.event.data - let record = await NftEntity.get(nftId.toString()) - if (record === undefined) { - record = new NftEntity(nftId.toString()) - record.nftId = nftId.toString() - record.collectionId = collectionId?.toString() || null - record.owner = owner.toString() - record.creator = owner.toString() - record.offchainData = formatString(offchainData.toString()) - record.royalty = Number(royalty.toString()) / 10000 - record.isCapsule = false - record.isListed = false - record.typeOfListing = null - record.isSecret = false - record.isRented = false - record.isDelegated = false - record.isSoulbound = isSoulbound.toString() === "true" - record.isSecretSynced = false - record.isCapsuleSynced = false - record.isTransmission = false - record.createdAt = commonEventData.timestamp - record.updatedAt = commonEventData.timestamp - record.timestampCreated = commonEventData.timestamp - await record.save() - if (record.collectionId) { - let collectionRecord = await CollectionEntity.get(record.collectionId) - if (collectionRecord === undefined) throw new Error("Collection where nft is added not found in db") - collectionRecord.nfts.push(record.nftId) - collectionRecord.nbNfts = collectionRecord.nbNfts + 1 - if (collectionRecord.nfts.length === collectionRecord.limit) collectionRecord.hasReachedLimit = true - await collectionRecord.save() - } - await nftOperationEntityHandler(record, null, commonEventData, NFTOperation.Created, [mintFee.toString()]) - await genericTransferHandler(owner, "Treasury", mintFee, commonEventData) + const record = new NftEntity(nftId.toString()) + record.nftId = nftId.toString() + record.collectionId = collectionId?.toString() || null + record.owner = owner.toString() + record.creator = owner.toString() + record.offchainData = formatString(offchainData.toString()) + record.royalty = Number(royalty.toString()) / 10000 + record.isCapsule = false + record.isListed = false + record.typeOfListing = null + record.isSecret = false + record.isRented = false + record.isDelegated = false + record.isSoulbound = isSoulbound.toString() === "true" + record.isSecretSynced = false + record.isCapsuleSynced = false + record.isTransmission = false + record.createdAt = commonEventData.timestamp + record.updatedAt = commonEventData.timestamp + record.timestampCreated = commonEventData.timestamp + await record.save() + if (record.collectionId) { + let collectionRecord = await CollectionEntity.get(record.collectionId) + if (collectionRecord === undefined) throw new Error("Collection where nft is added not found in db") + const newLength = collectionRecord.nfts.push(record.nftId) + collectionRecord.nbNfts = newLength + if (newLength === collectionRecord.limit) collectionRecord.hasReachedLimit = true + await collectionRecord.save() } + await nftOperationEntityHandler(record, null, commonEventData, NFTOperation.Created, [mintFee.toString()]) + await genericTransferHandler(owner, "Treasury", mintFee, commonEventData) } export const secretAddedToNFTHandler = async (event: SubstrateEvent): Promise => { @@ -141,20 +138,17 @@ export const nftCollectionCreatedHandler = async (event: SubstrateEvent): Promis const commonEventData = getCommonEventData(event) if (!commonEventData.isSuccess) throw new Error("NFT collection creation error, extrinsic isSuccess : false") const [collectionId, owner, offchainData, limit] = event.event.data - let record = await CollectionEntity.get(collectionId.toString()) - if (record === undefined) { - record = new CollectionEntity(collectionId.toString()) - record.owner = owner.toString() - record.offchainData = formatString(offchainData.toString()) - record.collectionId = collectionId.toString() - record.nfts = [] - record.nbNfts = 0 - record.hasReachedLimit = false - record.isClosed = false - record.limit = limit?.toString() ? Number(limit?.toString()) : null - record.timestampCreated = commonEventData.timestamp - await record.save() - } + const record = new CollectionEntity(collectionId.toString()) + record.owner = owner.toString() + record.offchainData = formatString(offchainData.toString()) + record.collectionId = collectionId.toString() + record.nfts = [] + record.nbNfts = 0 + record.hasReachedLimit = false + record.isClosed = false + record.limit = limit?.toString() ? Number(limit?.toString()) : null + record.timestampCreated = commonEventData.timestamp + await record.save() } export const nftCollectionBurnedHandler = async (event: SubstrateEvent): Promise => { @@ -190,6 +184,14 @@ export const nftCollectionLimitedHandler = async (event: SubstrateEvent): Promis await record.save() } +export const nftCollectionOffchainDataSetHandler = async (event: SubstrateEvent): Promise => { + const [collectionId, offchainData] = event.event.data + const record = await CollectionEntity.get(collectionId.toString()) + if (record === undefined) throw new Error("NFT collection to set offchain data not found in db") + record.offchainData = formatString(offchainData.toString()) + await record.save() +} + export const nftAddedToCollectionHandler = async (event: SubstrateEvent): Promise => { const commonEventData = getCommonEventData(event) if (!commonEventData.isSuccess) throw new Error("NFT add to collection error, extrinsic isSuccess : false") diff --git a/src/eventHandlers/rents.ts b/src/eventHandlers/rents.ts index 98f046f..0f8b4f4 100644 --- a/src/eventHandlers/rents.ts +++ b/src/eventHandlers/rents.ts @@ -35,6 +35,7 @@ export const rentContractCreatedHandler = async (event: SubstrateEvent): Promise let record = new RentEntity(`${commonEventData.extrinsicId}-${nftId.toString()}`) record.nftId = nftId.toString() + record.creationBlockId = Number(commonEventData.blockId) record.hasStarted = false record.hasEnded = false record.hasBeenCanceled = false diff --git a/src/mappings/mappingHandlers.ts b/src/mappings/mappingHandlers.ts index 75bfbcb..3ef2307 100644 --- a/src/mappings/mappingHandlers.ts +++ b/src/mappings/mappingHandlers.ts @@ -65,6 +65,9 @@ export async function handleEvent(event: SubstrateEvent): Promise { case "nft.CollectionLimited": await eventHandlers.nftCollectionLimitedHandler(event) break + case "nft.CollectionOffchainDataSet": + await eventHandlers.nftCollectionOffchainDataSetHandler(event) + break case "nft.NFTAddedToCollection": await eventHandlers.nftAddedToCollectionHandler(event) break diff --git a/src/types/models/RentEntity.ts b/src/types/models/RentEntity.ts index 4d73377..0c5bff4 100644 --- a/src/types/models/RentEntity.ts +++ b/src/types/models/RentEntity.ts @@ -32,6 +32,8 @@ export class RentEntity implements Entity { public startBlockId?: number; + public creationBlockId: number; + public durationType: string; public blockDuration?: number;