@lifi/sdk 2.0.0-beta.1 → 2.0.0-beta.11

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 (81) hide show
  1. package/dist/{Lifi.d.ts → LiFi.d.ts} +19 -63
  2. package/dist/{Lifi.js → LiFi.js} +19 -212
  3. package/dist/allowance/checkAllowance.d.ts +2 -2
  4. package/dist/allowance/checkAllowance.js +16 -2
  5. package/dist/allowance/utils.d.ts +2 -1
  6. package/dist/allowance/utils.js +7 -2
  7. package/dist/balance/checkBalance.d.ts +2 -2
  8. package/dist/balance/index.d.ts +1 -1
  9. package/dist/cjs/{Lifi.d.ts → LiFi.d.ts} +19 -63
  10. package/dist/cjs/{Lifi.js → LiFi.js} +22 -213
  11. package/dist/cjs/allowance/checkAllowance.d.ts +2 -2
  12. package/dist/cjs/allowance/checkAllowance.js +16 -2
  13. package/dist/cjs/allowance/utils.d.ts +2 -1
  14. package/dist/cjs/allowance/utils.js +7 -2
  15. package/dist/cjs/balance/checkBalance.d.ts +2 -2
  16. package/dist/cjs/balance/index.d.ts +1 -1
  17. package/dist/cjs/connectors.d.ts +1 -1
  18. package/dist/cjs/connectors.js +7 -9
  19. package/dist/cjs/execution/RouteExecutionManager.d.ts +65 -0
  20. package/dist/cjs/execution/RouteExecutionManager.js +220 -0
  21. package/dist/cjs/execution/StatusManager.d.ts +12 -12
  22. package/dist/cjs/execution/StatusManager.js +5 -5
  23. package/dist/cjs/execution/{ExecutionManager.d.ts → StepExecutionManager.d.ts} +1 -1
  24. package/dist/cjs/execution/{ExecutionManager.js → StepExecutionManager.js} +11 -3
  25. package/dist/cjs/execution/StepExecutor.d.ts +4 -4
  26. package/dist/cjs/execution/StepExecutor.js +17 -9
  27. package/dist/cjs/execution/stepComparison.d.ts +3 -3
  28. package/dist/cjs/execution/stepComparison.js +2 -5
  29. package/dist/cjs/execution/switchChain.d.ts +2 -2
  30. package/dist/cjs/execution/utils.d.ts +3 -3
  31. package/dist/cjs/helpers.d.ts +5 -7
  32. package/dist/cjs/helpers.js +10 -30
  33. package/dist/cjs/index.d.ts +1 -2
  34. package/dist/cjs/index.js +3 -6
  35. package/dist/cjs/request.d.ts +9 -0
  36. package/dist/cjs/request.js +60 -0
  37. package/dist/cjs/services/ApiService.d.ts +8 -7
  38. package/dist/cjs/services/ApiService.js +52 -31
  39. package/dist/cjs/services/ConfigService.js +6 -2
  40. package/dist/cjs/typeguards.d.ts +2 -2
  41. package/dist/cjs/types/internal.types.d.ts +54 -10
  42. package/dist/cjs/utils/errors.d.ts +20 -1
  43. package/dist/cjs/utils/errors.js +23 -1
  44. package/dist/cjs/utils/parseError.d.ts +4 -4
  45. package/dist/cjs/utils/parseError.js +29 -12
  46. package/dist/cjs/utils/utils.d.ts +3 -3
  47. package/dist/cjs/version.d.ts +1 -1
  48. package/dist/cjs/version.js +1 -1
  49. package/dist/connectors.d.ts +1 -1
  50. package/dist/connectors.js +7 -9
  51. package/dist/execution/RouteExecutionManager.d.ts +65 -0
  52. package/dist/execution/RouteExecutionManager.js +213 -0
  53. package/dist/execution/StatusManager.d.ts +12 -12
  54. package/dist/execution/StatusManager.js +5 -5
  55. package/dist/execution/{ExecutionManager.d.ts → StepExecutionManager.d.ts} +1 -1
  56. package/dist/execution/{ExecutionManager.js → StepExecutionManager.js} +9 -1
  57. package/dist/execution/StepExecutor.d.ts +4 -4
  58. package/dist/execution/StepExecutor.js +17 -9
  59. package/dist/execution/stepComparison.d.ts +3 -3
  60. package/dist/execution/stepComparison.js +2 -5
  61. package/dist/execution/switchChain.d.ts +2 -2
  62. package/dist/execution/utils.d.ts +3 -3
  63. package/dist/helpers.d.ts +5 -7
  64. package/dist/helpers.js +9 -29
  65. package/dist/index.d.ts +1 -2
  66. package/dist/index.js +1 -3
  67. package/dist/request.d.ts +9 -0
  68. package/dist/request.js +53 -0
  69. package/dist/services/ApiService.d.ts +8 -7
  70. package/dist/services/ApiService.js +43 -22
  71. package/dist/services/ConfigService.js +7 -2
  72. package/dist/typeguards.d.ts +2 -2
  73. package/dist/types/internal.types.d.ts +54 -10
  74. package/dist/utils/errors.d.ts +20 -1
  75. package/dist/utils/errors.js +22 -0
  76. package/dist/utils/parseError.d.ts +4 -4
  77. package/dist/utils/parseError.js +30 -13
  78. package/dist/utils/utils.d.ts +3 -3
  79. package/dist/version.d.ts +1 -1
  80. package/dist/version.js +1 -1
  81. package/package.json +15 -15
@@ -1,7 +1,7 @@
1
- import { providers } from 'ethers';
2
- import { ChainId } from './types';
3
- import ConfigService from './services/ConfigService';
1
+ import { FallbackProvider, StaticJsonRpcProvider, } from '@ethersproject/providers';
4
2
  import { getRandomNumber } from './helpers';
3
+ import ConfigService from './services/ConfigService';
4
+ import { ChainId } from './types';
5
5
  import { ServerError } from './utils/errors';
6
6
  // cached providers
7
7
  const chainProviders = {};
@@ -33,17 +33,15 @@ const getRandomProvider = (providerList) => {
33
33
  export const getRpcProvider = async (chainId, archive = false) => {
34
34
  if (archive && archiveRpcs[chainId]) {
35
35
  // return archive PRC, but don't cache it
36
- return new providers.FallbackProvider([
37
- new providers.StaticJsonRpcProvider(await getRpcUrl(chainId, archive), chainId),
36
+ return new FallbackProvider([
37
+ new StaticJsonRpcProvider(await getRpcUrl(chainId, archive), chainId),
38
38
  ]);
39
39
  }
40
- if (!chainProviders[chainId]) {
40
+ if (!chainProviders[chainId]?.length) {
41
41
  chainProviders[chainId] = [];
42
42
  const urls = await getRpcUrls(chainId, archive);
43
43
  urls.forEach((url) => {
44
- chainProviders[chainId].push(new providers.FallbackProvider([
45
- new providers.StaticJsonRpcProvider(url, chainId),
46
- ]));
44
+ chainProviders[chainId].push(new FallbackProvider([new StaticJsonRpcProvider(url, chainId)]));
47
45
  });
48
46
  }
49
47
  if (!chainProviders[chainId].length) {
@@ -0,0 +1,65 @@
1
+ import { Route } from '@lifi/types';
2
+ import { Signer } from 'ethers';
3
+ import ConfigService from '../services/ConfigService';
4
+ import { ConfigUpdate, ExecutionSettings } from '../types';
5
+ export declare class RouteExecutionManager {
6
+ private executionDictionary;
7
+ private executionPromiseDictionary;
8
+ protected configService: ConfigService;
9
+ constructor(configUpdate?: ConfigUpdate);
10
+ /**
11
+ * Execute a route.
12
+ * @param {Signer} signer - The signer required to send the transactions.
13
+ * @param {Route} route - The route that should be executed. Cannot be an active route.
14
+ * @param {ExecutionSettings} settings - An object containing settings and callbacks.
15
+ * @return {Promise<Route>} The executed route.
16
+ * @throws {LifiError} Throws a LifiError if the execution fails.
17
+ */
18
+ executeRoute: (signer: Signer, route: Route, settings?: ExecutionSettings) => Promise<Route>;
19
+ /**
20
+ * Resume the execution of a route that has been stopped or had an error while executing.
21
+ * @param {Signer} signer - The signer required to send the transactions.
22
+ * @param {Route} route - The route that is to be executed. Cannot be an active route.
23
+ * @param {ExecutionSettings} settings - An object containing settings and callbacks.
24
+ * @return {Promise<Route>} The executed route.
25
+ * @throws {LifiError} Throws a LifiError if the execution fails.
26
+ */
27
+ resumeRoute: (signer: Signer, route: Route, settings?: ExecutionSettings) => Promise<Route>;
28
+ private executeSteps;
29
+ /**
30
+ * Updates route execution to background or foreground state.
31
+ * @param {Route} route - A route that is currently in execution.
32
+ * @param {boolean} settings - An object with execution settings.
33
+ */
34
+ updateRouteExecution: (route: Route, settings: Pick<ExecutionSettings, 'executeInBackground'>) => void;
35
+ /**
36
+ * Update the ExecutionSettings for an active route.
37
+ * @param {ExecutionSettings} settings - An object with execution settings.
38
+ * @param {Route} route - The active route that gets the new execution settings.
39
+ * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
40
+ */
41
+ updateExecutionSettings: (settings: ExecutionSettings, route: Route) => void;
42
+ /**
43
+ * Executes a route until a user interaction is necessary (signing transactions, etc.) and then halts until the route is resumed.
44
+ * @param {Route} route - A route that is currently in execution.
45
+ * @deprecated use updateRouteExecution instead.
46
+ */
47
+ moveExecutionToBackground: (route: Route) => void;
48
+ /**
49
+ * Stops the execution of an active route.
50
+ * @param {Route} route - A route that is currently in execution.
51
+ * @return {Route} The stopped route.
52
+ */
53
+ stopExecution: (route: Route) => Route;
54
+ /**
55
+ * Get the list of active routes.
56
+ * @return {Route[]} A list of routes.
57
+ */
58
+ getActiveRoutes: () => Route[];
59
+ /**
60
+ * Return the current route information for given route. The route has to be active.
61
+ * @param {Route} route - A route object.
62
+ * @return {Route} The updated route.
63
+ */
64
+ getActiveRoute: (route: Route) => Route | undefined;
65
+ }
@@ -0,0 +1,213 @@
1
+ import ConfigService from '../services/ConfigService';
2
+ import { ValidationError } from '../utils/errors';
3
+ import { handlePreRestart } from '../utils/preRestart';
4
+ import { StatusManager } from './StatusManager';
5
+ import { StepExecutor } from './StepExecutor';
6
+ export class RouteExecutionManager {
7
+ constructor(configUpdate) {
8
+ this.executionDictionary = {};
9
+ this.executionPromiseDictionary = {};
10
+ /**
11
+ * Execute a route.
12
+ * @param {Signer} signer - The signer required to send the transactions.
13
+ * @param {Route} route - The route that should be executed. Cannot be an active route.
14
+ * @param {ExecutionSettings} settings - An object containing settings and callbacks.
15
+ * @return {Promise<Route>} The executed route.
16
+ * @throws {LifiError} Throws a LifiError if the execution fails.
17
+ */
18
+ this.executeRoute = async (signer, route, settings) => {
19
+ // Deep clone to prevent side effects
20
+ const clonedRoute = structuredClone(route);
21
+ let executionPromise = this.executionPromiseDictionary[clonedRoute.id];
22
+ // Check if route is already running
23
+ if (executionPromise) {
24
+ return executionPromise;
25
+ }
26
+ executionPromise = this.executeSteps(signer, clonedRoute, settings);
27
+ this.executionPromiseDictionary[clonedRoute.id] = executionPromise;
28
+ return executionPromise;
29
+ };
30
+ /**
31
+ * Resume the execution of a route that has been stopped or had an error while executing.
32
+ * @param {Signer} signer - The signer required to send the transactions.
33
+ * @param {Route} route - The route that is to be executed. Cannot be an active route.
34
+ * @param {ExecutionSettings} settings - An object containing settings and callbacks.
35
+ * @return {Promise<Route>} The executed route.
36
+ * @throws {LifiError} Throws a LifiError if the execution fails.
37
+ */
38
+ this.resumeRoute = async (signer, route, settings) => {
39
+ // Deep clone to prevent side effects
40
+ const clonedRoute = structuredClone(route);
41
+ const execution = this.executionDictionary[clonedRoute.id];
42
+ if (execution) {
43
+ const executionHalted = execution.executors.some((executor) => executor.executionStopped);
44
+ if (!executionHalted) {
45
+ // Check if we want to resume route execution in the background
46
+ this.updateRouteExecution(route, {
47
+ executeInBackground: settings?.executeInBackground,
48
+ });
49
+ const executionPromise = this.executionPromiseDictionary[clonedRoute.id];
50
+ return executionPromise ?? clonedRoute;
51
+ }
52
+ }
53
+ handlePreRestart(clonedRoute);
54
+ const executionPromise = this.executeSteps(signer, clonedRoute, settings);
55
+ this.executionPromiseDictionary[clonedRoute.id] = executionPromise;
56
+ return executionPromise;
57
+ };
58
+ this.executeSteps = async (signer, route, settings) => {
59
+ const config = this.configService.getConfig();
60
+ const execution = {
61
+ route,
62
+ executors: [],
63
+ settings: { ...config.defaultExecutionSettings, ...settings },
64
+ };
65
+ this.executionDictionary[route.id] = execution;
66
+ const statusManager = new StatusManager(route, execution.settings, (route) => {
67
+ if (this.executionDictionary[route.id]) {
68
+ execution.route = route;
69
+ }
70
+ });
71
+ // Loop over steps and execute them
72
+ for (let index = 0; index < route.steps.length; index++) {
73
+ const execution = this.executionDictionary[route.id];
74
+ // Check if execution has stopped in the meantime
75
+ if (!execution) {
76
+ break;
77
+ }
78
+ const step = route.steps[index];
79
+ const previousStep = route.steps[index - 1];
80
+ // Check if the step is already done
81
+ //
82
+ if (step.execution?.status === 'DONE') {
83
+ continue;
84
+ }
85
+ // Update amount using output of previous execution. In the future this should be handled by calling `updateRoute`
86
+ if (previousStep?.execution?.toAmount) {
87
+ step.action.fromAmount = previousStep.execution.toAmount;
88
+ }
89
+ try {
90
+ const stepExecutor = new StepExecutor(statusManager, execution.settings);
91
+ execution.executors.push(stepExecutor);
92
+ // Check if we want to execute this step in the background
93
+ this.updateRouteExecution(route, execution.settings);
94
+ const executedStep = await stepExecutor.executeStep(signer, step);
95
+ // We may reach this point if user interaction isn't allowed. We want to stop execution until we resume it
96
+ if (executedStep.execution?.status !== 'DONE') {
97
+ this.stopExecution(route);
98
+ }
99
+ // Execution stopped during the current step, we don't want to continue to the next step so we return already
100
+ if (stepExecutor.executionStopped) {
101
+ return route;
102
+ }
103
+ }
104
+ catch (e) {
105
+ this.stopExecution(route);
106
+ throw e;
107
+ }
108
+ }
109
+ // Clean up after the execution
110
+ delete this.executionDictionary[route.id];
111
+ return route;
112
+ };
113
+ /**
114
+ * Updates route execution to background or foreground state.
115
+ * @param {Route} route - A route that is currently in execution.
116
+ * @param {boolean} settings - An object with execution settings.
117
+ */
118
+ this.updateRouteExecution = (route, settings) => {
119
+ const execution = this.executionDictionary[route.id];
120
+ if (!execution) {
121
+ return;
122
+ }
123
+ for (const executor of execution.executors) {
124
+ executor.setInteraction({
125
+ allowInteraction: !settings.executeInBackground,
126
+ allowUpdates: true,
127
+ });
128
+ }
129
+ // Update active route settings so we know what the current state of execution is
130
+ execution.settings = {
131
+ ...execution.settings,
132
+ ...settings,
133
+ };
134
+ };
135
+ /**
136
+ * Update the ExecutionSettings for an active route.
137
+ * @param {ExecutionSettings} settings - An object with execution settings.
138
+ * @param {Route} route - The active route that gets the new execution settings.
139
+ * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
140
+ */
141
+ this.updateExecutionSettings = (settings, route) => {
142
+ const execution = this.executionDictionary[route.id];
143
+ if (!execution) {
144
+ throw new ValidationError("Can't set ExecutionSettings for the inactive route.");
145
+ }
146
+ const config = this.configService.getConfig();
147
+ execution.settings = {
148
+ ...config.defaultExecutionSettings,
149
+ ...settings,
150
+ };
151
+ };
152
+ /**
153
+ * Executes a route until a user interaction is necessary (signing transactions, etc.) and then halts until the route is resumed.
154
+ * @param {Route} route - A route that is currently in execution.
155
+ * @deprecated use updateRouteExecution instead.
156
+ */
157
+ this.moveExecutionToBackground = (route) => {
158
+ const execution = this.executionDictionary[route.id];
159
+ if (!execution) {
160
+ return;
161
+ }
162
+ for (const executor of execution.executors) {
163
+ executor.setInteraction({ allowInteraction: false, allowUpdates: true });
164
+ }
165
+ execution.settings = {
166
+ ...execution.settings,
167
+ executeInBackground: true,
168
+ };
169
+ };
170
+ /**
171
+ * Stops the execution of an active route.
172
+ * @param {Route} route - A route that is currently in execution.
173
+ * @return {Route} The stopped route.
174
+ */
175
+ this.stopExecution = (route) => {
176
+ const execution = this.executionDictionary[route.id];
177
+ if (!execution) {
178
+ return route;
179
+ }
180
+ for (const executor of execution.executors) {
181
+ executor.setInteraction({
182
+ allowInteraction: false,
183
+ allowUpdates: false,
184
+ stopExecution: true,
185
+ });
186
+ }
187
+ delete this.executionDictionary[route.id];
188
+ return route;
189
+ };
190
+ /**
191
+ * Get the list of active routes.
192
+ * @return {Route[]} A list of routes.
193
+ */
194
+ this.getActiveRoutes = () => {
195
+ return Object.values(this.executionDictionary)
196
+ .map((dict) => dict?.route)
197
+ .filter(Boolean);
198
+ };
199
+ /**
200
+ * Return the current route information for given route. The route has to be active.
201
+ * @param {Route} route - A route object.
202
+ * @return {Route} The updated route.
203
+ */
204
+ this.getActiveRoute = (route) => {
205
+ return this.executionDictionary[route.id]?.route;
206
+ };
207
+ this.configService = ConfigService.getInstance();
208
+ if (configUpdate) {
209
+ // Update API urls before we request chains
210
+ this.configService.updateConfig(configUpdate);
211
+ }
212
+ }
213
+ }
@@ -1,4 +1,4 @@
1
- import { Execution, InternalExecutionSettings, Process, ProcessType, Route, Status, Step, Token } from '../types';
1
+ import { Execution, InternalExecutionSettings, LifiStep, Process, ProcessType, Route, Status, Token } from '../types';
2
2
  interface Receipt {
3
3
  fromAmount?: string;
4
4
  toAmount?: string;
@@ -26,43 +26,43 @@ export declare class StatusManager {
26
26
  constructor(route: Route, settings: InternalExecutionSettings, internalUpdateRouteCallback: InternalUpdateRouteCallback);
27
27
  /**
28
28
  * Initializes the execution object of a Step.
29
- * @param {Step} step The current step in execution
29
+ * @param {LifiStep} step The current step in execution
30
30
  * @return {Execution} The initialized execution object for this step and a function to update this step
31
31
  */
32
- initExecutionObject: (step: Step) => Execution;
32
+ initExecutionObject: (step: LifiStep) => Execution;
33
33
  /**
34
34
  * Updates the execution object of a Step.
35
- * @param {Step} step The current step in execution
35
+ * @param {LifiStep} step The current step in execution
36
36
  * @param {Status} status The status for the execution
37
37
  * @param {Receipt} receipt Optional. Information about received tokens
38
38
  * @return {Step} The step with the updated execution object
39
39
  */
40
- updateExecution(step: Step, status: Status, receipt?: Receipt): Step;
40
+ updateExecution(step: LifiStep, status: Status, receipt?: Receipt): LifiStep;
41
41
  /**
42
42
  * Create and push a new process into the execution.
43
43
  * @param {ProcessType} type Type of the process. Used to identify already existing processes.
44
- * @param {Step} step The step that should contain the new process.
44
+ * @param {LifiStep} step The step that should contain the new process.
45
45
  * @param {Status} status By default created procces is set to the STARTED status. We can override new process with the needed status.
46
46
  * @return {Process}
47
47
  */
48
- findOrCreateProcess: (step: Step, type: ProcessType, status?: Status) => Process;
48
+ findOrCreateProcess: (step: LifiStep, type: ProcessType, status?: Status) => Process;
49
49
  /**
50
50
  * Update a process object.
51
- * @param {Step} step The step where the process should be updated
51
+ * @param {LifiStep} step The step where the process should be updated
52
52
  * @param {ProcessType} type The process type to update
53
53
  * @param {Status} status The status the process gets.
54
54
  * @param {object} [params] Additional parameters to append to the process.
55
55
  * @return {Process} The update process
56
56
  */
57
- updateProcess: (step: Step, type: ProcessType, status: Status, params?: OptionalParameters) => Process;
57
+ updateProcess: (step: LifiStep, type: ProcessType, status: Status, params?: OptionalParameters) => Process;
58
58
  /**
59
59
  * Remove a process from the execution
60
- * @param {Step} step The step where the process should be removed from
60
+ * @param {LifiStep} step The step where the process should be removed from
61
61
  * @param {ProcessType} type The process type to remove
62
62
  * @return {void}
63
63
  */
64
- removeProcess: (step: Step, type: ProcessType) => void;
65
- updateStepInRoute: (step: Step) => Step;
64
+ removeProcess: (step: LifiStep, type: ProcessType) => void;
65
+ updateStepInRoute: (step: LifiStep) => LifiStep;
66
66
  allowUpdates(value: boolean): void;
67
67
  }
68
68
  export {};
@@ -12,7 +12,7 @@ export class StatusManager {
12
12
  this.shouldUpdate = true;
13
13
  /**
14
14
  * Initializes the execution object of a Step.
15
- * @param {Step} step The current step in execution
15
+ * @param {LifiStep} step The current step in execution
16
16
  * @return {Execution} The initialized execution object for this step and a function to update this step
17
17
  */
18
18
  this.initExecutionObject = (step) => {
@@ -32,7 +32,7 @@ export class StatusManager {
32
32
  /**
33
33
  * Create and push a new process into the execution.
34
34
  * @param {ProcessType} type Type of the process. Used to identify already existing processes.
35
- * @param {Step} step The step that should contain the new process.
35
+ * @param {LifiStep} step The step that should contain the new process.
36
36
  * @param {Status} status By default created procces is set to the STARTED status. We can override new process with the needed status.
37
37
  * @return {Process}
38
38
  */
@@ -60,7 +60,7 @@ export class StatusManager {
60
60
  };
61
61
  /**
62
62
  * Update a process object.
63
- * @param {Step} step The step where the process should be updated
63
+ * @param {LifiStep} step The step where the process should be updated
64
64
  * @param {ProcessType} type The process type to update
65
65
  * @param {Status} status The status the process gets.
66
66
  * @param {object} [params] Additional parameters to append to the process.
@@ -112,7 +112,7 @@ export class StatusManager {
112
112
  };
113
113
  /**
114
114
  * Remove a process from the execution
115
- * @param {Step} step The step where the process should be removed from
115
+ * @param {LifiStep} step The step where the process should be removed from
116
116
  * @param {ProcessType} type The process type to remove
117
117
  * @return {void}
118
118
  */
@@ -143,7 +143,7 @@ export class StatusManager {
143
143
  }
144
144
  /**
145
145
  * Updates the execution object of a Step.
146
- * @param {Step} step The current step in execution
146
+ * @param {LifiStep} step The current step in execution
147
147
  * @param {Status} status The status for the execution
148
148
  * @param {Receipt} receipt Optional. Information about received tokens
149
149
  * @return {Step} The step with the updated execution object
@@ -1,6 +1,6 @@
1
1
  import { Execution } from '@lifi/types';
2
2
  import { ExecutionParams } from '../types';
3
- export declare class ExecutionManager {
3
+ export declare class StepExecutionManager {
4
4
  allowUserInteraction: boolean;
5
5
  allowInteraction: (value: boolean) => void;
6
6
  execute: ({ signer, step, statusManager, settings, }: ExecutionParams) => Promise<Execution>;
@@ -9,7 +9,7 @@ import { isZeroAddress, personalizeStep } from '../utils/utils';
9
9
  import { stepComparison } from './stepComparison';
10
10
  import { switchChain } from './switchChain';
11
11
  import { getSubstatusMessage, waitForReceivingTransaction } from './utils';
12
- export class ExecutionManager {
12
+ export class StepExecutionManager {
13
13
  constructor() {
14
14
  this.allowUserInteraction = true;
15
15
  this.allowInteraction = (value) => {
@@ -75,6 +75,14 @@ export class ExecutionManager {
75
75
  if (!this.allowUserInteraction) {
76
76
  return step.execution;
77
77
  }
78
+ if (settings.updateTransactionRequest) {
79
+ const customConfig = await settings.updateTransactionRequest(transactionRequest);
80
+ transactionRequest.gasLimit = customConfig.gasLimit;
81
+ transactionRequest.gasPrice = customConfig.gasPrice;
82
+ transactionRequest.maxPriorityFeePerGas =
83
+ customConfig.maxPriorityFeePerGas;
84
+ transactionRequest.maxFeePerGas = customConfig.maxFeePerGas;
85
+ }
78
86
  // Submit the transaction
79
87
  transaction = await signer.sendTransaction(transactionRequest);
80
88
  // STEP 4: Wait for the transaction
@@ -1,9 +1,9 @@
1
1
  import { Signer } from 'ethers';
2
- import { InteractionSettings, InternalExecutionSettings, Step } from '../types';
3
- import { ExecutionManager } from './ExecutionManager';
2
+ import { InteractionSettings, InternalExecutionSettings, LifiStep } from '../types';
4
3
  import { StatusManager } from './StatusManager';
4
+ import { StepExecutionManager } from './StepExecutionManager';
5
5
  export declare class StepExecutor {
6
- executionManager: ExecutionManager;
6
+ stepExecutionManager: StepExecutionManager;
7
7
  statusManager: StatusManager;
8
8
  settings: InternalExecutionSettings;
9
9
  allowUserInteraction: boolean;
@@ -11,5 +11,5 @@ export declare class StepExecutor {
11
11
  constructor(statusManager: StatusManager, settings: InternalExecutionSettings);
12
12
  setInteraction: (settings?: InteractionSettings) => void;
13
13
  checkChain: () => never;
14
- executeStep: (signer: Signer, step: Step) => Promise<Step>;
14
+ executeStep: (signer: Signer, step: LifiStep) => Promise<LifiStep>;
15
15
  }
@@ -1,4 +1,4 @@
1
- import { ExecutionManager } from './ExecutionManager';
1
+ import { StepExecutionManager } from './StepExecutionManager';
2
2
  import { switchChain } from './switchChain';
3
3
  // Please be careful when changing the defaults as it may break the behavior (e.g., background execution)
4
4
  const defaultInteractionSettings = {
@@ -16,7 +16,7 @@ export class StepExecutor {
16
16
  ...settings,
17
17
  };
18
18
  this.allowUserInteraction = interactionSettings.allowInteraction;
19
- this.executionManager.allowInteraction(interactionSettings.allowInteraction);
19
+ this.stepExecutionManager.allowInteraction(interactionSettings.allowInteraction);
20
20
  this.statusManager.allowUpdates(interactionSettings.allowUpdates);
21
21
  this.executionStopped = interactionSettings.stopExecution;
22
22
  };
@@ -28,22 +28,30 @@ export class StepExecutor {
28
28
  };
29
29
  this.executeStep = async (signer, step) => {
30
30
  // Make sure that the chain is still correct
31
- const updatedSigner = await switchChain(signer, this.statusManager, step, this.settings.switchChainHook, this.allowUserInteraction);
32
- if (!updatedSigner) {
33
- // Chain switch was not successful, stop execution here
34
- return step;
31
+ // Find if it's bridging and the step is waiting for a transaction on the receiving chain
32
+ const recievingChainProcess = step.execution?.process.find((process) => process.type === 'RECEIVING_CHAIN');
33
+ // If the step is waiting for a transaction on the receiving chain, we do not switch the chain
34
+ // All changes are already done from the source chain
35
+ // Return the step
36
+ if (recievingChainProcess?.substatus !== 'WAIT_DESTINATION_TRANSACTION' ||
37
+ !recievingChainProcess) {
38
+ const updatedSigner = await switchChain(signer, this.statusManager, step, this.settings.switchChainHook, this.allowUserInteraction);
39
+ if (!updatedSigner) {
40
+ // Chain switch was not successful, stop execution here
41
+ return step;
42
+ }
43
+ signer = updatedSigner;
35
44
  }
36
- signer = updatedSigner;
37
45
  const parameters = {
38
46
  signer,
39
47
  step,
40
48
  settings: this.settings,
41
49
  statusManager: this.statusManager,
42
50
  };
43
- await this.executionManager.execute(parameters);
51
+ await this.stepExecutionManager.execute(parameters);
44
52
  return step;
45
53
  };
46
- this.executionManager = new ExecutionManager();
54
+ this.stepExecutionManager = new StepExecutionManager();
47
55
  this.statusManager = statusManager;
48
56
  this.settings = settings;
49
57
  }
@@ -1,5 +1,5 @@
1
1
  import { StatusManager } from '.';
2
- import { InternalExecutionSettings, Step } from '../types';
2
+ import { InternalExecutionSettings, LifiStep } from '../types';
3
3
  /**
4
4
  * This method checks whether the new and updated Step meets the required exchange rate conditions.
5
5
  * If yes it returns the updated Step.
@@ -8,7 +8,7 @@ import { InternalExecutionSettings, Step } from '../types';
8
8
  * @param statusManager
9
9
  * @param oldStep
10
10
  * @param newStep
11
- * @param acceptSlippageUpdateHook
11
+ * @param settings
12
12
  * @param allowUserInteraction
13
13
  */
14
- export declare const stepComparison: (statusManager: StatusManager, oldStep: Step, newStep: Step, settings: InternalExecutionSettings, allowUserInteraction: boolean) => Promise<Step>;
14
+ export declare const stepComparison: (statusManager: StatusManager, oldStep: LifiStep, newStep: LifiStep, settings: InternalExecutionSettings, allowUserInteraction: boolean) => Promise<LifiStep>;
@@ -8,7 +8,7 @@ import { checkStepSlippageThreshold } from './utils';
8
8
  * @param statusManager
9
9
  * @param oldStep
10
10
  * @param newStep
11
- * @param acceptSlippageUpdateHook
11
+ * @param settings
12
12
  * @param allowUserInteraction
13
13
  */
14
14
  export const stepComparison = async (statusManager, oldStep, newStep, settings, allowUserInteraction) => {
@@ -16,15 +16,12 @@ export const stepComparison = async (statusManager, oldStep, newStep, settings,
16
16
  if (checkStepSlippageThreshold(oldStep, newStep)) {
17
17
  return statusManager.updateStepInRoute(newStep);
18
18
  }
19
- const acceptExchangeRateUpdateHook = settings.acceptExchangeRateUpdateHook ?? settings.acceptSlippageUpdateHook;
20
19
  let allowStepUpdate;
21
20
  if (allowUserInteraction) {
22
- allowStepUpdate = await acceptExchangeRateUpdateHook({
21
+ allowStepUpdate = await settings.acceptExchangeRateUpdateHook({
23
22
  oldToAmount: oldStep.estimate.toAmount,
24
23
  newToAmount: newStep.estimate.toAmount,
25
24
  toToken: newStep.action.toToken,
26
- oldSlippage: oldStep.action.slippage,
27
- newSlippage: newStep.action.slippage,
28
25
  });
29
26
  }
30
27
  if (!allowStepUpdate) {
@@ -1,4 +1,4 @@
1
- import { Step } from '@lifi/types';
1
+ import { LifiStep } from '@lifi/types';
2
2
  import { Signer } from 'ethers';
3
3
  import { SwitchChainHook } from '../types';
4
4
  import { StatusManager } from './StatusManager';
@@ -13,4 +13,4 @@ import { StatusManager } from './StatusManager';
13
13
  * @param switchChainHook
14
14
  * @param allowUserInteraction
15
15
  */
16
- export declare const switchChain: (signer: Signer, statusManager: StatusManager, step: Step, switchChainHook: SwitchChainHook, allowUserInteraction: boolean) => Promise<Signer | undefined>;
16
+ export declare const switchChain: (signer: Signer, statusManager: StatusManager, step: LifiStep, switchChainHook: SwitchChainHook, allowUserInteraction: boolean) => Promise<Signer | undefined>;
@@ -1,6 +1,6 @@
1
- import { ProcessType, Status, StatusMessage, StatusResponse, Step, Substatus } from '@lifi/types';
1
+ import { LifiStep, ProcessType, Status, StatusMessage, StatusResponse, Substatus } from '@lifi/types';
2
2
  import { StatusManager } from '..';
3
- export declare function waitForReceivingTransaction(txHash: string, statusManager: StatusManager, processType: ProcessType, step: Step): Promise<StatusResponse>;
3
+ export declare function waitForReceivingTransaction(txHash: string, statusManager: StatusManager, processType: ProcessType, step: LifiStep): Promise<StatusResponse>;
4
4
  export declare function getProcessMessage(type: ProcessType, status: Status): string | undefined;
5
5
  export declare function getSubstatusMessage(status: StatusMessage, substatus?: Substatus): string | undefined;
6
- export declare function checkStepSlippageThreshold(oldStep: Step, newStep: Step): boolean;
6
+ export declare function checkStepSlippageThreshold(oldStep: LifiStep, newStep: LifiStep): boolean;
package/dist/helpers.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { Route, Step, Token } from '@lifi/types';
1
+ import { LifiStep, Route, Token } from '@lifi/types';
2
+ import { TenderlyResponse } from './types';
2
3
  /**
3
4
  * Predefined hook that decrypts calldata using EIP-1193 compliant wallet functions.
4
5
  * @param {string} walletAddress - The wallet address of the user that should decrypt the calldata.
@@ -19,12 +20,9 @@ export declare const isSameToken: (tokenA: Token, tokenB: Token) => boolean;
19
20
  export declare const checkPackageUpdates: (packageName?: string, packageVersion?: string, disableCheck?: boolean) => Promise<void>;
20
21
  /**
21
22
  * Converts a quote to Route
22
- * @param {Step} step - Step returned from the quote endpoint.
23
+ * @param {LifiStep} step - Step returned from the quote endpoint.
23
24
  * @return {Route} - The route to be executed.
24
25
  * @throws {ValidationError} Throws a ValidationError if the step has missing values.
25
26
  */
26
- export declare const convertQuoteToRoute: (step: Step) => Route;
27
- export declare const requestSettings: {
28
- retries: number;
29
- };
30
- export declare const request: <T = Response>(url: string, options?: RequestInit, retries?: number) => Promise<T>;
27
+ export declare const convertQuoteToRoute: (step: LifiStep) => Route;
28
+ export declare const fetchTxErrorDetails: (txHash: string, chainId: number) => Promise<TenderlyResponse>;