From 78b5fa2eff6857bd22ade893b39dcfafdf4b3c10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Mart=C3=ADnez?= Date: Fri, 13 Sep 2019 15:59:43 +0200 Subject: [PATCH] Refactor approveTransaction to show notification after confirmation --- src/logic/safe/transactions/send.js | 55 ++++++++++++++----- .../safe/store/actions/createTransaction.js | 4 +- .../safe/store/actions/processTransaction.js | 27 +++++++-- 3 files changed, 65 insertions(+), 21 deletions(-) diff --git a/src/logic/safe/transactions/send.js b/src/logic/safe/transactions/send.js index 8e560350..128f63f8 100644 --- a/src/logic/safe/transactions/send.js +++ b/src/logic/safe/transactions/send.js @@ -15,6 +15,7 @@ export const TX_TYPE_EXECUTION = 'execution' export const TX_TYPE_CONFIRMATION = 'confirmation' export const approveTransaction = async ( + showNotification: Function, safeInstance: any, to: string, valueInWei: number | string, @@ -23,7 +24,7 @@ export const approveTransaction = async ( nonce: number, sender: string, ) => { - const contractTxHash = await safeInstance.getTransactionHash( + const txHash = await safeInstance.getTransactionHash( to, valueInWei, data, @@ -38,21 +39,45 @@ export const approveTransaction = async ( from: sender, }, ) - const receipt = await safeInstance.approveHash(contractTxHash, { from: sender }) - await saveTxToHistory( - safeInstance, - to, - valueInWei, - data, - operation, - nonce, - receipt.tx, // tx hash, - sender, - TX_TYPE_CONFIRMATION, - ) + try { + const web3 = getWeb3() + const contract = new web3.eth.Contract(GnosisSafeSol.abi, safeInstance.address) - return receipt + const transactionHash = await contract.methods.approveHash(txHash) + .send({ + from: sender, + }).once('transactionHash', () => { + showNotification() + }) + .on('error', (error) => { + console.log('Tx error:', error) + }) + .then(async (receipt) => { + await saveTxToHistory( + safeInstance, + to, + valueInWei, + data, + operation, + nonce, + receipt.transactionHash, + sender, + TX_TYPE_CONFIRMATION, + ) + + return receipt.transactionHash + }) + + return transactionHash + } catch (error) { + /* eslint-disable */ + const executeData = safeInstance.contract.methods.approveHash(txHash).encodeABI() + const errMsg = await getErrorMessage(safeInstance.address, 0, executeData, sender) + console.log(`Error executing the TX: ${errMsg}`) + + throw error + } } export const executeTransaction = async ( @@ -85,7 +110,7 @@ export const executeTransaction = async ( .send({ from: sender, }) - .once('transactionHash', (transactionHash) => { + .once('transactionHash', () => { showNotification() }) .on('error', (error) => { diff --git a/src/routes/safe/store/actions/createTransaction.js b/src/routes/safe/store/actions/createTransaction.js index c60a3e05..ad14b901 100644 --- a/src/routes/safe/store/actions/createTransaction.js +++ b/src/routes/safe/store/actions/createTransaction.js @@ -37,8 +37,8 @@ const createTransaction = ( txHash = await executeTransaction(showNotification, safeInstance, to, valueInWei, txData, CALL, nonce, from) openSnackbar(notifications.AFTER_EXECUTION, 'success') } else { - openSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, 'success') - txHash = await approveTransaction(safeInstance, to, valueInWei, txData, CALL, nonce, from) + const showNotification = () => openSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, 'success') + txHash = await approveTransaction(showNotification, safeInstance, to, valueInWei, txData, CALL, nonce, from) openSnackbar(notifications.CREATED_MORE_CONFIRMATIONS_NEEDED, 'success') } } catch (err) { diff --git a/src/routes/safe/store/actions/processTransaction.js b/src/routes/safe/store/actions/processTransaction.js index c30fd27c..1a077488 100644 --- a/src/routes/safe/store/actions/processTransaction.js +++ b/src/routes/safe/store/actions/processTransaction.js @@ -46,12 +46,31 @@ const processTransaction = ( let txHash if (shouldExecute) { - openSnackbar('Transaction has been submitted', 'success') - txHash = await executeTransaction(safeInstance, tx.recipient, tx.value, tx.data, CALL, nonce, from, sigs) + const showNotification = () => openSnackbar('Transaction has been submitted', 'success') + txHash = await executeTransaction( + showNotification, + safeInstance, + tx.recipient, + tx.value, + tx.data, + CALL, + nonce, + from, + sigs, + ) openSnackbar('Transaction has been confirmed', 'success') } else { - openSnackbar('Approval transaction has been submitted', 'success') - txHash = await approveTransaction(safeInstance, tx.recipient, tx.value, tx.data, CALL, nonce, from) + const showNotification = () => openSnackbar('Approval transaction has been submitted', 'success') + txHash = await approveTransaction( + showNotification, + safeInstance, + tx.recipient, + tx.value, + tx.data, + CALL, + nonce, + from, + ) openSnackbar('Approval transaction has been confirmed', 'success') }