@lifi/sdk 2.0.0-beta.2 → 2.0.0-beta.4

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 (31) hide show
  1. package/dist/{Lifi.d.ts → LiFi.d.ts} +4 -60
  2. package/dist/{Lifi.js → LiFi.js} +3 -213
  3. package/dist/cjs/{Lifi.d.ts → LiFi.d.ts} +4 -60
  4. package/dist/cjs/{Lifi.js → LiFi.js} +5 -214
  5. package/dist/cjs/execution/RouteExecutionManager.d.ts +65 -0
  6. package/dist/cjs/execution/RouteExecutionManager.js +220 -0
  7. package/dist/cjs/execution/{ExecutionManager.d.ts → StepExecutionManager.d.ts} +1 -1
  8. package/dist/cjs/execution/{ExecutionManager.js → StepExecutionManager.js} +3 -3
  9. package/dist/cjs/execution/StepExecutor.d.ts +2 -2
  10. package/dist/cjs/execution/StepExecutor.js +4 -4
  11. package/dist/cjs/helpers.d.ts +1 -1
  12. package/dist/cjs/index.d.ts +1 -2
  13. package/dist/cjs/index.js +3 -6
  14. package/dist/cjs/services/ApiService.js +8 -4
  15. package/dist/cjs/types/internal.types.d.ts +3 -7
  16. package/dist/cjs/version.d.ts +1 -1
  17. package/dist/cjs/version.js +1 -1
  18. package/dist/execution/RouteExecutionManager.d.ts +65 -0
  19. package/dist/execution/RouteExecutionManager.js +213 -0
  20. package/dist/execution/{ExecutionManager.d.ts → StepExecutionManager.d.ts} +1 -1
  21. package/dist/execution/{ExecutionManager.js → StepExecutionManager.js} +1 -1
  22. package/dist/execution/StepExecutor.d.ts +2 -2
  23. package/dist/execution/StepExecutor.js +4 -4
  24. package/dist/helpers.d.ts +1 -1
  25. package/dist/index.d.ts +1 -2
  26. package/dist/index.js +1 -3
  27. package/dist/services/ApiService.js +8 -4
  28. package/dist/types/internal.types.d.ts +3 -7
  29. package/dist/version.d.ts +1 -1
  30. package/dist/version.js +1 -1
  31. package/package.json +4 -4
@@ -1,11 +1,10 @@
1
1
  import { FallbackProvider } from '@ethersproject/providers';
2
- import { ChainId, ChainKey, ContractCallQuoteRequest, ExtendedChain, GasRecommendationRequest, GasRecommendationResponse, GetStatusRequest, PossibilitiesRequest, PossibilitiesResponse, QuoteRequest, RequestOptions, Route, RoutesRequest, RoutesResponse, StatusResponse, Step, Token, TokenAmount, TokensRequest, TokensResponse, ToolsRequest, ToolsResponse } from '@lifi/types';
2
+ import { ChainId, ChainKey, ContractCallQuoteRequest, ExtendedChain, GasRecommendationRequest, GasRecommendationResponse, GetStatusRequest, PossibilitiesRequest, PossibilitiesResponse, QuoteRequest, RequestOptions, RoutesRequest, RoutesResponse, StatusResponse, Step, Token, TokenAmount, TokensRequest, TokensResponse, ToolsRequest, ToolsResponse } from '@lifi/types';
3
3
  import { Signer } from 'ethers';
4
4
  import { ApproveTokenRequest, RevokeApprovalRequest } from './allowance';
5
- import { Config, ConfigUpdate, ExecutionSettings, RevokeTokenData } from './types';
6
- export default class LIFI {
7
- private activeRouteDictionary;
8
- private configService;
5
+ import { RouteExecutionManager } from './execution/RouteExecutionManager';
6
+ import { Config, ConfigUpdate, RevokeTokenData } from './types';
7
+ export declare class LiFi extends RouteExecutionManager {
9
8
  private chainsService;
10
9
  constructor(configUpdate?: ConfigUpdate);
11
10
  /**
@@ -101,61 +100,6 @@ export default class LIFI {
101
100
  * @throws {LifiError} Throws a LifiError if request fails.
102
101
  */
103
102
  getGasRecommendation: (request: GasRecommendationRequest, options?: RequestOptions) => Promise<GasRecommendationResponse>;
104
- /**
105
- * Stops the execution of an active route.
106
- * @param {Route} route - A route that is currently in execution.
107
- * @return {Route} The stopped route.
108
- */
109
- stopExecution: (route: Route) => Route;
110
- /**
111
- * Executes a route until a user interaction is necessary (signing transactions, etc.) and then halts until the route is resumed.
112
- * @param {Route} route - A route that is currently in execution.
113
- * @deprecated use updateRouteExecution instead.
114
- */
115
- moveExecutionToBackground: (route: Route) => void;
116
- /**
117
- * Updates route execution to background or foreground state.
118
- * @param {Route} route - A route that is currently in execution.
119
- * @param {boolean} settings - An object with execution settings.
120
- */
121
- updateRouteExecution: (route: Route, settings: Pick<ExecutionSettings, 'executeInBackground'>) => void;
122
- /**
123
- * Execute a route.
124
- * @param {Signer} signer - The signer required to send the transactions.
125
- * @param {Route} route - The route that should be executed. Cannot be an active route.
126
- * @param {ExecutionSettings} settings - An object containing settings and callbacks.
127
- * @return {Promise<Route>} The executed route.
128
- * @throws {LifiError} Throws a LifiError if the execution fails.
129
- */
130
- executeRoute: (signer: Signer, route: Route, settings?: ExecutionSettings) => Promise<Route>;
131
- /**
132
- * Resume the execution of a route that has been stopped or had an error while executing.
133
- * @param {Signer} signer - The signer required to send the transactions.
134
- * @param {Route} route - The route that is to be executed. Cannot be an active route.
135
- * @param {ExecutionSettings} settings - An object containing settings and callbacks.
136
- * @return {Promise<Route>} The executed route.
137
- * @throws {LifiError} Throws a LifiError if the execution fails.
138
- */
139
- resumeRoute: (signer: Signer, route: Route, settings?: ExecutionSettings) => Promise<Route>;
140
- private executeSteps;
141
- /**
142
- * Update the ExecutionSettings for an active route.
143
- * @param {ExecutionSettings} settings - An object with execution settings.
144
- * @param {Route} route - The active route that gets the new execution settings.
145
- * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
146
- */
147
- updateExecutionSettings: (settings: ExecutionSettings, route: Route) => void;
148
- /**
149
- * Get the list of active routes.
150
- * @return {Route[]} A list of routes.
151
- */
152
- getActiveRoutes: () => Route[];
153
- /**
154
- * Return the current route information for given route. The route has to be active.
155
- * @param {Route} route - A route object.
156
- * @return {Route} The updated route.
157
- */
158
- getActiveRoute: (route: Route) => Route | undefined;
159
103
  /**
160
104
  * Returns the balances of a specific token a wallet holds across all aggregated chains.
161
105
  * @param {string} walletAddress - A wallet address.
@@ -1,19 +1,16 @@
1
1
  import { approveToken, bulkGetTokenApproval, getTokenApproval, revokeTokenApproval, } from './allowance';
2
2
  import * as balance from './balance';
3
3
  import { getRpcProvider } from './connectors';
4
- import { StatusManager } from './execution/StatusManager';
5
- import { StepExecutor } from './execution/StepExecutor';
4
+ import { RouteExecutionManager } from './execution/RouteExecutionManager';
6
5
  import { checkPackageUpdates } from './helpers';
7
6
  import ApiService from './services/ApiService';
8
7
  import ChainsService from './services/ChainsService';
9
- import ConfigService from './services/ConfigService';
10
8
  import { isToken } from './typeguards';
11
9
  import { ValidationError } from './utils/errors';
12
- import { handlePreRestart } from './utils/preRestart';
13
10
  import { name, version } from './version';
14
- export default class LIFI {
11
+ export class LiFi extends RouteExecutionManager {
15
12
  constructor(configUpdate) {
16
- this.activeRouteDictionary = {};
13
+ super(configUpdate);
17
14
  /**
18
15
  * Get the current configuration of the SDK
19
16
  * @return {Config} - The config object
@@ -137,208 +134,6 @@ export default class LIFI {
137
134
  this.getGasRecommendation = async (request, options) => {
138
135
  return ApiService.getGasRecommendation(request, options);
139
136
  };
140
- /**
141
- * Stops the execution of an active route.
142
- * @param {Route} route - A route that is currently in execution.
143
- * @return {Route} The stopped route.
144
- */
145
- this.stopExecution = (route) => {
146
- if (!this.activeRouteDictionary[route.id]) {
147
- return route;
148
- }
149
- const { executionData } = this.activeRouteDictionary[route.id];
150
- for (const executor of executionData.executors) {
151
- executor.setInteraction({
152
- allowInteraction: false,
153
- allowUpdates: false,
154
- stopExecution: true,
155
- });
156
- }
157
- delete this.activeRouteDictionary[route.id];
158
- return route;
159
- };
160
- /**
161
- * Executes a route until a user interaction is necessary (signing transactions, etc.) and then halts until the route is resumed.
162
- * @param {Route} route - A route that is currently in execution.
163
- * @deprecated use updateRouteExecution instead.
164
- */
165
- this.moveExecutionToBackground = (route) => {
166
- const { executionData } = this.activeRouteDictionary[route.id];
167
- if (!executionData) {
168
- return;
169
- }
170
- for (const executor of executionData.executors) {
171
- executor.setInteraction({ allowInteraction: false, allowUpdates: true });
172
- }
173
- executionData.settings = {
174
- ...executionData.settings,
175
- executeInBackground: true,
176
- };
177
- };
178
- /**
179
- * Updates route execution to background or foreground state.
180
- * @param {Route} route - A route that is currently in execution.
181
- * @param {boolean} settings - An object with execution settings.
182
- */
183
- this.updateRouteExecution = (route, settings) => {
184
- const { executionData } = this.activeRouteDictionary[route.id];
185
- if (!executionData) {
186
- return;
187
- }
188
- for (const executor of executionData.executors) {
189
- executor.setInteraction({
190
- allowInteraction: !settings.executeInBackground,
191
- allowUpdates: true,
192
- });
193
- }
194
- // Update active route settings so we know what the current state of execution is
195
- executionData.settings = {
196
- ...executionData.settings,
197
- ...settings,
198
- };
199
- };
200
- /**
201
- * Execute a route.
202
- * @param {Signer} signer - The signer required to send the transactions.
203
- * @param {Route} route - The route that should be executed. Cannot be an active route.
204
- * @param {ExecutionSettings} settings - An object containing settings and callbacks.
205
- * @return {Promise<Route>} The executed route.
206
- * @throws {LifiError} Throws a LifiError if the execution fails.
207
- */
208
- this.executeRoute = async (signer, route, settings) => {
209
- // Deep clone to prevent side effects
210
- const clonedRoute = structuredClone(route);
211
- // Check if route is already running
212
- if (this.activeRouteDictionary[clonedRoute.id]) {
213
- // TODO: maybe inform user why nothing happens?
214
- return this.activeRouteDictionary[clonedRoute.id].executionPromise;
215
- }
216
- const executionPromise = this.executeSteps(signer, clonedRoute, settings);
217
- this.activeRouteDictionary[clonedRoute.id] = {
218
- ...this.activeRouteDictionary[clonedRoute.id],
219
- executionPromise,
220
- };
221
- return executionPromise;
222
- };
223
- /**
224
- * Resume the execution of a route that has been stopped or had an error while executing.
225
- * @param {Signer} signer - The signer required to send the transactions.
226
- * @param {Route} route - The route that is to be executed. Cannot be an active route.
227
- * @param {ExecutionSettings} settings - An object containing settings and callbacks.
228
- * @return {Promise<Route>} The executed route.
229
- * @throws {LifiError} Throws a LifiError if the execution fails.
230
- */
231
- this.resumeRoute = async (signer, route, settings) => {
232
- // Deep clone to prevent side effects
233
- const clonedRoute = structuredClone(route);
234
- const { executionData, executionPromise } = this.activeRouteDictionary[clonedRoute.id];
235
- if (executionData) {
236
- const executionHalted = executionData.executors.some((executor) => executor.executionStopped);
237
- if (!executionHalted) {
238
- // Check if we want to resume route execution in the background
239
- this.updateRouteExecution(route, {
240
- executeInBackground: settings?.executeInBackground,
241
- });
242
- return executionPromise;
243
- }
244
- }
245
- handlePreRestart(clonedRoute);
246
- const newExecutionPromise = this.executeSteps(signer, clonedRoute, settings);
247
- this.activeRouteDictionary[clonedRoute.id] = {
248
- ...this.activeRouteDictionary[clonedRoute.id],
249
- executionPromise: newExecutionPromise,
250
- };
251
- return newExecutionPromise;
252
- };
253
- this.executeSteps = async (signer, route, settings) => {
254
- const config = this.configService.getConfig();
255
- const updatedExecutionData = {
256
- route,
257
- executors: [],
258
- settings: { ...config.defaultExecutionSettings, ...settings },
259
- };
260
- this.activeRouteDictionary[route.id].executionData = {
261
- ...updatedExecutionData,
262
- };
263
- const { executionData } = this.activeRouteDictionary[route.id];
264
- const statusManager = new StatusManager(route, executionData.settings, (route) => {
265
- if (this.activeRouteDictionary[route.id]) {
266
- executionData.route = route;
267
- }
268
- });
269
- // Loop over steps and execute them
270
- for (let index = 0; index < route.steps.length; index++) {
271
- const { executionData } = this.activeRouteDictionary[route.id];
272
- // Check if execution has stopped in the meantime
273
- if (!executionData) {
274
- break;
275
- }
276
- const step = route.steps[index];
277
- const previousStep = route.steps[index - 1];
278
- // Check if the step is already done
279
- //
280
- if (step.execution?.status === 'DONE') {
281
- continue;
282
- }
283
- // Update amount using output of previous execution. In the future this should be handled by calling `updateRoute`
284
- if (previousStep?.execution?.toAmount) {
285
- step.action.fromAmount = previousStep.execution.toAmount;
286
- }
287
- try {
288
- const stepExecutor = new StepExecutor(statusManager, executionData.settings);
289
- executionData.executors.push(stepExecutor);
290
- // Check if we want to execute this step in the background
291
- this.updateRouteExecution(route, executionData.settings);
292
- const executedStep = await stepExecutor.executeStep(signer, step);
293
- // We may reach this point if user interaction isn't allowed. We want to stop execution until we resume it
294
- if (executedStep.execution?.status !== 'DONE') {
295
- this.stopExecution(route);
296
- }
297
- // Execution stopped during the current step, we don't want to continue to the next step so we return already
298
- if (stepExecutor.executionStopped) {
299
- return route;
300
- }
301
- }
302
- catch (e) {
303
- this.stopExecution(route);
304
- throw e;
305
- }
306
- }
307
- // Clean up after the execution
308
- delete this.activeRouteDictionary[route.id];
309
- return route;
310
- };
311
- /**
312
- * Update the ExecutionSettings for an active route.
313
- * @param {ExecutionSettings} settings - An object with execution settings.
314
- * @param {Route} route - The active route that gets the new execution settings.
315
- * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
316
- */
317
- this.updateExecutionSettings = (settings, route) => {
318
- if (!this.activeRouteDictionary[route.id]) {
319
- throw new ValidationError("Can't set ExecutionSettings for the inactive route.");
320
- }
321
- const config = this.configService.getConfig();
322
- this.activeRouteDictionary[route.id].executionData.settings = {
323
- ...config.defaultExecutionSettings,
324
- ...settings,
325
- };
326
- };
327
- /**
328
- * Get the list of active routes.
329
- * @return {Route[]} A list of routes.
330
- */
331
- this.getActiveRoutes = () => {
332
- return Object.values(this.activeRouteDictionary).map((dict) => dict.executionData.route);
333
- };
334
- /**
335
- * Return the current route information for given route. The route has to be active.
336
- * @param {Route} route - A route object.
337
- * @return {Route} The updated route.
338
- */
339
- this.getActiveRoute = (route) => {
340
- return this.activeRouteDictionary[route.id]?.executionData.route;
341
- };
342
137
  /**
343
138
  * Returns the balances of a specific token a wallet holds across all aggregated chains.
344
139
  * @param {string} walletAddress - A wallet address.
@@ -421,11 +216,6 @@ export default class LIFI {
421
216
  this.revokeTokenApproval = (request) => {
422
217
  return revokeTokenApproval(request);
423
218
  };
424
- this.configService = ConfigService.getInstance();
425
- if (configUpdate) {
426
- // Update API urls before we request chains
427
- this.configService.updateConfig(configUpdate);
428
- }
429
219
  this.chainsService = ChainsService.getInstance();
430
220
  this.chainsService.getChains().then((chains) => {
431
221
  this.configService.updateChains(chains);
@@ -1,11 +1,10 @@
1
1
  import { FallbackProvider } from '@ethersproject/providers';
2
- import { ChainId, ChainKey, ContractCallQuoteRequest, ExtendedChain, GasRecommendationRequest, GasRecommendationResponse, GetStatusRequest, PossibilitiesRequest, PossibilitiesResponse, QuoteRequest, RequestOptions, Route, RoutesRequest, RoutesResponse, StatusResponse, Step, Token, TokenAmount, TokensRequest, TokensResponse, ToolsRequest, ToolsResponse } from '@lifi/types';
2
+ import { ChainId, ChainKey, ContractCallQuoteRequest, ExtendedChain, GasRecommendationRequest, GasRecommendationResponse, GetStatusRequest, PossibilitiesRequest, PossibilitiesResponse, QuoteRequest, RequestOptions, RoutesRequest, RoutesResponse, StatusResponse, Step, Token, TokenAmount, TokensRequest, TokensResponse, ToolsRequest, ToolsResponse } from '@lifi/types';
3
3
  import { Signer } from 'ethers';
4
4
  import { ApproveTokenRequest, RevokeApprovalRequest } from './allowance';
5
- import { Config, ConfigUpdate, ExecutionSettings, RevokeTokenData } from './types';
6
- export default class LIFI {
7
- private activeRouteDictionary;
8
- private configService;
5
+ import { RouteExecutionManager } from './execution/RouteExecutionManager';
6
+ import { Config, ConfigUpdate, RevokeTokenData } from './types';
7
+ export declare class LiFi extends RouteExecutionManager {
9
8
  private chainsService;
10
9
  constructor(configUpdate?: ConfigUpdate);
11
10
  /**
@@ -101,61 +100,6 @@ export default class LIFI {
101
100
  * @throws {LifiError} Throws a LifiError if request fails.
102
101
  */
103
102
  getGasRecommendation: (request: GasRecommendationRequest, options?: RequestOptions) => Promise<GasRecommendationResponse>;
104
- /**
105
- * Stops the execution of an active route.
106
- * @param {Route} route - A route that is currently in execution.
107
- * @return {Route} The stopped route.
108
- */
109
- stopExecution: (route: Route) => Route;
110
- /**
111
- * Executes a route until a user interaction is necessary (signing transactions, etc.) and then halts until the route is resumed.
112
- * @param {Route} route - A route that is currently in execution.
113
- * @deprecated use updateRouteExecution instead.
114
- */
115
- moveExecutionToBackground: (route: Route) => void;
116
- /**
117
- * Updates route execution to background or foreground state.
118
- * @param {Route} route - A route that is currently in execution.
119
- * @param {boolean} settings - An object with execution settings.
120
- */
121
- updateRouteExecution: (route: Route, settings: Pick<ExecutionSettings, 'executeInBackground'>) => void;
122
- /**
123
- * Execute a route.
124
- * @param {Signer} signer - The signer required to send the transactions.
125
- * @param {Route} route - The route that should be executed. Cannot be an active route.
126
- * @param {ExecutionSettings} settings - An object containing settings and callbacks.
127
- * @return {Promise<Route>} The executed route.
128
- * @throws {LifiError} Throws a LifiError if the execution fails.
129
- */
130
- executeRoute: (signer: Signer, route: Route, settings?: ExecutionSettings) => Promise<Route>;
131
- /**
132
- * Resume the execution of a route that has been stopped or had an error while executing.
133
- * @param {Signer} signer - The signer required to send the transactions.
134
- * @param {Route} route - The route that is to be executed. Cannot be an active route.
135
- * @param {ExecutionSettings} settings - An object containing settings and callbacks.
136
- * @return {Promise<Route>} The executed route.
137
- * @throws {LifiError} Throws a LifiError if the execution fails.
138
- */
139
- resumeRoute: (signer: Signer, route: Route, settings?: ExecutionSettings) => Promise<Route>;
140
- private executeSteps;
141
- /**
142
- * Update the ExecutionSettings for an active route.
143
- * @param {ExecutionSettings} settings - An object with execution settings.
144
- * @param {Route} route - The active route that gets the new execution settings.
145
- * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
146
- */
147
- updateExecutionSettings: (settings: ExecutionSettings, route: Route) => void;
148
- /**
149
- * Get the list of active routes.
150
- * @return {Route[]} A list of routes.
151
- */
152
- getActiveRoutes: () => Route[];
153
- /**
154
- * Return the current route information for given route. The route has to be active.
155
- * @param {Route} route - A route object.
156
- * @return {Route} The updated route.
157
- */
158
- getActiveRoute: (route: Route) => Route | undefined;
159
103
  /**
160
104
  * Returns the balances of a specific token a wallet holds across all aggregated chains.
161
105
  * @param {string} walletAddress - A wallet address.
@@ -26,22 +26,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.LiFi = void 0;
29
30
  const allowance_1 = require("./allowance");
30
31
  const balance = __importStar(require("./balance"));
31
32
  const connectors_1 = require("./connectors");
32
- const StatusManager_1 = require("./execution/StatusManager");
33
- const StepExecutor_1 = require("./execution/StepExecutor");
33
+ const RouteExecutionManager_1 = require("./execution/RouteExecutionManager");
34
34
  const helpers_1 = require("./helpers");
35
35
  const ApiService_1 = __importDefault(require("./services/ApiService"));
36
36
  const ChainsService_1 = __importDefault(require("./services/ChainsService"));
37
- const ConfigService_1 = __importDefault(require("./services/ConfigService"));
38
37
  const typeguards_1 = require("./typeguards");
39
38
  const errors_1 = require("./utils/errors");
40
- const preRestart_1 = require("./utils/preRestart");
41
39
  const version_1 = require("./version");
42
- class LIFI {
40
+ class LiFi extends RouteExecutionManager_1.RouteExecutionManager {
43
41
  constructor(configUpdate) {
44
- this.activeRouteDictionary = {};
42
+ super(configUpdate);
45
43
  /**
46
44
  * Get the current configuration of the SDK
47
45
  * @return {Config} - The config object
@@ -165,208 +163,6 @@ class LIFI {
165
163
  this.getGasRecommendation = async (request, options) => {
166
164
  return ApiService_1.default.getGasRecommendation(request, options);
167
165
  };
168
- /**
169
- * Stops the execution of an active route.
170
- * @param {Route} route - A route that is currently in execution.
171
- * @return {Route} The stopped route.
172
- */
173
- this.stopExecution = (route) => {
174
- if (!this.activeRouteDictionary[route.id]) {
175
- return route;
176
- }
177
- const { executionData } = this.activeRouteDictionary[route.id];
178
- for (const executor of executionData.executors) {
179
- executor.setInteraction({
180
- allowInteraction: false,
181
- allowUpdates: false,
182
- stopExecution: true,
183
- });
184
- }
185
- delete this.activeRouteDictionary[route.id];
186
- return route;
187
- };
188
- /**
189
- * Executes a route until a user interaction is necessary (signing transactions, etc.) and then halts until the route is resumed.
190
- * @param {Route} route - A route that is currently in execution.
191
- * @deprecated use updateRouteExecution instead.
192
- */
193
- this.moveExecutionToBackground = (route) => {
194
- const { executionData } = this.activeRouteDictionary[route.id];
195
- if (!executionData) {
196
- return;
197
- }
198
- for (const executor of executionData.executors) {
199
- executor.setInteraction({ allowInteraction: false, allowUpdates: true });
200
- }
201
- executionData.settings = {
202
- ...executionData.settings,
203
- executeInBackground: true,
204
- };
205
- };
206
- /**
207
- * Updates route execution to background or foreground state.
208
- * @param {Route} route - A route that is currently in execution.
209
- * @param {boolean} settings - An object with execution settings.
210
- */
211
- this.updateRouteExecution = (route, settings) => {
212
- const { executionData } = this.activeRouteDictionary[route.id];
213
- if (!executionData) {
214
- return;
215
- }
216
- for (const executor of executionData.executors) {
217
- executor.setInteraction({
218
- allowInteraction: !settings.executeInBackground,
219
- allowUpdates: true,
220
- });
221
- }
222
- // Update active route settings so we know what the current state of execution is
223
- executionData.settings = {
224
- ...executionData.settings,
225
- ...settings,
226
- };
227
- };
228
- /**
229
- * Execute a route.
230
- * @param {Signer} signer - The signer required to send the transactions.
231
- * @param {Route} route - The route that should be executed. Cannot be an active route.
232
- * @param {ExecutionSettings} settings - An object containing settings and callbacks.
233
- * @return {Promise<Route>} The executed route.
234
- * @throws {LifiError} Throws a LifiError if the execution fails.
235
- */
236
- this.executeRoute = async (signer, route, settings) => {
237
- // Deep clone to prevent side effects
238
- const clonedRoute = structuredClone(route);
239
- // Check if route is already running
240
- if (this.activeRouteDictionary[clonedRoute.id]) {
241
- // TODO: maybe inform user why nothing happens?
242
- return this.activeRouteDictionary[clonedRoute.id].executionPromise;
243
- }
244
- const executionPromise = this.executeSteps(signer, clonedRoute, settings);
245
- this.activeRouteDictionary[clonedRoute.id] = {
246
- ...this.activeRouteDictionary[clonedRoute.id],
247
- executionPromise,
248
- };
249
- return executionPromise;
250
- };
251
- /**
252
- * Resume the execution of a route that has been stopped or had an error while executing.
253
- * @param {Signer} signer - The signer required to send the transactions.
254
- * @param {Route} route - The route that is to be executed. Cannot be an active route.
255
- * @param {ExecutionSettings} settings - An object containing settings and callbacks.
256
- * @return {Promise<Route>} The executed route.
257
- * @throws {LifiError} Throws a LifiError if the execution fails.
258
- */
259
- this.resumeRoute = async (signer, route, settings) => {
260
- // Deep clone to prevent side effects
261
- const clonedRoute = structuredClone(route);
262
- const { executionData, executionPromise } = this.activeRouteDictionary[clonedRoute.id];
263
- if (executionData) {
264
- const executionHalted = executionData.executors.some((executor) => executor.executionStopped);
265
- if (!executionHalted) {
266
- // Check if we want to resume route execution in the background
267
- this.updateRouteExecution(route, {
268
- executeInBackground: settings?.executeInBackground,
269
- });
270
- return executionPromise;
271
- }
272
- }
273
- (0, preRestart_1.handlePreRestart)(clonedRoute);
274
- const newExecutionPromise = this.executeSteps(signer, clonedRoute, settings);
275
- this.activeRouteDictionary[clonedRoute.id] = {
276
- ...this.activeRouteDictionary[clonedRoute.id],
277
- executionPromise: newExecutionPromise,
278
- };
279
- return newExecutionPromise;
280
- };
281
- this.executeSteps = async (signer, route, settings) => {
282
- const config = this.configService.getConfig();
283
- const updatedExecutionData = {
284
- route,
285
- executors: [],
286
- settings: { ...config.defaultExecutionSettings, ...settings },
287
- };
288
- this.activeRouteDictionary[route.id].executionData = {
289
- ...updatedExecutionData,
290
- };
291
- const { executionData } = this.activeRouteDictionary[route.id];
292
- const statusManager = new StatusManager_1.StatusManager(route, executionData.settings, (route) => {
293
- if (this.activeRouteDictionary[route.id]) {
294
- executionData.route = route;
295
- }
296
- });
297
- // Loop over steps and execute them
298
- for (let index = 0; index < route.steps.length; index++) {
299
- const { executionData } = this.activeRouteDictionary[route.id];
300
- // Check if execution has stopped in the meantime
301
- if (!executionData) {
302
- break;
303
- }
304
- const step = route.steps[index];
305
- const previousStep = route.steps[index - 1];
306
- // Check if the step is already done
307
- //
308
- if (step.execution?.status === 'DONE') {
309
- continue;
310
- }
311
- // Update amount using output of previous execution. In the future this should be handled by calling `updateRoute`
312
- if (previousStep?.execution?.toAmount) {
313
- step.action.fromAmount = previousStep.execution.toAmount;
314
- }
315
- try {
316
- const stepExecutor = new StepExecutor_1.StepExecutor(statusManager, executionData.settings);
317
- executionData.executors.push(stepExecutor);
318
- // Check if we want to execute this step in the background
319
- this.updateRouteExecution(route, executionData.settings);
320
- const executedStep = await stepExecutor.executeStep(signer, step);
321
- // We may reach this point if user interaction isn't allowed. We want to stop execution until we resume it
322
- if (executedStep.execution?.status !== 'DONE') {
323
- this.stopExecution(route);
324
- }
325
- // Execution stopped during the current step, we don't want to continue to the next step so we return already
326
- if (stepExecutor.executionStopped) {
327
- return route;
328
- }
329
- }
330
- catch (e) {
331
- this.stopExecution(route);
332
- throw e;
333
- }
334
- }
335
- // Clean up after the execution
336
- delete this.activeRouteDictionary[route.id];
337
- return route;
338
- };
339
- /**
340
- * Update the ExecutionSettings for an active route.
341
- * @param {ExecutionSettings} settings - An object with execution settings.
342
- * @param {Route} route - The active route that gets the new execution settings.
343
- * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
344
- */
345
- this.updateExecutionSettings = (settings, route) => {
346
- if (!this.activeRouteDictionary[route.id]) {
347
- throw new errors_1.ValidationError("Can't set ExecutionSettings for the inactive route.");
348
- }
349
- const config = this.configService.getConfig();
350
- this.activeRouteDictionary[route.id].executionData.settings = {
351
- ...config.defaultExecutionSettings,
352
- ...settings,
353
- };
354
- };
355
- /**
356
- * Get the list of active routes.
357
- * @return {Route[]} A list of routes.
358
- */
359
- this.getActiveRoutes = () => {
360
- return Object.values(this.activeRouteDictionary).map((dict) => dict.executionData.route);
361
- };
362
- /**
363
- * Return the current route information for given route. The route has to be active.
364
- * @param {Route} route - A route object.
365
- * @return {Route} The updated route.
366
- */
367
- this.getActiveRoute = (route) => {
368
- return this.activeRouteDictionary[route.id]?.executionData.route;
369
- };
370
166
  /**
371
167
  * Returns the balances of a specific token a wallet holds across all aggregated chains.
372
168
  * @param {string} walletAddress - A wallet address.
@@ -449,11 +245,6 @@ class LIFI {
449
245
  this.revokeTokenApproval = (request) => {
450
246
  return (0, allowance_1.revokeTokenApproval)(request);
451
247
  };
452
- this.configService = ConfigService_1.default.getInstance();
453
- if (configUpdate) {
454
- // Update API urls before we request chains
455
- this.configService.updateConfig(configUpdate);
456
- }
457
248
  this.chainsService = ChainsService_1.default.getInstance();
458
249
  this.chainsService.getChains().then((chains) => {
459
250
  this.configService.updateChains(chains);
@@ -461,4 +252,4 @@ class LIFI {
461
252
  (0, helpers_1.checkPackageUpdates)(version_1.name, version_1.version, configUpdate?.disableVersionCheck);
462
253
  }
463
254
  }
464
- exports.default = LIFI;
255
+ exports.LiFi = LiFi;