From 54cb38b864ad0ce3463f6b7f99649a561d440c16 Mon Sep 17 00:00:00 2001 From: apane Date: Thu, 14 Nov 2019 12:19:48 -0300 Subject: [PATCH] Adds update owner's list to the polling function --- src/routes/safe/components/Layout.jsx | 1 + .../ManageOwners/RemoveOwnerModal/index.jsx | 7 +++- .../Settings/ManageOwners/index.jsx | 4 +++ src/routes/safe/components/Settings/index.jsx | 4 +++ src/routes/safe/container/actions.js | 3 +- src/routes/safe/container/index.jsx | 4 +-- src/routes/safe/store/actions/fetchSafe.js | 33 ++++++++++++++++++- 7 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/routes/safe/components/Layout.jsx b/src/routes/safe/components/Layout.jsx index 9eaa3d2d..9063c619 100644 --- a/src/routes/safe/components/Layout.jsx +++ b/src/routes/safe/components/Layout.jsx @@ -196,6 +196,7 @@ const Layout = (props: Props) => { network={network} userAddress={userAddress} createTransaction={createTransaction} + safe={safe} /> )} /> diff --git a/src/routes/safe/components/Settings/ManageOwners/RemoveOwnerModal/index.jsx b/src/routes/safe/components/Settings/ManageOwners/RemoveOwnerModal/index.jsx index 027e1ba2..0358f6bf 100644 --- a/src/routes/safe/components/Settings/ManageOwners/RemoveOwnerModal/index.jsx +++ b/src/routes/safe/components/Settings/ManageOwners/RemoveOwnerModal/index.jsx @@ -10,6 +10,7 @@ import { TX_NOTIFICATION_TYPES } from '~/logic/safe/transactions' import CheckOwner from './screens/CheckOwner' import ThresholdForm from './screens/ThresholdForm' import ReviewRemoveOwner from './screens/Review' +import type { Safe } from '~/routes/safe/store/models/safe' const styles = () => ({ biggerModalWindow: { @@ -34,6 +35,7 @@ type Props = { removeSafeOwner: Function, enqueueSnackbar: Function, closeSnackbar: Function, + safe: Safe } type ActiveScreen = 'checkOwner' | 'selectThreshold' | 'reviewRemoveOwner' @@ -48,6 +50,7 @@ export const sendRemoveOwner = async ( closeSnackbar: Function, createTransaction: Function, removeSafeOwner: Function, + safe: Safe, ) => { const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress) const safeOwners = await gnosisSafe.getOwners() @@ -69,7 +72,7 @@ export const sendRemoveOwner = async ( closeSnackbar, ) - if (txHash) { + if (txHash && safe.threshold === 1) { removeSafeOwner({ safeAddress, ownerAddress: ownerAddressToRemove }) } } @@ -88,6 +91,7 @@ const RemoveOwner = ({ removeSafeOwner, enqueueSnackbar, closeSnackbar, + safe, }: Props) => { const [activeScreen, setActiveScreen] = useState('checkOwner') const [values, setValues] = useState({}) @@ -130,6 +134,7 @@ const RemoveOwner = ({ closeSnackbar, createTransaction, removeSafeOwner, + safe, ) } diff --git a/src/routes/safe/components/Settings/ManageOwners/index.jsx b/src/routes/safe/components/Settings/ManageOwners/index.jsx index 436788f0..df40790d 100644 --- a/src/routes/safe/components/Settings/ManageOwners/index.jsx +++ b/src/routes/safe/components/Settings/ManageOwners/index.jsx @@ -32,6 +32,7 @@ import ReplaceOwnerIcon from './assets/icons/replace-owner.svg' import RenameOwnerIcon from './assets/icons/rename-owner.svg' import RemoveOwnerIcon from '../assets/icons/bin.svg' import Paragraph from '~/components/layout/Paragraph/index' +import type { Safe } from '~/routes/safe/store/models/safe' export const RENAME_OWNER_BTN_TEST_ID = 'rename-owner-btn' export const REMOVE_OWNER_BTN_TEST_ID = 'remove-owner-btn' @@ -53,6 +54,7 @@ type Props = { replaceSafeOwner: Function, editSafeOwner: Function, granted: boolean, + safe: Safe } type State = { @@ -111,6 +113,7 @@ class ManageOwners extends React.Component { replaceSafeOwner, editSafeOwner, granted, + safe, } = this.props const { showAddOwner, @@ -235,6 +238,7 @@ class ManageOwners extends React.Component { userAddress={userAddress} createTransaction={createTransaction} removeSafeOwner={removeSafeOwner} + safe={safe} /> { removeSafeOwner, replaceSafeOwner, editSafeOwner, + safe, } = this.props return ( @@ -152,6 +155,7 @@ class Settings extends React.Component { replaceSafeOwner={replaceSafeOwner} editSafeOwner={editSafeOwner} granted={granted} + safe={safe} /> )} {menuOptionIndex === 3 && ( diff --git a/src/routes/safe/container/actions.js b/src/routes/safe/container/actions.js index 2dc0c895..03c49de9 100644 --- a/src/routes/safe/container/actions.js +++ b/src/routes/safe/container/actions.js @@ -1,5 +1,5 @@ // @flow -import fetchSafe from '~/routes/safe/store/actions/fetchSafe' +import fetchSafe, { checkAndUpdateSafeOwners } from '~/routes/safe/store/actions/fetchSafe' import fetchTokenBalances from '~/routes/safe/store/actions/fetchTokenBalances' import fetchEtherBalance from '~/routes/safe/store/actions/fetchEtherBalance' import createTransaction from '~/routes/safe/store/actions/createTransaction' @@ -28,4 +28,5 @@ export default { fetchTransactions, updateSafe, fetchEtherBalance, + checkAndUpdateSafeOwners, } diff --git a/src/routes/safe/container/index.jsx b/src/routes/safe/container/index.jsx index fa6b170e..7104ad3c 100644 --- a/src/routes/safe/container/index.jsx +++ b/src/routes/safe/container/index.jsx @@ -89,9 +89,9 @@ class SafeView extends React.Component { checkForUpdates() { const { - safeUrl, activeTokens, fetchTokenBalances, fetchEtherBalance, + safeUrl, activeTokens, fetchTokenBalances, fetchEtherBalance, checkAndUpdateSafeOwners, } = this.props - + checkAndUpdateSafeOwners(safeUrl) fetchTokenBalances(safeUrl, activeTokens) fetchEtherBalance(safeUrl) } diff --git a/src/routes/safe/store/actions/fetchSafe.js b/src/routes/safe/store/actions/fetchSafe.js index 84c1eb63..c4f0eaf6 100644 --- a/src/routes/safe/store/actions/fetchSafe.js +++ b/src/routes/safe/store/actions/fetchSafe.js @@ -5,9 +5,11 @@ import { type GlobalState } from '~/store/index' import { makeOwner } from '~/routes/safe/store/models/owner' import type { SafeProps } from '~/routes/safe/store/models/safe' import addSafe from '~/routes/safe/store/actions/addSafe' -import { getOwners, getSafeName } from '~/logic/safe/utils' +import { getOwners, getSafeName, SAFES_KEY } from '~/logic/safe/utils' import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts' import { getBalanceInEtherOf } from '~/logic/wallets/getWeb3' +import { loadFromStorage } from '~/utils/storage' +import updateSafe from '~/routes/safe/store/actions/updateSafe' const buildOwnersFrom = ( safeOwners: string[], @@ -35,6 +37,35 @@ export const buildSafe = async (safeAddress: string, safeName: string) => { return safe } +const getLocalSafe = async (safeAddress: string) => { + const storedSafes = (await loadFromStorage(SAFES_KEY)) || {} + return storedSafes[safeAddress] +} + +export const checkAndUpdateSafeOwners = (safeAddress: string) => async ( + dispatch: ReduxDispatch, +) => { + // Check if the owner's safe did change and update them + const [gnosisSafe, localSafe] = await Promise.all([getGnosisSafeInstanceAt(safeAddress), getLocalSafe(safeAddress)]) + const remoteOwners = await gnosisSafe.getOwners() + const localOwners = localSafe.owners.map((localOwner) => localOwner.address) + + if (remoteOwners.length !== localOwners.length) { + dispatch(updateSafe({ address: safeAddress, owners: remoteOwners })) + return + } + + // eslint-disable-next-line no-restricted-syntax + for (const remoteIterator of remoteOwners) { + if (!localOwners.includes(remoteIterator)) { + dispatch(updateSafe({ address: safeAddress, owners: remoteOwners })) + + return + } + } +} + +// eslint-disable-next-line consistent-return export default (safeAddress: string) => async (dispatch: ReduxDispatch) => { try { const safeName = (await getSafeName(safeAddress)) || 'LOADED SAFE'