refactor transactions logic functions and actions so the logic for sending notifications/posting to service is contained in actions only

This commit is contained in:
mmv 2019-09-25 19:28:18 +04:00
parent 10fc44d3d9
commit b5784d8e7e
3 changed files with 141 additions and 79 deletions

View File

@ -6,7 +6,7 @@ import { EMPTY_DATA } from '~/logic/wallets/ethTransactions'
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 Operation } from '~/logic/safe/transactions'
import { ZERO_ADDRESS } from '~/logic/wallets/ethAddresses'
import { getErrorMessage } from '~/test/utils/ethereumErrors'
@ -14,8 +14,7 @@ export const CALL = 0
export const TX_TYPE_EXECUTION = 'execution'
export const TX_TYPE_CONFIRMATION = 'confirmation'
export const approveTransaction = async (
showNotification: Function,
export const getApprovalTransaction = async (
safeInstance: any,
to: string,
valueInWei: number | string,
@ -44,32 +43,7 @@ export const approveTransaction = async (
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,
valueInWei,
data,
operation,
nonce,
receipt.transactionHash,
sender,
TX_TYPE_CONFIRMATION,
)
return receipt.transactionHash
})
return transactionHash
return contract.methods.approveHash(txHash)
} catch (error) {
/* eslint-disable */
const executeData = safeInstance.contract.methods.approveHash(txHash).encodeABI()
@ -80,8 +54,7 @@ export const approveTransaction = async (
}
}
export const executeTransaction = async (
showNotification: Function,
export const getExecutionTransaction = async (
safeInstance: any,
to: string,
valueInWei: number | string,
@ -105,34 +78,7 @@ export const executeTransaction = async (
const web3 = getWeb3()
const contract = new web3.eth.Contract(GnosisSafeSol.abi, safeInstance.address)
const transactionHash = await contract.methods
.execTransaction(to, valueInWei, data, operation, 0, 0, 0, ZERO_ADDRESS, ZERO_ADDRESS, sigs)
.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_EXECUTION,
)
return receipt.transactionHash
})
return transactionHash
return contract.methods.execTransaction(to, valueInWei, data, operation, 0, 0, 0, ZERO_ADDRESS, ZERO_ADDRESS, sigs)
} catch (error) {
/* eslint-disable */
const executeDataUsedSignatures = safeInstance.contract.methods

View File

@ -6,11 +6,14 @@ import fetchTransactions from '~/routes/safe/store/actions/fetchTransactions'
import { type GlobalState } from '~/store'
import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts'
import {
approveTransaction,
executeTransaction,
getApprovalTransaction,
getExecutionTransaction,
CALL,
type Notifications,
DEFAULT_NOTIFICATIONS,
TX_TYPE_CONFIRMATION,
TX_TYPE_EXECUTION,
saveTxToHistory,
} from '~/logic/safe/transactions'
const createTransaction = (
@ -34,11 +37,73 @@ const createTransaction = (
try {
if (isExecution) {
const showNotification = () => openSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, 'success')
txHash = await executeTransaction(showNotification, safeInstance, to, valueInWei, txData, CALL, nonce, from)
const tx = await getExecutionTransaction(
showNotification,
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 {
const showNotification = () => openSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, 'success')
txHash = await approveTransaction(showNotification, safeInstance, to, valueInWei, txData, CALL, nonce, from)
const tx = await getApprovalTransaction(safeInstance, to, valueInWei, txData, CALL, nonce, from)
await tx.send({
from,
})
.once('transactionHash', (hash) => {
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_CONFIRMATION,
)
return receipt.transactionHash
})
openSnackbar(notifications.CREATED_MORE_CONFIRMATIONS_NEEDED, 'success')
}
} catch (err) {

View File

@ -5,7 +5,14 @@ import { userAccountSelector } from '~/logic/wallets/store/selectors'
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 {
getApprovalTransaction,
getExecutionTransaction,
CALL,
saveTxToHistory,
TX_TYPE_EXECUTION,
TX_TYPE_CONFIRMATION,
} from '~/logic/safe/transactions'
// 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
@ -46,9 +53,7 @@ const processTransaction = (
let txHash
if (shouldExecute) {
const showNotification = () => openSnackbar('Transaction has been submitted', 'success')
txHash = await executeTransaction(
showNotification,
const transaction = await getExecutionTransaction(
safeInstance,
tx.recipient,
tx.value,
@ -58,19 +63,65 @@ const processTransaction = (
from,
sigs,
)
await transaction
.send({
from,
})
.once('transactionHash', (hash: string) => {
txHash = hash
openSnackbar('Transaction has been submitted', 'success')
})
.on('error', (error) => {
console.error('Tx error: ', error)
})
.then(async (receipt) => {
await saveTxToHistory(
safeInstance,
tx.recipient,
tx.value,
tx.data,
CALL,
nonce,
receipt.transactionHash,
from,
TX_TYPE_EXECUTION,
)
return receipt.transactionHash
})
openSnackbar('Transaction has been confirmed', 'success')
} else {
const showNotification = () => openSnackbar('Approval transaction has been submitted', 'success')
txHash = await approveTransaction(
showNotification,
safeInstance,
tx.recipient,
tx.value,
tx.data,
CALL,
nonce,
from,
)
const transaction = await getApprovalTransaction(safeInstance, tx.recipient, tx.value, tx.data, CALL, nonce, from)
await transaction
.send({
from,
})
.once('transactionHash', (hash) => {
txHash = hash
openSnackbar('Approval transaction has been submitted', 'success')
})
.on('error', (error) => {
console.error('Tx error: ', error)
})
.then(async (receipt) => {
await saveTxToHistory(
safeInstance,
tx.recipient,
tx.value,
tx.data,
CALL,
nonce,
receipt.transactionHash,
from,
TX_TYPE_CONFIRMATION,
)
return receipt.transactionHash
})
openSnackbar('Approval transaction has been confirmed', 'success')
}