@lifi/sdk 3.0.0-alpha.6 → 3.0.0-alpha.8

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 (121) hide show
  1. package/dist/LiFi.d.ts +21 -18
  2. package/dist/LiFi.js +16 -13
  3. package/dist/allowance/setAllowance.d.ts +1 -1
  4. package/dist/cjs/LiFi.d.ts +21 -18
  5. package/dist/cjs/LiFi.js +17 -14
  6. package/dist/cjs/allowance/setAllowance.d.ts +1 -1
  7. package/dist/cjs/connectors.d.ts +1 -1
  8. package/dist/cjs/connectors.js +3 -6
  9. package/dist/cjs/execution/BaseStepExecutor.d.ts +12 -0
  10. package/dist/cjs/execution/BaseStepExecutor.js +27 -0
  11. package/dist/cjs/execution/EVMStepExecutor.d.ts +13 -0
  12. package/dist/cjs/execution/EVMStepExecutor.js +292 -0
  13. package/dist/cjs/execution/RouteExecutionManager.d.ts +9 -8
  14. package/dist/cjs/execution/RouteExecutionManager.js +26 -24
  15. package/dist/cjs/execution/StatusManager.d.ts +8 -7
  16. package/dist/cjs/execution/StatusManager.js +1 -1
  17. package/dist/cjs/execution/StepExecutionManager.d.ts +3 -3
  18. package/dist/cjs/execution/StepExecutionManager.js +8 -8
  19. package/dist/cjs/execution/checkAllowance.d.ts +3 -2
  20. package/dist/cjs/execution/checkBalance.d.ts +2 -2
  21. package/dist/cjs/execution/index.d.ts +1 -0
  22. package/dist/cjs/execution/index.js +1 -0
  23. package/dist/cjs/execution/multisig.d.ts +2 -2
  24. package/dist/cjs/execution/multisig.js +2 -5
  25. package/dist/cjs/execution/prepareRestart.d.ts +2 -3
  26. package/dist/cjs/execution/prepareRestart.js +2 -2
  27. package/dist/cjs/execution/stepComparison.d.ts +4 -3
  28. package/dist/cjs/execution/stepComparison.js +1 -1
  29. package/dist/cjs/execution/switchChain.d.ts +4 -3
  30. package/dist/cjs/execution/switchChain.js +1 -0
  31. package/dist/cjs/{types/internal.types.d.ts → execution/types.d.ts} +33 -96
  32. package/dist/cjs/execution/utils.d.ts +2 -2
  33. package/dist/cjs/execution/waitForReceivingTransaction.d.ts +3 -3
  34. package/dist/cjs/helpers.d.ts +2 -2
  35. package/dist/cjs/index.d.ts +5 -1
  36. package/dist/cjs/index.js +10 -4
  37. package/dist/cjs/providers/EVM.d.ts +10 -0
  38. package/dist/cjs/providers/EVM.js +34 -0
  39. package/dist/cjs/providers/Solana.d.ts +9 -0
  40. package/dist/cjs/providers/Solana.js +38 -0
  41. package/dist/cjs/providers/index.d.ts +1 -0
  42. package/dist/cjs/providers/index.js +17 -0
  43. package/dist/cjs/providers/types.d.ts +12 -0
  44. package/dist/cjs/providers/types.js +8 -0
  45. package/dist/cjs/request.js +12 -9
  46. package/dist/cjs/services/ApiService.d.ts +4 -5
  47. package/dist/cjs/services/ApiService.js +15 -18
  48. package/dist/cjs/services/ChainsService.d.ts +1 -1
  49. package/dist/cjs/services/ChainsService.js +2 -1
  50. package/dist/cjs/services/ConfigService.d.ts +9 -6
  51. package/dist/cjs/services/ConfigService.js +7 -4
  52. package/dist/cjs/typeguards.d.ts +2 -2
  53. package/dist/cjs/types/index.d.ts +1 -3
  54. package/dist/cjs/types/index.js +1 -5
  55. package/dist/cjs/types/internal.d.ts +60 -0
  56. package/dist/cjs/types/internal.js +2 -0
  57. package/dist/cjs/utils/parseError.d.ts +4 -4
  58. package/dist/cjs/utils/parseError.js +3 -6
  59. package/dist/cjs/version.d.ts +1 -1
  60. package/dist/cjs/version.js +1 -1
  61. package/dist/connectors.d.ts +1 -1
  62. package/dist/connectors.js +1 -1
  63. package/dist/execution/BaseStepExecutor.d.ts +12 -0
  64. package/dist/execution/BaseStepExecutor.js +23 -0
  65. package/dist/execution/EVMStepExecutor.d.ts +13 -0
  66. package/dist/execution/EVMStepExecutor.js +285 -0
  67. package/dist/execution/RouteExecutionManager.d.ts +9 -8
  68. package/dist/execution/RouteExecutionManager.js +25 -20
  69. package/dist/execution/StatusManager.d.ts +8 -7
  70. package/dist/execution/StatusManager.js +1 -1
  71. package/dist/execution/StepExecutionManager.d.ts +3 -3
  72. package/dist/execution/StepExecutionManager.js +6 -6
  73. package/dist/execution/checkAllowance.d.ts +3 -2
  74. package/dist/execution/checkBalance.d.ts +2 -2
  75. package/dist/execution/index.d.ts +1 -0
  76. package/dist/execution/index.js +1 -0
  77. package/dist/execution/multisig.d.ts +2 -2
  78. package/dist/execution/multisig.js +1 -1
  79. package/dist/execution/prepareRestart.d.ts +2 -3
  80. package/dist/execution/prepareRestart.js +2 -2
  81. package/dist/execution/stepComparison.d.ts +4 -3
  82. package/dist/execution/stepComparison.js +1 -1
  83. package/dist/execution/switchChain.d.ts +4 -3
  84. package/dist/execution/switchChain.js +1 -0
  85. package/dist/{types/internal.types.d.ts → execution/types.d.ts} +33 -96
  86. package/dist/execution/utils.d.ts +2 -2
  87. package/dist/execution/waitForReceivingTransaction.d.ts +3 -3
  88. package/dist/helpers.d.ts +2 -2
  89. package/dist/index.d.ts +5 -1
  90. package/dist/index.js +5 -2
  91. package/dist/providers/EVM.d.ts +10 -0
  92. package/dist/providers/EVM.js +29 -0
  93. package/dist/providers/Solana.d.ts +9 -0
  94. package/dist/providers/Solana.js +33 -0
  95. package/dist/providers/index.d.ts +1 -0
  96. package/dist/providers/index.js +1 -0
  97. package/dist/providers/types.d.ts +12 -0
  98. package/dist/providers/types.js +5 -0
  99. package/dist/request.js +12 -6
  100. package/dist/services/ApiService.d.ts +4 -5
  101. package/dist/services/ApiService.js +3 -3
  102. package/dist/services/ChainsService.d.ts +1 -1
  103. package/dist/services/ChainsService.js +1 -1
  104. package/dist/services/ConfigService.d.ts +9 -6
  105. package/dist/services/ConfigService.js +6 -5
  106. package/dist/typeguards.d.ts +2 -2
  107. package/dist/types/index.d.ts +1 -3
  108. package/dist/types/index.js +1 -3
  109. package/dist/types/internal.d.ts +60 -0
  110. package/dist/types/internal.js +1 -0
  111. package/dist/utils/parseError.d.ts +4 -4
  112. package/dist/utils/parseError.js +1 -1
  113. package/dist/version.d.ts +1 -1
  114. package/dist/version.js +1 -1
  115. package/package.json +6 -5
  116. package/dist/cjs/execution/StepExecutor.d.ts +0 -15
  117. package/dist/cjs/execution/StepExecutor.js +0 -62
  118. package/dist/execution/StepExecutor.d.ts +0 -15
  119. package/dist/execution/StepExecutor.js +0 -58
  120. /package/dist/cjs/{types/internal.types.js → execution/types.js} +0 -0
  121. /package/dist/{types/internal.types.js → execution/types.js} +0 -0
@@ -0,0 +1,292 @@
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.EVMStepExecutor = void 0;
7
+ const viem_1 = require("viem");
8
+ const ApiService_1 = __importDefault(require("../services/ApiService"));
9
+ const ChainsService_1 = require("../services/ChainsService");
10
+ const ConfigService_1 = require("../services/ConfigService");
11
+ const utils_1 = require("../utils");
12
+ const errors_1 = require("../utils/errors");
13
+ const parseError_1 = require("../utils/parseError");
14
+ const utils_2 = require("../utils/utils");
15
+ const BaseStepExecutor_1 = require("./BaseStepExecutor");
16
+ const checkAllowance_1 = require("./checkAllowance");
17
+ const checkBalance_1 = require("./checkBalance");
18
+ const multisig_1 = require("./multisig");
19
+ const stepComparison_1 = require("./stepComparison");
20
+ const switchChain_1 = require("./switchChain");
21
+ const utils_3 = require("./utils");
22
+ const waitForReceivingTransaction_1 = require("./waitForReceivingTransaction");
23
+ class EVMStepExecutor extends BaseStepExecutor_1.BaseStepExecutor {
24
+ constructor(options) {
25
+ super(options);
26
+ // TODO: add checkChain method and update wallet client inside executors
27
+ // This can come in handy when we execute multiple routes simultaneously and
28
+ // should be sure that we are on the right chain when waiting for transactions.
29
+ this.checkChain = () => {
30
+ throw new Error('checkChain is not implemented.');
31
+ };
32
+ this.executeStep = async (step) => {
33
+ // Make sure that the chain is still correct
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 updatedWalletClient = await (0, switchChain_1.switchChain)(this.walletClient, this.statusManager, step, this.settings.switchChainHook, this.allowUserInteraction);
42
+ if (!updatedWalletClient) {
43
+ // Chain switch was not successful, stop execution here
44
+ return step;
45
+ }
46
+ this.walletClient = updatedWalletClient;
47
+ }
48
+ const client = this.walletClient.extend(viem_1.publicActions);
49
+ const config = ConfigService_1.ConfigService.getInstance().getConfig();
50
+ const isMultisigWalletClient = !!config.multisig?.isMultisigWalletClient;
51
+ const multisigBatchTransactions = [];
52
+ const shouldBatchTransactions = config.multisig?.shouldBatchTransactions &&
53
+ !!config.multisig.sendBatchTransaction;
54
+ step.execution = this.statusManager.initExecutionObject(step);
55
+ const chainsService = ChainsService_1.ChainsService.getInstance();
56
+ const fromChain = await chainsService.getChainById(step.action.fromChainId);
57
+ const toChain = await chainsService.getChainById(step.action.toChainId);
58
+ const isBridgeExecution = fromChain.id !== toChain.id;
59
+ const currentProcessType = isBridgeExecution ? 'CROSS_CHAIN' : 'SWAP';
60
+ // STEP 1: Check allowance
61
+ const existingProcess = step.execution.process.find((p) => p.type === currentProcessType);
62
+ // Check token approval only if fromToken is not the native token => no approval needed in that case
63
+ const checkForAllowance = !existingProcess?.txHash &&
64
+ !(0, utils_2.isZeroAddress)(step.action.fromToken.address) &&
65
+ (shouldBatchTransactions || !isMultisigWalletClient);
66
+ if (checkForAllowance) {
67
+ const data = await (0, checkAllowance_1.checkAllowance)(client, step, this.statusManager, this.settings, fromChain, this.allowUserInteraction, shouldBatchTransactions);
68
+ if (data) {
69
+ // allowance doesn't need value
70
+ const baseTransaction = {
71
+ to: step.action.fromToken.address,
72
+ data,
73
+ };
74
+ multisigBatchTransactions.push(baseTransaction);
75
+ }
76
+ }
77
+ // STEP 2: Get transaction
78
+ let process = this.statusManager.findOrCreateProcess(step, currentProcessType);
79
+ if (process.status !== 'DONE') {
80
+ const multisigProcess = step.execution.process.find((p) => !!p.multisigTxHash);
81
+ try {
82
+ if (isMultisigWalletClient && multisigProcess) {
83
+ const multisigTxHash = multisigProcess.multisigTxHash;
84
+ if (!multisigTxHash) {
85
+ throw new errors_1.ValidationError('Multisig internal transaction hash is undefined.');
86
+ }
87
+ await (0, multisig_1.updateMultisigRouteProcess)(multisigTxHash, step, this.statusManager, process.type, fromChain);
88
+ }
89
+ let txHash;
90
+ if (process.txHash) {
91
+ // Make sure that the chain is still correct
92
+ const updatedWalletClient = await (0, switchChain_1.switchChain)(this.walletClient, this.statusManager, step, this.settings.switchChainHook, this.allowUserInteraction);
93
+ if (!updatedWalletClient) {
94
+ // Chain switch was not successful, stop execution here
95
+ return step;
96
+ }
97
+ this.walletClient = updatedWalletClient;
98
+ // Load exiting transaction
99
+ txHash = process.txHash;
100
+ }
101
+ else {
102
+ process = this.statusManager.updateProcess(step, process.type, 'STARTED');
103
+ // Check balance
104
+ await (0, checkBalance_1.checkBalance)(client.account.address, step);
105
+ // Create new transaction
106
+ if (!step.transactionRequest) {
107
+ const updatedStep = await ApiService_1.default.getStepTransaction(step);
108
+ const comparedStep = await (0, stepComparison_1.stepComparison)(this.statusManager, step, updatedStep, this.settings, this.allowUserInteraction);
109
+ step = {
110
+ ...comparedStep,
111
+ execution: step.execution,
112
+ };
113
+ }
114
+ if (!step.transactionRequest) {
115
+ throw new errors_1.TransactionError(errors_1.LiFiErrorCode.TransactionUnprepared, 'Unable to prepare transaction.');
116
+ }
117
+ // STEP 3: Send the transaction
118
+ // Make sure that the chain is still correct
119
+ const updatedWalletClient = await (0, switchChain_1.switchChain)(this.walletClient, this.statusManager, step, this.settings.switchChainHook, this.allowUserInteraction);
120
+ if (!updatedWalletClient) {
121
+ // Chain switch was not successful, stop execution here
122
+ return step;
123
+ }
124
+ this.walletClient = updatedWalletClient;
125
+ process = this.statusManager.updateProcess(step, process.type, 'ACTION_REQUIRED');
126
+ if (!this.allowUserInteraction) {
127
+ return step;
128
+ }
129
+ let transactionRequest = {
130
+ to: step.transactionRequest.to,
131
+ from: step.transactionRequest.from,
132
+ data: step.transactionRequest.data,
133
+ value: step.transactionRequest.value
134
+ ? BigInt(step.transactionRequest.value)
135
+ : undefined,
136
+ gas: step.transactionRequest.gasLimit
137
+ ? BigInt(step.transactionRequest.gasLimit)
138
+ : undefined,
139
+ // gasPrice: step.transactionRequest.gasPrice
140
+ // ? BigInt(step.transactionRequest.gasPrice as string)
141
+ // : undefined,
142
+ // maxFeePerGas: step.transactionRequest.maxFeePerGas
143
+ // ? BigInt(step.transactionRequest.maxFeePerGas as string)
144
+ // : undefined,
145
+ maxPriorityFeePerGas: this.walletClient.account?.type === 'local'
146
+ ? await (0, utils_1.getMaxPriorityFeePerGas)(client)
147
+ : step.transactionRequest.maxPriorityFeePerGas
148
+ ? BigInt(step.transactionRequest.maxPriorityFeePerGas)
149
+ : undefined,
150
+ };
151
+ if (this.settings.updateTransactionRequestHook) {
152
+ const customizedTransactionRequest = await this.settings.updateTransactionRequestHook({
153
+ requestType: 'transaction',
154
+ ...transactionRequest,
155
+ });
156
+ transactionRequest = {
157
+ ...transactionRequest,
158
+ ...customizedTransactionRequest,
159
+ };
160
+ }
161
+ if (shouldBatchTransactions &&
162
+ config.multisig?.sendBatchTransaction) {
163
+ if (transactionRequest.to && transactionRequest.data) {
164
+ const populatedTransaction = {
165
+ value: transactionRequest.value,
166
+ to: transactionRequest.to,
167
+ data: transactionRequest.data,
168
+ };
169
+ multisigBatchTransactions.push(populatedTransaction);
170
+ txHash = await config.multisig?.sendBatchTransaction(multisigBatchTransactions);
171
+ }
172
+ else {
173
+ throw new errors_1.TransactionError(errors_1.LiFiErrorCode.TransactionUnprepared, 'Unable to prepare transaction.');
174
+ }
175
+ }
176
+ else {
177
+ txHash = await this.walletClient.sendTransaction({
178
+ to: transactionRequest.to,
179
+ account: this.walletClient.account,
180
+ data: transactionRequest.data,
181
+ gas: transactionRequest.gas,
182
+ gasPrice: transactionRequest.gasPrice,
183
+ maxFeePerGas: transactionRequest.maxFeePerGas,
184
+ maxPriorityFeePerGas: transactionRequest.maxPriorityFeePerGas,
185
+ chain: null,
186
+ });
187
+ }
188
+ // STEP 4: Wait for the transaction
189
+ if (isMultisigWalletClient) {
190
+ process = this.statusManager.updateProcess(step, process.type, 'ACTION_REQUIRED', {
191
+ multisigTxHash: txHash,
192
+ });
193
+ }
194
+ else {
195
+ process = this.statusManager.updateProcess(step, process.type, 'PENDING', {
196
+ txHash: txHash,
197
+ txLink: `${fromChain.metamask.blockExplorerUrls[0]}tx/${txHash}`,
198
+ });
199
+ }
200
+ }
201
+ let replacementReason;
202
+ const transactionReceipt = await client.waitForTransactionReceipt({
203
+ hash: txHash,
204
+ onReplaced: (response) => {
205
+ replacementReason = response.reason;
206
+ this.statusManager.updateProcess(step, process.type, 'PENDING', {
207
+ txHash: response.transaction.hash,
208
+ txLink: `${fromChain.metamask.blockExplorerUrls[0]}tx/${response.transaction.hash}`,
209
+ });
210
+ },
211
+ });
212
+ if (replacementReason === 'cancelled') {
213
+ throw new errors_1.TransactionError(errors_1.LiFiErrorCode.TransactionCanceled, 'User canceled transaction.');
214
+ }
215
+ // if it's multisig wallet client and the process is in ACTION_REQUIRED
216
+ // then signatures are still needed
217
+ if (isMultisigWalletClient && process.status === 'ACTION_REQUIRED') {
218
+ await (0, multisig_1.updateMultisigRouteProcess)(transactionReceipt.transactionHash, step, this.statusManager, process.type, fromChain);
219
+ }
220
+ if (!isMultisigWalletClient) {
221
+ process = this.statusManager.updateProcess(step, process.type, 'PENDING', {
222
+ txHash: transactionReceipt.transactionHash,
223
+ txLink: `${fromChain.metamask.blockExplorerUrls[0]}tx/${transactionReceipt.transactionHash}`,
224
+ });
225
+ }
226
+ if (isBridgeExecution) {
227
+ process = this.statusManager.updateProcess(step, process.type, 'DONE');
228
+ }
229
+ }
230
+ catch (e) {
231
+ const error = await (0, parseError_1.parseError)(e, step, process);
232
+ process = this.statusManager.updateProcess(step, process.type, 'FAILED', {
233
+ error: {
234
+ message: error.message,
235
+ htmlMessage: error.htmlMessage,
236
+ code: error.code,
237
+ },
238
+ });
239
+ this.statusManager.updateExecution(step, 'FAILED');
240
+ throw error;
241
+ }
242
+ }
243
+ // STEP 5: Wait for the receiving chain
244
+ const processTxHash = process.txHash;
245
+ if (isBridgeExecution) {
246
+ process = this.statusManager.findOrCreateProcess(step, 'RECEIVING_CHAIN', 'PENDING');
247
+ }
248
+ let statusResponse;
249
+ try {
250
+ if (!processTxHash) {
251
+ throw new Error('Transaction hash is undefined.');
252
+ }
253
+ statusResponse = (await (0, waitForReceivingTransaction_1.waitForReceivingTransaction)(processTxHash, this.statusManager, process.type, step));
254
+ const statusReceiving = statusResponse.receiving;
255
+ process = this.statusManager.updateProcess(step, process.type, 'DONE', {
256
+ substatus: statusResponse.substatus,
257
+ substatusMessage: statusResponse.substatusMessage ||
258
+ (0, utils_3.getSubstatusMessage)(statusResponse.status, statusResponse.substatus),
259
+ txHash: statusReceiving?.txHash,
260
+ txLink: `${toChain.metamask.blockExplorerUrls[0]}tx/${statusReceiving?.txHash}`,
261
+ });
262
+ this.statusManager.updateExecution(step, 'DONE', {
263
+ fromAmount: statusResponse.sending.amount,
264
+ toAmount: statusReceiving?.amount,
265
+ toToken: statusReceiving?.token,
266
+ gasAmount: statusResponse.sending.gasAmount,
267
+ gasAmountUSD: statusResponse.sending.gasAmountUSD,
268
+ gasPrice: statusResponse.sending.gasPrice,
269
+ gasToken: statusResponse.sending.gasToken,
270
+ gasUsed: statusResponse.sending.gasUsed,
271
+ });
272
+ }
273
+ catch (e) {
274
+ const htmlMessage = await (0, parseError_1.getTransactionFailedMessage)(step, process.txLink);
275
+ process = this.statusManager.updateProcess(step, process.type, 'FAILED', {
276
+ error: {
277
+ code: errors_1.LiFiErrorCode.TransactionFailed,
278
+ message: 'Failed while waiting for receiving chain.',
279
+ htmlMessage,
280
+ },
281
+ });
282
+ this.statusManager.updateExecution(step, 'FAILED');
283
+ console.warn(e);
284
+ throw e;
285
+ }
286
+ // DONE
287
+ return step;
288
+ };
289
+ this.walletClient = options.walletClient;
290
+ }
291
+ }
292
+ exports.EVMStepExecutor = EVMStepExecutor;
@@ -1,30 +1,30 @@
1
1
  import type { Route } from '@lifi/types';
2
- import type { WalletClient } from 'viem';
3
- import ConfigService from '../services/ConfigService';
4
- import type { ConfigUpdate, ExecutionSettings } from '../types';
2
+ import type { ProviderType, SDKProvider } from '../providers';
3
+ import { ConfigService } from '../services/ConfigService';
4
+ import type { SDKOptions } from '../types';
5
+ import type { ExecutionSettings } from './types';
5
6
  export declare class RouteExecutionManager {
6
7
  private executionDictionary;
7
8
  private executionPromiseDictionary;
8
9
  protected configService: ConfigService;
9
- constructor(configUpdate: ConfigUpdate);
10
+ private providers?;
11
+ constructor(options: SDKOptions);
10
12
  /**
11
13
  * Execute a route.
12
- * @param walletClient - The walletClient required to send the transactions.
13
14
  * @param route - The route that should be executed. Cannot be an active route.
14
15
  * @param settings - An object containing settings and callbacks.
15
16
  * @returns The executed route.
16
17
  * @throws {LiFiError} Throws a LiFiError if the execution fails.
17
18
  */
18
- executeRoute: (walletClient: WalletClient, route: Route, settings?: ExecutionSettings) => Promise<Route>;
19
+ executeRoute: (route: Route, settings?: ExecutionSettings) => Promise<Route>;
19
20
  /**
20
21
  * Resume the execution of a route that has been stopped or had an error while executing.
21
- * @param walletClient - The walletClient required to send the transactions.
22
22
  * @param route - The route that is to be executed. Cannot be an active route.
23
23
  * @param settings - An object containing settings and callbacks.
24
24
  * @returns The executed route.
25
25
  * @throws {LiFiError} Throws a LiFiError if the execution fails.
26
26
  */
27
- resumeRoute: (walletClient: WalletClient, route: Route, settings?: ExecutionSettings) => Promise<Route>;
27
+ resumeRoute: (route: Route, settings?: ExecutionSettings) => Promise<Route>;
28
28
  private executeSteps;
29
29
  /**
30
30
  * Updates route execution to background or foreground state.
@@ -56,4 +56,5 @@ export declare class RouteExecutionManager {
56
56
  * @returns The updated route.
57
57
  */
58
58
  getActiveRoute: (route: Route) => Route | undefined;
59
+ getProvider: (type: ProviderType) => SDKProvider | undefined;
59
60
  }
@@ -1,27 +1,22 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.RouteExecutionManager = void 0;
7
- const ConfigService_1 = __importDefault(require("../services/ConfigService"));
4
+ const ConfigService_1 = require("../services/ConfigService");
8
5
  const errors_1 = require("../utils/errors");
9
6
  const StatusManager_1 = require("./StatusManager");
10
- const StepExecutor_1 = require("./StepExecutor");
11
7
  const prepareRestart_1 = require("./prepareRestart");
12
8
  class RouteExecutionManager {
13
- constructor(configUpdate) {
9
+ constructor(options) {
14
10
  this.executionDictionary = {};
15
11
  this.executionPromiseDictionary = {};
16
12
  /**
17
13
  * Execute a route.
18
- * @param walletClient - The walletClient required to send the transactions.
19
14
  * @param route - The route that should be executed. Cannot be an active route.
20
15
  * @param settings - An object containing settings and callbacks.
21
16
  * @returns The executed route.
22
17
  * @throws {LiFiError} Throws a LiFiError if the execution fails.
23
18
  */
24
- this.executeRoute = async (walletClient, route, settings) => {
19
+ this.executeRoute = async (route, settings) => {
25
20
  // Deep clone to prevent side effects
26
21
  const clonedRoute = structuredClone(route);
27
22
  let executionPromise = this.executionPromiseDictionary[clonedRoute.id];
@@ -29,24 +24,23 @@ class RouteExecutionManager {
29
24
  if (executionPromise) {
30
25
  return executionPromise;
31
26
  }
32
- executionPromise = this.executeSteps(walletClient, clonedRoute, settings);
27
+ executionPromise = this.executeSteps(clonedRoute, settings);
33
28
  this.executionPromiseDictionary[clonedRoute.id] = executionPromise;
34
29
  return executionPromise;
35
30
  };
36
31
  /**
37
32
  * Resume the execution of a route that has been stopped or had an error while executing.
38
- * @param walletClient - The walletClient required to send the transactions.
39
33
  * @param route - The route that is to be executed. Cannot be an active route.
40
34
  * @param settings - An object containing settings and callbacks.
41
35
  * @returns The executed route.
42
36
  * @throws {LiFiError} Throws a LiFiError if the execution fails.
43
37
  */
44
- this.resumeRoute = async (walletClient, route, settings) => {
38
+ this.resumeRoute = async (route, settings) => {
45
39
  // Deep clone to prevent side effects
46
40
  const clonedRoute = structuredClone(route);
47
41
  const execution = this.executionDictionary[clonedRoute.id];
48
42
  if (execution) {
49
- const executionHalted = execution.executors.some((executor) => executor.executionStopped);
43
+ const executionHalted = execution.executors.some((executor) => !executor.allowExecution);
50
44
  if (!executionHalted) {
51
45
  // Check if we want to resume route execution in the background
52
46
  this.updateRouteExecution(route, {
@@ -56,12 +50,12 @@ class RouteExecutionManager {
56
50
  return executionPromise ?? clonedRoute;
57
51
  }
58
52
  }
59
- await (0, prepareRestart_1.prepareRestart)(clonedRoute, walletClient);
60
- const executionPromise = this.executeSteps(walletClient, clonedRoute, settings);
53
+ await (0, prepareRestart_1.prepareRestart)(clonedRoute);
54
+ const executionPromise = this.executeSteps(clonedRoute, settings);
61
55
  this.executionPromiseDictionary[clonedRoute.id] = executionPromise;
62
56
  return executionPromise;
63
57
  };
64
- this.executeSteps = async (walletClient, route, settings) => {
58
+ this.executeSteps = async (route, settings) => {
65
59
  const config = this.configService.getConfig();
66
60
  const execution = {
67
61
  route,
@@ -93,17 +87,24 @@ class RouteExecutionManager {
93
87
  step.action.fromAmount = previousStep.execution.toAmount;
94
88
  }
95
89
  try {
96
- const stepExecutor = new StepExecutor_1.StepExecutor(statusManager, execution.settings);
90
+ const provider = this.providers?.find((provider) => provider.isProviderStep(step));
91
+ if (!provider) {
92
+ throw new Error('SDK Execution Provider not found.');
93
+ }
94
+ const stepExecutor = await provider.getStepExecutor({
95
+ statusManager,
96
+ settings: execution.settings,
97
+ });
97
98
  execution.executors.push(stepExecutor);
98
99
  // Check if we want to execute this step in the background
99
100
  this.updateRouteExecution(route, execution.settings);
100
- const executedStep = await stepExecutor.executeStep(walletClient, step);
101
+ const executedStep = await stepExecutor.executeStep(step);
101
102
  // We may reach this point if user interaction isn't allowed. We want to stop execution until we resume it
102
103
  if (executedStep.execution?.status !== 'DONE') {
103
104
  this.stopExecution(route);
104
105
  }
105
106
  // 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
+ if (stepExecutor.allowExecution) {
107
108
  return route;
108
109
  }
109
110
  }
@@ -169,7 +170,7 @@ class RouteExecutionManager {
169
170
  executor.setInteraction({
170
171
  allowInteraction: false,
171
172
  allowUpdates: false,
172
- stopExecution: true,
173
+ allowExecution: false,
173
174
  });
174
175
  }
175
176
  delete this.executionDictionary[route.id];
@@ -192,11 +193,12 @@ class RouteExecutionManager {
192
193
  this.getActiveRoute = (route) => {
193
194
  return this.executionDictionary[route.id]?.route;
194
195
  };
195
- this.configService = ConfigService_1.default.getInstance();
196
- if (configUpdate) {
197
- // Update API urls before we request chains
198
- this.configService.updateConfig(configUpdate);
199
- }
196
+ this.getProvider = (type) => {
197
+ return this.providers?.find((provider) => provider.type === type);
198
+ };
199
+ this.configService = ConfigService_1.ConfigService.getInstance();
200
+ this.configService.updateConfig(options);
201
+ this.providers = options.providers;
200
202
  }
201
203
  }
202
204
  exports.RouteExecutionManager = RouteExecutionManager;
@@ -1,4 +1,5 @@
1
- import type { Execution, InternalExecutionSettings, LifiStep, Process, ProcessType, Route, Status, Token } from '../types';
1
+ import type { Execution, LiFiStep, Process, ProcessType, Route, Status, Token } from '@lifi/types';
2
+ import type { InternalExecutionSettings } from './types';
2
3
  interface Receipt {
3
4
  fromAmount?: string;
4
5
  toAmount?: string;
@@ -29,7 +30,7 @@ export declare class StatusManager {
29
30
  * @param step The current step in execution
30
31
  * @returns The initialized execution object for this step and a function to update this step
31
32
  */
32
- initExecutionObject: (step: LifiStep) => Execution;
33
+ initExecutionObject: (step: LiFiStep) => Execution;
33
34
  /**
34
35
  * Updates the execution object of a Step.
35
36
  * @param step The current step in execution
@@ -37,7 +38,7 @@ export declare class StatusManager {
37
38
  * @param receipt Optional. Information about received tokens
38
39
  * @returns The step with the updated execution object
39
40
  */
40
- updateExecution(step: LifiStep, status: Status, receipt?: Receipt): LifiStep;
41
+ updateExecution(step: LiFiStep, status: Status, receipt?: Receipt): LiFiStep;
41
42
  /**
42
43
  * Create and push a new process into the execution.
43
44
  * @param step The step that should contain the new process.
@@ -45,7 +46,7 @@ export declare class StatusManager {
45
46
  * @param status By default created procces is set to the STARTED status. We can override new process with the needed status.
46
47
  * @returns Returns process.
47
48
  */
48
- findOrCreateProcess: (step: LifiStep, type: ProcessType, status?: Status) => Process;
49
+ findOrCreateProcess: (step: LiFiStep, type: ProcessType, status?: Status) => Process;
49
50
  /**
50
51
  * Update a process object.
51
52
  * @param step The step where the process should be updated
@@ -54,14 +55,14 @@ export declare class StatusManager {
54
55
  * @param [params] Additional parameters to append to the process.
55
56
  * @returns The update process
56
57
  */
57
- updateProcess: (step: LifiStep, type: ProcessType, status: Status, params?: OptionalParameters) => Process;
58
+ updateProcess: (step: LiFiStep, type: ProcessType, status: Status, params?: OptionalParameters) => Process;
58
59
  /**
59
60
  * Remove a process from the execution
60
61
  * @param step The step where the process should be removed from
61
62
  * @param type The process type to remove
62
63
  */
63
- removeProcess: (step: LifiStep, type: ProcessType) => void;
64
- updateStepInRoute: (step: LifiStep) => LifiStep;
64
+ removeProcess: (step: LiFiStep, type: ProcessType) => void;
65
+ updateStepInRoute: (step: LiFiStep) => LiFiStep;
65
66
  allowUpdates(value: boolean): void;
66
67
  }
67
68
  export {};
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StatusManager = void 0;
4
- const types_1 = require("../types");
4
+ const types_1 = require("@lifi/types");
5
5
  const utils_1 = require("./utils");
6
6
  /**
7
7
  * Manages status updates of a route and provides various functions for tracking processes
@@ -1,7 +1,7 @@
1
- import type { Execution } from '@lifi/types';
2
- import type { ExecutionParams } from '../types';
1
+ import type { LiFiStep } from '@lifi/types';
2
+ import type { ExecutionParams } from './types';
3
3
  export declare class StepExecutionManager {
4
4
  allowUserInteraction: boolean;
5
5
  allowInteraction: (value: boolean) => void;
6
- execute: ({ walletClient, step, statusManager, settings, }: ExecutionParams) => Promise<Execution>;
6
+ execute: ({ walletClient, step, statusManager, settings, }: ExecutionParams) => Promise<LiFiStep>;
7
7
  }
@@ -6,8 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.StepExecutionManager = void 0;
7
7
  const viem_1 = require("viem");
8
8
  const ApiService_1 = __importDefault(require("../services/ApiService"));
9
- const ChainsService_1 = __importDefault(require("../services/ChainsService"));
10
- const ConfigService_1 = __importDefault(require("../services/ConfigService"));
9
+ const ChainsService_1 = require("../services/ChainsService");
10
+ const ConfigService_1 = require("../services/ConfigService");
11
11
  const utils_1 = require("../utils");
12
12
  const errors_1 = require("../utils/errors");
13
13
  const parseError_1 = require("../utils/parseError");
@@ -27,13 +27,13 @@ class StepExecutionManager {
27
27
  };
28
28
  this.execute = async ({ walletClient, step, statusManager, settings, }) => {
29
29
  const client = walletClient.extend(viem_1.publicActions);
30
- const config = ConfigService_1.default.getInstance().getConfig();
30
+ const config = ConfigService_1.ConfigService.getInstance().getConfig();
31
31
  const isMultisigWalletClient = !!config.multisig?.isMultisigWalletClient;
32
32
  const multisigBatchTransactions = [];
33
33
  const shouldBatchTransactions = config.multisig?.shouldBatchTransactions &&
34
34
  !!config.multisig.sendBatchTransaction;
35
35
  step.execution = statusManager.initExecutionObject(step);
36
- const chainsService = ChainsService_1.default.getInstance();
36
+ const chainsService = ChainsService_1.ChainsService.getInstance();
37
37
  const fromChain = await chainsService.getChainById(step.action.fromChainId);
38
38
  const toChain = await chainsService.getChainById(step.action.toChainId);
39
39
  const isBridgeExecution = fromChain.id !== toChain.id;
@@ -73,7 +73,7 @@ class StepExecutionManager {
73
73
  const updatedWalletClient = await (0, switchChain_1.switchChain)(walletClient, statusManager, step, settings.switchChainHook, this.allowUserInteraction);
74
74
  if (!updatedWalletClient) {
75
75
  // Chain switch was not successful, stop execution here
76
- return step.execution;
76
+ return step;
77
77
  }
78
78
  walletClient = updatedWalletClient;
79
79
  // Load exiting transaction
@@ -100,12 +100,12 @@ class StepExecutionManager {
100
100
  const updatedWalletClient = await (0, switchChain_1.switchChain)(walletClient, statusManager, step, settings.switchChainHook, this.allowUserInteraction);
101
101
  if (!updatedWalletClient) {
102
102
  // Chain switch was not successful, stop execution here
103
- return step.execution;
103
+ return step;
104
104
  }
105
105
  walletClient = updatedWalletClient;
106
106
  process = statusManager.updateProcess(step, process.type, 'ACTION_REQUIRED');
107
107
  if (!this.allowUserInteraction) {
108
- return step.execution;
108
+ return step;
109
109
  }
110
110
  let transactionRequest = {
111
111
  to: step.transactionRequest.to,
@@ -265,7 +265,7 @@ class StepExecutionManager {
265
265
  throw e;
266
266
  }
267
267
  // DONE
268
- return step.execution;
268
+ return step;
269
269
  };
270
270
  }
271
271
  }
@@ -1,4 +1,5 @@
1
+ import type { Chain, LiFiStep } from '@lifi/types';
1
2
  import type { Hash, WalletClient } from 'viem';
2
3
  import type { StatusManager } from '../execution/StatusManager';
3
- import type { Chain, InternalExecutionSettings, LifiStep } from '../types';
4
- export declare const checkAllowance: (walletClient: WalletClient, step: LifiStep, statusManager: StatusManager, settings: InternalExecutionSettings, chain: Chain, allowUserInteraction?: boolean, shouldBatchTransactions?: boolean) => Promise<Hash | void>;
4
+ import type { InternalExecutionSettings } from './types';
5
+ export declare const checkAllowance: (walletClient: WalletClient, step: LiFiStep, statusManager: StatusManager, settings: InternalExecutionSettings, chain: Chain, allowUserInteraction?: boolean, shouldBatchTransactions?: boolean) => Promise<Hash | void>;
@@ -1,2 +1,2 @@
1
- import type { LifiStep } from '..';
2
- export declare const checkBalance: (walletAddress: string, step: LifiStep, depth?: number) => Promise<void>;
1
+ import type { LiFiStep } from '@lifi/types';
2
+ export declare const checkBalance: (walletAddress: string, step: LiFiStep, depth?: number) => Promise<void>;
@@ -1 +1,2 @@
1
1
  export * from './StatusManager';
2
+ export * from './types';
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./StatusManager"), exports);
18
+ __exportStar(require("./types"), exports);
@@ -1,4 +1,4 @@
1
- import type { ExtendedChain, LifiStep, ProcessType } from '@lifi/types';
1
+ import type { ExtendedChain, LiFiStep, ProcessType } from '@lifi/types';
2
2
  import type { Hash } from 'viem';
3
3
  import type { StatusManager } from './StatusManager';
4
- export declare const updateMultisigRouteProcess: (internalTxHash: Hash, step: LifiStep, statusManager: StatusManager, processType: ProcessType, fromChain: ExtendedChain) => Promise<void>;
4
+ export declare const updateMultisigRouteProcess: (internalTxHash: Hash, step: LiFiStep, statusManager: StatusManager, processType: ProcessType, fromChain: ExtendedChain) => Promise<void>;