@@ -52,8 +52,20 @@ export class EVMStepExecutor extends BaseStepExecutor {
52
52
// TODO: add checkChain method and update wallet client inside executors
53
53
// This can come in handy when we execute multiple routes simultaneously and
54
54
// should be sure that we are on the right chain when waiting for transactions.
55
- checkChain = ( ) => {
56
- throw new Error ( 'checkChain is not implemented.' )
55
+ checkChain = async (
56
+ step : LiFiStepExtended
57
+ ) : Promise < WalletClient | undefined > => {
58
+ const updatedWalletClient = await switchChain (
59
+ this . walletClient ,
60
+ this . statusManager ,
61
+ step ,
62
+ this . allowUserInteraction ,
63
+ this . executionOptions ?. switchChainHook
64
+ )
65
+ if ( updatedWalletClient ) {
66
+ this . walletClient = updatedWalletClient
67
+ }
68
+ return updatedWalletClient
57
69
}
58
70
59
71
executeStep = async ( step : LiFiStepExtended ) : Promise < LiFiStepExtended > => {
@@ -67,28 +79,13 @@ export class EVMStepExecutor extends BaseStepExecutor {
67
79
// If the step is waiting for a transaction on the receiving chain, we do not switch the chain
68
80
// All changes are already done from the source chain
69
81
// Return the step
70
- if (
71
- recievingChainProcess ?. substatus !== 'WAIT_DESTINATION_TRANSACTION' ||
72
- ! recievingChainProcess
73
- ) {
74
- const updatedWalletClient = await switchChain (
75
- this . walletClient ,
76
- this . statusManager ,
77
- step ,
78
- this . allowUserInteraction ,
79
- this . executionOptions ?. switchChainHook
80
- )
81
-
82
+ if ( recievingChainProcess ?. substatus !== 'WAIT_DESTINATION_TRANSACTION' ) {
83
+ const updatedWalletClient = await this . checkChain ( step )
82
84
if ( ! updatedWalletClient ) {
83
- // Chain switch was not successful, stop execution here
84
85
return step
85
86
}
86
-
87
- this . walletClient = updatedWalletClient
88
87
}
89
88
90
- const client = this . walletClient . extend ( publicActions )
91
-
92
89
const isMultisigWalletClient = ! ! this . multisig ?. isMultisigWalletClient
93
90
const multisigBatchTransactions : MultisigTransaction [ ] = [ ]
94
91
@@ -110,7 +107,6 @@ export class EVMStepExecutor extends BaseStepExecutor {
110
107
)
111
108
112
109
// Check token approval only if fromToken is not the native token => no approval needed in that case
113
-
114
110
const checkForAllowance =
115
111
! existingProcess ?. txHash &&
116
112
! isZeroAddress ( step . action . fromToken . address ) &&
@@ -120,7 +116,7 @@ export class EVMStepExecutor extends BaseStepExecutor {
120
116
const data = await checkAllowance (
121
117
fromChain ,
122
118
step ,
123
- client ,
119
+ this . walletClient ,
124
120
this . statusManager ,
125
121
this . executionOptions ,
126
122
this . allowUserInteraction ,
@@ -170,22 +166,12 @@ export class EVMStepExecutor extends BaseStepExecutor {
170
166
let txHash : Hash
171
167
if ( process . txHash ) {
172
168
// Make sure that the chain is still correct
173
- const updatedWalletClient = await switchChain (
174
- this . walletClient ,
175
- this . statusManager ,
176
- step ,
177
- this . allowUserInteraction ,
178
- this . executionOptions ?. switchChainHook
179
- )
180
-
169
+ const updatedWalletClient = await this . checkChain ( step )
181
170
if ( ! updatedWalletClient ) {
182
- // Chain switch was not successful, stop execution here
183
171
return step
184
172
}
185
173
186
- this . walletClient = updatedWalletClient
187
-
188
- // Load exiting transaction
174
+ // Wait for exiting transaction
189
175
txHash = process . txHash as Hash
190
176
} else {
191
177
process = this . statusManager . updateProcess (
@@ -195,7 +181,7 @@ export class EVMStepExecutor extends BaseStepExecutor {
195
181
)
196
182
197
183
// Check balance
198
- await checkBalance ( client . account ! . address , step )
184
+ await checkBalance ( this . walletClient . account ! . address , step )
199
185
200
186
// Create new transaction
201
187
if ( ! step . transactionRequest ) {
@@ -223,21 +209,11 @@ export class EVMStepExecutor extends BaseStepExecutor {
223
209
224
210
// STEP 3: Send the transaction
225
211
// Make sure that the chain is still correct
226
- const updatedWalletClient = await switchChain (
227
- this . walletClient ,
228
- this . statusManager ,
229
- step ,
230
- this . allowUserInteraction ,
231
- this . executionOptions ?. switchChainHook
232
- )
233
-
212
+ const updatedWalletClient = await this . checkChain ( step )
234
213
if ( ! updatedWalletClient ) {
235
- // Chain switch was not successful, stop execution here
236
214
return step
237
215
}
238
216
239
- this . walletClient = updatedWalletClient
240
-
241
217
process = this . statusManager . updateProcess (
242
218
step ,
243
219
process . type ,
@@ -266,7 +242,9 @@ export class EVMStepExecutor extends BaseStepExecutor {
266
242
// : undefined,
267
243
maxPriorityFeePerGas :
268
244
this . walletClient . account ?. type === 'local'
269
- ? await getMaxPriorityFeePerGas ( client as PublicClient )
245
+ ? await getMaxPriorityFeePerGas (
246
+ this . walletClient . extend ( publicActions ) as PublicClient
247
+ )
270
248
: step . transactionRequest . maxPriorityFeePerGas
271
249
? BigInt (
272
250
step . transactionRequest . maxPriorityFeePerGas as string
@@ -343,16 +321,18 @@ export class EVMStepExecutor extends BaseStepExecutor {
343
321
}
344
322
345
323
let replacementReason : ReplacementReason | undefined
346
- const transactionReceipt = await client . waitForTransactionReceipt ( {
347
- hash : txHash ,
348
- onReplaced : ( response ) => {
349
- replacementReason = response . reason
350
- this . statusManager . updateProcess ( step , process . type , 'PENDING' , {
351
- txHash : response . transaction . hash ,
352
- txLink : `${ fromChain . metamask . blockExplorerUrls [ 0 ] } tx/${ response . transaction . hash } ` ,
353
- } )
354
- } ,
355
- } )
324
+ const transactionReceipt = await this . walletClient
325
+ . extend ( publicActions )
326
+ . waitForTransactionReceipt ( {
327
+ hash : txHash ,
328
+ onReplaced : ( response ) => {
329
+ replacementReason = response . reason
330
+ this . statusManager . updateProcess ( step , process . type , 'PENDING' , {
331
+ txHash : response . transaction . hash ,
332
+ txLink : `${ fromChain . metamask . blockExplorerUrls [ 0 ] } tx/${ response . transaction . hash } ` ,
333
+ } )
334
+ } ,
335
+ } )
356
336
357
337
if ( replacementReason === 'cancelled' ) {
358
338
throw new TransactionError (
0 commit comments