Skip to content

Commit

Permalink
add crab <> crab parachain (#51)
Browse files Browse the repository at this point in the history
* add crab parachain

* crab->crabparachain

* update genesis hash

* repair token

* update subql url

* some config

* enable crab parachain

* filter event

* repair target hash

Co-authored-by: sxlwar <[email protected]>
  • Loading branch information
xiaoch05 and sxlwar authored Jul 14, 2022
1 parent 7595fe5 commit d2c527a
Show file tree
Hide file tree
Showing 23 changed files with 6,880 additions and 28 deletions.
5 changes: 4 additions & 1 deletion apollo/.env.prod
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
SUBSTRATE_SUBSTRATE_BACKING=https://api.subquery.network/sq/helix-bridge/darwinia
SUBSTRATE_SUBSTRATE_ISSUING=https://crab-thegraph.darwinia.network/subgraphs/name/wormhole/Sub2SubMappingTokenFactory
SUBSTRATE_DVM_ENDPOINT=https://api.subquery.network/sq/helix-bridge/crab
CHAIN_TYPE=formal
SUBSTRATE_TO_PARACHAIN_BACKING=https://api.subquery.network/sq/helix-bridge/crab
SUBSTRATE_TO_PARACHAIN_ISSUING=https://api.subquery.network/sq/helix-bridge/cpchain
CHAIN_TYPE=formal
PARACHAIN_LOCK_FEE_TOKEN=Crab
31 changes: 8 additions & 23 deletions apollo/src/substrate2parachain/substrate2parachain.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import axios from 'axios';
import { last } from 'lodash';
import { AggregationService } from '../aggregation/aggregation.service';
import { RecordsService } from '../base/RecordsService';
import { Transfer, TransferAction } from '../base/TransferService';
Expand Down Expand Up @@ -44,10 +43,6 @@ export class Substrate2parachainService extends RecordsService implements OnModu
}

async onModuleInit() {
if (this.configService.get('CHAIN_TYPE') === 'formal') {
return;
}

this.transferService.transfers.forEach((item, index) => {
this.taskService.addInterval(
`${item.backing.chain}-parachain-fetch_history_data`,
Expand Down Expand Up @@ -118,13 +113,17 @@ export class Substrate2parachainService extends RecordsService implements OnModu
bridgeDispatchError: '',
});

if (!this.needSyncLock[index] && isLock) {
this.needSyncLock[index] = true;
} else if (!this.needSyncBurn[index] && !isLock) {
this.needSyncBurn[index] = true;
}

if (node.result === 0) {
if (!this.needSyncLockConfirmed[index] && isLock) {
this.needSyncLockConfirmed[index] = true;
this.needSyncLock[index] = true;
} else if (!this.needSyncBurnConfirmed && !isLock) {
this.needSyncBurnConfirmed[index] = true;
this.needSyncBurn[index] = true;
}
}
}
Expand Down Expand Up @@ -173,28 +172,14 @@ export class Substrate2parachainService extends RecordsService implements OnModu
return;
}

const dispatchLaneId = '726f6c69';
const recordLaneId = '70616c69';

const pickId = (id: string) => {
const target = last(id.split('-'));

return action === 'lock' ? target : target.replace(recordLaneId, dispatchLaneId);
};

const ids = uncheckedRecords.map((item) => `"${pickId(item.id)}"`).join(',');
const ids = uncheckedRecords.map((item) => `"${item.id.split('-')[3]}"`).join(',');

const nodes = await axios
.post(to.url, {
query: `query { bridgeDispatchEvents (filter: {id: {in: [${ids}]}}) { nodes {id, method, block }}}`,
variables: null,
})
.then((res) =>
res.data?.data?.bridgeDispatchEvents?.nodes.map(({ id, ...rest }) => ({
...rest,
id: id.replace(dispatchLaneId, recordLaneId),
}))
);
.then((res) => res.data?.data?.bridgeDispatchEvents?.nodes);

if (nodes && nodes.length > 0) {
for (const node of nodes) {
Expand Down
21 changes: 21 additions & 0 deletions subql/crab-parachain/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT LICENSE

Copyright 2020-2021 OnFinality Limited authors & contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
102 changes: 102 additions & 0 deletions subql/crab-parachain/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# SubQuery - Starter Package


The Starter Package is an example that you can use as a starting point for developing your SubQuery project.
A SubQuery package defines which data The SubQuery will index from the Substrate blockchain, and how it will store it.

## Preparation

#### Environment

- [Typescript](https://www.typescriptlang.org/) are required to compile project and define types.

- Both SubQuery CLI and generated Project have dependencies and require [Node](https://nodejs.org/en/).


#### Install the SubQuery CLI

Install SubQuery CLI globally on your terminal by using NPM:

```
npm install -g @subql/cli
```

Run help to see available commands and usage provide by CLI
```
subql help
```

## Initialize the starter package

Inside the directory in which you want to create the SubQuery project, simply replace `project-name` with your project name and run the command:
```
subql init --starter project-name
```
Then you should see a folder with your project name has been created inside the directory, you can use this as the start point of your project. And the files should be identical as in the [Directory Structure](https://doc.subquery.network/directory_structure.html).

Last, under the project directory, run following command to install all the dependency.
```
yarn install
```


## Configure your project

In the starter package, we have provided a simple example of project configuration. You will be mainly working on the following files:

- The Manifest in `project.yaml`
- The GraphQL Schema in `schema.graphql`
- The Mapping functions in `src/mappings/` directory

For more information on how to write the SubQuery,
check out our doc section on [Define the SubQuery](https://doc.subquery.network/define_a_subquery.html)

#### Code generation

In order to index your SubQuery project, it is mandatory to build your project first.
Run this command under the project directory.

````
yarn codegen
````

## Build the project

In order to deploy your SubQuery project to our hosted service, it is mandatory to pack your configuration before upload.
Run pack command from root directory of your project will automatically generate a `your-project-name.tgz` file.

```
yarn build
```

## Indexing and Query

#### Run required systems in docker


Under the project directory run following command:

```
docker-compose pull && docker-compose up
```
#### Query the project

Open your browser and head to `http://localhost:3000`.

Finally, you should see a GraphQL playground is showing in the explorer and the schemas that ready to query.

For the `subql-starter` project, you can try to query with the following code to get a taste of how it works.

````graphql
{
query{
starterEntities(first:10){
nodes{
field1,
field2,
field3
}
}
}
}
````
60 changes: 60 additions & 0 deletions subql/crab-parachain/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
version: '3'

services:
postgres:
image: postgres:12-alpine
ports:
- 5432:5432
volumes:
- .data/postgres:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: postgres
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U postgres']
interval: 5s
timeout: 5s
retries: 10

subquery-node:
image: onfinality/subql-node:v0.27.2-0
depends_on:
'postgres':
condition: service_healthy
restart: always
environment:
DB_USER: postgres
DB_PASS: postgres
DB_DATABASE: postgres
DB_HOST: postgres
DB_PORT: 5432
volumes:
- ./:/app
command:
- -f=/app
- --db-schema=app
healthcheck:
test: ['CMD', 'curl', '-f', 'http://subquery-node:3000/ready']
interval: 3s
timeout: 5s
retries: 10

graphql-engine:
image: onfinality/subql-query:v0.10.1-0
ports:
- 3000:3000
depends_on:
'postgres':
condition: service_healthy
'subquery-node':
condition: service_healthy
restart: always
environment:
DB_USER: postgres
DB_PASS: postgres
DB_DATABASE: postgres
DB_HOST: postgres
DB_PORT: 5432
command:
- --name=app
- --playground
- --indexer=http://subquery-node:3000
31 changes: 31 additions & 0 deletions subql/crab-parachain/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "crab-parachain",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"build": "tsc -b",
"prepack": "rm -rf dist && npm build",
"test": "jest",
"start": "docker-compose pull && docker-compose up --remove-orphans",
"codegen": "./node_modules/.bin/subql codegen"
},
"homepage": "https://github.com/subquery/subql-starter",
"repository": "github:subquery/subql-starter",
"resolutions": {
"ipfs-unixfs": "6.0.6"
},
"files": [
"dist",
"schema.graphql",
"project.yaml"
],
"author": "xiaoch05",
"license": "MIT",
"devDependencies": {
"@polkadot/api": "^8",
"@subql/types": "latest",
"typescript": "^4.2.4",
"@subql/cli": "latest"
}
}
57 changes: 57 additions & 0 deletions subql/crab-parachain/project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
specVersion: 0.2.0
name: helix-crab-parachain
version: 1.0.0
description: ''
repository: [email protected]:helix-bridge/indexer.git

schema:
file: ./schema.graphql

network:
genesisHash: '0xeac895d7768b17837a9c3a9f0280c01502c3ef40193df923490a0fa9c60ea076'
endpoint: wss://crab-parachain-rpc.darwinia.network
chaintypes:
file: ./types.yaml

dataSources:
- kind: substrate/Runtime
startBlock: 535900
mapping:
file: ./dist/index.js
handlers:
- handler: handleEvent
kind: substrate/EventHandler
filter:
module: bridgeCrabDispatch
method: MessageVersionSpecMismatch

- handler: handleEvent
kind: substrate/EventHandler
filter:
module: bridgeCrabDispatch
method: MessageWeightMismatch

- handler: handleEvent
kind: substrate/EventHandler
filter:
module: bridgeCrabDispatch
method: MessageDispatched

- handler: handleEvent
kind: substrate/EventHandler
filter:
module: bridgeCrabDispatch
method: MessageCallValidateFailed

- handler: handleEvent
kind: substrate/EventHandler
filter:
module: fromCrabIssuing
method: TokenBurnAndRemoteUnlocked

- handler: handleEvent
kind: substrate/EventHandler
filter:
module: fromCrabIssuing
method: TokenUnlockedConfirmed

Loading

0 comments on commit d2c527a

Please sign in to comment.