Refactor approveTransaction to show notification after confirmation

This commit is contained in:
Germán Martínez 2019-09-13 15:59:43 +02:00
parent 83d8c190df
commit 78b5fa2eff
3 changed files with 65 additions and 21 deletions

View File

@ -15,6 +15,7 @@ export const TX_TYPE_EXECUTION = 'execution'
export const TX_TYPE_CONFIRMATION = 'confirmation' export const TX_TYPE_CONFIRMATION = 'confirmation'
export const approveTransaction = async ( export const approveTransaction = async (
showNotification: Function,
safeInstance: any, safeInstance: any,
to: string, to: string,
valueInWei: number | string, valueInWei: number | string,
@ -23,7 +24,7 @@ export const approveTransaction = async (
nonce: number, nonce: number,
sender: string, sender: string,
) => { ) => {
const contractTxHash = await safeInstance.getTransactionHash( const txHash = await safeInstance.getTransactionHash(
to, to,
valueInWei, valueInWei,
data, data,
@ -38,8 +39,21 @@ export const approveTransaction = async (
from: sender, from: sender,
}, },
) )
const receipt = await safeInstance.approveHash(contractTxHash, { from: sender })
try {
const web3 = getWeb3()
const contract = new web3.eth.Contract(GnosisSafeSol.abi, safeInstance.address)
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( await saveTxToHistory(
safeInstance, safeInstance,
to, to,
@ -47,12 +61,23 @@ export const approveTransaction = async (
data, data,
operation, operation,
nonce, nonce,
receipt.tx, // tx hash, receipt.transactionHash,
sender, sender,
TX_TYPE_CONFIRMATION, TX_TYPE_CONFIRMATION,
) )
return receipt 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 ( export const executeTransaction = async (
@ -85,7 +110,7 @@ export const executeTransaction = async (
.send({ .send({
from: sender, from: sender,
}) })
.once('transactionHash', (transactionHash) => { .once('transactionHash', () => {
showNotification() showNotification()
}) })
.on('error', (error) => { .on('error', (error) => {

View File

@ -37,8 +37,8 @@ const createTransaction = (
txHash = await executeTransaction(showNotification, safeInstance, to, valueInWei, txData, CALL, nonce, from) txHash = await executeTransaction(showNotification, safeInstance, to, valueInWei, txData, CALL, nonce, from)
openSnackbar(notifications.AFTER_EXECUTION, 'success') openSnackbar(notifications.AFTER_EXECUTION, 'success')
} else { } else {
openSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, 'success') const showNotification = () => openSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, 'success')
txHash = await approveTransaction(safeInstance, to, valueInWei, txData, CALL, nonce, from) txHash = await approveTransaction(showNotification, safeInstance, to, valueInWei, txData, CALL, nonce, from)
openSnackbar(notifications.CREATED_MORE_CONFIRMATIONS_NEEDED, 'success') openSnackbar(notifications.CREATED_MORE_CONFIRMATIONS_NEEDED, 'success')
} }
} catch (err) { } catch (err) {

View File

@ -46,12 +46,31 @@ const processTransaction = (
let txHash let txHash
if (shouldExecute) { if (shouldExecute) {
openSnackbar('Transaction has been submitted', 'success') const showNotification = () => openSnackbar('Transaction has been submitted', 'success')
txHash = await executeTransaction(safeInstance, tx.recipient, tx.value, tx.data, CALL, nonce, from, sigs) txHash = await executeTransaction(
showNotification,
safeInstance,
tx.recipient,
tx.value,
tx.data,
CALL,
nonce,
from,
sigs,
)
openSnackbar('Transaction has been confirmed', 'success') openSnackbar('Transaction has been confirmed', 'success')
} else { } else {
openSnackbar('Approval transaction has been submitted', 'success') const showNotification = () => openSnackbar('Approval transaction has been submitted', 'success')
txHash = await approveTransaction(safeInstance, tx.recipient, tx.value, tx.data, CALL, nonce, from) txHash = await approveTransaction(
showNotification,
safeInstance,
tx.recipient,
tx.value,
tx.data,
CALL,
nonce,
from,
)
openSnackbar('Approval transaction has been confirmed', 'success') openSnackbar('Approval transaction has been confirmed', 'success')
} }