diff --git a/src/execution/allowance.execute.ts b/src/execution/allowance.execute.ts index 5b6d9d2e..9fa0cf88 100644 --- a/src/execution/allowance.execute.ts +++ b/src/execution/allowance.execute.ts @@ -20,7 +20,7 @@ export const checkAllowance = async ( ): Promise => { // Ask user to set allowance // -> set currentExecution - const allowanceProcess = statusManager.findOrCreateProcess( + let allowanceProcess: Process = statusManager.findOrCreateProcess( 'TOKEN_ALLOWANCE', step ) @@ -28,12 +28,24 @@ export const checkAllowance = async ( // -> check allowance try { if (allowanceProcess.txHash) { - statusManager.updateProcess(step, allowanceProcess.type, 'PENDING') - await getProvider(signer).waitForTransaction(allowanceProcess.txHash) - statusManager.updateProcess(step, allowanceProcess.type, 'DONE') + allowanceProcess = statusManager.updateProcess( + step, + allowanceProcess.type, + 'PENDING' + ) + await getProvider(signer).waitForTransaction(allowanceProcess!.txHash!) + allowanceProcess = statusManager.updateProcess( + step, + allowanceProcess.type, + 'DONE' + ) // TODO: Do we need this check? } else if (allowanceProcess.status === 'DONE') { - statusManager.updateProcess(step, allowanceProcess.type, 'DONE') + allowanceProcess = statusManager.updateProcess( + step, + allowanceProcess.type, + 'DONE' + ) } else { const approved = await getApproved(signer, token.address, spenderAddress) @@ -52,17 +64,31 @@ export const checkAllowance = async ( ) // update currentExecution - statusManager.updateProcess(step, allowanceProcess.type, 'PENDING', { - txHash: approveTx.hash, - txLink: chain.metamask.blockExplorerUrls[0] + 'tx/' + approveTx.hash, - }) + allowanceProcess = statusManager.updateProcess( + step, + allowanceProcess.type, + 'PENDING', + { + txHash: approveTx.hash, + txLink: + chain.metamask.blockExplorerUrls[0] + 'tx/' + approveTx.hash, + } + ) // wait for transcation await approveTx.wait() - statusManager.updateProcess(step, allowanceProcess.type, 'DONE') + allowanceProcess = statusManager.updateProcess( + step, + allowanceProcess.type, + 'DONE' + ) } else { - statusManager.updateProcess(step, allowanceProcess.type, 'DONE') + allowanceProcess = statusManager.updateProcess( + step, + allowanceProcess.type, + 'DONE' + ) } } } catch (e: any) { @@ -77,13 +103,18 @@ export const checkAllowance = async ( ) } else { const error = await parseError(e, step, allowanceProcess) - statusManager.updateProcess(step, allowanceProcess.type, 'FAILED', { - error: { - message: error.message, - htmlMessage: error.htmlMessage, - code: error.code, - }, - }) + allowanceProcess = statusManager.updateProcess( + step, + allowanceProcess.type, + 'FAILED', + { + error: { + message: error.message, + htmlMessage: error.htmlMessage, + code: error.code, + }, + } + ) statusManager.updateExecution(step, 'FAILED') throw error } @@ -98,12 +129,22 @@ const transactionReplaced = async ( statusManager: StatusManager ) => { try { - statusManager.updateProcess(step, allowanceProcess.type, 'PENDING', { - txHash: replacementTx.hash, - txLink: chain.metamask.blockExplorerUrls[0] + 'tx/' + replacementTx.hash, - }) + allowanceProcess = statusManager.updateProcess( + step, + allowanceProcess.type, + 'PENDING', + { + txHash: replacementTx.hash, + txLink: + chain.metamask.blockExplorerUrls[0] + 'tx/' + replacementTx.hash, + } + ) await replacementTx.wait() - statusManager.updateProcess(step, allowanceProcess.type, 'DONE') + allowanceProcess = statusManager.updateProcess( + step, + allowanceProcess.type, + 'DONE' + ) } catch (e: any) { if (e.code === 'TRANSACTION_REPLACED' && e.replacement) { await transactionReplaced( diff --git a/src/execution/bridges/bridge.execute.ts b/src/execution/bridges/bridge.execute.ts index 791ee2a6..8178e0a4 100644 --- a/src/execution/bridges/bridge.execute.ts +++ b/src/execution/bridges/bridge.execute.ts @@ -57,7 +57,7 @@ export class BridgeExecutionManager { } // STEP 2: Get Transaction //////////////////////////////////////////////// - const crossChainProcess = statusManager.findOrCreateProcess( + let crossChainProcess = statusManager.findOrCreateProcess( 'CROSS_CHAIN', step ) @@ -113,7 +113,7 @@ export class BridgeExecutionManager { signer = updatedSigner - statusManager.updateProcess( + crossChainProcess = statusManager.updateProcess( step, crossChainProcess.type, 'ACTION_REQUIRED' @@ -125,40 +125,59 @@ export class BridgeExecutionManager { tx = await signer.sendTransaction(transactionRequest) // STEP 4: Wait for Transaction /////////////////////////////////////////// - statusManager.updateProcess(step, crossChainProcess.type, 'PENDING', { - txHash: tx.hash, - txLink: fromChain.metamask.blockExplorerUrls[0] + 'tx/' + tx.hash, - }) + crossChainProcess = statusManager.updateProcess( + step, + crossChainProcess.type, + 'PENDING', + { + txHash: tx.hash, + txLink: fromChain.metamask.blockExplorerUrls[0] + 'tx/' + tx.hash, + } + ) } await tx.wait() } catch (e: any) { if (e.code === 'TRANSACTION_REPLACED' && e.replacement) { - statusManager.updateProcess(step, crossChainProcess.type, 'PENDING', { - txHash: e.replacement.hash, - txLink: - fromChain.metamask.blockExplorerUrls[0] + - 'tx/' + - e.replacement.hash, - }) + crossChainProcess = statusManager.updateProcess( + step, + crossChainProcess.type, + 'PENDING', + { + txHash: e.replacement.hash, + txLink: + fromChain.metamask.blockExplorerUrls[0] + + 'tx/' + + e.replacement.hash, + } + ) } else { const error = await parseError(e, step, crossChainProcess) - statusManager.updateProcess(step, crossChainProcess.type, 'FAILED', { - error: { - message: error.message, - htmlMessage: error.htmlMessage, - code: error.code, - }, - }) + crossChainProcess = statusManager.updateProcess( + step, + crossChainProcess.type, + 'FAILED', + { + error: { + message: error.message, + htmlMessage: error.htmlMessage, + code: error.code, + }, + } + ) statusManager.updateExecution(step, 'FAILED') throw error } } - statusManager.updateProcess(step, crossChainProcess.type, 'DONE') + crossChainProcess = statusManager.updateProcess( + step, + crossChainProcess.type, + 'DONE' + ) // STEP 5: Wait for Receiver ////////////////////////////////////// - const receivingChainProcess = statusManager.findOrCreateProcess( + let receivingChainProcess = statusManager.findOrCreateProcess( 'RECEIVING_CHAIN', step, 'PENDING' @@ -175,31 +194,41 @@ export class BridgeExecutionManager { step ) } catch (e: any) { - statusManager.updateProcess(step, receivingChainProcess.type, 'FAILED', { - error: { - code: LifiErrorCode.TransactionFailed, - message: 'Failed while waiting for receiving chain.', - htmlMessage: getTransactionFailedMessage( - step, - crossChainProcess.txLink - ), - }, - }) + receivingChainProcess = statusManager.updateProcess( + step, + receivingChainProcess.type, + 'FAILED', + { + error: { + code: LifiErrorCode.TransactionFailed, + message: 'Failed while waiting for receiving chain.', + htmlMessage: getTransactionFailedMessage( + step, + crossChainProcess.txLink + ), + }, + } + ) statusManager.updateExecution(step, 'FAILED') throw e } - statusManager.updateProcess(step, receivingChainProcess.type, 'DONE', { - substatus: statusResponse.substatus, - substatusMessage: - statusResponse.substatusMessage || - getSubstatusMessage(statusResponse.status, statusResponse.substatus), - txHash: statusResponse.receiving?.txHash, - txLink: - toChain.metamask.blockExplorerUrls[0] + - 'tx/' + - statusResponse.receiving?.txHash, - }) + receivingChainProcess = statusManager.updateProcess( + step, + receivingChainProcess.type, + 'DONE', + { + substatus: statusResponse.substatus, + substatusMessage: + statusResponse.substatusMessage || + getSubstatusMessage(statusResponse.status, statusResponse.substatus), + txHash: statusResponse.receiving?.txHash, + txLink: + toChain.metamask.blockExplorerUrls[0] + + 'tx/' + + statusResponse.receiving?.txHash, + } + ) statusManager.updateExecution(step, 'DONE', { fromAmount: statusResponse.sending.amount, diff --git a/src/execution/exchanges/swap.execute.ts b/src/execution/exchanges/swap.execute.ts index f64645f4..c51fcaa7 100644 --- a/src/execution/exchanges/swap.execute.ts +++ b/src/execution/exchanges/swap.execute.ts @@ -55,7 +55,7 @@ export class SwapExecutionManager { // Start Swap // -> set step.execution - const swapProcess = statusManager.findOrCreateProcess('SWAP', step) + let swapProcess = statusManager.findOrCreateProcess('SWAP', step) // -> swapping let tx: TransactionResponse @@ -108,7 +108,11 @@ export class SwapExecutionManager { signer = updatedSigner // -> set step.execution - statusManager.updateProcess(step, swapProcess.type, 'ACTION_REQUIRED') + swapProcess = swapProcess = statusManager.updateProcess( + step, + swapProcess.type, + 'ACTION_REQUIRED' + ) if (!this.shouldContinue) { return step.execution! // stop before user interaction is needed } @@ -118,22 +122,32 @@ export class SwapExecutionManager { } } catch (e) { const error = await parseError(e, step, swapProcess) - statusManager.updateProcess(step, swapProcess.type, 'FAILED', { - error: { - message: error.message, - htmlMessage: error.htmlMessage, - code: error.code, - }, - }) + swapProcess = statusManager.updateProcess( + step, + swapProcess.type, + 'FAILED', + { + error: { + message: error.message, + htmlMessage: error.htmlMessage, + code: error.code, + }, + } + ) statusManager.updateExecution(step, 'FAILED') throw error } // Wait for Transaction - statusManager.updateProcess(step, swapProcess.type, 'PENDING', { - txLink: fromChain.metamask.blockExplorerUrls[0] + 'tx/' + tx.hash, - txHash: tx.hash, - }) + swapProcess = statusManager.updateProcess( + step, + swapProcess.type, + 'PENDING', + { + txLink: fromChain.metamask.blockExplorerUrls[0] + 'tx/' + tx.hash, + txHash: tx.hash, + } + ) // -> waiting let receipt: TransactionReceipt @@ -143,22 +157,32 @@ export class SwapExecutionManager { // -> set status if (e.code === 'TRANSACTION_REPLACED' && e.replacement) { receipt = e.replacement - statusManager.updateProcess(step, swapProcess.type, 'PENDING', { - txHash: e.replacement.hash, - txLink: - fromChain.metamask.blockExplorerUrls[0] + - 'tx/' + - e.replacement.hash, - }) + swapProcess = statusManager.updateProcess( + step, + swapProcess.type, + 'PENDING', + { + txHash: e.replacement.hash, + txLink: + fromChain.metamask.blockExplorerUrls[0] + + 'tx/' + + e.replacement.hash, + } + ) } else { const error = await parseError(e) - statusManager.updateProcess(step, swapProcess.type, 'FAILED', { - error: { - message: error.message, - htmlMessage: error.htmlMessage, - code: error.code, - }, - }) + swapProcess = statusManager.updateProcess( + step, + swapProcess.type, + 'FAILED', + { + error: { + message: error.message, + htmlMessage: error.htmlMessage, + code: error.code, + }, + } + ) statusManager.updateExecution(step, 'FAILED') throw error } @@ -176,18 +200,23 @@ export class SwapExecutionManager { step ) } catch (e: any) { - statusManager.updateProcess(step, swapProcess.type, 'FAILED', { - error: { - code: LifiErrorCode.TransactionFailed, - message: 'Failed while waiting for receiving chain.', - htmlMessage: getTransactionFailedMessage(step, swapProcess.txLink), - }, - }) + swapProcess = statusManager.updateProcess( + step, + swapProcess.type, + 'FAILED', + { + error: { + code: LifiErrorCode.TransactionFailed, + message: 'Failed while waiting for receiving chain.', + htmlMessage: getTransactionFailedMessage(step, swapProcess.txLink), + }, + } + ) statusManager.updateExecution(step, 'FAILED') throw e } - statusManager.updateProcess(step, swapProcess.type, 'DONE', { + swapProcess = statusManager.updateProcess(step, swapProcess.type, 'DONE', { txHash: statusResponse.receiving?.txHash, txLink: fromChain.metamask.blockExplorerUrls[0] + diff --git a/src/execution/switchChain.ts b/src/execution/switchChain.ts index 8fc99e8a..be4e4ace 100644 --- a/src/execution/switchChain.ts +++ b/src/execution/switchChain.ts @@ -31,7 +31,7 @@ export const switchChain = async ( step.execution = statusManager.initExecutionObject(step) statusManager.updateExecution(step, 'CHAIN_SWITCH_REQUIRED') - const switchProcess = statusManager.findOrCreateProcess( + let switchProcess = statusManager.findOrCreateProcess( 'SWITCH_CHAIN', step, 'PENDING' @@ -53,16 +53,25 @@ export const switchChain = async ( ) } - statusManager.updateProcess(step, switchProcess.type, 'DONE') + switchProcess = statusManager.updateProcess( + step, + switchProcess.type, + 'DONE' + ) statusManager.updateExecution(step, 'PENDING') return updatedSigner } catch (error: any) { - statusManager.updateProcess(step, switchProcess.type, 'FAILED', { - error: { - message: error.message, - code: error.code, - }, - }) + switchProcess = statusManager.updateProcess( + step, + switchProcess.type, + 'FAILED', + { + error: { + message: error.message, + code: error.code, + }, + } + ) statusManager.updateExecution(step, 'FAILED') throw error }