From 5be9a93c9e70bc44a171488e7198b9a9fa859fe6 Mon Sep 17 00:00:00 2001 From: godlin Date: Sat, 30 Mar 2024 19:48:31 +0100 Subject: [PATCH] Featuring Peaq --- .../.github/scripts/publish-deploy.sh | 15 ++ .../.github/workflows/cli-deploy.yml | 34 +++ Peaq/peaq-starter/.github/workflows/pr.yml | 24 ++ Peaq/peaq-starter/.gitignore | 58 +++++ Peaq/peaq-starter/README.md | 90 +++++++ Peaq/peaq-starter/docker-compose.yml | 66 ++++++ Peaq/peaq-starter/docker/load-extensions.sh | 5 + Peaq/peaq-starter/docker/pg-Dockerfile | 12 + Peaq/peaq-starter/erc20.abi.json | 222 ++++++++++++++++++ Peaq/peaq-starter/package.json | 42 ++++ Peaq/peaq-starter/project.ts | 82 +++++++ Peaq/peaq-starter/schema.graphql | 17 ++ Peaq/peaq-starter/src/index.ts | 3 + .../src/mappings/mappingHandlers.ts | 53 +++++ .../src/test/mappingHandlers.test.ts | 11 + Peaq/peaq-starter/tsconfig.json | 20 ++ 16 files changed, 754 insertions(+) create mode 100644 Peaq/peaq-starter/.github/scripts/publish-deploy.sh create mode 100644 Peaq/peaq-starter/.github/workflows/cli-deploy.yml create mode 100644 Peaq/peaq-starter/.github/workflows/pr.yml create mode 100644 Peaq/peaq-starter/.gitignore create mode 100644 Peaq/peaq-starter/README.md create mode 100644 Peaq/peaq-starter/docker-compose.yml create mode 100644 Peaq/peaq-starter/docker/load-extensions.sh create mode 100644 Peaq/peaq-starter/docker/pg-Dockerfile create mode 100644 Peaq/peaq-starter/erc20.abi.json create mode 100644 Peaq/peaq-starter/package.json create mode 100644 Peaq/peaq-starter/project.ts create mode 100644 Peaq/peaq-starter/schema.graphql create mode 100644 Peaq/peaq-starter/src/index.ts create mode 100644 Peaq/peaq-starter/src/mappings/mappingHandlers.ts create mode 100644 Peaq/peaq-starter/src/test/mappingHandlers.test.ts create mode 100644 Peaq/peaq-starter/tsconfig.json diff --git a/Peaq/peaq-starter/.github/scripts/publish-deploy.sh b/Peaq/peaq-starter/.github/scripts/publish-deploy.sh new file mode 100644 index 00000000..3c9dc04b --- /dev/null +++ b/Peaq/peaq-starter/.github/scripts/publish-deploy.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +while getopts p:o:e: flag +do + case "${flag}" in + e) ENDPOINT=${OPTARG};; + p) PROJECTNAME=${OPTARG};; + o) ORG=${OPTARG};; + *) echo "Usage: $0 [-p projectname] [-o org] [-e endpoint]" && exit 1;; + esac +done + +IPFSCID=$(npx subql publish -o -f .) + +npx subql deployment:deploy -d --ipfsCID="$IPFSCID" --projectName="${PROJECTNAME}" --org="${ORG%/*}" --endpoint="${ENDPOINT}" diff --git a/Peaq/peaq-starter/.github/workflows/cli-deploy.yml b/Peaq/peaq-starter/.github/workflows/cli-deploy.yml new file mode 100644 index 00000000..658d2c6c --- /dev/null +++ b/Peaq/peaq-starter/.github/workflows/cli-deploy.yml @@ -0,0 +1,34 @@ +name: "CLI deploy" + +on: + workflow_dispatch: + inputs: + projectName: + description: "Project name" + required: true + type: string +jobs: + deploy: + name: CLI Deploy + runs-on: ubuntu-latest + environment: + name: DEPLOYMENT + env: + SUBQL_ACCESS_TOKEN: ${{ secrets.SUBQL_ACCESS_TOKEN }} + ENDPOINT: ${{ secrets.ENDPOINT }} + steps: + - uses: actions/checkout@v2 + - name: Setup Node.js environment + uses: actions/setup-node@v2 + with: + node-version: 16 + - run: yarn + - name: Codegen + run: yarn codegen + - name: Version + run: npx subql --version + - name: repo + run: echo ${{github.repository}} + - name: Publish and Deploy + run: | + sh .github/workflows/scripts/publish-deploy.sh -o ${{github.repository}} -p ${{github.event.inputs.projectName}} -e ${{secrets.ENDPOINT}} diff --git a/Peaq/peaq-starter/.github/workflows/pr.yml b/Peaq/peaq-starter/.github/workflows/pr.yml new file mode 100644 index 00000000..b428f2d8 --- /dev/null +++ b/Peaq/peaq-starter/.github/workflows/pr.yml @@ -0,0 +1,24 @@ +name: PR +on: + pull_request: + paths-ignore: + - ".github/workflows/**" +jobs: + pr: + name: pr + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Setup Node.js environment + uses: actions/setup-node@v2 + with: + node-version: 16 + - run: yarn + - name: Codegen + run: yarn codegen + - name: Build + run: yarn build + - name: Install subql-node + run: yarn global add @subql/node + - name: Run tests with Subquery Node + run: subql-node test -f ${{ github.workspace }} diff --git a/Peaq/peaq-starter/.gitignore b/Peaq/peaq-starter/.gitignore new file mode 100644 index 00000000..9615e3cf --- /dev/null +++ b/Peaq/peaq-starter/.gitignore @@ -0,0 +1,58 @@ +# These are some examples of commonly ignored file patterns. +# You should customize this list as applicable to your project. +# Learn more about .gitignore: +# https://www.atlassian.com/git/tutorials/saving-changes/gitignore + +# Node artifact files +node_modules/ +dist/ + +# lock files +yarn.lock +package-lock.json + +# Compiled Java class files +*.class + +# Compiled Python bytecode +*.py[cod] + +# Log files +*.log + +# Package files +*.jar + +# Generated files +target/ +dist/ +src/types +project.yaml + +# JetBrains IDE +.idea/ + +# Unit test reports +TEST*.xml + +# Generated by MacOS +.DS_Store + +# Generated by Windows +Thumbs.db + +# Applications +*.app +*.exe +*.war + +# Large media files +*.mp4 +*.tiff +*.avi +*.flv +*.mov +*.wmv + +.data +.eslintcache diff --git a/Peaq/peaq-starter/README.md b/Peaq/peaq-starter/README.md new file mode 100644 index 00000000..bf3886b6 --- /dev/null +++ b/Peaq/peaq-starter/README.md @@ -0,0 +1,90 @@ +# SubQuery - Example Project for Peaq + +[SubQuery](https://subquery.network) is a fast, flexible, and reliable open-source data indexer that provides you with custom APIs for your web3 project across all of our supported networks. To learn about how to get started with SubQuery, [visit our docs](https://academy.subquery.network). + +**This SubQuery project indexes all transfers and approval events for all ERC20 tokens on Peaq's EVM** + +## Start + +First, install SubQuery CLI globally on your terminal by using NPM `npm install -g @subql/cli` + +You can either clone this GitHub repo, or use the `subql` CLI to bootstrap a clean project in the network of your choosing by running `subql init` and following the prompts. + +Don't forget to install dependencies with `npm install` or `yarn install`! + +## Editing your SubQuery project + +Although this is a working example SubQuery project, you can edit the SubQuery project by changing the following files: + +- The project manifest in `project.yaml` defines the key project configuration and mapping handler filters +- The GraphQL Schema (`schema.graphql`) defines the shape of the resulting data that you are using SubQuery to index +- The Mapping functions in `src/mappings/` directory are typescript functions that handle transformation logic + +SubQuery supports various layer-1 blockchain networks and provides [dedicated quick start guides](https://academy.subquery.network/quickstart/quickstart.html) as well as [detailed technical documentation](https://academy.subquery.network/build/introduction.html) for each of them. + +## Run your project + +_If you get stuck, find out how to get help below._ + +The simplest way to run your project is by running `yarn dev` or `npm run-script dev`. This does all of the following: + +1. `yarn codegen` - Generates types from the GraphQL schema definition and contract ABIs and saves them in the `/src/types` directory. This must be done after each change to the `schema.graphql` file or the contract ABIs +2. `yarn build` - Builds and packages the SubQuery project into the `/dist` directory +3. `docker-compose pull && docker-compose up` - Runs a Docker container with an indexer, PostgeSQL DB, and a query service. This requires [Docker to be installed](https://docs.docker.com/engine/install) and running locally. The configuration for this container is set from your `docker-compose.yml` + +You can observe the three services start, and once all are running (it may take a few minutes on your first start), please open your browser and head to [http://localhost:3000](http://localhost:3000) - you should see a GraphQL playground showing with the schemas ready to query. [Read the docs for more information](https://academy.subquery.network/run_publish/run.html) or [explore the possible service configuration for running SubQuery](https://academy.subquery.network/run_publish/references.html). + +## Query your project + +For this project, you can try to query with the following GraphQL code to get a taste of how it works. + +```graphql +{ + query { + transactions(first: 5, orderBy: VALUE_DESC) { + totalCount + nodes { + id + transactionHash + blockHeight + from + to + value + contractAddress + } + } + } + approvals(first: 5) { + nodes { + id + owner + spender + value + contractAddress + } + } +} +``` + +You can explore the different possible queries and entities to help you with GraphQL using the documentation draw on the right. + +## Publish your project + +SubQuery is open-source, meaning you have the freedom to run it in the following three ways: + +- Locally on your own computer (or a cloud provider of your choosing), [view the instructions on how to run SubQuery Locally](https://academy.subquery.network/run_publish/run.html) +- By publishing it to our enterprise-level [Managed Service](https://managedservice.subquery.network), where we'll host your SubQuery project in production ready services for mission critical data with zero-downtime blue/green deployments. We even have a generous free tier. [Find out how](https://academy.subquery.network/run_publish/publish.html) +- [Coming Soon] By publishing it to the decentralised [SubQuery Network](https://subquery.network/network), the most open, performant, reliable, and scalable data service for dApp developers. The SubQuery Network indexes and services data to the global community in an incentivised and verifiable way + +## What Next? + +Take a look at some of our advanced features to take your project to the next level! + +- [**Multi-chain indexing support**](https://academy.subquery.network/build/multi-chain.html) - SubQuery allows you to index data from across different layer-1 networks into the same database, this allows you to query a single endpoint to get data for all supported networks. +- [**Dynamic Data Sources**](https://academy.subquery.network/build/dynamicdatasources.html) - When you want to index factory contracts, for example on a DEX or generative NFT project. +- [**Project Optimisation Advice**](https://academy.subquery.network/build/optimisation.html) - Some common tips on how to tweak your project to maximise performance. +- [**GraphQL Subscriptions**](https://academy.subquery.network/run_publish/subscription.html) - Build more reactive front end applications that subscribe to changes in your SubQuery project. + +## Need Help? + +The fastest way to get support is by [searching our documentation](https://academy.subquery.network), or by [joining our discord](https://discord.com/invite/subquery) and messaging us in the `#technical-support` channel. diff --git a/Peaq/peaq-starter/docker-compose.yml b/Peaq/peaq-starter/docker-compose.yml new file mode 100644 index 00000000..57a72876 --- /dev/null +++ b/Peaq/peaq-starter/docker-compose.yml @@ -0,0 +1,66 @@ +version: "3" + +services: + postgres: + build: + context: . + dockerfile: ./docker/pg-Dockerfile + ports: + - 5432:5432 + volumes: + - .data/postgres:/var/lib/postgresql/data + environment: + POSTGRES_PASSWORD: postgres + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres"] + interval: 5s + timeout: 5s + retries: 5 + + subquery-node: + image: subquerynetwork/subql-node-substrate:latest + depends_on: + "postgres": + condition: service_healthy + restart: unless-stopped + environment: + DB_USER: postgres + DB_PASS: postgres + DB_DATABASE: postgres + DB_HOST: postgres + DB_PORT: 5432 + volumes: + - ./:/app + command: + - ${SUB_COMMAND:-} # set SUB_COMMAND env variable to "test" to run tests + - -f=/app + - --db-schema=app + - --workers=4 + - --batch-size=30 + - --unfinalized-blocks=true + healthcheck: + test: ["CMD", "curl", "-f", "http://subquery-node:3000/ready"] + interval: 3s + timeout: 5s + retries: 10 + + graphql-engine: + image: subquerynetwork/subql-query:latest + ports: + - 3000:3000 + depends_on: + "postgres": + condition: service_healthy + "subquery-node": + condition: service_healthy + restart: always + environment: + DB_USER: postgres + DB_PASS: postgres + DB_DATABASE: postgres + DB_HOST: postgres + DB_PORT: 5432 + command: + - --name=app + - --playground + - --indexer=http://subquery-node:3000 diff --git a/Peaq/peaq-starter/docker/load-extensions.sh b/Peaq/peaq-starter/docker/load-extensions.sh new file mode 100644 index 00000000..6d33f863 --- /dev/null +++ b/Peaq/peaq-starter/docker/load-extensions.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" < = { + specVersion: "1.0.0", + version: "0.0.1", + name: "peaq-starter", + description: `A basic Frontier EVM example project with an event and call handler. Read more + about this at https://university.subquery.network/create/frontier/. This + project can be use as a starting point for developing your SubQuery project`, + runner: { + node: { + name: "@subql/node", + version: ">=3.0.1", + }, + query: { + name: "@subql/query", + version: "*", + }, + }, + schema: { + file: "./schema.graphql", + }, + network: { + /* The genesis hash of the network (hash of block 0) */ + chainId: + "0xd2a5d385932d1f650dae03ef8e2748983779ee342c614f80854d32b8cd8fa48c", + /** + * These endpoint(s) should be public non-pruned archive node + * We recommend providing more than one endpoint for improved reliability, performance, and uptime + * Public nodes may be rate limited, which can affect indexing speed + * When developing your project we suggest getting a private API key + * If you use a rate limited endpoint, adjust the --batch-size and --workers parameters + * These settings can be found in your docker-compose.yaml, they will slow indexing but prevent your project being rate limited + */ + endpoint: ["wss://mpfn1.peaq.network"], + }, + dataSources: [ + { + kind: "substrate/FrontierEvm", + startBlock: 1, + processor: { + file: "./node_modules/@subql/frontier-evm-processor/dist/bundle.js", + options: { + abi: "erc20", + // address: '0x0000000000000000000000000000000000000000' // A specific contract to index + }, + }, + assets: new Map([["erc20", { file: "./erc20.abi.json" }]]), + mapping: { + file: "./dist/index.js", + handlers: [ + { + handler: "handleEvmEvent", + kind: "substrate/FrontierEvmEvent", + filter: { + topics: [ + "Transfer(address indexed from,address indexed to,uint256 value)", + ], + }, + }, + { + handler: "handleEvmCall", + kind: "substrate/FrontierEvmCall", + filter: { + function: "approve(address to,uint256 value)", + }, + }, + ], + }, + }, + ], +}; + +// Must set default to the project instance +export default project; diff --git a/Peaq/peaq-starter/schema.graphql b/Peaq/peaq-starter/schema.graphql new file mode 100644 index 00000000..24d64105 --- /dev/null +++ b/Peaq/peaq-starter/schema.graphql @@ -0,0 +1,17 @@ +type Transaction @entity { + id: ID! # Unique identifier + transactionHash: String! + value: BigInt + to: String + from: String + blockHeight: String + contractAddress: String! +} + +type Approval @entity { + id: ID! # Unique identifier + value: BigInt + owner: String! + spender: String! + contractAddress: String! +} diff --git a/Peaq/peaq-starter/src/index.ts b/Peaq/peaq-starter/src/index.ts new file mode 100644 index 00000000..89dc0c5f --- /dev/null +++ b/Peaq/peaq-starter/src/index.ts @@ -0,0 +1,3 @@ +//Exports all handler functions +import "@polkadot/api-augment"; +export * from "./mappings/mappingHandlers"; diff --git a/Peaq/peaq-starter/src/mappings/mappingHandlers.ts b/Peaq/peaq-starter/src/mappings/mappingHandlers.ts new file mode 100644 index 00000000..68b5e75c --- /dev/null +++ b/Peaq/peaq-starter/src/mappings/mappingHandlers.ts @@ -0,0 +1,53 @@ +import { Approval, Transaction } from "../types"; +import { + FrontierEvmEvent, + FrontierEvmCall, +} from "@subql/frontier-evm-processor"; +import { BigNumber } from "ethers"; +import assert from "assert"; + +// Setup types from ABI +type TransferEventArgs = [string, string, BigNumber] & { + from: string; + to: string; + value: BigNumber; +}; + +type ApproveCallArgs = [string, BigNumber] & { + _spender: string; + _value: BigNumber; +}; + +export async function handleEvmEvent( + event: FrontierEvmEvent, +): Promise { + assert(event.transactionHash, "No transactionHash"); + + const transaction = Transaction.create({ + id: `${event.blockNumber}-${event.transactionHash}-${event.logIndex}`, + value: event.args?.value.toBigInt(), + from: event.args?.from, + to: event.args?.to, + blockHeight: event.blockNumber.toString(), + contractAddress: event.address, + transactionHash: event.transactionHash, + }); + await transaction.save(); +} + +export async function handleEvmCall( + event: FrontierEvmCall, +): Promise { + assert(event.args, "No event.args"); + assert(event.to, "No event.to"); + + const approval = Approval.create({ + id: event.hash, + owner: event.from, + value: event.args._value.toBigInt(), + spender: event.args._spender, + contractAddress: event.to, + }); + + await approval.save(); +} diff --git a/Peaq/peaq-starter/src/test/mappingHandlers.test.ts b/Peaq/peaq-starter/src/test/mappingHandlers.test.ts new file mode 100644 index 00000000..d3c8ed58 --- /dev/null +++ b/Peaq/peaq-starter/src/test/mappingHandlers.test.ts @@ -0,0 +1,11 @@ +import { subqlTest } from "@subql/testing"; + +// See https://academy.subquery.network/build/testing.html + +subqlTest( + "handleTransfer test", // Test name + 191, // Block height to test at + [], // Dependent entities + [], // Expected entities + "handleEvent", // handler name +); diff --git a/Peaq/peaq-starter/tsconfig.json b/Peaq/peaq-starter/tsconfig.json new file mode 100644 index 00000000..3837c009 --- /dev/null +++ b/Peaq/peaq-starter/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "esModuleInterop": true, + "declaration": true, + "importHelpers": true, + "resolveJsonModule": true, + "module": "commonjs", + "outDir": "dist", + "rootDir": "src", + "target": "es2017", + "strict": true, + }, + "include": [ + "src/**/*", + "node_modules/@subql/types-core/dist/global.d.ts", + "node_modules/@subql/types/dist/global.d.ts", + ], +}