Refactor createTransaction action, reuse the events code for both execution and confirmation txs'

This commit is contained in:
mmv 2019-09-26 15:41:36 +04:00
parent e33c63d68c
commit 6702056709
1 changed files with 31 additions and 62 deletions

View File

@ -34,76 +34,45 @@ const createTransaction = (
const isExecution = threshold.toNumber() === 1 || shouldExecute const isExecution = threshold.toNumber() === 1 || shouldExecute
let txHash let txHash
let tx
try { try {
if (isExecution) { if (isExecution) {
const tx = await getExecutionTransaction( tx = await getExecutionTransaction(safeInstance, to, valueInWei, txData, CALL, nonce, from)
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')
} else { } 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, from,
}) })
.once('transactionHash', (hash) => { .once('transactionHash', (hash) => {
txHash = hash txHash = hash
openSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, 'success') openSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, 'success')
}) })
.on('error', (error) => { .on('error', (error) => {
console.error('Tx error: ', error) console.error('Tx error: ', error)
}) })
.then(async (receipt) => { .then(async (receipt) => {
await saveTxToHistory( await saveTxToHistory(
safeInstance, safeInstance,
to, to,
valueInWei, valueInWei,
txData, txData,
CALL, CALL,
nonce, nonce,
receipt.transactionHash, receipt.transactionHash,
from, from,
TX_TYPE_CONFIRMATION, 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) { } catch (err) {
openSnackbar(notifications.ERROR, 'error') openSnackbar(notifications.ERROR, 'error')
console.error(`Error while creating transaction: ${err}`) console.error(`Error while creating transaction: ${err}`)