Skip to content

Commit

Permalink
fix: statusmanager reference issue (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
Addminus authored Aug 3, 2022
1 parent c5acdab commit 0e4fe27
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 109 deletions.
87 changes: 64 additions & 23 deletions src/execution/allowance.execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,32 @@ export const checkAllowance = async (
): Promise<void> => {
// Ask user to set allowance
// -> set currentExecution
const allowanceProcess = statusManager.findOrCreateProcess(
let allowanceProcess: Process = statusManager.findOrCreateProcess(
'TOKEN_ALLOWANCE',
step
)

// -> 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)

Expand All @@ -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) {
Expand All @@ -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
}
Expand All @@ -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(
Expand Down
115 changes: 72 additions & 43 deletions src/execution/bridges/bridge.execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class BridgeExecutionManager {
}

// STEP 2: Get Transaction ////////////////////////////////////////////////
const crossChainProcess = statusManager.findOrCreateProcess(
let crossChainProcess = statusManager.findOrCreateProcess(
'CROSS_CHAIN',
step
)
Expand Down Expand Up @@ -113,7 +113,7 @@ export class BridgeExecutionManager {

signer = updatedSigner

statusManager.updateProcess(
crossChainProcess = statusManager.updateProcess(
step,
crossChainProcess.type,
'ACTION_REQUIRED'
Expand All @@ -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'
Expand All @@ -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,
Expand Down
Loading

0 comments on commit 0e4fe27

Please sign in to comment.