From ce1a3fc141e29f8e1d00a654e156c4982d7711bf Mon Sep 17 00:00:00 2001 From: Janison Sivarajah Date: Tue, 31 Jan 2023 10:43:23 -0500 Subject: [PATCH] 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 --- package.json | 3 ++- scripts/verify-polyscan.js | 2 +- subgraph/README.md | 41 ++++++++++++++++++++++++++++++++++++++ subgraph/schema.graphql | 12 +++++++++++ subgraph/src/fleek-nfa.ts | 24 ++++++++++++++++++++++ subgraph/subgraph.yaml | 1 + 6 files changed, 81 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 8d03daf..732dffa 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "compile": "hardhat compile", "postinstall": "husky install", "prepack": "pinst --disable", - "postpack": "pinst --enable" + "postpack": "pinst --enable", + "verify:mumbai": "npx hardhat run ./scripts/verify-polyscan.js --network mumbai" }, "repository": { "type": "git", diff --git a/scripts/verify-polyscan.js b/scripts/verify-polyscan.js index b5bc181..b5325b7 100644 --- a/scripts/verify-polyscan.js +++ b/scripts/verify-polyscan.js @@ -7,7 +7,7 @@ async function main() { await hre.run('verify:verify', { address: address, constructorArguments: [ - 'FleekNFAs', // Collection name + 'FleekNFA', // Collection name 'FLKNFA', // Collection symbol ], }); diff --git a/subgraph/README.md b/subgraph/README.md index 7a46156..d1ec3f2 100644 --- a/subgraph/README.md +++ b/subgraph/README.md @@ -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` +## 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 You can run the unit tests found in `./tests/` with `yarn test` or `npm run test`. \ No newline at end of file diff --git a/subgraph/schema.graphql b/subgraph/schema.graphql index 0933645..8f756cc 100644 --- a/subgraph/schema.graphql +++ b/subgraph/schema.graphql @@ -129,3 +129,15 @@ type Transfer @entity(immutable: true) { blockTimestamp: BigInt! 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") +} diff --git a/subgraph/src/fleek-nfa.ts b/subgraph/src/fleek-nfa.ts index ff62fcc..85931f8 100644 --- a/subgraph/src/fleek-nfa.ts +++ b/subgraph/src/fleek-nfa.ts @@ -1,8 +1,10 @@ +import { Address, log } from '@graphprotocol/graph-ts'; import { Approval as ApprovalEvent, ApprovalForAll as ApprovalForAllEvent, CollectionRoleGranted as CollectionRoleGrantedEvent, CollectionRoleRevoked as CollectionRoleRevokedEvent, + FleekNFA, NewBuild as NewBuildEvent, NewTokenDescription as NewTokenDescriptionEvent, NewTokenENS as NewTokenENSEvent, @@ -232,4 +234,26 @@ export function handleTransfer(event: TransferEvent): void { entity.transactionHash = event.transaction.hash; 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(); + } } diff --git a/subgraph/subgraph.yaml b/subgraph/subgraph.yaml index 155b014..f4cbebf 100644 --- a/subgraph/subgraph.yaml +++ b/subgraph/subgraph.yaml @@ -8,6 +8,7 @@ dataSources: source: address: "0x34F21E970A7cd383eE429aDB5ed57bbc40ea2B57" abi: FleekNFA + startBlock: 30825341 mapping: kind: ethereum/events apiVersion: 0.0.7