Skip to content

Commit f659805

Browse files
committed
finished substrate
1 parent eb7d204 commit f659805

5 files changed

Lines changed: 285 additions & 218 deletions

File tree

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "@kyve/substrate",
3-
"version": "0.1.3",
3+
"version": "0.3.0",
44
"license": "MIT",
55
"scripts": {
66
"build": "rimraf dist && tsc",
7-
"build:binaries": "yarn build && rimraf out && pkg --no-bytecode --public-packages '*' --output out/kyve package.json && node ./node_modules/@kyve/core/dist/src/checksum.js",
7+
"build:binaries": "yarn build && rimraf out && pkg --no-bytecode --public-packages '*' --output out/kyve package.json && node ./node_modules/@kyve/core/dist/src/scripts/checksum.js",
88
"start": "node ./dist/src/index.js",
99
"format": "prettier --write ."
1010
},
@@ -22,7 +22,7 @@
2222
"singleQuote": true
2323
},
2424
"dependencies": {
25-
"@kyve/core": "1.2.3",
25+
"@kyve/core": "1.3.0",
2626
"axios": "^0.27.2"
2727
},
2828
"devDependencies": {

src/index.ts

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,10 @@
1-
import KYVE, { Item } from '@kyve/core';
2-
import { Signature } from './types';
3-
import { fetchBlock, isHeightOutOfRange } from './utils';
4-
import { name, version } from '../package.json';
1+
import { Node, Arweave, Gzip, JsonFileCache } from '@kyve/core';
52

6-
process.env.KYVE_RUNTIME = name;
7-
process.env.KYVE_VERSION = version;
3+
import Substrate from './runtime';
84

9-
KYVE.metrics.register.setDefaultLabels({
10-
app: process.env.KYVE_RUNTIME,
11-
});
12-
13-
class KyveSubstrate extends KYVE {
14-
public async getDataItem(key: string): Promise<Item> {
15-
let block;
16-
17-
try {
18-
block = await fetchBlock(
19-
this.pool.config.rpc,
20-
+key,
21-
await this.getSignature()
22-
);
23-
} catch (err) {
24-
if (isHeightOutOfRange(err)) throw new Error();
25-
26-
this.logger.warn(`Failed to fetch block ${key}. Retrying ...`);
27-
28-
throw err;
29-
}
30-
31-
return { key, value: block };
32-
}
33-
34-
public async getNextKey(key: string): Promise<string> {
35-
if (key) {
36-
return (parseInt(key) + 1).toString();
37-
}
38-
39-
return '0';
40-
}
41-
42-
public async formatValue(value: any): Promise<string> {
43-
return value.hash;
44-
}
45-
46-
private async getSignature(): Promise<Signature> {
47-
const address = await this.sdk.wallet.getAddress();
48-
const timestamp = new Date().valueOf().toString();
49-
50-
const message = `${address}//${this.poolId}//${timestamp}`;
51-
52-
const { signature, pub_key } = await this.sdk.signString(message);
53-
54-
return {
55-
signature,
56-
pubKey: pub_key.value,
57-
poolId: this.poolId.toString(),
58-
timestamp,
59-
};
60-
}
61-
}
62-
63-
// noinspection JSIgnoredPromiseFromCall
64-
new KyveSubstrate().start();
5+
new Node()
6+
.addRuntime(new Substrate())
7+
.addStorageProvider(new Arweave())
8+
.addCompression(new Gzip())
9+
.addCache(new JsonFileCache())
10+
.start();

src/runtime.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { DataItem, IRuntime, Node } from '@kyve/core';
2+
import { name, version } from '../package.json';
3+
import { fetchBlock, isHeightOutOfRange } from './utils';
4+
5+
export default class Substrate implements IRuntime {
6+
public name = name;
7+
public version = version;
8+
9+
public async getDataItem(core: Node, key: string): Promise<DataItem> {
10+
let block;
11+
12+
const headers = await this.generateCoinbaseCloudHeaders(core);
13+
14+
try {
15+
block = await fetchBlock(core.poolConfig.rpc, +key, headers);
16+
} catch (err) {
17+
if (isHeightOutOfRange(err)) throw new Error();
18+
19+
throw err;
20+
}
21+
22+
return { key, value: block };
23+
}
24+
25+
public async getNextKey(key: string): Promise<string> {
26+
return (parseInt(key) + 1).toString();
27+
}
28+
29+
public async formatValue(value: any): Promise<string> {
30+
return value.hash;
31+
}
32+
33+
private async generateCoinbaseCloudHeaders(core: Node): Promise<any> {
34+
// requestSignature for coinbase cloud
35+
const address = core.client.account.address;
36+
const timestamp = new Date().valueOf().toString();
37+
const poolId = core.pool.id;
38+
39+
const { signature, pub_key } = await core.client.signString(
40+
`${address}//${poolId}//${timestamp}`
41+
);
42+
43+
return {
44+
'Content-Type': 'application/json',
45+
Signature: signature,
46+
'Public-Key': pub_key.value,
47+
'Pool-ID': poolId,
48+
Timestamp: timestamp,
49+
};
50+
}
51+
}

src/utils.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { Signature, UNABLE_TO_RETRIEVE } from './types';
44
export async function fetchBlock(
55
endpoint: string,
66
height: number,
7-
signature: Signature
7+
headers: any
88
) {
9-
return await requestSidecarAPI(`${endpoint}/blocks/${height}`, signature);
9+
return await requestSidecarAPI(`${endpoint}/blocks/${height}`, headers);
1010
}
1111

1212
export function isHeightOutOfRange(err: any): boolean {
@@ -23,15 +23,9 @@ export function isHeightOutOfRange(err: any): boolean {
2323
return false;
2424
}
2525

26-
async function requestSidecarAPI(endpoint: string, signature: Signature) {
26+
async function requestSidecarAPI(endpoint: string, headers: any) {
2727
const { data } = await axios.get(endpoint, {
28-
headers: {
29-
'Content-Type': 'application/json',
30-
Signature: signature.signature,
31-
'Public-Key': signature.pubKey,
32-
'Pool-ID': signature.poolId,
33-
Timestamp: signature.timestamp,
34-
},
28+
headers,
3529
});
3630

3731
return data;

0 commit comments

Comments
 (0)