@steerprotocol/data-connector-loader 0.0.6 → 0.0.8
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/index.js +39 -35
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -22,7 +22,6 @@ class DataConnectorExecution {
|
|
|
22
22
|
delay(ms) {
|
|
23
23
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
24
24
|
}
|
|
25
|
-
// create function to perform console logs
|
|
26
25
|
log(...args) {
|
|
27
26
|
if (process.env.DEBUG === 'true') {
|
|
28
27
|
console.log(...args);
|
|
@@ -30,74 +29,79 @@ class DataConnectorExecution {
|
|
|
30
29
|
}
|
|
31
30
|
fetchData(request, attempts) {
|
|
32
31
|
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
-
// intentionally kept console logs to debug
|
|
34
32
|
this.log('Request payload: =============', request);
|
|
35
33
|
attempts = attempts + 1;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
try {
|
|
35
|
+
const response = (yield (0, axios_1.default)(request)).data;
|
|
36
|
+
this.log('API Response: =============', response);
|
|
37
|
+
if ('errors' in response && response['errors'].length > 0) {
|
|
38
|
+
if (attempts < 3) {
|
|
39
|
+
return yield this.fetchData(request, attempts);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
throw new Error('Maximum retry attempts reached');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return response;
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
40
48
|
if (attempts < 3) {
|
|
41
49
|
return yield this.fetchData(request, attempts);
|
|
42
50
|
}
|
|
43
51
|
else {
|
|
44
|
-
throw
|
|
52
|
+
throw error;
|
|
45
53
|
}
|
|
46
54
|
}
|
|
47
|
-
return response;
|
|
48
55
|
});
|
|
49
56
|
}
|
|
50
57
|
execute(vaultAddress, _vaultConfig) {
|
|
51
58
|
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
-
// get current timestamp
|
|
53
59
|
const executionTimestamp = new Date().getTime();
|
|
54
|
-
// get vault configuration
|
|
55
60
|
const vaultConfig = JSON.parse(_vaultConfig);
|
|
56
|
-
// get data connector configurations
|
|
57
61
|
const dataConnectorConfigs = vaultConfig.dataConnectorsData;
|
|
58
|
-
// Each data connector results will be pushed into single array
|
|
59
|
-
// they will passed to execution bundle
|
|
60
62
|
const transformationData = [];
|
|
61
63
|
const pluginManager = new pluginManager_1.PluginManager();
|
|
62
64
|
yield pluginManager.registerFromPublicConfig();
|
|
63
|
-
// Iterate over each data connector
|
|
64
65
|
for (const connectorConfig of dataConnectorConfigs) {
|
|
65
|
-
// If bundle hash is not present, skip the data connector
|
|
66
66
|
if (!connectorConfig.bundleHash) {
|
|
67
67
|
continue;
|
|
68
68
|
}
|
|
69
|
-
this.dataConnector.setExecutionContext({
|
|
70
|
-
executionTimestamp,
|
|
71
|
-
epochLength: Number(vaultConfig.strategyConfigData.epochLength)
|
|
72
|
-
});
|
|
73
|
-
// Append vault address to config
|
|
74
69
|
if (vaultAddress) {
|
|
75
70
|
connectorConfig.configData.vaultAddress = vaultAddress;
|
|
76
71
|
}
|
|
77
|
-
// loading data connector
|
|
78
72
|
if (!this.dataConnector.module) {
|
|
79
73
|
yield this.dataConnector.load();
|
|
80
74
|
}
|
|
81
|
-
|
|
75
|
+
this.dataConnector.setExecutionContext({
|
|
76
|
+
executionTimestamp,
|
|
77
|
+
epochLength: Number(vaultConfig.strategyConfigData.epochLength)
|
|
78
|
+
});
|
|
82
79
|
let request = yield this.dataConnector.execute('');
|
|
83
80
|
let response;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
81
|
+
try {
|
|
82
|
+
while (request !== 'true') {
|
|
83
|
+
const parsedRequest = yield pluginManager.process(typeof request == 'string' ? JSON.parse(request) : request);
|
|
84
|
+
if (this.dataConnector.configObj.isChainRead == true) {
|
|
85
|
+
response = yield this.dataConnector.view(parsedRequest);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
yield this.delay(1000);
|
|
89
|
+
response = yield this.fetchData(parsedRequest, 0);
|
|
90
|
+
}
|
|
91
|
+
request = yield this.dataConnector.execute(JSON.stringify(response));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
catch (e) {
|
|
95
|
+
// process.env.SILENT_ERRORS
|
|
96
|
+
if (process.env.DATA_LOADER_SILENT_ERRORS !== 'true') {
|
|
97
|
+
console.error('Data connector failed to process.', e);
|
|
89
98
|
}
|
|
90
99
|
else {
|
|
91
|
-
|
|
92
|
-
// add a delay on the request
|
|
93
|
-
yield this.delay(1000);
|
|
94
|
-
response = yield this.fetchData(parsedRequest, 0);
|
|
100
|
+
throw e;
|
|
95
101
|
}
|
|
96
|
-
// break;
|
|
97
|
-
request = yield this.dataConnector.execute(JSON.stringify(response));
|
|
98
102
|
}
|
|
99
|
-
const
|
|
100
|
-
transformationData.push(
|
|
103
|
+
const transformedData = JSON.parse(this.dataConnector.getResults());
|
|
104
|
+
transformationData.push(transformedData);
|
|
101
105
|
}
|
|
102
106
|
return transformationData;
|
|
103
107
|
});
|