@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.
- package/dist/{Lifi.d.ts → LiFi.d.ts} +19 -63
- package/dist/{Lifi.js → LiFi.js} +19 -212
- package/dist/allowance/checkAllowance.d.ts +2 -2
- package/dist/allowance/checkAllowance.js +16 -2
- package/dist/allowance/utils.d.ts +2 -1
- package/dist/allowance/utils.js +7 -2
- package/dist/balance/checkBalance.d.ts +2 -2
- package/dist/balance/index.d.ts +1 -1
- package/dist/cjs/{Lifi.d.ts → LiFi.d.ts} +19 -63
- package/dist/cjs/{Lifi.js → LiFi.js} +22 -213
- package/dist/cjs/allowance/checkAllowance.d.ts +2 -2
- package/dist/cjs/allowance/checkAllowance.js +16 -2
- package/dist/cjs/allowance/utils.d.ts +2 -1
- package/dist/cjs/allowance/utils.js +7 -2
- package/dist/cjs/balance/checkBalance.d.ts +2 -2
- package/dist/cjs/balance/index.d.ts +1 -1
- package/dist/cjs/connectors.d.ts +1 -1
- package/dist/cjs/connectors.js +7 -9
- package/dist/cjs/execution/RouteExecutionManager.d.ts +65 -0
- package/dist/cjs/execution/RouteExecutionManager.js +220 -0
- package/dist/cjs/execution/StatusManager.d.ts +12 -12
- package/dist/cjs/execution/StatusManager.js +5 -5
- package/dist/cjs/execution/{ExecutionManager.d.ts → StepExecutionManager.d.ts} +1 -1
- package/dist/cjs/execution/{ExecutionManager.js → StepExecutionManager.js} +11 -3
- package/dist/cjs/execution/StepExecutor.d.ts +4 -4
- package/dist/cjs/execution/StepExecutor.js +17 -9
- package/dist/cjs/execution/stepComparison.d.ts +3 -3
- package/dist/cjs/execution/stepComparison.js +2 -5
- package/dist/cjs/execution/switchChain.d.ts +2 -2
- package/dist/cjs/execution/utils.d.ts +3 -3
- package/dist/cjs/helpers.d.ts +5 -7
- package/dist/cjs/helpers.js +10 -30
- package/dist/cjs/index.d.ts +1 -2
- package/dist/cjs/index.js +3 -6
- package/dist/cjs/request.d.ts +9 -0
- package/dist/cjs/request.js +60 -0
- package/dist/cjs/services/ApiService.d.ts +8 -7
- package/dist/cjs/services/ApiService.js +52 -31
- package/dist/cjs/services/ConfigService.js +6 -2
- package/dist/cjs/typeguards.d.ts +2 -2
- package/dist/cjs/types/internal.types.d.ts +54 -10
- package/dist/cjs/utils/errors.d.ts +20 -1
- package/dist/cjs/utils/errors.js +23 -1
- package/dist/cjs/utils/parseError.d.ts +4 -4
- package/dist/cjs/utils/parseError.js +29 -12
- package/dist/cjs/utils/utils.d.ts +3 -3
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/connectors.d.ts +1 -1
- package/dist/connectors.js +7 -9
- package/dist/execution/RouteExecutionManager.d.ts +65 -0
- package/dist/execution/RouteExecutionManager.js +213 -0
- package/dist/execution/StatusManager.d.ts +12 -12
- package/dist/execution/StatusManager.js +5 -5
- package/dist/execution/{ExecutionManager.d.ts → StepExecutionManager.d.ts} +1 -1
- package/dist/execution/{ExecutionManager.js → StepExecutionManager.js} +9 -1
- package/dist/execution/StepExecutor.d.ts +4 -4
- package/dist/execution/StepExecutor.js +17 -9
- package/dist/execution/stepComparison.d.ts +3 -3
- package/dist/execution/stepComparison.js +2 -5
- package/dist/execution/switchChain.d.ts +2 -2
- package/dist/execution/utils.d.ts +3 -3
- package/dist/helpers.d.ts +5 -7
- package/dist/helpers.js +9 -29
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -3
- package/dist/request.d.ts +9 -0
- package/dist/request.js +53 -0
- package/dist/services/ApiService.d.ts +8 -7
- package/dist/services/ApiService.js +43 -22
- package/dist/services/ConfigService.js +7 -2
- package/dist/typeguards.d.ts +2 -2
- package/dist/types/internal.types.d.ts +54 -10
- package/dist/utils/errors.d.ts +20 -1
- package/dist/utils/errors.js +22 -0
- package/dist/utils/parseError.d.ts +4 -4
- package/dist/utils/parseError.js +30 -13
- package/dist/utils/utils.d.ts +3 -3
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +15 -15
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.RouteExecutionManager = void 0;
|
|
7
|
+
const ConfigService_1 = __importDefault(require("../services/ConfigService"));
|
|
8
|
+
const errors_1 = require("../utils/errors");
|
|
9
|
+
const preRestart_1 = require("../utils/preRestart");
|
|
10
|
+
const StatusManager_1 = require("./StatusManager");
|
|
11
|
+
const StepExecutor_1 = require("./StepExecutor");
|
|
12
|
+
class RouteExecutionManager {
|
|
13
|
+
constructor(configUpdate) {
|
|
14
|
+
this.executionDictionary = {};
|
|
15
|
+
this.executionPromiseDictionary = {};
|
|
16
|
+
/**
|
|
17
|
+
* Execute a route.
|
|
18
|
+
* @param {Signer} signer - The signer required to send the transactions.
|
|
19
|
+
* @param {Route} route - The route that should be executed. Cannot be an active route.
|
|
20
|
+
* @param {ExecutionSettings} settings - An object containing settings and callbacks.
|
|
21
|
+
* @return {Promise<Route>} The executed route.
|
|
22
|
+
* @throws {LifiError} Throws a LifiError if the execution fails.
|
|
23
|
+
*/
|
|
24
|
+
this.executeRoute = async (signer, route, settings) => {
|
|
25
|
+
// Deep clone to prevent side effects
|
|
26
|
+
const clonedRoute = structuredClone(route);
|
|
27
|
+
let executionPromise = this.executionPromiseDictionary[clonedRoute.id];
|
|
28
|
+
// Check if route is already running
|
|
29
|
+
if (executionPromise) {
|
|
30
|
+
return executionPromise;
|
|
31
|
+
}
|
|
32
|
+
executionPromise = this.executeSteps(signer, clonedRoute, settings);
|
|
33
|
+
this.executionPromiseDictionary[clonedRoute.id] = executionPromise;
|
|
34
|
+
return executionPromise;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Resume the execution of a route that has been stopped or had an error while executing.
|
|
38
|
+
* @param {Signer} signer - The signer required to send the transactions.
|
|
39
|
+
* @param {Route} route - The route that is to be executed. Cannot be an active route.
|
|
40
|
+
* @param {ExecutionSettings} settings - An object containing settings and callbacks.
|
|
41
|
+
* @return {Promise<Route>} The executed route.
|
|
42
|
+
* @throws {LifiError} Throws a LifiError if the execution fails.
|
|
43
|
+
*/
|
|
44
|
+
this.resumeRoute = async (signer, route, settings) => {
|
|
45
|
+
// Deep clone to prevent side effects
|
|
46
|
+
const clonedRoute = structuredClone(route);
|
|
47
|
+
const execution = this.executionDictionary[clonedRoute.id];
|
|
48
|
+
if (execution) {
|
|
49
|
+
const executionHalted = execution.executors.some((executor) => executor.executionStopped);
|
|
50
|
+
if (!executionHalted) {
|
|
51
|
+
// Check if we want to resume route execution in the background
|
|
52
|
+
this.updateRouteExecution(route, {
|
|
53
|
+
executeInBackground: settings?.executeInBackground,
|
|
54
|
+
});
|
|
55
|
+
const executionPromise = this.executionPromiseDictionary[clonedRoute.id];
|
|
56
|
+
return executionPromise ?? clonedRoute;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
(0, preRestart_1.handlePreRestart)(clonedRoute);
|
|
60
|
+
const executionPromise = this.executeSteps(signer, clonedRoute, settings);
|
|
61
|
+
this.executionPromiseDictionary[clonedRoute.id] = executionPromise;
|
|
62
|
+
return executionPromise;
|
|
63
|
+
};
|
|
64
|
+
this.executeSteps = async (signer, route, settings) => {
|
|
65
|
+
const config = this.configService.getConfig();
|
|
66
|
+
const execution = {
|
|
67
|
+
route,
|
|
68
|
+
executors: [],
|
|
69
|
+
settings: { ...config.defaultExecutionSettings, ...settings },
|
|
70
|
+
};
|
|
71
|
+
this.executionDictionary[route.id] = execution;
|
|
72
|
+
const statusManager = new StatusManager_1.StatusManager(route, execution.settings, (route) => {
|
|
73
|
+
if (this.executionDictionary[route.id]) {
|
|
74
|
+
execution.route = route;
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
// Loop over steps and execute them
|
|
78
|
+
for (let index = 0; index < route.steps.length; index++) {
|
|
79
|
+
const execution = this.executionDictionary[route.id];
|
|
80
|
+
// Check if execution has stopped in the meantime
|
|
81
|
+
if (!execution) {
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
const step = route.steps[index];
|
|
85
|
+
const previousStep = route.steps[index - 1];
|
|
86
|
+
// Check if the step is already done
|
|
87
|
+
//
|
|
88
|
+
if (step.execution?.status === 'DONE') {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
// Update amount using output of previous execution. In the future this should be handled by calling `updateRoute`
|
|
92
|
+
if (previousStep?.execution?.toAmount) {
|
|
93
|
+
step.action.fromAmount = previousStep.execution.toAmount;
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
const stepExecutor = new StepExecutor_1.StepExecutor(statusManager, execution.settings);
|
|
97
|
+
execution.executors.push(stepExecutor);
|
|
98
|
+
// Check if we want to execute this step in the background
|
|
99
|
+
this.updateRouteExecution(route, execution.settings);
|
|
100
|
+
const executedStep = await stepExecutor.executeStep(signer, step);
|
|
101
|
+
// We may reach this point if user interaction isn't allowed. We want to stop execution until we resume it
|
|
102
|
+
if (executedStep.execution?.status !== 'DONE') {
|
|
103
|
+
this.stopExecution(route);
|
|
104
|
+
}
|
|
105
|
+
// Execution stopped during the current step, we don't want to continue to the next step so we return already
|
|
106
|
+
if (stepExecutor.executionStopped) {
|
|
107
|
+
return route;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
catch (e) {
|
|
111
|
+
this.stopExecution(route);
|
|
112
|
+
throw e;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// Clean up after the execution
|
|
116
|
+
delete this.executionDictionary[route.id];
|
|
117
|
+
return route;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Updates route execution to background or foreground state.
|
|
121
|
+
* @param {Route} route - A route that is currently in execution.
|
|
122
|
+
* @param {boolean} settings - An object with execution settings.
|
|
123
|
+
*/
|
|
124
|
+
this.updateRouteExecution = (route, settings) => {
|
|
125
|
+
const execution = this.executionDictionary[route.id];
|
|
126
|
+
if (!execution) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
for (const executor of execution.executors) {
|
|
130
|
+
executor.setInteraction({
|
|
131
|
+
allowInteraction: !settings.executeInBackground,
|
|
132
|
+
allowUpdates: true,
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
// Update active route settings so we know what the current state of execution is
|
|
136
|
+
execution.settings = {
|
|
137
|
+
...execution.settings,
|
|
138
|
+
...settings,
|
|
139
|
+
};
|
|
140
|
+
};
|
|
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
|
+
this.updateExecutionSettings = (settings, route) => {
|
|
148
|
+
const execution = this.executionDictionary[route.id];
|
|
149
|
+
if (!execution) {
|
|
150
|
+
throw new errors_1.ValidationError("Can't set ExecutionSettings for the inactive route.");
|
|
151
|
+
}
|
|
152
|
+
const config = this.configService.getConfig();
|
|
153
|
+
execution.settings = {
|
|
154
|
+
...config.defaultExecutionSettings,
|
|
155
|
+
...settings,
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Executes a route until a user interaction is necessary (signing transactions, etc.) and then halts until the route is resumed.
|
|
160
|
+
* @param {Route} route - A route that is currently in execution.
|
|
161
|
+
* @deprecated use updateRouteExecution instead.
|
|
162
|
+
*/
|
|
163
|
+
this.moveExecutionToBackground = (route) => {
|
|
164
|
+
const execution = this.executionDictionary[route.id];
|
|
165
|
+
if (!execution) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
for (const executor of execution.executors) {
|
|
169
|
+
executor.setInteraction({ allowInteraction: false, allowUpdates: true });
|
|
170
|
+
}
|
|
171
|
+
execution.settings = {
|
|
172
|
+
...execution.settings,
|
|
173
|
+
executeInBackground: true,
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
/**
|
|
177
|
+
* Stops the execution of an active route.
|
|
178
|
+
* @param {Route} route - A route that is currently in execution.
|
|
179
|
+
* @return {Route} The stopped route.
|
|
180
|
+
*/
|
|
181
|
+
this.stopExecution = (route) => {
|
|
182
|
+
const execution = this.executionDictionary[route.id];
|
|
183
|
+
if (!execution) {
|
|
184
|
+
return route;
|
|
185
|
+
}
|
|
186
|
+
for (const executor of execution.executors) {
|
|
187
|
+
executor.setInteraction({
|
|
188
|
+
allowInteraction: false,
|
|
189
|
+
allowUpdates: false,
|
|
190
|
+
stopExecution: true,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
delete this.executionDictionary[route.id];
|
|
194
|
+
return route;
|
|
195
|
+
};
|
|
196
|
+
/**
|
|
197
|
+
* Get the list of active routes.
|
|
198
|
+
* @return {Route[]} A list of routes.
|
|
199
|
+
*/
|
|
200
|
+
this.getActiveRoutes = () => {
|
|
201
|
+
return Object.values(this.executionDictionary)
|
|
202
|
+
.map((dict) => dict?.route)
|
|
203
|
+
.filter(Boolean);
|
|
204
|
+
};
|
|
205
|
+
/**
|
|
206
|
+
* Return the current route information for given route. The route has to be active.
|
|
207
|
+
* @param {Route} route - A route object.
|
|
208
|
+
* @return {Route} The updated route.
|
|
209
|
+
*/
|
|
210
|
+
this.getActiveRoute = (route) => {
|
|
211
|
+
return this.executionDictionary[route.id]?.route;
|
|
212
|
+
};
|
|
213
|
+
this.configService = ConfigService_1.default.getInstance();
|
|
214
|
+
if (configUpdate) {
|
|
215
|
+
// Update API urls before we request chains
|
|
216
|
+
this.configService.updateConfig(configUpdate);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
exports.RouteExecutionManager = RouteExecutionManager;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Execution, InternalExecutionSettings, Process, ProcessType, Route, Status,
|
|
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 {
|
|
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:
|
|
32
|
+
initExecutionObject: (step: LifiStep) => Execution;
|
|
33
33
|
/**
|
|
34
34
|
* Updates the execution object of a Step.
|
|
35
|
-
* @param {
|
|
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:
|
|
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 {
|
|
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:
|
|
48
|
+
findOrCreateProcess: (step: LifiStep, type: ProcessType, status?: Status) => Process;
|
|
49
49
|
/**
|
|
50
50
|
* Update a process object.
|
|
51
|
-
* @param {
|
|
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:
|
|
57
|
+
updateProcess: (step: LifiStep, type: ProcessType, status: Status, params?: OptionalParameters) => Process;
|
|
58
58
|
/**
|
|
59
59
|
* Remove a process from the execution
|
|
60
|
-
* @param {
|
|
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:
|
|
65
|
-
updateStepInRoute: (step:
|
|
64
|
+
removeProcess: (step: LifiStep, type: ProcessType) => void;
|
|
65
|
+
updateStepInRoute: (step: LifiStep) => LifiStep;
|
|
66
66
|
allowUpdates(value: boolean): void;
|
|
67
67
|
}
|
|
68
68
|
export {};
|
|
@@ -15,7 +15,7 @@ class StatusManager {
|
|
|
15
15
|
this.shouldUpdate = true;
|
|
16
16
|
/**
|
|
17
17
|
* Initializes the execution object of a Step.
|
|
18
|
-
* @param {
|
|
18
|
+
* @param {LifiStep} step The current step in execution
|
|
19
19
|
* @return {Execution} The initialized execution object for this step and a function to update this step
|
|
20
20
|
*/
|
|
21
21
|
this.initExecutionObject = (step) => {
|
|
@@ -35,7 +35,7 @@ class StatusManager {
|
|
|
35
35
|
/**
|
|
36
36
|
* Create and push a new process into the execution.
|
|
37
37
|
* @param {ProcessType} type Type of the process. Used to identify already existing processes.
|
|
38
|
-
* @param {
|
|
38
|
+
* @param {LifiStep} step The step that should contain the new process.
|
|
39
39
|
* @param {Status} status By default created procces is set to the STARTED status. We can override new process with the needed status.
|
|
40
40
|
* @return {Process}
|
|
41
41
|
*/
|
|
@@ -63,7 +63,7 @@ class StatusManager {
|
|
|
63
63
|
};
|
|
64
64
|
/**
|
|
65
65
|
* Update a process object.
|
|
66
|
-
* @param {
|
|
66
|
+
* @param {LifiStep} step The step where the process should be updated
|
|
67
67
|
* @param {ProcessType} type The process type to update
|
|
68
68
|
* @param {Status} status The status the process gets.
|
|
69
69
|
* @param {object} [params] Additional parameters to append to the process.
|
|
@@ -115,7 +115,7 @@ class StatusManager {
|
|
|
115
115
|
};
|
|
116
116
|
/**
|
|
117
117
|
* Remove a process from the execution
|
|
118
|
-
* @param {
|
|
118
|
+
* @param {LifiStep} step The step where the process should be removed from
|
|
119
119
|
* @param {ProcessType} type The process type to remove
|
|
120
120
|
* @return {void}
|
|
121
121
|
*/
|
|
@@ -146,7 +146,7 @@ class StatusManager {
|
|
|
146
146
|
}
|
|
147
147
|
/**
|
|
148
148
|
* Updates the execution object of a Step.
|
|
149
|
-
* @param {
|
|
149
|
+
* @param {LifiStep} step The current step in execution
|
|
150
150
|
* @param {Status} status The status for the execution
|
|
151
151
|
* @param {Receipt} receipt Optional. Information about received tokens
|
|
152
152
|
* @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
|
|
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>;
|
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.StepExecutionManager = void 0;
|
|
7
7
|
const allowance_1 = require("../allowance");
|
|
8
8
|
const balance_1 = require("../balance");
|
|
9
9
|
const ApiService_1 = __importDefault(require("../services/ApiService"));
|
|
@@ -15,7 +15,7 @@ const utils_1 = require("../utils/utils");
|
|
|
15
15
|
const stepComparison_1 = require("./stepComparison");
|
|
16
16
|
const switchChain_1 = require("./switchChain");
|
|
17
17
|
const utils_2 = require("./utils");
|
|
18
|
-
class
|
|
18
|
+
class StepExecutionManager {
|
|
19
19
|
constructor() {
|
|
20
20
|
this.allowUserInteraction = true;
|
|
21
21
|
this.allowInteraction = (value) => {
|
|
@@ -81,6 +81,14 @@ class ExecutionManager {
|
|
|
81
81
|
if (!this.allowUserInteraction) {
|
|
82
82
|
return step.execution;
|
|
83
83
|
}
|
|
84
|
+
if (settings.updateTransactionRequest) {
|
|
85
|
+
const customConfig = await settings.updateTransactionRequest(transactionRequest);
|
|
86
|
+
transactionRequest.gasLimit = customConfig.gasLimit;
|
|
87
|
+
transactionRequest.gasPrice = customConfig.gasPrice;
|
|
88
|
+
transactionRequest.maxPriorityFeePerGas =
|
|
89
|
+
customConfig.maxPriorityFeePerGas;
|
|
90
|
+
transactionRequest.maxFeePerGas = customConfig.maxFeePerGas;
|
|
91
|
+
}
|
|
84
92
|
// Submit the transaction
|
|
85
93
|
transaction = await signer.sendTransaction(transactionRequest);
|
|
86
94
|
// STEP 4: Wait for the transaction
|
|
@@ -172,4 +180,4 @@ class ExecutionManager {
|
|
|
172
180
|
};
|
|
173
181
|
}
|
|
174
182
|
}
|
|
175
|
-
exports.
|
|
183
|
+
exports.StepExecutionManager = StepExecutionManager;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Signer } from 'ethers';
|
|
2
|
-
import { InteractionSettings, InternalExecutionSettings,
|
|
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
|
-
|
|
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:
|
|
14
|
+
executeStep: (signer: Signer, step: LifiStep) => Promise<LifiStep>;
|
|
15
15
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.StepExecutor = void 0;
|
|
4
|
-
const
|
|
4
|
+
const StepExecutionManager_1 = require("./StepExecutionManager");
|
|
5
5
|
const switchChain_1 = require("./switchChain");
|
|
6
6
|
// Please be careful when changing the defaults as it may break the behavior (e.g., background execution)
|
|
7
7
|
const defaultInteractionSettings = {
|
|
@@ -19,7 +19,7 @@ class StepExecutor {
|
|
|
19
19
|
...settings,
|
|
20
20
|
};
|
|
21
21
|
this.allowUserInteraction = interactionSettings.allowInteraction;
|
|
22
|
-
this.
|
|
22
|
+
this.stepExecutionManager.allowInteraction(interactionSettings.allowInteraction);
|
|
23
23
|
this.statusManager.allowUpdates(interactionSettings.allowUpdates);
|
|
24
24
|
this.executionStopped = interactionSettings.stopExecution;
|
|
25
25
|
};
|
|
@@ -31,22 +31,30 @@ class StepExecutor {
|
|
|
31
31
|
};
|
|
32
32
|
this.executeStep = async (signer, step) => {
|
|
33
33
|
// Make sure that the chain is still correct
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
// Find if it's bridging and the step is waiting for a transaction on the receiving chain
|
|
35
|
+
const recievingChainProcess = step.execution?.process.find((process) => process.type === 'RECEIVING_CHAIN');
|
|
36
|
+
// If the step is waiting for a transaction on the receiving chain, we do not switch the chain
|
|
37
|
+
// All changes are already done from the source chain
|
|
38
|
+
// Return the step
|
|
39
|
+
if (recievingChainProcess?.substatus !== 'WAIT_DESTINATION_TRANSACTION' ||
|
|
40
|
+
!recievingChainProcess) {
|
|
41
|
+
const updatedSigner = await (0, switchChain_1.switchChain)(signer, this.statusManager, step, this.settings.switchChainHook, this.allowUserInteraction);
|
|
42
|
+
if (!updatedSigner) {
|
|
43
|
+
// Chain switch was not successful, stop execution here
|
|
44
|
+
return step;
|
|
45
|
+
}
|
|
46
|
+
signer = updatedSigner;
|
|
38
47
|
}
|
|
39
|
-
signer = updatedSigner;
|
|
40
48
|
const parameters = {
|
|
41
49
|
signer,
|
|
42
50
|
step,
|
|
43
51
|
settings: this.settings,
|
|
44
52
|
statusManager: this.statusManager,
|
|
45
53
|
};
|
|
46
|
-
await this.
|
|
54
|
+
await this.stepExecutionManager.execute(parameters);
|
|
47
55
|
return step;
|
|
48
56
|
};
|
|
49
|
-
this.
|
|
57
|
+
this.stepExecutionManager = new StepExecutionManager_1.StepExecutionManager();
|
|
50
58
|
this.statusManager = statusManager;
|
|
51
59
|
this.settings = settings;
|
|
52
60
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { StatusManager } from '.';
|
|
2
|
-
import { InternalExecutionSettings,
|
|
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
|
|
11
|
+
* @param settings
|
|
12
12
|
* @param allowUserInteraction
|
|
13
13
|
*/
|
|
14
|
-
export declare const stepComparison: (statusManager: StatusManager, oldStep:
|
|
14
|
+
export declare const stepComparison: (statusManager: StatusManager, oldStep: LifiStep, newStep: LifiStep, settings: InternalExecutionSettings, allowUserInteraction: boolean) => Promise<LifiStep>;
|
|
@@ -11,7 +11,7 @@ const utils_1 = require("./utils");
|
|
|
11
11
|
* @param statusManager
|
|
12
12
|
* @param oldStep
|
|
13
13
|
* @param newStep
|
|
14
|
-
* @param
|
|
14
|
+
* @param settings
|
|
15
15
|
* @param allowUserInteraction
|
|
16
16
|
*/
|
|
17
17
|
const stepComparison = async (statusManager, oldStep, newStep, settings, allowUserInteraction) => {
|
|
@@ -19,15 +19,12 @@ const stepComparison = async (statusManager, oldStep, newStep, settings, allowUs
|
|
|
19
19
|
if ((0, utils_1.checkStepSlippageThreshold)(oldStep, newStep)) {
|
|
20
20
|
return statusManager.updateStepInRoute(newStep);
|
|
21
21
|
}
|
|
22
|
-
const acceptExchangeRateUpdateHook = settings.acceptExchangeRateUpdateHook ?? settings.acceptSlippageUpdateHook;
|
|
23
22
|
let allowStepUpdate;
|
|
24
23
|
if (allowUserInteraction) {
|
|
25
|
-
allowStepUpdate = await acceptExchangeRateUpdateHook({
|
|
24
|
+
allowStepUpdate = await settings.acceptExchangeRateUpdateHook({
|
|
26
25
|
oldToAmount: oldStep.estimate.toAmount,
|
|
27
26
|
newToAmount: newStep.estimate.toAmount,
|
|
28
27
|
toToken: newStep.action.toToken,
|
|
29
|
-
oldSlippage: oldStep.action.slippage,
|
|
30
|
-
newSlippage: newStep.action.slippage,
|
|
31
28
|
});
|
|
32
29
|
}
|
|
33
30
|
if (!allowStepUpdate) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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:
|
|
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,
|
|
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:
|
|
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:
|
|
6
|
+
export declare function checkStepSlippageThreshold(oldStep: LifiStep, newStep: LifiStep): boolean;
|
package/dist/cjs/helpers.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
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 {
|
|
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:
|
|
27
|
-
export declare const
|
|
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>;
|
package/dist/cjs/helpers.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.fetchTxErrorDetails = exports.convertQuoteToRoute = exports.checkPackageUpdates = exports.isSameToken = exports.getRandomNumber = exports.getEthereumPublicKeyHook = exports.getEthereumDecryptionHook = void 0;
|
|
4
|
+
const request_1 = require("./request");
|
|
4
5
|
const errors_1 = require("./utils/errors");
|
|
5
|
-
const utils_1 = require("./utils/utils");
|
|
6
6
|
const version_1 = require("./version");
|
|
7
7
|
const ethereumRequest = async (method, params) => {
|
|
8
8
|
// If ethereum.request() exists, the provider is probably EIP-1193 compliant.
|
|
@@ -65,7 +65,7 @@ const checkPackageUpdates = async (packageName, packageVersion, disableCheck) =>
|
|
|
65
65
|
}
|
|
66
66
|
try {
|
|
67
67
|
const pkgName = packageName ?? version_1.name;
|
|
68
|
-
const response = await (0,
|
|
68
|
+
const response = await (0, request_1.request)(`https://registry.npmjs.org/${pkgName}/latest`, { skipTrackingHeaders: true });
|
|
69
69
|
const latestVersion = response.version;
|
|
70
70
|
const currentVersion = packageVersion ?? version_1.version;
|
|
71
71
|
if (semverCompare(latestVersion, currentVersion)) {
|
|
@@ -81,7 +81,7 @@ const checkPackageUpdates = async (packageName, packageVersion, disableCheck) =>
|
|
|
81
81
|
exports.checkPackageUpdates = checkPackageUpdates;
|
|
82
82
|
/**
|
|
83
83
|
* Converts a quote to Route
|
|
84
|
-
* @param {
|
|
84
|
+
* @param {LifiStep} step - Step returned from the quote endpoint.
|
|
85
85
|
* @return {Route} - The route to be executed.
|
|
86
86
|
* @throws {ValidationError} Throws a ValidationError if the step has missing values.
|
|
87
87
|
*/
|
|
@@ -92,11 +92,6 @@ const convertQuoteToRoute = (step) => {
|
|
|
92
92
|
if (!step.estimate.toAmountUSD) {
|
|
93
93
|
throw new errors_1.ValidationError("Missing 'toAmountUSD' in step estimate.");
|
|
94
94
|
}
|
|
95
|
-
const lifiStep = {
|
|
96
|
-
...step,
|
|
97
|
-
type: 'lifi',
|
|
98
|
-
includedSteps: [],
|
|
99
|
-
};
|
|
100
95
|
const route = {
|
|
101
96
|
fromToken: step.action.fromToken,
|
|
102
97
|
toToken: step.action.toToken,
|
|
@@ -107,30 +102,15 @@ const convertQuoteToRoute = (step) => {
|
|
|
107
102
|
toChainId: step.action.toToken.chainId,
|
|
108
103
|
fromAmountUSD: step.estimate.fromAmountUSD,
|
|
109
104
|
toAmountUSD: step.estimate.toAmountUSD,
|
|
110
|
-
steps: [
|
|
105
|
+
steps: [step],
|
|
111
106
|
toAmountMin: step.estimate.toAmountMin,
|
|
107
|
+
insurance: { state: 'NOT_INSURABLE', feeAmountUsd: '0' },
|
|
112
108
|
};
|
|
113
109
|
return route;
|
|
114
110
|
};
|
|
115
111
|
exports.convertQuoteToRoute = convertQuoteToRoute;
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
const request = async (url, options, retries = exports.requestSettings.retries) => {
|
|
120
|
-
try {
|
|
121
|
-
const response = await fetch(url, options);
|
|
122
|
-
if (!response.ok) {
|
|
123
|
-
throw new errors_1.HTTPError(response);
|
|
124
|
-
}
|
|
125
|
-
const data = await response.json();
|
|
126
|
-
return data;
|
|
127
|
-
}
|
|
128
|
-
catch (error) {
|
|
129
|
-
if (retries > 0 && error?.status === 500) {
|
|
130
|
-
await (0, utils_1.sleep)(500);
|
|
131
|
-
return (0, exports.request)(url, options, retries - 1);
|
|
132
|
-
}
|
|
133
|
-
throw error;
|
|
134
|
-
}
|
|
112
|
+
const fetchTxErrorDetails = async (txHash, chainId) => {
|
|
113
|
+
const response = await (0, request_1.request)(`https://api.tenderly.co/api/v1/public-contract/${chainId}/tx/${txHash}`);
|
|
114
|
+
return response;
|
|
135
115
|
};
|
|
136
|
-
exports.
|
|
116
|
+
exports.fetchTxErrorDetails = fetchTxErrorDetails;
|
package/dist/cjs/index.d.ts
CHANGED
package/dist/cjs/index.js
CHANGED
|
@@ -13,15 +13,12 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
13
13
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
-
};
|
|
19
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.LiFi = void 0;
|
|
20
18
|
// expose types and helpers
|
|
21
|
-
const Lifi_1 = __importDefault(require("./Lifi"));
|
|
22
19
|
__exportStar(require("./execution"), exports);
|
|
23
20
|
__exportStar(require("./helpers"), exports);
|
|
21
|
+
var LiFi_1 = require("./LiFi");
|
|
22
|
+
Object.defineProperty(exports, "LiFi", { enumerable: true, get: function () { return LiFi_1.LiFi; } });
|
|
24
23
|
__exportStar(require("./types"), exports);
|
|
25
24
|
__exportStar(require("./utils/errors"), exports);
|
|
26
|
-
// expose sdk
|
|
27
|
-
exports.default = Lifi_1.default;
|