@steerprotocol/data-connector-loader 2.1.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/lib/{base → esm/bundle}/bundleBase.d.ts +7 -5
  2. package/lib/esm/bundle/bundleBase.js +127 -0
  3. package/lib/{bundle/bundleExecution.d.ts → esm/bundle/bundleDataConnector.d.ts} +2 -1
  4. package/lib/esm/bundle/bundleDataConnector.js +57 -0
  5. package/lib/esm/execution/executionApp.d.ts +17 -0
  6. package/lib/esm/execution/executionApp.js +304 -0
  7. package/lib/{loader/loaderBase.d.ts → esm/execution/executionBase.d.ts} +2 -2
  8. package/lib/esm/execution/executionBase.js +95 -0
  9. package/lib/esm/execution/executionDataConnector.d.ts +9 -0
  10. package/lib/esm/execution/executionDataConnector.js +90 -0
  11. package/lib/esm/index.d.ts +5 -0
  12. package/lib/esm/index.js +5 -0
  13. package/lib/esm/package.json +1 -0
  14. package/lib/esm/types.d.ts +104 -0
  15. package/lib/esm/types.js +1 -0
  16. package/lib/esm/utils/blockFinder.js +20 -0
  17. package/lib/esm/utils/consts.d.ts +4 -0
  18. package/lib/esm/utils/consts.js +4 -0
  19. package/lib/esm/utils/pluginManager.js +67 -0
  20. package/lib/esm/utils/utils.d.ts +16 -0
  21. package/lib/esm/utils/utils.js +119 -0
  22. package/lib/execution/executionApp.d.ts +3 -3
  23. package/lib/execution/executionApp.js +78 -80
  24. package/lib/execution/executionDataConnector.d.ts +2 -2
  25. package/lib/types.d.ts +1 -1
  26. package/lib/utils/consts.js +2 -2
  27. package/lib/utils/utils.d.ts +5 -5
  28. package/lib/utils/utils.js +5 -5
  29. package/package.json +33 -3
  30. package/lib/base/bundleBase.js +0 -119
  31. package/lib/base/loaderBase.d.ts +0 -14
  32. package/lib/base/loaderBase.js +0 -111
  33. package/lib/blockFinder.js +0 -36
  34. package/lib/bundle/bundleApp.d.ts +0 -6
  35. package/lib/bundle/bundleApp.js +0 -11
  36. package/lib/bundle/bundleExecution.js +0 -42
  37. package/lib/bundleDataConnector.d.ts +0 -7
  38. package/lib/bundleDataConnector.js +0 -42
  39. package/lib/bundleExecution.d.ts +0 -7
  40. package/lib/bundleExecution.js +0 -42
  41. package/lib/dataConnector.d.ts +0 -27
  42. package/lib/dataConnector.js +0 -133
  43. package/lib/execution/executionBundle.d.ts +0 -5
  44. package/lib/execution/executionBundle.js +0 -29
  45. package/lib/loader/loaderBase.js +0 -111
  46. package/lib/pluginManager.js +0 -84
  47. package/lib/utils.d.ts +0 -2
  48. package/lib/utils.js +0 -28
  49. /package/lib/{blockFinder.d.ts → esm/utils/blockFinder.d.ts} +0 -0
  50. /package/lib/{pluginManager.d.ts → esm/utils/pluginManager.d.ts} +0 -0
@@ -1,111 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.ExecutionBase = void 0;
13
- class ExecutionBase {
14
- constructor(maxRetries = 3) {
15
- this.maxRetries = 0;
16
- this.maxRetries = maxRetries;
17
- }
18
- delay(ms) {
19
- return new Promise((resolve) => setTimeout(resolve, ms));
20
- }
21
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
- log(...args) {
23
- if (process.env.DEBUG === 'true') {
24
- console.log(...args);
25
- }
26
- }
27
- getExecutionContext(dataConnector, vaultConfig) {
28
- return __awaiter(this, void 0, void 0, function* () {
29
- const provider = dataConnector.getProvider({});
30
- let blockTime = 0;
31
- let blockNumber = 9;
32
- if (provider) {
33
- blockTime = yield this.getAverageBlockTime(provider);
34
- blockNumber = yield this.getBlockNumberFromAvgBlockTime(provider, vaultConfig.epochTimestamp / 1000, // convert to seconds
35
- blockNumber);
36
- }
37
- const context = {
38
- executionTimestamp: new Date().getTime(),
39
- epochTimestamp: vaultConfig.epochTimestamp,
40
- epochLength: Number(vaultConfig.strategyConfigData.epochLength),
41
- vaultAddress: vaultConfig.vaultAddress,
42
- blockTime: blockTime,
43
- blockNumber: blockNumber,
44
- };
45
- return context;
46
- });
47
- }
48
- execute(_vaultConfig) {
49
- return __awaiter(this, void 0, void 0, function* () {
50
- throw new Error('Not implemented');
51
- });
52
- }
53
- getDataConnectorVersion(dataConnector) {
54
- return __awaiter(this, void 0, void 0, function* () {
55
- let version = 1;
56
- try {
57
- version = yield dataConnector.version();
58
- }
59
- catch (e) {
60
- // Data connector is missing version function, fallback to v1 logic
61
- }
62
- return version;
63
- });
64
- }
65
- getAverageBlockTime(provider, blockCount = 5) {
66
- return __awaiter(this, void 0, void 0, function* () {
67
- const latestBlock = yield provider.getBlock('latest');
68
- if (latestBlock) {
69
- const startBlockNumber = Math.max(0, latestBlock.number - blockCount + 1);
70
- let totalTimeDiff = 0;
71
- let previousBlock = yield provider.getBlock(startBlockNumber);
72
- if (!previousBlock) {
73
- return 0;
74
- }
75
- for (let i = startBlockNumber + 1; i <= latestBlock.number; i++) {
76
- const currentBlock = yield provider.getBlock(i);
77
- if (!currentBlock || !previousBlock || !currentBlock.timestamp || !previousBlock.timestamp) {
78
- continue;
79
- }
80
- const timeDiff = currentBlock.timestamp - previousBlock.timestamp;
81
- totalTimeDiff += timeDiff;
82
- previousBlock = currentBlock;
83
- }
84
- const averageBlockTime = totalTimeDiff / (blockCount - 1);
85
- return averageBlockTime;
86
- }
87
- else {
88
- return 0;
89
- }
90
- });
91
- }
92
- getBlockNumberFromAvgBlockTime(provider, targetTimestamp, avgBlockTime) {
93
- return __awaiter(this, void 0, void 0, function* () {
94
- const latestBlock = yield provider.getBlock('latest');
95
- const currentTime = Math.floor(Date.now() / 1000);
96
- const timeDifference = currentTime - targetTimestamp;
97
- if (!latestBlock) {
98
- return 0;
99
- }
100
- if (avgBlockTime === 0) {
101
- return latestBlock.number;
102
- }
103
- // Calculate the estimated number of blocks based on the average block time
104
- const estimatedBlockCount = Math.floor(timeDifference / avgBlockTime);
105
- // Calculate the estimated block number
106
- const estimatedBlockNumber = latestBlock.number - estimatedBlockCount;
107
- return Math.max(0, estimatedBlockNumber);
108
- });
109
- }
110
- }
111
- exports.ExecutionBase = ExecutionBase;
@@ -1,36 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.getBlockFromTimestamp = void 0;
13
- // import { providers, } from "ethers";
14
- /**
15
- * Iterates down from current block to find the block previous or equal to the given timestamp
16
- * Not the most efficient for far past timestamps, a binary search could be used for better performance
17
- * @param timestamp
18
- * @param Idenity
19
- */
20
- function getBlockFromTimestamp(timestamp, provider) {
21
- var _a, _b;
22
- return __awaiter(this, void 0, void 0, function* () {
23
- let currentBlock = (yield provider.getBlockNumber()) - 1;
24
- let runningTimestamp = (_a = (yield provider.getBlock(currentBlock))) === null || _a === void 0 ? void 0 : _a.timestamp;
25
- if (!runningTimestamp)
26
- return currentBlock;
27
- while (runningTimestamp > timestamp) {
28
- currentBlock -= 1;
29
- runningTimestamp = (_b = (yield provider.getBlock(currentBlock))) === null || _b === void 0 ? void 0 : _b.timestamp;
30
- if (!runningTimestamp)
31
- return currentBlock;
32
- }
33
- return currentBlock;
34
- });
35
- }
36
- exports.getBlockFromTimestamp = getBlockFromTimestamp;
@@ -1,6 +0,0 @@
1
- import { providers } from 'ethers';
2
- import { BundleBase } from './bundleBase';
3
- import { EtherRpcConfig } from '../types';
4
- export declare class AppBundle extends BundleBase {
5
- constructor(wasmFile: string | ArrayBuffer, _config: string, identity?: providers.Provider, etherRpcConfig?: EtherRpcConfig);
6
- }
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AppBundle = void 0;
4
- const bundleBase_1 = require("./bundleBase");
5
- class AppBundle extends bundleBase_1.BundleBase {
6
- // Identity should come from the node's IdentityManager.wallet
7
- constructor(wasmFile, _config, identity, etherRpcConfig) {
8
- super(wasmFile, _config, identity, etherRpcConfig);
9
- }
10
- }
11
- exports.AppBundle = AppBundle;
@@ -1,42 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.DataConnector = void 0;
13
- const ethers_1 = require("ethers");
14
- const bundleBase_1 = require("./bundleBase");
15
- class DataConnector extends bundleBase_1.BundleBase {
16
- // Identity should come from the node's IdentityManager.wallet
17
- constructor(wasmFile, _config, identity, etherRpcConfig) {
18
- super(wasmFile, _config, identity, etherRpcConfig);
19
- }
20
- // Makes contract instance and calls, returns stringified result
21
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
- view(config) {
23
- var _a;
24
- return __awaiter(this, void 0, void 0, function* () {
25
- const provider = this.getProvider(config);
26
- if (!provider)
27
- throw new Error('Provider not initialized');
28
- const contract = new ethers_1.Contract(config.address, config.abi, provider);
29
- // For formatting the parameters if needed
30
- const paramsArray = config.arguments;
31
- // Calculate block number for determinism
32
- // Best way to handle contextually non-null values?
33
- // const syncedBlock = await getBlockFromTimestamp(this.executionContext!.epochTimestamp, this.providerIdentity!) //@note uncomment for deterministic blocks
34
- // Call the desired method with the specified arguments at synced block
35
- // , {blockTag: syncedBlock}
36
- const result = yield contract[config.method](...paramsArray, { blockTag: (_a = this.executionContext) === null || _a === void 0 ? void 0 : _a.blockNumber }); //@note uncomment for deterministic blocks
37
- // This gets stringified before calling main
38
- return result;
39
- });
40
- }
41
- }
42
- exports.DataConnector = DataConnector;
@@ -1,7 +0,0 @@
1
- import { providers } from 'ethers';
2
- import { BundleBase } from './base/bundleBase';
3
- import { EtherRpcConfig, ViewConfig } from './types';
4
- export declare class DataConnector extends BundleBase {
5
- constructor(wasmFile: string | ArrayBuffer, _config: string, identity?: providers.Provider, etherRpcConfig?: EtherRpcConfig);
6
- view(config: ViewConfig): Promise<string>;
7
- }
@@ -1,42 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.DataConnector = void 0;
13
- const ethers_1 = require("ethers");
14
- const bundleBase_1 = require("./base/bundleBase");
15
- class DataConnector extends bundleBase_1.BundleBase {
16
- // Identity should come from the node's IdentityManager.wallet
17
- constructor(wasmFile, _config, identity, etherRpcConfig) {
18
- super(wasmFile, _config, identity, etherRpcConfig);
19
- }
20
- // Makes contract instance and calls, returns stringified result
21
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
- view(config) {
23
- var _a;
24
- return __awaiter(this, void 0, void 0, function* () {
25
- const provider = this.getProvider(config);
26
- if (!provider)
27
- throw new Error('Provider not initialized');
28
- const contract = new ethers_1.Contract(config.address, config.abi, provider);
29
- // For formatting the parameters if needed
30
- const paramsArray = config.arguments;
31
- // Calculate block number for determinism
32
- // Best way to handle contextually non-null values?
33
- // const syncedBlock = await getBlockFromTimestamp(this.executionContext!.epochTimestamp, this.providerIdentity!) //@note uncomment for deterministic blocks
34
- // Call the desired method with the specified arguments at synced block
35
- // , {blockTag: syncedBlock}
36
- const result = yield contract[config.method](...paramsArray, { blockTag: (_a = this.executionContext) === null || _a === void 0 ? void 0 : _a.blockNumber }); //@note uncomment for deterministic blocks
37
- // This gets stringified before calling main
38
- return result;
39
- });
40
- }
41
- }
42
- exports.DataConnector = DataConnector;
@@ -1,7 +0,0 @@
1
- import { providers } from 'ethers';
2
- import { BundleBase } from './base/bundleBase';
3
- import { EtherRpcConfig, ViewConfig } from './types';
4
- export declare class DataConnector extends BundleBase {
5
- constructor(wasmFile: string | ArrayBuffer, _config: string, identity?: providers.Provider, etherRpcConfig?: EtherRpcConfig);
6
- view(config: ViewConfig): Promise<string>;
7
- }
@@ -1,42 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.DataConnector = void 0;
13
- const ethers_1 = require("ethers");
14
- const bundleBase_1 = require("./base/bundleBase");
15
- class DataConnector extends bundleBase_1.BundleBase {
16
- // Identity should come from the node's IdentityManager.wallet
17
- constructor(wasmFile, _config, identity, etherRpcConfig) {
18
- super(wasmFile, _config, identity, etherRpcConfig);
19
- }
20
- // Makes contract instance and calls, returns stringified result
21
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
- view(config) {
23
- var _a;
24
- return __awaiter(this, void 0, void 0, function* () {
25
- const provider = this.getProvider(config);
26
- if (!provider)
27
- throw new Error('Provider not initialized');
28
- const contract = new ethers_1.Contract(config.address, config.abi, provider);
29
- // For formatting the parameters if needed
30
- const paramsArray = config.arguments;
31
- // Calculate block number for determinism
32
- // Best way to handle contextually non-null values?
33
- // const syncedBlock = await getBlockFromTimestamp(this.executionContext!.epochTimestamp, this.providerIdentity!) //@note uncomment for deterministic blocks
34
- // Call the desired method with the specified arguments at synced block
35
- // , {blockTag: syncedBlock}
36
- const result = yield contract[config.method](...paramsArray, { blockTag: (_a = this.executionContext) === null || _a === void 0 ? void 0 : _a.blockNumber }); //@note uncomment for deterministic blocks
37
- // This gets stringified before calling main
38
- return result;
39
- });
40
- }
41
- }
42
- exports.DataConnector = DataConnector;
@@ -1,27 +0,0 @@
1
- import { WasmModule } from '@steerprotocol/app-loader';
2
- import { ethers } from 'ethers';
3
- import { EtherRpcConfig, ExecutionContext } from './types';
4
- export declare class DataConnector {
5
- private wasmFile;
6
- module?: WasmModule;
7
- config: string;
8
- configObj: {
9
- [key: string]: any;
10
- };
11
- executionContext?: ExecutionContext & {
12
- epochTimestamp: number;
13
- };
14
- providerIdentity?: ethers.providers.Provider;
15
- etherRpcConfig?: EtherRpcConfig;
16
- constructor(wasmFile: string | ArrayBuffer, _config: string, identity?: ethers.providers.Provider, etherRpcConfig?: EtherRpcConfig);
17
- load(): Promise<WasmModule>;
18
- loadSync(): WasmModule;
19
- reset(): void;
20
- setExecutionContext(executionContext: ExecutionContext): void;
21
- execute(response: string): Promise<string>;
22
- getProvider(config: any): ethers.providers.Provider | undefined;
23
- view(config: any): Promise<string>;
24
- getResults(): string;
25
- version(): number;
26
- private subtractDate;
27
- }
@@ -1,133 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.DataConnector = void 0;
13
- const app_loader_1 = require("@steerprotocol/app-loader");
14
- const ethers_1 = require("ethers");
15
- class DataConnector {
16
- // Identity should come from the node's IdentityManager.wallet
17
- constructor(wasmFile, _config, identity, etherRpcConfig) {
18
- this.wasmFile = wasmFile;
19
- this.config = _config;
20
- this.configObj = JSON.parse(_config);
21
- this.providerIdentity = identity;
22
- this.etherRpcConfig = etherRpcConfig;
23
- }
24
- load() {
25
- return __awaiter(this, void 0, void 0, function* () {
26
- const instance = yield (0, app_loader_1.loadWasm)(this.wasmFile, {});
27
- this.module = instance;
28
- this.module.initialize(this.config);
29
- return this.module;
30
- });
31
- }
32
- loadSync() {
33
- if (typeof this.wasmFile === "string")
34
- throw new Error("Cannot load module synchronously unless wasmFile is of type ArrayBuffer");
35
- this.module = (0, app_loader_1.loadWasmSync)(this.wasmFile, {});
36
- return this.module;
37
- }
38
- reset() {
39
- if (this.module && this.module["reset"]) {
40
- this.module.reset();
41
- }
42
- }
43
- setExecutionContext(executionContext) {
44
- var _a;
45
- // Determine the timestamp for epoch
46
- const timestamp = this.subtractDate(executionContext.executionTimestamp, Number(executionContext.epochLength)).getTime();
47
- // Divide milliseconds data to seconds for transformation bundle
48
- const timestampInSecs = Math.floor(timestamp / 1000);
49
- this.executionContext = Object.assign(Object.assign({}, executionContext), { epochTimestamp: timestampInSecs });
50
- (_a = this.module) === null || _a === void 0 ? void 0 : _a.initialize(JSON.stringify(Object.assign(Object.assign({}, JSON.parse(this.config)), { executionContext: this.executionContext })));
51
- }
52
- execute(response) {
53
- return __awaiter(this, void 0, void 0, function* () {
54
- if (!this.executionContext) {
55
- throw new Error('Execution context missing');
56
- }
57
- // If the module is not initialized, try to load it
58
- if (!this.module) {
59
- try {
60
- yield this.load();
61
- }
62
- catch (_a) {
63
- throw new Error('Module not initialized');
64
- }
65
- }
66
- // If the module is still not initialized, throw an error
67
- if (!this.module)
68
- throw new Error('Module not initialized');
69
- // Call the main function with the response
70
- return yield this.module.execute(response);
71
- });
72
- }
73
- // This function returns a provider based on the chainId of the request
74
- // The chainId is first checked in the data connector config, and if not found
75
- // it is checked in the request config. If the chainId is found in either of
76
- // these places, the provider is returned from the etherRpcConfig object
77
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
78
- getProvider(config) {
79
- let chainId;
80
- // check if chainId is configured in data connector config
81
- if (this.configObj.chainId) {
82
- chainId = this.configObj.chainId;
83
- }
84
- else if (config && config.chainId) {
85
- // if chainId is in the request config from dataconnector execution
86
- chainId = config.chainId;
87
- }
88
- if (chainId && this.etherRpcConfig && this.etherRpcConfig[chainId]) {
89
- const provider = new ethers_1.ethers.providers.JsonRpcProvider(this.etherRpcConfig[chainId].url);
90
- return provider;
91
- }
92
- else {
93
- return this.providerIdentity;
94
- }
95
- }
96
- // Makes contract instance and calls, returns stringified result
97
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
98
- view(config) {
99
- var _a;
100
- return __awaiter(this, void 0, void 0, function* () {
101
- const provider = this.getProvider(config);
102
- if (!provider)
103
- throw new Error('Provider not initialized');
104
- const contract = new ethers_1.ethers.Contract(config.address, config.abi, provider);
105
- // For formatting the parameters if needed
106
- const paramsArray = config.arguments;
107
- // Calculate block number for determinism
108
- // Best way to handle contextually non-null values?
109
- // const syncedBlock = await getBlockFromTimestamp(this.executionContext!.epochTimestamp, this.providerIdentity!) //@note uncomment for deterministic blocks
110
- // Call the desired method with the specified arguments at synced block
111
- // , {blockTag: syncedBlock}
112
- const result = yield contract[config.method](...paramsArray, { blockTag: (_a = this.executionContext) === null || _a === void 0 ? void 0 : _a.blockNumber }); //@note uncomment for deterministic blocks
113
- // This gets stringified before calling main
114
- return result;
115
- });
116
- }
117
- getResults() {
118
- if (!this.module)
119
- throw new Error('Module not initialized');
120
- return this.module.transform();
121
- }
122
- version() {
123
- if (!this.module)
124
- throw new Error('Module not initialized');
125
- return Number(this.module.version());
126
- }
127
- subtractDate(timestamp, epochLength) {
128
- const date = new Date(timestamp);
129
- date.setSeconds(date.getSeconds() - epochLength);
130
- return date;
131
- }
132
- }
133
- exports.DataConnector = DataConnector;
@@ -1,5 +0,0 @@
1
- import { ExecutionBase } from "../loader/loaderBase";
2
- export declare class ExecutionBundle extends ExecutionBase {
3
- constructor(maxRetries?: number);
4
- execute<T>(_vaultConfig: string): Promise<Array<T>>;
5
- }
@@ -1,29 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.ExecutionBundle = void 0;
13
- const bundleDataConnector_1 = require("../bundle/bundleDataConnector");
14
- const loaderBase_1 = require("../loader/loaderBase");
15
- class ExecutionBundle extends loaderBase_1.ExecutionBase {
16
- constructor(maxRetries = 3) {
17
- super(maxRetries);
18
- }
19
- execute(_vaultConfig) {
20
- return __awaiter(this, void 0, void 0, function* () {
21
- const dataConnector = new bundleDataConnector_1.DataConnector(_vaultConfig, _vaultConfig);
22
- const executionContext = yield this.getExecutionContext(dataConnector, JSON.parse(_vaultConfig));
23
- dataConnector.setExecutionContext(executionContext);
24
- const result = yield dataConnector.execute(_vaultConfig);
25
- return [JSON.parse(result)];
26
- });
27
- }
28
- }
29
- exports.ExecutionBundle = ExecutionBundle;
@@ -1,111 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.ExecutionBase = void 0;
13
- class ExecutionBase {
14
- constructor(maxRetries = 3) {
15
- this.maxRetries = 0;
16
- this.maxRetries = maxRetries;
17
- }
18
- delay(ms) {
19
- return new Promise((resolve) => setTimeout(resolve, ms));
20
- }
21
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
- log(...args) {
23
- if (process.env.DEBUG === 'true') {
24
- console.log(...args);
25
- }
26
- }
27
- getExecutionContext(dataConnector, vaultConfig) {
28
- return __awaiter(this, void 0, void 0, function* () {
29
- const provider = dataConnector.getProvider({});
30
- let blockTime = 0;
31
- let blockNumber = 9;
32
- if (provider) {
33
- blockTime = yield this.getAverageBlockTime(provider);
34
- blockNumber = yield this.getBlockNumberFromAvgBlockTime(provider, vaultConfig.epochTimestamp / 1000, // convert to seconds
35
- blockNumber);
36
- }
37
- const context = {
38
- executionTimestamp: new Date().getTime(),
39
- epochTimestamp: vaultConfig.epochTimestamp,
40
- epochLength: Number(vaultConfig.strategyConfigData.epochLength),
41
- vaultAddress: vaultConfig.vaultAddress,
42
- blockTime: blockTime,
43
- blockNumber: blockNumber,
44
- };
45
- return context;
46
- });
47
- }
48
- execute(_vaultConfig) {
49
- return __awaiter(this, void 0, void 0, function* () {
50
- throw new Error('Not implemented');
51
- });
52
- }
53
- getDataConnectorVersion(dataConnector) {
54
- return __awaiter(this, void 0, void 0, function* () {
55
- let version = 1;
56
- try {
57
- version = yield dataConnector.version();
58
- }
59
- catch (e) {
60
- // Data connector is missing version function, fallback to v1 logic
61
- }
62
- return version;
63
- });
64
- }
65
- getAverageBlockTime(provider, blockCount = 5) {
66
- return __awaiter(this, void 0, void 0, function* () {
67
- const latestBlock = yield provider.getBlock('latest');
68
- if (latestBlock) {
69
- const startBlockNumber = Math.max(0, latestBlock.number - blockCount + 1);
70
- let totalTimeDiff = 0;
71
- let previousBlock = yield provider.getBlock(startBlockNumber);
72
- if (!previousBlock) {
73
- return 0;
74
- }
75
- for (let i = startBlockNumber + 1; i <= latestBlock.number; i++) {
76
- const currentBlock = yield provider.getBlock(i);
77
- if (!currentBlock || !previousBlock || !currentBlock.timestamp || !previousBlock.timestamp) {
78
- continue;
79
- }
80
- const timeDiff = currentBlock.timestamp - previousBlock.timestamp;
81
- totalTimeDiff += timeDiff;
82
- previousBlock = currentBlock;
83
- }
84
- const averageBlockTime = totalTimeDiff / (blockCount - 1);
85
- return averageBlockTime;
86
- }
87
- else {
88
- return 0;
89
- }
90
- });
91
- }
92
- getBlockNumberFromAvgBlockTime(provider, targetTimestamp, avgBlockTime) {
93
- return __awaiter(this, void 0, void 0, function* () {
94
- const latestBlock = yield provider.getBlock('latest');
95
- const currentTime = Math.floor(Date.now() / 1000);
96
- const timeDifference = currentTime - targetTimestamp;
97
- if (!latestBlock) {
98
- return 0;
99
- }
100
- if (avgBlockTime === 0) {
101
- return latestBlock.number;
102
- }
103
- // Calculate the estimated number of blocks based on the average block time
104
- const estimatedBlockCount = Math.floor(timeDifference / avgBlockTime);
105
- // Calculate the estimated block number
106
- const estimatedBlockNumber = latestBlock.number - estimatedBlockCount;
107
- return Math.max(0, estimatedBlockNumber);
108
- });
109
- }
110
- }
111
- exports.ExecutionBase = ExecutionBase;