The graph

部署本地SubGraph

参考链接:https://thegraph.academy/developers/local-development/

  1. Setting up Ganache CLI
    安装选用npm或者yarn
npm install -g truffle ganache-cli 
yarn global add truffle ganache-cli

命令行起一条测试链

ganache-cli -h 127.0.0.1
  1. Starting Graph Node locally
git clone https://github.com/graphprotocol/graph-node/
cd graph-node/docker

启动本地graph节点:docker-compose up
3. Initializing new subgraph
安装graph cli

npm install -g @graphprotocol/graph-cli / yarn global add @graphprotocol/graph-cli
  1. Deploying sample smart contract to Ganache
    安装truffle并初始化项目truffle init
    在contracts目录下编写智能合约
    truffle compile编译智能合约
    修改配置文件truffle-config.js,修改网络配置连接到本地区块链网络(Ganache)。
    打开位于项目根目录下的truffle-config.js文件,修改内容如下:
module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*" // Match any network id
    }
  },
  solc: {
    optimizer: {
      enabled: true,
      runs: 200
    }
  }
}

在migrations目录中创建迁移脚本2_deploy_contracts.js
文件内容如下:

var MyContract = artifacts.require("./MyContract.sol");

module.exports = function(deployer) {
  deployer.deploy(MyContract);
};

运行truffle migrate部署智能合约至本地ganache

  1. Deploying subgraph to local Graph Node
    输入子图的根目录将其部署到本地图形节点
$ sed -i -e 's/0x2E645469f354BB4F5c8a05B3b30A929361cf77eC/<CONTRACT_ADDRESS>/g'  subgraph.yaml

安装依赖
yarn && yarn codegen
分配子图名称
yarn create-local
yarn deploy-local

子图

子图定义由几个文件组成:

  • subgraph.yaml: 包含子图清单的 YAML 文件,定义了您的子图索引的智能合约,这些合约中需要关注的事件,以及如何将事件数据映射到 Graph 节点存储并允许查询的实体,完整规范参考https://github.com/graphprotocol/graph-node/blob/master/docs/subgraph-manifest.md
  • schema.graphql: 一个 GraphQL 模式文件,它定义了为您的子图存储哪些数据,以及如何通过 GraphQL 查询这些数据,包括一对一,一对多,多对多

一对一

type Transaction @entity(immutable: true) {
  id: Bytes!
  transactionReceipt: TransactionReceipt
}

type TransactionReceipt @entity(immutable: true) {
  id: Bytes!
  transaction: Transaction
}

一对多

type Token @entity(immutable: true) {
  id: Bytes!
}

type TokenBalance @entity {
  id: Bytes!
  amount: Int!
  token: Token!
}

多对多

type Organization @entity {
  id: Bytes!
  name: String!
  members: [User!]!
}

type User @entity {
  id: Bytes!
  name: String!
  organizations: [Organization!]! @derivedFrom(field: "members")
}

查询

参考官方文档:https://thegraph.com/docs/developer/create-subgraph-hosted,与sql和文档数据库有类似的地方

{
  实体(筛选条件) {
    查询返回的字段
  }
}

schema.graphsql

type AccountPool @entity {
  id: ID!
  accountAddress: Bytes!
  poolAddress: String!
  amount: BigInt!
  createdAt: BigInt!
  pool: Pool!
  account: Account!
}

查询用户的pool地址

{
  AccountPool(first: 1000, skip:0, where:{account: 0x123xxxxxxxxxxxxxxxx}) {
    id,
	poolAddress
  }
}

排序

// 通过price排序,排序方式为asc【acs升序,desc降序】
{
  tokens(orderBy: price, orderDirection: asc) {
    id
    owner
  }
}

分页

// 查询前10个

{
  tokens(first: 10) {
    id
    owner
  }
}

// 查询 10 个Token实体,从集合的开头偏移 10 个位置,获得11~20的Token
{
  tokens(first: 10, skip: 10) {
    id
    owner
  }
}

总结

The Graph是捕捉区块链事件并提供一个查询事件的GraphQL接口,以便的跟踪数据的变化。
TheGraph中定义如何为数据建立索引,称为Subgraph,包含三个组件:Manifest 清单(subgraph.yaml) - 定义配置项、Schema 模式(schema.graphql) - 定义数据、Mapping 映射(mapping.ts) - 定义事件到数据的转换。
部署Subgraph到TheGraph,实现数据索引,在graphql查询数据。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值