|
1 | | -import KYVE from "@kyve/core"; |
2 | | -import { providers } from "ethers"; |
3 | | -import { version } from "../package.json"; |
| 1 | +import KYVE from '@kyve/core'; |
| 2 | +import { Network, Signature } from './types'; |
| 3 | +import { fetchBlock } from './utils'; |
| 4 | +import { name, version } from '../package.json'; |
4 | 5 |
|
5 | | -process.env.KYVE_RUNTIME = "@kyve/evm"; |
| 6 | +process.env.KYVE_RUNTIME = name; |
6 | 7 | process.env.KYVE_VERSION = version; |
7 | 8 |
|
8 | 9 | KYVE.metrics.register.setDefaultLabels({ |
9 | 10 | app: process.env.KYVE_RUNTIME, |
10 | 11 | }); |
11 | 12 |
|
12 | | -class EVM extends KYVE { |
13 | | - // pull data item from source |
| 13 | +class KyveEvm extends KYVE { |
14 | 14 | public async getDataItem(key: number): Promise<{ key: number; value: any }> { |
15 | | - let provider; |
16 | 15 | let block; |
17 | 16 |
|
18 | | - // setup provider for evm chain |
19 | 17 | try { |
20 | | - provider = new providers.StaticJsonRpcProvider(this.pool.config.rpc); |
| 18 | + let network: Network | undefined; |
| 19 | + if (this.pool.config.chainId && this.pool.config.chainName) { |
| 20 | + network = { |
| 21 | + chainId: this.pool.config.chainId, |
| 22 | + name: this.pool.config.chainName, |
| 23 | + }; |
| 24 | + } |
| 25 | + |
| 26 | + block = await fetchBlock( |
| 27 | + this.pool.config.rpc, |
| 28 | + key, |
| 29 | + await this.getSignature(), |
| 30 | + network |
| 31 | + ); |
21 | 32 | } catch (err) { |
22 | 33 | this.logger.warn( |
23 | | - ` EXTERNAL ERROR: Failed to connect with rpc: ${this.pool.config.rpc}. Retrying ...` |
| 34 | + `⚠️ EXTERNAL ERROR: Failed to fetch block ${key}. Retrying ...` |
24 | 35 | ); |
25 | | - // forward error to core |
| 36 | + |
26 | 37 | throw err; |
27 | 38 | } |
28 | 39 |
|
29 | | - // try to fetch current block height |
30 | | - try { |
31 | | - const height = await provider.getBlockNumber(); |
| 40 | + if (!block) throw new Error(); |
32 | 41 |
|
33 | | - // throw error if requested block height is not available yet |
34 | | - if (height < key) { |
35 | | - this.logger.warn( |
36 | | - ` EXTERNAL ERROR: Requested block does not exist yet. Retrying ...` |
37 | | - ); |
38 | | - throw new Error(); |
39 | | - } |
40 | | - } catch (err) { |
41 | | - this.logger.warn( |
42 | | - ` EXTERNAL ERROR: Failed to fetch current block height. Retrying ...` |
43 | | - ); |
44 | | - // forward error to core |
45 | | - throw err; |
46 | | - } |
| 42 | + return { key, value: block }; |
| 43 | + } |
47 | 44 |
|
48 | | - // fetch block with transactions at requested height |
49 | | - try { |
50 | | - block = await provider?.getBlockWithTransactions(key)!; |
| 45 | + private async getSignature(): Promise<Signature> { |
| 46 | + const address = await this.sdk.wallet.getAddress(); |
| 47 | + const timestamp = new Date().valueOf().toString(); |
51 | 48 |
|
52 | | - // delete transaction confirmations from block since they are not deterministic |
53 | | - block.transactions.forEach( |
54 | | - (transaction: Partial<providers.TransactionResponse>) => |
55 | | - delete transaction.confirmations |
56 | | - ); |
57 | | - } catch (err) { |
58 | | - this.logger.warn( |
59 | | - ` EXTERNAL ERROR: Failed to fetch data item from source at height ${key}. Retrying ...` |
60 | | - ); |
61 | | - // forward error to core |
62 | | - throw err; |
63 | | - } |
| 49 | + const message = `${address}//${this.poolId}//${timestamp}`; |
| 50 | + |
| 51 | + const { signature, pub_key } = await this.sdk.signString(message); |
64 | 52 |
|
65 | 53 | return { |
66 | | - key, |
67 | | - value: block, |
| 54 | + signature, |
| 55 | + pubKey: pub_key.value, |
| 56 | + poolId: this.poolId.toString(), |
| 57 | + timestamp, |
68 | 58 | }; |
69 | 59 | } |
70 | 60 | } |
71 | 61 |
|
72 | | -new EVM().start(); |
| 62 | +// noinspection JSIgnoredPromiseFromCall |
| 63 | +new KyveEvm().start(); |
0 commit comments