The Graph is a decentralized protocol for indexing and querying blockchain data. Creating Subgraphs uses the standard GraphQl API to query these indexes to solve the pain of decentralized data traversal.

Huge amounts of decentralized data are difficult to calculate statistically, which is now solved by a host service that provides indexes to blockchain data. These indexes (” Subgraphs “) are queried using the standard GraphQl API. In the future, hosting services will evolve into fully decentralized protocols with the same functionality. Both are powered by an open source implementation of Graph Node, which provides an open and transparent solution for querying Ethereum and IPFS network data. Anyone can create and publish indexed data based on the open API it provides, namely SubGraph.

This introductory document takes you through a journey like a traditional database gathering (gathering and fetching) with a simple transaction statistics example. Once you have THe Graph’s knowledge in hand, let’s get started.

Main research route (private server all the way to the end)

  1. Smart contract published by Truffer CMD to Truffle/Ganache container (private chain)
  2. Contract adress&abis and thegraph nodes (private nodes)
  3. The MarkDemo1 project (The Graph) listens, collects data, processes data, and wraps common queries
  4. Dapp2 (8000->3000)/Query Page (8001)
  5. Dapp2 calls a contract transaction that triggers the graph to output changes

Auxiliary output:

  1. The graph subgraphs website browse https://thegraph.com/explorer/dashboard https://thegraph.com/explorer personal subgraphs center
  2. Truffle/Ganache for smart contract management (deployment, debugging)
  3. RIMAX browser version for contract operation
  4. Dapp2 operations on smart contracts
  5. Dapp1 listens to smart contracts
  6. Truffle/Ganache, Ganache CL, The Graph private node deployment and use
  7. A variety of networks, native environments, scripts to replace the stepped pit (pit is to save time for later)
  8. Publish an online subgraph to https://thegraph.com/explorer

Play Subgraph online

On https://thegraph.com/explorer there are a lot of subgraph, let’s take a look at the uniswap – v2

Github https://github.com/Uniswap Post address
ID QmWTrJJ9W8h3JE19FhCzzPYsJ2tgXZCdUqnbyuo64ToTBN subgraph
Queries (HTTP) https://api.thegraph.com/subg… Plenty of experiential data queries

uniswap-v2

Go to the browserExperience theFirst,

Enter query function, parameters and conditions [click the purple button] -> to node data center 3 ->4. Query the desired result; When the generic API 2 can also be called programmatically, passing in different parameters, experiment with GraphQl API queries

{ pairs{id}} { pairs(where:{id:”0x00004ee988665cdda9a1080d5792cecd16dc1220″}){id}} { pairs(orderBy:id){id}}

The Graph uses Subgraph to track smart contracts



Data capture/event monitoring data aggregation and query

Gets MarkDemo1, a simple example of the Subgraph that stores and displays an online smart contract

1. Catch the records from the current time to the last update time. 2. Take the number of transaction records of a certain customer and the total amount. 4. The above graph is only in the imagination.

1) The system initialized the most recent block in the chain, and periodically fetched the most recent block 2) The listening event of the subgraph (the principle is that the event listens all the data, so 1, 2, 3 are not significant) is pulling down all the data 3) Let’s take a look at the pits I have been in

  • MAC machines environmental legacy file can’t find the brew upgrade, a temporary solution to find a replacement with similar directory file (always can’t sleep, Please refer to the appendix for the solution of BREW Upgrade and the problem of installation error. Replace Homebrew-bottles). The agent port 8001 conflicts with the PORT OF THE GRAPH system, change the agent
  • YARN, Git’s proxies and images have to switch back and forward in order to cope with what is known as the web
  • The graph in Docker accesses the outside truffle/ganache system configuration, the script generates an access unreachable ip172.18.2.0, Modify to native IP 192.168.0.136 (can’t use 127.0.0.1 brain docker access localhost back to where?)
  • SEL order to replace the contract number for the second time should use the contract number of the first time: there are two contracts in the example program, and they may be modified again
  • Truffle/Ganache will generate different account sequences each time it starts, so the owner of the contract imported at different times may be different. Do not remember the first account, which will cause death (you can check the owner with the historical transaction record of the contract, See MarkDemo2) 4) Writing a subgraph requires attention or has already completed the scaffolding
  • id: ID! Each entity is required to have a non-empty non-repeating primary key ID
  • The DataSources configured the initial read block to skip those blocks before the contract was published: startBlock: 6627917

Publish the subgraph example MarkDemo1

First deploy the container to publish a smart contract (see HardHat + VS Code for Ganache deployment here)

Step 1: Ganache and Required Parameters

$ ganache-cli -h 0.0.0.0

Such as: Ganach-cli-h 0.0.0.0-v/db-b 6-a 8-e 1000-d expect chair toe trade spider wedding say item scare fog shrimp garlic

Open the existing repository, set the auto mining time 6, set 8 1000gas accounts, import the existing private key, (-d, can be outside or can be generated by -m) bootstrap-account — db — blockTime Technical documentation is understood from different perspectives with different focus and attention points — DB open (remember that this directory and file must have read and write permissions to operate subsequent account import) — Account: specify the account private key and account balance to create the initial test account. Can be set multiple times Note that the private key is 64 characters long and must be entered as a hexadecimal string with the 0X prefix. The balance can be entered as an integer or a hexadecimal value prefixed with 0X, specifying the number of WEI in the account. When you use — Account, the HD wallet is not created for you. -U or — UNLOCK: Specifies -UNLOCK… Pass the address or account index multiple times to unlock a specific account. When used with — secure, –unlock overrides the locked state of the specified account. $ ganache-cli –secure –unlock “0x1234…” –unlock “0xabcd…” -b or r — BlockTime: Specifies the BlockTime for automatic mining, in seconds. The default value is 0, which means that automatic mining is not performed. Ganache will a new block for every transaction.

  • -A or — Accounts: Specifies the number of test accounts to be created at startup.
  • -E or — defaultBalanceether: The amount of ether assigned to each test account, default being 100.
  • -b or r — BlockTime: Specifies the BlockTime for automatic mining, in seconds. The default value is 0, which means that automatic mining is not performed.
  • -d or — Deterministic: Generate fixed test account addresses based on predetermined mnemonic.
  • -n or — secure: Lock all test accounts by default, facilitating third-party trade signing.
  • -m or — mnemonic: The mnemonic used to generate the address of the test account.
  • -p or — port: Sets the listening port. Default is 8545.
  • -h or — hostname: Set the listening host to the same default value as NodeJS ‘server.listen().
  • -s or — seed: Sets the seed from which the mnemonic word is generated.
  • -g or — GASPrice: Sets the Gas price. Default value is 20000000000.
  • -l or — gasLimit: Sets the upper limit for Gas. Default is 90000.
  • -f or — fork: Forks from a specified block of running Ethereum node client software. The input value should be the HTTP address and port of the node, such as http://localhost:8545. Optionally, use the @ tag to specify a specific block, for example: http://localhost:8545@1599200.
  • -i or — networkID: Specifies the network ID. Default is the current time, or use the network ID of the forked chain.
  • — db: Sets the directory to store the chain data. If the link data is already in the path, Ganache-CLI will use it to initialize the chain rather than recreate it.
  • — debug: Output VM opcodes for debugging.
  • — mem: Output Ganache-CLI memory usage statistics, which replaces the standard output.
  • — NovMerrorson RPCResponse: Do not send failed trades as RCP errors. Enabling this flag makes error reporting compatible with other node clients, such as Geth and Parity.
-a or --accounts: Specify the number of accounts to generate at startup. -e or --defaultBalanceEther: Amount of ether to assign each test account. Default is 100. -b or --blockTime: Specify blockTime in seconds for automatic mining. If you don't specify this flag, ganache will instantly mine a new block for every transaction. Using the --blockTime flag is discouraged unless you have  tests which require a specific mining interval. -d or --deterministic: Generate deterministic addresses based on a pre-defined mnemonic. -n or --secure: Lock available accounts by default (good for third party transaction signing) -m or --mnemonic: Use a bip39 mnemonic phrase for generating a PRNG seed, which is in turn used for hierarchical deterministic (HD) account generation. -p or --port: Port number to listen on. Defaults to 8545. -h or --host or --hostname: Default to 127.0.0.1 (Defaults to 0.0.0.0 for Docker instances of ganache-cli). -s or --seed: Use arbitrary data to generate the HD wallet mnemonic to be used. -g or --gasPrice: The price of gas in wei (defaults to 20000000000) -l or --gasLimit: The block gas limit (defaults to 0x6691b7) --callGasLimit: Sets the transaction gas limit for eth_call and eth_estimateGas calls. Must be specified as a hex string. Defaults to "0x1fffffffffffff" (Number.MAX_SAFE_INTEGER) -k or --hardfork: Allows users to specify which hardfork should be used. Supported hardforks are byzantium, constantinople, petersburg, istanbul, and muirGlacier (default). -f or --fork: Fork from another currently running Ethereum client at a given block. Input should be the HTTP location and port of the other client, e.g. http://localhost:8545. You can optionally specify the block to fork from using an @ sign: http://localhost:8545@1599200. forkCacheSize: number - The maximum size, in bytes, of the in-memory cache for queries on a chain fork. Defaults to 1_073_741_824 bytes (1 gigabyte). You can set this to 0 to disable caching (not recommended), or to -1 for unlimited (will be limited by your node process). -i or --networkId: Specify the network id ganache-cli will use to identify itself (defaults to the current time or the network id of the forked blockchain if configured) --chainId: Specify the Chain ID ganache-cli will use for eth_chainId RPC and the CHAINID opcode. For legacy reasons, the default is currently 1337 for eth_chainId RPC and 1 for the CHAINID opcode. Setting this flag will align the chainId  values. This will be fixed in the next major version of ganache-cli and ganache-core! --db: Specify a path to a directory to save the chain database. If a database already exists, ganache-cli will initialize that chain instead of creating a new one. --debug: Output VM opcodes for debugging --mem: Output ganache-cli memory usage statistics. This replaces normal output. -q or --quiet: Run ganache-cli without any logs. -v or --verbose: Log all requests and responses to stdout -? or --help: Display help information --version: Display the version of ganache-cli --account_keys_path or --acctKeys: Specifies a file to save accounts and private keys to, for testing. --noVMErrorsOnRPCResponse: Do not transmit transaction failures as RPC errors. Enable this flag for error reporting behaviour which is compatible with other clients such as geth and Parity. --allowUnlimitedContractSize: Allows unlimited contract sizes while debugging. By enabling this flag, the check within the EVM for contract size limit of 24KB (see EIP-170) is bypassed. Enabling this flag will cause ganache-cli to behave differently than production environments. --keepAliveTimeout: Sets the HTTP server's keepAliveTimeout in milliseconds. See the NodeJS HTTP docs for details. 5000 by default. -t or --time: Date (ISO 8601) that the first block should start. Use this feature, along with the evm_increaseTime method to test time-dependent code.

Step 2: Running a local Graph node

In the ~ graph-node/docker directory

$ docker-compose up;

or

#! /bin/bash docker-compose down -v; if [ -d "data" ] then echo "Found old data for the graph node - deleting it"; # we need to sudo this to remove system locked files sudo rm -rf data/; fi docker-compose up;

Graph – node_1 | Jun 24 01:34:13. 558 WARN Trying again after net_version RPC call failed (attempt # 18) with the result Err(Transport error: Error(Connect, Os { code: 111, kind: ConnectionRefused, message: “Connection refused” })), provider: Mainnet xml-rpc – 0 graph – node_1 | Jun 24 01:34:14. 517 ERRO Connection to the provider failed. Not using this provider, the error: deadline has elapsed, provider: mainnet-rpc-0

This error will cause an error to be reported in Step 3:

Failed to deploy to Graph node http://127.0.0.1:8020/: Ethereum network not supported by registrar: mainnet. DOCKER-COMPOSE.YML-E: docker/docker-compose. YML-E: docker/docker-compose. YML-E: docker/docker-compose. ‘mainnet: http://host.docker.internal:8545’ is modified to the local: ethereum: ‘mainnet: http://192.168.0.136:8545’

Step 3-0: Publish the MarkDemo1 contract

$ truffle compile
$ truffle migrate

Migrations: 0 x83ad4160f00259d6d329c09a1436386a706e3818 GravatarRegistr: 0 xe54ba45f29b4247d75e86fd7a83a1e44160610d2

$ sed -i -e  's/0x2E645469f354BB4F5c8a05B3b30A929361cf77eC/0x83Ad4160F00259D6D329c09A1436386a706e3818/g'    subgraph.yaml

Step 3: Deploying to your local Graph Node

$ yarn create-local
$ yarn build && yarn deploy-local

The last example output effect is as follows: (http://127.0.0.1:8000/subgraphs/name/moluoping/markg)

Step 4: DAPP call

$ git clone https://github.com/graphprotocol/ethdenver-dapp/
$ echo 'REACT_APP_GRAPHQL_ENDPOINT=http://localhost:8000/subgraphs/name/moluoping/markdemo1' > .env
$ yarn && yarn start

Listen to smart contract trading events Subgraph example MarkDemo2

The previous section briefly explained a sub-graph publishing process, and we’ll add the following code to our dApp project to demonstrate the entire smart contract event listening process

var Web3 = require("web3") var web3; if (typeof web3 ! == 'undefined') { web3 = new Web3(web3.currentProvider); } else {web3 = new web3 (new web3. Will. WebsocketProvider (ws: / / 127.0.0.1: "8545")); } web3.eth.defaultAccount = '0x4386997160134D4a67FD6A14DE7f924315D6F0A4'; console.log('defaultAccount:' + web3.eth.defaultAccount) var contractAbi = [{"constant":false,"inputs":[{"name":"_imageUrl","type":"string"}],"name":"updateGravatarImage","outputs":[],"payable":f alse,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"setMythicalGravatar","outpu ts":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"owner","typ e":"address"}],"name":"getGravatar","outputs":[{"name":"","type":"string"},{"name":"","type":"string"}],"payable":false, "stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"gravatarToO wner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":t rue,"inputs":[{"name":"","type":"address"}],"name":"ownerToGravatar","outputs":[{"name":"","type":"uint256"}],"payable": false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_displayName","type":"string"}]," name":"updateGravatarName","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":fa lse,"inputs":[{"name":"_displayName","type":"string"},{"name":"_imageUrl","type":"string"}],"name":"createGravatar","out puts":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type": "uint256"}],"name":"gravatars","outputs":[{"name":"owner","type":"address"},{"name":"displayName","type":"string"},{"nam e":"imageUrl","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"anonymous":false,"inputs": [{"indexed":false,"name":"id","type":"uint256"},{"indexed":false,"name":"owner","type":"address"},{"indexed":false,"name ":"displayName","type":"string"},{"indexed":false,"name":"imageUrl","type":"string"}],"name":"NewGravatar","type":"event "},{"anonymous":false,"inputs":[{"indexed":false,"name":"id","type":"uint256"},{"indexed":false,"name":"owner","type":"a ddress"},{"indexed":false,"name":"displayName","type":"string"},{"indexed":false,"name":"imageUrl","type":"string"}],"na Me ":" UpdatedGravatar ", "type" : "the event"}] / / Abi data from / / truffle/ganache: markdemo1 abis/Gravity. The json / / want to: web https://remix.ethereum.org/ tab 'SOLIDITY COMPILER'->bottom 'abi' var contractAbiTruffle=contractAbi var ContractAbiRemix = contractAbi var contractaAddressRemix = '0 x4ef9beb56eb0ab92cded01eff9a60554ae594d45 / / want to: web https://remix.ethereum.org/ tab 'DEPLOY & RUN TRANSACTIONS'->bottom 'Deployed Contracts' list var contractaAddressTruffle ='0x38FcB3d9c3C8958373D61B702049D695A6725AFE' //$ truffle migrate { var MyContract = new web3.eth.Contract(contractAbiRemix, contractaAddressRemix); MyContract.methods.updateGravatarName('aaaa').send({from: "0x4386997160134D4a67FD6A14DE7f924315D6F0A4"}) .then(console.log); //from data: this app project http://localhost:3000/F12 history event data returnValues->"owner" MyContract.methods.getGravatar('0x38FcB3d9c3C8958373D61B702049D695A6725AFE').call() .then(console.log); } // { // var MyContract = new web3.eth.Contract(contractAbiTruffle, contractaAddressTruffle); // MyContract.methods.updateGravatarName('bbbb').send({from: '0xC187EB4620dCEF0FFe5C45566969BdF2be3Ea7B7'}) // .then(console.log); // //from data: this app project http://localhost:3000/F12 history event data returnValues->"owner" // MyContract.methods.getGravatar('0x38FcB3d9c3C8958373D61B702049D695A6725AFE').call() // .then(console.log); // } var myEventNew = MyContract.events.NewGravatar({ filter:{}, fromBlock: 0 }, function(error, event){}) .on('data', function(event){ console.log('NewGravatar'); console.log(event); // same results as the optional callback above }) .on('changed', function(event){ // remove event from local database }) .on('error', console.error); var myEventUpdate = MyContract.events.UpdatedGravatar({ filter:{}, fromBlock: 0 }, function(error, event){}) .on('data', function(event){ console.log('UpdatedGravatar'); console.log(event); // same results as the optional callback above }) .on('changed', function(event){ // remove event from local database }) .on('error', console.error);
  1. On an example of truffle in $/ ganache environment truffle migrate contract address visible intelligence, Abi data can be in markdemo1 engineering markdemo1 abis/Gravity. Json, but beginners with want to experience first.
  2. Look at the above code and then come out, the data finally run…….

MarkDemo3 (a sales subgraph)

Subgraph is online on The Graph Market

1. Login on GitHub website with your registered account (register a new account without an account)

2. Gain thegraph website token after logging in at https://thegraph.com/explorer/dashboard/ associate, obtain tocken

3. Create subgraphgraph

graph init --from-example moluo.... /markdemo3

graph init –from-contract

\ [–network

] \ [–abi

] \

/

[

] moluo…. GitHub account name/MarkDemo3 subgraph name to create the project and compile and generate related resources

✔ Subgraph name, moluo… / markDemo3 ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ ➤ Command in package. JSON Initialize subgraph repository bookmarks Install dependencies with yarn bookmarks Generate ABI and schema Types with yarn codegen You can see the compilation process from the above script run

A. Log on to TheGraph Development Center with a token

$ graph auth https://api.thegraph.com/deploy/880d70986bfc4172902641d870d96f37

Graph auth https://api.thegraph.com/deploy/ < access token – > < access token – > for step 2 to obtain tocken note tocken front and a blank space, these are the two parameters

The original error was: Cannot find module 'keytar' $NPM i-g keytar

B. create a subgraph name https://thegraph.com/explorer/dashboard/



Subgraph starts with a capital letter and anything else is optional.

C. Post your subgraph to thegraph.com

$ cd markdemo3
$ yarn deploy

If there is no do step b, will be an error You may need to create subgraph at https://thegraph.com/explorer… error Command failed with exit code 1. c. If together well, you can see: https://api.thegraph.com/subgraphs/name/moluoping/markdemo3

Example parsing (focusing on Subgraph’s MarkDemo4)

This example generates a Subgraph project directly from the contract wizard in the container:

$ graph init --from-contract 0x1e1215caD01aD7192832e0DACfA930Caf0132b43  --network mainnet --abi markdemo4.json moluoping/markdemo4

The smart contract is deployed from Remix. The Compile page of the Remix page can be copied to Contract Adress, the Deploy page can be copied to the ABI and saved as a file. Graph Init will generate the scaffolding file necessary for the function. And automatically code generation the first event listener of the contract as the query entity (other smart contract functions are commented)

1) SubGraph. YAML address, ABI from the command line, startBlock defines the starting block on the monitoring chain according to future Settings ‘0xc0a47dFe034B400B47bDaD5FecDa2621de6c4d95’ abi: Contract startBlock: 88888

Now it seems that all the entities that can be queried by GraphGL are entities that can be queried by GraphGL, and the entities that can’t be queried don’t seem to have an open ID and must have a string field. GraphGL is the standard for the graph database, so look at the many-to-many relationship.

3) The mapping.ts event handling implementation is configured in SubGraph. Yaml EventHandlers:

  • event: Transfer(indexed address,indexed address,uint256)

    handler: handleTransfer
  • event: Burn(indexed address,uint256) handler: handleBurn file: The./ SRC /mapping.ts event variable contains the contract related (event, contract function) exampleEntity “.. “/generated/schema” contains local persistence associations to the subgraph

4) Class local read and write class Schema. Ts smart Contract function class Contract.ts generated in the wizard

5) Other package.json contains subgraph from compilation to local deployment. The graph deployments other command line files that are generated by copy


The Graph Basics

  1. TheGraph community 1) official https://www.thegraph.com/ https://thegraph.com/docs/define-a-subgraph 2) introduction: The etheric fang data index platform The Graph using tutorial at https://developer.aliyun.com/article/776668
  2. 1) understand The knowledge system Graph on https://zhuanlan.zhihu.com/p/196773044 (2) understand The Graph ( https://thegraph.com/blog/the-graph-network-in-depth-part-2 (3) ether fang development (a) – Truffle and Ganache https://blog.csdn.net/TurkeyCock/article/details/79165602 4) ether lane test network Rinkeby tutorial (don’t have the money to buy COINS but still want to play the etheric lane? In the etheric lane test network ~ ~ ~ ~) https://blog.csdn.net/wuhuimin521/article/details/85135610
  3. 1) applications using TheGraph perfect Web3 event data retrieval https://learnblockchain.cn/article/1589 links: the original author: https://soliditydeveloper.com/thegraph Markuswaas 2) Ethereum Truffle + Ganache contract deployment and debugging and Web3.js event listening process record https://www.e-learn.cn/topic/3716459 The original link: https://my.oschina.net/u/4277087/blog/4325668 unruly time Submitted in the 2020-08-06 04:50:03 web3. 3) all tutorial js 1.0 manual in Chinese http://cw.hubwiz.com/card/c/web3.js-1.0/1/4/8/ https://github.com/bluketalk/sweb3

We welcome like-minded partners in the blockchain industry to join WeChat and BlockGeek blockchain technology exchange group to jointly promote the popularization and development of blockchain technology