Skip to content

Commit

Permalink
Merge pull request #181 from capsule-corp-ternoa/dev
Browse files Browse the repository at this point in the history
Implement CollectionOffchainDataSet event + creationBlokId added to RentEntity
  • Loading branch information
ipapandinas authored Apr 4, 2023
2 parents 1a6a944 + 2dcc986 commit a0c35a1
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 48 deletions.
7 changes: 6 additions & 1 deletion project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ dataSources:
filter:
module: marketplace
method: NFTSold
- handler: handleEvent
kind: substrate/EventHandler
filter:
module: nft
method: NFTAddedToCollection

# Secret NFT
- handler: handleEvent
Expand Down Expand Up @@ -238,7 +243,7 @@ dataSources:
kind: substrate/EventHandler
filter:
module: nft
method: NFTAddedToCollection
method: CollectionOffchainDataSet

# Marketplace
- handler: handleEvent
Expand Down
1 change: 1 addition & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ type RentEntity @entity {
renter: String! @index
rentee: String @index
startBlockId: Int
creationBlockId: Int!
durationType: String!
blockDuration: Int
maxSubscriptionBlockDuration: Int
Expand Down
96 changes: 49 additions & 47 deletions src/eventHandlers/nfts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,37 @@ export const nftCreatedHandler = async (event: SubstrateEvent): Promise<void> =>
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<void> => {
Expand Down Expand Up @@ -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<void> => {
Expand Down Expand Up @@ -190,6 +184,14 @@ export const nftCollectionLimitedHandler = async (event: SubstrateEvent): Promis
await record.save()
}

export const nftCollectionOffchainDataSetHandler = async (event: SubstrateEvent): Promise<void> => {
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<void> => {
const commonEventData = getCommonEventData(event)
if (!commonEventData.isSuccess) throw new Error("NFT add to collection error, extrinsic isSuccess : false")
Expand Down
1 change: 1 addition & 0 deletions src/eventHandlers/rents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export async function handleEvent(event: SubstrateEvent): Promise<void> {
case "nft.CollectionLimited":
await eventHandlers.nftCollectionLimitedHandler(event)
break
case "nft.CollectionOffchainDataSet":
await eventHandlers.nftCollectionOffchainDataSetHandler(event)
break
case "nft.NFTAddedToCollection":
await eventHandlers.nftAddedToCollectionHandler(event)
break
Expand Down
2 changes: 2 additions & 0 deletions src/types/models/RentEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export class RentEntity implements Entity {

public startBlockId?: number;

public creationBlockId: number;

public durationType: string;

public blockDuration?: number;
Expand Down

0 comments on commit a0c35a1

Please sign in to comment.