@lifi/sdk 2.2.3 → 3.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. package/dist/LiFi.d.ts +69 -62
  2. package/dist/LiFi.js +71 -60
  3. package/dist/allowance/getAllowance.d.ts +6 -0
  4. package/dist/allowance/getAllowance.js +78 -0
  5. package/dist/allowance/index.d.ts +3 -2
  6. package/dist/allowance/index.js +3 -2
  7. package/dist/allowance/setAllowance.d.ts +5 -0
  8. package/dist/allowance/setAllowance.js +50 -0
  9. package/dist/allowance/types.d.ts +27 -0
  10. package/dist/allowance/types.js +1 -0
  11. package/dist/balance/getBalance.d.ts +2 -0
  12. package/dist/balance/getBalance.js +93 -0
  13. package/dist/balance/getTokenBalance.d.ts +2 -2
  14. package/dist/balance/getTokenBalance.js +4 -4
  15. package/dist/balance/index.d.ts +1 -12
  16. package/dist/balance/index.js +1 -9
  17. package/dist/cjs/LiFi.d.ts +69 -62
  18. package/dist/cjs/LiFi.js +69 -58
  19. package/dist/cjs/allowance/getAllowance.d.ts +6 -0
  20. package/dist/cjs/allowance/getAllowance.js +85 -0
  21. package/dist/cjs/allowance/index.d.ts +3 -2
  22. package/dist/cjs/allowance/index.js +3 -2
  23. package/dist/cjs/allowance/setAllowance.d.ts +5 -0
  24. package/dist/cjs/allowance/setAllowance.js +56 -0
  25. package/dist/cjs/allowance/types.d.ts +27 -0
  26. package/dist/cjs/allowance/types.js +2 -0
  27. package/dist/cjs/balance/getBalance.d.ts +2 -0
  28. package/dist/cjs/balance/getBalance.js +97 -0
  29. package/dist/cjs/balance/getTokenBalance.d.ts +2 -2
  30. package/dist/cjs/balance/getTokenBalance.js +6 -9
  31. package/dist/cjs/balance/index.d.ts +1 -12
  32. package/dist/cjs/balance/index.js +1 -9
  33. package/dist/cjs/connectors.d.ts +7 -5
  34. package/dist/cjs/connectors.js +45 -37
  35. package/dist/cjs/constants.d.ts +6 -0
  36. package/dist/cjs/constants.js +9 -0
  37. package/dist/cjs/execution/RouteExecutionManager.d.ts +24 -30
  38. package/dist/cjs/execution/RouteExecutionManager.js +27 -45
  39. package/dist/cjs/execution/StatusManager.d.ts +22 -23
  40. package/dist/cjs/execution/StatusManager.js +21 -22
  41. package/dist/cjs/execution/StepExecutionManager.d.ts +3 -3
  42. package/dist/cjs/execution/StepExecutionManager.js +118 -125
  43. package/dist/cjs/execution/StepExecutor.d.ts +4 -4
  44. package/dist/cjs/execution/StepExecutor.js +6 -6
  45. package/dist/cjs/execution/checkAllowance.d.ts +4 -0
  46. package/dist/cjs/execution/checkAllowance.js +77 -0
  47. package/dist/cjs/execution/checkBalance.d.ts +2 -0
  48. package/dist/cjs/execution/checkBalance.js +42 -0
  49. package/dist/cjs/execution/multisig.d.ts +4 -3
  50. package/dist/cjs/execution/multisig.js +9 -11
  51. package/dist/cjs/execution/prepareRestart.d.ts +3 -0
  52. package/dist/cjs/execution/prepareRestart.js +65 -0
  53. package/dist/cjs/execution/stepComparison.d.ts +3 -3
  54. package/dist/cjs/execution/stepComparison.js +2 -2
  55. package/dist/cjs/execution/switchChain.d.ts +13 -8
  56. package/dist/cjs/execution/switchChain.js +16 -11
  57. package/dist/cjs/execution/utils.d.ts +9 -3
  58. package/dist/cjs/execution/utils.js +19 -66
  59. package/dist/cjs/execution/waitForReceivingTransaction.d.ts +3 -0
  60. package/dist/cjs/execution/waitForReceivingTransaction.js +58 -0
  61. package/dist/cjs/helpers.d.ts +7 -17
  62. package/dist/cjs/helpers.js +6 -38
  63. package/dist/cjs/request.js +1 -1
  64. package/dist/cjs/services/ApiService.d.ts +2 -2
  65. package/dist/cjs/services/ApiService.js +4 -2
  66. package/dist/cjs/services/ChainsService.d.ts +1 -1
  67. package/dist/cjs/services/ConfigService.d.ts +1 -1
  68. package/dist/cjs/services/ConfigService.js +1 -2
  69. package/dist/cjs/typeguards.d.ts +1 -1
  70. package/dist/cjs/types/abi.d.ts +5 -0
  71. package/dist/cjs/types/abi.js +45 -0
  72. package/dist/cjs/types/index.d.ts +2 -2
  73. package/dist/cjs/types/index.js +3 -3
  74. package/dist/cjs/types/internal.types.d.ts +24 -31
  75. package/dist/cjs/utils/errors.d.ts +15 -14
  76. package/dist/cjs/utils/errors.js +38 -37
  77. package/dist/cjs/utils/getMaxPriorityFeePerGas.d.ts +2 -0
  78. package/dist/cjs/utils/getMaxPriorityFeePerGas.js +22 -0
  79. package/dist/cjs/utils/index.d.ts +5 -0
  80. package/dist/cjs/utils/index.js +21 -0
  81. package/dist/cjs/utils/median.d.ts +1 -0
  82. package/dist/cjs/utils/median.js +15 -0
  83. package/dist/cjs/utils/parseError.d.ts +4 -4
  84. package/dist/cjs/utils/parseError.js +41 -38
  85. package/dist/cjs/utils/utils.d.ts +1 -16
  86. package/dist/cjs/utils/utils.js +9 -78
  87. package/dist/cjs/version.d.ts +1 -1
  88. package/dist/cjs/version.js +1 -1
  89. package/dist/connectors.d.ts +7 -5
  90. package/dist/connectors.js +19 -35
  91. package/dist/constants.d.ts +6 -0
  92. package/dist/constants.js +6 -0
  93. package/dist/execution/RouteExecutionManager.d.ts +24 -30
  94. package/dist/execution/RouteExecutionManager.js +27 -45
  95. package/dist/execution/StatusManager.d.ts +22 -23
  96. package/dist/execution/StatusManager.js +22 -23
  97. package/dist/execution/StepExecutionManager.d.ts +3 -3
  98. package/dist/execution/StepExecutionManager.js +116 -123
  99. package/dist/execution/StepExecutor.d.ts +4 -4
  100. package/dist/execution/StepExecutor.js +6 -6
  101. package/dist/execution/checkAllowance.d.ts +4 -0
  102. package/dist/execution/checkAllowance.js +73 -0
  103. package/dist/execution/checkBalance.d.ts +2 -0
  104. package/dist/execution/checkBalance.js +38 -0
  105. package/dist/execution/multisig.d.ts +4 -3
  106. package/dist/execution/multisig.js +10 -12
  107. package/dist/execution/prepareRestart.d.ts +3 -0
  108. package/dist/execution/prepareRestart.js +61 -0
  109. package/dist/execution/stepComparison.d.ts +3 -3
  110. package/dist/execution/stepComparison.js +3 -3
  111. package/dist/execution/switchChain.d.ts +13 -8
  112. package/dist/execution/switchChain.js +17 -12
  113. package/dist/execution/utils.d.ts +9 -3
  114. package/dist/execution/utils.js +18 -61
  115. package/dist/execution/waitForReceivingTransaction.d.ts +3 -0
  116. package/dist/execution/waitForReceivingTransaction.js +51 -0
  117. package/dist/helpers.d.ts +7 -17
  118. package/dist/helpers.js +5 -34
  119. package/dist/request.js +2 -2
  120. package/dist/services/ApiService.d.ts +2 -2
  121. package/dist/services/ApiService.js +4 -2
  122. package/dist/services/ChainsService.d.ts +1 -1
  123. package/dist/services/ConfigService.d.ts +1 -1
  124. package/dist/services/ConfigService.js +2 -3
  125. package/dist/typeguards.d.ts +1 -1
  126. package/dist/types/abi.d.ts +5 -0
  127. package/dist/types/abi.js +42 -0
  128. package/dist/types/index.d.ts +2 -2
  129. package/dist/types/index.js +2 -2
  130. package/dist/types/internal.types.d.ts +24 -31
  131. package/dist/utils/errors.d.ts +15 -14
  132. package/dist/utils/errors.js +36 -35
  133. package/dist/utils/getMaxPriorityFeePerGas.d.ts +2 -0
  134. package/dist/utils/getMaxPriorityFeePerGas.js +18 -0
  135. package/dist/utils/index.d.ts +5 -0
  136. package/dist/utils/index.js +5 -0
  137. package/dist/utils/median.d.ts +1 -0
  138. package/dist/utils/median.js +11 -0
  139. package/dist/utils/parseError.d.ts +4 -4
  140. package/dist/utils/parseError.js +42 -39
  141. package/dist/utils/utils.d.ts +1 -16
  142. package/dist/utils/utils.js +7 -69
  143. package/dist/version.d.ts +1 -1
  144. package/dist/version.js +1 -1
  145. package/package.json +43 -40
  146. package/dist/allowance/checkAllowance.d.ts +0 -4
  147. package/dist/allowance/checkAllowance.js +0 -96
  148. package/dist/allowance/tokenApproval.d.ts +0 -22
  149. package/dist/allowance/tokenApproval.js +0 -59
  150. package/dist/allowance/utils.d.ts +0 -15
  151. package/dist/allowance/utils.js +0 -77
  152. package/dist/balance/checkBalance.d.ts +0 -3
  153. package/dist/balance/checkBalance.js +0 -38
  154. package/dist/balance/utils.d.ts +0 -6
  155. package/dist/balance/utils.js +0 -143
  156. package/dist/cjs/allowance/checkAllowance.d.ts +0 -4
  157. package/dist/cjs/allowance/checkAllowance.js +0 -103
  158. package/dist/cjs/allowance/tokenApproval.d.ts +0 -22
  159. package/dist/cjs/allowance/tokenApproval.js +0 -69
  160. package/dist/cjs/allowance/utils.d.ts +0 -15
  161. package/dist/cjs/allowance/utils.js +0 -87
  162. package/dist/cjs/balance/checkBalance.d.ts +0 -3
  163. package/dist/cjs/balance/checkBalance.js +0 -45
  164. package/dist/cjs/balance/utils.d.ts +0 -6
  165. package/dist/cjs/balance/utils.js +0 -150
  166. package/dist/cjs/types/ERC20.d.ts +0 -22
  167. package/dist/cjs/types/ERC20.js +0 -53
  168. package/dist/cjs/utils/getProvider.d.ts +0 -3
  169. package/dist/cjs/utils/getProvider.js +0 -11
  170. package/dist/cjs/utils/multicall.d.ts +0 -10
  171. package/dist/cjs/utils/multicall.js +0 -68
  172. package/dist/cjs/utils/multicallAbi.json +0 -313
  173. package/dist/cjs/utils/preRestart.d.ts +0 -3
  174. package/dist/cjs/utils/preRestart.js +0 -56
  175. package/dist/types/ERC20.d.ts +0 -22
  176. package/dist/types/ERC20.js +0 -50
  177. package/dist/utils/getProvider.d.ts +0 -3
  178. package/dist/utils/getProvider.js +0 -7
  179. package/dist/utils/multicall.d.ts +0 -10
  180. package/dist/utils/multicall.js +0 -61
  181. package/dist/utils/multicallAbi.json +0 -313
  182. package/dist/utils/preRestart.d.ts +0 -3
  183. 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 ethers_1 = require("ethers");
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 utils_1 = require("../utils/utils");
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 utils_2 = require("./utils");
19
- const ConfigService_1 = __importDefault(require("../services/ConfigService"));
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 ({ signer, step, statusManager, settings, }) => {
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 isMultisigSigner = !!config.multisigConfig?.isMultisigSigner;
31
+ const isMultisigWalletClient = !!config.multisig?.isMultisigWalletClient;
30
32
  const multisigBatchTransactions = [];
31
- const shouldBatchTransactions = config.multisigConfig?.shouldBatchTransactions &&
32
- !!config.multisigConfig.sendBatchTransaction;
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, utils_1.isZeroAddress)(step.action.fromToken.address) &&
44
- (shouldBatchTransactions || !isMultisigSigner);
45
+ !(0, utils_2.isZeroAddress)(step.action.fromToken.address) &&
46
+ (shouldBatchTransactions || !isMultisigWalletClient);
45
47
  if (checkForAllowance) {
46
- const populatedTransaction = await (0, allowance_1.checkAllowance)(signer, step, statusManager, settings, fromChain, this.allowUserInteraction, shouldBatchTransactions);
47
- if (populatedTransaction) {
48
- const { to, data } = populatedTransaction;
49
- if (to && data) {
50
- // allowance doesn't need value
51
- const cleanedPopulatedTransaction = {
52
- value: ethers_1.BigNumber.from(0).toString(),
53
- to,
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 (isMultisigSigner && multisigProcess) {
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 transaction;
70
+ let txHash;
80
71
  if (process.txHash) {
81
72
  // Make sure that the chain is still correct
82
- const updatedSigner = await (0, switchChain_1.switchChain)(signer, statusManager, step, settings.switchChainHook, this.allowUserInteraction);
83
- if (!updatedSigner) {
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
- signer = updatedSigner;
78
+ walletClient = updatedWalletClient;
88
79
  // Load exiting transaction
89
- transaction = await (0, getProvider_1.getProvider)(signer).getTransaction(process.txHash);
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, balance_1.checkBalance)(signer, step);
85
+ await (0, checkBalance_1.checkBalance)(client.account.address, step);
95
86
  // Create new transaction
96
87
  if (!step.transactionRequest) {
97
- const personalizedStep = await (0, utils_1.personalizeStep)(signer, step);
98
- const updatedStep = await ApiService_1.default.getStepTransaction(personalizedStep);
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
- const { transactionRequest } = step;
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.LifiErrorCode.TransactionUnprepared, 'Unable to prepare transaction.');
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 updatedSigner = await (0, switchChain_1.switchChain)(signer, statusManager, step, settings.switchChainHook, this.allowUserInteraction);
112
- if (!updatedSigner) {
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
- signer = updatedSigner;
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 customConfig = await settings.updateTransactionRequestHook(transactionRequest);
123
- transactionRequest.gasLimit = customConfig.gasLimit;
124
- transactionRequest.gasPrice = customConfig.gasPrice;
125
- transactionRequest.maxPriorityFeePerGas =
126
- customConfig.maxPriorityFeePerGas;
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.multisigConfig?.sendBatchTransaction) {
146
- const { to, data, value } = await signer.populateTransaction(transactionRequest);
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?.toString() ?? ethers_1.BigNumber.from(0).toString(),
151
- to,
152
- data: data.toString(),
144
+ value: transactionRequest.value,
145
+ to: transactionRequest.to,
146
+ data: transactionRequest.data,
153
147
  };
154
148
  multisigBatchTransactions.push(populatedTransaction);
155
- transaction = await config.multisigConfig?.sendBatchTransaction(multisigBatchTransactions);
149
+ txHash = await config.multisig?.sendBatchTransaction(multisigBatchTransactions);
156
150
  }
157
151
  else {
158
- throw new errors_1.TransactionError(errors_1.LifiErrorCode.TransactionUnprepared, 'Unable to prepare transaction.');
152
+ throw new errors_1.TransactionError(errors_1.LiFiErrorCode.TransactionUnprepared, 'Unable to prepare transaction.');
159
153
  }
160
154
  }
161
155
  else {
162
- transaction = await signer.sendTransaction(transactionRequest);
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 (isMultisigSigner) {
165
+ if (isMultisigWalletClient) {
166
166
  process = statusManager.updateProcess(step, process.type, 'ACTION_REQUIRED', {
167
- multisigTxHash: transaction.hash,
167
+ multisigTxHash: txHash,
168
168
  });
169
169
  }
170
170
  else {
171
171
  process = statusManager.updateProcess(step, process.type, 'PENDING', {
172
- txHash: transaction.hash,
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
- await transaction.wait?.();
180
- // if it's multisig signer and the process is in ACTION_REQUIRED
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 (isMultisigSigner &&
183
- process.status === 'ACTION_REQUIRED' &&
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 (!isMultisigSigner) {
196
+ if (!isMultisigWalletClient) {
190
197
  process = statusManager.updateProcess(step, process.type, 'PENDING', {
191
- txHash: transaction.hash,
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
- if (e.code === 'TRANSACTION_REPLACED' && e.replacement) {
203
- process = statusManager.updateProcess(step, process.type, 'DONE', {
204
- txHash: e.replacement.hash,
205
- txLink: fromChain.metamask.blockExplorerUrls[0] +
206
- 'tx/' +
207
- e.replacement.hash,
208
- });
209
- }
210
- else {
211
- const error = await (0, parseError_1.parseError)(e, step, process);
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, utils_2.waitForReceivingTransaction)(processTxHash, statusManager, process.type, step));
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, utils_2.getSubstatusMessage)(statusResponse.status, statusResponse.substatus),
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.LifiErrorCode.TransactionFailed,
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 { Signer } from 'ethers';
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: (signer: Signer, step: LifiStep) => Promise<LifiStep>;
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 signer inside executors
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 (signer, step) => {
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 updatedSigner = await (0, switchChain_1.switchChain)(signer, this.statusManager, step, this.settings.switchChainHook, this.allowUserInteraction);
42
- if (!updatedSigner) {
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
- signer = updatedSigner;
46
+ walletClient = updatedWalletClient;
47
47
  }
48
48
  const parameters = {
49
- signer,
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,2 @@
1
+ import type { LifiStep } from '..';
2
+ export declare const checkBalance: (walletAddress: string, step: LifiStep, depth?: number) => Promise<void>;
@@ -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, Process } from '@lifi/types';
2
- import { StatusManager } from '.';
3
- export declare const updateMultisigRouteProcess: (internalTxHash: string, step: LifiStep, statusManager: StatusManager, process: Process, fromChain: ExtendedChain) => Promise<void>;
1
+ import type { ExtendedChain, LifiStep, ProcessType } from '@lifi/types';
2
+ import type { Hash } from 'viem';
3
+ import type { StatusManager } from './StatusManager';
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, process, fromChain) => {
9
+ const updateMultisigRouteProcess = async (internalTxHash, step, statusManager, processType, fromChain) => {
10
10
  const config = ConfigService_1.default.getInstance().getConfig();
11
- if (!config.multisigConfig?.getMultisigTransactionDetails) {
12
- throw new Error('"getMultisigTransactionDetails()" is missing in Multisig config.');
11
+ if (!config.multisig?.getMultisigTransactionDetails) {
12
+ throw new Error('getMultisigTransactionDetails is missing in multisig config.');
13
13
  }
14
14
  const updateIntermediateMultisigStatus = () => {
15
- process = statusManager.updateProcess(step, process.type, 'PENDING');
15
+ statusManager.updateProcess(step, processType, 'PENDING');
16
16
  };
17
- const multisigStatusResponse = await config.multisigConfig?.getMultisigTransactionDetails(internalTxHash, fromChain.id, updateIntermediateMultisigStatus);
17
+ const multisigStatusResponse = await config.multisig?.getMultisigTransactionDetails(internalTxHash, fromChain.id, updateIntermediateMultisigStatus);
18
18
  if (multisigStatusResponse.status === 'DONE') {
19
- process = statusManager.updateProcess(step, process.type, 'PENDING', {
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.LifiErrorCode.TransactionFailed, 'Multisig transaction failed.');
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.LifiErrorCode.TransactionRejected, 'Transaction was rejected by users.');
29
+ throw new errors_1.TransactionError(errors_1.LiFiErrorCode.SignatureRejected, 'Transaction was rejected by user.');
32
30
  }
33
31
  };
34
32
  exports.updateMultisigRouteProcess = updateMultisigRouteProcess;
@@ -0,0 +1,3 @@
1
+ import { type WalletClient } from 'viem';
2
+ import type { Route } from '../types';
3
+ export declare const prepareRestart: (route: Route, walletClient: WalletClient) => Promise<void>;