diff --git a/subgraph/schema.graphql b/subgraph/schema.graphql index 620c40d..4e9d9fd 100644 --- a/subgraph/schema.graphql +++ b/subgraph/schema.graphql @@ -148,3 +148,15 @@ type Controller @entity { id: Bytes! #address tokens: [Token!] @derivedFrom(field: "controllers") } +type Collection @entity { + id: Bytes! #address + deployer: Bytes! #address + transactionHash: Bytes! #transaction hash + owners: [CollectionOwner!] +} + +type CollectionOwner @entity { + id: Bytes! # address + accessGrantedBy: Bytes! #address + transactionHash: Bytes! +} diff --git a/subgraph/src/fleek-nfa.ts b/subgraph/src/fleek-nfa.ts index 91ab371..c7bebc9 100644 --- a/subgraph/src/fleek-nfa.ts +++ b/subgraph/src/fleek-nfa.ts @@ -1,4 +1,4 @@ -import { Address, Bytes, log } from '@graphprotocol/graph-ts'; +import { Address, Bytes, log, store, ethereum } from '@graphprotocol/graph-ts'; import { Approval as ApprovalEvent, ApprovalForAll as ApprovalForAllEvent, @@ -18,6 +18,8 @@ import { import { Approval, ApprovalForAll, + Collection, + CollectionOwner, CollectionRoleGranted, CollectionRoleRevoked, Controller, @@ -79,6 +81,52 @@ export function handleCollectionRoleGranted( entity.transactionHash = event.transaction.hash; entity.save(); + + if (event.params.role === 0) { + // Role 0 => Owner [Probably going to change this after the ACL refactor.] + // Should create a new CollectionOwner entity with the address from the parameters. + // If it already is a collection owner, should log a warning. + + let collectionOwner = CollectionOwner.load(event.params.toAddress); + + if (collectionOwner) { + // Collection Owner already exists. + // Print warning log message. + log.warning( + 'Although Address {} is already a collection owner, a CollectionRoleGranted event was emitted that indicated the address was granted the same role, again.', + [event.params.toAddress.toHexString()] + ); + } else { + // Create a new collection owner entity and assign the values + collectionOwner = new CollectionOwner(event.params.toAddress); + collectionOwner.accessGrantedBy = event.params.byAddress; + collectionOwner.transactionHash = event.transaction.hash; + + // Log the new CollectionOwner entity creation. + log.info('Created a new collection owner entity with address {}.', [ + event.params.toAddress.toHexString(), + ]); + + // Save the collection owner. + collectionOwner.save(); + } + + if (event.params.byAddress === event.params.toAddress) { + // This is the contract creation transaction. + log.warning('This is the contract creation transaction.', []); + if (event.receipt) { + let receipt = event.receipt as ethereum.TransactionReceipt; + log.warning('Contract address is: {}', [ + receipt.contractAddress.toHexString(), + ]); + let collection = new Collection(receipt.contractAddress); + collection.deployer = event.params.byAddress; + collection.transactionHash = event.transaction.hash; + collection.owners = [event.params.toAddress]; + collection.save(); + } + } + } } export function handleCollectionRoleRevoked( @@ -96,6 +144,13 @@ export function handleCollectionRoleRevoked( entity.transactionHash = event.transaction.hash; entity.save(); + + if (event.params.role === 0) { + // Role 0 => Owner [Probably going to change this after the ACL refactor.] + // Should remove the CollectionOwner entity. + + store.remove('CollectionOwner', event.params.toAddress.toHexString()); + } } export function handleNewBuild(event: NewBuildEvent): void { diff --git a/subgraph/subgraph.yaml b/subgraph/subgraph.yaml index 1bb4586..a0e6d50 100644 --- a/subgraph/subgraph.yaml +++ b/subgraph/subgraph.yaml @@ -30,6 +30,7 @@ dataSources: - Token - Owner - Controller + - CollectionOwner abis: - name: FleekNFA file: ../contracts/artifacts/contracts/FleekERC721.sol/FleekERC721.json