From 6702056709300fe9c21d963585bb2d37652f4ef0 Mon Sep 17 00:00:00 2001 From: mmv Date: Thu, 26 Sep 2019 15:41:36 +0400 Subject: [PATCH] Refactor createTransaction action, reuse the events code for both execution and confirmation txs' --- .../safe/store/actions/createTransaction.js | 93 +++++++------------ 1 file changed, 31 insertions(+), 62 deletions(-) diff --git a/src/routes/safe/store/actions/createTransaction.js b/src/routes/safe/store/actions/createTransaction.js index 58ddbe67..a1fe34ca 100644 --- a/src/routes/safe/store/actions/createTransaction.js +++ b/src/routes/safe/store/actions/createTransaction.js @@ -34,76 +34,45 @@ const createTransaction = ( const isExecution = threshold.toNumber() === 1 || shouldExecute let txHash + let tx try { if (isExecution) { - const tx = await getExecutionTransaction( - safeInstance, - to, - valueInWei, - txData, - CALL, - nonce, - from, - ) - - await tx - .send({ - from, - }) - .once('transactionHash', (hash: string) => { - txHash = hash - openSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, 'success') - }) - .on('error', (error) => { - console.error('Tx error: ', error) - }) - .then(async (receipt) => { - await saveTxToHistory( - safeInstance, - to, - valueInWei, - txData, - CALL, - nonce, - receipt.transactionHash, - from, - TX_TYPE_EXECUTION, - ) - - return receipt.transactionHash - }) - openSnackbar(notifications.AFTER_EXECUTION, 'success') + tx = await getExecutionTransaction(safeInstance, to, valueInWei, txData, CALL, nonce, from) } else { - const tx = await getApprovalTransaction(safeInstance, to, valueInWei, txData, CALL, nonce, from) + tx = await getApprovalTransaction(safeInstance, to, valueInWei, txData, CALL, nonce, from) + } - await tx.send({ + await tx + .send({ from, }) - .once('transactionHash', (hash) => { - txHash = hash - openSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, 'success') - }) - .on('error', (error) => { - console.error('Tx error: ', error) - }) - .then(async (receipt) => { - await saveTxToHistory( - safeInstance, - to, - valueInWei, - txData, - CALL, - nonce, - receipt.transactionHash, - from, - TX_TYPE_CONFIRMATION, - ) + .once('transactionHash', (hash) => { + txHash = hash + openSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, 'success') + }) + .on('error', (error) => { + console.error('Tx error: ', error) + }) + .then(async (receipt) => { + await saveTxToHistory( + safeInstance, + to, + valueInWei, + txData, + CALL, + nonce, + receipt.transactionHash, + from, + isExecution ? TX_TYPE_EXECUTION : TX_TYPE_CONFIRMATION, + ) - return receipt.transactionHash - }) + return receipt.transactionHash + }) - openSnackbar(notifications.CREATED_MORE_CONFIRMATIONS_NEEDED, 'success') - } + openSnackbar( + isExecution ? notifications.AFTER_EXECUTION : notifications.CREATED_MORE_CONFIRMATIONS_NEEDED, + 'success', + ) } catch (err) { openSnackbar(notifications.ERROR, 'error') console.error(`Error while creating transaction: ${err}`)