├── .gitignore ├── LICENSE ├── README.md ├── block-timestamp ├── .DS_Store ├── README.md ├── docker-compose.yml ├── package.json ├── project.yaml ├── schema.graphql ├── src │ ├── index.ts │ └── mappings │ │ └── BlockTs.ts └── tsconfig.json ├── entity-relation ├── README.md ├── docker-compose.yml ├── package.json ├── project.yaml ├── schema.graphql ├── src │ ├── index.ts │ └── mappings │ │ ├── AccountTransfers.ts │ │ └── Calls.ts └── tsconfig.json ├── extrinsic-finalized-block ├── README.md ├── docker-compose.yml ├── package.json ├── project.yaml ├── schema.graphql ├── src │ ├── index.ts │ └── mappings │ │ └── Extrinsic.ts └── tsconfig.json ├── kitty ├── README.md ├── docker-compose.yml ├── package.json ├── project.yaml ├── schema.graphql ├── src │ ├── api-interfaces │ │ ├── definitions.ts │ │ ├── kitties │ │ │ └── definitions.ts │ │ └── kitty.json │ ├── index.ts │ └── mappings │ │ └── Kitty.ts └── tsconfig.json ├── sum-reward ├── README.md ├── docker-compose.yml ├── package-lock.json ├── package.json ├── project.yaml ├── schema.graphql ├── src │ ├── index.ts │ └── mappings │ │ ├── Reward.ts │ │ └── Staking.ts └── tsconfig.json └── validator-threshold ├── README.md ├── docker-compose.yml ├── package.json ├── project.yaml ├── schema.graphql ├── src ├── index.ts └── mappings │ └── ValidatorThreshold.ts └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | */src/types 4 | yarn.lock 5 | .data 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![open grant logo](https://raw.githubusercontent.com/w3f/General-Grants-Program/master/src/badge_black.svg) 2 | # subquery-examples 3 | [Subquery](https://github.com/subquery/subql) is a Web3 Foundation Open Grant Project. 4 | This repo includes examples that we made to demonstrate how to use subquery. 5 | 6 | | Example | Description | Keywords | 7 | |---------------------------|------------------------------------------------------|--------------| 8 | | [extrinsic-finalized-block](extrinsic-finalized-block) | Index extrinsics and so they can be queried by hash. | blockHandler | 9 | | [block-timestamp](block-timestamp) | Indexes timestamp of each finalized block. | callHandler | 10 | | [sum-reward](sum-reward) | Indexes staking bond, reward and slash from events of finalized block. | eventHandler | 11 | | [kitty](kitty) | Indexes birthinfo of kitties. | callHandler, eventHandler, customTypes | 12 | | [validator-threshold](validator-threshold) | Indexes the least staking amount required for a validator to be elected. | blockHandler, @polkadot/api | 13 | | [entity-relation](entity-relation) | Indexes balance transfers between accounts, also indexes utility batchAll to find out the content of the extrinsic calls | One-to-many, many-to-many relationship | 14 | -------------------------------------------------------------------------------- /block-timestamp/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/subquery/subql-examples/13ea30789bbd742655bff17c0e87d5bb44a03ea0/block-timestamp/.DS_Store -------------------------------------------------------------------------------- /block-timestamp/README.md: -------------------------------------------------------------------------------- 1 | # What is SubQuery? 2 | 3 | SubQuery powers the next generation of Polkadot dApps by allowing developers to extract, transform and query blockchain data in real time using GraphQL. In addition to this, SubQuery provides production quality hosting infrastructure to run these projects in. 4 | 5 | # SubQuery Example - Timestamp of Blocks 6 | 7 | This subquery example indexes the timestamp of each finalized block and it is an example of a CallHandler. By processing the `timestamp.set` extrinsic, and extracting the first arguments of it, we can retrieve the timestamp. 8 | 9 | # Getting Started 10 | 11 | ### 1. Clone the entire subql-example repository 12 | 13 | ```shell 14 | git clone https://github.com/subquery/subql-examples.git 15 | 16 | ``` 17 | 18 | ### 2. Install dependencies 19 | 20 | ```shell 21 | cd block-timestamp 22 | yarn 23 | ``` 24 | 25 | ### 3. Generate types 26 | 27 | ```shell 28 | yarn codegen 29 | ``` 30 | 31 | ### 4. Build the project 32 | 33 | ```shell 34 | yarn build 35 | ``` 36 | 37 | ### 5. Run locally 38 | 39 | ```shell 40 | yarn start:docker 41 | ``` 42 | 43 | ### 6. Example query to run 44 | 45 | ```shell 46 | { 47 | query{ 48 | blockTs{ 49 | nodes{ 50 | blockHeight, 51 | timestamp 52 | } 53 | } 54 | } 55 | } 56 | ``` 57 | 58 | # Understanding this project 59 | 60 | As mentioned above, this project has a function called handleTimestampSet which is defined in the mappings file called BlockTs.ts. It uses a [CallHandler](https://doc.subquery.network/create/mapping.html#call-handler) which is defined in the [manifest file](https://doc.subquery.network/create/manifest.html) (project.yaml) as "kind: substrate/CallHandler" 61 | 62 | The [schema.graphql](https://doc.subquery.network/create/graphql.html) file defines the variables blockHeight and timestamp which are both mandatory and of type BigInt and Date respectively. 63 | 64 | If we examine the code in BlockTs.ts in more detail, you can see that the function handleTimestampSet takes one argument of type SubstrateExtrinsic. It then creates a new instance of BlockTs passing in the extrinsic.blockblock.header.hash argument as a string and assigning this to the variable record. 65 | 66 | Next, the blocknumber is converted to a BigInt and assigned to record.blockHeight which is a BlockTs entity meaning that the fields within are accessed with a dot. 67 | 68 | For the timestamp, this is extracted from the first argument of the extrinsic using the moments JS library. It then gets converted a Date object, then serialized as a number before being assigned to the timestamp field. 69 | -------------------------------------------------------------------------------- /block-timestamp/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_POST: 5432 24 | volumes: 25 | - ./:/app 26 | command: 27 | - -f=/app 28 | 29 | graphql-engine: 30 | image: onfinality/subql-query:latest 31 | ports: 32 | - 3000:3000 33 | depends_on: 34 | - "postgres" 35 | restart: always 36 | environment: 37 | DB_USER: postgres 38 | DB_PASS: postgres 39 | DB_DATABASE: postgres 40 | DB_HOST: postgres 41 | DB_PORT: 5432 42 | command: 43 | - --name=app 44 | - --playground 45 | 46 | -------------------------------------------------------------------------------- /block-timestamp/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "block-timestamp", 3 | "version": "0.1.0", 4 | "description": "This subquery indexes timestamp of each finalized block", 5 | "main": "dist/index.js", 6 | "homepage": "https://github.com/subquery/subql-examples", 7 | "repository": "github:subquery/subql-examples", 8 | "scripts": { 9 | "build": "tsc -b", 10 | "prepack": "rm -rf dist && npm build", 11 | "start:docker": "docker-compose pull && docker-compose up --remove-orphans", 12 | "codegen": "./node_modules/.bin/subql codegen" 13 | }, 14 | "files": [ 15 | "dist", 16 | "schema.graphql", 17 | "project.yaml" 18 | ], 19 | "author": "Ian He", 20 | "license": "Apache-2.0", 21 | "devDependencies": { 22 | "@polkadot/api": "^4", 23 | "@subql/types": "^0.7.1", 24 | "typescript": "^4.1.3", 25 | "@subql/cli": "^0.9.3" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /block-timestamp/project.yaml: -------------------------------------------------------------------------------- 1 | specVersion: "0.0.1" 2 | description: "This subquery indexes timestamp of each block" 3 | repository: "https://github.com/subquery/subql-examples" 4 | 5 | schema: "./schema.graphql" 6 | 7 | network: 8 | endpoint: "wss://polkadot.api.onfinality.io/public-ws" 9 | 10 | dataSources: 11 | - name: runtime 12 | kind: substrate/Runtime 13 | startBlock: 500000 14 | mapping: 15 | handlers: 16 | - handler: handleTimestampSet 17 | kind: substrate/CallHandler 18 | filter: 19 | module: timestamp 20 | method: set 21 | -------------------------------------------------------------------------------- /block-timestamp/schema.graphql: -------------------------------------------------------------------------------- 1 | type BlockTs @entity { 2 | # BlockHash 3 | id: ID! 4 | 5 | blockHeight: BigInt! 6 | 7 | timestamp: Date! 8 | } 9 | -------------------------------------------------------------------------------- /block-timestamp/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './mappings/BlockTs'; 2 | -------------------------------------------------------------------------------- /block-timestamp/src/mappings/BlockTs.ts: -------------------------------------------------------------------------------- 1 | import {BlockTs} from '../types/models/BlockTs'; 2 | import {SubstrateExtrinsic} from "@subql/types"; 3 | import {Compact} from '@polkadot/types'; 4 | import {Moment} from '@polkadot/types/interfaces'; 5 | 6 | export async function handleTimestampSet(extrinsic: SubstrateExtrinsic): Promise { 7 | const record = new BlockTs(extrinsic.block.block.header.hash.toString()); 8 | record.blockHeight = extrinsic.block.block.header.number.toBigInt(); 9 | const moment = extrinsic.extrinsic.args[0] as Compact; 10 | record.timestamp = new Date(moment.toNumber()); 11 | await record.save(); 12 | } 13 | -------------------------------------------------------------------------------- /block-timestamp/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 | -------------------------------------------------------------------------------- /entity-relation/README.md: -------------------------------------------------------------------------------- 1 | # What is SubQuery? 2 | 3 | SubQuery powers the next generation of Polkadot dApps by allowing developers to extract, transform and query blockchain data in real time using GraphQL. In addition to this, SubQuery provides production quality hosting infrastructure to run these projects in. 4 | 5 | # SubQuery Example - Entity relations 6 | 7 | This subquery indexes balance transfers between accounts, it is designed to demonstrate the many-to-many relationship within these entities. 8 | 9 | This subquery also indexes the utility batchAll calls which are formated in tree structure, and we established an one-to-many relationship within those entities, allow us to understand the actual actions of this extrinsic and know its position in the tree. 10 | 11 | # Getting Started 12 | 13 | ### 1. Clone the entire subql-example repository 14 | 15 | ```shell 16 | git clone https://github.com/subquery/subql-examples.git 17 | 18 | ``` 19 | ### 2. Install dependencies 20 | 21 | ```shell 22 | cd entity-relation 23 | # Yarn 24 | yarn 25 | 26 | #NPM 27 | npm install 28 | ``` 29 | 30 | ### 3. Generate types 31 | 32 | ```shell 33 | #Yarn 34 | yarn codegen 35 | 36 | #NPM 37 | npm run-script codegen 38 | ``` 39 | 40 | ### 4. Build the project 41 | 42 | ```shell 43 | #Yarn 44 | yarn build 45 | 46 | #NPM 47 | npm run-script build 48 | ``` 49 | 50 | ### 5. Run locally 51 | 52 | ```shell 53 | #Yarn 54 | yarn start:docker 55 | 56 | #NPM 57 | npm run start:docker 58 | ``` 59 | ### 6. Example queries to run 60 | ```shell 61 | { 62 | query{ 63 | transfers{ 64 | nodes{ 65 | amount 66 | } 67 | } 68 | } 69 | } 70 | ``` -------------------------------------------------------------------------------- /entity-relation/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_POST: 5432 24 | volumes: 25 | - ./:/app 26 | command: 27 | - -f=/app 28 | - --local #remove 29 | 30 | graphql-engine: 31 | image: onfinality/subql-query:latest 32 | ports: 33 | - 3000:3000 34 | depends_on: 35 | - "postgres" 36 | restart: always 37 | environment: 38 | DB_USER: postgres 39 | DB_PASS: postgres 40 | DB_DATABASE: postgres 41 | DB_HOST: postgres 42 | DB_PORT: 5432 43 | command: 44 | - --name=app 45 | - --playground 46 | 47 | -------------------------------------------------------------------------------- /entity-relation/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "entity-relation", 3 | "version": "1.0.0", 4 | "description": "This is an example of using 1-N relations", 5 | "main": "dist/index.js", 6 | "scripts": { 7 | "build": "tsc -b", 8 | "prepack": "rm -rf dist && npm build", 9 | "start:docker": "docker-compose pull && docker-compose up --remove-orphans", 10 | "codegen": "./node_modules/.bin/subql codegen" 11 | }, 12 | "files": [ 13 | "dist", 14 | "schema.graphql", 15 | "project.yaml" 16 | ], 17 | "author": "Jay Ji", 18 | "license": "Apache-2.0", 19 | "dependencies": { 20 | "lodash": "^4.17.21" 21 | }, 22 | "devDependencies": { 23 | "@polkadot/api": "^4", 24 | "@subql/types": "^0.7.1", 25 | "typescript": "^4.1.3", 26 | "@subql/cli": "^0.9.3" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /entity-relation/project.yaml: -------------------------------------------------------------------------------- 1 | specVersion: 0.0.1 2 | description: This is an example of using 1-N relations 3 | repository: '' 4 | schema: ./schema.graphql 5 | network: 6 | endpoint: 'wss://polkadot.api.onfinality.io/public-ws' 7 | dataSources: 8 | - name: main 9 | kind: substrate/Runtime 10 | startBlock: 1 11 | mapping: 12 | handlers: 13 | - handler: handleCall 14 | kind: substrate/CallHandler 15 | filter: 16 | module: utility 17 | method: batchAll 18 | success: true 19 | - handler: handleTransfer 20 | kind: substrate/EventHandler 21 | filter: 22 | module: balances 23 | method: Transfer 24 | -------------------------------------------------------------------------------- /entity-relation/schema.graphql: -------------------------------------------------------------------------------- 1 | type Account @entity { 2 | 3 | id: ID! 4 | 5 | transfers: [Transfer] @derivedFrom(field: "from") 6 | 7 | } 8 | 9 | type Transfer @entity { 10 | 11 | id: ID! 12 | 13 | amount: BigInt 14 | 15 | to: Account! 16 | 17 | from: Account! 18 | 19 | } 20 | 21 | type Call @entity { 22 | 23 | id: ID! 24 | 25 | module: String! 26 | 27 | method: String! 28 | 29 | parentCall: Call 30 | 31 | childCalls: [Call] @derivedFrom(field: "parentCall") 32 | 33 | } 34 | 35 | -------------------------------------------------------------------------------- /entity-relation/src/index.ts: -------------------------------------------------------------------------------- 1 | //Exports all handler functions 2 | export * from './mappings/AccountTransfers' 3 | export * from './mappings/Calls' 4 | 5 | -------------------------------------------------------------------------------- /entity-relation/src/mappings/AccountTransfers.ts: -------------------------------------------------------------------------------- 1 | import { SubstrateEvent } from '@subql/types'; 2 | import { Account } from '../types/models/Account'; 3 | import { Transfer } from '../types/models/Transfer'; 4 | import { Balance } from '@polkadot/types/interfaces'; 5 | 6 | async function ensureAccounts(accountIds: string[]): Promise { 7 | for (const accountId of accountIds) { 8 | const account = await Account.get(accountId); 9 | if (!account) { 10 | await new Account(accountId).save(); 11 | } 12 | } 13 | } 14 | 15 | export async function handleTransfer(event: SubstrateEvent): Promise { 16 | const { 17 | event: { 18 | data: [from, to, amount], 19 | }, 20 | } = event; 21 | await ensureAccounts([from.toString(), to.toString()]); 22 | const transferInfo = new Transfer( 23 | `${event.block.block.header.number.toNumber()}-${event.idx}`, 24 | ); 25 | transferInfo.fromId = from.toString(); 26 | transferInfo.toId = to.toString(); 27 | transferInfo.amount = (amount as Balance).toBigInt(); 28 | await transferInfo.save(); 29 | } 30 | -------------------------------------------------------------------------------- /entity-relation/src/mappings/Calls.ts: -------------------------------------------------------------------------------- 1 | import { SubstrateExtrinsic } from '@subql/types'; 2 | import { Call } from '../types/models/Call'; 3 | import { Vec } from '@polkadot/types'; 4 | import { AnyTuple, CallBase } from '@polkadot/types/types'; 5 | import { flatten } from "lodash" 6 | 7 | function extractCalls( 8 | call: CallBase, 9 | id: number, 10 | parentCallId: string, 11 | isExtrinsic: boolean, 12 | ): Call[] { 13 | const callId = `${parentCallId}-${id}`; 14 | const entity = new Call(callId); 15 | const entities = [] as Call[]; 16 | entity.method = call.method; 17 | entity.module = call.section; 18 | entity.parentCallId = isExtrinsic ? null : parentCallId; 19 | entities.push(entity); 20 | if (call.method == 'batchAll' && call.section == 'utility') { 21 | const calls = call.args[0] as Vec>; 22 | return entities.concat( 23 | flatten( 24 | calls.map((call, idx) => extractCalls(call, idx, callId, false)) 25 | ) 26 | ); 27 | } else { 28 | return entities; 29 | } 30 | } 31 | 32 | export async function handleCall(extrinsic: SubstrateExtrinsic): Promise { 33 | const calls = extractCalls( 34 | extrinsic.extrinsic.method, 35 | extrinsic.idx, 36 | extrinsic.block.block.header.number.toString(), 37 | true, 38 | ); 39 | await Promise.all(calls.map((call) => call.save())); 40 | } 41 | -------------------------------------------------------------------------------- /entity-relation/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 | -------------------------------------------------------------------------------- /extrinsic-finalized-block/README.md: -------------------------------------------------------------------------------- 1 | # SubQuery Example - Extrinsics within Finalized Blocks 2 | 3 | This subquery will index extrinsics and so they can be queried by hash 4 | 5 | # Get Started 6 | ### 1. install dependencies 7 | ```shell 8 | yarn 9 | ``` 10 | 11 | ### 2. generate types 12 | ```shell 13 | yarn codegen 14 | ``` 15 | 16 | ### 3. build 17 | ```shell 18 | yarn build 19 | ``` 20 | 21 | ### 4. run locally 22 | ```shell 23 | yarn start:docker 24 | ``` 25 | -------------------------------------------------------------------------------- /extrinsic-finalized-block/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_POST: 5432 24 | volumes: 25 | - ./:/app 26 | command: 27 | - -f=/app 28 | 29 | graphql-engine: 30 | image: onfinality/subql-query:latest 31 | ports: 32 | - 3000:3000 33 | depends_on: 34 | - "postgres" 35 | restart: always 36 | environment: 37 | DB_USER: postgres 38 | DB_PASS: postgres 39 | DB_DATABASE: postgres 40 | DB_HOST: postgres 41 | DB_PORT: 5432 42 | command: 43 | - --name=app 44 | - --playground 45 | 46 | -------------------------------------------------------------------------------- /extrinsic-finalized-block/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "extrinsic-finalized-block", 3 | "version": "0.1.0", 4 | "description": "This subquery indexes which block an extrinsic is finalized in", 5 | "main": "dist/index.js", 6 | "homepage": "https://github.com/subquery/subql-examples", 7 | "repository": "github:subquery/subql-examples", 8 | "scripts": { 9 | "build": "tsc -b", 10 | "prepack": "rm -rf dist && npm build", 11 | "start:docker": "docker-compose pull && docker-compose up --remove-orphans", 12 | "codegen": "./node_modules/.bin/subql codegen" 13 | }, 14 | "files": [ 15 | "dist", 16 | "schema.graphql", 17 | "project.yaml" 18 | ], 19 | "author": "Ian He", 20 | "license": "Apache-2.0", 21 | "devDependencies": { 22 | "@polkadot/api": "^4", 23 | "@subql/types": "^0.7.1", 24 | "typescript": "^4.1.3", 25 | "@subql/cli": "^0.9.3" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /extrinsic-finalized-block/project.yaml: -------------------------------------------------------------------------------- 1 | specVersion: "0.0.1" 2 | description: "This subquery indexes which block a given extrinsic is finalized in" 3 | repository: "https://github.com/subquery/subql-examples" 4 | 5 | schema: "./schema.graphql" 6 | 7 | network: 8 | endpoint: "wss://polkadot.api.onfinality.io/public-ws" 9 | 10 | dataSources: 11 | - name: runtime 12 | kind: substrate/Runtime 13 | startBlock: 1 14 | mapping: 15 | handlers: 16 | - handler: handleBlock 17 | kind: substrate/BlockHandler 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /extrinsic-finalized-block/schema.graphql: -------------------------------------------------------------------------------- 1 | 2 | type Extrinsic @entity { 3 | 4 | id: ID! 5 | 6 | blockHash: String! 7 | 8 | blockHeight: BigInt! 9 | 10 | origin: String 11 | } 12 | -------------------------------------------------------------------------------- /extrinsic-finalized-block/src/index.ts: -------------------------------------------------------------------------------- 1 | export {handleBlock} from './mappings/Extrinsic' 2 | -------------------------------------------------------------------------------- /extrinsic-finalized-block/src/mappings/Extrinsic.ts: -------------------------------------------------------------------------------- 1 | import {SignedBlock} from "@polkadot/types/interfaces"; 2 | import {Extrinsic} from "../types/models/Extrinsic"; 3 | 4 | export async function handleBlock(thisBlock: SignedBlock): Promise { 5 | const blockHash = thisBlock.block.header.hash.toString(); 6 | 7 | await Promise.all(thisBlock.block.extrinsics.map(async extrinsic => { 8 | if (extrinsic.isSigned) { 9 | const origin = extrinsic.signer.toString(); 10 | const entity = new Extrinsic(extrinsic.hash.toString()); 11 | entity.blockHash = blockHash; 12 | entity.blockHeight = thisBlock.block.header.number.toBigInt(); 13 | entity.origin = origin; 14 | await entity.save(); 15 | } 16 | })); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /extrinsic-finalized-block/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 | -------------------------------------------------------------------------------- /kitty/README.md: -------------------------------------------------------------------------------- 1 | # SubQuery Example - Kitty 2 | 3 | This subquery indexes birthinfo of kitties. 4 | 5 | It demonstrates how to use Subquery to extract data from your dedicated chain. 6 | 7 | The chain that works with this example is assignment #5 of [substrate-runtime-developer-academy](https://www.industryconnect.org/substrate-runtime-developer-academy/). 8 | We will ask for their permission so we can post the link of that project (or copy the code). 9 | 10 | # Get Started 11 | ### 1. install dependencies 12 | ```shell 13 | yarn 14 | ``` 15 | 16 | ### 2. generate types 17 | ```shell 18 | yarn codegen 19 | ``` 20 | 21 | ### 3. Generate interfaces for custom substrate chain 22 | 23 | SubQuery can be used on any Substrate-based chain, not just Polkadot or Kusama. 24 | 25 | You can use a custom Substrate-based chain, and we provide tools to importing types, interfaces, and additional methods automatically using [@polkadot/typegen](https://polkadot.js.org/docs/api/examples/promise/typegen/). 26 | 27 | The following instruction explain the integration process. 28 | 29 | #### Preparation 30 | 31 | Create a new directory `api-interfaces` under the project `src` folder to store all required and generated files. We also create an `api-interfaces/kitties` directory as we want to add decoration in API from the `kitties` module. 32 | 33 | ##### Metadata 34 | 35 | We need metadata to generate the actual API endpoints. In the kitty example, we use an endpoint from a local testnet, and it provides additional types. 36 | Follow the steps in [PolkadotJS metadata setup](https://polkadot.js.org/docs/api/examples/promise/typegen#metadata-setup) to retrieve a node's metadata from its **HTTP** endpoint. 37 | 38 | ```shell 39 | curl -H "Content-Type: application/json" -d '{"id":"1", "jsonrpc":"2.0", "method": "state_getMetadata", "params":[]}' http://localhost:9933 40 | ``` 41 | or from its **websocket** endpoint with help from [`websocat`](https://github.com/vi/websocat): 42 | 43 | ```shell 44 | //Install the websocat 45 | brew install websocat 46 | 47 | //Get metadata 48 | echo state_getMetadata | websocat 'ws://127.0.0.1:9944' --jsonrpc 49 | ``` 50 | 51 | Then copy and paste the output to a JSON file. In our [kitty example](https://github.com/subquery/subql-examples/tree/main/kitty), we have created `api-interface/kitty.json`. 52 | 53 | ##### Type definitions 54 | In our assumption, the user should know the specific types and RPC from the chain, and it is defined in the [Manifest](/create/manifest.html). 55 | Following [types setup](https://polkadot.js.org/docs/api/examples/promise/typegen#metadata-setup), we create : 56 | 57 | - `src/api-interfaces/definitions.ts` - this exports all the sub-folder definitions 58 | 59 | ```ts 60 | export { default as kitties } from './kitties/definitions'; 61 | ``` 62 | 63 | - `src/api-interfaces/kitties/definitions.ts` - type definitions for the kitties module 64 | ```ts 65 | export default { 66 | // custom types 67 | types: { 68 | Address: "AccountId", 69 | LookupSource: "AccountId", 70 | KittyIndex: "u32", 71 | Kitty: "[u8; 16]" 72 | }, 73 | // custom rpc : api.rpc.kitties.getKittyPrice 74 | rpc: { 75 | getKittyPrice:{ 76 | description: 'Get Kitty price', 77 | params: [ 78 | { 79 | name: 'at', 80 | type: 'BlockHash', 81 | isHistoric: true, 82 | isOptional: false 83 | }, 84 | { 85 | name: 'kittyIndex', 86 | type: 'KittyIndex', 87 | isOptional: false 88 | } 89 | ], 90 | type: 'Balance' 91 | } 92 | } 93 | } 94 | ``` 95 | 96 | ##### Packages 97 | 98 | 99 | - In the `package.json` development, make sure to add `@polkadot/typegen` as a development dependency and `@polkadot/api` as a regular dependency (ideally the same version). We also need the `ts-node` in development dependency to help us run the scripts. 100 | - We add scripts to run both types `generate:defs` and metadata `generate:meta` generators (in that order, so metadata can use the types). 101 | 102 | Here is a simplified version of `package.json`. Make sure in the **scripts** section the package name is correct and directories are valid. 103 | 104 | ```json 105 | { 106 | "name": "kitty-birthinfo", 107 | "scripts": { 108 | "generate:defs": "ts-node --skip-project node_modules/.bin/polkadot-types-from-defs --package kitty-birthinfo/api-interfaces --input ./src/api-interfaces", 109 | "generate:meta": "ts-node --skip-project node_modules/.bin/polkadot-types-from-chain --package kitty-birthinfo/api-interfaces --endpoint ./src/api-interfaces/kitty.json --output ./src/api-interfaces --strict" 110 | }, 111 | "dependencies": { 112 | "@polkadot/api": "^4.9.2" 113 | }, 114 | "devDependencies": { 115 | "typescript": "^4.1.3", 116 | "@polkadot/typegen": "^4.9.2", 117 | "ts-node": "^8.6.2" 118 | } 119 | } 120 | ``` 121 | 122 | #### Type generation 123 | 124 | Now the preparation is completed, we are ready to generate types and metadata. Run the commands below: 125 | 126 | ```shell 127 | # Yarn to install new dependencies 128 | yarn 129 | 130 | # Generate types 131 | yarn generate:defs 132 | ``` 133 | 134 | In each modules folder (eg `/kitties`), there should now be a generated `types.ts` that defines all interfaces from this modules' definitions, also a file `index.ts` that exports them all. 135 | 136 | Then run: 137 | 138 | ```shell 139 | 140 | # Generate meta 141 | yarn generate:meta 142 | 143 | ``` 144 | 145 | This command will generate the metadata and new api-augment for the APIs. As we don't want to use the built-in API, we need to replace them by adding an explicit override in our `tsconfig.json`. 146 | After updates, the paths in the config look like so (without the comments) 147 | 148 | ```json 149 | { 150 | "compilerOptions": { 151 | // this is the package name we use (in the interface imports, --package for generators) */ 152 | "kitty-birthinfo/*": ["src/*"], 153 | // here we replace the @polkadot/api augmentation with our own, generated from chain 154 | "@polkadot/api/augment": ["src/interfaces/augment-api.ts"], 155 | // replace the augmented types with our own, as generated from definitions 156 | "@polkadot/types/augment": ["src/interfaces/augment-types.ts"] 157 | } 158 | } 159 | ``` 160 | 161 | 162 | #### Usage 163 | 164 | Now in the mapping function, we can show how the metadata and types actually decorate the API. 165 | 166 | 167 | 168 | ```typescript 169 | export async function kittyApiHandler(): Promise { 170 | //return the KittyIndex type 171 | const nextKittyId = await api.query.kitties.nextKittyId(); 172 | // return the Kitty type, input parameters types are AccountId and KittyIndex 173 | const allKitties = await api.query.kitties.kitties('xxxxxxxxx',123) 174 | // return kitty price as Balance type, take one blockHash and KittyIndex for inputs. 175 | // This feature is not support yet, wait to complete https://github.com/subquery/subql/issues/302 176 | const kittyPrice = await api.rpc.kitties.getKittyPrice(undefined,nextKittyId); 177 | logger.info(`Next kitty id ${nextKittyId}`) 178 | } 179 | ``` 180 | 181 | 182 | ### 4. build 183 | ```shell 184 | yarn build 185 | ``` 186 | 187 | ### 5. run locally 188 | ```shell 189 | yarn start:docker 190 | ``` 191 | 192 | ### 6. Publish a project 193 | 194 | **If you wish to publish this project to our explorer, please include the generated files in `src/api-interfaces`.** 195 | -------------------------------------------------------------------------------- /kitty/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_POST: 5432 24 | volumes: 25 | - ./:/app 26 | command: 27 | - -f=/app 28 | 29 | graphql-engine: 30 | image: onfinality/subql-query:latest 31 | ports: 32 | - 3000:3000 33 | depends_on: 34 | - "postgres" 35 | restart: always 36 | environment: 37 | DB_USER: postgres 38 | DB_PASS: postgres 39 | DB_DATABASE: postgres 40 | DB_HOST: postgres 41 | DB_PORT: 5432 42 | command: 43 | - --name=app 44 | - --playground 45 | 46 | -------------------------------------------------------------------------------- /kitty/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "kitty-birthinfo", 3 | "version": "0.1.0", 4 | "description": "This subquery indexes birthinfo of kitties", 5 | "main": "dist/index.js", 6 | "homepage": "https://github.com/subquery/subql-examples", 7 | "repository": "github:subquery/subql-examples", 8 | "scripts": { 9 | "build": "tsc -b", 10 | "prepack": "rm -rf dist && npm build", 11 | "start:docker": "docker-compose pull && docker-compose up --remove-orphans", 12 | "codegen": "./node_modules/.bin/subql codegen", 13 | "interface-build": "yarn generate:defs && yarn generate:meta && tsc -b", 14 | "generate:defs": "ts-node --skip-project node_modules/.bin/polkadot-types-from-defs --package kitty-birthinfo/api-interfaces --input ./src/api-interfaces", 15 | "generate:meta": "ts-node --skip-project node_modules/.bin/polkadot-types-from-chain --package kitty-birthinfo/api-interfaces --endpoint ./src/api-interfaces/kitty.json --output ./src/api-interfaces --strict" 16 | }, 17 | "files": [ 18 | "dist", 19 | "schema.graphql", 20 | "project.yaml" 21 | ], 22 | "author": "Ian He", 23 | "license": "Apache-2.0", 24 | "devDependencies": { 25 | "@polkadot/api": "4.6.1", 26 | "@subql/types": "^0.7.1", 27 | "typescript": "^4.1.3", 28 | "@subql/cli": "^0.9.3", 29 | "@polkadot/typegen": "4.6.1", 30 | "ts-node": "^8.6.2" 31 | }, 32 | "resolutions": { 33 | "@polkadot/api": "4.6.1" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /kitty/project.yaml: -------------------------------------------------------------------------------- 1 | specVersion: "0.0.1" 2 | description: "This subquery indexes kitty's birth info" 3 | repository: "https://github.com/subquery/subql-examples" 4 | 5 | schema: "./schema.graphql" 6 | 7 | network: 8 | endpoint: "ws://host.docker.internal:9944" 9 | types: { 10 | "KittyIndex": "u32", 11 | "Kitty": "[u8; 16]", 12 | } 13 | typesBundle: { 14 | spec: { 15 | chainname: { 16 | rpc: { 17 | kitties: { 18 | getKittyPrice:{ 19 | description: string, 20 | params: [ 21 | { 22 | name: 'at', 23 | type: 'BlockHash', 24 | isHistoric: true, 25 | isOptional: false 26 | }, 27 | { 28 | name: 'kittyIndex', 29 | type: 'KittyIndex', 30 | isOptional: false 31 | } 32 | ], 33 | type: "Balance", 34 | } 35 | } 36 | } 37 | } 38 | } 39 | } 40 | 41 | dataSources: 42 | - name: runtime 43 | kind: substrate/Runtime 44 | startBlock: 1 45 | mapping: 46 | handlers: 47 | - handler: handleKittyCreated 48 | kind: substrate/EventHandler 49 | filter: 50 | module: kitties 51 | method: KittyCreated 52 | - handler: handleKittyBred 53 | kind: substrate/CallHandler 54 | filter: 55 | module: kitties 56 | method: breed 57 | success: true 58 | - handler: handleKittyTransferred 59 | kind: substrate/EventHandler 60 | filter: 61 | module: kitties 62 | method: KittyTransferred 63 | -------------------------------------------------------------------------------- /kitty/schema.graphql: -------------------------------------------------------------------------------- 1 | 2 | type KittyBirthInfo @entity { 3 | # KittyId 4 | id: ID! 5 | 6 | birthBlockHeight: BigInt! 7 | 8 | owner: String! 9 | 10 | firstOwner: String! 11 | 12 | parent1: String 13 | 14 | parent2: String 15 | } 16 | -------------------------------------------------------------------------------- /kitty/src/api-interfaces/definitions.ts: -------------------------------------------------------------------------------- 1 | export { default as kitties } from './kitties/definitions'; -------------------------------------------------------------------------------- /kitty/src/api-interfaces/kitties/definitions.ts: -------------------------------------------------------------------------------- 1 | export default { 2 | types: { 3 | Address: "AccountId", 4 | LookupSource: "AccountId", 5 | KittyIndex: "u32", 6 | Kitty: "[u8; 16]" 7 | }, 8 | rpc: { 9 | getKittyPrice:{ 10 | description: 'Get Kitty price', 11 | params: [ 12 | { 13 | name: 'at', 14 | type: 'BlockHash', 15 | isHistoric: true, 16 | isOptional: false 17 | }, 18 | { 19 | name: 'kittyIndex', 20 | type: 'KittyIndex', 21 | isOptional: false 22 | } 23 | ], 24 | type: 'Balance' 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /kitty/src/api-interfaces/kitty.json: -------------------------------------------------------------------------------- 1 | {"jsonrpc":"2.0","result":"0x6d6574610c281853797374656d011853797374656d401c4163636f756e7401010230543a3a4163636f756e744964944163636f756e74496e666f3c543a3a496e6465782c20543a3a4163636f756e74446174613e00210100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004e8205468652066756c6c206163636f756e7420696e666f726d6174696f6e20666f72206120706172746963756c6172206163636f756e742049442e3845787472696e736963436f756e7400000c753332040004b820546f74616c2065787472696e7369637320636f756e7420666f72207468652063757272656e7420626c6f636b2e2c426c6f636b576569676874010064776569676874733a3a45787472696e7369637357656967687440000000000000000000000000000000000488205468652063757272656e742077656967687420666f722074686520626c6f636b2e40416c6c45787472696e736963734c656e00000c753332040004410120546f74616c206c656e6774682028696e2062797465732920666f7220616c6c2065787472696e736963732070757420746f6765746865722c20666f72207468652063757272656e7420626c6f636b2e24426c6f636b4861736801010538543a3a426c6f636b4e756d6265721c543a3a48617368008000000000000000000000000000000000000000000000000000000000000000000498204d6170206f6620626c6f636b206e756d6265727320746f20626c6f636b206861736865732e3445787472696e736963446174610101050c7533321c5665633c75383e000400043d012045787472696e73696373206461746120666f72207468652063757272656e7420626c6f636b20286d61707320616e2065787472696e736963277320696e64657820746f206974732064617461292e184e756d626572010038543a3a426c6f636b4e756d6265721000000000040901205468652063757272656e7420626c6f636b206e756d626572206265696e672070726f6365737365642e205365742062792060657865637574655f626c6f636b602e28506172656e744861736801001c543a3a4861736880000000000000000000000000000000000000000000000000000000000000000004702048617368206f66207468652070726576696f757320626c6f636b2e3845787472696e73696373526f6f7401001c543a3a486173688000000000000000000000000000000000000000000000000000000000000000000415012045787472696e7369637320726f6f74206f66207468652063757272656e7420626c6f636b2c20616c736f2070617274206f662074686520626c6f636b206865616465722e1844696765737401002c4469676573744f663c543e040004f020446967657374206f66207468652063757272656e7420626c6f636b2c20616c736f2070617274206f662074686520626c6f636b206865616465722e184576656e747301008c5665633c4576656e745265636f72643c543a3a4576656e742c20543a3a486173683e3e040004a0204576656e7473206465706f736974656420666f72207468652063757272656e7420626c6f636b2e284576656e74436f756e740100284576656e74496e646578100000000004b820546865206e756d626572206f66206576656e747320696e2074686520604576656e74733c543e60206c6973742e2c4576656e74546f706963730101021c543a3a48617368845665633c28543a3a426c6f636b4e756d6265722c204576656e74496e646578293e000400282501204d617070696e67206265747765656e206120746f7069632028726570726573656e74656420627920543a3a486173682920616e64206120766563746f72206f6620696e646578657394206f66206576656e747320696e2074686520603c4576656e74733c543e3e60206c6973742e00510120416c6c20746f70696320766563746f727320686176652064657465726d696e69737469632073746f72616765206c6f636174696f6e7320646570656e64696e67206f6e2074686520746f7069632e2054686973450120616c6c6f7773206c696768742d636c69656e747320746f206c6576657261676520746865206368616e67657320747269652073746f7261676520747261636b696e67206d656368616e69736d20616e64e420696e2063617365206f66206368616e67657320666574636820746865206c697374206f66206576656e7473206f6620696e7465726573742e004d01205468652076616c756520686173207468652074797065206028543a3a426c6f636b4e756d6265722c204576656e74496e646578296020626563617573652069662077652075736564206f6e6c79206a7573744d012074686520604576656e74496e64657860207468656e20696e20636173652069662074686520746f70696320686173207468652073616d6520636f6e74656e7473206f6e20746865206e65787420626c6f636b0101206e6f206e6f74696669636174696f6e2077696c6c20626520747269676765726564207468757320746865206576656e74206d69676874206265206c6f73742e484c61737452756e74696d65557067726164650000584c61737452756e74696d6555706772616465496e666f04000455012053746f726573207468652060737065635f76657273696f6e6020616e642060737065635f6e616d6560206f66207768656e20746865206c6173742072756e74696d6520757067726164652068617070656e65642e545570677261646564546f553332526566436f756e74010010626f6f6c0400044d012054727565206966207765206861766520757067726164656420736f207468617420607479706520526566436f756e74602069732060753332602e2046616c7365202864656661756c7429206966206e6f742e38457865637574696f6e50686173650000145068617365040004882054686520657865637574696f6e207068617365206f662074686520626c6f636b2e01282866696c6c5f626c6f636b04185f726174696f1c50657262696c6c040901204120646973706174636820746861742077696c6c2066696c6c2074686520626c6f636b2077656967687420757020746f2074686520676976656e20726174696f2e1872656d61726b041c5f72656d61726b1c5665633c75383e1c6c204d616b6520736f6d65206f6e2d636861696e2072656d61726b2e002c2023203c7765696768743e24202d20604f28312960e0202d2042617365205765696768743a20302e36363520c2b5732c20696e646570656e64656e74206f662072656d61726b206c656e6774682e50202d204e6f204442206f7065726174696f6e732e302023203c2f7765696768743e387365745f686561705f7061676573041470616765730c75363420fc2053657420746865206e756d626572206f6620706167657320696e2074686520576562417373656d626c7920656e7669726f6e6d656e74277320686561702e002c2023203c7765696768743e24202d20604f283129604c202d20312073746f726167652077726974652e64202d2042617365205765696768743a20312e34303520c2b57360202d203120777269746520746f20484541505f5041474553302023203c2f7765696768743e207365745f636f64650410636f64651c5665633c75383e28682053657420746865206e65772072756e74696d6520636f64652e002c2023203c7765696768743e3501202d20604f2843202b2053296020776865726520604360206c656e677468206f662060636f64656020616e642060536020636f6d706c6578697479206f66206063616e5f7365745f636f64656088202d20312073746f726167652077726974652028636f64656320604f28432960292e7901202d20312063616c6c20746f206063616e5f7365745f636f6465603a20604f28532960202863616c6c73206073705f696f3a3a6d6973633a3a72756e74696d655f76657273696f6e6020776869636820697320657870656e73697665292e2c202d2031206576656e742e7d012054686520776569676874206f6620746869732066756e6374696f6e20697320646570656e64656e74206f6e207468652072756e74696d652c206275742067656e6572616c6c792074686973206973207665727920657870656e736976652e902057652077696c6c207472656174207468697320617320612066756c6c20626c6f636b2e302023203c2f7765696768743e5c7365745f636f64655f776974686f75745f636865636b730410636f64651c5665633c75383e201d012053657420746865206e65772072756e74696d6520636f646520776974686f757420646f696e6720616e7920636865636b73206f662074686520676976656e2060636f6465602e002c2023203c7765696768743e90202d20604f2843296020776865726520604360206c656e677468206f662060636f64656088202d20312073746f726167652077726974652028636f64656320604f28432960292e2c202d2031206576656e742e75012054686520776569676874206f6620746869732066756e6374696f6e20697320646570656e64656e74206f6e207468652072756e74696d652e2057652077696c6c207472656174207468697320617320612066756c6c20626c6f636b2e302023203c2f7765696768743e5c7365745f6368616e6765735f747269655f636f6e666967044c6368616e6765735f747269655f636f6e666967804f7074696f6e3c4368616e67657354726965436f6e66696775726174696f6e3e28a02053657420746865206e6577206368616e676573207472696520636f6e66696775726174696f6e2e002c2023203c7765696768743e24202d20604f28312960b0202d20312073746f72616765207772697465206f722064656c6574652028636f64656320604f28312960292ed8202d20312063616c6c20746f20606465706f7369745f6c6f67603a20557365732060617070656e6460204150492c20736f204f28312964202d2042617365205765696768743a20372e32313820c2b57334202d204442205765696768743aa820202020202d205772697465733a204368616e67657320547269652c2053797374656d20446967657374302023203c2f7765696768743e2c7365745f73746f7261676504146974656d73345665633c4b657956616c75653e206c2053657420736f6d65206974656d73206f662073746f726167652e002c2023203c7765696768743e94202d20604f2849296020776865726520604960206c656e677468206f6620606974656d73607c202d206049602073746f72616765207772697465732028604f28312960292e74202d2042617365205765696768743a20302e353638202a206920c2b57368202d205772697465733a204e756d626572206f66206974656d73302023203c2f7765696768743e306b696c6c5f73746f7261676504106b657973205665633c4b65793e2078204b696c6c20736f6d65206974656d732066726f6d2073746f726167652e002c2023203c7765696768743efc202d20604f28494b296020776865726520604960206c656e677468206f6620606b6579736020616e6420604b60206c656e677468206f66206f6e65206b657964202d206049602073746f726167652064656c6574696f6e732e70202d2042617365205765696768743a202e333738202a206920c2b57368202d205772697465733a204e756d626572206f66206974656d73302023203c2f7765696768743e2c6b696c6c5f70726566697808187072656669780c4b6579205f7375626b6579730c7533322c1501204b696c6c20616c6c2073746f72616765206974656d7320776974682061206b657920746861742073746172747320776974682074686520676976656e207072656669782e003d01202a2a4e4f54453a2a2a2057652072656c79206f6e2074686520526f6f74206f726967696e20746f2070726f7669646520757320746865206e756d626572206f66207375626b65797320756e64657241012074686520707265666978207765206172652072656d6f76696e6720746f2061636375726174656c792063616c63756c6174652074686520776569676874206f6620746869732066756e6374696f6e2e002c2023203c7765696768743edc202d20604f285029602077686572652060506020616d6f756e74206f66206b65797320776974682070726566697820607072656669786064202d206050602073746f726167652064656c6574696f6e732e74202d2042617365205765696768743a20302e383334202a205020c2b57380202d205772697465733a204e756d626572206f66207375626b657973202b2031302023203c2f7765696768743e1c7375696369646500286501204b696c6c207468652073656e64696e67206163636f756e742c20617373756d696e6720746865726520617265206e6f207265666572656e636573206f75747374616e64696e6720616e642074686520636f6d706f7369746590206461746120697320657175616c20746f206974732064656661756c742076616c75652e002c2023203c7765696768743e24202d20604f283129607c202d20312073746f72616765207265616420616e642064656c6574696f6e2e54202d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d5c2042617365205765696768743a20382e36323620c2b5731101204e6f2044422052656164206f72205772697465206f7065726174696f6e7320626563617573652063616c6c657220697320616c726561647920696e206f7665726c6179302023203c2f7765696768743e01144045787472696e7369635375636365737304304469737061746368496e666f04b820416e2065787472696e73696320636f6d706c65746564207375636365737366756c6c792e205c5b696e666f5c5d3c45787472696e7369634661696c6564083444697370617463684572726f72304469737061746368496e666f049420416e2065787472696e736963206661696c65642e205c5b6572726f722c20696e666f5c5d2c436f64655570646174656400045420603a636f6465602077617320757064617465642e284e65774163636f756e7404244163636f756e744964047c2041206e6577205c5b6163636f756e745c5d2077617320637265617465642e344b696c6c65644163636f756e7404244163636f756e744964046c20416e205c5b6163636f756e745c5d20776173207265617065642e1838426c6f636b48617368436f756e7438543a3a426c6f636b4e756d626572106009000004d820546865206d6178696d756d206e756d626572206f6620626c6f636b7320746f20616c6c6f7720696e206d6f7274616c20657261732e484d6178696d756d426c6f636b576569676874185765696768742000204aa9d1010000047c20546865206d6178696d756d20776569676874206f66206120626c6f636b2e2044625765696768743c52756e74696d6544625765696768744040787d010000000000e1f505000000000409012054686520776569676874206f662072756e74696d65206461746162617365206f7065726174696f6e73207468652072756e74696d652063616e20696e766f6b652e50426c6f636b457865637574696f6e576569676874185765696768742000f2052a0100000004510120546865206261736520776569676874206f6620657865637574696e67206120626c6f636b2c20696e646570656e64656e74206f6620746865207472616e73616374696f6e7320696e2074686520626c6f636b2e4c45787472696e736963426173655765696768741857656967687420405973070000000004790120546865206261736520776569676874206f6620616e2045787472696e73696320696e2074686520626c6f636b2c20696e646570656e64656e74206f6620746865206f662065787472696e736963206265696e672065786563757465642e484d6178696d756d426c6f636b4c656e6774680c753332100000500004a820546865206d6178696d756d206c656e677468206f66206120626c6f636b2028696e206279746573292e143c496e76616c6964537065634e616d6508150120546865206e616d65206f662073706563696669636174696f6e20646f6573206e6f74206d61746368206265747765656e207468652063757272656e742072756e74696d655420616e6420746865206e65772072756e74696d652e685370656356657273696f6e4e65656473546f496e637265617365084501205468652073706563696669636174696f6e2076657273696f6e206973206e6f7420616c6c6f77656420746f206465637265617365206265747765656e207468652063757272656e742072756e74696d655420616e6420746865206e65772072756e74696d652e744661696c6564546f4578747261637452756e74696d6556657273696f6e0cf0204661696c656420746f2065787472616374207468652072756e74696d652076657273696f6e2066726f6d20746865206e65772072756e74696d652e000d01204569746865722063616c6c696e672060436f72655f76657273696f6e60206f72206465636f64696e67206052756e74696d6556657273696f6e60206661696c65642e4c4e6f6e44656661756c74436f6d706f7369746504010120537569636964652063616c6c6564207768656e20746865206163636f756e7420686173206e6f6e2d64656661756c7420636f6d706f7369746520646174612e3c4e6f6e5a65726f526566436f756e740439012054686572652069732061206e6f6e2d7a65726f207265666572656e636520636f756e742070726576656e74696e6720746865206163636f756e742066726f6d206265696e67207075726765642e006052616e646f6d6e657373436f6c6c656374697665466c6970016052616e646f6d6e657373436f6c6c656374697665466c6970043852616e646f6d4d6174657269616c0100305665633c543a3a486173683e04000c610120536572696573206f6620626c6f636b20686561646572732066726f6d20746865206c61737420383120626c6f636b73207468617420616374732061732072616e646f6d2073656564206d6174657269616c2e2054686973610120697320617272616e67656420617320612072696e672062756666657220776974682060626c6f636b5f6e756d626572202520383160206265696e672074686520696e64657820696e746f20746865206056656360206f664420746865206f6c6465737420686173682e0100000000012454696d657374616d70012454696d657374616d70080c4e6f77010024543a3a4d6f6d656e7420000000000000000004902043757272656e742074696d6520666f72207468652063757272656e7420626c6f636b2e24446964557064617465010010626f6f6c040004b420446964207468652074696d657374616d7020676574207570646174656420696e207468697320626c6f636b3f01040c736574040c6e6f7748436f6d706163743c543a3a4d6f6d656e743e3c5820536574207468652063757272656e742074696d652e00590120546869732063616c6c2073686f756c6420626520696e766f6b65642065786163746c79206f6e63652070657220626c6f636b2e2049742077696c6c2070616e6963206174207468652066696e616c697a6174696f6ed82070686173652c20696620746869732063616c6c206861736e2774206265656e20696e766f6b656420627920746861742074696d652e004501205468652074696d657374616d702073686f756c642062652067726561746572207468616e207468652070726576696f7573206f6e652062792074686520616d6f756e74207370656369666965642062794420604d696e696d756d506572696f64602e00d820546865206469737061746368206f726967696e20666f7220746869732063616c6c206d7573742062652060496e686572656e74602e002c2023203c7765696768743ed0202d20604f285429602077686572652060546020636f6d706c6578697479206f6620606f6e5f74696d657374616d705f73657460a101202d20312073746f72616765207265616420616e6420312073746f72616765206d75746174696f6e2028636f64656320604f28312960292e202862656361757365206f6620604469645570646174653a3a74616b656020696e20606f6e5f66696e616c697a656029b4202d2031206576656e742068616e646c657220606f6e5f74696d657374616d705f7365746020604f285429602e302023203c2f7765696768743e0004344d696e696d756d506572696f6424543a3a4d6f6d656e7420e80300000000000010690120546865206d696e696d756d20706572696f64206265747765656e20626c6f636b732e204265776172652074686174207468697320697320646966666572656e7420746f20746865202a65787065637465642a20706572696f64690120746861742074686520626c6f636b2070726f64756374696f6e206170706172617475732070726f76696465732e20596f75722063686f73656e20636f6e73656e7375732073797374656d2077696c6c2067656e6572616c6c79650120776f726b2077697468207468697320746f2064657465726d696e6520612073656e7369626c6520626c6f636b2074696d652e20652e672e20466f7220417572612c2069742077696c6c20626520646f75626c6520746869737020706572696f64206f6e2064656661756c742073657474696e67732e000210417572610000000000031c4772616e647061013c4772616e64706146696e616c6974791814537461746501006c53746f72656453746174653c543a3a426c6f636b4e756d6265723e04000490205374617465206f66207468652063757272656e7420617574686f72697479207365742e3450656e64696e674368616e676500008c53746f72656450656e64696e674368616e67653c543a3a426c6f636b4e756d6265723e040004c42050656e64696e67206368616e67653a20287369676e616c65642061742c207363686564756c6564206368616e6765292e284e657874466f72636564000038543a3a426c6f636b4e756d626572040004bc206e65787420626c6f636b206e756d6265722077686572652077652063616e20666f7263652061206368616e67652e1c5374616c6c656400008028543a3a426c6f636b4e756d6265722c20543a3a426c6f636b4e756d626572290400049020607472756560206966207765206172652063757272656e746c79207374616c6c65642e3043757272656e7453657449640100145365744964200000000000000000085d0120546865206e756d626572206f66206368616e6765732028626f746820696e207465726d73206f66206b65797320616e6420756e6465726c79696e672065636f6e6f6d696320726573706f6e736962696c697469657329c420696e20746865202273657422206f66204772616e6470612076616c696461746f72732066726f6d2067656e657369732e30536574496453657373696f6e0001051453657449643053657373696f6e496e6465780004001059012041206d617070696e672066726f6d206772616e6470612073657420494420746f2074686520696e646578206f6620746865202a6d6f737420726563656e742a2073657373696f6e20666f722077686963682069747368206d656d62657273207765726520726573706f6e7369626c652e00b82054574f582d4e4f54453a2060536574496460206973206e6f7420756e646572207573657220636f6e74726f6c2e010c4c7265706f72745f65717569766f636174696f6e084865717569766f636174696f6e5f70726f6f66a845717569766f636174696f6e50726f6f663c543a3a486173682c20543a3a426c6f636b4e756d6265723e3c6b65795f6f776e65725f70726f6f6640543a3a4b65794f776e657250726f6f66100d01205265706f727420766f7465722065717569766f636174696f6e2f6d69736265686176696f722e2054686973206d6574686f642077696c6c2076657269667920746865f82065717569766f636174696f6e2070726f6f6620616e642076616c69646174652074686520676976656e206b6579206f776e6572736869702070726f6f66fc20616761696e73742074686520657874726163746564206f6666656e6465722e20496620626f7468206172652076616c69642c20746865206f6666656e6365482077696c6c206265207265706f727465642e707265706f72745f65717569766f636174696f6e5f756e7369676e6564084865717569766f636174696f6e5f70726f6f66a845717569766f636174696f6e50726f6f663c543a3a486173682c20543a3a426c6f636b4e756d6265723e3c6b65795f6f776e65725f70726f6f6640543a3a4b65794f776e657250726f6f66240d01205265706f727420766f7465722065717569766f636174696f6e2f6d69736265686176696f722e2054686973206d6574686f642077696c6c2076657269667920746865f82065717569766f636174696f6e2070726f6f6620616e642076616c69646174652074686520676976656e206b6579206f776e6572736869702070726f6f66fc20616761696e73742074686520657874726163746564206f6666656e6465722e20496620626f7468206172652076616c69642c20746865206f6666656e6365482077696c6c206265207265706f727465642e00110120546869732065787472696e736963206d7573742062652063616c6c656420756e7369676e656420616e642069742069732065787065637465642074686174206f6e6c79190120626c6f636b20617574686f72732077696c6c2063616c6c206974202876616c69646174656420696e206056616c6964617465556e7369676e656460292c206173207375636819012069662074686520626c6f636b20617574686f7220697320646566696e65642069742077696c6c20626520646566696e6564206173207468652065717569766f636174696f6e28207265706f727465722e306e6f74655f7374616c6c6564081464656c617938543a3a426c6f636b4e756d6265726c626573745f66696e616c697a65645f626c6f636b5f6e756d62657238543a3a426c6f636b4e756d6265721c1d01204e6f74652074686174207468652063757272656e7420617574686f7269747920736574206f6620746865204752414e4450412066696e616c69747920676164676574206861732901207374616c6c65642e20546869732077696c6c2074726967676572206120666f7263656420617574686f7269747920736574206368616e67652061742074686520626567696e6e696e672101206f6620746865206e6578742073657373696f6e2c20746f20626520656e6163746564206064656c61796020626c6f636b7320616674657220746861742e205468652064656c617915012073686f756c64206265206869676820656e6f75676820746f20736166656c7920617373756d6520746861742074686520626c6f636b207369676e616c6c696e6720746865290120666f72636564206368616e67652077696c6c206e6f742062652072652d6f726765642028652e672e203130303020626c6f636b73292e20546865204752414e44504120766f7465727329012077696c6c20737461727420746865206e657720617574686f7269747920736574207573696e672074686520676976656e2066696e616c697a656420626c6f636b20617320626173652e5c204f6e6c792063616c6c61626c6520627920726f6f742e010c384e6577417574686f7269746965730434417574686f726974794c69737404d8204e657720617574686f726974792073657420686173206265656e206170706c6965642e205c5b617574686f726974795f7365745c5d1850617573656400049c2043757272656e7420617574686f726974792073657420686173206265656e207061757365642e1c526573756d65640004a02043757272656e7420617574686f726974792073657420686173206265656e20726573756d65642e001c2c50617573654661696c656408090120417474656d707420746f207369676e616c204752414e445041207061757365207768656e2074686520617574686f72697479207365742069736e2774206c697665a8202865697468657220706175736564206f7220616c72656164792070656e64696e67207061757365292e30526573756d654661696c656408150120417474656d707420746f207369676e616c204752414e44504120726573756d65207768656e2074686520617574686f72697479207365742069736e277420706175736564a42028656974686572206c697665206f7220616c72656164792070656e64696e6720726573756d65292e344368616e676550656e64696e6704ec20417474656d707420746f207369676e616c204752414e445041206368616e67652077697468206f6e6520616c72656164792070656e64696e672e1c546f6f536f6f6e04c02043616e6e6f74207369676e616c20666f72636564206368616e676520736f20736f6f6e206166746572206c6173742e60496e76616c69644b65794f776e65727368697050726f6f660435012041206b6579206f776e6572736869702070726f6f662070726f76696465642061732070617274206f6620616e2065717569766f636174696f6e207265706f727420697320696e76616c69642e60496e76616c696445717569766f636174696f6e50726f6f6604350120416e2065717569766f636174696f6e2070726f6f662070726f76696465642061732070617274206f6620616e2065717569766f636174696f6e207265706f727420697320696e76616c69642e584475706c69636174654f6666656e63655265706f7274041901204120676976656e2065717569766f636174696f6e207265706f72742069732076616c69642062757420616c72656164792070726576696f75736c79207265706f727465642e042042616c616e636573012042616c616e6365731034546f74616c49737375616e6365010028543a3a42616c616e6365400000000000000000000000000000000004982054686520746f74616c20756e6974732069737375656420696e207468652073797374656d2e1c4163636f756e7401010230543a3a4163636f756e7449645c4163636f756e74446174613c543a3a42616c616e63653e000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c6c205468652062616c616e6365206f6620616e206163636f756e742e004101204e4f54453a2054686973206973206f6e6c79207573656420696e20746865206361736520746861742074686973206d6f64756c65206973207573656420746f2073746f72652062616c616e6365732e144c6f636b7301010230543a3a4163636f756e744964705665633c42616c616e63654c6f636b3c543a3a42616c616e63653e3e00040008b820416e79206c6971756964697479206c6f636b73206f6e20736f6d65206163636f756e742062616c616e6365732e2501204e4f54453a2053686f756c64206f6e6c79206265206163636573736564207768656e2073657474696e672c206368616e67696e6720616e642066726565696e672061206c6f636b2e3853746f7261676556657273696f6e01002052656c656173657304000c7c2053746f726167652076657273696f6e206f66207468652070616c6c65742e00a020546869732069732073657420746f2076322e302e3020666f72206e6577206e6574776f726b732e0110207472616e736665720810646573748c3c543a3a4c6f6f6b7570206173205374617469634c6f6f6b75703e3a3a536f757263651476616c75654c436f6d706163743c543a3a42616c616e63653e6cd8205472616e7366657220736f6d65206c697175696420667265652062616c616e636520746f20616e6f74686572206163636f756e742e00090120607472616e73666572602077696c6c207365742074686520604672656542616c616e636560206f66207468652073656e64657220616e642072656365697665722e21012049742077696c6c2064656372656173652074686520746f74616c2069737375616e6365206f66207468652073797374656d2062792074686520605472616e73666572466565602e1501204966207468652073656e6465722773206163636f756e742069732062656c6f7720746865206578697374656e7469616c206465706f736974206173206120726573756c74b4206f6620746865207472616e736665722c20746865206163636f756e742077696c6c206265207265617065642e00190120546865206469737061746368206f726967696e20666f7220746869732063616c6c206d75737420626520605369676e65646020627920746865207472616e736163746f722e002c2023203c7765696768743e3101202d20446570656e64656e74206f6e20617267756d656e747320627574206e6f7420637269746963616c2c20676976656e2070726f70657220696d706c656d656e746174696f6e7320666f72cc202020696e70757420636f6e6669672074797065732e205365652072656c617465642066756e6374696f6e732062656c6f772e6901202d20497420636f6e7461696e732061206c696d69746564206e756d626572206f6620726561647320616e642077726974657320696e7465726e616c6c7920616e64206e6f20636f6d706c657820636f6d7075746174696f6e2e004c2052656c617465642066756e6374696f6e733a0051012020202d2060656e737572655f63616e5f77697468647261776020697320616c776179732063616c6c656420696e7465726e616c6c792062757420686173206120626f756e64656420636f6d706c65786974792e2d012020202d205472616e7366657272696e672062616c616e63657320746f206163636f756e7473207468617420646964206e6f74206578697374206265666f72652077696c6c206361757365d420202020202060543a3a4f6e4e65774163636f756e743a3a6f6e5f6e65775f6163636f756e746020746f2062652063616c6c65642e61012020202d2052656d6f76696e6720656e6f7567682066756e64732066726f6d20616e206163636f756e742077696c6c20747269676765722060543a3a4475737452656d6f76616c3a3a6f6e5f756e62616c616e636564602e49012020202d20607472616e736665725f6b6565705f616c6976656020776f726b73207468652073616d652077617920617320607472616e73666572602c206275742068617320616e206164646974696f6e616cf82020202020636865636b207468617420746865207472616e736665722077696c6c206e6f74206b696c6c20746865206f726967696e206163636f756e742e88202d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4501202d2042617365205765696768743a2037332e363420c2b5732c20776f7273742063617365207363656e6172696f20286163636f756e7420637265617465642c206163636f756e742072656d6f76656429dc202d204442205765696768743a2031205265616420616e64203120577269746520746f2064657374696e6174696f6e206163636f756e741501202d204f726967696e206163636f756e7420697320616c726561647920696e206d656d6f72792c20736f206e6f204442206f7065726174696f6e7320666f72207468656d2e302023203c2f7765696768743e2c7365745f62616c616e63650c0c77686f8c3c543a3a4c6f6f6b7570206173205374617469634c6f6f6b75703e3a3a536f75726365206e65775f667265654c436f6d706163743c543a3a42616c616e63653e306e65775f72657365727665644c436f6d706163743c543a3a42616c616e63653e489420536574207468652062616c616e636573206f66206120676976656e206163636f756e742e00210120546869732077696c6c20616c74657220604672656542616c616e63656020616e642060526573657276656442616c616e63656020696e2073746f726167652e2069742077696c6c090120616c736f2064656372656173652074686520746f74616c2069737375616e6365206f66207468652073797374656d202860546f74616c49737375616e636560292e190120496620746865206e65772066726565206f722072657365727665642062616c616e63652069732062656c6f7720746865206578697374656e7469616c206465706f7369742c01012069742077696c6c20726573657420746865206163636f756e74206e6f6e63652028606672616d655f73797374656d3a3a4163636f756e744e6f6e636560292e00b420546865206469737061746368206f726967696e20666f7220746869732063616c6c2069732060726f6f74602e002c2023203c7765696768743e80202d20496e646570656e64656e74206f662074686520617267756d656e74732ec4202d20436f6e7461696e732061206c696d69746564206e756d626572206f6620726561647320616e64207772697465732e58202d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d3c202d2042617365205765696768743a6820202020202d204372656174696e673a2032372e353620c2b5736420202020202d204b696c6c696e673a2033352e313120c2b57398202d204442205765696768743a203120526561642c203120577269746520746f206077686f60302023203c2f7765696768743e38666f7263655f7472616e736665720c18736f757263658c3c543a3a4c6f6f6b7570206173205374617469634c6f6f6b75703e3a3a536f7572636510646573748c3c543a3a4c6f6f6b7570206173205374617469634c6f6f6b75703e3a3a536f757263651476616c75654c436f6d706163743c543a3a42616c616e63653e1851012045786163746c7920617320607472616e73666572602c2065786365707420746865206f726967696e206d75737420626520726f6f7420616e642074686520736f75726365206163636f756e74206d61792062652c207370656369666965642e2c2023203c7765696768743e4101202d2053616d65206173207472616e736665722c20627574206164646974696f6e616c207265616420616e6420777269746520626563617573652074686520736f75726365206163636f756e74206973902020206e6f7420617373756d656420746f20626520696e20746865206f7665726c61792e302023203c2f7765696768743e4c7472616e736665725f6b6565705f616c6976650810646573748c3c543a3a4c6f6f6b7570206173205374617469634c6f6f6b75703e3a3a536f757263651476616c75654c436f6d706163743c543a3a42616c616e63653e2c51012053616d6520617320746865205b607472616e73666572605d2063616c6c2c206275742077697468206120636865636b207468617420746865207472616e736665722077696c6c206e6f74206b696c6c2074686540206f726967696e206163636f756e742e00bc20393925206f66207468652074696d6520796f752077616e74205b607472616e73666572605d20696e73746561642e00c4205b607472616e73666572605d3a207374727563742e4d6f64756c652e68746d6c236d6574686f642e7472616e736665722c2023203c7765696768743ee8202d2043686561706572207468616e207472616e736665722062656361757365206163636f756e742063616e6e6f74206265206b696c6c65642e60202d2042617365205765696768743a2035312e3420c2b5731d01202d204442205765696768743a2031205265616420616e64203120577269746520746f2064657374202873656e64657220697320696e206f7665726c617920616c7265616479292c20233c2f7765696768743e01201c456e646f77656408244163636f756e7449641c42616c616e636504250120416e206163636f756e74207761732063726561746564207769746820736f6d6520667265652062616c616e63652e205c5b6163636f756e742c20667265655f62616c616e63655c5d20447573744c6f737408244163636f756e7449641c42616c616e636508410120416e206163636f756e74207761732072656d6f7665642077686f73652062616c616e636520776173206e6f6e2d7a65726f206275742062656c6f77204578697374656e7469616c4465706f7369742cd020726573756c74696e6720696e20616e206f75747269676874206c6f73732e205c5b6163636f756e742c2062616c616e63655c5d205472616e736665720c244163636f756e744964244163636f756e7449641c42616c616e636504a0205472616e73666572207375636365656465642e205c5b66726f6d2c20746f2c2076616c75655c5d2842616c616e63655365740c244163636f756e7449641c42616c616e63651c42616c616e636504cc20412062616c616e6365207761732073657420627920726f6f742e205c5b77686f2c20667265652c2072657365727665645c5d1c4465706f73697408244163636f756e7449641c42616c616e636504210120536f6d6520616d6f756e7420776173206465706f73697465642028652e672e20666f72207472616e73616374696f6e2066656573292e205c5b77686f2c206465706f7369745c5d20526573657276656408244163636f756e7449641c42616c616e636504210120536f6d652062616c616e63652077617320726573657276656420286d6f7665642066726f6d206672656520746f207265736572766564292e205c5b77686f2c2076616c75655c5d28556e726573657276656408244163636f756e7449641c42616c616e636504290120536f6d652062616c616e63652077617320756e726573657276656420286d6f7665642066726f6d20726573657276656420746f2066726565292e205c5b77686f2c2076616c75655c5d4852657365727665526570617472696174656410244163636f756e744964244163636f756e7449641c42616c616e6365185374617475730c510120536f6d652062616c616e636520776173206d6f7665642066726f6d207468652072657365727665206f6620746865206669727374206163636f756e7420746f20746865207365636f6e64206163636f756e742edc2046696e616c20617267756d656e7420696e64696361746573207468652064657374696e6174696f6e2062616c616e636520747970652ea8205c5b66726f6d2c20746f2c2062616c616e63652c2064657374696e6174696f6e5f7374617475735c5d04484578697374656e7469616c4465706f73697428543a3a42616c616e636540f401000000000000000000000000000004d420546865206d696e696d756d20616d6f756e7420726571756972656420746f206b65657020616e206163636f756e74206f70656e2e203856657374696e6742616c616e6365049c2056657374696e672062616c616e636520746f6f206869676820746f2073656e642076616c7565544c69717569646974795265737472696374696f6e7304c8204163636f756e74206c6971756964697479207265737472696374696f6e732070726576656e74207769746864726177616c204f766572666c6f77047420476f7420616e206f766572666c6f7720616674657220616464696e674c496e73756666696369656e7442616c616e636504782042616c616e636520746f6f206c6f7720746f2073656e642076616c7565484578697374656e7469616c4465706f73697404ec2056616c756520746f6f206c6f7720746f20637265617465206163636f756e742064756520746f206578697374656e7469616c206465706f736974244b656570416c6976650490205472616e736665722f7061796d656e7420776f756c64206b696c6c206163636f756e745c4578697374696e6756657374696e675363686564756c6504cc20412076657374696e67207363686564756c6520616c72656164792065786973747320666f722074686973206163636f756e742c446561644163636f756e74048c2042656e6566696369617279206163636f756e74206d757374207072652d657869737405485472616e73616374696f6e5061796d656e7401485472616e73616374696f6e5061796d656e7408444e6578744665654d756c7469706c6965720100284d756c7469706c69657240000064a7b3b6e00d0000000000000000003853746f7261676556657273696f6e01002052656c6561736573040000000008485472616e73616374696f6e427974654665653042616c616e63654f663c543e4001000000000000000000000000000000040d01205468652066656520746f206265207061696420666f72206d616b696e672061207472616e73616374696f6e3b20746865207065722d6279746520706f7274696f6e2e2c576569676874546f466565a45665633c576569676874546f466565436f656666696369656e743c42616c616e63654f663c543e3e3e5c0401000000000000000000000000000000000000000001040d012054686520706f6c796e6f6d69616c2074686174206973206170706c69656420696e206f7264657220746f20646572697665206665652066726f6d207765696768742e0006105375646f01105375646f040c4b6579010030543a3a4163636f756e74496480000000000000000000000000000000000000000000000000000000000000000004842054686520604163636f756e74496460206f6620746865207375646f206b65792e0110107375646f041063616c6c5c426f783c3c542061732054726169743e3a3a43616c6c3e2839012041757468656e7469636174657320746865207375646f206b657920616e64206469737061746368657320612066756e6374696f6e2063616c6c20776974682060526f6f7460206f726967696e2e00d020546865206469737061746368206f726967696e20666f7220746869732063616c6c206d757374206265205f5369676e65645f2e002c2023203c7765696768743e20202d204f2831292e64202d204c696d697465642073746f726167652072656164732e60202d204f6e6520444220777269746520286576656e74292ec8202d20576569676874206f662064657269766174697665206063616c6c6020657865637574696f6e202b2031302c3030302e302023203c2f7765696768743e547375646f5f756e636865636b65645f776569676874081063616c6c5c426f783c3c542061732054726169743e3a3a43616c6c3e1c5f776569676874185765696768742839012041757468656e7469636174657320746865207375646f206b657920616e64206469737061746368657320612066756e6374696f6e2063616c6c20776974682060526f6f7460206f726967696e2e310120546869732066756e6374696f6e20646f6573206e6f7420636865636b2074686520776569676874206f66207468652063616c6c2c20616e6420696e737465616420616c6c6f777320746865b4205375646f207573657220746f20737065636966792074686520776569676874206f66207468652063616c6c2e00d020546865206469737061746368206f726967696e20666f7220746869732063616c6c206d757374206265205f5369676e65645f2e002c2023203c7765696768743e20202d204f2831292ed0202d2054686520776569676874206f6620746869732063616c6c20697320646566696e6564206279207468652063616c6c65722e302023203c2f7765696768743e1c7365745f6b6579040c6e65778c3c543a3a4c6f6f6b7570206173205374617469634c6f6f6b75703e3a3a536f757263652475012041757468656e74696361746573207468652063757272656e74207375646f206b657920616e6420736574732074686520676976656e204163636f756e7449642028606e6577602920617320746865206e6577207375646f206b65792e00d020546865206469737061746368206f726967696e20666f7220746869732063616c6c206d757374206265205f5369676e65645f2e002c2023203c7765696768743e20202d204f2831292e64202d204c696d697465642073746f726167652072656164732e44202d204f6e65204442206368616e67652e302023203c2f7765696768743e1c7375646f5f6173080c77686f8c3c543a3a4c6f6f6b7570206173205374617469634c6f6f6b75703e3a3a536f757263651063616c6c5c426f783c3c542061732054726169743e3a3a43616c6c3e2c51012041757468656e7469636174657320746865207375646f206b657920616e64206469737061746368657320612066756e6374696f6e2063616c6c207769746820605369676e656460206f726967696e2066726f6d44206120676976656e206163636f756e742e00d020546865206469737061746368206f726967696e20666f7220746869732063616c6c206d757374206265205f5369676e65645f2e002c2023203c7765696768743e20202d204f2831292e64202d204c696d697465642073746f726167652072656164732e60202d204f6e6520444220777269746520286576656e74292ec8202d20576569676874206f662064657269766174697665206063616c6c6020657865637574696f6e202b2031302c3030302e302023203c2f7765696768743e010c14537564696404384469737061746368526573756c74048c2041207375646f206a75737420746f6f6b20706c6163652e205c5b726573756c745c5d284b65794368616e67656404244163636f756e74496404010120546865205c5b7375646f65725c5d206a757374207377697463686564206964656e746974793b20746865206f6c64206b657920697320737570706c6965642e285375646f4173446f6e650410626f6f6c048c2041207375646f206a75737420746f6f6b20706c6163652e205c5b726573756c745c5d00042c526571756972655375646f04802053656e646572206d75737420626520746865205375646f206163636f756e74073854656d706c6174654d6f64756c65013854656d706c6174654d6f64756c650424536f6d657468696e6700000c753332040000010830646f5f736f6d657468696e670424736f6d657468696e670c753332085d0120416e206578616d706c6520646973706174636861626c6520746861742074616b657320612073696e676c65732076616c7565206173206120706172616d657465722c20777269746573207468652076616c756520746f51012073746f7261676520616e6420656d69747320616e206576656e742e20546869732066756e6374696f6e206d75737420626520646973706174636865642062792061207369676e65642065787472696e7369632e2c63617573655f6572726f720004dc20416e206578616d706c6520646973706174636861626c652074686174206d6179207468726f77206120637573746f6d206572726f722e01043c536f6d657468696e6753746f726564080c753332244163636f756e744964085d01204576656e7420646f63756d656e746174696f6e2073686f756c6420656e64207769746820616e20617272617920746861742070726f7669646573206465736372697074697665206e616d657320666f72206576656e747420706172616d65746572732e205b736f6d657468696e672c2077686f5d0008244e6f6e6556616c7565048c204572726f72206e616d65732073686f756c642062652064657363726970746976652e3c53746f726167654f766572666c6f7704fc204572726f72732073686f756c6420686176652068656c7066756c20646f63756d656e746174696f6e206173736f6369617465642077697468207468656d2e081c4b697474696573011c4b697474696573081c4b69747469657300020230543a3a4163636f756e74496434543a3a4b69747479496e646578144b6974747902040004b02053746f72657320616c6c20746865206b6974746965732c206b657920697320746865206b697474792069642c4e6578744b697474794964010034543a3a4b69747479496e646578100000000004642053746f72657320746865206e657874206b69747479204944010c1863726561746500044c204372656174652061206e6577206b6974747914627265656408286b697474795f69645f3134543a3a4b69747479496e646578286b697474795f69645f3234543a3a4b69747479496e6465780438204272656564206b697474696573207472616e736665720808746f30543a3a4163636f756e744964206b697474795f696434543a3a4b69747479496e6465780478205472616e736665722061206b6974747920746f206e6577206f776e6572010c304b69747479437265617465640c244163636f756e744964284b69747479496e646578144b6974747904bc2041206b6974747920697320637265617465642e205c5b6f776e65722c206b697474795f69642c206b697474795c5d244b69747479427265640c244163636f756e744964284b69747479496e646578144b6974747904c42041206e6577206b697474656e20697320627265642e205c5b6f776e65722c206b697474795f69642c206b697474795c5d404b697474795472616e736665727265640c244163636f756e744964244163636f756e744964284b69747479496e64657804bc2041206b69747479206973207472616e736665727265642e205c5b66726f6d2c20746f2c206b697474795f69645c5d000c444b69747469657349644f766572666c6f770038496e76616c69644b697474794964002853616d6547656e6465720009041c40436865636b5370656356657273696f6e38436865636b547856657273696f6e30436865636b47656e6573697338436865636b4d6f7274616c69747928436865636b4e6f6e63652c436865636b576569676874604368617267655472616e73616374696f6e5061796d656e74","id":"1"} 2 | -------------------------------------------------------------------------------- /kitty/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './mappings/Kitty'; 2 | -------------------------------------------------------------------------------- /kitty/src/mappings/Kitty.ts: -------------------------------------------------------------------------------- 1 | import {KittyBirthInfo} from '../types/models/KittyBirthInfo'; 2 | import {SubstrateEvent, SubstrateExtrinsic} from "@subql/types"; 3 | 4 | export async function handleKittyCreated(event: SubstrateEvent): Promise { 5 | const {event: {data: [owner, kittyId, kitty]}} = event; 6 | const record = new KittyBirthInfo(kittyId.toString()); 7 | record.birthBlockHeight = event.block.block.header.number.toBigInt(); 8 | record.firstOwner = owner.toString(); 9 | record.owner = owner.toString(); 10 | await record.save(); 11 | } 12 | 13 | export async function handleKittyBred(extrinsic: SubstrateExtrinsic): Promise { 14 | const bredEvent = extrinsic.events.find(e => e.event.section === 'kitties' && e.event.method === 'KittyBred'); 15 | const {event: {data: [owner, kittyId, kitty]}} = bredEvent; 16 | const record = new KittyBirthInfo(kittyId.toString()); 17 | record.birthBlockHeight = extrinsic.block.block.header.number.toBigInt(); 18 | record.firstOwner = owner.toString(); 19 | record.owner = owner.toString(); 20 | const {extrinsic: {method: {args: [kittyId1, kittyId2]}}} = extrinsic; 21 | record.parent1 = kittyId1.toString(); 22 | record.parent2 = kittyId2.toString(); 23 | await record.save(); 24 | } 25 | 26 | export async function handleKittyTransferred(event: SubstrateEvent): Promise { 27 | const {event: {data: [from, to, kittyId]}} = event; 28 | const record = await KittyBirthInfo.get(kittyId.toString()); 29 | record.owner = to.toString(); 30 | await record.save(); 31 | } 32 | 33 | //For demonstration only 34 | export async function kittyApiHandler(): Promise { 35 | //return the KittyIndex type 36 | const nextKittyId = await api.query.kitties.nextKittyId(); 37 | // return the Kitty type, input parameters types are AccountId and KittyIndex 38 | const allKitties = await api.query.kitties.kitties('xxxxxxxxx',123) 39 | // return kitty price as Balance type, take BlockHash and KittyIndex for inputs. 40 | // This feature is not support yet, wait to complete https://github.com/subquery/subql/issues/302 41 | const kittyPrice = await api.rpc.kitties.getKittyPrice(undefined,nextKittyId); 42 | logger.info(`Next kitty id ${nextKittyId}`) 43 | } 44 | -------------------------------------------------------------------------------- /kitty/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 | "baseUrl": ".", 11 | "outDir": "dist", 12 | "rootDir": "src", 13 | "target": "es2017", 14 | "paths": { 15 | "kitty-birthinfo/*": ["src/*"], 16 | "@polkadot/api/augment": ["src/api-interfaces/augment-api.ts"], 17 | "@polkadot/types/augment": ["src/api-interfaces/augment-types.ts"] 18 | } 19 | }, 20 | "include": [ 21 | "src/**/*", 22 | "node_modules/@subql/types/dist/global.d.ts" 23 | ] 24 | } 25 | -------------------------------------------------------------------------------- /sum-reward/README.md: -------------------------------------------------------------------------------- 1 | # What is SubQuery? 2 | 3 | SubQuery powers the next generation of Polkadot dApps by allowing developers to extract, transform and query blockchain data in real time using GraphQL. In addition to this, SubQuery provides production quality hosting infrastructure to run these projects in. 4 | 5 | # SubQuery Example - Summary of Rewards and Slashes 6 | 7 | This subquery indexes staking bonds, rewards and slashes of each finalized blocks. 8 | 9 | This is an example of EventHandler that provides records of all the accounts that have participated in staking and summarizes their total rewards and slashes. It also provides the total earnings. 10 | 11 | # Getting Started 12 | 13 | ### 1. Clone the entire subql-example repository 14 | 15 | ```shell 16 | git clone https://github.com/subquery/subql-examples.git 17 | 18 | ``` 19 | ### 2. Install dependencies 20 | 21 | ```shell 22 | cd sum-reward 23 | # Yarn 24 | yarn 25 | 26 | #NPM 27 | npm install 28 | ``` 29 | 30 | ### 3. Generate types 31 | 32 | ```shell 33 | #Yarn 34 | yarn codegen 35 | 36 | #NPM 37 | npm run-script codegen 38 | ``` 39 | 40 | ### 4. Build the project 41 | 42 | ```shell 43 | #Yarn 44 | yarn build 45 | 46 | #NPM 47 | npm run-script build 48 | ``` 49 | 50 | ### 5. Run locally 51 | 52 | ```shell 53 | #Yarn 54 | yarn start:docker 55 | 56 | #NPM 57 | npm run start:docker 58 | ``` 59 | 60 | ### 5. Example queries to run 61 | 62 | ```shell 63 | { 64 | query{ 65 | stakingRewards{ 66 | nodes{ 67 | balance 68 | } 69 | } 70 | } 71 | } 72 | ``` 73 | 74 | ```shell 75 | { 76 | query{ 77 | stakingSlashes{ 78 | nodes{ 79 | balance 80 | } 81 | } 82 | } 83 | } 84 | ``` 85 | 86 | ```shell 87 | { 88 | query{ 89 | sumRewards{ 90 | nodes{ 91 | rewards{ 92 | nodes{ 93 | balance 94 | } 95 | } 96 | } 97 | } 98 | } 99 | } 100 | ``` -------------------------------------------------------------------------------- /sum-reward/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_POST: 5432 24 | volumes: 25 | - ./:/app 26 | command: 27 | - -f=/app 28 | 29 | graphql-engine: 30 | image: onfinality/subql-query:latest 31 | ports: 32 | - 3000:3000 33 | depends_on: 34 | - "postgres" 35 | restart: always 36 | environment: 37 | DB_USER: postgres 38 | DB_PASS: postgres 39 | DB_DATABASE: postgres 40 | DB_HOST: postgres 41 | DB_PORT: 5432 42 | command: 43 | - --name=app 44 | - --playground 45 | 46 | -------------------------------------------------------------------------------- /sum-reward/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sum-reward", 3 | "version": "0.1.0", 4 | "description": "This subquery indexes bond, reward and slash from events of finalized block", 5 | "main": "dist/index.js", 6 | "homepage": "https://github.com/subquery/subql-examples", 7 | "repository": "github:subquery/subql-examples", 8 | "scripts": { 9 | "build": "tsc -b", 10 | "prepack": "rm -rf dist && npm build", 11 | "start:docker": "docker-compose pull && docker-compose up --remove-orphans", 12 | "codegen": "./node_modules/.bin/subql codegen" 13 | }, 14 | "files": [ 15 | "dist", 16 | "schema.graphql", 17 | "project.yaml" 18 | ], 19 | "author": "Jay Ji", 20 | "license": "Apache-2.0", 21 | "devDependencies": { 22 | "@polkadot/api": "^4", 23 | "@subql/types": "^0.7.1", 24 | "typescript": "^4.1.3", 25 | "@subql/cli": "^0.9.3" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /sum-reward/project.yaml: -------------------------------------------------------------------------------- 1 | specVersion: "0.0.1" 2 | description: "This subquery indexes bond,reward and slash of each block, summarize total earning of an account" 3 | repository: "https://github.com/subquery/subql-examples" 4 | 5 | schema: "./schema.graphql" 6 | 7 | network: 8 | endpoint: "wss://polkadot.api.onfinality.io/public-ws" 9 | 10 | dataSources: 11 | - name: runtime 12 | kind: substrate/Runtime 13 | startBlock: 1 14 | mapping: 15 | handlers: 16 | - handler: handleBond 17 | kind: substrate/EventHandler 18 | filter: 19 | module: staking 20 | method: Bonded 21 | - handler: handleReward 22 | kind: substrate/EventHandler 23 | filter: 24 | module: staking 25 | method: Reward 26 | - handler: handleRewarded 27 | kind: substrate/EventHandler 28 | filter: 29 | module: staking 30 | method: Rewarded 31 | - handler: handleSlash 32 | kind: substrate/EventHandler 33 | filter: 34 | module: staking 35 | method: Slash 36 | - handler: handleSlashed 37 | kind: substrate/EventHandler 38 | filter: 39 | module: staking 40 | method: Slashed 41 | - handler: handleStakingReward 42 | kind: substrate/EventHandler 43 | filter: 44 | module: staking 45 | method: Reward 46 | - handler: handleStakingRewarded 47 | kind: substrate/EventHandler 48 | filter: 49 | module: staking 50 | method: Rewarded 51 | - handler: handleStakingSlash 52 | kind: substrate/EventHandler 53 | filter: 54 | module: staking 55 | method: Slash 56 | - handler: handleStakingSlashed 57 | kind: substrate/EventHandler 58 | filter: 59 | module: staking 60 | method: Slashed -------------------------------------------------------------------------------- /sum-reward/schema.graphql: -------------------------------------------------------------------------------- 1 | type SumReward @entity{ 2 | 3 | id: ID! # AccountId 4 | 5 | accountReward: BigInt! 6 | 7 | accountSlash: BigInt! 8 | 9 | accountTotal: BigInt! 10 | 11 | rewards: [StakingReward] @derivedFrom(field: "account") 12 | 13 | slashs: [StakingSlash] @derivedFrom(field: "account") 14 | } 15 | 16 | # in early stage of kusama, some validators didn't need to bond to start staking 17 | type NoBondRecordAccount @entity { 18 | id: ID! # AccountId 19 | 20 | firstRewardAt: Int! 21 | } 22 | 23 | 24 | type StakingReward @entity{ 25 | id: ID! #blockHeight-eventIdx 26 | account: SumReward! 27 | balance: BigInt! 28 | date: Date! 29 | } 30 | 31 | 32 | type StakingSlash @entity{ 33 | id: ID! #blockHeight-eventIdx 34 | account: SumReward! 35 | balance: BigInt! 36 | date: Date! 37 | } 38 | -------------------------------------------------------------------------------- /sum-reward/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './mappings/Reward'; 2 | export * from './mappings/Staking'; 3 | -------------------------------------------------------------------------------- /sum-reward/src/mappings/Reward.ts: -------------------------------------------------------------------------------- 1 | import {SumReward} from '../types/models/SumReward'; 2 | import {SubstrateEvent} from "@subql/types"; 3 | import {Balance} from '@polkadot/types/interfaces'; 4 | import {NoBondRecordAccount} from "../types/models/NoBondRecordAccount"; 5 | 6 | function createSumReward(accountId: string): SumReward { 7 | const entity = new SumReward(accountId); 8 | entity.accountReward = BigInt(0); 9 | entity.accountSlash = BigInt(0); 10 | entity.accountTotal = BigInt(0); 11 | return entity; 12 | } 13 | 14 | export async function handleBond(event: SubstrateEvent): Promise { 15 | const {event: {data: [account, balance]}} = event; 16 | const entity = await SumReward.get(account.toString()); 17 | if (entity === undefined){ 18 | await createSumReward(account.toString()).save(); 19 | } 20 | } 21 | 22 | export async function handleRewarded(event: SubstrateEvent): Promise { 23 | await handleReward(event) 24 | } 25 | 26 | export async function handleReward(event: SubstrateEvent): Promise { 27 | const {event: {data: [account, newReward]}} = event; 28 | let entity = await SumReward.get(account.toString()); 29 | if (entity === undefined){ 30 | // in early stage of kusama, some validators didn't need to bond to start staking 31 | // to not break our code, we will create a SumReward record for them and log them in NoBondRecordAccount 32 | entity = createSumReward(account.toString()); 33 | const errorRecord = new NoBondRecordAccount(account.toString()); 34 | errorRecord.firstRewardAt = event.block.block.header.number.toNumber(); 35 | await errorRecord.save(); 36 | } 37 | 38 | entity.accountReward = entity.accountReward + (newReward as Balance).toBigInt(); 39 | entity.accountTotal = entity.accountReward - entity.accountSlash; 40 | await entity.save(); 41 | } 42 | 43 | export async function handleSlashed(event: SubstrateEvent): Promise { 44 | await handleSlash(event) 45 | } 46 | 47 | export async function handleSlash(event: SubstrateEvent): Promise { 48 | const {event: {data: [account, newSlash]}} = event; 49 | let entity = await SumReward.get(account.toString()); 50 | if (entity === undefined){ 51 | // in early stage of kusama, some validators didn't need to bond to start staking 52 | // to not break our code, we will create a SumReward record for them and log them in NoBondRecordAccount 53 | entity = createSumReward(account.toString()); 54 | const errorRecord = new NoBondRecordAccount(account.toString()); 55 | errorRecord.firstRewardAt = event.block.block.header.number.toNumber(); 56 | await errorRecord.save(); 57 | } 58 | 59 | entity.accountSlash = entity.accountSlash + (newSlash as Balance).toBigInt(); 60 | entity.accountTotal = entity.accountReward - entity.accountSlash; 61 | await entity.save(); 62 | } 63 | -------------------------------------------------------------------------------- /sum-reward/src/mappings/Staking.ts: -------------------------------------------------------------------------------- 1 | import {StakingReward, StakingSlash} from '../types/models'; 2 | import {SubstrateEvent} from "@subql/types"; 3 | import {Balance} from '@polkadot/types/interfaces'; 4 | 5 | export async function handleStakingRewarded(event: SubstrateEvent): Promise { 6 | await handleStakingReward(event) 7 | } 8 | 9 | export async function handleStakingReward(event: SubstrateEvent): Promise { 10 | const {event: {data: [account, newReward]}} = event; 11 | const entity = new StakingReward(`${event.block.block.header.number}-${event.idx.toString()}`); 12 | entity.accountId = account.toString(); 13 | entity.balance = (newReward as Balance).toBigInt(); 14 | entity.date = event.block.timestamp; 15 | await entity.save(); 16 | } 17 | 18 | export async function handleStakingSlashed(event: SubstrateEvent): Promise { 19 | await handleStakingSlash(event) 20 | } 21 | 22 | export async function handleStakingSlash(event: SubstrateEvent): Promise { 23 | const {event: {data: [account, newSlash]}} = event; 24 | const entity = new StakingSlash(`${event.block.block.header.number}-${event.idx.toString()}`); 25 | entity.accountId = account.toString(); 26 | entity.balance = (newSlash as Balance).toBigInt(); 27 | entity.date = event.block.timestamp; 28 | await entity.save(); 29 | } 30 | -------------------------------------------------------------------------------- /sum-reward/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 | -------------------------------------------------------------------------------- /validator-threshold/README.md: -------------------------------------------------------------------------------- 1 | # What is SubQuery? 2 | 3 | SubQuery powers the next generation of Polkadot dApps by allowing developers to extract, transform and query blockchain data in real time using GraphQL. In addition to this, SubQuery provides production quality hosting infrastructure to run these projects in. 4 | 5 | # SubQuery Example - Validator Threshold 6 | 7 | This subquery indexes the least staking amount required for a validator to be elected. 8 | 9 | This is an example of using `@polkadot/api` in the mapping function. Proofs: 10 | 1. historical metadatas are correctly handled, registry, consts are swapped. (see maxNominatorRewardedPerValidator) 11 | 2. Queries are restricted to the current processed block. (see validators and exposures) 12 | 3. api.queryMulti is supported. (see activeEra, currentEra) 13 | 14 | # Getting Started 15 | 16 | ### 1. Clone the entire subql-example repository 17 | 18 | ```shell 19 | git clone https://github.com/subquery/subql-examples.git 20 | 21 | ``` 22 | ### 2. Install dependencies 23 | 24 | ```shell 25 | cd validator-threshold 26 | # Yarn 27 | yarn 28 | 29 | #NPM 30 | npm install 31 | ``` 32 | 33 | ### 3. Generate types 34 | 35 | ```shell 36 | #Yarn 37 | yarn codegen 38 | 39 | #NPM 40 | npm run-script codegen 41 | ``` 42 | 43 | ### 4. Build the project 44 | 45 | ```shell 46 | #Yarn 47 | yarn build 48 | 49 | #NPM 50 | npm run-script build 51 | ``` 52 | 53 | ### 5. Run locally 54 | 55 | ```shell 56 | #Yarn 57 | yarn start:docker 58 | 59 | #NPM 60 | npm run start:docker 61 | ``` 62 | 63 | ### 5. Example query to run 64 | 65 | ```shell 66 | { 67 | query{ 68 | validatorThresholds{ 69 | nodes{ 70 | leastStaked, 71 | totalStaked 72 | } 73 | } 74 | } 75 | } 76 | ``` -------------------------------------------------------------------------------- /validator-threshold/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_POST: 5432 24 | volumes: 25 | - ./:/app 26 | command: 27 | - -f=/app 28 | 29 | graphql-engine: 30 | image: onfinality/subql-query:latest 31 | ports: 32 | - 3000:3000 33 | depends_on: 34 | - "postgres" 35 | restart: always 36 | environment: 37 | DB_USER: postgres 38 | DB_PASS: postgres 39 | DB_DATABASE: postgres 40 | DB_HOST: postgres 41 | DB_PORT: 5432 42 | command: 43 | - --name=app 44 | - --playground 45 | 46 | -------------------------------------------------------------------------------- /validator-threshold/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "validator-threshold", 3 | "version": "0.2.0", 4 | "description": "This subquery indexes the least staking amount required for a validator to be elected.", 5 | "main": "dist/index.js", 6 | "homepage": "https://github.com/subquery/subql-examples", 7 | "repository": "github:subquery/subql-examples", 8 | "scripts": { 9 | "build": "tsc -b", 10 | "prepack": "rm -rf dist && npm build", 11 | "start:docker": "docker-compose pull && docker-compose up --remove-orphans", 12 | "codegen": "subql codegen" 13 | }, 14 | "files": [ 15 | "dist", 16 | "schema.graphql", 17 | "project.yaml" 18 | ], 19 | "author": "Ian He", 20 | "license": "Apache-2.0", 21 | "devDependencies": { 22 | "@polkadot/api": "^6", 23 | "@subql/types": "^0.11.0", 24 | "typescript": "^4.4.4", 25 | "@subql/cli": "^0.16.1" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /validator-threshold/project.yaml: -------------------------------------------------------------------------------- 1 | specVersion: "0.0.1" 2 | description: "This subquery indexes bond,reward and slash of each block, summarize total earning of an account" 3 | repository: "https://github.com/subquery/subql-examples" 4 | 5 | schema: "./schema.graphql" 6 | 7 | network: 8 | endpoint: "wss://polkadot.api.onfinality.io/public-ws" 9 | dictionary: https://api.subquery.network/sq/subquery/dictionary-polkadot 10 | 11 | dataSources: 12 | - name: runtime 13 | kind: substrate/Runtime 14 | # for kusama startBlock: 1445458 15 | startBlock: 1 16 | mapping: 17 | handlers: 18 | - handler: handleBlock 19 | kind: substrate/EventHandler 20 | filter: 21 | module: session 22 | method: NewSession 23 | -------------------------------------------------------------------------------- /validator-threshold/schema.graphql: -------------------------------------------------------------------------------- 1 | 2 | type ValidatorThreshold @entity { 3 | # era index 4 | id: ID! 5 | 6 | startBlock: Int! 7 | 8 | timestamp: Date! 9 | 10 | validatorWithLeastBond: String 11 | 12 | leastStaked: BigInt 13 | 14 | totalStaked: BigInt 15 | 16 | maxNominatorRewardedPerValidator: Int 17 | 18 | totalValidators: Int 19 | } 20 | -------------------------------------------------------------------------------- /validator-threshold/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './mappings/ValidatorThreshold'; 2 | -------------------------------------------------------------------------------- /validator-threshold/src/mappings/ValidatorThreshold.ts: -------------------------------------------------------------------------------- 1 | import {ActiveEraInfo, Balance, EraIndex, Exposure } from "@polkadot/types/interfaces"; 2 | import {PalletStakingExposure} from "@polkadot/types/lookup"; 3 | import {Option} from "@polkadot/types" 4 | import {SubstrateEvent} from "@subql/types"; 5 | import {ValidatorThreshold} from "../types/models/ValidatorThreshold"; 6 | 7 | export async function handleBlock({ block }: SubstrateEvent): Promise { 8 | // in the early stage of kusama, staking.activeEra didn't exist 9 | if (!api.query.staking.activeEra) return; 10 | const [activeEra] = await api.queryMulti<[Option, Option]>([ 11 | api.query.staking.activeEra, 12 | // api.query.staking.currentEra 13 | ]); 14 | if (activeEra.isEmpty) return; 15 | const entity = new ValidatorThreshold(activeEra.unwrap().index.toString()); 16 | const validators = await api.query.session.validators(); 17 | const exposureInfos = await api.queryMulti( 18 | validators.map(validator=>[api.query.staking.erasStakersClipped, [activeEra.unwrap().index, validator]]) 19 | ); 20 | const thresholdValidator = exposureInfos.reduce<{accountId: string, total: Balance}>((acc, exposure, idx)=>{ 21 | if (!acc || exposure.total.unwrap().lt(acc.total)) { 22 | return {accountId: validators[idx].toString(), total: exposure.total.unwrap()}; 23 | } 24 | return acc; 25 | }, undefined ); 26 | entity.startBlock = block.block.header.number.toNumber(); 27 | entity.timestamp = block.timestamp; 28 | entity.totalValidators = validators.length; 29 | entity.validatorWithLeastBond = thresholdValidator.accountId; 30 | entity.leastStaked = thresholdValidator.total.toBigInt(); 31 | entity.totalStaked = (await api.query.staking.erasTotalStake(activeEra.unwrap().index)).toBigInt(); 32 | entity.maxNominatorRewardedPerValidator = api.consts.staking.maxNominatorRewardedPerValidator?.toNumber(); 33 | await entity.save(); 34 | } 35 | -------------------------------------------------------------------------------- /validator-threshold/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 | --------------------------------------------------------------------------------