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,10 +56,14 @@ type State = {
} }
export class SharedSnackbarProvider extends React.Component<Props, State> { export class SharedSnackbarProvider extends React.Component<Props, State> {
state = { constructor(props: Props) {
isOpen: false, super(props)
message: '',
variant: 'info', this.state = {
isOpen: false,
message: '',
variant: 'info',
}
} }
openSnackbar = (message: string, variant: Variant) => { openSnackbar = (message: string, variant: Variant) => {

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,26 +105,21 @@ const RemoveOwner = ({
} }
return ( return (
<React.Fragment> <>
<SharedSnackbarConsumer> <SharedSnackbarConsumer>
{({ openSnackbar }) => { {({ openSnackbar }) => {
const onRemoveOwner = () => { const onRemoveOwner = () => {
onClose() onClose()
try { sendRemoveOwner(
sendRemoveOwner( values,
values, safeAddress,
safeAddress, ownerAddress,
ownerAddress, ownerName,
ownerName, owners,
owners, openSnackbar,
openSnackbar, 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
@ -91,11 +91,10 @@ const ReviewRemoveOwner = ({
<Paragraph size="lg" color="primary" noMargin weight="bolder" className={classes.name}> <Paragraph size="lg" color="primary" noMargin weight="bolder" className={classes.name}>
{values.threshold} {values.threshold}
{' '} {' '}
out of out of
{' '}
{owners.size - 1} {owners.size - 1}
{' '} {' '}
owner(s) owner(s)
</Paragraph> </Paragraph>
</Block> </Block>
</Block> </Block>
@ -105,12 +104,12 @@ const ReviewRemoveOwner = ({
<Paragraph size="lg" color="primary" noMargin> <Paragraph size="lg" color="primary" noMargin>
{owners.size - 1} {owners.size - 1}
{' '} {' '}
Safe owner(s) Safe owner(s)
</Paragraph> </Paragraph>
</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
if (isExecution) { try {
openSnackbar('Transaction has been submitted', 'success') if (isExecution) {
txHash = await executeTransaction(safeInstance, to, valueInWei, txData, CALL, nonce, from) openSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, 'success')
openSnackbar('Transaction has been confirmed', 'success') txHash = await executeTransaction(safeInstance, to, valueInWei, txData, CALL, nonce, from)
} else { openSnackbar(notifications.AFTER_EXECUTION, 'success')
openSnackbar('Approval transaction has been submitted', 'success') } else {
txHash = await approveTransaction(safeInstance, to, valueInWei, txData, CALL, nonce, from) openSnackbar(notifications.BEFORE_EXECUTION_OR_CREATION, 'success')
openSnackbar('Approval transaction has been confirmed', '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)) dispatch(fetchTransactions(safeAddress))