@lifi/sdk 3.6.1 → 3.6.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 (137) hide show
  1. package/package.json +3 -3
  2. package/src/_cjs/core/BaseStepExecutor.js +13 -38
  3. package/src/_cjs/core/BaseStepExecutor.js.map +1 -1
  4. package/src/_cjs/core/EVM/EVMStepExecutor.js +329 -359
  5. package/src/_cjs/core/EVM/EVMStepExecutor.js.map +1 -1
  6. package/src/_cjs/core/EVM/checkAllowance.js +18 -9
  7. package/src/_cjs/core/EVM/checkAllowance.js.map +1 -1
  8. package/src/_cjs/core/EVM/checkPermitSupport.js +2 -2
  9. package/src/_cjs/core/EVM/checkPermitSupport.js.map +1 -1
  10. package/src/_cjs/core/EVM/permits/allowanceTransfer.js +8 -8
  11. package/src/_cjs/core/EVM/permits/allowanceTransfer.js.map +1 -1
  12. package/src/_cjs/core/EVM/permits/getNativePermit.js +12 -10
  13. package/src/_cjs/core/EVM/permits/getNativePermit.js.map +1 -1
  14. package/src/_cjs/core/EVM/permits/signPermit2Message.js +27 -26
  15. package/src/_cjs/core/EVM/permits/signPermit2Message.js.map +1 -1
  16. package/src/_cjs/core/EVM/permits/signatureTransfer.js +8 -8
  17. package/src/_cjs/core/EVM/permits/signatureTransfer.js.map +1 -1
  18. package/src/_cjs/core/EVM/typeguards.js +5 -2
  19. package/src/_cjs/core/EVM/typeguards.js.map +1 -1
  20. package/src/_cjs/core/EVM/types.js.map +1 -1
  21. package/src/_cjs/core/Solana/KeypairWalletAdapter.js +8 -33
  22. package/src/_cjs/core/Solana/KeypairWalletAdapter.js.map +1 -1
  23. package/src/_cjs/core/Solana/SolanaStepExecutor.js +97 -112
  24. package/src/_cjs/core/Solana/SolanaStepExecutor.js.map +1 -1
  25. package/src/_cjs/core/StatusManager.js +100 -135
  26. package/src/_cjs/core/StatusManager.js.map +1 -1
  27. package/src/_cjs/core/UTXO/UTXOStepExecutor.js +136 -151
  28. package/src/_cjs/core/UTXO/UTXOStepExecutor.js.map +1 -1
  29. package/src/_cjs/core/prepareRestart.js +7 -1
  30. package/src/_cjs/core/prepareRestart.js.map +1 -1
  31. package/src/_cjs/errors/SDKError.js +5 -30
  32. package/src/_cjs/errors/SDKError.js.map +1 -1
  33. package/src/_cjs/errors/baseError.js +2 -12
  34. package/src/_cjs/errors/baseError.js.map +1 -1
  35. package/src/_cjs/errors/httpError.js +6 -36
  36. package/src/_cjs/errors/httpError.js.map +1 -1
  37. package/src/_cjs/index.js +3 -2
  38. package/src/_cjs/index.js.map +1 -1
  39. package/src/_cjs/services/api.js +2 -4
  40. package/src/_cjs/services/api.js.map +1 -1
  41. package/src/_cjs/utils/withDedupe.js +1 -6
  42. package/src/_cjs/utils/withDedupe.js.map +1 -1
  43. package/src/_cjs/version.js +1 -1
  44. package/src/_esm/core/BaseStepExecutor.js +13 -38
  45. package/src/_esm/core/BaseStepExecutor.js.map +1 -1
  46. package/src/_esm/core/EVM/EVMStepExecutor.js +367 -397
  47. package/src/_esm/core/EVM/EVMStepExecutor.js.map +1 -1
  48. package/src/_esm/core/EVM/checkAllowance.js +18 -9
  49. package/src/_esm/core/EVM/checkAllowance.js.map +1 -1
  50. package/src/_esm/core/EVM/checkPermitSupport.js +2 -2
  51. package/src/_esm/core/EVM/checkPermitSupport.js.map +1 -1
  52. package/src/_esm/core/EVM/permits/allowanceTransfer.js +8 -8
  53. package/src/_esm/core/EVM/permits/allowanceTransfer.js.map +1 -1
  54. package/src/_esm/core/EVM/permits/getNativePermit.js +12 -10
  55. package/src/_esm/core/EVM/permits/getNativePermit.js.map +1 -1
  56. package/src/_esm/core/EVM/permits/signPermit2Message.js +29 -28
  57. package/src/_esm/core/EVM/permits/signPermit2Message.js.map +1 -1
  58. package/src/_esm/core/EVM/permits/signatureTransfer.js +8 -8
  59. package/src/_esm/core/EVM/permits/signatureTransfer.js.map +1 -1
  60. package/src/_esm/core/EVM/typeguards.js +4 -2
  61. package/src/_esm/core/EVM/typeguards.js.map +1 -1
  62. package/src/_esm/core/EVM/types.js.map +1 -1
  63. package/src/_esm/core/Solana/KeypairWalletAdapter.js +12 -37
  64. package/src/_esm/core/Solana/KeypairWalletAdapter.js.map +1 -1
  65. package/src/_esm/core/Solana/SolanaStepExecutor.js +105 -120
  66. package/src/_esm/core/Solana/SolanaStepExecutor.js.map +1 -1
  67. package/src/_esm/core/StatusManager.js +129 -164
  68. package/src/_esm/core/StatusManager.js.map +1 -1
  69. package/src/_esm/core/UTXO/UTXOStepExecutor.js +144 -159
  70. package/src/_esm/core/UTXO/UTXOStepExecutor.js.map +1 -1
  71. package/src/_esm/core/prepareRestart.js +10 -1
  72. package/src/_esm/core/prepareRestart.js.map +1 -1
  73. package/src/_esm/errors/SDKError.js +5 -30
  74. package/src/_esm/errors/SDKError.js.map +1 -1
  75. package/src/_esm/errors/baseError.js +2 -12
  76. package/src/_esm/errors/baseError.js.map +1 -1
  77. package/src/_esm/errors/httpError.js +6 -36
  78. package/src/_esm/errors/httpError.js.map +1 -1
  79. package/src/_esm/index.js +1 -1
  80. package/src/_esm/index.js.map +1 -1
  81. package/src/_esm/services/api.js +2 -4
  82. package/src/_esm/services/api.js.map +1 -1
  83. package/src/_esm/utils/withDedupe.js +1 -6
  84. package/src/_esm/utils/withDedupe.js.map +1 -1
  85. package/src/_esm/version.js +1 -1
  86. package/src/_types/core/EVM/EVMStepExecutor.d.ts.map +1 -1
  87. package/src/_types/core/EVM/checkAllowance.d.ts +2 -3
  88. package/src/_types/core/EVM/checkAllowance.d.ts.map +1 -1
  89. package/src/_types/core/EVM/checkPermitSupport.d.ts.map +1 -1
  90. package/src/_types/core/EVM/permits/allowanceTransfer.d.ts +4 -4
  91. package/src/_types/core/EVM/permits/allowanceTransfer.d.ts.map +1 -1
  92. package/src/_types/core/EVM/permits/getNativePermit.d.ts +1 -2
  93. package/src/_types/core/EVM/permits/getNativePermit.d.ts.map +1 -1
  94. package/src/_types/core/EVM/permits/signPermit2Message.d.ts +2 -11
  95. package/src/_types/core/EVM/permits/signPermit2Message.d.ts.map +1 -1
  96. package/src/_types/core/EVM/permits/signatureTransfer.d.ts +2 -2
  97. package/src/_types/core/EVM/permits/signatureTransfer.d.ts.map +1 -1
  98. package/src/_types/core/EVM/permits/types.d.ts +3 -9
  99. package/src/_types/core/EVM/permits/types.d.ts.map +1 -1
  100. package/src/_types/core/EVM/typeguards.d.ts +6 -2
  101. package/src/_types/core/EVM/typeguards.d.ts.map +1 -1
  102. package/src/_types/core/EVM/types.d.ts +2 -5
  103. package/src/_types/core/EVM/types.d.ts.map +1 -1
  104. package/src/_types/core/prepareRestart.d.ts.map +1 -1
  105. package/src/_types/index.d.ts +1 -1
  106. package/src/_types/index.d.ts.map +1 -1
  107. package/src/_types/services/api.d.ts +2 -2
  108. package/src/_types/services/api.d.ts.map +1 -1
  109. package/src/_types/version.d.ts +1 -1
  110. package/src/core/EVM/EVMStepExecutor.ts +39 -44
  111. package/src/core/EVM/checkAllowance.ts +28 -19
  112. package/src/core/EVM/checkPermitSupport.ts +14 -2
  113. package/src/core/EVM/permits/allowanceTransfer.ts +14 -10
  114. package/src/core/EVM/permits/getNativePermit.ts +13 -11
  115. package/src/core/EVM/permits/signPermit2Message.ts +43 -63
  116. package/src/core/EVM/permits/signatureTransfer.ts +10 -10
  117. package/src/core/EVM/permits/types.ts +3 -16
  118. package/src/core/EVM/typeguards.ts +14 -4
  119. package/src/core/EVM/types.ts +2 -6
  120. package/src/core/prepareRestart.ts +14 -2
  121. package/src/index.ts +1 -1
  122. package/src/services/api.ts +3 -6
  123. package/src/version.ts +1 -1
  124. package/src/_cjs/core/EVM/permits/signNativePermitMessage.js +0 -20
  125. package/src/_cjs/core/EVM/permits/signNativePermitMessage.js.map +0 -1
  126. package/src/_cjs/core/EVM/permits/utils.js +0 -31
  127. package/src/_cjs/core/EVM/permits/utils.js.map +0 -1
  128. package/src/_esm/core/EVM/permits/signNativePermitMessage.js +0 -16
  129. package/src/_esm/core/EVM/permits/signNativePermitMessage.js.map +0 -1
  130. package/src/_esm/core/EVM/permits/utils.js +0 -26
  131. package/src/_esm/core/EVM/permits/utils.js.map +0 -1
  132. package/src/_types/core/EVM/permits/signNativePermitMessage.d.ts +0 -4
  133. package/src/_types/core/EVM/permits/signNativePermitMessage.d.ts.map +0 -1
  134. package/src/_types/core/EVM/permits/utils.d.ts +0 -6
  135. package/src/_types/core/EVM/permits/utils.d.ts.map +0 -1
  136. package/src/core/EVM/permits/signNativePermitMessage.ts +0 -27
  137. package/src/core/EVM/permits/utils.ts +0 -40
@@ -18,399 +18,369 @@ const parseEVMErrors_js_1 = require("./parseEVMErrors.js");
18
18
  const encodeNativePermitData_js_1 = require("./permits/encodeNativePermitData.js");
19
19
  const encodePermit2Data_js_1 = require("./permits/encodePermit2Data.js");
20
20
  const signPermit2Message_js_1 = require("./permits/signPermit2Message.js");
21
- const utils_js_1 = require("./permits/utils.js");
22
21
  const switchChain_js_1 = require("./switchChain.js");
23
22
  const typeguards_js_1 = require("./typeguards.js");
24
- const utils_js_2 = require("./utils.js");
23
+ const utils_js_1 = require("./utils.js");
25
24
  const waitForBatchTransactionReceipt_js_1 = require("./waitForBatchTransactionReceipt.js");
26
25
  const waitForRelayedTransactionReceipt_js_1 = require("./waitForRelayedTransactionReceipt.js");
27
26
  const waitForTransactionReceipt_js_1 = require("./waitForTransactionReceipt.js");
28
27
  class EVMStepExecutor extends BaseStepExecutor_js_1.BaseStepExecutor {
28
+ client;
29
29
  constructor(options) {
30
30
  super(options);
31
- Object.defineProperty(this, "client", {
32
- enumerable: true,
33
- configurable: true,
34
- writable: true,
35
- value: void 0
36
- });
37
- Object.defineProperty(this, "checkClient", {
38
- enumerable: true,
39
- configurable: true,
40
- writable: true,
41
- value: async (step, process) => {
42
- const updatedClient = await (0, switchChain_js_1.switchChain)(this.client, this.statusManager, step, this.allowUserInteraction, this.executionOptions?.switchChainHook);
43
- if (updatedClient) {
44
- this.client = updatedClient;
45
- }
46
- let accountAddress = this.client.account?.address;
47
- if (!accountAddress) {
48
- const accountAddresses = (await (0, utils_1.getAction)(this.client, actions_1.getAddresses, 'getAddresses')(undefined));
49
- accountAddress = accountAddresses?.[0];
50
- }
51
- if (accountAddress?.toLowerCase() !== step.action.fromAddress?.toLowerCase()) {
52
- let processToUpdate = process;
53
- if (!processToUpdate) {
54
- processToUpdate = this.statusManager.findOrCreateProcess({
55
- step,
56
- type: 'TRANSACTION',
31
+ this.client = options.client;
32
+ }
33
+ checkClient = async (step, process) => {
34
+ const updatedClient = await (0, switchChain_js_1.switchChain)(this.client, this.statusManager, step, this.allowUserInteraction, this.executionOptions?.switchChainHook);
35
+ if (updatedClient) {
36
+ this.client = updatedClient;
37
+ }
38
+ let accountAddress = this.client.account?.address;
39
+ if (!accountAddress) {
40
+ const accountAddresses = (await (0, utils_1.getAction)(this.client, actions_1.getAddresses, 'getAddresses')(undefined));
41
+ accountAddress = accountAddresses?.[0];
42
+ }
43
+ if (accountAddress?.toLowerCase() !== step.action.fromAddress?.toLowerCase()) {
44
+ let processToUpdate = process;
45
+ if (!processToUpdate) {
46
+ processToUpdate = this.statusManager.findOrCreateProcess({
47
+ step,
48
+ type: 'TRANSACTION',
49
+ });
50
+ }
51
+ const errorMessage = 'The wallet address that requested the quote does not match the wallet address attempting to sign the transaction.';
52
+ this.statusManager.updateProcess(step, processToUpdate.type, 'FAILED', {
53
+ error: {
54
+ code: constants_js_1.LiFiErrorCode.WalletChangedDuringExecution,
55
+ message: errorMessage,
56
+ },
57
+ });
58
+ this.statusManager.updateExecution(step, 'FAILED');
59
+ throw await (0, parseEVMErrors_js_1.parseEVMErrors)(new errors_js_1.TransactionError(constants_js_1.LiFiErrorCode.WalletChangedDuringExecution, errorMessage), step, process);
60
+ }
61
+ return updatedClient;
62
+ };
63
+ waitForTransaction = async ({ step, process, fromChain, toChain, txType, txHash, isBridgeExecution, }) => {
64
+ let transactionReceipt;
65
+ switch (txType) {
66
+ case 'batched':
67
+ transactionReceipt = await (0, waitForBatchTransactionReceipt_js_1.waitForBatchTransactionReceipt)(this.client, txHash);
68
+ break;
69
+ case 'relayed':
70
+ transactionReceipt = await (0, waitForRelayedTransactionReceipt_js_1.waitForRelayedTransactionReceipt)(txHash);
71
+ break;
72
+ default:
73
+ transactionReceipt = await (0, waitForTransactionReceipt_js_1.waitForTransactionReceipt)({
74
+ client: this.client,
75
+ chainId: fromChain.id,
76
+ txHash,
77
+ onReplaced: (response) => {
78
+ this.statusManager.updateProcess(step, process.type, 'PENDING', {
79
+ txHash: response.transaction.hash,
80
+ txLink: `${fromChain.metamask.blockExplorerUrls[0]}tx/${response.transaction.hash}`,
57
81
  });
58
- }
59
- const errorMessage = 'The wallet address that requested the quote does not match the wallet address attempting to sign the transaction.';
60
- this.statusManager.updateProcess(step, processToUpdate.type, 'FAILED', {
61
- error: {
62
- code: constants_js_1.LiFiErrorCode.WalletChangedDuringExecution,
63
- message: errorMessage,
64
- },
65
- });
66
- this.statusManager.updateExecution(step, 'FAILED');
67
- throw await (0, parseEVMErrors_js_1.parseEVMErrors)(new errors_js_1.TransactionError(constants_js_1.LiFiErrorCode.WalletChangedDuringExecution, errorMessage), step, process);
68
- }
69
- return updatedClient;
82
+ },
83
+ });
84
+ }
85
+ if (transactionReceipt?.transactionHash &&
86
+ transactionReceipt.transactionHash !== txHash) {
87
+ process = this.statusManager.updateProcess(step, process.type, 'PENDING', {
88
+ txHash: transactionReceipt.transactionHash,
89
+ txLink: `${fromChain.metamask.blockExplorerUrls[0]}tx/${transactionReceipt.transactionHash}`,
90
+ });
91
+ }
92
+ if (isBridgeExecution) {
93
+ process = this.statusManager.updateProcess(step, process.type, 'DONE');
94
+ }
95
+ await (0, waitForDestinationChainTransaction_js_1.waitForDestinationChainTransaction)(step, process, fromChain, toChain, this.statusManager);
96
+ };
97
+ executeStep = async (step) => {
98
+ step.execution = this.statusManager.initExecutionObject(step);
99
+ const destinationChainProcess = step.execution?.process.find((process) => process.type === 'RECEIVING_CHAIN');
100
+ if (destinationChainProcess?.substatus !== 'WAIT_DESTINATION_TRANSACTION') {
101
+ const updatedClient = await this.checkClient(step);
102
+ if (!updatedClient) {
103
+ return step;
70
104
  }
105
+ }
106
+ const fromChain = await config_js_1.config.getChainById(step.action.fromChainId);
107
+ const toChain = await config_js_1.config.getChainById(step.action.toChainId);
108
+ const calls = [];
109
+ const batchingSupported = await (0, isBatchingSupported_js_1.isBatchingSupported)({
110
+ client: this.client,
111
+ chainId: fromChain.id,
71
112
  });
72
- Object.defineProperty(this, "waitForTransaction", {
73
- enumerable: true,
74
- configurable: true,
75
- writable: true,
76
- value: async ({ step, process, fromChain, toChain, txType, txHash, isBridgeExecution, }) => {
77
- let transactionReceipt;
78
- switch (txType) {
79
- case 'batched':
80
- transactionReceipt = await (0, waitForBatchTransactionReceipt_js_1.waitForBatchTransactionReceipt)(this.client, txHash);
81
- break;
82
- case 'relayed':
83
- transactionReceipt = await (0, waitForRelayedTransactionReceipt_js_1.waitForRelayedTransactionReceipt)(txHash);
84
- break;
85
- default:
86
- transactionReceipt = await (0, waitForTransactionReceipt_js_1.waitForTransactionReceipt)({
87
- client: this.client,
88
- chainId: fromChain.id,
89
- txHash,
90
- onReplaced: (response) => {
91
- this.statusManager.updateProcess(step, process.type, 'PENDING', {
92
- txHash: response.transaction.hash,
93
- txLink: `${fromChain.metamask.blockExplorerUrls[0]}tx/${response.transaction.hash}`,
94
- });
95
- },
96
- });
113
+ const isBridgeExecution = fromChain.id !== toChain.id;
114
+ const currentProcessType = isBridgeExecution ? 'CROSS_CHAIN' : 'SWAP';
115
+ const existingProcess = step.execution.process.find((p) => p.type === currentProcessType);
116
+ const isFromNativeToken = fromChain.nativeToken.address === step.action.fromToken.address;
117
+ const isRelayerTransaction = (0, typeguards_js_1.isRelayerStep)(step);
118
+ const permit2Supported = !!fromChain.permit2 &&
119
+ !!fromChain.permit2Proxy &&
120
+ !batchingSupported &&
121
+ !isFromNativeToken;
122
+ const checkForAllowance = !existingProcess?.txHash &&
123
+ !isFromNativeToken;
124
+ let signedNativePermitTypedData;
125
+ if (checkForAllowance) {
126
+ const allowanceResult = await (0, checkAllowance_js_1.checkAllowance)({
127
+ client: this.client,
128
+ chain: fromChain,
129
+ step,
130
+ statusManager: this.statusManager,
131
+ executionOptions: this.executionOptions,
132
+ allowUserInteraction: this.allowUserInteraction,
133
+ batchingSupported,
134
+ permit2Supported,
135
+ });
136
+ if (allowanceResult.status === 'BATCH_APPROVAL') {
137
+ if (batchingSupported) {
138
+ calls.push(allowanceResult.data);
139
+ }
140
+ }
141
+ if (allowanceResult.status === 'NATIVE_PERMIT') {
142
+ signedNativePermitTypedData = allowanceResult.data;
143
+ }
144
+ if (allowanceResult.status === 'ACTION_REQUIRED' &&
145
+ !this.allowUserInteraction) {
146
+ return step;
147
+ }
148
+ }
149
+ let process = this.statusManager.findProcess(step, currentProcessType);
150
+ if (process?.status === 'DONE') {
151
+ await (0, waitForDestinationChainTransaction_js_1.waitForDestinationChainTransaction)(step, process, fromChain, toChain, this.statusManager);
152
+ return step;
153
+ }
154
+ try {
155
+ if (process?.txHash) {
156
+ const updatedClient = await this.checkClient(step, process);
157
+ if (!updatedClient) {
158
+ return step;
97
159
  }
98
- if (transactionReceipt?.transactionHash &&
99
- transactionReceipt.transactionHash !== txHash) {
100
- process = this.statusManager.updateProcess(step, process.type, 'PENDING', {
101
- txHash: transactionReceipt.transactionHash,
102
- txLink: `${fromChain.metamask.blockExplorerUrls[0]}tx/${transactionReceipt.transactionHash}`,
160
+ const txHash = process.txHash;
161
+ const txType = process.txType;
162
+ await this.waitForTransaction({
163
+ step,
164
+ process,
165
+ fromChain,
166
+ toChain,
167
+ txType,
168
+ txHash,
169
+ isBridgeExecution,
170
+ });
171
+ return step;
172
+ }
173
+ const permitRequired = !batchingSupported && !signedNativePermitTypedData && permit2Supported;
174
+ process = this.statusManager.findOrCreateProcess({
175
+ step,
176
+ type: permitRequired ? 'PERMIT' : currentProcessType,
177
+ status: 'STARTED',
178
+ chainId: fromChain.id,
179
+ });
180
+ await (0, checkBalance_js_1.checkBalance)(this.client.account.address, step);
181
+ if (!step.transactionRequest) {
182
+ const { execution, ...stepBase } = step;
183
+ let updatedStep;
184
+ if (isRelayerTransaction) {
185
+ const updatedRelayedStep = await (0, api_js_1.getRelayerQuote)({
186
+ fromChain: stepBase.action.fromChainId,
187
+ fromToken: stepBase.action.fromToken.address,
188
+ fromAddress: stepBase.action.fromAddress,
189
+ fromAmount: stepBase.action.fromAmount,
190
+ toChain: stepBase.action.toChainId,
191
+ toToken: stepBase.action.toToken.address,
192
+ slippage: stepBase.action.slippage,
193
+ toAddress: stepBase.action.toAddress,
194
+ allowBridges: [stepBase.tool],
103
195
  });
196
+ updatedStep = {
197
+ ...updatedRelayedStep,
198
+ id: stepBase.id,
199
+ };
104
200
  }
105
- if (isBridgeExecution) {
106
- process = this.statusManager.updateProcess(step, process.type, 'DONE');
201
+ else {
202
+ updatedStep = await (0, api_js_1.getStepTransaction)(stepBase);
107
203
  }
108
- await (0, waitForDestinationChainTransaction_js_1.waitForDestinationChainTransaction)(step, process, fromChain, toChain, this.statusManager);
204
+ const comparedStep = await (0, stepComparison_js_1.stepComparison)(this.statusManager, step, updatedStep, this.allowUserInteraction, this.executionOptions);
205
+ Object.assign(step, {
206
+ ...comparedStep,
207
+ execution: step.execution,
208
+ });
109
209
  }
110
- });
111
- Object.defineProperty(this, "executeStep", {
112
- enumerable: true,
113
- configurable: true,
114
- writable: true,
115
- value: async (step) => {
116
- step.execution = this.statusManager.initExecutionObject(step);
117
- const destinationChainProcess = step.execution?.process.find((process) => process.type === 'RECEIVING_CHAIN');
118
- if (destinationChainProcess?.substatus !== 'WAIT_DESTINATION_TRANSACTION') {
119
- const updatedClient = await this.checkClient(step);
120
- if (!updatedClient) {
121
- return step;
122
- }
210
+ if (!step.transactionRequest) {
211
+ throw new errors_js_1.TransactionError(constants_js_1.LiFiErrorCode.TransactionUnprepared, 'Unable to prepare transaction.');
212
+ }
213
+ let transactionRequest = {
214
+ to: step.transactionRequest.to,
215
+ from: step.transactionRequest.from,
216
+ data: step.transactionRequest.data,
217
+ value: step.transactionRequest.value
218
+ ? BigInt(step.transactionRequest.value)
219
+ : undefined,
220
+ gas: step.transactionRequest.gasLimit
221
+ ? BigInt(step.transactionRequest.gasLimit)
222
+ : undefined,
223
+ maxPriorityFeePerGas: this.client.account?.type === 'local'
224
+ ? await (0, utils_js_1.getMaxPriorityFeePerGas)(this.client)
225
+ : step.transactionRequest.maxPriorityFeePerGas
226
+ ? BigInt(step.transactionRequest.maxPriorityFeePerGas)
227
+ : undefined,
228
+ };
229
+ if (this.executionOptions?.updateTransactionRequestHook) {
230
+ const customizedTransactionRequest = await this.executionOptions.updateTransactionRequestHook({
231
+ requestType: 'transaction',
232
+ ...transactionRequest,
233
+ });
234
+ transactionRequest = {
235
+ ...transactionRequest,
236
+ ...customizedTransactionRequest,
237
+ };
238
+ }
239
+ const updatedClient = await this.checkClient(step, process);
240
+ if (!updatedClient) {
241
+ return step;
242
+ }
243
+ process = this.statusManager.updateProcess(step, process.type, 'ACTION_REQUIRED');
244
+ if (!this.allowUserInteraction) {
245
+ return step;
246
+ }
247
+ let txHash;
248
+ let txType = 'standard';
249
+ if (batchingSupported) {
250
+ const transferCall = {
251
+ chainId: fromChain.id,
252
+ data: transactionRequest.data,
253
+ to: transactionRequest.to,
254
+ value: transactionRequest.value,
255
+ };
256
+ calls.push(transferCall);
257
+ txHash = (await (0, utils_1.getAction)(this.client, experimental_1.sendCalls, 'sendCalls')({
258
+ account: this.client.account,
259
+ calls,
260
+ }));
261
+ txType = 'batched';
262
+ }
263
+ else if (isRelayerTransaction) {
264
+ const permitWitnessTransferFromData = step.typedData.find((p) => p.primaryType === 'PermitWitnessTransferFrom');
265
+ if (!permitWitnessTransferFromData) {
266
+ throw new errors_js_1.TransactionError(constants_js_1.LiFiErrorCode.TransactionUnprepared, 'Unable to prepare transaction. Permit data for transfer is not found.');
123
267
  }
124
- const fromChain = await config_js_1.config.getChainById(step.action.fromChainId);
125
- const toChain = await config_js_1.config.getChainById(step.action.toChainId);
126
- const calls = [];
127
- const batchingSupported = await (0, isBatchingSupported_js_1.isBatchingSupported)({
128
- client: this.client,
268
+ const signature = await (0, utils_1.getAction)(this.client, actions_1.signTypedData, 'signTypedData')({
269
+ account: this.client.account,
270
+ primaryType: permitWitnessTransferFromData.primaryType,
271
+ domain: permitWitnessTransferFromData.domain,
272
+ types: permitWitnessTransferFromData.types,
273
+ message: permitWitnessTransferFromData.message,
274
+ });
275
+ this.statusManager.updateProcess(step, process.type, 'DONE');
276
+ process = this.statusManager.findOrCreateProcess({
277
+ step,
278
+ type: currentProcessType,
279
+ status: 'PENDING',
129
280
  chainId: fromChain.id,
130
281
  });
131
- const isBridgeExecution = fromChain.id !== toChain.id;
132
- const currentProcessType = isBridgeExecution ? 'CROSS_CHAIN' : 'SWAP';
133
- const existingProcess = step.execution.process.find((p) => p.type === currentProcessType);
134
- const isFromNativeToken = fromChain.nativeToken.address === step.action.fromToken.address;
135
- const isRelayerTransaction = (0, typeguards_js_1.isRelayerStep)(step);
136
- const permit2Supported = !!fromChain.permit2 &&
137
- !!fromChain.permit2Proxy &&
138
- !batchingSupported &&
139
- !isFromNativeToken;
140
- const checkForAllowance = !existingProcess?.txHash &&
141
- !isFromNativeToken;
142
- let nativePermitSignature;
143
- if (checkForAllowance) {
144
- const allowanceResult = await (0, checkAllowance_js_1.checkAllowance)({
282
+ const signedTypedData = [
283
+ {
284
+ ...permitWitnessTransferFromData,
285
+ signature: signature,
286
+ },
287
+ ];
288
+ if (signedNativePermitTypedData) {
289
+ signedTypedData.unshift(signedNativePermitTypedData);
290
+ }
291
+ const { execution, ...stepBase } = step;
292
+ const relayedTransaction = await (0, api_js_1.relayTransaction)({
293
+ ...stepBase,
294
+ typedData: signedTypedData,
295
+ });
296
+ txHash = relayedTransaction.taskId;
297
+ txType = 'relayed';
298
+ }
299
+ else {
300
+ if (signedNativePermitTypedData) {
301
+ transactionRequest.data = (0, encodeNativePermitData_js_1.encodeNativePermitData)(step.action.fromToken.address, BigInt(step.action.fromAmount), signedNativePermitTypedData.message.deadline, signedNativePermitTypedData.signature, transactionRequest.data);
302
+ }
303
+ else if (permit2Supported) {
304
+ const permit2Signature = await (0, signPermit2Message_js_1.signPermit2Message)({
145
305
  client: this.client,
146
306
  chain: fromChain,
147
- step,
148
- statusManager: this.statusManager,
149
- executionOptions: this.executionOptions,
150
- allowUserInteraction: this.allowUserInteraction,
151
- batchingSupported,
152
- permit2Supported,
307
+ tokenAddress: step.action.fromToken.address,
308
+ amount: BigInt(step.action.fromAmount),
309
+ data: transactionRequest.data,
153
310
  });
154
- if (allowanceResult.status === 'BATCH_APPROVAL') {
155
- if (batchingSupported) {
156
- calls.push(allowanceResult.data);
157
- }
158
- }
159
- if (allowanceResult.status === 'NATIVE_PERMIT') {
160
- nativePermitSignature = allowanceResult.data;
161
- }
162
- if (allowanceResult.status === 'ACTION_REQUIRED' &&
163
- !this.allowUserInteraction) {
164
- return step;
165
- }
166
- }
167
- let process = this.statusManager.findProcess(step, currentProcessType);
168
- if (process?.status === 'DONE') {
169
- await (0, waitForDestinationChainTransaction_js_1.waitForDestinationChainTransaction)(step, process, fromChain, toChain, this.statusManager);
170
- return step;
171
- }
172
- try {
173
- if (process?.txHash) {
174
- const updatedClient = await this.checkClient(step, process);
175
- if (!updatedClient) {
176
- return step;
177
- }
178
- const txHash = process.txHash;
179
- const txType = process.txType;
180
- await this.waitForTransaction({
181
- step,
182
- process,
183
- fromChain,
184
- toChain,
185
- txType,
186
- txHash,
187
- isBridgeExecution,
188
- });
189
- return step;
190
- }
191
- const permitRequired = !batchingSupported && !nativePermitSignature && permit2Supported;
311
+ this.statusManager.updateProcess(step, process.type, 'DONE');
192
312
  process = this.statusManager.findOrCreateProcess({
193
313
  step,
194
- type: permitRequired ? 'PERMIT' : currentProcessType,
195
- status: 'STARTED',
314
+ type: currentProcessType,
315
+ status: 'PENDING',
196
316
  chainId: fromChain.id,
197
317
  });
198
- await (0, checkBalance_js_1.checkBalance)(this.client.account.address, step);
199
- if (!step.transactionRequest) {
200
- const { execution, ...stepBase } = step;
201
- let updatedStep;
202
- if (isRelayerTransaction) {
203
- const updatedRelayedStep = await (0, api_js_1.getRelayerQuote)({
204
- fromChain: stepBase.action.fromChainId,
205
- fromToken: stepBase.action.fromToken.address,
206
- fromAddress: stepBase.action.fromAddress,
207
- fromAmount: stepBase.action.fromAmount,
208
- toChain: stepBase.action.toChainId,
209
- toToken: stepBase.action.toToken.address,
210
- slippage: stepBase.action.slippage,
211
- toAddress: stepBase.action.toAddress,
212
- allowBridges: [stepBase.tool],
213
- });
214
- updatedStep = {
215
- ...updatedRelayedStep.quote,
216
- permits: updatedRelayedStep.permits,
217
- id: stepBase.id,
218
- };
219
- }
220
- else {
221
- updatedStep = await (0, api_js_1.getStepTransaction)(stepBase);
222
- }
223
- const comparedStep = await (0, stepComparison_js_1.stepComparison)(this.statusManager, step, updatedStep, this.allowUserInteraction, this.executionOptions);
224
- Object.assign(step, {
225
- ...comparedStep,
226
- execution: step.execution,
227
- });
228
- }
229
- if (!step.transactionRequest) {
230
- throw new errors_js_1.TransactionError(constants_js_1.LiFiErrorCode.TransactionUnprepared, 'Unable to prepare transaction.');
231
- }
232
- let transactionRequest = {
233
- to: step.transactionRequest.to,
234
- from: step.transactionRequest.from,
235
- data: step.transactionRequest.data,
236
- value: step.transactionRequest.value
237
- ? BigInt(step.transactionRequest.value)
238
- : undefined,
239
- gas: step.transactionRequest.gasLimit
240
- ? BigInt(step.transactionRequest.gasLimit)
241
- : undefined,
242
- maxPriorityFeePerGas: this.client.account?.type === 'local'
243
- ? await (0, utils_js_2.getMaxPriorityFeePerGas)(this.client)
244
- : step.transactionRequest.maxPriorityFeePerGas
245
- ? BigInt(step.transactionRequest.maxPriorityFeePerGas)
246
- : undefined,
247
- };
248
- if (this.executionOptions?.updateTransactionRequestHook) {
249
- const customizedTransactionRequest = await this.executionOptions.updateTransactionRequestHook({
250
- requestType: 'transaction',
251
- ...transactionRequest,
252
- });
253
- transactionRequest = {
254
- ...transactionRequest,
255
- ...customizedTransactionRequest,
256
- };
257
- }
258
- const updatedClient = await this.checkClient(step, process);
259
- if (!updatedClient) {
260
- return step;
261
- }
262
- process = this.statusManager.updateProcess(step, process.type, 'ACTION_REQUIRED');
263
- if (!this.allowUserInteraction) {
264
- return step;
265
- }
266
- let txHash;
267
- let txType = 'standard';
268
- if (batchingSupported) {
269
- const transferCall = {
270
- chainId: fromChain.id,
271
- data: transactionRequest.data,
272
- to: transactionRequest.to,
273
- value: transactionRequest.value,
274
- };
275
- calls.push(transferCall);
276
- txHash = (await (0, utils_1.getAction)(this.client, experimental_1.sendCalls, 'sendCalls')({
318
+ transactionRequest.data = (0, encodePermit2Data_js_1.encodePermit2Data)(step.action.fromToken.address, BigInt(step.action.fromAmount), permit2Signature.message.nonce, permit2Signature.message.deadline, transactionRequest.data, permit2Signature.signature);
319
+ }
320
+ if (signedNativePermitTypedData || permit2Supported) {
321
+ try {
322
+ transactionRequest.to = fromChain.permit2Proxy;
323
+ const estimatedGas = await (0, actions_1.estimateGas)(this.client, {
277
324
  account: this.client.account,
278
- calls,
279
- }));
280
- txType = 'batched';
281
- }
282
- else if (isRelayerTransaction) {
283
- const permitWitnessTransferFromData = step.permits.find((p) => p.permitType === 'PermitWitnessTransferFrom');
284
- if (!permitWitnessTransferFromData) {
285
- throw new errors_js_1.TransactionError(constants_js_1.LiFiErrorCode.TransactionUnprepared, 'Unable to prepare transaction. Permit data for transfer is not found.');
286
- }
287
- const permit2Signature = await (0, signPermit2Message_js_1.signPermit2Message)({
288
- client: this.client,
289
- chain: fromChain,
290
- tokenAddress: step.action.fromToken.address,
291
- amount: BigInt(step.action.fromAmount),
292
- data: transactionRequest.data,
293
- permitData: (0, utils_js_1.prettifyPermit2Data)(permitWitnessTransferFromData.permitData),
294
- witness: true,
295
- });
296
- this.statusManager.updateProcess(step, process.type, 'DONE');
297
- process = this.statusManager.findOrCreateProcess({
298
- step,
299
- type: currentProcessType,
300
- status: 'PENDING',
301
- chainId: fromChain.id,
302
- });
303
- const signedPermits = [
304
- {
305
- permitType: 'PermitWitnessTransferFrom',
306
- permit: permit2Signature.values,
307
- signature: permit2Signature.signature,
308
- },
309
- ];
310
- if (nativePermitSignature) {
311
- signedPermits.unshift({
312
- permitType: 'Permit',
313
- permit: nativePermitSignature.values,
314
- signature: nativePermitSignature.signature,
315
- });
316
- }
317
- const relayedTransaction = await (0, api_js_1.relayTransaction)({
318
- tokenOwner: this.client.account.address,
319
- chainId: fromChain.id,
320
- permits: signedPermits,
321
- callData: transactionRequest.data,
322
- });
323
- txHash = relayedTransaction.taskId;
324
- txType = 'relayed';
325
- }
326
- else {
327
- if (nativePermitSignature) {
328
- transactionRequest.data = (0, encodeNativePermitData_js_1.encodeNativePermitData)(step.action.fromToken.address, BigInt(step.action.fromAmount), nativePermitSignature.values.deadline, nativePermitSignature.signature, transactionRequest.data);
329
- }
330
- else if (permit2Supported) {
331
- const permit2Signature = await (0, signPermit2Message_js_1.signPermit2Message)({
332
- client: this.client,
333
- chain: fromChain,
334
- tokenAddress: step.action.fromToken.address,
335
- amount: BigInt(step.action.fromAmount),
336
- data: transactionRequest.data,
337
- });
338
- this.statusManager.updateProcess(step, process.type, 'DONE');
339
- process = this.statusManager.findOrCreateProcess({
340
- step,
341
- type: currentProcessType,
342
- status: 'PENDING',
343
- chainId: fromChain.id,
344
- });
345
- transactionRequest.data = (0, encodePermit2Data_js_1.encodePermit2Data)(step.action.fromToken.address, BigInt(step.action.fromAmount), permit2Signature.values.nonce, permit2Signature.values.deadline, transactionRequest.data, permit2Signature.signature);
346
- }
347
- if (nativePermitSignature || permit2Supported) {
348
- try {
349
- transactionRequest.to = fromChain.permit2Proxy;
350
- const estimatedGas = await (0, actions_1.estimateGas)(this.client, {
351
- account: this.client.account,
352
- to: transactionRequest.to,
353
- data: transactionRequest.data,
354
- value: transactionRequest.value,
355
- });
356
- transactionRequest.gas =
357
- transactionRequest.gas && transactionRequest.gas > estimatedGas
358
- ? transactionRequest.gas
359
- : estimatedGas;
360
- }
361
- catch {
362
- transactionRequest.gas = undefined;
363
- }
364
- finally {
365
- this.statusManager.updateProcess(step, process.type, 'DONE');
366
- }
367
- }
368
- process = this.statusManager.updateProcess(step, process.type, 'ACTION_REQUIRED');
369
- txHash = await (0, utils_1.getAction)(this.client, actions_1.sendTransaction, 'sendTransaction')({
370
325
  to: transactionRequest.to,
371
- account: this.client.account,
372
326
  data: transactionRequest.data,
373
327
  value: transactionRequest.value,
374
- gas: transactionRequest.gas,
375
- gasPrice: transactionRequest.gasPrice,
376
- maxFeePerGas: transactionRequest.maxFeePerGas,
377
- maxPriorityFeePerGas: transactionRequest.maxPriorityFeePerGas,
378
- chain: (0, utils_js_2.convertExtendedChain)(fromChain),
379
328
  });
329
+ transactionRequest.gas =
330
+ transactionRequest.gas && transactionRequest.gas > estimatedGas
331
+ ? transactionRequest.gas
332
+ : estimatedGas;
333
+ }
334
+ catch {
335
+ transactionRequest.gas = undefined;
336
+ }
337
+ finally {
338
+ this.statusManager.updateProcess(step, process.type, 'DONE');
380
339
  }
381
- process = this.statusManager.updateProcess(step, process.type, 'PENDING', {
382
- txHash,
383
- txType,
384
- txLink: txType === 'standard'
385
- ? `${fromChain.metamask.blockExplorerUrls[0]}tx/${txHash}`
386
- : undefined,
387
- });
388
- await this.waitForTransaction({
389
- step,
390
- process,
391
- fromChain,
392
- toChain,
393
- txHash,
394
- txType,
395
- isBridgeExecution,
396
- });
397
- return step;
398
- }
399
- catch (e) {
400
- const error = await (0, parseEVMErrors_js_1.parseEVMErrors)(e, step, process);
401
- process = this.statusManager.updateProcess(step, process?.type || currentProcessType, 'FAILED', {
402
- error: {
403
- message: error.cause.message,
404
- code: error.code,
405
- },
406
- });
407
- this.statusManager.updateExecution(step, 'FAILED');
408
- throw error;
409
340
  }
341
+ process = this.statusManager.updateProcess(step, process.type, 'ACTION_REQUIRED');
342
+ txHash = await (0, utils_1.getAction)(this.client, actions_1.sendTransaction, 'sendTransaction')({
343
+ to: transactionRequest.to,
344
+ account: this.client.account,
345
+ data: transactionRequest.data,
346
+ value: transactionRequest.value,
347
+ gas: transactionRequest.gas,
348
+ gasPrice: transactionRequest.gasPrice,
349
+ maxFeePerGas: transactionRequest.maxFeePerGas,
350
+ maxPriorityFeePerGas: transactionRequest.maxPriorityFeePerGas,
351
+ chain: (0, utils_js_1.convertExtendedChain)(fromChain),
352
+ });
410
353
  }
411
- });
412
- this.client = options.client;
413
- }
354
+ process = this.statusManager.updateProcess(step, process.type, 'PENDING', {
355
+ txHash,
356
+ txType,
357
+ txLink: txType === 'standard'
358
+ ? `${fromChain.metamask.blockExplorerUrls[0]}tx/${txHash}`
359
+ : undefined,
360
+ });
361
+ await this.waitForTransaction({
362
+ step,
363
+ process,
364
+ fromChain,
365
+ toChain,
366
+ txHash,
367
+ txType,
368
+ isBridgeExecution,
369
+ });
370
+ return step;
371
+ }
372
+ catch (e) {
373
+ const error = await (0, parseEVMErrors_js_1.parseEVMErrors)(e, step, process);
374
+ process = this.statusManager.updateProcess(step, process?.type || currentProcessType, 'FAILED', {
375
+ error: {
376
+ message: error.cause.message,
377
+ code: error.code,
378
+ },
379
+ });
380
+ this.statusManager.updateExecution(step, 'FAILED');
381
+ throw error;
382
+ }
383
+ };
414
384
  }
415
385
  exports.EVMStepExecutor = EVMStepExecutor;
416
386
  //# sourceMappingURL=EVMStepExecutor.js.map