├── .gitignore ├── README.md ├── docker-compose.yml ├── package.json ├── project.yaml ├── schema.graphql ├── src ├── index.ts └── mappings │ └── mappingHandlers.ts └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | # These are some examples of commonly ignored file patterns. 2 | # You should customize this list as applicable to your project. 3 | # Learn more about .gitignore: 4 | # https://www.atlassian.com/git/tutorials/saving-changes/gitignore 5 | 6 | # Node artifact files 7 | node_modules/ 8 | dist/ 9 | 10 | # lock files 11 | yarn.lock 12 | package-lock.json 13 | 14 | # Compiled Java class files 15 | *.class 16 | 17 | # Compiled Python bytecode 18 | *.py[cod] 19 | 20 | # Log files 21 | *.log 22 | 23 | # Package files 24 | *.jar 25 | 26 | # Maven 27 | target/ 28 | dist/ 29 | src/types 30 | 31 | # JetBrains IDE 32 | .idea/ 33 | 34 | # Unit test reports 35 | TEST*.xml 36 | 37 | # Generated by MacOS 38 | .DS_Store 39 | 40 | # Generated by Windows 41 | Thumbs.db 42 | 43 | # Applications 44 | *.app 45 | *.exe 46 | *.war 47 | 48 | # Large media files 49 | *.mp4 50 | *.tiff 51 | *.avi 52 | *.flv 53 | *.mov 54 | *.wmv 55 | 56 | # Local DataBase files 57 | .data/ 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SubQuery - Starter Package 2 | 3 | 4 | The Starter Package is an example that you can use as a starting point for developing your SubQuery project. 5 | A SubQuery package defines which data The SubQuery will index from the Substrate blockchain, and how it will store it. 6 | 7 | ## Preparation 8 | 9 | #### Environment 10 | 11 | - [Typescript](https://www.typescriptlang.org/) are required to compile project and define types. 12 | 13 | - Both SubQuery CLI and generated Project have dependencies and require [Node](https://nodejs.org/en/). 14 | 15 | 16 | #### Install the SubQuery CLI 17 | 18 | Install SubQuery CLI globally on your terminal by using NPM: 19 | 20 | ``` 21 | npm install -g @subql/cli 22 | ``` 23 | 24 | Run help to see available commands and usage provide by CLI 25 | ``` 26 | subql help 27 | ``` 28 | 29 | ## Initialize the starter package 30 | 31 | Inside the directory in which you want to create the SubQuery project, simply replace `project-name` with your project name and run the command: 32 | ``` 33 | subql init --starter project-name 34 | ``` 35 | Then you should see a folder with your project name has been created inside the directory, you can use this as the start point of your project. And the files should be identical as in the [Directory Structure](https://doc.subquery.network/directory_structure.html). 36 | 37 | Last, under the project directory, run following command to install all the dependency. 38 | ``` 39 | yarn install 40 | ``` 41 | 42 | 43 | ## Configure your project 44 | 45 | In the starter package, we have provided a simple example of project configuration. You will be mainly working on the following files: 46 | 47 | - The Manifest in `project.yaml` 48 | - The GraphQL Schema in `schema.graphql` 49 | - The Mapping functions in `src/mappings/` directory 50 | 51 | For more information on how to write the SubQuery, 52 | check out our doc section on [Define the SubQuery](https://doc.subquery.network/define_a_subquery.html) 53 | 54 | #### Code generation 55 | 56 | In order to index your SubQuery project, it is mandatory to build your project first. 57 | Run this command under the project directory. 58 | 59 | ```` 60 | yarn codegen 61 | ```` 62 | 63 | ## Build the project 64 | 65 | In order to deploy your SubQuery project to our hosted service, it is mandatory to pack your configuration before upload. 66 | Run pack command from root directory of your project will automatically generate a `your-project-name.tgz` file. 67 | 68 | ``` 69 | yarn build 70 | ``` 71 | 72 | ## Indexing and Query 73 | 74 | #### Run required systems in docker 75 | 76 | 77 | Under the project directory run following command: 78 | 79 | ``` 80 | docker-compose pull && docker-compose up 81 | ``` 82 | #### Query the project 83 | 84 | Open your browser and head to `http://localhost:3000`. 85 | 86 | Finally, you should see a GraphQL playground is showing in the explorer and the schemas that ready to query. 87 | 88 | For the `subql-starter` project, you can try to query with the following code to get a taste of how it works. 89 | 90 | ````graphql 91 | { 92 | query{ 93 | starterEntities(first:10){ 94 | nodes{ 95 | field1, 96 | field2, 97 | field3 98 | } 99 | } 100 | } 101 | } 102 | ```` 103 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | postgres: 5 | image: postgres:12-alpine 6 | ports: 7 | - 5432:5432 8 | volumes: 9 | - .data/postgres:/var/lib/postgresql/data 10 | environment: 11 | POSTGRES_PASSWORD: postgres 12 | 13 | subquery-node: 14 | image: onfinality/subql-node:latest 15 | depends_on: 16 | - "postgres" 17 | restart: always 18 | environment: 19 | DB_USER: postgres 20 | DB_PASS: postgres 21 | DB_DATABASE: postgres 22 | DB_HOST: postgres 23 | DB_PORT: 5432 24 | volumes: 25 | - ./:/app 26 | command: 27 | - -f=/app 28 | - --local 29 | 30 | graphql-engine: 31 | image: onfinality/subql-query:latest 32 | ports: 33 | - 3000:3000 34 | depends_on: 35 | - "postgres" 36 | - "subquery-node" 37 | restart: always 38 | environment: 39 | DB_USER: postgres 40 | DB_PASS: postgres 41 | DB_DATABASE: postgres 42 | DB_HOST: postgres 43 | DB_PORT: 5432 44 | command: 45 | - --name=app 46 | - --playground 47 | - --indexer=http://subquery-node:3000 48 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "HelloWorld", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "dist/index.js", 6 | "scripts": { 7 | "build": "tsc -b", 8 | "prepack": "rm -rf dist && npm build", 9 | "test": "jest", 10 | "codegen": "./node_modules/.bin/subql codegen" 11 | }, 12 | "homepage": "https://github.com/subquery/subql-starter", 13 | "repository": "github:subquery/subql-starter", 14 | "files": [ 15 | "dist", 16 | "schema.graphql", 17 | "project.yaml" 18 | ], 19 | "author": "Arkda12", 20 | "license": "Apache-2.0", 21 | "devDependencies": { 22 | "@polkadot/api": "6.4.1", 23 | "@subql/types": "0.10.0", 24 | "typescript": "4.4.4", 25 | "@subql/cli": "0.14.0" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /project.yaml: -------------------------------------------------------------------------------- 1 | specVersion: 0.0.1 2 | description: '' 3 | repository: '' 4 | schema: ./schema.graphql 5 | network: 6 | endpoint: wss://polkadot.api.onfinality.io/public-ws 7 | dictionary: https://api.subquery.network/sq/subquery/dictionary-polkadot 8 | dataSources: 9 | - name: main 10 | kind: substrate/Runtime 11 | startBlock: 1 12 | mapping: 13 | handlers: 14 | - handler: handleBlock 15 | kind: substrate/BlockHandler 16 | - handler: handleEvent 17 | kind: substrate/EventHandler 18 | filter: 19 | module: balances 20 | method: Deposit 21 | - handler: handleCall 22 | kind: substrate/CallHandler 23 | -------------------------------------------------------------------------------- /schema.graphql: -------------------------------------------------------------------------------- 1 | type StarterEntity @entity { 2 | id: ID! 3 | blockHeight: Int! 4 | } 5 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | //Exports all handler functions 2 | export * from './mappings/mappingHandlers' 3 | -------------------------------------------------------------------------------- /src/mappings/mappingHandlers.ts: -------------------------------------------------------------------------------- 1 | import { SubstrateExtrinsic, SubstrateEvent, SubstrateBlock } from '@subql/types'; 2 | import { StarterEntity } from '../types'; 3 | import { Balance } from '@polkadot/types/interfaces'; 4 | 5 | export async function handleBlock(block: SubstrateBlock): Promise { 6 | //Create a new starterEntity with ID using block hash 7 | let record = new StarterEntity(block.block.header.hash.toString()); 8 | //Record block number 9 | record.blockHeight = block.block.header.number.toNumber(); 10 | await record.save(); 11 | } 12 | 13 | export async function handleEvent(event: SubstrateEvent): Promise { 14 | const { 15 | event: { 16 | data: [account, balance], 17 | }, 18 | } = event; 19 | //Retrieve the record by its ID 20 | const record = await StarterEntity.get(event.extrinsic.block.block.header.hash.toString()); 21 | //Big integer type Balance of a transfer event 22 | await record.save(); 23 | } 24 | 25 | export async function handleCall(extrinsic: SubstrateExtrinsic): Promise { 26 | const record = await StarterEntity.get(extrinsic.block.block.header.hash.toString()); 27 | //Date type timestamp 28 | //Boolean tyep 29 | await record.save(); 30 | } 31 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "emitDecoratorMetadata": true, 4 | "experimentalDecorators": true, 5 | "esModuleInterop": true, 6 | "declaration": true, 7 | "importHelpers": true, 8 | "resolveJsonModule": true, 9 | "module": "commonjs", 10 | "outDir": "dist", 11 | "rootDir": "src", 12 | "target": "es2017" 13 | }, 14 | "include": [ 15 | "src/**/*", 16 | "node_modules/@subql/types/dist/global.d.ts" 17 | ] 18 | } 19 | --------------------------------------------------------------------------------