From 590147682a9011ea76a9678154694ce62bf40566 Mon Sep 17 00:00:00 2001 From: Mikhail Mikheev Date: Wed, 4 Sep 2019 19:27:19 +0400 Subject: [PATCH] Pass notifications to createTransaction/executeTransaction to allow custom messages WIP --- src/components/SharedSnackBar/index.jsx | 18 ++++----- src/logic/safe/transactions/send.js | 6 +-- .../ManageOwners/RemoveOwnerModal/index.jsx | 37 +++++++++---------- .../RemoveOwnerModal/screens/Review/index.jsx | 19 ++++++---- .../safe/store/actions/createTransaction.js | 36 ++++++++++++++---- 5 files changed, 67 insertions(+), 49 deletions(-) diff --git a/src/components/SharedSnackBar/index.jsx b/src/components/SharedSnackBar/index.jsx index c208afdb..c22c9213 100644 --- a/src/components/SharedSnackBar/index.jsx +++ b/src/components/SharedSnackBar/index.jsx @@ -20,11 +20,7 @@ export const SharedSnackbar = () => ( autoHideDuration={4000} onClose={closeSnackbar} > - + ) }} @@ -60,10 +56,14 @@ type State = { } export class SharedSnackbarProvider extends React.Component { - state = { - isOpen: false, - message: '', - variant: 'info', + constructor(props: Props) { + super(props) + + this.state = { + isOpen: false, + message: '', + variant: 'info', + } } openSnackbar = (message: string, variant: Variant) => { diff --git a/src/logic/safe/transactions/send.js b/src/logic/safe/transactions/send.js index abe2920d..6bba674d 100644 --- a/src/logic/safe/transactions/send.js +++ b/src/logic/safe/transactions/send.js @@ -108,11 +108,9 @@ export const executeTransaction = async ( .execTransaction(to, valueInWei, data, operation, 0, 0, 0, ZERO_ADDRESS, ZERO_ADDRESS, sigs) .encodeABI() const errMsg = await getErrorMessage(safeInstance.address, 0, executeDataUsedSignatures, sender) - - console.log(`Error executing the TX: ${error}`) console.log(`Error executing the TX: ${errMsg}`) - /* eslint-enable */ - return 0 + + throw error; } } diff --git a/src/routes/safe/components/Settings/ManageOwners/RemoveOwnerModal/index.jsx b/src/routes/safe/components/Settings/ManageOwners/RemoveOwnerModal/index.jsx index 244c6b9d..c245965b 100644 --- a/src/routes/safe/components/Settings/ManageOwners/RemoveOwnerModal/index.jsx +++ b/src/routes/safe/components/Settings/ManageOwners/RemoveOwnerModal/index.jsx @@ -46,7 +46,9 @@ export const sendRemoveOwner = async ( ) => { const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress) const safeOwners = await gnosisSafe.getOwners() - const index = safeOwners.findIndex(ownerAddress => ownerAddress.toLowerCase() === ownerAddressToRemove.toLowerCase()) + const index = safeOwners.findIndex( + (ownerAddress) => ownerAddress.toLowerCase() === ownerAddressToRemove.toLowerCase(), + ) const prevAddress = index === 0 ? SENTINEL_ADDRESS : safeOwners[index - 1] const txData = gnosisSafe.contract.methods .removeOwner(prevAddress, ownerAddressToRemove, values.threshold) @@ -103,26 +105,21 @@ const RemoveOwner = ({ } return ( - + <> {({ openSnackbar }) => { const onRemoveOwner = () => { onClose() - try { - sendRemoveOwner( - values, - safeAddress, - ownerAddress, - ownerName, - owners, - openSnackbar, - createTransaction, - removeSafeOwner, - ) - } catch (error) { - // eslint-disable-next-line - console.log('Error while removing an owner ' + error) - } + sendRemoveOwner( + values, + safeAddress, + ownerAddress, + ownerName, + owners, + openSnackbar, + createTransaction, + removeSafeOwner, + ) } return ( @@ -133,7 +130,7 @@ const RemoveOwner = ({ open={isOpen} paperClassName={classes.biggerModalWindow} > - + <> {activeScreen === 'checkOwner' && ( )} - + ) }} - + ) } diff --git a/src/routes/safe/components/Settings/ManageOwners/RemoveOwnerModal/screens/Review/index.jsx b/src/routes/safe/components/Settings/ManageOwners/RemoveOwnerModal/screens/Review/index.jsx index e5e7921e..37ea76fe 100644 --- a/src/routes/safe/components/Settings/ManageOwners/RemoveOwnerModal/screens/Review/index.jsx +++ b/src/routes/safe/components/Settings/ManageOwners/RemoveOwnerModal/screens/Review/index.jsx @@ -56,7 +56,7 @@ const ReviewRemoveOwner = ({ } return ( - + <> Remove owner @@ -91,11 +91,10 @@ const ReviewRemoveOwner = ({ {values.threshold} {' '} - out of - {' '} +out of {owners.size - 1} {' '} - owner(s) +owner(s) @@ -105,12 +104,12 @@ const ReviewRemoveOwner = ({ {owners.size - 1} {' '} - Safe owner(s) +Safe owner(s) {owners.map( - owner => owner.address !== ownerAddress && ( + (owner) => owner.address !== ownerAddress && ( @@ -159,7 +158,11 @@ const ReviewRemoveOwner = ({ {ownerAddress} - + @@ -187,7 +190,7 @@ const ReviewRemoveOwner = ({ Submit - + ) } diff --git a/src/routes/safe/store/actions/createTransaction.js b/src/routes/safe/store/actions/createTransaction.js index 164427ee..03982ea7 100644 --- a/src/routes/safe/store/actions/createTransaction.js +++ b/src/routes/safe/store/actions/createTransaction.js @@ -7,6 +7,20 @@ import { type GlobalState } from '~/store' import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts' import { approveTransaction, executeTransaction, CALL } from '~/logic/safe/transactions' +export type Notifications = { + BEFORE_EXECUTION_OR_CREATION: string, + AFTER_EXECUTION: string, + CREATED_MORE_CONFIRMATIONS_NEEDED: string, + ERROR: string, +} + +const DEFAULT_NOTIFICATIONS: Notifications = { + BEFORE_EXECUTION_OR_CREATION: 'Transaction in progress', + AFTER_EXECUTION: 'Transaction successfully executed', + CREATED_MORE_CONFIRMATIONS_NEEDED: 'Transaction in progress: More confirmations required to execute', + ERROR: 'Transaction failed', +} + const createTransaction = ( safeAddress: string, to: string, @@ -14,6 +28,7 @@ const createTransaction = ( txData: string = EMPTY_DATA, openSnackbar: Function, shouldExecute?: boolean, + notifications?: Notifications = DEFAULT_NOTIFICATIONS, ) => async (dispatch: ReduxDispatch, getState: GetState) => { const state: GlobalState = getState() @@ -24,14 +39,19 @@ const createTransaction = ( const isExecution = threshold.toNumber() === 1 || shouldExecute let txHash - if (isExecution) { - openSnackbar('Transaction has been submitted', 'success') - txHash = await executeTransaction(safeInstance, to, valueInWei, txData, CALL, nonce, from) - openSnackbar('Transaction has been confirmed', 'success') - } else { - openSnackbar('Approval transaction has been submitted', 'success') - txHash = await approveTransaction(safeInstance, to, valueInWei, txData, CALL, nonce, from) - openSnackbar('Approval transaction has been confirmed', 'success') + try { + if (isExecution) { + openSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, 'success') + txHash = await executeTransaction(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) + openSnackbar(notifications.CREATED_MORE_CONFIRMATIONS_NEEDED, 'success') + } + } catch (err) { + openSnackbar(notifications.ERROR, '') + console.error(`Error while creating transaction: ${err}`) } dispatch(fetchTransactions(safeAddress))