@lifi/sdk 1.3.1 → 1.5.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 (42) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/Lifi.d.ts +7 -0
  3. package/dist/Lifi.js +72 -30
  4. package/dist/cjs/Lifi.d.ts +7 -0
  5. package/dist/cjs/Lifi.js +72 -30
  6. package/dist/cjs/execution/StatusManager.d.ts +2 -2
  7. package/dist/cjs/execution/StatusManager.js +14 -7
  8. package/dist/cjs/execution/StepExecutor.d.ts +4 -2
  9. package/dist/cjs/execution/StepExecutor.js +27 -16
  10. package/dist/cjs/execution/allowance.execute.d.ts +2 -2
  11. package/dist/cjs/execution/allowance.execute.js +14 -21
  12. package/dist/cjs/execution/bridges/bridge.execute.d.ts +2 -2
  13. package/dist/cjs/execution/bridges/bridge.execute.js +102 -90
  14. package/dist/cjs/execution/exchanges/swap.execute.d.ts +2 -2
  15. package/dist/cjs/execution/exchanges/swap.execute.js +42 -54
  16. package/dist/cjs/execution/switchChain.js +3 -3
  17. package/dist/cjs/helpers.js +1 -1
  18. package/dist/cjs/services/ConfigService.js +4 -3
  19. package/dist/cjs/types/internal.types.d.ts +5 -1
  20. package/dist/cjs/utils/parseError.js +4 -5
  21. package/dist/cjs/utils/preRestart.js +5 -1
  22. package/dist/cjs/version.d.ts +1 -1
  23. package/dist/cjs/version.js +1 -1
  24. package/dist/execution/StatusManager.d.ts +2 -2
  25. package/dist/execution/StatusManager.js +14 -7
  26. package/dist/execution/StepExecutor.d.ts +4 -2
  27. package/dist/execution/StepExecutor.js +27 -16
  28. package/dist/execution/allowance.execute.d.ts +2 -2
  29. package/dist/execution/allowance.execute.js +14 -21
  30. package/dist/execution/bridges/bridge.execute.d.ts +2 -2
  31. package/dist/execution/bridges/bridge.execute.js +103 -91
  32. package/dist/execution/exchanges/swap.execute.d.ts +2 -2
  33. package/dist/execution/exchanges/swap.execute.js +43 -55
  34. package/dist/execution/switchChain.js +3 -3
  35. package/dist/helpers.js +1 -1
  36. package/dist/services/ConfigService.js +4 -3
  37. package/dist/types/internal.types.d.ts +5 -1
  38. package/dist/utils/parseError.js +4 -5
  39. package/dist/utils/preRestart.js +5 -1
  40. package/dist/version.d.ts +1 -1
  41. package/dist/version.js +1 -1
  42. package/package.json +10 -10
package/CHANGELOG.md CHANGED
@@ -2,6 +2,22 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [1.5.0](https://github.com/lifinance/sdk/compare/v1.4.1...v1.5.0) (2022-09-06)
6
+
7
+
8
+ ### Features
9
+
10
+ * add an option to start or resume execution in background ([#103](https://github.com/lifinance/sdk/issues/103)) ([c452559](https://github.com/lifinance/sdk/commit/c45255991ebcf494715c094db77f6c7599080c5d)), closes [#106](https://github.com/lifinance/sdk/issues/106) [#104](https://github.com/lifinance/sdk/issues/104)
11
+
12
+ ### [1.4.1](https://github.com/lifinance/sdk/compare/v1.4.0...v1.4.1) (2022-09-06)
13
+
14
+ ## [1.4.0](https://github.com/lifinance/sdk/compare/v1.3.1...v1.4.0) (2022-09-05)
15
+
16
+
17
+ ### Features
18
+
19
+ * skip step tx generation if tx data already provided ([#100](https://github.com/lifinance/sdk/issues/100)) ([280de4a](https://github.com/lifinance/sdk/commit/280de4a3d96b8077893e0bc1e0ed944f4772c5fb))
20
+
5
21
  ### [1.3.1](https://github.com/lifinance/sdk/compare/v1.3.0...v1.3.1) (2022-09-05)
6
22
 
7
23
 
package/dist/Lifi.d.ts CHANGED
@@ -104,8 +104,15 @@ export default class LIFI {
104
104
  /**
105
105
  * Executes a route until a user interaction is necessary (signing transactions, etc.) and then halts until the route is resumed.
106
106
  * @param {Route} route - A route that is currently in execution.
107
+ * @deprecated use updateRouteExecution instead.
107
108
  */
108
109
  moveExecutionToBackground: (route: Route) => void;
110
+ /**
111
+ * Updates route execution to background or foreground state.
112
+ * @param {Route} route - A route that is currently in execution.
113
+ * @param {boolean} settings - An object with execution settings.
114
+ */
115
+ updateRouteExecution: (route: Route, settings: Pick<ExecutionSettings, 'executeInBackground'>) => void;
109
116
  /**
110
117
  * Execute a route.
111
118
  * @param {Signer} signer - The signer required to send the transactions.
package/dist/Lifi.js CHANGED
@@ -149,7 +149,11 @@ export default class LIFI {
149
149
  return route;
150
150
  }
151
151
  for (const executor of this.activeRouteDictionary[route.id].executors) {
152
- executor.stopStepExecution({ allowUpdates: false });
152
+ executor.setInteraction({
153
+ allowInteraction: false,
154
+ allowUpdates: false,
155
+ stopExecution: true,
156
+ });
153
157
  }
154
158
  delete this.activeRouteDictionary[route.id];
155
159
  return route;
@@ -157,14 +161,36 @@ export default class LIFI {
157
161
  /**
158
162
  * Executes a route until a user interaction is necessary (signing transactions, etc.) and then halts until the route is resumed.
159
163
  * @param {Route} route - A route that is currently in execution.
164
+ * @deprecated use updateRouteExecution instead.
160
165
  */
161
166
  this.moveExecutionToBackground = (route) => {
162
- if (!this.activeRouteDictionary[route.id]) {
167
+ const activeRoute = this.activeRouteDictionary[route.id];
168
+ if (!activeRoute) {
163
169
  return;
164
170
  }
165
- for (const executor of this.activeRouteDictionary[route.id].executors) {
166
- executor.stopStepExecution({ allowUpdates: true });
171
+ for (const executor of activeRoute.executors) {
172
+ executor.setInteraction({ allowInteraction: false, allowUpdates: true });
167
173
  }
174
+ activeRoute.settings = Object.assign(Object.assign({}, activeRoute.settings), { executeInBackground: true });
175
+ };
176
+ /**
177
+ * Updates route execution to background or foreground state.
178
+ * @param {Route} route - A route that is currently in execution.
179
+ * @param {boolean} settings - An object with execution settings.
180
+ */
181
+ this.updateRouteExecution = (route, settings) => {
182
+ const activeRoute = this.activeRouteDictionary[route.id];
183
+ if (!activeRoute) {
184
+ return;
185
+ }
186
+ for (const executor of activeRoute.executors) {
187
+ executor.setInteraction({
188
+ allowInteraction: !settings.executeInBackground,
189
+ allowUpdates: true,
190
+ });
191
+ }
192
+ // Update active route settings so we know what the current state of execution is
193
+ activeRoute.settings = Object.assign(Object.assign({}, activeRoute.settings), settings);
168
194
  };
169
195
  /**
170
196
  * Execute a route.
@@ -175,8 +201,9 @@ export default class LIFI {
175
201
  * @throws {LifiError} Throws a LifiError if the execution fails.
176
202
  */
177
203
  this.executeRoute = (signer, route, settings) => __awaiter(this, void 0, void 0, function* () {
178
- const clonedRoute = deepClone(route); // deep clone to prevent side effects
179
- // check if route is already running
204
+ // Deep clone to prevent side effects
205
+ const clonedRoute = deepClone(route);
206
+ // Check if route is already running
180
207
  if (this.activeRouteDictionary[clonedRoute.id]) {
181
208
  // TODO: maybe inform user why nothing happens?
182
209
  return clonedRoute;
@@ -192,11 +219,16 @@ export default class LIFI {
192
219
  * @throws {LifiError} Throws a LifiError if the execution fails.
193
220
  */
194
221
  this.resumeRoute = (signer, route, settings) => __awaiter(this, void 0, void 0, function* () {
195
- const clonedRoute = deepClone(route); // deep clone to prevent side effects
222
+ // Deep clone to prevent side effects
223
+ const clonedRoute = deepClone(route);
196
224
  const activeRoute = this.activeRouteDictionary[clonedRoute.id];
197
225
  if (activeRoute) {
198
226
  const executionHalted = activeRoute.executors.some((executor) => executor.executionStopped);
199
227
  if (!executionHalted) {
228
+ // Check if we want to resume route execution in the background
229
+ this.updateRouteExecution(route, {
230
+ executeInBackground: settings === null || settings === void 0 ? void 0 : settings.executeInBackground,
231
+ });
200
232
  return clonedRoute;
201
233
  }
202
234
  }
@@ -204,48 +236,57 @@ export default class LIFI {
204
236
  return this.executeSteps(signer, clonedRoute, settings);
205
237
  });
206
238
  this.executeSteps = (signer, route, settings) => __awaiter(this, void 0, void 0, function* () {
239
+ var _a, _b, _c;
207
240
  const config = this.configService.getConfig();
208
- const execData = {
241
+ const executionData = {
209
242
  route,
210
243
  executors: [],
211
244
  settings: Object.assign(Object.assign({}, config.defaultExecutionSettings), settings),
212
245
  };
213
- this.activeRouteDictionary[route.id] = execData;
214
- const statusManager = new StatusManager(route, this.activeRouteDictionary[route.id].settings, (route) => (this.activeRouteDictionary[route.id].route = route));
215
- // loop over steps and execute them
246
+ this.activeRouteDictionary[route.id] = executionData;
247
+ const statusManager = new StatusManager(route, this.activeRouteDictionary[route.id].settings, (route) => {
248
+ if (this.activeRouteDictionary[route.id]) {
249
+ this.activeRouteDictionary[route.id].route = route;
250
+ }
251
+ });
252
+ // Loop over steps and execute them
216
253
  for (let index = 0; index < route.steps.length; index++) {
217
- //check if execution has stopped in meantime
218
- if (!this.activeRouteDictionary[route.id]) {
254
+ const activeRoute = this.activeRouteDictionary[route.id];
255
+ // Check if execution has stopped in the meantime
256
+ if (!activeRoute) {
219
257
  break;
220
258
  }
221
259
  const step = route.steps[index];
222
- const previousStep = index !== 0 ? route.steps[index - 1] : undefined;
223
- // check if step already done
224
- if (step.execution && step.execution.status === 'DONE') {
260
+ const previousStep = route.steps[index - 1];
261
+ // Check if the step is already done
262
+ if (((_a = step.execution) === null || _a === void 0 ? void 0 : _a.status) === 'DONE') {
225
263
  continue;
226
264
  }
227
- // update amount using output of previous execution. In the future this should be handled by calling `updateRoute`
228
- if (previousStep &&
229
- previousStep.execution &&
230
- previousStep.execution.toAmount) {
265
+ // Update amount using output of previous execution. In the future this should be handled by calling `updateRoute`
266
+ if ((_b = previousStep === null || previousStep === void 0 ? void 0 : previousStep.execution) === null || _b === void 0 ? void 0 : _b.toAmount) {
231
267
  step.action.fromAmount = previousStep.execution.toAmount;
232
268
  }
233
- let stepExecutor;
234
269
  try {
235
- stepExecutor = new StepExecutor(statusManager, this.activeRouteDictionary[route.id].settings);
236
- this.activeRouteDictionary[route.id].executors.push(stepExecutor);
237
- yield stepExecutor.executeStep(signer, step);
270
+ const stepExecutor = new StepExecutor(statusManager, activeRoute.settings);
271
+ activeRoute.executors.push(stepExecutor);
272
+ // Check if we want to execute this step in the background
273
+ this.updateRouteExecution(route, activeRoute.settings);
274
+ const executedStep = yield stepExecutor.executeStep(signer, step);
275
+ // We may reach this point if user interaction isn't allowed. We want to stop execution until we resume it
276
+ if (((_c = executedStep.execution) === null || _c === void 0 ? void 0 : _c.status) !== 'DONE') {
277
+ this.stopExecution(route);
278
+ }
279
+ // Execution stopped during the current step, we don't want to continue to the next step so we return already
280
+ if (stepExecutor.executionStopped) {
281
+ return route;
282
+ }
238
283
  }
239
284
  catch (e) {
240
285
  this.stopExecution(route);
241
286
  throw e;
242
287
  }
243
- // execution stopped during the current step, we don't want to continue to the next step so we return already
244
- if (stepExecutor.executionStopped) {
245
- return route;
246
- }
247
288
  }
248
- //clean up after execution
289
+ // Clean up after the execution
249
290
  delete this.activeRouteDictionary[route.id];
250
291
  return route;
251
292
  });
@@ -362,7 +403,8 @@ export default class LIFI {
362
403
  };
363
404
  this.configService = ConfigService.getInstance();
364
405
  if (configUpdate) {
365
- this.configService.updateConfig(configUpdate); // update API urls before we request chains
406
+ // Update API urls before we request chains
407
+ this.configService.updateConfig(configUpdate);
366
408
  }
367
409
  this.chainsService = ChainsService.getInstance();
368
410
  this.chainsService.getChains().then((chains) => {
@@ -104,8 +104,15 @@ export default class LIFI {
104
104
  /**
105
105
  * Executes a route until a user interaction is necessary (signing transactions, etc.) and then halts until the route is resumed.
106
106
  * @param {Route} route - A route that is currently in execution.
107
+ * @deprecated use updateRouteExecution instead.
107
108
  */
108
109
  moveExecutionToBackground: (route: Route) => void;
110
+ /**
111
+ * Updates route execution to background or foreground state.
112
+ * @param {Route} route - A route that is currently in execution.
113
+ * @param {boolean} settings - An object with execution settings.
114
+ */
115
+ updateRouteExecution: (route: Route, settings: Pick<ExecutionSettings, 'executeInBackground'>) => void;
109
116
  /**
110
117
  * Execute a route.
111
118
  * @param {Signer} signer - The signer required to send the transactions.
package/dist/cjs/Lifi.js CHANGED
@@ -154,7 +154,11 @@ class LIFI {
154
154
  return route;
155
155
  }
156
156
  for (const executor of this.activeRouteDictionary[route.id].executors) {
157
- executor.stopStepExecution({ allowUpdates: false });
157
+ executor.setInteraction({
158
+ allowInteraction: false,
159
+ allowUpdates: false,
160
+ stopExecution: true,
161
+ });
158
162
  }
159
163
  delete this.activeRouteDictionary[route.id];
160
164
  return route;
@@ -162,14 +166,36 @@ class LIFI {
162
166
  /**
163
167
  * Executes a route until a user interaction is necessary (signing transactions, etc.) and then halts until the route is resumed.
164
168
  * @param {Route} route - A route that is currently in execution.
169
+ * @deprecated use updateRouteExecution instead.
165
170
  */
166
171
  this.moveExecutionToBackground = (route) => {
167
- if (!this.activeRouteDictionary[route.id]) {
172
+ const activeRoute = this.activeRouteDictionary[route.id];
173
+ if (!activeRoute) {
168
174
  return;
169
175
  }
170
- for (const executor of this.activeRouteDictionary[route.id].executors) {
171
- executor.stopStepExecution({ allowUpdates: true });
176
+ for (const executor of activeRoute.executors) {
177
+ executor.setInteraction({ allowInteraction: false, allowUpdates: true });
172
178
  }
179
+ activeRoute.settings = Object.assign(Object.assign({}, activeRoute.settings), { executeInBackground: true });
180
+ };
181
+ /**
182
+ * Updates route execution to background or foreground state.
183
+ * @param {Route} route - A route that is currently in execution.
184
+ * @param {boolean} settings - An object with execution settings.
185
+ */
186
+ this.updateRouteExecution = (route, settings) => {
187
+ const activeRoute = this.activeRouteDictionary[route.id];
188
+ if (!activeRoute) {
189
+ return;
190
+ }
191
+ for (const executor of activeRoute.executors) {
192
+ executor.setInteraction({
193
+ allowInteraction: !settings.executeInBackground,
194
+ allowUpdates: true,
195
+ });
196
+ }
197
+ // Update active route settings so we know what the current state of execution is
198
+ activeRoute.settings = Object.assign(Object.assign({}, activeRoute.settings), settings);
173
199
  };
174
200
  /**
175
201
  * Execute a route.
@@ -180,8 +206,9 @@ class LIFI {
180
206
  * @throws {LifiError} Throws a LifiError if the execution fails.
181
207
  */
182
208
  this.executeRoute = (signer, route, settings) => __awaiter(this, void 0, void 0, function* () {
183
- const clonedRoute = (0, utils_1.deepClone)(route); // deep clone to prevent side effects
184
- // check if route is already running
209
+ // Deep clone to prevent side effects
210
+ const clonedRoute = (0, utils_1.deepClone)(route);
211
+ // Check if route is already running
185
212
  if (this.activeRouteDictionary[clonedRoute.id]) {
186
213
  // TODO: maybe inform user why nothing happens?
187
214
  return clonedRoute;
@@ -197,11 +224,16 @@ class LIFI {
197
224
  * @throws {LifiError} Throws a LifiError if the execution fails.
198
225
  */
199
226
  this.resumeRoute = (signer, route, settings) => __awaiter(this, void 0, void 0, function* () {
200
- const clonedRoute = (0, utils_1.deepClone)(route); // deep clone to prevent side effects
227
+ // Deep clone to prevent side effects
228
+ const clonedRoute = (0, utils_1.deepClone)(route);
201
229
  const activeRoute = this.activeRouteDictionary[clonedRoute.id];
202
230
  if (activeRoute) {
203
231
  const executionHalted = activeRoute.executors.some((executor) => executor.executionStopped);
204
232
  if (!executionHalted) {
233
+ // Check if we want to resume route execution in the background
234
+ this.updateRouteExecution(route, {
235
+ executeInBackground: settings === null || settings === void 0 ? void 0 : settings.executeInBackground,
236
+ });
205
237
  return clonedRoute;
206
238
  }
207
239
  }
@@ -209,48 +241,57 @@ class LIFI {
209
241
  return this.executeSteps(signer, clonedRoute, settings);
210
242
  });
211
243
  this.executeSteps = (signer, route, settings) => __awaiter(this, void 0, void 0, function* () {
244
+ var _a, _b, _c;
212
245
  const config = this.configService.getConfig();
213
- const execData = {
246
+ const executionData = {
214
247
  route,
215
248
  executors: [],
216
249
  settings: Object.assign(Object.assign({}, config.defaultExecutionSettings), settings),
217
250
  };
218
- this.activeRouteDictionary[route.id] = execData;
219
- const statusManager = new StatusManager_1.StatusManager(route, this.activeRouteDictionary[route.id].settings, (route) => (this.activeRouteDictionary[route.id].route = route));
220
- // loop over steps and execute them
251
+ this.activeRouteDictionary[route.id] = executionData;
252
+ const statusManager = new StatusManager_1.StatusManager(route, this.activeRouteDictionary[route.id].settings, (route) => {
253
+ if (this.activeRouteDictionary[route.id]) {
254
+ this.activeRouteDictionary[route.id].route = route;
255
+ }
256
+ });
257
+ // Loop over steps and execute them
221
258
  for (let index = 0; index < route.steps.length; index++) {
222
- //check if execution has stopped in meantime
223
- if (!this.activeRouteDictionary[route.id]) {
259
+ const activeRoute = this.activeRouteDictionary[route.id];
260
+ // Check if execution has stopped in the meantime
261
+ if (!activeRoute) {
224
262
  break;
225
263
  }
226
264
  const step = route.steps[index];
227
- const previousStep = index !== 0 ? route.steps[index - 1] : undefined;
228
- // check if step already done
229
- if (step.execution && step.execution.status === 'DONE') {
265
+ const previousStep = route.steps[index - 1];
266
+ // Check if the step is already done
267
+ if (((_a = step.execution) === null || _a === void 0 ? void 0 : _a.status) === 'DONE') {
230
268
  continue;
231
269
  }
232
- // update amount using output of previous execution. In the future this should be handled by calling `updateRoute`
233
- if (previousStep &&
234
- previousStep.execution &&
235
- previousStep.execution.toAmount) {
270
+ // Update amount using output of previous execution. In the future this should be handled by calling `updateRoute`
271
+ if ((_b = previousStep === null || previousStep === void 0 ? void 0 : previousStep.execution) === null || _b === void 0 ? void 0 : _b.toAmount) {
236
272
  step.action.fromAmount = previousStep.execution.toAmount;
237
273
  }
238
- let stepExecutor;
239
274
  try {
240
- stepExecutor = new StepExecutor_1.StepExecutor(statusManager, this.activeRouteDictionary[route.id].settings);
241
- this.activeRouteDictionary[route.id].executors.push(stepExecutor);
242
- yield stepExecutor.executeStep(signer, step);
275
+ const stepExecutor = new StepExecutor_1.StepExecutor(statusManager, activeRoute.settings);
276
+ activeRoute.executors.push(stepExecutor);
277
+ // Check if we want to execute this step in the background
278
+ this.updateRouteExecution(route, activeRoute.settings);
279
+ const executedStep = yield stepExecutor.executeStep(signer, step);
280
+ // We may reach this point if user interaction isn't allowed. We want to stop execution until we resume it
281
+ if (((_c = executedStep.execution) === null || _c === void 0 ? void 0 : _c.status) !== 'DONE') {
282
+ this.stopExecution(route);
283
+ }
284
+ // Execution stopped during the current step, we don't want to continue to the next step so we return already
285
+ if (stepExecutor.executionStopped) {
286
+ return route;
287
+ }
243
288
  }
244
289
  catch (e) {
245
290
  this.stopExecution(route);
246
291
  throw e;
247
292
  }
248
- // execution stopped during the current step, we don't want to continue to the next step so we return already
249
- if (stepExecutor.executionStopped) {
250
- return route;
251
- }
252
293
  }
253
- //clean up after execution
294
+ // Clean up after the execution
254
295
  delete this.activeRouteDictionary[route.id];
255
296
  return route;
256
297
  });
@@ -367,7 +408,8 @@ class LIFI {
367
408
  };
368
409
  this.configService = ConfigService_1.default.getInstance();
369
410
  if (configUpdate) {
370
- this.configService.updateConfig(configUpdate); // update API urls before we request chains
411
+ // Update API urls before we request chains
412
+ this.configService.updateConfig(configUpdate);
371
413
  }
372
414
  this.chainsService = ChainsService_1.default.getInstance();
373
415
  this.chainsService.getChains().then((chains) => {
@@ -42,7 +42,7 @@ export declare class StatusManager {
42
42
  * @param {Status} status By default created procces is set to the STARTED status. We can override new process with the needed status.
43
43
  * @return {Process}
44
44
  */
45
- findOrCreateProcess: (type: ProcessType, step: Step, status?: Status) => Process;
45
+ findOrCreateProcess: (step: Step, type: ProcessType, status?: Status) => Process;
46
46
  /**
47
47
  * Update a process object.
48
48
  * @param {Step} step The step where the process should be updated
@@ -60,6 +60,6 @@ export declare class StatusManager {
60
60
  */
61
61
  removeProcess: (step: Step, type: ProcessType) => void;
62
62
  updateStepInRoute: (step: Step) => Step;
63
- setShouldUpdate(value: boolean): void;
63
+ allowUpdates(value: boolean): void;
64
64
  }
65
65
  export {};
@@ -40,12 +40,17 @@ class StatusManager {
40
40
  * @param {Status} status By default created procces is set to the STARTED status. We can override new process with the needed status.
41
41
  * @return {Process}
42
42
  */
43
- this.findOrCreateProcess = (type, step, status) => {
44
- if (!step.execution || !step.execution.process) {
43
+ this.findOrCreateProcess = (step, type, status) => {
44
+ var _a;
45
+ if (!((_a = step.execution) === null || _a === void 0 ? void 0 : _a.process)) {
45
46
  throw new Error("Execution hasn't been initialized.");
46
47
  }
47
48
  const process = step.execution.process.find((p) => p.type === type);
48
49
  if (process) {
50
+ if (status && process.status !== status) {
51
+ process.status = status;
52
+ this.updateStepInRoute(step);
53
+ }
49
54
  return process;
50
55
  }
51
56
  const newProcess = {
@@ -67,7 +72,7 @@ class StatusManager {
67
72
  * @return {Process} The update process
68
73
  */
69
74
  this.updateProcess = (step, type, status, params) => {
70
- var _a;
75
+ var _a, _b, _c;
71
76
  if (!step.execution) {
72
77
  throw new Error("Can't update an empty step execution.");
73
78
  }
@@ -92,9 +97,6 @@ class StatusManager {
92
97
  case 'ACTION_REQUIRED':
93
98
  step.execution.status = 'ACTION_REQUIRED';
94
99
  break;
95
- case 'CHAIN_SWITCH_REQUIRED':
96
- step.execution.status = 'CHAIN_SWITCH_REQUIRED';
97
- break;
98
100
  default:
99
101
  break;
100
102
  }
@@ -106,6 +108,11 @@ class StatusManager {
106
108
  currentProcess[key] = value;
107
109
  }
108
110
  }
111
+ // Sort processes, the ones with DONE status go first
112
+ step.execution.process = [
113
+ ...(_b = step === null || step === void 0 ? void 0 : step.execution) === null || _b === void 0 ? void 0 : _b.process.filter((process) => process.status === 'DONE'),
114
+ ...(_c = step === null || step === void 0 ? void 0 : step.execution) === null || _c === void 0 ? void 0 : _c.process.filter((process) => process.status !== 'DONE'),
115
+ ];
109
116
  this.updateStepInRoute(step); // updates the step in the route
110
117
  return currentProcess;
111
118
  };
@@ -160,7 +167,7 @@ class StatusManager {
160
167
  this.updateStepInRoute(step);
161
168
  return step;
162
169
  }
163
- setShouldUpdate(value) {
170
+ allowUpdates(value) {
164
171
  this.shouldUpdate = value;
165
172
  }
166
173
  }
@@ -1,14 +1,16 @@
1
1
  import { Signer } from 'ethers';
2
- import { HaltingSettings, InternalExecutionSettings, Step } from '../types';
2
+ import { InteractionSettings, InternalExecutionSettings, Step } from '../types';
3
3
  import { StatusManager } from './StatusManager';
4
4
  export declare class StepExecutor {
5
5
  settings: InternalExecutionSettings;
6
6
  statusManager: StatusManager;
7
7
  private swapExecutionManager;
8
8
  private bridgeExecutionManager;
9
+ allowUserInteraction: boolean;
9
10
  executionStopped: boolean;
10
11
  constructor(statusManager: StatusManager, settings: InternalExecutionSettings);
11
- stopStepExecution: (settings?: HaltingSettings) => void;
12
+ setInteraction: (settings?: InteractionSettings) => void;
13
+ checkChain: () => never;
12
14
  executeStep: (signer: Signer, step: Step) => Promise<Step>;
13
15
  private executeSwap;
14
16
  private executeCross;
@@ -13,26 +13,37 @@ exports.StepExecutor = void 0;
13
13
  const bridge_execute_1 = require("./bridges/bridge.execute");
14
14
  const swap_execute_1 = require("./exchanges/swap.execute");
15
15
  const switchChain_1 = require("./switchChain");
16
- const defaultExecutionHaltSettings = {
16
+ // Please be careful when changing the defaults as it may break the behavior (e.g., background execution)
17
+ const defaultInteractionSettings = {
18
+ allowInteraction: true,
17
19
  allowUpdates: true,
20
+ stopExecution: false,
18
21
  };
19
22
  class StepExecutor {
20
23
  constructor(statusManager, settings) {
21
24
  this.swapExecutionManager = new swap_execute_1.SwapExecutionManager();
22
25
  this.bridgeExecutionManager = new bridge_execute_1.BridgeExecutionManager();
26
+ this.allowUserInteraction = true;
23
27
  this.executionStopped = false;
24
- this.stopStepExecution = (settings) => {
25
- const haltingSettings = Object.assign(Object.assign({}, defaultExecutionHaltSettings), settings);
26
- this.swapExecutionManager.setShouldContinue(false);
27
- this.bridgeExecutionManager.setShouldContinue(false);
28
- this.statusManager.setShouldUpdate(haltingSettings.allowUpdates);
29
- this.executionStopped = true;
28
+ this.setInteraction = (settings) => {
29
+ const interactionSettings = Object.assign(Object.assign({}, defaultInteractionSettings), settings);
30
+ this.allowUserInteraction = interactionSettings.allowInteraction;
31
+ this.swapExecutionManager.allowInteraction(interactionSettings.allowInteraction);
32
+ this.bridgeExecutionManager.allowInteraction(interactionSettings.allowInteraction);
33
+ this.statusManager.allowUpdates(interactionSettings.allowUpdates);
34
+ this.executionStopped = interactionSettings.stopExecution;
35
+ };
36
+ // TODO: add checkChain method and update signer inside executors
37
+ // This can come in handy when we execute multiple routes simultaneously and
38
+ // should be sure that we are on the right chain when waiting for transactions.
39
+ this.checkChain = () => {
40
+ throw new Error('checkChain is not implemented.');
30
41
  };
31
42
  this.executeStep = (signer, step) => __awaiter(this, void 0, void 0, function* () {
32
- // check if signer is for correct chain
33
- const updatedSigner = yield (0, switchChain_1.switchChain)(signer, this.statusManager, step, this.settings.switchChainHook, !this.executionStopped);
43
+ // Make sure that the chain is still correct
44
+ const updatedSigner = yield (0, switchChain_1.switchChain)(signer, this.statusManager, step, this.settings.switchChainHook, this.allowUserInteraction);
34
45
  if (!updatedSigner) {
35
- // chain switch was not successful, stop execution here
46
+ // Chain switch was not successful, stop execution here
36
47
  return step;
37
48
  }
38
49
  signer = updatedSigner;
@@ -49,24 +60,24 @@ class StepExecutor {
49
60
  }
50
61
  return step;
51
62
  });
52
- this.executeSwap = (signer, step) => __awaiter(this, void 0, void 0, function* () {
63
+ this.executeSwap = (signer, step) => {
53
64
  const swapParams = {
54
65
  signer,
55
66
  step,
56
67
  settings: this.settings,
57
68
  statusManager: this.statusManager,
58
69
  };
59
- return yield this.swapExecutionManager.execute(swapParams);
60
- });
61
- this.executeCross = (signer, step) => __awaiter(this, void 0, void 0, function* () {
70
+ return this.swapExecutionManager.execute(swapParams);
71
+ };
72
+ this.executeCross = (signer, step) => {
62
73
  const crossParams = {
63
74
  signer,
64
75
  step,
65
76
  settings: this.settings,
66
77
  statusManager: this.statusManager,
67
78
  };
68
- return yield this.bridgeExecutionManager.execute(crossParams);
69
- });
79
+ return this.bridgeExecutionManager.execute(crossParams);
80
+ };
70
81
  this.statusManager = statusManager;
71
82
  this.settings = settings;
72
83
  }
@@ -1,4 +1,4 @@
1
1
  import { Signer } from 'ethers';
2
- import { Chain, Step, Token } from '../types';
2
+ import { Chain, InternalExecutionSettings, Step } from '../types';
3
3
  import { StatusManager } from './StatusManager';
4
- export declare const checkAllowance: (signer: Signer, step: Step, chain: Chain, token: Token, amount: string, spenderAddress: string, statusManager: StatusManager, infiniteApproval?: boolean, allowUserInteraction?: boolean) => Promise<void>;
4
+ export declare const checkAllowance: (signer: Signer, step: Step, statusManager: StatusManager, settings: InternalExecutionSettings, chain: Chain, allowUserInteraction?: boolean) => Promise<void>;