@steerprotocol/data-connector-loader 0.2.0 → 1.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 (54) 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/executionBase.js +9 -2
  25. package/lib/execution/executionDataConnector.d.ts +2 -2
  26. package/lib/execution/executionDataConnector.js +22 -4
  27. package/lib/index.d.ts +1 -0
  28. package/lib/index.js +15 -0
  29. package/lib/types.d.ts +2 -1
  30. package/lib/utils/consts.js +2 -2
  31. package/lib/utils/utils.d.ts +5 -5
  32. package/lib/utils/utils.js +5 -5
  33. package/package.json +35 -5
  34. package/lib/base/bundleBase.js +0 -119
  35. package/lib/base/loaderBase.d.ts +0 -14
  36. package/lib/base/loaderBase.js +0 -111
  37. package/lib/blockFinder.js +0 -36
  38. package/lib/bundle/bundleApp.d.ts +0 -6
  39. package/lib/bundle/bundleApp.js +0 -11
  40. package/lib/bundle/bundleExecution.js +0 -42
  41. package/lib/bundleDataConnector.d.ts +0 -7
  42. package/lib/bundleDataConnector.js +0 -42
  43. package/lib/bundleExecution.d.ts +0 -7
  44. package/lib/bundleExecution.js +0 -42
  45. package/lib/dataConnector.d.ts +0 -27
  46. package/lib/dataConnector.js +0 -133
  47. package/lib/execution/executionBundle.d.ts +0 -5
  48. package/lib/execution/executionBundle.js +0 -29
  49. package/lib/loader/loaderBase.js +0 -111
  50. package/lib/pluginManager.js +0 -84
  51. package/lib/utils.d.ts +0 -2
  52. package/lib/utils.js +0 -28
  53. /package/lib/{blockFinder.d.ts → esm/utils/blockFinder.d.ts} +0 -0
  54. /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,84 +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
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.PluginManager = void 0;
16
- const axios_1 = __importDefault(require("axios"));
17
- class PluginManager {
18
- constructor() {
19
- this.registeredPluginConfigs = [];
20
- }
21
- registerFromPublicConfig() {
22
- return __awaiter(this, void 0, void 0, function* () {
23
- const { data: dataConnectorPlugins } = yield axios_1.default.get('https://ro81h8hq6b.execute-api.us-east-1.amazonaws.com/data-connector-plugins');
24
- for (const plugin of dataConnectorPlugins) {
25
- this.registerPluginConfig(plugin.name, plugin.domain, plugin.headers);
26
- }
27
- });
28
- }
29
- registerPluginConfig(name, domain, headers) {
30
- this.registeredPluginConfigs.push({ name, domain, headers });
31
- }
32
- findMatchingPluginConfigs(url) {
33
- const matchingHeaders = {};
34
- for (const pluginConfig of this.registeredPluginConfigs) {
35
- if (url.includes(pluginConfig.domain)) {
36
- Object.assign(matchingHeaders, pluginConfig.headers);
37
- }
38
- }
39
- return matchingHeaders;
40
- }
41
- interpolateEnvVars(str) {
42
- return str.replace(/\{\{(\w+)\}\}/g, (_match, envVar) => {
43
- const envValue = process.env[envVar];
44
- if (envValue === undefined) {
45
- console.log(`Environment variable '${envVar}' not found.`);
46
- return '';
47
- }
48
- return envValue;
49
- });
50
- }
51
- interpolateHeaders(headers) {
52
- const interpolatedHeaders = {};
53
- for (const [key, value] of Object.entries(headers)) {
54
- interpolatedHeaders[key] = typeof value === 'string' ? this.interpolateEnvVars(value) : value;
55
- }
56
- return interpolatedHeaders;
57
- }
58
- process(config) {
59
- return __awaiter(this, void 0, void 0, function* () {
60
- if (!config.url) {
61
- throw new Error('Request URL is not provided');
62
- }
63
- const matchingHeaders = this.findMatchingPluginConfigs(config.url);
64
- const interpolatedHeaders = this.interpolateHeaders(matchingHeaders);
65
- const updatedHeaders = Object.assign(Object.assign({}, config.headers), interpolatedHeaders);
66
- const updatedConfig = Object.assign(Object.assign({}, config), { headers: updatedHeaders });
67
- return updatedConfig;
68
- });
69
- }
70
- }
71
- exports.PluginManager = PluginManager;
72
- // // Usage example:
73
- // (async () => {
74
- // const headerMerger = new PluginManager();
75
- // headerMerger.registerPluginConfig('Example API', 'example.com', { 'X-Custom-Header': '{{MY_ENV_VAR}}' });
76
- // process.env['MY_ENV_VAR'] = 'ExampleValue';
77
- // const requestConfig: AxiosRequestConfig = {
78
- // url: 'https://example.com/api/some-resource',
79
- // method: 'GET',
80
- // };
81
- // const updatedRequestConfig = await headerMerger.process(requestConfig);
82
- // const response = await axios(updatedRequestConfig);
83
- // console.log(response.data);
84
- // })();
package/lib/utils.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { Candle } from "@steerprotocol/app-loader";
2
- export declare const extendOHLCV: (rawData: Candle[], period: number, endTimestamp: number) => Candle[];
package/lib/utils.js DELETED
@@ -1,28 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.extendOHLCV = void 0;
4
- // The purpose of this function is to look at the end timestamp and extend the current candle array so that it backfills any missing candles up to the end timestamp
5
- const extendOHLCV = (rawData, period, endTimestamp) => {
6
- // Sort rawData by timestamp
7
- const sortedData = rawData.sort((a, b) => a.timestamp - b.timestamp);
8
- const lastCandle = sortedData[sortedData.length - 1];
9
- const startTimestamp = lastCandle ? lastCandle.timestamp : endTimestamp;
10
- // Calculate the number of periods to backfill
11
- const periodsToBackfill = Math.floor(((endTimestamp - startTimestamp)) / 1000 / period);
12
- // Backfill the candles
13
- for (let i = 0; i < periodsToBackfill; i++) {
14
- const candleWidth = (period * (i + 1));
15
- const newCandle = {
16
- timestamp: startTimestamp + candleWidth,
17
- open: lastCandle.open,
18
- high: lastCandle.open,
19
- low: lastCandle.open,
20
- close: lastCandle.open,
21
- volume: 0, // You may need to replace these with appropriate logic
22
- };
23
- sortedData.push(newCandle);
24
- }
25
- // Return the converted data
26
- return sortedData;
27
- };
28
- exports.extendOHLCV = extendOHLCV;