Pass notifications to createTransaction/executeTransaction to allow custom messages WIP

This commit is contained in:
Mikhail Mikheev 2019-09-04 19:27:19 +04:00
parent 753e46a243
commit 590147682a
5 changed files with 67 additions and 49 deletions

View File

@ -20,11 +20,7 @@ export const SharedSnackbar = () => (
autoHideDuration={4000} autoHideDuration={4000}
onClose={closeSnackbar} onClose={closeSnackbar}
> >
<SnackbarContent <SnackbarContent onClose={closeSnackbar} message={message} variant={variant} />
onClose={closeSnackbar}
message={message}
variant={variant}
/>
</Snackbar> </Snackbar>
) )
}} }}
@ -60,11 +56,15 @@ type State = {
} }
export class SharedSnackbarProvider extends React.Component<Props, State> { export class SharedSnackbarProvider extends React.Component<Props, State> {
state = { constructor(props: Props) {
super(props)
this.state = {
isOpen: false, isOpen: false,
message: '', message: '',
variant: 'info', variant: 'info',
} }
}
openSnackbar = (message: string, variant: Variant) => { openSnackbar = (message: string, variant: Variant) => {
this.setState({ this.setState({

View File

@ -108,11 +108,9 @@ export const executeTransaction = async (
.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)
.encodeABI() .encodeABI()
const errMsg = await getErrorMessage(safeInstance.address, 0, executeDataUsedSignatures, sender) const errMsg = await getErrorMessage(safeInstance.address, 0, executeDataUsedSignatures, sender)
console.log(`Error executing the TX: ${error}`)
console.log(`Error executing the TX: ${errMsg}`) console.log(`Error executing the TX: ${errMsg}`)
/* eslint-enable */
return 0 throw error;
} }
} }

View File

@ -46,7 +46,9 @@ export const sendRemoveOwner = async (
) => { ) => {
const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress) const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress)
const safeOwners = await gnosisSafe.getOwners() 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 prevAddress = index === 0 ? SENTINEL_ADDRESS : safeOwners[index - 1]
const txData = gnosisSafe.contract.methods const txData = gnosisSafe.contract.methods
.removeOwner(prevAddress, ownerAddressToRemove, values.threshold) .removeOwner(prevAddress, ownerAddressToRemove, values.threshold)
@ -103,12 +105,11 @@ const RemoveOwner = ({
} }
return ( return (
<React.Fragment> <>
<SharedSnackbarConsumer> <SharedSnackbarConsumer>
{({ openSnackbar }) => { {({ openSnackbar }) => {
const onRemoveOwner = () => { const onRemoveOwner = () => {
onClose() onClose()
try {
sendRemoveOwner( sendRemoveOwner(
values, values,
safeAddress, safeAddress,
@ -119,10 +120,6 @@ const RemoveOwner = ({
createTransaction, createTransaction,
removeSafeOwner, removeSafeOwner,
) )
} catch (error) {
// eslint-disable-next-line
console.log('Error while removing an owner ' + error)
}
} }
return ( return (
@ -133,7 +130,7 @@ const RemoveOwner = ({
open={isOpen} open={isOpen}
paperClassName={classes.biggerModalWindow} paperClassName={classes.biggerModalWindow}
> >
<React.Fragment> <>
{activeScreen === 'checkOwner' && ( {activeScreen === 'checkOwner' && (
<CheckOwner <CheckOwner
onClose={onClose} onClose={onClose}
@ -165,12 +162,12 @@ const RemoveOwner = ({
onSubmit={onRemoveOwner} onSubmit={onRemoveOwner}
/> />
)} )}
</React.Fragment> </>
</Modal> </Modal>
) )
}} }}
</SharedSnackbarConsumer> </SharedSnackbarConsumer>
</React.Fragment> </>
) )
} }

View File

@ -56,7 +56,7 @@ const ReviewRemoveOwner = ({
} }
return ( return (
<React.Fragment> <>
<Row align="center" grow className={classes.heading}> <Row align="center" grow className={classes.heading}>
<Paragraph weight="bolder" className={classes.manage} noMargin> <Paragraph weight="bolder" className={classes.manage} noMargin>
Remove owner Remove owner
@ -92,7 +92,6 @@ const ReviewRemoveOwner = ({
{values.threshold} {values.threshold}
{' '} {' '}
out of out of
{' '}
{owners.size - 1} {owners.size - 1}
{' '} {' '}
owner(s) owner(s)
@ -110,7 +109,7 @@ const ReviewRemoveOwner = ({
</Row> </Row>
<Hairline /> <Hairline />
{owners.map( {owners.map(
owner => owner.address !== ownerAddress && ( (owner) => owner.address !== ownerAddress && (
<React.Fragment key={owner.address}> <React.Fragment key={owner.address}>
<Row className={classes.owner}> <Row className={classes.owner}>
<Col xs={1} align="center"> <Col xs={1} align="center">
@ -159,7 +158,11 @@ const ReviewRemoveOwner = ({
<Paragraph size="md" color="disabled" noMargin> <Paragraph size="md" color="disabled" noMargin>
{ownerAddress} {ownerAddress}
</Paragraph> </Paragraph>
<Link className={classes.open} to={getEtherScanLink('address', ownerAddress, network)} target="_blank"> <Link
className={classes.open}
to={getEtherScanLink('address', ownerAddress, network)}
target="_blank"
>
<OpenInNew style={openIconStyle} /> <OpenInNew style={openIconStyle} />
</Link> </Link>
</Block> </Block>
@ -187,7 +190,7 @@ const ReviewRemoveOwner = ({
Submit Submit
</Button> </Button>
</Row> </Row>
</React.Fragment> </>
) )
} }

View File

@ -7,6 +7,20 @@ 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 { 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 = ( const createTransaction = (
safeAddress: string, safeAddress: string,
to: string, to: string,
@ -14,6 +28,7 @@ const createTransaction = (
txData: string = EMPTY_DATA, txData: string = EMPTY_DATA,
openSnackbar: Function, openSnackbar: Function,
shouldExecute?: boolean, shouldExecute?: boolean,
notifications?: Notifications = DEFAULT_NOTIFICATIONS,
) => async (dispatch: ReduxDispatch<GlobalState>, getState: GetState<GlobalState>) => { ) => async (dispatch: ReduxDispatch<GlobalState>, getState: GetState<GlobalState>) => {
const state: GlobalState = getState() const state: GlobalState = getState()
@ -24,14 +39,19 @@ const createTransaction = (
const isExecution = threshold.toNumber() === 1 || shouldExecute const isExecution = threshold.toNumber() === 1 || shouldExecute
let txHash let txHash
try {
if (isExecution) { if (isExecution) {
openSnackbar('Transaction has been submitted', 'success') openSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, 'success')
txHash = await executeTransaction(safeInstance, to, valueInWei, txData, CALL, nonce, from) txHash = await executeTransaction(safeInstance, to, valueInWei, txData, CALL, nonce, from)
openSnackbar('Transaction has been confirmed', 'success') openSnackbar(notifications.AFTER_EXECUTION, 'success')
} else { } else {
openSnackbar('Approval transaction has been submitted', 'success') openSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, 'success')
txHash = await approveTransaction(safeInstance, to, valueInWei, txData, CALL, nonce, from) txHash = await approveTransaction(safeInstance, to, valueInWei, txData, CALL, nonce, from)
openSnackbar('Approval transaction has been confirmed', 'success') openSnackbar(notifications.CREATED_MORE_CONFIRMATIONS_NEEDED, 'success')
}
} catch (err) {
openSnackbar(notifications.ERROR, '')
console.error(`Error while creating transaction: ${err}`)
} }
dispatch(fetchTransactions(safeAddress)) dispatch(fetchTransactions(safeAddress))