Subgraph
Introduction
Web3Task utilizes a Subgraph that can be deployed on the local network or executed in the front end to fetch data quickly and effectively.
Running subgraph locally
Prerequisites
Install dependencies
Guide to install docker and Nodejs
Install dependencies inside the web3task-contracts directory
web3task-contracts directorycd web3task-contracts
yarn
npm iNPM or Yarn to install the graph-cli
npm install @graphprotocol/graph-cli
or
yarn add @graphprotocol/graph-cli
Setting Up Local Blockchain
Run a Hardhat node:
npx hardhat nodeThe node will generate some accounts. You can realize they are already set at .env.sample, you should just let it be.
Add the first one to Metamask to be the leader and the second to be the member. The account will be:
0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
0x70997970C51812dc3A010C7d01b50e0d17dc79C8To add the localhost network to metamask, click on the network dropdown and select Custom RPC. Fill in the following fields:
Network Name:
localhostNew RPC URL:
http://localhost:8545Chain ID:
31337Currency Symbol:
ETH
Deploying Smart Contracts in the Localhost
Makefile will set everything for us, just run:
make mocksInstalling dependencies of subgraph
cd web3task-subgraph
npm iRunning docker
Go to the graph-node directory
cd subgraph/graph-nodeMake sure there is not any residual file remaining in the directory
rm -rf graph-node/data/start the docker
docker-compose upYou should see the log looking like this for the docker:
*graph-node-graph-node-1 |* Oct 15 04:44:57.420 INFO Downloading latest blocks from Ethereum, this may take a few minutes..., provider: localhost-rpc-0, component: EthereumPollingBlockIngestorAnd looking something like this for hardhat:
eth_blockNumber (2)
eth_getBlockByNumber (19)
eth_blockNumber (2)
eth_getBlockByNumber (14)Create a subgraph
cd web3task-subgraph
graph create --node http://localhost:8020/ subgraph/web3taskDeploy the subgraph
graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 subgraph/web3taskThe Graph Explorer
IMPORTANT
You must run the test scripts on the local network to generate events to check on The Graph Explorer with queries. In case that step is skipped, it won't be possible to fetch data from the subgraph.
Query Examples
Below are some sample queries you can use to gather information from the Web3Task contracts.
You can build your own queries using GraphQL Explorer to test it out and query exactly what you need.
Updated tasks
Retrieve all tasks that have been updated.
{
taskUpdateds {
id
taskId
status
blockNumber
transactionHash
}
}Querying Authorized Personnel for a Specific Role
Fetch all personnel authorized for a specific role.
{
authorizePersonnels(where: { roleId: 123 }) {
id
roleId
authorizedAddress
isAuthorized
blockNumber
transactionHash
}
}All approvals
Fetch all approvals.
{
approvals {
id
owner
approved
tokenId
blockNumber
blockTimestamp
transactionHash
}
}In case you update the smart contract with new events to be added
Cancel and clean the docker. In the
graph-nodefolder running the docker, presscrtl + cand typedocker-compose down, then delete the data folderrm -rf /dataUpdate the smart contract and compile it
Update the files
web3task-subgraph/networks.jsonand theweb3task-subgraph/subgraph.yamlwith the new addressCopy and paste the new ABI on
web3task-subgraph/abis/TasksManager.jsonUpdate the file
web3task-subgraph/schema.graphqlwith the new entitiesUpdate the file
web3task-subgraph/subgraph.ymalwith the new event handlersUpdate the file
web3task-subgraph/src/tasks-manager.tswith the new functions and importsRun the commands
graph codegenandgraph build --network localhostin theweb3task-subgraphfolder.Deploy the new version of the subgraph
graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 subgraph/web3task
Deploying to Tesnet and Mainnet
Access the studio https://thegraph.com/studio/, connect your wallet and create a subgraph
Give a name and select a network
Run the command line:
graph init --studio <your subgraph name>Choose the protocol you want to deploy the subgraph
Create the slug which will be the unique identifier your subgraph will be identified by
Create the name of the directory that will have the subgraph files
Choose the network you want to deploy the subgraph
Fill the address field with the smart contract deployed on the network
Set the path to the ABI file
Fill the block in which the contract was deployed to
Give the name of the contract
Press "Y" to index events as entities
Press "n" if you don't want to add another contract.
run the command
graph auth --studio <your subgraph deploy key>Go to the subgraph's directory
cd web3task-subgraphRun the commands
graph codegen && graph buildto generate the necessary filesRun the command
graph deploy --studio web3task-subgraphto deploy the subgraph
Use The Graph Studio to play around with the queries. You can use the query mentioned earlier to start.
Executing Subgraph in the frontend
Prerequisites
Node service.js
Install dependencies
Install dependencies inside the web3task-frontend directory
web3task-frontend directorycd web3task-frontend
yarn
or
npm iExecute the subgraph service
Node src/services/subgraph-queries.tsThis will execute an example query that will retrieve data from the Web3Task smart contract deployed to the Mumbai testnet.
To test other queries, you can follow the previous examples by editing the file web3task-frontend/src/services/subgraph-queries.ts
Last updated