feat: add token and holder entities (#94)

* chore: add the verify command to the package.json file

* docs: add the re-deployment section to the README.md file

* feat: add support for token and holder entities based on transfer events that are emitted from the contract during the mint process.

---------

Co-authored-by: EmperorOrokuSaki <artie.eth@gmail.com>
This commit is contained in:
Janison Sivarajah 2023-01-31 10:43:23 -05:00 committed by GitHub
parent 91aeb47aef
commit ce1a3fc141
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 81 additions and 2 deletions

View File

@ -14,7 +14,8 @@
"compile": "hardhat compile", "compile": "hardhat compile",
"postinstall": "husky install", "postinstall": "husky install",
"prepack": "pinst --disable", "prepack": "pinst --disable",
"postpack": "pinst --enable" "postpack": "pinst --enable",
"verify:mumbai": "npx hardhat run ./scripts/verify-polyscan.js --network mumbai"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -7,7 +7,7 @@ async function main() {
await hre.run('verify:verify', { await hre.run('verify:verify', {
address: address, address: address,
constructorArguments: [ constructorArguments: [
'FleekNFAs', // Collection name 'FleekNFA', // Collection name
'FLKNFA', // Collection symbol 'FLKNFA', // Collection symbol
], ],
}); });

View File

@ -86,6 +86,47 @@ The `graph-cli` command to init a subgraph is included below, please make sure y
`graph init --contract-name CONTRACT_NAME --index-events --studio --from-contract CONTRACT_ADDRESS --abi PATH/TO/ABI/JSON --network mumbai SUBGRAPH_NAME` `graph init --contract-name CONTRACT_NAME --index-events --studio --from-contract CONTRACT_ADDRESS --abi PATH/TO/ABI/JSON --network mumbai SUBGRAPH_NAME`
## Re-deployment
If a change is needed in the subgraph, you should update the `schema.graphql` and `subgraph.yaml` files to match the new changes that have happened on the contract side.
A typical re-deployment consists of a new contract address, ABI, and finally entities.
### Updating the addresses
To update the address of the contract, the following files need to be changed:
- [networks.json](./networks.json): update the address for the right blockchain network
- [subgraph.yaml](./subgraph.yaml): update the address in `dataSources.source.address`
### Updating the entities and handlers
It is important to make sure the subgraph is going to support the new version of the entities by updating the `schema.graphql` file.
Define the new entities and the relationships between them. Also, take care of entities that should be removed from the schema.
If your contract is emitting new events, update the `subgraph.yaml` file's `dataSources.mapping.eventHandlers`. Remove events that are not a part of the new contract.
### Re-generating the TS files
To re-generate files in `generated/` you can simply run `yarn codegen` or `npm run codegen` only and only **after** the previous two steps. If you do it before, you will still need to redo it later.
### Update the handlers
If you need new handlers in your code, you should update the `./src/fleek-nfa.ts` file and create functions to handle new events.
**NOTE: The name of your handler functions should be exactly the same as the name you have specified in the `subgraph.yaml` file for the target event.**
### Generate a new build
Finally, you can generate the build that is going to be deployed to the Hosted Service by running `yarn build` or `npm run build`.
### Re-deployment
The command that should be used for re-deployment purposes is no different than the one that is used to deploy subgraphs in the first place (remember to replace the access token and the github_username/subgraph_name parts):
`graph deploy --product hosted-service --deploy-key YOUR_ACCESS_TOKEN --version-lable v0.0.1 YOUR_GITHUB_USERNAME/SUBGRAPH_NAME_ON_HOSTED_SERVICE`
## Testing ## Testing
You can run the unit tests found in `./tests/` with `yarn test` or `npm run test`. You can run the unit tests found in `./tests/` with `yarn test` or `npm run test`.

View File

@ -129,3 +129,15 @@ type Transfer @entity(immutable: true) {
blockTimestamp: BigInt! blockTimestamp: BigInt!
transactionHash: Bytes! transactionHash: Bytes!
} }
type Token @entity {
id: Bytes! #transaction hash
tokenId: BigInt! # uint256
owner: Holder!
minted_by: Bytes!
}
type Holder @entity {
id: Bytes! #address
tokens: [Token!] @derivedFrom(field: "owner")
}

View File

@ -1,8 +1,10 @@
import { Address, log } from '@graphprotocol/graph-ts';
import { import {
Approval as ApprovalEvent, Approval as ApprovalEvent,
ApprovalForAll as ApprovalForAllEvent, ApprovalForAll as ApprovalForAllEvent,
CollectionRoleGranted as CollectionRoleGrantedEvent, CollectionRoleGranted as CollectionRoleGrantedEvent,
CollectionRoleRevoked as CollectionRoleRevokedEvent, CollectionRoleRevoked as CollectionRoleRevokedEvent,
FleekNFA,
NewBuild as NewBuildEvent, NewBuild as NewBuildEvent,
NewTokenDescription as NewTokenDescriptionEvent, NewTokenDescription as NewTokenDescriptionEvent,
NewTokenENS as NewTokenENSEvent, NewTokenENS as NewTokenENSEvent,
@ -232,4 +234,26 @@ export function handleTransfer(event: TransferEvent): void {
entity.transactionHash = event.transaction.hash; entity.transactionHash = event.transaction.hash;
entity.save(); entity.save();
if (parseInt(event.params.from.toHexString()) === 0) {
// This is a new mint
let id = event.transaction.hash;
let token = new Token(id);
let owner = event.params.to;
let holder = Holder.load(owner);
if (!holder) {
// Create a new holder entity
holder = new Holder(owner);
}
token.owner = owner;
token.minted_by = event.transaction.from;
token.tokenId = event.params.tokenId;
holder.save();
token.save();
}
} }

View File

@ -8,6 +8,7 @@ dataSources:
source: source:
address: "0x34F21E970A7cd383eE429aDB5ed57bbc40ea2B57" address: "0x34F21E970A7cd383eE429aDB5ed57bbc40ea2B57"
abi: FleekNFA abi: FleekNFA
startBlock: 30825341
mapping: mapping:
kind: ethereum/events kind: ethereum/events
apiVersion: 0.0.7 apiVersion: 0.0.7