Skip to content

Commit

Permalink
finished near
Browse files Browse the repository at this point in the history
  • Loading branch information
troykessler committed Jul 13, 2022
1 parent 659c095 commit 7eddce3
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 230 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@kyve/near",
"version": "0.2.3",
"version": "0.3.0",
"license": "MIT",
"scripts": {
"build": "rimraf dist && tsc",
"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",
"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",
"start": "node ./dist/src/index.js",
"format": "prettier --write ."
},
Expand All @@ -22,7 +22,7 @@
"singleQuote": true
},
"dependencies": {
"@kyve/core": "1.2.3",
"@kyve/core": "1.3.0",
"near-api-js": "^0.44.2"
},
"devDependencies": {
Expand Down
76 changes: 8 additions & 68 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,10 @@
import KYVE, { Item } from '@kyve/core';
import { Signature } from './types';
import { fetchBlock, fetchHeight, isBlockNotFound } from './utils';
import { name, version } from '../package.json';
import { Node, Arweave, Gzip, JsonFileCache } from '@kyve/core';

process.env.KYVE_RUNTIME = name;
process.env.KYVE_VERSION = version;
import Near from './runtime';

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

class KyveNear extends KYVE {
public async getDataItem(key: string): Promise<Item> {
let block;

const height = await fetchHeight(
this.pool.config.rpc,
await this.getSignature()
);
if (+key > height) throw new Error();

try {
block = await fetchBlock(
this.pool.config.rpc,
+key,
await this.getSignature()
);
} catch (err) {
if (isBlockNotFound(err)) return { key, value: null };

this.logger.warn(`Failed to fetch block ${key}. Retrying ...`);

throw err;
}

return { key, value: block };
}

public async getNextKey(key: string): Promise<string> {
if (key) {
return (parseInt(key) + 1).toString();
}

return '0';
}

public async formatValue(value: any): Promise<string> {
return value.header?.hash ?? 'error';
}

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 KyveNear().start();
new Node()
.addRuntime(new Near())
.addStorageProvider(new Arweave())
.addCompression(new Gzip())
.addCache(new JsonFileCache())
.start();
54 changes: 54 additions & 0 deletions src/runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { DataItem, IRuntime, Node } from '@kyve/core';
import { fetchBlock, fetchHeight, isBlockNotFound } from './utils';
import { name, version } from '../package.json';

export default class Near implements IRuntime {
public name = name;
public version = version;

public async getDataItem(core: Node, key: string): Promise<DataItem> {
let block;

const headers = await this.generateCoinbaseCloudHeaders(core);

const height = await fetchHeight(core.poolConfig.rpc, headers);
if (+key > height) throw new Error();

try {
block = await fetchBlock(core.poolConfig.rpc, +key, headers);
} catch (err) {
if (isBlockNotFound(err)) return { key, value: null };

throw err;
}

return { key, value: block };
}

public async getNextKey(key: string): Promise<string> {
return (parseInt(key) + 1).toString();
}

public async formatValue(value: any): Promise<string> {
return value.hash;
}

private async generateCoinbaseCloudHeaders(core: Node): Promise<any> {
// requestSignature for coinbase cloud
const address = core.client.account.address;
const timestamp = new Date().valueOf().toString();
const poolId = core.pool.id;

const { signature, pub_key } = await core.client.signString(
`${address}//${poolId}//${timestamp}`
);

return {
'Content-Type': 'application/json',
Signature: signature,
'Public-Key': pub_key.value,
'Pool-ID': poolId,
Timestamp: timestamp,
};
}
}
26 changes: 10 additions & 16 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { connect } from 'near-api-js';
import { NearConfig } from 'near-api-js/lib/near';
import { Provider } from 'near-api-js/lib/providers';
import { ChunkResult } from 'near-api-js/lib/providers/provider';
import { NOT_FOUND, Signature } from './types';
import { NOT_FOUND } from './types';

export async function fetchHeight(
endpoint: string,
signature: Signature
headers: any
): Promise<number> {
const provider = await initialiseNearRPC(endpoint, signature);
const provider = await initialiseNearRPC(endpoint, headers);

const latestBlock = await provider.block({
finality: 'final',
Expand All @@ -20,9 +20,9 @@ export async function fetchHeight(
export async function fetchBlock(
endpoint: string,
height: number,
signature: Signature
headers: any
): Promise<any> {
const provider = await initialiseNearRPC(endpoint, signature);
const provider = await initialiseNearRPC(endpoint, headers);

const block = await provider.block({
blockId: height,
Expand All @@ -31,7 +31,7 @@ export async function fetchBlock(
const chunks = await fetchChunks(
endpoint,
block.chunks.map((chunk) => chunk.chunk_hash),
signature
headers
);

return {
Expand All @@ -43,9 +43,9 @@ export async function fetchBlock(
async function fetchChunks(
endpoint: string,
hashes: string[],
signature: Signature
headers: any
): Promise<any[]> {
const provider = await initialiseNearRPC(endpoint, signature);
const provider = await initialiseNearRPC(endpoint, headers);
const chunks: ChunkResult[] = [];

for (const hash of hashes) {
Expand All @@ -58,20 +58,14 @@ async function fetchChunks(

async function initialiseNearRPC(
endpoint: string,
signature: Signature
headers: any
): Promise<Provider> {
const config: NearConfig = {
// @ts-ignore
deps: {},
networkId: 'mainnet',
nodeUrl: endpoint,
headers: {
'Content-Type': 'application/json',
Signature: signature.signature,
'Public-Key': signature.pubKey,
'Pool-ID': signature.poolId,
Timestamp: signature.timestamp,
},
headers,
};

const client = await connect(config);
Expand Down
Loading

0 comments on commit 7eddce3

Please sign in to comment.