@steerprotocol/data-connector-loader 0.1.5 → 2.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.
- package/lib/base/bundleBase.d.ts +27 -0
- package/lib/base/bundleBase.js +119 -0
- package/lib/base/loaderBase.d.ts +14 -0
- package/lib/base/loaderBase.js +111 -0
- package/lib/bundle/bundleApp.d.ts +6 -0
- package/lib/bundle/bundleApp.js +11 -0
- package/lib/bundle/bundleBase.d.ts +29 -0
- package/lib/bundle/bundleBase.js +150 -0
- package/lib/bundle/bundleDataConnector.d.ts +8 -0
- package/lib/bundle/bundleDataConnector.js +73 -0
- package/lib/bundle/bundleExecution.d.ts +7 -0
- package/lib/bundle/bundleExecution.js +42 -0
- package/lib/bundleDataConnector.d.ts +7 -0
- package/lib/bundleDataConnector.js +42 -0
- package/lib/bundleExecution.d.ts +7 -0
- package/lib/bundleExecution.js +42 -0
- package/lib/execution/executionApp.d.ts +17 -0
- package/lib/execution/executionApp.js +325 -0
- package/lib/execution/executionBase.d.ts +14 -0
- package/lib/execution/executionBase.js +111 -0
- package/lib/execution/executionBundle.d.ts +5 -0
- package/lib/execution/executionBundle.js +29 -0
- package/lib/execution/executionDataConnector.d.ts +9 -0
- package/lib/execution/executionDataConnector.js +110 -0
- package/lib/index.d.ts +4 -15
- package/lib/index.js +7 -198
- package/lib/loader/loaderBase.d.ts +14 -0
- package/lib/loader/loaderBase.js +111 -0
- package/lib/types.d.ts +39 -0
- package/lib/utils/blockFinder.d.ts +8 -0
- package/lib/utils/blockFinder.js +36 -0
- package/lib/utils/consts.d.ts +4 -0
- package/lib/utils/consts.js +7 -0
- package/lib/utils/pluginManager.d.ts +11 -0
- package/lib/utils/pluginManager.js +84 -0
- package/lib/utils/utils.d.ts +16 -0
- package/lib/utils/utils.js +146 -0
- package/package.json +3 -3
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { WasmModule } from '@steerprotocol/app-loader';
|
|
2
|
+
import { EtherRpcConfig, ExecutionContext } from '../types';
|
|
3
|
+
import { providers } from 'ethers';
|
|
4
|
+
export declare class BundleBase {
|
|
5
|
+
protected module?: WasmModule;
|
|
6
|
+
protected wasmFile: ArrayBuffer | string;
|
|
7
|
+
protected config: string;
|
|
8
|
+
protected configObj: {
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
};
|
|
11
|
+
protected etherRpcConfig?: EtherRpcConfig;
|
|
12
|
+
protected providerIdentity?: providers.Provider;
|
|
13
|
+
executionContext?: ExecutionContext & {
|
|
14
|
+
epochTimestamp: number;
|
|
15
|
+
};
|
|
16
|
+
constructor(wasmFile: string | ArrayBuffer, _config: string, identity?: providers.Provider, etherRpcConfig?: EtherRpcConfig);
|
|
17
|
+
load(): Promise<WasmModule>;
|
|
18
|
+
loadSync(): WasmModule;
|
|
19
|
+
execute(response: string): Promise<string>;
|
|
20
|
+
reset(): void;
|
|
21
|
+
setExecutionContext(executionContext: ExecutionContext): void;
|
|
22
|
+
protected executeModule(response: string): Promise<string>;
|
|
23
|
+
getProvider(config: any): providers.Provider | undefined;
|
|
24
|
+
getResults(): string;
|
|
25
|
+
version(): number;
|
|
26
|
+
private subtractDate;
|
|
27
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
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.BundleBase = void 0;
|
|
13
|
+
const app_loader_1 = require("@steerprotocol/app-loader");
|
|
14
|
+
const ethers_1 = require("ethers");
|
|
15
|
+
class BundleBase {
|
|
16
|
+
constructor(wasmFile, _config, identity, etherRpcConfig) {
|
|
17
|
+
this.wasmFile = wasmFile;
|
|
18
|
+
this.config = _config;
|
|
19
|
+
this.configObj = JSON.parse(_config);
|
|
20
|
+
this.providerIdentity = identity;
|
|
21
|
+
this.etherRpcConfig = etherRpcConfig;
|
|
22
|
+
}
|
|
23
|
+
load() {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
const instance = yield (0, app_loader_1.loadWasm)(this.wasmFile, {});
|
|
26
|
+
this.module = instance;
|
|
27
|
+
this.module.initialize(this.config);
|
|
28
|
+
return this.module;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
loadSync() {
|
|
32
|
+
if (typeof this.wasmFile === 'string')
|
|
33
|
+
throw new Error('Cannot load module synchronously unless wasmFile is of type ArrayBuffer');
|
|
34
|
+
this.module = (0, app_loader_1.loadWasmSync)(this.wasmFile, {});
|
|
35
|
+
return this.module;
|
|
36
|
+
}
|
|
37
|
+
execute(response) {
|
|
38
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
if (!this.executionContext) {
|
|
40
|
+
throw new Error('Execution context missing');
|
|
41
|
+
}
|
|
42
|
+
// If the module is not initialized, try to load it
|
|
43
|
+
if (!this.module) {
|
|
44
|
+
try {
|
|
45
|
+
yield this.load();
|
|
46
|
+
}
|
|
47
|
+
catch (_a) {
|
|
48
|
+
throw new Error('Module not initialized');
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// If the module is still not initialized, throw an error
|
|
52
|
+
if (!this.module)
|
|
53
|
+
throw new Error('Module not initialized');
|
|
54
|
+
// Call the main function with the response
|
|
55
|
+
return yield this.module.execute(response);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
reset() {
|
|
59
|
+
if (this.module && this.module['reset']) {
|
|
60
|
+
this.module.reset();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
setExecutionContext(executionContext) {
|
|
64
|
+
var _a;
|
|
65
|
+
// Determine the timestamp for epoch
|
|
66
|
+
const timestamp = this.subtractDate(executionContext.executionTimestamp, Number(executionContext.epochLength)).getTime();
|
|
67
|
+
// Divide milliseconds data to seconds for transformation bundle
|
|
68
|
+
const timestampInSecs = Math.floor(timestamp / 1000);
|
|
69
|
+
this.executionContext = Object.assign(Object.assign({}, executionContext), { epochTimestamp: timestampInSecs });
|
|
70
|
+
(_a = this.module) === null || _a === void 0 ? void 0 : _a.initialize(JSON.stringify(Object.assign(Object.assign({}, JSON.parse(this.config)), { executionContext: this.executionContext })));
|
|
71
|
+
}
|
|
72
|
+
executeModule(response) {
|
|
73
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
74
|
+
if (!this.module) {
|
|
75
|
+
throw new Error('Module not initialized');
|
|
76
|
+
}
|
|
77
|
+
return yield this.module.execute(response);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
// This function returns a provider based on the chainId of the request
|
|
81
|
+
// The chainId is first checked in the data connector config, and if not found
|
|
82
|
+
// it is checked in the request config. If the chainId is found in either of
|
|
83
|
+
// these places, the provider is returned from the etherRpcConfig object
|
|
84
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
85
|
+
getProvider(config) {
|
|
86
|
+
let chainId;
|
|
87
|
+
// check if chainId is configured in data connector config
|
|
88
|
+
if (this.configObj.chainId) {
|
|
89
|
+
chainId = this.configObj.chainId;
|
|
90
|
+
}
|
|
91
|
+
else if (config && config.chainId) {
|
|
92
|
+
// if chainId is in the request config from dataconnector execution
|
|
93
|
+
chainId = config.chainId;
|
|
94
|
+
}
|
|
95
|
+
if (chainId && this.etherRpcConfig && this.etherRpcConfig[chainId]) {
|
|
96
|
+
const provider = new ethers_1.providers.JsonRpcProvider(this.etherRpcConfig[chainId].url);
|
|
97
|
+
return provider;
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
return this.providerIdentity;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
getResults() {
|
|
104
|
+
if (!this.module)
|
|
105
|
+
throw new Error('Module not initialized');
|
|
106
|
+
return this.module.transform();
|
|
107
|
+
}
|
|
108
|
+
version() {
|
|
109
|
+
if (!this.module)
|
|
110
|
+
throw new Error('Module not initialized');
|
|
111
|
+
return Number(this.module.version());
|
|
112
|
+
}
|
|
113
|
+
subtractDate(timestamp, epochLength) {
|
|
114
|
+
const date = new Date(timestamp);
|
|
115
|
+
date.setSeconds(date.getSeconds() - epochLength);
|
|
116
|
+
return date;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
exports.BundleBase = BundleBase;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
import { DataConnector } from '../bundleDataConnector';
|
|
3
|
+
import { VaultConfigPayload, ExecutionContext } from '../types';
|
|
4
|
+
export declare class ExecutionBase {
|
|
5
|
+
maxRetries: number;
|
|
6
|
+
constructor(maxRetries?: number);
|
|
7
|
+
protected delay(ms: number): Promise<void>;
|
|
8
|
+
protected log(...args: any[]): void;
|
|
9
|
+
protected getExecutionContext(dataConnector: DataConnector, vaultConfig: VaultConfigPayload): Promise<ExecutionContext>;
|
|
10
|
+
execute<T>(_vaultConfig: string): Promise<Array<T>>;
|
|
11
|
+
private getDataConnectorVersion;
|
|
12
|
+
getAverageBlockTime(provider: ethers.providers.Provider, blockCount?: number): Promise<number>;
|
|
13
|
+
getBlockNumberFromAvgBlockTime(provider: ethers.providers.Provider, targetTimestamp: number, avgBlockTime: number): Promise<number>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
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;
|
|
@@ -0,0 +1,6 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
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;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { WasmModule } from '@steerprotocol/app-loader';
|
|
2
|
+
import { providers } from 'ethers';
|
|
3
|
+
import { EtherRpcConfig, ExecutionContext } from '../types';
|
|
4
|
+
export declare class BundleBase {
|
|
5
|
+
module?: WasmModule;
|
|
6
|
+
protected wasmFile: ArrayBuffer | string;
|
|
7
|
+
protected config: string;
|
|
8
|
+
configObj: {
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
};
|
|
11
|
+
protected etherRpcConfig?: EtherRpcConfig;
|
|
12
|
+
protected providerIdentity?: providers.Provider;
|
|
13
|
+
executionContext?: ExecutionContext & {
|
|
14
|
+
epochTimestamp: number;
|
|
15
|
+
};
|
|
16
|
+
protected scheduleEpochTimestamp: number | undefined;
|
|
17
|
+
constructor(wasmFile: string | ArrayBuffer, _config: string, identity?: providers.Provider, etherRpcConfig?: EtherRpcConfig);
|
|
18
|
+
load(): Promise<WasmModule>;
|
|
19
|
+
loadSync(): WasmModule;
|
|
20
|
+
execute(response: string): Promise<string>;
|
|
21
|
+
executeBundle(data: Array<any>): Promise<string>;
|
|
22
|
+
setExecutionContextStrategyExecution(executionContext: ExecutionContext): void;
|
|
23
|
+
setExecutionContextDataConnector(executionContext: ExecutionContext): void;
|
|
24
|
+
protected executeModule(response: string): Promise<string>;
|
|
25
|
+
getProvider(config: any): providers.Provider | undefined;
|
|
26
|
+
getResults(): string;
|
|
27
|
+
version(): number;
|
|
28
|
+
private subtractDate;
|
|
29
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
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.BundleBase = void 0;
|
|
13
|
+
const app_loader_1 = require("@steerprotocol/app-loader");
|
|
14
|
+
const ethers_1 = require("ethers");
|
|
15
|
+
class BundleBase {
|
|
16
|
+
constructor(wasmFile, _config, identity, etherRpcConfig) {
|
|
17
|
+
this.wasmFile = wasmFile;
|
|
18
|
+
this.config = _config;
|
|
19
|
+
this.configObj = JSON.parse(_config);
|
|
20
|
+
this.providerIdentity = identity;
|
|
21
|
+
this.etherRpcConfig = etherRpcConfig;
|
|
22
|
+
}
|
|
23
|
+
load() {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
const instance = yield (0, app_loader_1.loadWasm)(this.wasmFile, {});
|
|
26
|
+
this.module = instance;
|
|
27
|
+
this.module.initialize(this.config);
|
|
28
|
+
return this.module;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
loadSync() {
|
|
32
|
+
if (typeof this.wasmFile === 'string')
|
|
33
|
+
throw new Error('Cannot load module synchronously unless wasmFile is of type ArrayBuffer');
|
|
34
|
+
this.module = (0, app_loader_1.loadWasmSync)(this.wasmFile, {});
|
|
35
|
+
return this.module;
|
|
36
|
+
}
|
|
37
|
+
execute(response) {
|
|
38
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
if (!this.executionContext) {
|
|
40
|
+
throw new Error('Execution context missing');
|
|
41
|
+
}
|
|
42
|
+
// If the module is not initialized, try to load it
|
|
43
|
+
if (!this.module) {
|
|
44
|
+
try {
|
|
45
|
+
yield this.load();
|
|
46
|
+
}
|
|
47
|
+
catch (_a) {
|
|
48
|
+
throw new Error('Module not initialized');
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// If the module is still not initialized, throw an error
|
|
52
|
+
if (!this.module)
|
|
53
|
+
throw new Error('Module not initialized');
|
|
54
|
+
// Call the main function with the response
|
|
55
|
+
return yield this.module.execute(response);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
executeBundle(data) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
if (!this.executionContext) {
|
|
61
|
+
throw new Error('Execution context missing');
|
|
62
|
+
}
|
|
63
|
+
// If the module is not initialized, try to load it
|
|
64
|
+
if (!this.module) {
|
|
65
|
+
try {
|
|
66
|
+
yield this.load();
|
|
67
|
+
}
|
|
68
|
+
catch (_a) {
|
|
69
|
+
throw new Error('Module not initialized');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// If the module is still not initialized, throw an error
|
|
73
|
+
if (!this.module)
|
|
74
|
+
throw new Error('Module not initialized');
|
|
75
|
+
// Call the main function with the response
|
|
76
|
+
return this.module.execute(...data);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
setExecutionContextStrategyExecution(executionContext) {
|
|
80
|
+
var _a;
|
|
81
|
+
// Determine the timestamp for epoch
|
|
82
|
+
const timestamp = this.subtractDate(executionContext.executionTimestamp, Number(executionContext.epochLength)).getTime();
|
|
83
|
+
// Divide milliseconds data to seconds for transformation bundle
|
|
84
|
+
const timestampInSecs = Math.floor(timestamp / 1000);
|
|
85
|
+
this.executionContext = Object.assign(Object.assign({}, executionContext), { epochTimestamp: timestampInSecs });
|
|
86
|
+
const completeConfig = Object.assign(Object.assign({}, JSON.parse(this.config)), this.executionContext);
|
|
87
|
+
this.scheduleEpochTimestamp = executionContext.epochTimestamp;
|
|
88
|
+
console.log('Complete execution config : ', completeConfig);
|
|
89
|
+
(_a = this.module) === null || _a === void 0 ? void 0 : _a.initialize(JSON.stringify(completeConfig));
|
|
90
|
+
}
|
|
91
|
+
setExecutionContextDataConnector(executionContext) {
|
|
92
|
+
var _a;
|
|
93
|
+
// Determine the timestamp for epoch
|
|
94
|
+
const timestamp = this.subtractDate(executionContext.executionTimestamp, Number(executionContext.epochLength)).getTime();
|
|
95
|
+
// Divide milliseconds data to seconds for transformation bundle
|
|
96
|
+
const timestampInSecs = Math.floor(timestamp / 1000);
|
|
97
|
+
this.executionContext = Object.assign(Object.assign({}, executionContext), { epochTimestamp: timestampInSecs });
|
|
98
|
+
const completeConfig = Object.assign(Object.assign({}, JSON.parse(this.config)), { executionContext: this.executionContext });
|
|
99
|
+
this.scheduleEpochTimestamp = executionContext.epochTimestamp;
|
|
100
|
+
console.log('Complete execution config : ', completeConfig);
|
|
101
|
+
(_a = this.module) === null || _a === void 0 ? void 0 : _a.initialize(JSON.stringify(completeConfig));
|
|
102
|
+
}
|
|
103
|
+
executeModule(response) {
|
|
104
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
105
|
+
if (!this.module) {
|
|
106
|
+
throw new Error('Module not initialized');
|
|
107
|
+
}
|
|
108
|
+
return yield this.module.execute(response);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
// This function returns a provider based on the chainId of the request
|
|
112
|
+
// The chainId is first checked in the data connector config, and if not found
|
|
113
|
+
// it is checked in the request config. If the chainId is found in either of
|
|
114
|
+
// these places, the provider is returned from the etherRpcConfig object
|
|
115
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
116
|
+
getProvider(config) {
|
|
117
|
+
let chainId;
|
|
118
|
+
// check if chainId is configured in data connector config
|
|
119
|
+
if (this.configObj.chainId) {
|
|
120
|
+
chainId = this.configObj.chainId;
|
|
121
|
+
}
|
|
122
|
+
else if (config && config.chainId) {
|
|
123
|
+
// if chainId is in the request config from dataconnector execution
|
|
124
|
+
chainId = config.chainId;
|
|
125
|
+
}
|
|
126
|
+
if (chainId && this.etherRpcConfig && this.etherRpcConfig[chainId]) {
|
|
127
|
+
const provider = new ethers_1.providers.JsonRpcProvider(this.etherRpcConfig[chainId].url);
|
|
128
|
+
return provider;
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
return this.providerIdentity;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
getResults() {
|
|
135
|
+
if (!this.module)
|
|
136
|
+
throw new Error('Module not initialized');
|
|
137
|
+
return this.module.transform();
|
|
138
|
+
}
|
|
139
|
+
version() {
|
|
140
|
+
if (!this.module)
|
|
141
|
+
throw new Error('Module not initialized');
|
|
142
|
+
return Number(this.module.version());
|
|
143
|
+
}
|
|
144
|
+
subtractDate(timestamp, epochLength) {
|
|
145
|
+
const date = new Date(timestamp);
|
|
146
|
+
date.setSeconds(date.getSeconds() - epochLength);
|
|
147
|
+
return date;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
exports.BundleBase = BundleBase;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { providers } from 'ethers';
|
|
2
|
+
import { EtherRpcConfig, ViewConfig } from '../types';
|
|
3
|
+
import { BundleBase } from './bundleBase';
|
|
4
|
+
export declare class DataConnector extends BundleBase {
|
|
5
|
+
constructor(wasmFile: string | ArrayBuffer, _config: string, identity?: providers.Provider, etherRpcConfig?: EtherRpcConfig);
|
|
6
|
+
getRpcUrl(config: any): string | undefined;
|
|
7
|
+
view(config: ViewConfig): Promise<string>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
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 consts_1 = require("../utils/consts");
|
|
15
|
+
const utils_1 = require("../utils/utils");
|
|
16
|
+
const bundleBase_1 = require("./bundleBase");
|
|
17
|
+
class DataConnector extends bundleBase_1.BundleBase {
|
|
18
|
+
// Identity should come from the node's IdentityManager.wallet
|
|
19
|
+
constructor(wasmFile, _config, identity, etherRpcConfig) {
|
|
20
|
+
super(wasmFile, _config, identity, etherRpcConfig);
|
|
21
|
+
}
|
|
22
|
+
// get rpc url from configuration
|
|
23
|
+
getRpcUrl(config) {
|
|
24
|
+
let chainId;
|
|
25
|
+
// check if chainId is configured in data connector config
|
|
26
|
+
if (this.configObj.chainId) {
|
|
27
|
+
chainId = this.configObj.chainId;
|
|
28
|
+
}
|
|
29
|
+
else if (config && config.chainId) {
|
|
30
|
+
// if chainId is in the request config from dataconnector execution
|
|
31
|
+
chainId = config.chainId;
|
|
32
|
+
}
|
|
33
|
+
if (chainId && this.etherRpcConfig && this.etherRpcConfig[chainId]) {
|
|
34
|
+
return this.etherRpcConfig[chainId].url;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// Makes contract instance and calls, returns stringified result
|
|
41
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
42
|
+
view(config) {
|
|
43
|
+
var _a;
|
|
44
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
const provider = this.getProvider(config);
|
|
46
|
+
if (!provider)
|
|
47
|
+
throw new Error('Provider not initialized');
|
|
48
|
+
const contract = new ethers_1.Contract(config.address, config.abi, provider);
|
|
49
|
+
// For formatting the parameters if needed
|
|
50
|
+
const paramsArray = config.arguments;
|
|
51
|
+
let result;
|
|
52
|
+
if (consts_1.CENTRALIZE_CONTRACT_INTERACTION_CHAINS.indexOf(this.configObj.chainId.toString()) === -1) {
|
|
53
|
+
result = yield contract[config.method](...paramsArray, {
|
|
54
|
+
blockTag: (_a = this.executionContext) === null || _a === void 0 ? void 0 : _a.blockNumber,
|
|
55
|
+
}); //@note uncomment for deterministic blocks
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
const viewContractConfig = {
|
|
59
|
+
epochTimestamp: this.scheduleEpochTimestamp,
|
|
60
|
+
address: this.configObj.address || config.address,
|
|
61
|
+
abi: config.abi,
|
|
62
|
+
arguments: paramsArray,
|
|
63
|
+
method: config.method,
|
|
64
|
+
rpcUrl: this.getRpcUrl(config),
|
|
65
|
+
};
|
|
66
|
+
result = yield (0, utils_1.callWithRetry)(viewContractConfig);
|
|
67
|
+
}
|
|
68
|
+
// This gets stringified before calling main
|
|
69
|
+
return result;
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.DataConnector = DataConnector;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { providers } from 'ethers';
|
|
2
|
+
import { BundleBase } from './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
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
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;
|
|
@@ -0,0 +1,7 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
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;
|