The Graph uses

Create a submap on the official website

The Graph is mainly used to monitor data on the chain. The Graph official website

  1. create subgraph

Click to create a subgraph

Click to create a subgraph
2. Select the chain

Select the chain where you want to query the corresponding contract

insert image description here
3. Fill in the name of your own submap

insert image description here

Locally create subplot frames

Create a folder locally

insert image description here

Initialize the name of the subgraph, here is to create a folder named graph_5 and configure the basic file

insert image description here

Select the network you want to monitor, here demo choose ethereum

insert image description here

Enter the prompt, press Enter directly, and then fill in the contract address

insert image description here

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

insert image description here

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.

insert image description here

Here is the standard weth event and function template

insert image description here

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.

insert image description here

Here is to create a submap, which will automatically configure some files for you. A step that must be performed.

insert image description here

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.

insert image description here

Here, replace the file in the bottom corner with: /src/mapping.ts

insert image description here

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.

insert image description here

Then re-graph codegen && graph build
to generate the file, if no error is reported, the subgraph can be deployed

insert image description here

This is the success style

insert image description here

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.

insert image description here

This is a successful rendering

insert image description here
insert image description here

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)
})
}
						制作时间匆忙,有不足请提出。

Guess you like

Origin blog.csdn.net/qq_42671505/article/details/126469178