Adds threshold update on checkAndUpdateSafe (#320)

This commit is contained in:
Agustin Pane 2019-12-11 07:06:26 -03:00 committed by Mikhail Mikheev
parent 747bcef17a
commit 56a6e16158
4 changed files with 26 additions and 3 deletions

View File

@ -1,5 +1,5 @@
// @flow
import fetchSafe, { checkAndUpdateSafeOwners } from '~/routes/safe/store/actions/fetchSafe'
import fetchSafe, { checkAndUpdateSafe } 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'
@ -31,5 +31,5 @@ export default {
activateTokensByBalance,
updateSafe,
fetchEtherBalance,
checkAndUpdateSafeOwners,
checkAndUpdateSafeOwners: checkAndUpdateSafe,
}

View File

@ -11,6 +11,7 @@ import { getBalanceInEtherOf } from '~/logic/wallets/getWeb3'
import { loadFromStorage } from '~/utils/storage'
import removeSafeOwner from '~/routes/safe/store/actions/removeSafeOwner'
import addSafeOwner from '~/routes/safe/store/actions/addSafeOwner'
import updateSafeThreshold from '~/routes/safe/store/actions/updateSafeThreshold'
const buildOwnersFrom = (
safeOwners: string[],
@ -43,13 +44,19 @@ const getLocalSafe = async (safeAddress: string) => {
return storedSafes[safeAddress]
}
export const checkAndUpdateSafeOwners = (safeAddress: string) => async (dispatch: ReduxDispatch<GlobalState>) => {
export const checkAndUpdateSafe = (safeAddress: string) => async (dispatch: ReduxDispatch<GlobalState>) => {
// 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()
// Converts from [ { address, ownerName} ] to address array
const localOwners = localSafe.owners.map((localOwner) => localOwner.address)
// Updates threshold values
const threshold = await gnosisSafe.getThreshold()
localSafe.threshold = threshold.toNumber()
dispatch(updateSafeThreshold({ safeAddress, threshold: threshold.toNumber() }))
// If the remote owners does not contain a local address, we remove that local owner
localOwners.forEach((localAddress) => {
if (!remoteOwners.includes(localAddress)) {

View File

@ -0,0 +1,8 @@
// @flow
import { createAction } from 'redux-actions'
export const UPDATE_SAFE_THRESHOLD = 'UPDATE_SAFE_THRESHOLD'
const updateSafeThreshold = createAction<string, *>(UPDATE_SAFE_THRESHOLD)
export default updateSafeThreshold

View File

@ -12,6 +12,7 @@ import { REMOVE_SAFE_OWNER } from '~/routes/safe/store/actions/removeSafeOwner'
import { REPLACE_SAFE_OWNER } from '~/routes/safe/store/actions/replaceSafeOwner'
import { EDIT_SAFE_OWNER } from '~/routes/safe/store/actions/editSafeOwner'
import { SET_DEFAULT_SAFE } from '~/routes/safe/store/actions/setDefaultSafe'
import { UPDATE_SAFE_THRESHOLD } from '~/routes/safe/store/actions/updateSafeThreshold'
export const SAFE_REDUCER_ID = 'safes'
@ -115,6 +116,13 @@ export default handleActions<SafeReducerState, *>(
return prevSafe.merge({ owners: updatedOwners })
})
},
[UPDATE_SAFE_THRESHOLD]: (state: SafeReducerState, action: ActionType<Function>): SafeReducerState => {
const { safeAddress, threshold } = action.payload
return state.updateIn(['safes', safeAddress], (prevSafe) => {
return prevSafe.set('threshold', threshold)
})
},
[SET_DEFAULT_SAFE]: (state: SafeReducerState, action: ActionType<Function>): SafeReducerState => state.set('defaultSafe', action.payload),
},
Map({