@lifi/sdk 2.0.0-beta.3 → 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 (33) 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.d.ts +1 -1
  15. package/dist/cjs/services/ApiService.js +10 -8
  16. package/dist/cjs/types/internal.types.d.ts +3 -7
  17. package/dist/cjs/version.d.ts +1 -1
  18. package/dist/cjs/version.js +1 -1
  19. package/dist/execution/RouteExecutionManager.d.ts +65 -0
  20. package/dist/execution/RouteExecutionManager.js +213 -0
  21. package/dist/execution/{ExecutionManager.d.ts → StepExecutionManager.d.ts} +1 -1
  22. package/dist/execution/{ExecutionManager.js → StepExecutionManager.js} +1 -1
  23. package/dist/execution/StepExecutor.d.ts +2 -2
  24. package/dist/execution/StepExecutor.js +4 -4
  25. package/dist/helpers.d.ts +1 -1
  26. package/dist/index.d.ts +1 -2
  27. package/dist/index.js +1 -3
  28. package/dist/services/ApiService.d.ts +1 -1
  29. package/dist/services/ApiService.js +10 -8
  30. package/dist/types/internal.types.d.ts +3 -7
  31. package/dist/version.d.ts +1 -1
  32. package/dist/version.js +1 -1
  33. package/package.json +4 -4
@@ -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,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) => {
@@ -1,9 +1,9 @@
1
1
  import { Signer } from 'ethers';
2
2
  import { InteractionSettings, InternalExecutionSettings, Step } from '../types';
3
- import { ExecutionManager } from './ExecutionManager';
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;
@@ -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
  };
@@ -40,10 +40,10 @@ export class StepExecutor {
40
40
  settings: this.settings,
41
41
  statusManager: this.statusManager,
42
42
  };
43
- await this.executionManager.execute(parameters);
43
+ await this.stepExecutionManager.execute(parameters);
44
44
  return step;
45
45
  };
46
- this.executionManager = new ExecutionManager();
46
+ this.stepExecutionManager = new StepExecutionManager();
47
47
  this.statusManager = statusManager;
48
48
  this.settings = settings;
49
49
  }
package/dist/helpers.d.ts CHANGED
@@ -27,4 +27,4 @@ export declare const convertQuoteToRoute: (step: Step) => Route;
27
27
  export declare const requestSettings: {
28
28
  retries: number;
29
29
  };
30
- export declare const request: <T = Response>(url: string, options?: RequestInit, retries?: number) => Promise<T>;
30
+ export declare const request: <T = Response>(url: RequestInfo | URL, options?: RequestInit, retries?: number) => Promise<T>;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- import LIFI from './Lifi';
2
1
  export * from './execution';
3
2
  export * from './helpers';
3
+ export { LiFi } from './LiFi';
4
4
  export * from './types';
5
5
  export * from './utils/errors';
6
- export default LIFI;
package/dist/index.js CHANGED
@@ -1,8 +1,6 @@
1
1
  // expose types and helpers
2
- import LIFI from './Lifi';
3
2
  export * from './execution';
4
3
  export * from './helpers';
4
+ export { LiFi } from './LiFi';
5
5
  export * from './types';
6
6
  export * from './utils/errors';
7
- // expose sdk
8
- export default LIFI;
@@ -3,7 +3,7 @@ import { ChainId, ChainKey, ExtendedChain, PossibilitiesRequest, PossibilitiesRe
3
3
  declare const _default: {
4
4
  getChains: (options?: RequestOptions | undefined) => Promise<ExtendedChain[]>;
5
5
  getContractCallQuote: (requestConfig: ContractCallQuoteRequest, options?: RequestOptions | undefined) => Promise<Step>;
6
- getGasRecommendation: (requestConfig: GasRecommendationRequest, options?: RequestOptions | undefined) => Promise<GasRecommendationResponse>;
6
+ getGasRecommendation: ({ chainId, fromChain, fromToken }: GasRecommendationRequest, options?: RequestOptions | undefined) => Promise<GasRecommendationResponse>;
7
7
  getPossibilities: (requestConfig?: PossibilitiesRequest | undefined, options?: RequestOptions | undefined) => Promise<PossibilitiesResponse>;
8
8
  getQuote: (requestConfig: QuoteRequest, options?: RequestOptions | undefined) => Promise<Step>;
9
9
  getRoutes: (requestConfig: RoutesRequest, options?: RequestOptions | undefined) => Promise<RoutesResponse>;
@@ -272,18 +272,20 @@ const getTokens = async (requestConfig, options) => {
272
272
  });
273
273
  return response;
274
274
  };
275
- const getGasRecommendation = async (requestConfig, options) => {
275
+ const getGasRecommendation = async ({ chainId, fromChain, fromToken }, options) => {
276
276
  const config = ConfigService.getInstance().getConfig();
277
- Object.keys(requestConfig).forEach((key) => !requestConfig[key] &&
278
- delete requestConfig[key]);
279
- if (!requestConfig.chainId) {
277
+ if (!chainId) {
280
278
  throw new ValidationError('Required parameter "chainId" is missing.');
281
279
  }
280
+ const url = new URL(`${config.apiUrl}/gas/suggestion/${chainId}`);
281
+ if (fromChain) {
282
+ url.searchParams.append('fromChain', fromChain);
283
+ }
284
+ if (fromToken) {
285
+ url.searchParams.append('fromToken', fromToken);
286
+ }
282
287
  try {
283
- const response = await request(`${config.apiUrl}/gas/suggestion/${requestConfig.chainId}?${new URLSearchParams({
284
- fromChain: requestConfig.fromChain,
285
- fromToken: requestConfig.fromToken,
286
- })}`, {
288
+ const response = await request(url, {
287
289
  signal: options?.signal,
288
290
  });
289
291
  return response;
@@ -54,7 +54,7 @@ export interface ExchangeRateUpdateParams {
54
54
  newToAmount: string;
55
55
  }
56
56
  export type AcceptExchangeRateUpdateHook = (params: ExchangeRateUpdateParams) => Promise<boolean | undefined>;
57
- export interface ExecutionData {
57
+ export interface RouteExecutionData {
58
58
  route: Route;
59
59
  executors: StepExecutor[];
60
60
  settings: InternalExecutionSettings;
@@ -71,12 +71,8 @@ export interface InternalExecutionSettings {
71
71
  export type EnforcedObjectProperties<T> = T & {
72
72
  [P in keyof T]-?: T[P];
73
73
  };
74
- export interface ActiveRouteDictionary {
75
- [k: string]: {
76
- executionData: ExecutionData;
77
- executionPromise: Promise<Route>;
78
- };
79
- }
74
+ export type RouteExecutionDictionary = Partial<Record<string, RouteExecutionData>>;
75
+ export type RouteExecutionPromiseDictionary = Partial<Record<string, Promise<Route>>>;
80
76
  export type RevokeTokenData = {
81
77
  token: Token;
82
78
  approvalAddress: string;
package/dist/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export declare const name = "@lifi/sdk";
2
- export declare const version = "2.0.0-beta.3";
2
+ export declare const version = "2.0.0-beta.4";
package/dist/version.js CHANGED
@@ -1,2 +1,2 @@
1
1
  export const name = '@lifi/sdk';
2
- export const version = '2.0.0-beta.3';
2
+ export const version = '2.0.0-beta.4';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lifi/sdk",
3
- "version": "2.0.0-beta.3",
3
+ "version": "2.0.0-beta.4",
4
4
  "description": "LI.FI Any-to-Any Cross-Chain-Swap SDK",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/index.js",
@@ -76,7 +76,7 @@
76
76
  "dependencies": {
77
77
  "@ethersproject/abi": "^5.7.0",
78
78
  "@ethersproject/contracts": "^5.7.0",
79
- "@lifi/types": "^2.3.0",
79
+ "@lifi/types": "^2.4.1",
80
80
  "bignumber.js": "^9.1.1",
81
81
  "eth-rpc-errors": "^4.0.3",
82
82
  "ethers": "^5.7.2"
@@ -88,11 +88,11 @@
88
88
  "@typescript-eslint/parser": "^5.54.1",
89
89
  "@vitest/coverage-c8": "^0.29.2",
90
90
  "cross-fetch": "^3.1.5",
91
- "eslint": "^8.35.0",
91
+ "eslint": "^8.36.0",
92
92
  "eslint-config-prettier": "^8.7.0",
93
93
  "eslint-plugin-prettier": "^4.2.1",
94
94
  "husky": "^8.0.3",
95
- "lint-staged": "^13.1.4",
95
+ "lint-staged": "^13.2.0",
96
96
  "msw": "1.0.1",
97
97
  "npm-run-all": "^4.1.5",
98
98
  "pinst": "^3.0.0",