@steerprotocol/data-connector-loader 0.1.5 → 0.2.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 +92 -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 +1 -1
|
@@ -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;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ethers } from "ethers";
|
|
2
|
+
import { EtherRpcConfig } from "../types";
|
|
3
|
+
import { ExecutionBase } from "./executionBase";
|
|
4
|
+
export declare class ExecutionApp extends ExecutionBase {
|
|
5
|
+
steerSubgraph: string;
|
|
6
|
+
vault: string;
|
|
7
|
+
isAlgebraPool: boolean;
|
|
8
|
+
isDirectionalPool: boolean;
|
|
9
|
+
chainId: number;
|
|
10
|
+
constructor(vault: string, steerSubgraph: string, isAlgebraPool: boolean, isDirectionalPool: boolean, chainId: number, maxRetries?: number);
|
|
11
|
+
execute<T>(_wasmFile: string | ArrayBuffer, _vaultConfig: string, _data: Array<any>, _identity?: ethers.providers.Provider, _etherRpcConfig?: EtherRpcConfig): Promise<Array<T>>;
|
|
12
|
+
private getSmartPoolExecutionContext;
|
|
13
|
+
private getPoolCurrentTick;
|
|
14
|
+
private getVaultPoolAddress;
|
|
15
|
+
private getSlot0;
|
|
16
|
+
private getGlobalState;
|
|
17
|
+
}
|
|
@@ -0,0 +1,325 @@
|
|
|
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.ExecutionApp = void 0;
|
|
13
|
+
const bundleDataConnector_1 = require("../bundle/bundleDataConnector");
|
|
14
|
+
const utils_1 = require("../utils/utils");
|
|
15
|
+
const executionBase_1 = require("./executionBase");
|
|
16
|
+
class ExecutionApp extends executionBase_1.ExecutionBase {
|
|
17
|
+
constructor(vault, steerSubgraph, isAlgebraPool, isDirectionalPool, chainId, maxRetries = 3) {
|
|
18
|
+
super(maxRetries);
|
|
19
|
+
this.getSlot0 = (pool, currentExecutionContext, provider, rpc) => __awaiter(this, void 0, void 0, function* () {
|
|
20
|
+
const slot0Abi = [
|
|
21
|
+
{
|
|
22
|
+
inputs: [],
|
|
23
|
+
name: "slot0",
|
|
24
|
+
outputs: [
|
|
25
|
+
{ internalType: "uint160", name: "sqrtPriceX96", type: "uint160" },
|
|
26
|
+
{ internalType: "int24", name: "tick", type: "int24" },
|
|
27
|
+
{ internalType: "uint16", name: "observationIndex", type: "uint16" },
|
|
28
|
+
{
|
|
29
|
+
internalType: "uint16",
|
|
30
|
+
name: "observationCardinality",
|
|
31
|
+
type: "uint16",
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
internalType: "uint16",
|
|
35
|
+
name: "observationCardinalityNext",
|
|
36
|
+
type: "uint16",
|
|
37
|
+
},
|
|
38
|
+
{ internalType: "uint8", name: "feeProtocol", type: "uint8" },
|
|
39
|
+
{ internalType: "bool", name: "unlocked", type: "bool" },
|
|
40
|
+
],
|
|
41
|
+
stateMutability: "view",
|
|
42
|
+
type: "function",
|
|
43
|
+
},
|
|
44
|
+
];
|
|
45
|
+
const slot0 = yield (0, utils_1.readSlot0Contract)({
|
|
46
|
+
address: pool,
|
|
47
|
+
abi: slot0Abi,
|
|
48
|
+
currentExecutionContext,
|
|
49
|
+
functionName: "slot0",
|
|
50
|
+
provider: provider,
|
|
51
|
+
rpc,
|
|
52
|
+
chainId: this.chainId
|
|
53
|
+
});
|
|
54
|
+
return {
|
|
55
|
+
sqrtPriceX96: slot0[0],
|
|
56
|
+
tick: slot0[1],
|
|
57
|
+
observationIndex: slot0[2],
|
|
58
|
+
observationCardinality: slot0[3],
|
|
59
|
+
observationCardinalityNext: slot0[4],
|
|
60
|
+
feeProtocol: slot0[5],
|
|
61
|
+
unlocked: slot0[6],
|
|
62
|
+
};
|
|
63
|
+
});
|
|
64
|
+
this.getGlobalState = (pool, currentExecutionContext, provider, rpc) => __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
const slot0Abi = [
|
|
66
|
+
!this.isDirectionalPool
|
|
67
|
+
? {
|
|
68
|
+
inputs: [],
|
|
69
|
+
name: "globalState",
|
|
70
|
+
outputs: [
|
|
71
|
+
{
|
|
72
|
+
internalType: "uint160",
|
|
73
|
+
name: "price",
|
|
74
|
+
type: "uint160",
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
internalType: "int24",
|
|
78
|
+
name: "tick",
|
|
79
|
+
type: "int24",
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
internalType: "int24",
|
|
83
|
+
name: "prevInitializedTick",
|
|
84
|
+
type: "int24",
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
internalType: "uint16",
|
|
88
|
+
name: "fee",
|
|
89
|
+
type: "uint16",
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
internalType: "uint16",
|
|
93
|
+
name: "timepointIndex",
|
|
94
|
+
type: "uint16",
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
internalType: "uint8",
|
|
98
|
+
name: "communityFee",
|
|
99
|
+
type: "uint8",
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
internalType: "bool",
|
|
103
|
+
name: "unlocked",
|
|
104
|
+
type: "bool",
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
stateMutability: "view",
|
|
108
|
+
type: "function",
|
|
109
|
+
}
|
|
110
|
+
: {
|
|
111
|
+
inputs: [],
|
|
112
|
+
name: "globalState",
|
|
113
|
+
outputs: [
|
|
114
|
+
{
|
|
115
|
+
internalType: "uint160",
|
|
116
|
+
name: "price",
|
|
117
|
+
type: "uint160",
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
internalType: "int24",
|
|
121
|
+
name: "tick",
|
|
122
|
+
type: "int24",
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
internalType: "uint16",
|
|
126
|
+
name: "feeZto",
|
|
127
|
+
type: "uint16",
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
internalType: "uint16",
|
|
131
|
+
name: "feeOtz",
|
|
132
|
+
type: "uint16",
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
internalType: "uint16",
|
|
136
|
+
name: "timepointIndex",
|
|
137
|
+
type: "uint16",
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
internalType: "uint8",
|
|
141
|
+
name: "communityFeeToken0",
|
|
142
|
+
type: "uint8",
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
internalType: "uint8",
|
|
146
|
+
name: "communityFeeToken1",
|
|
147
|
+
type: "uint8",
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
internalType: "bool",
|
|
151
|
+
name: "unlocked",
|
|
152
|
+
type: "bool",
|
|
153
|
+
},
|
|
154
|
+
],
|
|
155
|
+
stateMutability: "view",
|
|
156
|
+
type: "function",
|
|
157
|
+
},
|
|
158
|
+
];
|
|
159
|
+
const slot0 = yield (0, utils_1.readSlot0Contract)({
|
|
160
|
+
address: pool,
|
|
161
|
+
abi: slot0Abi,
|
|
162
|
+
currentExecutionContext,
|
|
163
|
+
functionName: "globalState",
|
|
164
|
+
provider: provider,
|
|
165
|
+
rpc,
|
|
166
|
+
chainId: this.chainId
|
|
167
|
+
});
|
|
168
|
+
return this.isDirectionalPool
|
|
169
|
+
? {
|
|
170
|
+
price: slot0[0],
|
|
171
|
+
tick: slot0[1],
|
|
172
|
+
observationIndex: slot0[2],
|
|
173
|
+
observationCardinality: slot0[3],
|
|
174
|
+
observationCardinalityNext: slot0[4],
|
|
175
|
+
feeProtocol: slot0[5],
|
|
176
|
+
unlocked: slot0[6],
|
|
177
|
+
}
|
|
178
|
+
: {
|
|
179
|
+
price: slot0[0],
|
|
180
|
+
tick: slot0[1],
|
|
181
|
+
feeProtocol: slot0[2],
|
|
182
|
+
observationIndex: slot0[2],
|
|
183
|
+
observationCardinality: slot0[3],
|
|
184
|
+
observationCardinalityNext: slot0[4],
|
|
185
|
+
unlocked: slot0[6],
|
|
186
|
+
};
|
|
187
|
+
});
|
|
188
|
+
this.steerSubgraph = steerSubgraph;
|
|
189
|
+
this.vault = vault;
|
|
190
|
+
this.isAlgebraPool = isAlgebraPool;
|
|
191
|
+
this.isDirectionalPool = isDirectionalPool;
|
|
192
|
+
this.chainId = chainId;
|
|
193
|
+
}
|
|
194
|
+
execute(_wasmFile, _vaultConfig, _data, _identity, _etherRpcConfig) {
|
|
195
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
196
|
+
const dataConnector = new bundleDataConnector_1.DataConnector(_wasmFile, _vaultConfig, _identity, _etherRpcConfig);
|
|
197
|
+
yield dataConnector.loadSync();
|
|
198
|
+
const executionContext = yield this.getExecutionContext(dataConnector, JSON.parse(_vaultConfig));
|
|
199
|
+
let rpc = '';
|
|
200
|
+
if (_etherRpcConfig) {
|
|
201
|
+
rpc = _etherRpcConfig[this.chainId.toString()].url;
|
|
202
|
+
}
|
|
203
|
+
const smartPoolExecutionContext = yield this.getSmartPoolExecutionContext(executionContext, dataConnector, JSON.parse(_vaultConfig), rpc);
|
|
204
|
+
dataConnector.setExecutionContextStrategyExecution(Object.assign(Object.assign({}, executionContext), smartPoolExecutionContext));
|
|
205
|
+
_data = _data.map((element) => JSON.stringify(element));
|
|
206
|
+
const result = yield dataConnector.executeBundle(_data);
|
|
207
|
+
return JSON.parse(result);
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
getSmartPoolExecutionContext(currentExecutionContext, dataConnector, vaultConfig, rpc) {
|
|
211
|
+
var _a, _b, _c, _d, _e, _f;
|
|
212
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
213
|
+
const smartPoolExecutionContext = {};
|
|
214
|
+
const request = {
|
|
215
|
+
method: 'POST',
|
|
216
|
+
url: this.steerSubgraph,
|
|
217
|
+
data: {
|
|
218
|
+
query: `
|
|
219
|
+
{
|
|
220
|
+
vaultPositions(where:{
|
|
221
|
+
vault: "${this.vault}",
|
|
222
|
+
timestamp_lt: "${currentExecutionContext.epochTimestamp}"
|
|
223
|
+
}, orderDirection: desc, orderBy: timestamp, first: 1){
|
|
224
|
+
id
|
|
225
|
+
timestamp
|
|
226
|
+
upperTick
|
|
227
|
+
lowerTick
|
|
228
|
+
relativeWeight
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
`,
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
const response = yield (0, utils_1.fetchData)(request, 4);
|
|
235
|
+
this.log("Subgraph response vault positions :", response);
|
|
236
|
+
const requestSnapshot = {
|
|
237
|
+
method: 'POST',
|
|
238
|
+
url: this.steerSubgraph,
|
|
239
|
+
data: {
|
|
240
|
+
query: `
|
|
241
|
+
{
|
|
242
|
+
vaultSnapshots(where:{
|
|
243
|
+
vaultAddress: "${this.vault}",
|
|
244
|
+
timestamp_lt: "${currentExecutionContext.epochTimestamp}"
|
|
245
|
+
}, orderDirection: desc, orderBy: timestamp, first: 1){
|
|
246
|
+
id
|
|
247
|
+
timestamp
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
`,
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
const responseSnapshot = yield (0, utils_1.fetchData)(requestSnapshot, 4);
|
|
254
|
+
this.log("Subgraph response current snapshot", responseSnapshot);
|
|
255
|
+
const positions = [];
|
|
256
|
+
if (((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.vaultPositions) && ((_b = response === null || response === void 0 ? void 0 : response.data) === null || _b === void 0 ? void 0 : _b.vaultPositions.length) > 0) {
|
|
257
|
+
const positionResponse = (_c = response === null || response === void 0 ? void 0 : response.data) === null || _c === void 0 ? void 0 : _c.vaultPositions[0];
|
|
258
|
+
for (let i = 0; i < positionResponse.upperTick.length; i++) {
|
|
259
|
+
const position = positionResponse;
|
|
260
|
+
positions.push({
|
|
261
|
+
startTick: Number(position.lowerTick[i]),
|
|
262
|
+
endTick: Number(position.upperTick[i]),
|
|
263
|
+
weight: Number(position.relativeWeight[i]),
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
let lastSnapshot;
|
|
268
|
+
if (((_d = responseSnapshot === null || responseSnapshot === void 0 ? void 0 : responseSnapshot.data) === null || _d === void 0 ? void 0 : _d.vaultSnapshots) && ((_e = responseSnapshot === null || responseSnapshot === void 0 ? void 0 : responseSnapshot.data) === null || _e === void 0 ? void 0 : _e.vaultSnapshots.length) > 0) {
|
|
269
|
+
lastSnapshot = (_f = responseSnapshot === null || responseSnapshot === void 0 ? void 0 : responseSnapshot.data) === null || _f === void 0 ? void 0 : _f.vaultSnapshots[0];
|
|
270
|
+
}
|
|
271
|
+
this.log("Subgraph response vault positions", positions);
|
|
272
|
+
smartPoolExecutionContext.positions = positions;
|
|
273
|
+
if (lastSnapshot) {
|
|
274
|
+
smartPoolExecutionContext.lastExecutionTime =
|
|
275
|
+
(Date.now() - (Number(lastSnapshot.timestamp) * 1000)) / 1000;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
smartPoolExecutionContext.lastExecutionTime =
|
|
279
|
+
currentExecutionContext.epochTimestamp;
|
|
280
|
+
}
|
|
281
|
+
const provider = dataConnector.getProvider(vaultConfig);
|
|
282
|
+
if (!provider) {
|
|
283
|
+
throw new Error("Provider configuration issue, unable to process Smart Pool execution context.");
|
|
284
|
+
}
|
|
285
|
+
const poolAddress = yield this.getVaultPoolAddress();
|
|
286
|
+
const currentTick = yield this.getPoolCurrentTick(poolAddress, provider, currentExecutionContext, rpc);
|
|
287
|
+
smartPoolExecutionContext.currentTick = currentTick;
|
|
288
|
+
// "currentTick": 10,
|
|
289
|
+
return smartPoolExecutionContext;
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
getPoolCurrentTick(poolAddress, provider, currentExecutionContext, rpc) {
|
|
293
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
294
|
+
if (!this.isAlgebraPool) {
|
|
295
|
+
const slot0 = yield this.getSlot0(poolAddress, currentExecutionContext, provider, rpc);
|
|
296
|
+
return slot0.tick;
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
const slot0 = yield this.getGlobalState(poolAddress, currentExecutionContext, provider, rpc);
|
|
300
|
+
return slot0.tick;
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
getVaultPoolAddress() {
|
|
305
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
306
|
+
const requestSnapshot = {
|
|
307
|
+
method: 'POST',
|
|
308
|
+
url: this.steerSubgraph,
|
|
309
|
+
data: {
|
|
310
|
+
query: `
|
|
311
|
+
{
|
|
312
|
+
vault(id: "${this.vault}"){
|
|
313
|
+
id
|
|
314
|
+
pool
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
`,
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
const response = yield (0, utils_1.fetchData)(requestSnapshot, 4);
|
|
321
|
+
return response.data.vault.pool;
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
exports.ExecutionApp = ExecutionApp;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
import { DataConnector } from '../bundle/bundleDataConnector';
|
|
3
|
+
import { VaultConfigPayload, ExecutionContext, EtherRpcConfig } 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>(_wasmFile: string | ArrayBuffer, _vaultConfig: string, _data: Array<any>, _identity?: ethers.providers.Provider, _etherRpcConfig?: EtherRpcConfig): Promise<Array<T>>;
|
|
11
|
+
protected getDataConnectorVersion(dataConnector: DataConnector): Promise<number>;
|
|
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(_wasmFile, _vaultConfig, _data, _identity, _etherRpcConfig) {
|
|
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,29 @@
|
|
|
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;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ExecutionBase } from "./executionBase";
|
|
2
|
+
import { DataConnector } from "../bundle/bundleDataConnector";
|
|
3
|
+
export declare class DataConnectorExecution extends ExecutionBase {
|
|
4
|
+
private dataConnectors;
|
|
5
|
+
constructor(dataConnectors: Array<DataConnector>, maxRetries?: number);
|
|
6
|
+
execute<T>(_vaultConfig: string): Promise<Array<T>>;
|
|
7
|
+
private handleRequestResponse;
|
|
8
|
+
private transformData;
|
|
9
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
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.DataConnectorExecution = void 0;
|
|
13
|
+
const executionBase_1 = require("./executionBase");
|
|
14
|
+
const pluginManager_1 = require("../utils/pluginManager");
|
|
15
|
+
const utils_1 = require("../utils/utils");
|
|
16
|
+
class DataConnectorExecution extends executionBase_1.ExecutionBase {
|
|
17
|
+
constructor(dataConnectors, maxRetries = 3) {
|
|
18
|
+
super(maxRetries);
|
|
19
|
+
this.dataConnectors = dataConnectors;
|
|
20
|
+
}
|
|
21
|
+
execute(_vaultConfig) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
const vaultConfig = JSON.parse(_vaultConfig);
|
|
24
|
+
const transformationData = [];
|
|
25
|
+
const pluginManager = new pluginManager_1.PluginManager();
|
|
26
|
+
yield pluginManager.registerFromPublicConfig();
|
|
27
|
+
for (const dataConnector of this.dataConnectors) {
|
|
28
|
+
if (!dataConnector.module) {
|
|
29
|
+
yield dataConnector.load();
|
|
30
|
+
}
|
|
31
|
+
const executionContext = yield this.getExecutionContext(dataConnector, vaultConfig);
|
|
32
|
+
dataConnector.setExecutionContextDataConnector(executionContext);
|
|
33
|
+
const initialRequest = yield dataConnector.execute('');
|
|
34
|
+
const finalResponse = yield this.handleRequestResponse(dataConnector, pluginManager, initialRequest);
|
|
35
|
+
const transformedData = yield this.transformData(dataConnector, finalResponse);
|
|
36
|
+
transformationData.push(transformedData);
|
|
37
|
+
}
|
|
38
|
+
return transformationData;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
handleRequestResponse(dataConnector, pluginManager, initialRequest) {
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
let request = initialRequest;
|
|
44
|
+
let response;
|
|
45
|
+
try {
|
|
46
|
+
while (request !== 'true') {
|
|
47
|
+
if (dataConnector.configObj.isChainRead == true) {
|
|
48
|
+
response = yield dataConnector.view(JSON.parse(request));
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
const parsedRequest = yield pluginManager.process(typeof request == 'string' ? JSON.parse(request) : request);
|
|
52
|
+
yield this.delay(1000);
|
|
53
|
+
response = yield (0, utils_1.fetchData)(parsedRequest, 0);
|
|
54
|
+
}
|
|
55
|
+
request = yield dataConnector.execute(JSON.stringify(response));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch (e) {
|
|
59
|
+
// process.env.SILENT_ERRORS
|
|
60
|
+
if (process.env.DATA_LOADER_SILENT_ERRORS !== 'true') {
|
|
61
|
+
console.error('Data connector failed to process.', e);
|
|
62
|
+
}
|
|
63
|
+
throw e;
|
|
64
|
+
}
|
|
65
|
+
return dataConnector.getResults();
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
69
|
+
transformData(dataConnector, rawData) {
|
|
70
|
+
var _a, _b, _c;
|
|
71
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
+
let transformedData = typeof rawData === 'string' ? JSON.parse(rawData) : rawData;
|
|
73
|
+
const version = yield this.getDataConnectorVersion(dataConnector);
|
|
74
|
+
if (version == 1 && transformedData.length > 0) {
|
|
75
|
+
const keys = Object.keys(transformedData[0]);
|
|
76
|
+
const requiredKeys = ['timestamp', 'open', 'high', 'low', 'close'];
|
|
77
|
+
const hasAllKeys = requiredKeys.every((key) => keys.includes(key));
|
|
78
|
+
// has the 'lookback' key
|
|
79
|
+
const lookback = (_a = dataConnector === null || dataConnector === void 0 ? void 0 : dataConnector.configObj) === null || _a === void 0 ? void 0 : _a.lookback;
|
|
80
|
+
if (hasAllKeys && !!lookback) {
|
|
81
|
+
// 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
|
|
82
|
+
if ((_b = dataConnector === null || dataConnector === void 0 ? void 0 : dataConnector.executionContext) === null || _b === void 0 ? void 0 : _b.epochTimestamp) {
|
|
83
|
+
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);
|
|
84
|
+
transformedData = extended;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return transformedData;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.DataConnectorExecution = DataConnectorExecution;
|