Skip to content

Commit

Permalink
finished evm
Browse files Browse the repository at this point in the history
  • Loading branch information
troykessler committed Jul 13, 2022
2 parents 130afc7 + 362e31c commit a9d5c00
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 254 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/evm",
"version": "1.2.3",
"version": "1.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",
"ethers": "^5.6.5"
},
"devDependencies": {
Expand Down
78 changes: 8 additions & 70 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,10 @@
import KYVE, { Item } from '@kyve/core';
import { Network, Signature } from './types';
import { fetchBlock } 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 EVM from './runtime';

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

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

try {
let network: Network | undefined;
if (this.pool.config.chainId && this.pool.config.chainName) {
network = {
chainId: this.pool.config.chainId,
name: this.pool.config.chainName,
};
}

block = await fetchBlock(
this.pool.config.rpc,
+key,
await this.getSignature(),
network
);
} catch (err) {
this.logger.warn(` Failed to get data item from height ${key}`);
throw err;
}

if (!block) throw new Error();

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.hash;
}

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

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

public async getDataItem(core: Node, key: string): Promise<DataItem> {
try {
// set network settings if available
let network;

if (core.poolConfig.chainId && core.poolConfig.chainName) {
network = {
chainId: core.poolConfig.chainId,
name: core.poolConfig.chainName,
};
}

// get auth headers for coinbase cloud endpoints
const headers = await this.generateCoinbaseCloudHeaders(core);

// setup web3 provider
const provider = new providers.StaticJsonRpcProvider(
{
url: core.poolConfig.rpc,
headers,
},
network
);

// fetch data item
const value = await provider.getBlockWithTransactions(+key);

// throw if data item is not available
if (!value) throw new Error();

// Delete the number of confirmations from a transaction to keep data deterministic.
value.transactions.forEach(
(tx: Partial<providers.TransactionResponse>) => delete tx.confirmations
);

return {
key,
value,
};
} catch (error) {
throw error;
}
}

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,
'Pool-ID': poolId,
Timestamp: timestamp,
};
}
}
11 changes: 0 additions & 11 deletions src/types.ts

This file was deleted.

35 changes: 0 additions & 35 deletions src/utils.ts

This file was deleted.

Loading

0 comments on commit a9d5c00

Please sign in to comment.