Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
mbreithecker committed Jul 6, 2022
2 parents 474e4df + 91cdc16 commit a4ab406
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 196 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.1.0",
"version": "0.2.1",
"license": "MIT",
"scripts": {
"build": "rimraf dist && tsc",
"build:binaries": "yarn build && rimraf out && pkg --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/checksum.js",
"start": "node ./dist/src/index.js",
"format": "prettier --write ."
},
Expand All @@ -22,7 +22,7 @@
"singleQuote": true
},
"dependencies": {
"@kyve/core": "1.1.0",
"@kyve/core": "1.2.1",
"near-api-js": "^0.44.2"
},
"devDependencies": {
Expand Down
24 changes: 17 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import KYVE from '@kyve/core';
import KYVE, { Item } from '@kyve/core';
import { Signature } from './types';
import { fetchBlock, fetchHeight, isBlockNotFound } from './utils';
import { name, version } from '../package.json';
Expand All @@ -11,34 +11,44 @@ KYVE.metrics.register.setDefaultLabels({
});

class KyveNear extends KYVE {
public async getDataItem(key: number): Promise<{ key: number; value: any }> {
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();
if (+key > height) throw new Error();

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

this.logger.warn(
`⚠️ EXTERNAL ERROR: Failed to fetch block ${key}. Retrying ...`
);
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();
Expand Down
Loading

0 comments on commit a4ab406

Please sign in to comment.