@lifi/sdk 3.0.0-alpha.4 → 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 +28 -21
- package/dist/LiFi.js +20 -13
- package/dist/allowance/setAllowance.d.ts +2 -1
- package/dist/allowance/setAllowance.js +23 -7
- package/dist/cjs/LiFi.d.ts +28 -21
- package/dist/cjs/LiFi.js +20 -13
- package/dist/cjs/allowance/setAllowance.d.ts +2 -1
- package/dist/cjs/allowance/setAllowance.js +23 -7
- 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 +34 -29
- package/dist/cjs/execution/checkAllowance.d.ts +3 -2
- package/dist/cjs/execution/checkAllowance.js +1 -1
- 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} +36 -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 +34 -29
- package/dist/execution/checkAllowance.d.ts +3 -2
- package/dist/execution/checkAllowance.js +1 -1
- 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} +36 -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 +61 -62
- 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
|
@@ -6,7 +6,7 @@ const types_1 = require("../types");
|
|
|
6
6
|
const utils_1 = require("../utils");
|
|
7
7
|
const utils_2 = require("../utils/utils");
|
|
8
8
|
const getAllowance_1 = require("./getAllowance");
|
|
9
|
-
const setAllowance = async (walletClient, tokenAddress, contractAddress, amount, returnPopulatedTransaction) => {
|
|
9
|
+
const setAllowance = async (walletClient, tokenAddress, contractAddress, amount, settings, returnPopulatedTransaction) => {
|
|
10
10
|
const data = (0, viem_1.encodeFunctionData)({
|
|
11
11
|
abi: types_1.approveAbi,
|
|
12
12
|
functionName: 'approve',
|
|
@@ -16,15 +16,31 @@ const setAllowance = async (walletClient, tokenAddress, contractAddress, amount,
|
|
|
16
16
|
return data;
|
|
17
17
|
}
|
|
18
18
|
const client = walletClient.extend(viem_1.publicActions);
|
|
19
|
-
let
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
let transactionRequest = {
|
|
20
|
+
to: tokenAddress,
|
|
21
|
+
data,
|
|
22
|
+
maxPriorityFeePerGas: walletClient.account?.type === 'local'
|
|
23
|
+
? await (0, utils_1.getMaxPriorityFeePerGas)(client)
|
|
24
|
+
: undefined,
|
|
25
|
+
};
|
|
26
|
+
if (settings?.updateTransactionRequestHook) {
|
|
27
|
+
const customizedTransactionRequest = await settings.updateTransactionRequestHook({
|
|
28
|
+
requestType: 'approve',
|
|
29
|
+
...transactionRequest,
|
|
30
|
+
});
|
|
31
|
+
transactionRequest = {
|
|
32
|
+
...transactionRequest,
|
|
33
|
+
...customizedTransactionRequest,
|
|
34
|
+
};
|
|
22
35
|
}
|
|
23
36
|
return client.sendTransaction({
|
|
24
|
-
to:
|
|
37
|
+
to: transactionRequest.to,
|
|
25
38
|
account: walletClient.account,
|
|
26
|
-
data,
|
|
27
|
-
|
|
39
|
+
data: transactionRequest.data,
|
|
40
|
+
gas: transactionRequest.gas,
|
|
41
|
+
gasPrice: transactionRequest.gasPrice,
|
|
42
|
+
maxFeePerGas: transactionRequest.maxFeePerGas,
|
|
43
|
+
maxPriorityFeePerGas: transactionRequest.maxPriorityFeePerGas,
|
|
28
44
|
chain: null,
|
|
29
45
|
});
|
|
30
46
|
};
|
package/dist/cjs/connectors.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import type { ChainId } from '@lifi/types';
|
|
1
2
|
import type { PublicClient } from 'viem';
|
|
2
3
|
import type { Chain } from 'viem/chains';
|
|
3
|
-
import type { ChainId } from './types';
|
|
4
4
|
export declare const getChainById: (chainId: ChainId) => Chain | undefined;
|
|
5
5
|
export declare const getRpcUrl: (chainId: ChainId) => Promise<string>;
|
|
6
6
|
export declare const getRpcUrls: (chainId: ChainId) => Promise<string[]>;
|
|
@@ -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,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseStepExecutor = void 0;
|
|
4
|
+
// Please be careful when changing the defaults as it may break the behavior (e.g., background execution)
|
|
5
|
+
const defaultInteractionSettings = {
|
|
6
|
+
allowInteraction: true,
|
|
7
|
+
allowUpdates: true,
|
|
8
|
+
allowExecution: true,
|
|
9
|
+
};
|
|
10
|
+
class BaseStepExecutor {
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.allowUserInteraction = true;
|
|
13
|
+
this.allowExecution = true;
|
|
14
|
+
this.setInteraction = (settings) => {
|
|
15
|
+
const interactionSettings = {
|
|
16
|
+
...defaultInteractionSettings,
|
|
17
|
+
...settings,
|
|
18
|
+
};
|
|
19
|
+
this.allowUserInteraction = interactionSettings.allowInteraction;
|
|
20
|
+
this.statusManager.allowUpdates(interactionSettings.allowUpdates);
|
|
21
|
+
this.allowExecution = interactionSettings.allowExecution;
|
|
22
|
+
};
|
|
23
|
+
this.statusManager = options.statusManager;
|
|
24
|
+
this.settings = options.settings;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.BaseStepExecutor = BaseStepExecutor;
|
|
@@ -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,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 = __importDefault(require("../services/ChainsService"));
|
|
10
|
+
const ConfigService_1 = __importDefault(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.default.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.default.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,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.
|
|
@@ -4,24 +4,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.RouteExecutionManager = void 0;
|
|
7
|
+
const EVMProvider_1 = require("../providers/EVMProvider");
|
|
7
8
|
const ConfigService_1 = __importDefault(require("../services/ConfigService"));
|
|
8
9
|
const errors_1 = require("../utils/errors");
|
|
9
10
|
const StatusManager_1 = require("./StatusManager");
|
|
10
|
-
const StepExecutor_1 = require("./StepExecutor");
|
|
11
11
|
const prepareRestart_1 = require("./prepareRestart");
|
|
12
12
|
class RouteExecutionManager {
|
|
13
|
-
constructor(
|
|
13
|
+
constructor(options) {
|
|
14
14
|
this.executionDictionary = {};
|
|
15
15
|
this.executionPromiseDictionary = {};
|
|
16
16
|
/**
|
|
17
17
|
* Execute a route.
|
|
18
|
-
* @param walletClient - The walletClient required to send the transactions.
|
|
19
18
|
* @param route - The route that should be executed. Cannot be an active route.
|
|
20
19
|
* @param settings - An object containing settings and callbacks.
|
|
21
20
|
* @returns The executed route.
|
|
22
21
|
* @throws {LiFiError} Throws a LiFiError if the execution fails.
|
|
23
22
|
*/
|
|
24
|
-
this.executeRoute = async (
|
|
23
|
+
this.executeRoute = async (route, settings) => {
|
|
25
24
|
// Deep clone to prevent side effects
|
|
26
25
|
const clonedRoute = structuredClone(route);
|
|
27
26
|
let executionPromise = this.executionPromiseDictionary[clonedRoute.id];
|
|
@@ -29,24 +28,23 @@ class RouteExecutionManager {
|
|
|
29
28
|
if (executionPromise) {
|
|
30
29
|
return executionPromise;
|
|
31
30
|
}
|
|
32
|
-
executionPromise = this.executeSteps(
|
|
31
|
+
executionPromise = this.executeSteps(clonedRoute, settings);
|
|
33
32
|
this.executionPromiseDictionary[clonedRoute.id] = executionPromise;
|
|
34
33
|
return executionPromise;
|
|
35
34
|
};
|
|
36
35
|
/**
|
|
37
36
|
* 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
37
|
* @param route - The route that is to be executed. Cannot be an active route.
|
|
40
38
|
* @param settings - An object containing settings and callbacks.
|
|
41
39
|
* @returns The executed route.
|
|
42
40
|
* @throws {LiFiError} Throws a LiFiError if the execution fails.
|
|
43
41
|
*/
|
|
44
|
-
this.resumeRoute = async (
|
|
42
|
+
this.resumeRoute = async (route, settings) => {
|
|
45
43
|
// Deep clone to prevent side effects
|
|
46
44
|
const clonedRoute = structuredClone(route);
|
|
47
45
|
const execution = this.executionDictionary[clonedRoute.id];
|
|
48
46
|
if (execution) {
|
|
49
|
-
const executionHalted = execution.executors.some((executor) => executor.
|
|
47
|
+
const executionHalted = execution.executors.some((executor) => !executor.allowExecution);
|
|
50
48
|
if (!executionHalted) {
|
|
51
49
|
// Check if we want to resume route execution in the background
|
|
52
50
|
this.updateRouteExecution(route, {
|
|
@@ -56,12 +54,12 @@ class RouteExecutionManager {
|
|
|
56
54
|
return executionPromise ?? clonedRoute;
|
|
57
55
|
}
|
|
58
56
|
}
|
|
59
|
-
await (0, prepareRestart_1.prepareRestart)(clonedRoute
|
|
60
|
-
const executionPromise = this.executeSteps(
|
|
57
|
+
await (0, prepareRestart_1.prepareRestart)(clonedRoute);
|
|
58
|
+
const executionPromise = this.executeSteps(clonedRoute, settings);
|
|
61
59
|
this.executionPromiseDictionary[clonedRoute.id] = executionPromise;
|
|
62
60
|
return executionPromise;
|
|
63
61
|
};
|
|
64
|
-
this.executeSteps = async (
|
|
62
|
+
this.executeSteps = async (route, settings) => {
|
|
65
63
|
const config = this.configService.getConfig();
|
|
66
64
|
const execution = {
|
|
67
65
|
route,
|
|
@@ -93,17 +91,21 @@ class RouteExecutionManager {
|
|
|
93
91
|
step.action.fromAmount = previousStep.execution.toAmount;
|
|
94
92
|
}
|
|
95
93
|
try {
|
|
96
|
-
const
|
|
94
|
+
const provider = this.providers.find((provider) => provider.isProviderStep(step));
|
|
95
|
+
const stepExecutor = await provider.getStepExecutor({
|
|
96
|
+
statusManager,
|
|
97
|
+
settings: execution.settings,
|
|
98
|
+
});
|
|
97
99
|
execution.executors.push(stepExecutor);
|
|
98
100
|
// Check if we want to execute this step in the background
|
|
99
101
|
this.updateRouteExecution(route, execution.settings);
|
|
100
|
-
const executedStep = await stepExecutor.executeStep(
|
|
102
|
+
const executedStep = await stepExecutor.executeStep(step);
|
|
101
103
|
// We may reach this point if user interaction isn't allowed. We want to stop execution until we resume it
|
|
102
104
|
if (executedStep.execution?.status !== 'DONE') {
|
|
103
105
|
this.stopExecution(route);
|
|
104
106
|
}
|
|
105
107
|
// Execution stopped during the current step, we don't want to continue to the next step so we return already
|
|
106
|
-
if (stepExecutor.
|
|
108
|
+
if (stepExecutor.allowExecution) {
|
|
107
109
|
return route;
|
|
108
110
|
}
|
|
109
111
|
}
|
|
@@ -169,7 +171,7 @@ class RouteExecutionManager {
|
|
|
169
171
|
executor.setInteraction({
|
|
170
172
|
allowInteraction: false,
|
|
171
173
|
allowUpdates: false,
|
|
172
|
-
|
|
174
|
+
allowExecution: false,
|
|
173
175
|
});
|
|
174
176
|
}
|
|
175
177
|
delete this.executionDictionary[route.id];
|
|
@@ -193,10 +195,8 @@ class RouteExecutionManager {
|
|
|
193
195
|
return this.executionDictionary[route.id]?.route;
|
|
194
196
|
};
|
|
195
197
|
this.configService = ConfigService_1.default.getInstance();
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
this.configService.updateConfig(configUpdate);
|
|
199
|
-
}
|
|
198
|
+
this.configService.updateConfig(options);
|
|
199
|
+
this.providers = options.providers ?? [new EVMProvider_1.EVMProvider()];
|
|
200
200
|
}
|
|
201
201
|
}
|
|
202
202
|
exports.RouteExecutionManager = RouteExecutionManager;
|
|
@@ -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
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.StatusManager = void 0;
|
|
4
|
-
const types_1 = require("
|
|
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 {
|
|
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
|
}
|