The Graph usage process
Create a submap on the official website
The Graph is mainly used to monitor data on the chain. The Graph official website
- create subgraph
Click to create a subgraph
2. Select the chain
Select the chain where you want to query the corresponding contract
3. Fill in the name of your own submap
Locally create subplot frames
Create a folder locally
Initialize the name of the subgraph, here is to create a folder named graph_5 and configure the basic file
Select the network you want to monitor, here demo choose ethereum
Enter the prompt, press Enter directly, and then fill in the contract address
If it fails here, download the abi file into json format, put it locally, and directly fill in the name of the abi file (note that the abi file is placed in the current directory). If the creation is successful here, cd
into the submap folder you want to deploy
Here you need to focus on understanding! This is what you want to query. For example, if you want to query the id, count, src, and guy parameters of an event, you can write it like this.
Here is the standard weth event and function template
This is also very important, it is the configuration list. It means that you need to check which events and methods occurred in the contract, including all block information about this contract.
Below eventHandlers are events, which are divided into event and handler. Handler is the method name written in the mapping.ts mapping file to be written in the next step. For example, handleApproval must correspond to a function name in mapping.ts.
Here is to create a submap, which will automatically configure some files for you. A step that must be performed.
It is normal to fail here, because the mapping.ts is not well written, but this step is also very important, and will help you generate an extra build folder, which contains many functions and events automatically generated based on this abi.json.
Here, replace the file in the bottom corner with: /src/mapping.ts
Write mapping.ts, the mapping file, this is to save the instance corresponding to the instance you set at that time, and the instance corresponding to scheme.graphql.
Then re-graph codegen && graph build
to generate the file, if no error is reported, the subgraph can be deployed
This is the success style
Execute the command graph deploy --studio graph_5 (change your own file name here)
to select the version first, this is set by yourself, it is for the iterative upgrade of your own subgraph in the future, it can be 0.0.2 or 0.1.2.
This is a successful rendering
Query through gql query statement
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>//引入axios
async function test_graph(){
axios.post('https://api.studio.thegraph.com/query/32121/graph_4/0.0.3', {
query: `
{
{
eventApprovals(first: 5) {
id
from
to
value
}
eventTransfers(first: 5) {
id
from
to
value
}
}
}
`
})
.then((res) => {
// for (const trans of res.data.data.transfers) {
console.log(res)
// }
})
.catch((error) => {
console.error(error)
})
}
制作时间匆忙,有不足请提出。