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