Skip to content

Commit 766e298

Browse files
committed
chore: switch chain improvements
1 parent 8b1ca42 commit 766e298

File tree

3 files changed

+39
-58
lines changed

3 files changed

+39
-58
lines changed

src/core/EVM/EVMStepExecutor.ts

Lines changed: 36 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,20 @@ export class EVMStepExecutor extends BaseStepExecutor {
5252
// TODO: add checkChain method and update wallet client inside executors
5353
// This can come in handy when we execute multiple routes simultaneously and
5454
// 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
5769
}
5870

5971
executeStep = async (step: LiFiStepExtended): Promise<LiFiStepExtended> => {
@@ -67,28 +79,13 @@ export class EVMStepExecutor extends BaseStepExecutor {
6779
// If the step is waiting for a transaction on the receiving chain, we do not switch the chain
6880
// All changes are already done from the source chain
6981
// 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)
8284
if (!updatedWalletClient) {
83-
// Chain switch was not successful, stop execution here
8485
return step
8586
}
86-
87-
this.walletClient = updatedWalletClient
8887
}
8988

90-
const client = this.walletClient.extend(publicActions)
91-
9289
const isMultisigWalletClient = !!this.multisig?.isMultisigWalletClient
9390
const multisigBatchTransactions: MultisigTransaction[] = []
9491

@@ -110,7 +107,6 @@ export class EVMStepExecutor extends BaseStepExecutor {
110107
)
111108

112109
// Check token approval only if fromToken is not the native token => no approval needed in that case
113-
114110
const checkForAllowance =
115111
!existingProcess?.txHash &&
116112
!isZeroAddress(step.action.fromToken.address) &&
@@ -120,7 +116,7 @@ export class EVMStepExecutor extends BaseStepExecutor {
120116
const data = await checkAllowance(
121117
fromChain,
122118
step,
123-
client,
119+
this.walletClient,
124120
this.statusManager,
125121
this.executionOptions,
126122
this.allowUserInteraction,
@@ -170,22 +166,12 @@ export class EVMStepExecutor extends BaseStepExecutor {
170166
let txHash: Hash
171167
if (process.txHash) {
172168
// 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)
181170
if (!updatedWalletClient) {
182-
// Chain switch was not successful, stop execution here
183171
return step
184172
}
185173

186-
this.walletClient = updatedWalletClient
187-
188-
// Load exiting transaction
174+
// Wait for exiting transaction
189175
txHash = process.txHash as Hash
190176
} else {
191177
process = this.statusManager.updateProcess(
@@ -195,7 +181,7 @@ export class EVMStepExecutor extends BaseStepExecutor {
195181
)
196182

197183
// Check balance
198-
await checkBalance(client.account!.address, step)
184+
await checkBalance(this.walletClient.account!.address, step)
199185

200186
// Create new transaction
201187
if (!step.transactionRequest) {
@@ -223,21 +209,11 @@ export class EVMStepExecutor extends BaseStepExecutor {
223209

224210
// STEP 3: Send the transaction
225211
// 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)
234213
if (!updatedWalletClient) {
235-
// Chain switch was not successful, stop execution here
236214
return step
237215
}
238216

239-
this.walletClient = updatedWalletClient
240-
241217
process = this.statusManager.updateProcess(
242218
step,
243219
process.type,
@@ -266,7 +242,9 @@ export class EVMStepExecutor extends BaseStepExecutor {
266242
// : undefined,
267243
maxPriorityFeePerGas:
268244
this.walletClient.account?.type === 'local'
269-
? await getMaxPriorityFeePerGas(client as PublicClient)
245+
? await getMaxPriorityFeePerGas(
246+
this.walletClient.extend(publicActions) as PublicClient
247+
)
270248
: step.transactionRequest.maxPriorityFeePerGas
271249
? BigInt(
272250
step.transactionRequest.maxPriorityFeePerGas as string
@@ -343,16 +321,18 @@ export class EVMStepExecutor extends BaseStepExecutor {
343321
}
344322

345323
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+
})
356336

357337
if (replacementReason === 'cancelled') {
358338
throw new TransactionError(

src/core/EVM/switchChain.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ export const switchChain = async (
2828
switchChainHook?: SwitchChainHook
2929
): Promise<WalletClient | undefined> => {
3030
// if we are already on the correct chain we can proceed directly
31-
if ((await walletClient.getChainId()) === step.action.fromChainId) {
31+
const currentChainId = await walletClient.getChainId()
32+
if (currentChainId === step.action.fromChainId) {
3233
return walletClient
3334
}
3435

src/core/StatusManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export class StatusManager {
139139
* @param step The step where the process should be updated
140140
* @param type The process type to update
141141
* @param status The status the process gets.
142-
* @param [params] Additional parameters to append to the process.
142+
* @param [params] Additional parameters to append to the process.
143143
* @returns The update process
144144
*/
145145
updateProcess = (

0 commit comments

Comments
 (0)