@steerprotocol/data-connector-loader 0.0.15 → 0.0.17

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.
@@ -56,7 +56,7 @@ class DataConnector {
56
56
  if (!this.module)
57
57
  throw new Error('Module not initialized');
58
58
  // Call the main function with the response
59
- return this.module['execute(param_1: string)'](response);
59
+ return this.module.execute(response);
60
60
  });
61
61
  }
62
62
  // This function returns a provider based on the chainId of the request
@@ -109,7 +109,7 @@ class DataConnector {
109
109
  version() {
110
110
  if (!this.module)
111
111
  throw new Error('Module not initialized');
112
- return this.module.version();
112
+ return Number(this.module.version());
113
113
  }
114
114
  subtractDate(timestamp, epochLength) {
115
115
  const date = new Date(timestamp);
package/lib/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { DataConnector } from './dataConnector';
2
2
  import { ethers } from 'ethers';
3
- export { DataConnector };
4
3
  export declare class DataConnectorExecution {
5
4
  private dataConnectors;
6
5
  constructor(dataConnectors: Array<DataConnector>);
package/lib/index.js CHANGED
@@ -12,11 +12,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.DataConnectorExecution = exports.DataConnector = void 0;
16
- const dataConnector_1 = require("./dataConnector");
17
- Object.defineProperty(exports, "DataConnector", { enumerable: true, get: function () { return dataConnector_1.DataConnector; } });
15
+ exports.DataConnectorExecution = void 0;
18
16
  const axios_1 = __importDefault(require("axios"));
19
17
  const pluginManager_1 = require("./pluginManager");
18
+ const utils_1 = require("./utils");
20
19
  class DataConnectorExecution {
21
20
  constructor(dataConnectors) {
22
21
  this.dataConnectors = dataConnectors;
@@ -65,6 +64,7 @@ class DataConnectorExecution {
65
64
  return delay;
66
65
  }
67
66
  execute(_vaultConfig) {
67
+ var _a, _b, _c;
68
68
  return __awaiter(this, void 0, void 0, function* () {
69
69
  const executionTimestamp = new Date().getTime();
70
70
  const vaultConfig = JSON.parse(_vaultConfig);
@@ -91,6 +91,7 @@ class DataConnectorExecution {
91
91
  blockTime,
92
92
  blockNumber,
93
93
  });
94
+ const version = yield dataConnector.version();
94
95
  let request = yield dataConnector.execute('');
95
96
  let response;
96
97
  try {
@@ -115,7 +116,21 @@ class DataConnectorExecution {
115
116
  throw e;
116
117
  }
117
118
  }
118
- const transformedData = JSON.parse(dataConnector.getResults());
119
+ let transformedData = JSON.parse(dataConnector.getResults());
120
+ if (version == 1 && transformedData.length > 0) {
121
+ const keys = Object.keys(transformedData[0]);
122
+ const requiredKeys = ['open', 'high', 'low', 'close', 'volume'];
123
+ const hasAllKeys = requiredKeys.every((key) => keys.includes(key));
124
+ // has the 'lookback' key
125
+ const lookback = (_a = dataConnector === null || dataConnector === void 0 ? void 0 : dataConnector.configObj) === null || _a === void 0 ? void 0 : _a.lookback;
126
+ if (hasAllKeys && !!lookback) {
127
+ // Now that we have identified a v1 data connector, we need to transform the data so that the data is up to the current execution epoch timestamp
128
+ if ((_b = dataConnector === null || dataConnector === void 0 ? void 0 : dataConnector.executionContext) === null || _b === void 0 ? void 0 : _b.epochTimestamp) {
129
+ const extended = (0, utils_1.extendOHLCV)(transformedData, lookback, ((_c = dataConnector === null || dataConnector === void 0 ? void 0 : dataConnector.executionContext) === null || _c === void 0 ? void 0 : _c.epochTimestamp) * 1000);
130
+ transformedData = extended;
131
+ }
132
+ }
133
+ }
119
134
  transformationData.push(transformedData);
120
135
  }
121
136
  return transformationData;
@@ -124,17 +139,28 @@ class DataConnectorExecution {
124
139
  getAverageBlockTime(provider, blockCount = 5) {
125
140
  return __awaiter(this, void 0, void 0, function* () {
126
141
  const latestBlock = yield provider.getBlock('latest');
127
- const startBlockNumber = Math.max(0, latestBlock.number - blockCount + 1);
128
- let totalTimeDiff = 0;
129
- let previousBlock = yield provider.getBlock(startBlockNumber);
130
- for (let i = startBlockNumber + 1; i <= latestBlock.number; i++) {
131
- const currentBlock = yield provider.getBlock(i);
132
- const timeDiff = currentBlock.timestamp - previousBlock.timestamp;
133
- totalTimeDiff += timeDiff;
134
- previousBlock = currentBlock;
142
+ if (latestBlock) {
143
+ const startBlockNumber = Math.max(0, latestBlock.number - blockCount + 1);
144
+ let totalTimeDiff = 0;
145
+ let previousBlock = yield provider.getBlock(startBlockNumber);
146
+ if (!previousBlock) {
147
+ return 0;
148
+ }
149
+ for (let i = startBlockNumber + 1; i <= latestBlock.number; i++) {
150
+ const currentBlock = yield provider.getBlock(i);
151
+ if (!currentBlock || !previousBlock || !currentBlock.timestamp || !previousBlock.timestamp) {
152
+ continue;
153
+ }
154
+ const timeDiff = currentBlock.timestamp - previousBlock.timestamp;
155
+ totalTimeDiff += timeDiff;
156
+ previousBlock = currentBlock;
157
+ }
158
+ const averageBlockTime = totalTimeDiff / (blockCount - 1);
159
+ return averageBlockTime;
160
+ }
161
+ else {
162
+ return 0;
135
163
  }
136
- const averageBlockTime = totalTimeDiff / (blockCount - 1);
137
- return averageBlockTime;
138
164
  });
139
165
  }
140
166
  getBlockNumberFromAvgBlockTime(provider, targetTimestamp, avgBlockTime) {
@@ -142,6 +168,12 @@ class DataConnectorExecution {
142
168
  const latestBlock = yield provider.getBlock('latest');
143
169
  const currentTime = Math.floor(Date.now() / 1000);
144
170
  const timeDifference = currentTime - targetTimestamp;
171
+ if (!latestBlock) {
172
+ return 0;
173
+ }
174
+ if (avgBlockTime === 0) {
175
+ return latestBlock.number;
176
+ }
145
177
  // Calculate the estimated number of blocks based on the average block time
146
178
  const estimatedBlockCount = Math.floor(timeDifference / avgBlockTime);
147
179
  // Calculate the estimated block number
package/lib/utils.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { Candle } from "@steerprotocol/app-loader";
2
+ export declare const extendOHLCV: (rawData: Candle[], period: number, endTimestamp: number) => Candle[];
package/lib/utils.js ADDED
@@ -0,0 +1,28 @@
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steerprotocol/data-connector-loader",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
4
4
  "description": "App Loader for Steer Protocol",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -46,10 +46,10 @@
46
46
  "lib/**/*"
47
47
  ],
48
48
  "dependencies": {
49
- "@steerprotocol/app-loader": "0.1.6",
50
- "@steerprotocol/strategy-utils": "^2.1.8",
49
+ "@steerprotocol/app-loader": "0.2.0",
50
+ "@steerprotocol/strategy-utils": "^2.1.0",
51
51
  "axios": "^1.3.4",
52
52
  "ethers": "^5.1.0",
53
- "pondjs": "1.0.0-alpha.10"
53
+ "timestring": "^7.0.0"
54
54
  }
55
55
  }