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 { isEther } from '~/logic/tokens/utils/tokenHelpers'
import { type Token } from '~/logic/tokens/store/model/token' import { type Token } from '~/logic/tokens/store/model/token'
import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts' 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 { ZERO_ADDRESS } from '~/logic/wallets/ethAddresses'
import { getErrorMessage } from '~/test/utils/ethereumErrors' import { getErrorMessage } from '~/test/utils/ethereumErrors'
@ -14,8 +14,7 @@ export const CALL = 0
export const TX_TYPE_EXECUTION = 'execution' export const TX_TYPE_EXECUTION = 'execution'
export const TX_TYPE_CONFIRMATION = 'confirmation' export const TX_TYPE_CONFIRMATION = 'confirmation'
export const approveTransaction = async ( export const getApprovalTransaction = async (
showNotification: Function,
safeInstance: any, safeInstance: any,
to: string, to: string,
valueInWei: number | string, valueInWei: number | string,
@ -44,32 +43,7 @@ export const approveTransaction = async (
const web3 = getWeb3() const web3 = getWeb3()
const contract = new web3.eth.Contract(GnosisSafeSol.abi, safeInstance.address) const contract = new web3.eth.Contract(GnosisSafeSol.abi, safeInstance.address)
const transactionHash = await contract.methods.approveHash(txHash) return 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) { } catch (error) {
/* eslint-disable */ /* eslint-disable */
const executeData = safeInstance.contract.methods.approveHash(txHash).encodeABI() const executeData = safeInstance.contract.methods.approveHash(txHash).encodeABI()
@ -80,8 +54,7 @@ export const approveTransaction = async (
} }
} }
export const executeTransaction = async ( export const getExecutionTransaction = async (
showNotification: Function,
safeInstance: any, safeInstance: any,
to: string, to: string,
valueInWei: number | string, valueInWei: number | string,
@ -105,34 +78,7 @@ export const executeTransaction = async (
const web3 = getWeb3() const web3 = getWeb3()
const contract = new web3.eth.Contract(GnosisSafeSol.abi, safeInstance.address) const contract = new web3.eth.Contract(GnosisSafeSol.abi, safeInstance.address)
const transactionHash = await contract.methods return contract.methods.execTransaction(to, valueInWei, data, operation, 0, 0, 0, ZERO_ADDRESS, ZERO_ADDRESS, sigs)
.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
} catch (error) { } catch (error) {
/* eslint-disable */ /* eslint-disable */
const executeDataUsedSignatures = safeInstance.contract.methods 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 { type GlobalState } from '~/store'
import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts' import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts'
import { import {
approveTransaction, getApprovalTransaction,
executeTransaction, getExecutionTransaction,
CALL, CALL,
type Notifications, type Notifications,
DEFAULT_NOTIFICATIONS, DEFAULT_NOTIFICATIONS,
TX_TYPE_CONFIRMATION,
TX_TYPE_EXECUTION,
saveTxToHistory,
} from '~/logic/safe/transactions' } from '~/logic/safe/transactions'
const createTransaction = ( const createTransaction = (
@ -34,11 +37,73 @@ const createTransaction = (
try { try {
if (isExecution) { if (isExecution) {
const showNotification = () => openSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, 'success') 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') openSnackbar(notifications.AFTER_EXECUTION, 'success')
} else { } else {
const showNotification = () => openSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, 'success') const tx = await getApprovalTransaction(safeInstance, to, valueInWei, txData, CALL, nonce, from)
txHash = await approveTransaction(showNotification, 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') openSnackbar(notifications.CREATED_MORE_CONFIRMATIONS_NEEDED, 'success')
} }
} catch (err) { } catch (err) {

View File

@ -5,7 +5,14 @@ import { userAccountSelector } from '~/logic/wallets/store/selectors'
import fetchTransactions from '~/routes/safe/store/actions/fetchTransactions' import fetchTransactions from '~/routes/safe/store/actions/fetchTransactions'
import { type GlobalState } from '~/store' import { type GlobalState } from '~/store'
import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts' 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://gnosis-safe.readthedocs.io/en/latest/contracts/signatures.html#pre-validated-signatures
// https://github.com/gnosis/safe-contracts/blob/master/test/gnosisSafeTeamEdition.js#L26 // https://github.com/gnosis/safe-contracts/blob/master/test/gnosisSafeTeamEdition.js#L26
@ -46,9 +53,7 @@ const processTransaction = (
let txHash let txHash
if (shouldExecute) { if (shouldExecute) {
const showNotification = () => openSnackbar('Transaction has been submitted', 'success') const transaction = await getExecutionTransaction(
txHash = await executeTransaction(
showNotification,
safeInstance, safeInstance,
tx.recipient, tx.recipient,
tx.value, tx.value,
@ -58,19 +63,65 @@ const processTransaction = (
from, from,
sigs, sigs,
) )
openSnackbar('Transaction has been confirmed', 'success')
} else { await transaction
const showNotification = () => openSnackbar('Approval transaction has been submitted', 'success') .send({
txHash = await approveTransaction( from,
showNotification, })
.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, safeInstance,
tx.recipient, tx.recipient,
tx.value, tx.value,
tx.data, tx.data,
CALL, CALL,
nonce, nonce,
receipt.transactionHash,
from, from,
TX_TYPE_EXECUTION,
) )
return receipt.transactionHash
})
openSnackbar('Transaction has been confirmed', 'success')
} else {
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') openSnackbar('Approval transaction has been confirmed', 'success')
} }