Refactor execute and process transaction

This commit is contained in:
Germán Martínez 2019-09-24 11:00:37 +02:00
parent 1244b4528a
commit 8d8d45dd44
3 changed files with 74 additions and 31 deletions

View File

@ -7,6 +7,7 @@ import { isEther } from '~/logic/tokens/utils/tokenHelpers'
import { type Token } from '~/logic/tokens/store/model/token'
import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts'
import { type Operation, saveTxToHistory } from '~/logic/safe/transactions'
import { type NotificationsQueue } from '~/logic/notifications'
import { ZERO_ADDRESS } from '~/logic/wallets/ethAddresses'
import { getErrorMessage } from '~/test/utils/ethereumErrors'
@ -15,7 +16,9 @@ export const TX_TYPE_EXECUTION = 'execution'
export const TX_TYPE_CONFIRMATION = 'confirmation'
export const approveTransaction = async (
showNotification: Function,
notiQueue: NotificationsQueue,
enqueueSnackbar: Function,
closeSnackbar: Function,
safeInstance: any,
to: string,
valueInWei: number | string,
@ -40,6 +43,8 @@ export const approveTransaction = async (
},
)
const beforeExecutionKey = enqueueSnackbar(notiQueue.beforeExecution.description, notiQueue.beforeExecution.options)
let pendingExecutionKey
try {
const web3 = getWeb3()
const contract = new web3.eth.Contract(GnosisSafeSol.abi, safeInstance.address)
@ -50,13 +55,18 @@ export const approveTransaction = async (
from: sender,
})
.once('transactionHash', () => {
showNotification()
closeSnackbar(beforeExecutionKey)
pendingExecutionKey = enqueueSnackbar(
notiQueue.pendingExecution.single.description,
notiQueue.pendingExecution.single.options,
)
})
.on('error', (error) => {
/* eslint-disable */
console.log('Tx error:', error)
})
.then(async (receipt) => {
closeSnackbar(pendingExecutionKey)
await saveTxToHistory(
safeInstance,
to,
@ -68,12 +78,15 @@ export const approveTransaction = async (
sender,
TX_TYPE_CONFIRMATION,
)
enqueueSnackbar(notiQueue.afterExecution.description, notiQueue.afterExecution.options)
return receipt.transactionHash
})
return transactionHash
} catch (error) {
closeSnackbar(pendingExecutionKey)
enqueueSnackbar(notiQueue.afterExecutionError.description, notiQueue.afterExecutionError.options)
/* eslint-disable */
const executeData = safeInstance.contract.methods.approveHash(txHash).encodeABI()
const errMsg = await getErrorMessage(safeInstance.address, 0, executeData, sender)
@ -84,7 +97,9 @@ export const approveTransaction = async (
}
export const executeTransaction = async (
showNotification: Function,
notiQueue: NotificationsQueue,
enqueueSnackbar: Function,
closeSnackbar: Function,
safeInstance: any,
to: string,
valueInWei: number | string,
@ -104,6 +119,8 @@ export const executeTransaction = async (
)}000000000000000000000000000000000000000000000000000000000000000001`
}
const beforeExecutionKey = enqueueSnackbar(notiQueue.beforeExecution.description, notiQueue.beforeExecution.options)
let pendingExecutionKey
try {
const web3 = getWeb3()
const contract = new web3.eth.Contract(GnosisSafeSol.abi, safeInstance.address)
@ -114,12 +131,17 @@ export const executeTransaction = async (
from: sender,
})
.once('transactionHash', () => {
showNotification()
closeSnackbar(beforeExecutionKey)
pendingExecutionKey = enqueueSnackbar(
notiQueue.pendingExecution.single.description,
notiQueue.pendingExecution.single.options,
)
})
.on('error', (error) => {
console.log('Tx error:', error)
})
.then(async (receipt) => {
closeSnackbar(pendingExecutionKey)
await saveTxToHistory(
safeInstance,
to,
@ -131,12 +153,15 @@ export const executeTransaction = async (
sender,
TX_TYPE_EXECUTION,
)
enqueueSnackbar(notiQueue.afterExecution.description, notiQueue.afterExecution.options)
return receipt.transactionHash
})
return transactionHash
} catch (error) {
closeSnackbar(beforeExecutionKey)
enqueueSnackbar(notiQueue.afterExecutionError.description, notiQueue.afterExecutionError.options)
/* eslint-disable */
const executeDataUsedSignatures = safeInstance.contract.methods
.execTransaction(to, valueInWei, data, operation, 0, 0, 0, ZERO_ADDRESS, ZERO_ADDRESS, sigs)

View File

@ -6,24 +6,19 @@ import fetchTransactions from '~/routes/safe/store/actions/fetchTransactions'
import { type GlobalState } from '~/store'
import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts'
import {
approveTransaction,
executeTransaction,
CALL,
type NotifiedTransaction, approveTransaction, executeTransaction, CALL,
} from '~/logic/safe/transactions'
import {
type Notifications,
NOTIFICATIONS,
} from '~/logic/notifications'
import { type Variant, SUCCESS, ERROR } from '~/components/Header'
import { getNofiticationsFromTxType } from '~/logic/notifications'
const createTransaction = (
safeAddress: string,
to: string,
valueInWei: string,
txData: string = EMPTY_DATA,
enqueueSnackbar: (message: string, variant: Variant) => void,
notifiedTransaction: NotifiedTransaction,
enqueueSnackbar: Function,
closeSnackbar: Function,
shouldExecute?: boolean,
notifications?: Notifications = NOTIFICATIONS,
) => async (dispatch: ReduxDispatch<GlobalState>, getState: GetState<GlobalState>) => {
const state: GlobalState = getState()
@ -33,19 +28,38 @@ const createTransaction = (
const nonce = (await safeInstance.nonce()).toString()
const isExecution = threshold.toNumber() === 1 || shouldExecute
const notificationsQueue = getNofiticationsFromTxType(notifiedTransaction)
let txHash
try {
if (isExecution) {
const showNotification = () => enqueueSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, { variant: SUCCESS })
txHash = await executeTransaction(showNotification, safeInstance, to, valueInWei, txData, CALL, nonce, from)
enqueueSnackbar(notifications.AFTER_EXECUTION, { variant: SUCCESS })
txHash = await executeTransaction(
notificationsQueue,
enqueueSnackbar,
closeSnackbar,
safeInstance,
to,
valueInWei,
txData,
CALL,
nonce,
from,
)
} else {
const showNotification = () => enqueueSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, { variant: SUCCESS })
txHash = await approveTransaction(showNotification, safeInstance, to, valueInWei, txData, CALL, nonce, from)
enqueueSnackbar(notifications.CREATED_MORE_CONFIRMATIONS_NEEDED, { variant: SUCCESS })
txHash = await approveTransaction(
notificationsQueue,
enqueueSnackbar,
closeSnackbar,
safeInstance,
to,
valueInWei,
txData,
CALL,
nonce,
from,
)
}
} catch (err) {
enqueueSnackbar(notifications.ERROR, { variant: ERROR })
console.error(`Error while creating transaction: ${err}`)
}

View File

@ -6,7 +6,7 @@ import fetchTransactions from '~/routes/safe/store/actions/fetchTransactions'
import { type GlobalState } from '~/store'
import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts'
import { approveTransaction, executeTransaction, CALL } from '~/logic/safe/transactions'
import { type Variant, SUCCESS } from '~/components/Header'
import { getNofiticationsFromTxType } from '~/logic/notifications'
// https://gnosis-safe.readthedocs.io/en/latest/contracts/signatures.html#pre-validated-signatures
// https://github.com/gnosis/safe-contracts/blob/master/test/gnosisSafeTeamEdition.js#L26
@ -32,8 +32,10 @@ const generateSignaturesFromTxConfirmations = (tx: Transaction, preApprovingOwne
const processTransaction = (
safeAddress: string,
tx: Transaction,
enqueueSnackbar: (message: string, variant: Variant) => void,
userAddress: string,
notifiedTransaction: NotifiedTransaction,
enqueueSnackbar: Function,
closeSnackbar: Function,
approveAndExecute?: boolean,
) => async (dispatch: ReduxDispatch<GlobalState>, getState: GetState<GlobalState>) => {
const state: GlobalState = getState()
@ -45,11 +47,14 @@ const processTransaction = (
const shouldExecute = threshold === tx.confirmations.size || approveAndExecute
const sigs = generateSignaturesFromTxConfirmations(tx, approveAndExecute && userAddress)
const notificationsQueue = getNofiticationsFromTxType(notifiedTransaction)
let txHash
if (shouldExecute) {
const showNotification = () => enqueueSnackbar('Transaction has been submitted', { variant: SUCCESS })
txHash = await executeTransaction(
showNotification,
notificationsQueue,
enqueueSnackbar,
closeSnackbar,
safeInstance,
tx.recipient,
tx.value,
@ -59,11 +64,11 @@ const processTransaction = (
from,
sigs,
)
enqueueSnackbar('Transaction has been confirmed', { variant: SUCCESS })
} else {
const showNotification = () => enqueueSnackbar('Approval transaction has been submitted', { variant: SUCCESS })
txHash = await approveTransaction(
showNotification,
notificationsQueue,
enqueueSnackbar,
closeSnackbar,
safeInstance,
tx.recipient,
tx.value,
@ -72,7 +77,6 @@ const processTransaction = (
nonce,
from,
)
enqueueSnackbar('Approval transaction has been confirmed', { variant: SUCCESS })
}
dispatch(fetchTransactions(safeAddress))