Skip to content

Instantly share code, notes, and snippets.

@hosseinnedaee
Last active August 6, 2022 07:26
Show Gist options
  • Save hosseinnedaee/48607a54acf2602ef65c97c02356b517 to your computer and use it in GitHub Desktop.
Save hosseinnedaee/48607a54acf2602ef65c97c02356b517 to your computer and use it in GitHub Desktop.
A step by step guide to run a graph node

Run Graph Node :)

An step by step guide.

1. First clone graph-node from this repo.

go to the docker directory and run setup.sh. it will replace host.docker.internal in the docker-compose.yaml with the docker host ip address.

here also you need to change ethereum in graph-node environment based on node you are going to connect:

# example for ethereum classic node
ethereum: 'classic:http://172.18.0.1:8545'

# example for ganache node
ethereum: 'mainnet:http://172.18.0.1:8545'

# example for Hardhat node
ethereum: 'mainnet:http://172.18.0.1:8545'

# example for devChain local geth node (devChain is our datadir geth name)
ethereum: 'devChain:http://172.18.0.1:8545'

and then run this command:

docker-compose up

this will ipfs, postgresql and graph-node containers.

thats it!

2. Second run our blockchain node

Ganache

install ganache:

npm install -g ganache

and run the node with this command:

ganache --server.host 0.0.0.0 \
    --miner.coinbase "<THE_ADDRESS_THAT_MINING_REWARDS_WILL_SENT_TO>" \
    --wallet.mnemonic "<MY_METAMASK_RECOVERY_PHRASES>"

here I used my metamask wallet key phrases so I will have access it in metamask and can send transactions.

now in the remix ethereum we can select 'Ganache Provider'. simply we connected to currently running Ganache and we can deploy contract and interact with it.

it's so fast and simple!

Hardhat

run hardhat node with --hostname 0.0.0.0

npx hardhat node --hostname 0.0.0.0

Go Ethereum (a private network)

note you can find more details on go-ethereum repo.

install go-ethereum, here is a guide to install Go Ethereum on ubuntu via PPAs.

first we need to create the genesis stateof our network. create a json file like dev_genesis.json:

{
    "config": {
        "chainId": 1337,
        "homesteadBlock": 0,
        "eip150Block": 0,
        "eip155Block": 0,
        "eip158Block": 0,
        "byzantiumBlock": 0,
        "constantinopleBlock": 0,
        "petersburgBlock": 0,
        "istanbulBlock": 0,
        "berlinBlock": 0,
        "londonBlock": 0
    },
    "alloc": {
        "<PUBLIC_ADDRESS_1>": {
            "balance": "999999999"
        },
        "<PUBLIC_ADDRESS_2>": {
            "balance": "999999999"
        },
        "<PUBLIC_ADDRESS_3>": {
            "balance": "999999999"
        }
    },
    "coinbase": "<PUBLIC_ADDRESS>",
    "difficulty": "0x00001",
    "extraData": "",
    "gasLimit": "0x2fefd8",
    "nonce": "0x0000a00840000042",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "timestamp": "0x00"
}

then initialize node with the json file:

geth --datadir=devChain init dev_genesis.json

you can also import your MetaMask account to the node. for example create a file and write you account private key into it and save. this way you can add that account:

geth account import <FILE>

now we are ready to run our node:

geth --datadir=devChain --http --http.addr 0.0.0.0 \
    --http.corsdomain "*" --ws \
    --mine --miner.threads 1 --miner.etherbase <PUBLIC_ADDRESS> \
    --allow-insecure-unlock \
    --unlock <PUBLIC_ADDRESS_1>,<PUBLIC_ADDRESS_2>,<PUBLIC_ADDRESS_3>,<PUBLIC_ADDRESS_4> \
    --miner.extradata "myblockchain" --fakepow --snapshot

good to know you can connect to the node by starting an interactive JavaScript environment

geth attach ./devChain/geth.ipc 

Ethereum Classic node

We need to use etclabscore/core-geth.

Run it with docker: (there is a guide also here.)

docker run -d \
    --name core-geth \
    -v data:/root \
    -p 30303:30303 \
    -p 8545:8545 \
    etclabscore/core-geth \
    --classic \
    --metrics \
    --http \
    --http.api eth,net,web3 \
    --http.addr 0.0.0.0

The node will start syncing.

3. Subgraph

Clone this example subgraph.

install and codegen

yarn
yarn codegen

in the subgraph.yml file you need to change datasource network based on you node:

  • Ganache -> mainnet
  • classic network -> classic
  • go-ethereum private network -> devChain (this is based on my network name which is defined by --datadir tag)

this is an example for devChain

specVersion: 0.0.2
description: Gravatar for Ethereum
repository: https://github.com/graphprotocol/example-subgraph
schema:
  file: ./schema.graphql
dataSources:
  - kind: ethereum/contract
    name: Gravity
    network: devChain
    source:
      address: '0xCe461188bA4F0834aFB07B88c6E6D2fd7e95D4Dc'
      abi: Gravity
    mapping:
      kind: ethereum/events
      apiVersion: 0.0.5
      language: wasm/assemblyscript
      entities:
        - Gravatar
      abis:
        - name: Gravity
          file: ./abis/Gravity.json
      eventHandlers:
        - event: NewGravatar(uint256,address,string,string)
          handler: handleNewGravatar
        - event: UpdatedGravatar(uint256,address,string,string)
          handler: handleUpdatedGravatar
      file: ./src/mapping.ts

and then:

yarn create-local # to register subgraph in graph-node
yarn deploy-local # deploy the subgraph to graph node

note if needed you can remove an already registered subgraph from node with this command:

npx graph remove <subgraph-name> --node <node address>

useful links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment