Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
johnletey committed May 4, 2022
0 parents commit a070f5c
Show file tree
Hide file tree
Showing 12 changed files with 2,939 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .github/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!--suppress HtmlDeprecatedAttribute -->

<div align="center">
<h1>KYVE + Substrate</h1>
</div>

![banner](https://github.com/kyve-org/assets/raw/main/banners/Substrate.png)

## Configuration

```json
{
"rpc": "https://proxy.kyve.network/polkadot",
"github": "https://github.com/kyve-org/substrate"
}
```

## Endpoints

- [`/blocks/{height}`](https://paritytech.github.io/substrate-api-sidecar/dist)
14 changes: 14 additions & 0 deletions .github/webhook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const Discord = require('webhook-discord');
const client = new Discord.Webhook(process.env.DISCORD);

const message = new Discord.MessageBuilder()
.setColor('#58C6B2')
.setTitle(':new: New Release :new:')
.setDescription(`\`@kyve/substrate\` ${process.env.VERSION} released.`)
.setURL(
`https://github.com/kyve-org/substrate/releases/tag/${process.env.VERSION}`
)
.setName('Release Bot');

// noinspection JSIgnoredPromiseFromCall
client.send(message);
70 changes: 70 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Release

on:
release:
types: [released]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
binaries:
name: Build release binaries.
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v3

- name: Install yarn.
run: npm install -g yarn
- name: Install dependencies.
run: yarn install
- name: Build.
run: yarn build:binaries

- uses: actions/upload-artifact@v3
with:
name: kyve-substrate-linux
path: ./out/substrate-linux
- uses: actions/upload-artifact@v3
with:
name: kyve-substrate-macos
path: ./out/substrate-macos
- uses: actions/upload-artifact@v3
with:
name: kyve-substrate-win
path: ./out/substrate-win.exe

docker:
name: Build and publish Docker container.
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Login to Docker.
run: docker login -u ${{ secrets.DOCKER_USER }} -p ${{ secrets.DOCKER_PASSWORD }}

- name: Build the Docker image.
run: docker build -t kyve/substrate:${GITHUB_REF#refs/tags/} -t kyve/substrate:latest .

- name: Push to Docker hub.
run: docker push -a kyve/substrate

webhook:
name: Post a message to Discord.
needs: [binaries, docker]
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v3

- name: Install yarn.
run: npm install -g yarn
- name: Install dependencies.
run: yarn install

- name: Run script.
run: DISCORD=${{ secrets.DISCORD }} VERSION=${GITHUB_REF#refs/tags/} node .github/webhook.js
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.idea
db
dist
logs
node_modules
out
.DS_Store
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
db
dist
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM node:current-alpine

COPY package.json .
RUN yarn install
COPY . .
RUN yarn build

ENTRYPOINT [ "yarn", "start" ]
37 changes: 37 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@kyve/substrate",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"build": "rimraf dist && tsc",
"build:binaries": "yarn build && rimraf out && pkg package.json",
"start": "node ./dist/src/index.js",
"format": "prettier --write ."
},
"bin": "./dist/src/index.js",
"pkg": {
"scripts": "./dist/src/index.js",
"assets": "./node_modules/@kyve/sdk/dist/proto/*",
"targets": [
"latest-linux-x64",
"latest-macos-x64",
"latest-win-x64"
],
"outputPath": "out"
},
"prettier": {
"singleQuote": true
},
"dependencies": {
"@kyve/core": "KYVENetwork/core#v0.4.0",
"@kyve/sdk": "KYVENetwork/sdk#main",
"axios": "^0.27.2"
},
"devDependencies": {
"pkg": "^5.6.0",
"prettier": "^2.6.2",
"rimraf": "^3.0.2",
"typescript": "^4.6.4",
"webhook-discord": "^3.7.8"
}
}
54 changes: 54 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import KYVE from '@kyve/core';
import { Signature } from './types';
import { fetchBlock, isHeightOutOfRange } from './utils';
import { name, version } from '../package.json';

process.env.KYVE_RUNTIME = name;
process.env.KYVE_VERSION = version;

KYVE.metrics.register.setDefaultLabels({
app: process.env.KYVE_RUNTIME,
});

class KyveSubstrate extends KYVE {
public async getDataItem(key: number): Promise<{ key: number; value: any }> {
let block;

try {
block = await fetchBlock(
this.pool.config.rpc,
key,
await this.getSignature()
);
} catch (err) {
if (isHeightOutOfRange(err)) throw new Error();

this.logger.warn(
`⚠️ EXTERNAL ERROR: Failed to fetch block ${key}. Retrying ...`
);

throw err;
}

return { key, value: block };
}

private async getSignature(): Promise<Signature> {
const address = await this.sdk.wallet.getAddress();
const timestamp = new Date().valueOf().toString();

const message = `${address}//${this.poolId}//${timestamp}`;

const { signature, pub_key } = await this.sdk.signString(message);

return {
signature,
pubKey: pub_key.value,
poolId: this.poolId.toString(),
timestamp,
};
}
}

// noinspection JSIgnoredPromiseFromCall
new KyveSubstrate().start();
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const UNABLE_TO_RETRIEVE =
'Unable to retrieve header and parent from supplied hash';

export interface Signature {
signature: string;
pubKey: string;
poolId: string;
timestamp: string;
}
37 changes: 37 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import axios, { AxiosResponse } from 'axios';
import { Signature, UNABLE_TO_RETRIEVE } from './types';

export async function fetchBlock(
endpoint: string,
height: number,
signature: Signature
) {
return await requestSidecarAPI(`${endpoint}/blocks/${height}`, signature);
}

export function isHeightOutOfRange(err: any): boolean {
if (err.isAxiosError) {
const response: AxiosResponse = err.response;

if (response && response.data && response.data.message) {
if (response.data.message === UNABLE_TO_RETRIEVE) {
return true;
}
}
}

return false;
}

async function requestSidecarAPI(endpoint: string, signature: Signature) {
const { data } = await axios.get(endpoint, {
headers: {
Signature: signature.signature,
'Public-Key': signature.pubKey,
'Pool-ID': signature.poolId,
Timestamp: signature.timestamp,
},
});

return data;
}
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2019",
"strict": true,
"outDir": "dist",
"esModuleInterop": true,
"resolveJsonModule": true
},
"files": ["src/index.ts"]
}
Loading

0 comments on commit a070f5c

Please sign in to comment.