-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a070f5c
Showing
12 changed files
with
2,939 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.idea | ||
db | ||
dist | ||
logs | ||
node_modules | ||
out | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
db | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
Oops, something went wrong.