feat: collection and collectionOwner entities (#122)

* feat: add Collection and CollectionOwner entities. Handle CollectionOwner on CollectionRoleGranted and CollectionRoleRevoked events.

* feat: collection entity and its handler.
This commit is contained in:
Shredder 2023-02-11 00:45:27 +03:30 committed by GitHub
parent 2adb39641b
commit 1fe91a137b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 1 deletions

View File

@ -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!
}

View File

@ -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 {

View File

@ -30,6 +30,7 @@ dataSources:
- Token
- Owner
- Controller
- CollectionOwner
abis:
- name: FleekNFA
file: ../contracts/artifacts/contracts/FleekERC721.sol/FleekERC721.json