When writing a smart contract, the state change is usually expressed by triggering an event. The Graph captures blockchain events and provides a GraphQL interface for querying events, allowing us to easily track data changes. In fact, many DEFI protocols and The Graph are based on query data.
https://thegraph.academy/developers/local-development/
We will make some additional additions based on this tutorial.
Tool preparation: nodejs, yarn, docker, truffle, graph-cli
1. Contract development and deployment
-
Contract development environment construction
npm install -g truffle ganache-cli
-
Contract writing
This contract is a handwritten test contract, which mainly defines two events, PairCreated and AddLiquidity. We use thegarph to listen to the following events.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Pair{
string private _name;
uint private _liquidity;
constructor(string memory name){
_name = name;
}
function addLiquidity(uint amount) public {
_liquidity+=amount;
}
function getLiquidity() public view returns(uint){
return _liquidity;
}
function getName() public view returns(string memory){
return _name;
}
}
contract Dex {
address private _owner;
uint private _index = 0;
mapping(address => Pair) private _pairs;
mapping(uint => address) private _indexs;
modifier onlyOwner(){
require(msg.sender == _owner,"must be owner");
_;
}
event PairCreated(string name, address pairAddress);
event AddLiquidity(address pairAddress, uint liquidity);
function createPair(string memory name) public {
Pair pair = new Pair(name);
_pairs[address(pair)] = pair;
_indexs[_index] = address(pair);
_index++;
emit PairCreated(name, address(pair));
}
function addLiquidity(address pairAddress,uint liquidity) public {
_pairs[pairAddress].addLiquidity(liquidity);
emit AddLiquidity(pairAddress,liquidity);
}
function getCount() public view returns(uint){
return _index;
}
function pairAddress(uint index) public view returns(address){
return _indexs[index];
}
function getPairName(address pairAddress) public view returns(string memory){
return _pairs[pairAddress].getName();
}
function getLiquidity(address pairAddress) public view returns(uint liquidity){
return _pairs[pairAddress].getLiquidity();
}
}
-
Deploy to the self-built ganache blockchain through truffle
truffle migrate
-
Call the contract to write data through remix to trigger an event
2. Create a Subgraph of index data
TheGraph defines how to index data, called Subgraph, which consists of three components:
- Manifest list (subgraph.yaml) - defines configuration items
specVersion: 0.0.1
description: study
repository: https://github.com/graphprotocol/example-subgraph
schema:
file: ./schema.graphql
dataSources:
- kind: ethereum/contract
name: study
network: mainnet
source:
address: '0xf80e65c3c1B49626bae2ECE7e67A3e1AdBD480C2'
abi: Dex
mapping:
kind: ethereum/events
apiVersion: 0.0.5
language: wasm/assemblyscript
entities:
- Pairs
abis:
- name: Dex
file: ./build/contracts/Dex.json
eventHandlers:
- event: PairCreated(string,address)
handler: handleNewPairCreated
- event: AddLiquidity(address,uint256)
handler: handleAddLiquidity
file: ./src/mapping.ts
- Schema schema (schema.graphql) - defines the data
type Pair @entity {
id: ID!
displayName: String!
liquidity: Int!
}
-
generate code
graph codegen
-
Mapping mapping (mapping.ts) - defines the transformation of events to data
import {AddLiquidity, PairCreated} from '../generated/study/Dex'
import {Pair} from '../generated/schema'
export function handleNewPairCreated(event: PairCreated): void {
let pair = new Pair(event.params.pairAddress.toHexString())
pair.displayName = event.params.name
pair.liquidity = 0
pair.save()
}
export function handleAddLiquidity(event: AddLiquidity): void {
let id = event.params.pairAddress.toHexString()
let pair = Pair.load(id)
if (pair == null) {
return
}
pair.liquidity += event.params.liquidity.toI32()
pair.save()
}
3. Deploy Subgraph to TheGraph to implement data indexing
- Take building a local node as an example, run graph-node
git clone https://github.com/graphprotocol/graph-node/
cd graph-node/docker
docker compose up
- Install project dependencies && deploy to graph nodes
yarn && yarn codegen
yarn create-local
yarn deploy-local
4. Query data in graphql
query{
pairs(first:10){
id,
displayName,
liquidity
}
}
The display results are as follows
5. Stepping pit guide
- Failed to deploy to Graph node http://127.0.0.1:8020/: Ethereum network not supported by registrar: mainnet.
It is because of the ganache blockchain network that I play locally, graph-node cannot find the host in the container IP, changed the configuration ethereum in docker/docker-compose.yml: 'mainnet:http://docker.for.mac.host.internal:8545'