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 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,8 +39,21 @@ export const approveTransaction = async (
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(
safeInstance,
to,
@ -47,12 +61,23 @@ export const approveTransaction = async (
data,
operation,
nonce,
receipt.tx, // tx hash,
receipt.transactionHash,
sender,
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 (
@ -85,7 +110,7 @@ export const executeTransaction = async (
.send({
from: sender,
})
.once('transactionHash', (transactionHash) => {
.once('transactionHash', () => {
showNotification()
})
.on('error', (error) => {

View File

@ -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) {

View File

@ -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')
}