@lifi/sdk 2.2.2 → 3.0.0-alpha.1
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 +69 -62
- package/dist/LiFi.js +71 -60
- package/dist/allowance/getAllowance.d.ts +6 -0
- package/dist/allowance/getAllowance.js +78 -0
- package/dist/allowance/index.d.ts +3 -2
- package/dist/allowance/index.js +3 -2
- package/dist/allowance/setAllowance.d.ts +5 -0
- package/dist/allowance/setAllowance.js +50 -0
- package/dist/allowance/types.d.ts +27 -0
- package/dist/allowance/types.js +1 -0
- package/dist/balance/getBalance.d.ts +2 -0
- package/dist/balance/getBalance.js +93 -0
- package/dist/balance/getTokenBalance.d.ts +2 -2
- package/dist/balance/getTokenBalance.js +4 -4
- package/dist/balance/index.d.ts +1 -12
- package/dist/balance/index.js +1 -9
- package/dist/cjs/LiFi.d.ts +69 -62
- package/dist/cjs/LiFi.js +69 -58
- package/dist/cjs/allowance/getAllowance.d.ts +6 -0
- package/dist/cjs/allowance/getAllowance.js +85 -0
- package/dist/cjs/allowance/index.d.ts +3 -2
- package/dist/cjs/allowance/index.js +3 -2
- package/dist/cjs/allowance/setAllowance.d.ts +5 -0
- package/dist/cjs/allowance/setAllowance.js +56 -0
- package/dist/cjs/allowance/types.d.ts +27 -0
- package/dist/cjs/allowance/types.js +2 -0
- package/dist/cjs/balance/getBalance.d.ts +2 -0
- package/dist/cjs/balance/getBalance.js +97 -0
- package/dist/cjs/balance/getTokenBalance.d.ts +2 -2
- package/dist/cjs/balance/getTokenBalance.js +6 -9
- package/dist/cjs/balance/index.d.ts +1 -12
- package/dist/cjs/balance/index.js +1 -9
- package/dist/cjs/connectors.d.ts +7 -5
- package/dist/cjs/connectors.js +45 -37
- package/dist/cjs/constants.d.ts +6 -0
- package/dist/cjs/constants.js +9 -0
- package/dist/cjs/execution/RouteExecutionManager.d.ts +24 -30
- package/dist/cjs/execution/RouteExecutionManager.js +27 -45
- package/dist/cjs/execution/StatusManager.d.ts +22 -23
- package/dist/cjs/execution/StatusManager.js +21 -22
- package/dist/cjs/execution/StepExecutionManager.d.ts +3 -3
- package/dist/cjs/execution/StepExecutionManager.js +118 -125
- package/dist/cjs/execution/StepExecutor.d.ts +4 -4
- package/dist/cjs/execution/StepExecutor.js +6 -6
- package/dist/cjs/execution/checkAllowance.d.ts +4 -0
- package/dist/cjs/execution/checkAllowance.js +77 -0
- package/dist/cjs/execution/checkBalance.d.ts +2 -0
- package/dist/cjs/execution/checkBalance.js +42 -0
- package/dist/cjs/execution/multisig.d.ts +4 -3
- package/dist/cjs/execution/multisig.js +9 -11
- package/dist/cjs/execution/prepareRestart.d.ts +3 -0
- package/dist/cjs/execution/prepareRestart.js +65 -0
- package/dist/cjs/execution/stepComparison.d.ts +3 -3
- package/dist/cjs/execution/stepComparison.js +2 -2
- package/dist/cjs/execution/switchChain.d.ts +13 -8
- package/dist/cjs/execution/switchChain.js +16 -11
- package/dist/cjs/execution/utils.d.ts +9 -3
- package/dist/cjs/execution/utils.js +19 -66
- package/dist/cjs/execution/waitForReceivingTransaction.d.ts +3 -0
- package/dist/cjs/execution/waitForReceivingTransaction.js +58 -0
- package/dist/cjs/helpers.d.ts +7 -17
- package/dist/cjs/helpers.js +6 -38
- package/dist/cjs/request.js +1 -1
- package/dist/cjs/services/ApiService.d.ts +2 -2
- package/dist/cjs/services/ApiService.js +2 -0
- package/dist/cjs/services/ChainsService.d.ts +1 -1
- package/dist/cjs/services/ConfigService.d.ts +1 -1
- package/dist/cjs/services/ConfigService.js +1 -2
- package/dist/cjs/typeguards.d.ts +1 -1
- package/dist/cjs/types/abi.d.ts +5 -0
- package/dist/cjs/types/abi.js +45 -0
- package/dist/cjs/types/index.d.ts +2 -2
- package/dist/cjs/types/index.js +3 -3
- package/dist/cjs/types/internal.types.d.ts +24 -30
- package/dist/cjs/utils/errors.d.ts +15 -14
- package/dist/cjs/utils/errors.js +38 -37
- package/dist/cjs/utils/getMaxPriorityFeePerGas.d.ts +2 -0
- package/dist/cjs/utils/getMaxPriorityFeePerGas.js +22 -0
- package/dist/cjs/utils/index.d.ts +5 -0
- package/dist/cjs/utils/index.js +21 -0
- package/dist/cjs/utils/median.d.ts +1 -0
- package/dist/cjs/utils/median.js +15 -0
- package/dist/cjs/utils/parseError.d.ts +4 -4
- package/dist/cjs/utils/parseError.js +41 -38
- package/dist/cjs/utils/utils.d.ts +1 -16
- package/dist/cjs/utils/utils.js +9 -78
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/connectors.d.ts +7 -5
- package/dist/connectors.js +19 -35
- package/dist/constants.d.ts +6 -0
- package/dist/constants.js +6 -0
- package/dist/execution/RouteExecutionManager.d.ts +24 -30
- package/dist/execution/RouteExecutionManager.js +27 -45
- package/dist/execution/StatusManager.d.ts +22 -23
- package/dist/execution/StatusManager.js +22 -23
- package/dist/execution/StepExecutionManager.d.ts +3 -3
- package/dist/execution/StepExecutionManager.js +116 -123
- package/dist/execution/StepExecutor.d.ts +4 -4
- package/dist/execution/StepExecutor.js +6 -6
- package/dist/execution/checkAllowance.d.ts +4 -0
- package/dist/execution/checkAllowance.js +73 -0
- package/dist/execution/checkBalance.d.ts +2 -0
- package/dist/execution/checkBalance.js +38 -0
- package/dist/execution/multisig.d.ts +4 -3
- package/dist/execution/multisig.js +10 -12
- package/dist/execution/prepareRestart.d.ts +3 -0
- package/dist/execution/prepareRestart.js +61 -0
- package/dist/execution/stepComparison.d.ts +3 -3
- package/dist/execution/stepComparison.js +3 -3
- package/dist/execution/switchChain.d.ts +13 -8
- package/dist/execution/switchChain.js +17 -12
- package/dist/execution/utils.d.ts +9 -3
- package/dist/execution/utils.js +18 -61
- package/dist/execution/waitForReceivingTransaction.d.ts +3 -0
- package/dist/execution/waitForReceivingTransaction.js +51 -0
- package/dist/helpers.d.ts +7 -17
- package/dist/helpers.js +5 -34
- package/dist/request.js +2 -2
- package/dist/services/ApiService.d.ts +2 -2
- package/dist/services/ApiService.js +2 -0
- package/dist/services/ChainsService.d.ts +1 -1
- package/dist/services/ConfigService.d.ts +1 -1
- package/dist/services/ConfigService.js +2 -3
- package/dist/typeguards.d.ts +1 -1
- package/dist/types/abi.d.ts +5 -0
- package/dist/types/abi.js +42 -0
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.js +2 -2
- package/dist/types/internal.types.d.ts +24 -30
- package/dist/utils/errors.d.ts +15 -14
- package/dist/utils/errors.js +36 -35
- package/dist/utils/getMaxPriorityFeePerGas.d.ts +2 -0
- package/dist/utils/getMaxPriorityFeePerGas.js +18 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.js +5 -0
- package/dist/utils/median.d.ts +1 -0
- package/dist/utils/median.js +11 -0
- package/dist/utils/parseError.d.ts +4 -4
- package/dist/utils/parseError.js +42 -39
- package/dist/utils/utils.d.ts +1 -16
- package/dist/utils/utils.js +7 -69
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +28 -27
- package/dist/allowance/checkAllowance.d.ts +0 -4
- package/dist/allowance/checkAllowance.js +0 -96
- package/dist/allowance/tokenApproval.d.ts +0 -22
- package/dist/allowance/tokenApproval.js +0 -59
- package/dist/allowance/utils.d.ts +0 -15
- package/dist/allowance/utils.js +0 -77
- package/dist/balance/checkBalance.d.ts +0 -3
- package/dist/balance/checkBalance.js +0 -38
- package/dist/balance/utils.d.ts +0 -6
- package/dist/balance/utils.js +0 -143
- package/dist/cjs/allowance/checkAllowance.d.ts +0 -4
- package/dist/cjs/allowance/checkAllowance.js +0 -103
- package/dist/cjs/allowance/tokenApproval.d.ts +0 -22
- package/dist/cjs/allowance/tokenApproval.js +0 -69
- package/dist/cjs/allowance/utils.d.ts +0 -15
- package/dist/cjs/allowance/utils.js +0 -87
- package/dist/cjs/balance/checkBalance.d.ts +0 -3
- package/dist/cjs/balance/checkBalance.js +0 -45
- package/dist/cjs/balance/utils.d.ts +0 -6
- package/dist/cjs/balance/utils.js +0 -150
- package/dist/cjs/types/ERC20.d.ts +0 -22
- package/dist/cjs/types/ERC20.js +0 -53
- package/dist/cjs/utils/getProvider.d.ts +0 -3
- package/dist/cjs/utils/getProvider.js +0 -11
- package/dist/cjs/utils/multicall.d.ts +0 -10
- package/dist/cjs/utils/multicall.js +0 -68
- package/dist/cjs/utils/multicallAbi.json +0 -313
- package/dist/cjs/utils/preRestart.d.ts +0 -3
- package/dist/cjs/utils/preRestart.js +0 -56
- package/dist/types/ERC20.d.ts +0 -22
- package/dist/types/ERC20.js +0 -50
- package/dist/utils/getProvider.d.ts +0 -3
- package/dist/utils/getProvider.js +0 -7
- package/dist/utils/multicall.d.ts +0 -10
- package/dist/utils/multicall.js +0 -61
- package/dist/utils/multicallAbi.json +0 -313
- package/dist/utils/preRestart.d.ts +0 -3
- package/dist/utils/preRestart.js +0 -52
|
@@ -4,32 +4,34 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.StepExecutionManager = void 0;
|
|
7
|
-
const
|
|
8
|
-
const allowance_1 = require("../allowance");
|
|
9
|
-
const balance_1 = require("../balance");
|
|
7
|
+
const viem_1 = require("viem");
|
|
10
8
|
const ApiService_1 = __importDefault(require("../services/ApiService"));
|
|
11
9
|
const ChainsService_1 = __importDefault(require("../services/ChainsService"));
|
|
10
|
+
const ConfigService_1 = __importDefault(require("../services/ConfigService"));
|
|
11
|
+
const utils_1 = require("../utils");
|
|
12
12
|
const errors_1 = require("../utils/errors");
|
|
13
|
-
const getProvider_1 = require("../utils/getProvider");
|
|
14
13
|
const parseError_1 = require("../utils/parseError");
|
|
15
|
-
const
|
|
14
|
+
const utils_2 = require("../utils/utils");
|
|
15
|
+
const checkAllowance_1 = require("./checkAllowance");
|
|
16
|
+
const checkBalance_1 = require("./checkBalance");
|
|
17
|
+
const multisig_1 = require("./multisig");
|
|
16
18
|
const stepComparison_1 = require("./stepComparison");
|
|
17
19
|
const switchChain_1 = require("./switchChain");
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const multisig_1 = require("./multisig");
|
|
20
|
+
const utils_3 = require("./utils");
|
|
21
|
+
const waitForReceivingTransaction_1 = require("./waitForReceivingTransaction");
|
|
21
22
|
class StepExecutionManager {
|
|
22
23
|
constructor() {
|
|
23
24
|
this.allowUserInteraction = true;
|
|
24
25
|
this.allowInteraction = (value) => {
|
|
25
26
|
this.allowUserInteraction = value;
|
|
26
27
|
};
|
|
27
|
-
this.execute = async ({
|
|
28
|
+
this.execute = async ({ walletClient, step, statusManager, settings, }) => {
|
|
29
|
+
const client = walletClient.extend(viem_1.publicActions);
|
|
28
30
|
const config = ConfigService_1.default.getInstance().getConfig();
|
|
29
|
-
const
|
|
31
|
+
const isMultisigWalletClient = !!config.multisig?.isMultisigWalletClient;
|
|
30
32
|
const multisigBatchTransactions = [];
|
|
31
|
-
const shouldBatchTransactions = config.
|
|
32
|
-
!!config.
|
|
33
|
+
const shouldBatchTransactions = config.multisig?.shouldBatchTransactions &&
|
|
34
|
+
!!config.multisig.sendBatchTransaction;
|
|
33
35
|
step.execution = statusManager.initExecutionObject(step);
|
|
34
36
|
const chainsService = ChainsService_1.default.getInstance();
|
|
35
37
|
const fromChain = await chainsService.getChainById(step.action.fromChainId);
|
|
@@ -40,21 +42,17 @@ class StepExecutionManager {
|
|
|
40
42
|
const existingProcess = step.execution.process.find((p) => p.type === currentProcessType);
|
|
41
43
|
// Check token approval only if fromToken is not the native token => no approval needed in that case
|
|
42
44
|
const checkForAllowance = !existingProcess?.txHash &&
|
|
43
|
-
!(0,
|
|
44
|
-
(shouldBatchTransactions || !
|
|
45
|
+
!(0, utils_2.isZeroAddress)(step.action.fromToken.address) &&
|
|
46
|
+
(shouldBatchTransactions || !isMultisigWalletClient);
|
|
45
47
|
if (checkForAllowance) {
|
|
46
|
-
const
|
|
47
|
-
if (
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
data,
|
|
55
|
-
};
|
|
56
|
-
multisigBatchTransactions.push(cleanedPopulatedTransaction);
|
|
57
|
-
}
|
|
48
|
+
const data = await (0, checkAllowance_1.checkAllowance)(client, step, statusManager, settings, fromChain, this.allowUserInteraction, shouldBatchTransactions);
|
|
49
|
+
if (data) {
|
|
50
|
+
// allowance doesn't need value
|
|
51
|
+
const baseTransaction = {
|
|
52
|
+
to: step.action.fromToken.address,
|
|
53
|
+
data,
|
|
54
|
+
};
|
|
55
|
+
multisigBatchTransactions.push(baseTransaction);
|
|
58
56
|
}
|
|
59
57
|
}
|
|
60
58
|
// STEP 2: Get transaction
|
|
@@ -62,136 +60,143 @@ class StepExecutionManager {
|
|
|
62
60
|
if (process.status !== 'DONE') {
|
|
63
61
|
const multisigProcess = step.execution.process.find((p) => !!p.multisigTxHash);
|
|
64
62
|
try {
|
|
65
|
-
if (
|
|
66
|
-
if (!multisigProcess) {
|
|
67
|
-
throw new errors_1.ValidationError('Multisig process is undefined.');
|
|
68
|
-
}
|
|
69
|
-
if (!config.multisigConfig?.getMultisigTransactionDetails) {
|
|
70
|
-
throw new errors_1.ValidationError('"getMultisigTransactionDetails()" is missing in Multisig config.');
|
|
71
|
-
}
|
|
63
|
+
if (isMultisigWalletClient && multisigProcess) {
|
|
72
64
|
const multisigTxHash = multisigProcess.multisigTxHash;
|
|
73
65
|
if (!multisigTxHash) {
|
|
74
|
-
// need to check what happens in failed tx
|
|
75
66
|
throw new errors_1.ValidationError('Multisig internal transaction hash is undefined.');
|
|
76
67
|
}
|
|
77
|
-
await (0, multisig_1.updateMultisigRouteProcess)(multisigTxHash, step, statusManager, process, fromChain);
|
|
68
|
+
await (0, multisig_1.updateMultisigRouteProcess)(multisigTxHash, step, statusManager, process.type, fromChain);
|
|
78
69
|
}
|
|
79
|
-
let
|
|
70
|
+
let txHash;
|
|
80
71
|
if (process.txHash) {
|
|
81
72
|
// Make sure that the chain is still correct
|
|
82
|
-
const
|
|
83
|
-
if (!
|
|
73
|
+
const updatedWalletClient = await (0, switchChain_1.switchChain)(walletClient, statusManager, step, settings.switchChainHook, this.allowUserInteraction);
|
|
74
|
+
if (!updatedWalletClient) {
|
|
84
75
|
// Chain switch was not successful, stop execution here
|
|
85
76
|
return step.execution;
|
|
86
77
|
}
|
|
87
|
-
|
|
78
|
+
walletClient = updatedWalletClient;
|
|
88
79
|
// Load exiting transaction
|
|
89
|
-
|
|
80
|
+
txHash = process.txHash;
|
|
90
81
|
}
|
|
91
82
|
else {
|
|
92
83
|
process = statusManager.updateProcess(step, process.type, 'STARTED');
|
|
93
84
|
// Check balance
|
|
94
|
-
await (0,
|
|
85
|
+
await (0, checkBalance_1.checkBalance)(client.account.address, step);
|
|
95
86
|
// Create new transaction
|
|
96
87
|
if (!step.transactionRequest) {
|
|
97
|
-
const
|
|
98
|
-
const
|
|
99
|
-
const comparedStep = await (0, stepComparison_1.stepComparison)(statusManager, personalizedStep, updatedStep, settings, this.allowUserInteraction);
|
|
88
|
+
const updatedStep = await ApiService_1.default.getStepTransaction(step);
|
|
89
|
+
const comparedStep = await (0, stepComparison_1.stepComparison)(statusManager, step, updatedStep, settings, this.allowUserInteraction);
|
|
100
90
|
step = {
|
|
101
91
|
...comparedStep,
|
|
102
92
|
execution: step.execution,
|
|
103
93
|
};
|
|
104
94
|
}
|
|
105
|
-
|
|
95
|
+
let transactionRequest = {
|
|
96
|
+
to: step.transactionRequest?.to,
|
|
97
|
+
from: step.transactionRequest?.from,
|
|
98
|
+
data: step.transactionRequest?.data,
|
|
99
|
+
value: step.transactionRequest?.value
|
|
100
|
+
? BigInt(step.transactionRequest.value)
|
|
101
|
+
: undefined,
|
|
102
|
+
maxPriorityFeePerGas: walletClient.account?.type === 'local'
|
|
103
|
+
? await (0, utils_1.getMaxPriorityFeePerGas)(client)
|
|
104
|
+
: undefined,
|
|
105
|
+
// gas: step.transactionRequest?.gasLimit
|
|
106
|
+
// ? BigInt(step.transactionRequest.gasLimit as string)
|
|
107
|
+
// : undefined,
|
|
108
|
+
// gasPrice: step.transactionRequest?.gasPrice
|
|
109
|
+
// ? BigInt(step.transactionRequest.gasPrice as string)
|
|
110
|
+
// : undefined,
|
|
111
|
+
// maxFeePerGas: step.transactionRequest?.maxFeePerGas
|
|
112
|
+
// ? BigInt(step.transactionRequest.maxFeePerGas as string)
|
|
113
|
+
// : undefined,
|
|
114
|
+
// maxPriorityFeePerGas: step.transactionRequest?.maxPriorityFeePerGas
|
|
115
|
+
// ? BigInt(step.transactionRequest.maxPriorityFeePerGas as string)
|
|
116
|
+
// : undefined,
|
|
117
|
+
};
|
|
106
118
|
if (!transactionRequest) {
|
|
107
|
-
throw new errors_1.TransactionError(errors_1.
|
|
119
|
+
throw new errors_1.TransactionError(errors_1.LiFiErrorCode.TransactionUnprepared, 'Unable to prepare transaction.');
|
|
108
120
|
}
|
|
109
121
|
// STEP 3: Send the transaction
|
|
110
122
|
// Make sure that the chain is still correct
|
|
111
|
-
const
|
|
112
|
-
if (!
|
|
123
|
+
const updatedWalletClient = await (0, switchChain_1.switchChain)(walletClient, statusManager, step, settings.switchChainHook, this.allowUserInteraction);
|
|
124
|
+
if (!updatedWalletClient) {
|
|
113
125
|
// Chain switch was not successful, stop execution here
|
|
114
126
|
return step.execution;
|
|
115
127
|
}
|
|
116
|
-
|
|
128
|
+
walletClient = updatedWalletClient;
|
|
117
129
|
process = statusManager.updateProcess(step, process.type, 'ACTION_REQUIRED');
|
|
118
130
|
if (!this.allowUserInteraction) {
|
|
119
131
|
return step.execution;
|
|
120
132
|
}
|
|
121
133
|
if (settings.updateTransactionRequestHook) {
|
|
122
|
-
const
|
|
123
|
-
transactionRequest
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
transactionRequest.maxFeePerGas = customConfig.maxFeePerGas;
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
try {
|
|
131
|
-
const estimatedGasLimit = await signer.estimateGas(transactionRequest);
|
|
132
|
-
if (estimatedGasLimit) {
|
|
133
|
-
transactionRequest.gasLimit = ethers_1.BigNumber.from(`${(BigInt(estimatedGasLimit.toString()) * 125n) / 100n}`);
|
|
134
|
-
}
|
|
135
|
-
// Fetch latest gasPrice from provider and use it
|
|
136
|
-
const gasPrice = await signer.getGasPrice();
|
|
137
|
-
if (gasPrice) {
|
|
138
|
-
transactionRequest.gasPrice = gasPrice;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
catch (error) { }
|
|
134
|
+
const customizedTransactionRequest = await settings.updateTransactionRequestHook(transactionRequest);
|
|
135
|
+
transactionRequest = {
|
|
136
|
+
...transactionRequest,
|
|
137
|
+
...customizedTransactionRequest,
|
|
138
|
+
};
|
|
142
139
|
}
|
|
143
|
-
// Submit the transaction
|
|
144
140
|
if (shouldBatchTransactions &&
|
|
145
|
-
config.
|
|
146
|
-
|
|
147
|
-
const isValidTransaction = to && data;
|
|
148
|
-
if (isValidTransaction) {
|
|
141
|
+
config.multisig?.sendBatchTransaction) {
|
|
142
|
+
if (transactionRequest.to && transactionRequest.data) {
|
|
149
143
|
const populatedTransaction = {
|
|
150
|
-
value: value
|
|
151
|
-
to,
|
|
152
|
-
data: data
|
|
144
|
+
value: transactionRequest.value,
|
|
145
|
+
to: transactionRequest.to,
|
|
146
|
+
data: transactionRequest.data,
|
|
153
147
|
};
|
|
154
148
|
multisigBatchTransactions.push(populatedTransaction);
|
|
155
|
-
|
|
149
|
+
txHash = await config.multisig?.sendBatchTransaction(multisigBatchTransactions);
|
|
156
150
|
}
|
|
157
151
|
else {
|
|
158
|
-
throw new errors_1.TransactionError(errors_1.
|
|
152
|
+
throw new errors_1.TransactionError(errors_1.LiFiErrorCode.TransactionUnprepared, 'Unable to prepare transaction.');
|
|
159
153
|
}
|
|
160
154
|
}
|
|
161
155
|
else {
|
|
162
|
-
|
|
156
|
+
txHash = await walletClient.sendTransaction({
|
|
157
|
+
to: transactionRequest.to,
|
|
158
|
+
account: walletClient.account,
|
|
159
|
+
data: transactionRequest.data,
|
|
160
|
+
maxPriorityFeePerGas: transactionRequest.maxPriorityFeePerGas,
|
|
161
|
+
chain: null,
|
|
162
|
+
});
|
|
163
163
|
}
|
|
164
164
|
// STEP 4: Wait for the transaction
|
|
165
|
-
if (
|
|
165
|
+
if (isMultisigWalletClient) {
|
|
166
166
|
process = statusManager.updateProcess(step, process.type, 'ACTION_REQUIRED', {
|
|
167
|
-
multisigTxHash:
|
|
167
|
+
multisigTxHash: txHash,
|
|
168
168
|
});
|
|
169
169
|
}
|
|
170
170
|
else {
|
|
171
171
|
process = statusManager.updateProcess(step, process.type, 'PENDING', {
|
|
172
|
-
txHash:
|
|
173
|
-
txLink: fromChain.metamask.blockExplorerUrls[0]
|
|
174
|
-
'tx/' +
|
|
175
|
-
transaction.hash,
|
|
172
|
+
txHash: txHash,
|
|
173
|
+
txLink: `${fromChain.metamask.blockExplorerUrls[0]}tx/${txHash}`,
|
|
176
174
|
});
|
|
177
175
|
}
|
|
178
176
|
}
|
|
179
|
-
|
|
180
|
-
|
|
177
|
+
let replacementReason;
|
|
178
|
+
const transactionReceipt = await client.waitForTransactionReceipt({
|
|
179
|
+
hash: txHash,
|
|
180
|
+
onReplaced(response) {
|
|
181
|
+
replacementReason = response.reason;
|
|
182
|
+
statusManager.updateProcess(step, process.type, 'PENDING', {
|
|
183
|
+
txHash: response.transaction.hash,
|
|
184
|
+
txLink: `${fromChain.metamask.blockExplorerUrls[0]}tx/${response.transaction.hash}`,
|
|
185
|
+
});
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
if (replacementReason === 'cancelled') {
|
|
189
|
+
throw new errors_1.TransactionError(errors_1.LiFiErrorCode.TransactionCanceled, 'User canceled transaction.');
|
|
190
|
+
}
|
|
191
|
+
// if it's multisig wallet client and the process is in ACTION_REQUIRED
|
|
181
192
|
// then signatures are still needed
|
|
182
|
-
if (
|
|
183
|
-
|
|
184
|
-
transaction.hash) {
|
|
185
|
-
// Return the execution object without updating the process
|
|
186
|
-
// The execution would progress once all multisigs signer approve
|
|
187
|
-
await (0, multisig_1.updateMultisigRouteProcess)(transaction.hash, step, statusManager, process, fromChain);
|
|
193
|
+
if (isMultisigWalletClient && process.status === 'ACTION_REQUIRED') {
|
|
194
|
+
await (0, multisig_1.updateMultisigRouteProcess)(transactionReceipt.transactionHash, step, statusManager, process.type, fromChain);
|
|
188
195
|
}
|
|
189
|
-
if (!
|
|
196
|
+
if (!isMultisigWalletClient) {
|
|
190
197
|
process = statusManager.updateProcess(step, process.type, 'PENDING', {
|
|
191
|
-
txHash:
|
|
192
|
-
txLink: fromChain.metamask.blockExplorerUrls[0]
|
|
193
|
-
'tx/' +
|
|
194
|
-
transaction.hash,
|
|
198
|
+
txHash: transactionReceipt.transactionHash,
|
|
199
|
+
txLink: `${fromChain.metamask.blockExplorerUrls[0]}tx/${transactionReceipt.transactionHash}`,
|
|
195
200
|
});
|
|
196
201
|
}
|
|
197
202
|
if (isBridgeExecution) {
|
|
@@ -199,26 +204,16 @@ class StepExecutionManager {
|
|
|
199
204
|
}
|
|
200
205
|
}
|
|
201
206
|
catch (e) {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
process = statusManager.updateProcess(step, process.type, 'FAILED', {
|
|
213
|
-
error: {
|
|
214
|
-
message: error.message,
|
|
215
|
-
htmlMessage: error.htmlMessage,
|
|
216
|
-
code: error.code,
|
|
217
|
-
},
|
|
218
|
-
});
|
|
219
|
-
statusManager.updateExecution(step, 'FAILED');
|
|
220
|
-
throw error;
|
|
221
|
-
}
|
|
207
|
+
const error = await (0, parseError_1.parseError)(e, step, process);
|
|
208
|
+
process = statusManager.updateProcess(step, process.type, 'FAILED', {
|
|
209
|
+
error: {
|
|
210
|
+
message: error.message,
|
|
211
|
+
htmlMessage: error.htmlMessage,
|
|
212
|
+
code: error.code,
|
|
213
|
+
},
|
|
214
|
+
});
|
|
215
|
+
statusManager.updateExecution(step, 'FAILED');
|
|
216
|
+
throw error;
|
|
222
217
|
}
|
|
223
218
|
}
|
|
224
219
|
// STEP 5: Wait for the receiving chain
|
|
@@ -231,16 +226,14 @@ class StepExecutionManager {
|
|
|
231
226
|
if (!processTxHash) {
|
|
232
227
|
throw new Error('Transaction hash is undefined.');
|
|
233
228
|
}
|
|
234
|
-
statusResponse = (await (0,
|
|
229
|
+
statusResponse = (await (0, waitForReceivingTransaction_1.waitForReceivingTransaction)(processTxHash, statusManager, process.type, step));
|
|
235
230
|
const statusReceiving = statusResponse.receiving;
|
|
236
231
|
process = statusManager.updateProcess(step, process.type, 'DONE', {
|
|
237
232
|
substatus: statusResponse.substatus,
|
|
238
233
|
substatusMessage: statusResponse.substatusMessage ||
|
|
239
|
-
(0,
|
|
234
|
+
(0, utils_3.getSubstatusMessage)(statusResponse.status, statusResponse.substatus),
|
|
240
235
|
txHash: statusReceiving?.txHash,
|
|
241
|
-
txLink: toChain.metamask.blockExplorerUrls[0]
|
|
242
|
-
'tx/' +
|
|
243
|
-
statusReceiving?.txHash,
|
|
236
|
+
txLink: `${toChain.metamask.blockExplorerUrls[0]}tx/${statusReceiving?.txHash}`,
|
|
244
237
|
});
|
|
245
238
|
statusManager.updateExecution(step, 'DONE', {
|
|
246
239
|
fromAmount: statusResponse.sending.amount,
|
|
@@ -257,7 +250,7 @@ class StepExecutionManager {
|
|
|
257
250
|
const htmlMessage = await (0, parseError_1.getTransactionFailedMessage)(step, process.txLink);
|
|
258
251
|
process = statusManager.updateProcess(step, process.type, 'FAILED', {
|
|
259
252
|
error: {
|
|
260
|
-
code: errors_1.
|
|
253
|
+
code: errors_1.LiFiErrorCode.TransactionFailed,
|
|
261
254
|
message: 'Failed while waiting for receiving chain.',
|
|
262
255
|
htmlMessage,
|
|
263
256
|
},
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { InteractionSettings, InternalExecutionSettings, LifiStep } from '../types';
|
|
3
|
-
import { StatusManager } from './StatusManager';
|
|
1
|
+
import type { WalletClient } from 'viem';
|
|
2
|
+
import type { InteractionSettings, InternalExecutionSettings, LifiStep } from '../types';
|
|
3
|
+
import type { StatusManager } from './StatusManager';
|
|
4
4
|
import { StepExecutionManager } from './StepExecutionManager';
|
|
5
5
|
export declare class StepExecutor {
|
|
6
6
|
stepExecutionManager: StepExecutionManager;
|
|
@@ -11,5 +11,5 @@ export declare class StepExecutor {
|
|
|
11
11
|
constructor(statusManager: StatusManager, settings: InternalExecutionSettings);
|
|
12
12
|
setInteraction: (settings?: InteractionSettings) => void;
|
|
13
13
|
checkChain: () => never;
|
|
14
|
-
executeStep: (
|
|
14
|
+
executeStep: (walletClient: WalletClient, step: LifiStep) => Promise<LifiStep>;
|
|
15
15
|
}
|
|
@@ -23,13 +23,13 @@ class StepExecutor {
|
|
|
23
23
|
this.statusManager.allowUpdates(interactionSettings.allowUpdates);
|
|
24
24
|
this.executionStopped = interactionSettings.stopExecution;
|
|
25
25
|
};
|
|
26
|
-
// TODO: add checkChain method and update
|
|
26
|
+
// TODO: add checkChain method and update wallet client inside executors
|
|
27
27
|
// This can come in handy when we execute multiple routes simultaneously and
|
|
28
28
|
// should be sure that we are on the right chain when waiting for transactions.
|
|
29
29
|
this.checkChain = () => {
|
|
30
30
|
throw new Error('checkChain is not implemented.');
|
|
31
31
|
};
|
|
32
|
-
this.executeStep = async (
|
|
32
|
+
this.executeStep = async (walletClient, step) => {
|
|
33
33
|
// Make sure that the chain is still correct
|
|
34
34
|
// Find if it's bridging and the step is waiting for a transaction on the receiving chain
|
|
35
35
|
const recievingChainProcess = step.execution?.process.find((process) => process.type === 'RECEIVING_CHAIN');
|
|
@@ -38,15 +38,15 @@ class StepExecutor {
|
|
|
38
38
|
// Return the step
|
|
39
39
|
if (recievingChainProcess?.substatus !== 'WAIT_DESTINATION_TRANSACTION' ||
|
|
40
40
|
!recievingChainProcess) {
|
|
41
|
-
const
|
|
42
|
-
if (!
|
|
41
|
+
const updatedWalletClient = await (0, switchChain_1.switchChain)(walletClient, this.statusManager, step, this.settings.switchChainHook, this.allowUserInteraction);
|
|
42
|
+
if (!updatedWalletClient) {
|
|
43
43
|
// Chain switch was not successful, stop execution here
|
|
44
44
|
return step;
|
|
45
45
|
}
|
|
46
|
-
|
|
46
|
+
walletClient = updatedWalletClient;
|
|
47
47
|
}
|
|
48
48
|
const parameters = {
|
|
49
|
-
|
|
49
|
+
walletClient,
|
|
50
50
|
step,
|
|
51
51
|
settings: this.settings,
|
|
52
52
|
statusManager: this.statusManager,
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Hash, WalletClient } from 'viem';
|
|
2
|
+
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>;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.checkAllowance = void 0;
|
|
4
|
+
const viem_1 = require("viem");
|
|
5
|
+
const allowance_1 = require("../allowance");
|
|
6
|
+
const utils_1 = require("../utils");
|
|
7
|
+
const parseError_1 = require("../utils/parseError");
|
|
8
|
+
const checkAllowance = async (walletClient, step, statusManager, settings, chain, allowUserInteraction = false, shouldBatchTransactions = false) => {
|
|
9
|
+
// Ask the user to set an allowance
|
|
10
|
+
let allowanceProcess = statusManager.findOrCreateProcess(step, 'TOKEN_ALLOWANCE');
|
|
11
|
+
// Check allowance
|
|
12
|
+
try {
|
|
13
|
+
if (allowanceProcess.txHash && allowanceProcess.status !== 'DONE') {
|
|
14
|
+
await waitForApprovalTransaction(walletClient, allowanceProcess.txHash, allowanceProcess.type, step, chain, statusManager);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
const approved = await (0, allowance_1.getAllowance)(chain.id, step.action.fromToken.address, walletClient.account.address, step.estimate.approvalAddress);
|
|
18
|
+
const fromAmount = BigInt(step.action.fromAmount);
|
|
19
|
+
if (fromAmount > approved) {
|
|
20
|
+
if (!allowUserInteraction) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const approvalAmount = settings.infiniteApproval
|
|
24
|
+
? viem_1.maxUint256
|
|
25
|
+
: fromAmount;
|
|
26
|
+
if (shouldBatchTransactions) {
|
|
27
|
+
const approveTxHash = await (0, allowance_1.setAllowance)(walletClient, step.action.fromToken.address, step.estimate.approvalAddress, approvalAmount, true);
|
|
28
|
+
allowanceProcess = statusManager.updateProcess(step, allowanceProcess.type, 'DONE');
|
|
29
|
+
return approveTxHash;
|
|
30
|
+
}
|
|
31
|
+
const approveTxHash = await (0, allowance_1.setAllowance)(walletClient, step.action.fromToken.address, step.estimate.approvalAddress, approvalAmount);
|
|
32
|
+
await waitForApprovalTransaction(walletClient, approveTxHash, allowanceProcess.type, step, chain, statusManager);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
allowanceProcess = statusManager.updateProcess(step, allowanceProcess.type, 'DONE');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
const error = await (0, parseError_1.parseError)(e, step, allowanceProcess);
|
|
41
|
+
allowanceProcess = statusManager.updateProcess(step, allowanceProcess.type, 'FAILED', {
|
|
42
|
+
error: {
|
|
43
|
+
message: error.message,
|
|
44
|
+
htmlMessage: error.htmlMessage,
|
|
45
|
+
code: error.code,
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
statusManager.updateExecution(step, 'FAILED');
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
exports.checkAllowance = checkAllowance;
|
|
53
|
+
const waitForApprovalTransaction = async (walletClient, txHash, processType, step, chain, statusManager) => {
|
|
54
|
+
const client = walletClient.extend(viem_1.publicActions);
|
|
55
|
+
statusManager.updateProcess(step, processType, 'PENDING', {
|
|
56
|
+
txHash,
|
|
57
|
+
txLink: `${chain.metamask.blockExplorerUrls[0]}tx/${txHash}`,
|
|
58
|
+
});
|
|
59
|
+
let replacementReason;
|
|
60
|
+
const transactionReceipt = await client.waitForTransactionReceipt({
|
|
61
|
+
hash: txHash,
|
|
62
|
+
onReplaced(response) {
|
|
63
|
+
replacementReason = response.reason;
|
|
64
|
+
statusManager.updateProcess(step, processType, 'PENDING', {
|
|
65
|
+
txHash: response.transaction.hash,
|
|
66
|
+
txLink: `${chain.metamask.blockExplorerUrls[0]}tx/${response.transaction.hash}`,
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
if (replacementReason === 'cancelled') {
|
|
71
|
+
throw new utils_1.TransactionError(utils_1.LiFiErrorCode.TransactionCanceled, 'User canceled transaction.');
|
|
72
|
+
}
|
|
73
|
+
statusManager.updateProcess(step, processType, 'DONE', {
|
|
74
|
+
txHash: transactionReceipt.transactionHash,
|
|
75
|
+
txLink: `${chain.metamask.blockExplorerUrls[0]}tx/${transactionReceipt.transactionHash}`,
|
|
76
|
+
});
|
|
77
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.checkBalance = void 0;
|
|
4
|
+
const viem_1 = require("viem");
|
|
5
|
+
const balance_1 = require("../balance");
|
|
6
|
+
const errors_1 = require("../utils/errors");
|
|
7
|
+
const checkBalance = async (walletAddress, step, depth = 0) => {
|
|
8
|
+
const token = await (0, balance_1.getTokenBalance)(walletAddress, step.action.fromToken);
|
|
9
|
+
if (token) {
|
|
10
|
+
const currentBalance = token.amount ?? 0n;
|
|
11
|
+
const neededBalance = BigInt(step.action.fromAmount);
|
|
12
|
+
if (currentBalance < neededBalance) {
|
|
13
|
+
if (depth <= 3) {
|
|
14
|
+
await new Promise((resolve) => {
|
|
15
|
+
setTimeout(resolve, 200);
|
|
16
|
+
});
|
|
17
|
+
await (0, exports.checkBalance)(walletAddress, step, depth + 1);
|
|
18
|
+
}
|
|
19
|
+
else if ((neededBalance * BigInt((1 - step.action.slippage) * 1000000000)) /
|
|
20
|
+
1000000000n <=
|
|
21
|
+
currentBalance) {
|
|
22
|
+
// adjust amount in slippage limits
|
|
23
|
+
step.action.fromAmount = currentBalance.toString();
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
const neeeded = (0, viem_1.formatUnits)(neededBalance, token.decimals);
|
|
27
|
+
const current = (0, viem_1.formatUnits)(currentBalance, token.decimals);
|
|
28
|
+
let errorMessage = `Your ${token.symbol} balance is too low, ` +
|
|
29
|
+
`you try to transfer ${neeeded} ${token.symbol}, ` +
|
|
30
|
+
`but your wallet only holds ${current} ${token.symbol}. ` +
|
|
31
|
+
`No funds have been sent.`;
|
|
32
|
+
if (currentBalance !== 0n) {
|
|
33
|
+
errorMessage +=
|
|
34
|
+
`If the problem consists, please delete this transfer and ` +
|
|
35
|
+
`start a new one with a maximum of ${current} ${token.symbol}.`;
|
|
36
|
+
}
|
|
37
|
+
throw new errors_1.BalanceError('The balance is too low.', errorMessage);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
exports.checkBalance = checkBalance;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
import { ExtendedChain, LifiStep,
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import type { ExtendedChain, LifiStep, ProcessType } from '@lifi/types';
|
|
2
|
+
import type { Hash } from 'viem';
|
|
3
|
+
import type { StatusManager } from '.';
|
|
4
|
+
export declare const updateMultisigRouteProcess: (internalTxHash: Hash, step: LifiStep, statusManager: StatusManager, processType: ProcessType, fromChain: ExtendedChain) => Promise<void>;
|
|
@@ -6,29 +6,27 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.updateMultisigRouteProcess = void 0;
|
|
7
7
|
const ConfigService_1 = __importDefault(require("../services/ConfigService"));
|
|
8
8
|
const errors_1 = require("../utils/errors");
|
|
9
|
-
const updateMultisigRouteProcess = async (internalTxHash, step, statusManager,
|
|
9
|
+
const updateMultisigRouteProcess = async (internalTxHash, step, statusManager, processType, fromChain) => {
|
|
10
10
|
const config = ConfigService_1.default.getInstance().getConfig();
|
|
11
|
-
if (!config.
|
|
12
|
-
throw new Error('
|
|
11
|
+
if (!config.multisig?.getMultisigTransactionDetails) {
|
|
12
|
+
throw new Error('getMultisigTransactionDetails is missing in multisig config.');
|
|
13
13
|
}
|
|
14
14
|
const updateIntermediateMultisigStatus = () => {
|
|
15
|
-
|
|
15
|
+
statusManager.updateProcess(step, processType, 'PENDING');
|
|
16
16
|
};
|
|
17
|
-
const multisigStatusResponse = await config.
|
|
17
|
+
const multisigStatusResponse = await config.multisig?.getMultisigTransactionDetails(internalTxHash, fromChain.id, updateIntermediateMultisigStatus);
|
|
18
18
|
if (multisigStatusResponse.status === 'DONE') {
|
|
19
|
-
|
|
19
|
+
statusManager.updateProcess(step, processType, 'PENDING', {
|
|
20
20
|
txHash: multisigStatusResponse.txHash,
|
|
21
21
|
multisigTxHash: undefined,
|
|
22
|
-
txLink: fromChain.metamask.blockExplorerUrls[0]
|
|
23
|
-
'tx/' +
|
|
24
|
-
multisigStatusResponse.txHash,
|
|
22
|
+
txLink: `${fromChain.metamask.blockExplorerUrls[0]}tx/${multisigStatusResponse.txHash}`,
|
|
25
23
|
});
|
|
26
24
|
}
|
|
27
25
|
if (multisigStatusResponse.status === 'FAILED') {
|
|
28
|
-
throw new errors_1.TransactionError(errors_1.
|
|
26
|
+
throw new errors_1.TransactionError(errors_1.LiFiErrorCode.TransactionFailed, 'Multisig transaction failed.');
|
|
29
27
|
}
|
|
30
28
|
if (multisigStatusResponse.status === 'CANCELLED') {
|
|
31
|
-
throw new errors_1.TransactionError(errors_1.
|
|
29
|
+
throw new errors_1.TransactionError(errors_1.LiFiErrorCode.SignatureRejected, 'Transaction was rejected by user.');
|
|
32
30
|
}
|
|
33
31
|
};
|
|
34
32
|
exports.updateMultisigRouteProcess = updateMultisigRouteProcess;
|