Using The Graph on REI Network
Checking Prerequisites
To use Graph on REI, you should run a local Graph Node against REI testnet and point your Subgraph to it. From the link below you can get information on how to run a nodeThe Bank Contract
Running a Graph Node on REI NetworkFor this example, a simple Bank contract will be used. You can find the Solidity file in the
REIBank-subgraph repository
The contract contains a bank where users can deposit rei for themselves, or transfer their rei in bank to others. They can also withdraw their rei from bank.
The main functions of the contract are the following:
deposit() — function to send rei to the contract, then you can get your own proof of deposit in the bank
transfer(address to, uint256 amount) — transfer your rei in bank to others
whithdraw(unit256 amount) — redeem your money based on certificates of deposit
Events of the Bank Contract
The Graph uses the events emitted by the contract to index data. The bank contract emits three events:
Deposit(address indexed from, uint256 indexed amount) — in the deposit function. It provides information related to deposit entry, including account address and deposit rei amount
Transfer(address indexed from, address indexed to, uint256 indexed amount) — in the transfer function. It provides information on transaction between accounts in the bank, including accounts address and rei amount
Withdraw(address indexed from, uint256 indexed amount) — in the withdraw function. It provides information related to withdraw entry, including account address and withdraw rei amount
Creating a Subgraph
This section goes through the process of creating a Subgraph. For the Bank Subgraph, a GitHub repository was prepared with everything you need to help you get started. The repository also includes the Bank contract, as well as a Hardhat configuration file and deployment script. If you are not familiar with it, you can check our Hardhat integration guide to learn about the configuration file and how to deploy a contract using Hardhat.
To get started, first clone the repository and install the dependencies
Now, you can create the TypeScript types for the Graph by running
The types will in the src/types/
Creating the types requires you to have the ABI files specified in the subgraph.yaml
file. This sample repository has the file already, but this is usually obtained after compiling the contract.
You also can use following command to specify the generated directory
For this example, the contract was deployed to 0x1Ec9238A1c0adca222251e1f607a77237E8686a3
. The README.md
file in the project has the steps necessary to compile and deploy the contract if required.
Subgraphs Core Structure
In general terms, Subgraphs define the data that The Graph will index from the blockchain and the way it is stored. The subgraph definition consists of a few files:
subgraph.yaml — is a YAML file that contains the Subgraph's manifest
schema.graphql — a GraphQL schema that defines what data is stored for your subgraph, and how to query it via GraphQL
AssemblyScript mappings — AssemblyScript code that translates from the event data to the entities defined in your schema (e.g.
bank.ts
in this tutorial)
Schema.graphql
The schema for your subgraph is in the file schema.graphql
. GraphQL schemas are defined using the GraphQL interface definition language. If you've never written a GraphQL schema, it is recommended that you check out this primer on the GraphQL type system. Reference documentation for GraphQL schemas can be found in the GraphQL API section.For this example, here one entry is defined for bank:
Account — Record the users in bank, about their account address and balance
So the schema.graphql
should look like the following snippet:
Subgraph Manifest
The subgraph manifest subgraph.yaml
defines the smart contracts your subgraph indexes, which events from these contracts to pay attention to, and how to map event data to entities that Graph Node stores and allows to query. The full specification for subgraph manifests can be found here.
For the example subgraph, subgraph.yaml
is:
Some of the most important parameters in the subgraph.yaml
file are:
repository — Github repository of the subgraph
schema/file — location of the
schema.graphql
filedataSources/name — the name of the Subgraph
network — refers to the network name. This value must be set to the local graph node name which you setted
dataSources/source/address — the address of the contract
dataSources/source/abi — refers to where the interface of the contract is stored inside the
types
folder created with thecodegen
commanddataSources/source/startBlock — refers to the start block from which the indexing will start
dataSources/mapping/file — refers to the location of the mapping file, eg
bank.ts
dataSources/mapping/entities — definitions of the entities in the
schema.graphql
filedataSources/abis/name — where the interface of the contract is stored inside the
types/dataSources/name
dataSources/abis/file — refers to the location where the
.json
file with the contract's ABI is storeddataSources/eventHandlers — no value needs to be defined here, but this section refers to all the events that The Graph will index
dataSources/eventHandlers/event — refers to the structure of an event to be tracked inside the contract. You need to provide the event name and its type of variables
dataSources/eventHandlers/handler — refers to the name of the function inside the
mapping.ts
file which handles the event data
In short, the subgraph.yaml
should look like the following snippet:
Mappings
The mappings transform the Ethereum data your mappings are sourcing into entities defined in your schema. Mappings are written in a subset of TypeScript called AssemblyScript which can be compiled to WASM (WebAssembly). AssemblyScript is stricter than normal TypeScript, yet provides a familiar syntax.
The mapping file used for the Bank example can be found in the project:
Deploying a Subgraph
With your local Graph Node, you can create your Subgraph executing the following code:
Where:
username — refers to the username related to the Subgraph being created
subgraph-name — the Subgraph name
graph-node — refers to the URL of the hosted service to use. Typically, for a local Graph Node is
http://127.0.0.1:8020
Once created, you can deploy your Subgraph by running the following command with the same parameters as before:
Where:
username — refers to the username used when creating the Subgraph
subraph-name — refers to the Subgraph name defined when creating the Subgraph
ipfs-url — refers to the URL for IPFS. For your local Graph Node, the default value is
http://localhost:5001
graph-node — refers to the URL of the hosted service to use. For your local Graph Node, the default value is
http://localhost:8020
The logs for the sucessful deployed should look like:
DApps can now use the Subgraph endpoints to fetch the data indexed by The Graph protocol.
If there has existed call record for contract, you can call the Graph node to get the data, just like this:
References
Last updated