(Fix) (development) Missing collectibles and Safe version (#1375)

* use `updateSafe` instead of `addSafe`

* fix SAFE_UPDATE reducer

- treat every key individually

* allow to load owners on the first request

* Set UPDATE_SAFE to individually handling all props

* Handle List special case

* Add comment to list check

Co-authored-by: Daniel Sanchez <daniel.sanchez@gnosis.pm>
This commit is contained in:
Fernando 2020-09-23 09:39:25 -03:00 committed by GitHub
parent eec6e64c84
commit a764a23e66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 28 deletions

View File

@ -6,7 +6,6 @@ import { getLocalSafe, getSafeName } from 'src/logic/safe/utils'
import { enabledFeatures, safeNeedsUpdate } from 'src/logic/safe/utils/safeVersion' import { enabledFeatures, safeNeedsUpdate } from 'src/logic/safe/utils/safeVersion'
import { sameAddress } from 'src/logic/wallets/ethAddresses' import { sameAddress } from 'src/logic/wallets/ethAddresses'
import { getBalanceInEtherOf } from 'src/logic/wallets/getWeb3' import { getBalanceInEtherOf } from 'src/logic/wallets/getWeb3'
import addSafe from 'src/logic/safe/store/actions/addSafe'
import addSafeOwner from 'src/logic/safe/store/actions/addSafeOwner' import addSafeOwner from 'src/logic/safe/store/actions/addSafeOwner'
import removeSafeOwner from 'src/logic/safe/store/actions/removeSafeOwner' import removeSafeOwner from 'src/logic/safe/store/actions/removeSafeOwner'
import updateSafe from 'src/logic/safe/store/actions/updateSafe' import updateSafe from 'src/logic/safe/store/actions/updateSafe'
@ -115,7 +114,7 @@ export const checkAndUpdateSafe = (safeAdd: string) => async (dispatch: Dispatch
]) ])
// Converts from [ { address, ownerName} ] to address array // Converts from [ { address, ownerName} ] to address array
const localOwners = localSafe ? localSafe.owners.map((localOwner) => localOwner.address) : undefined const localOwners = localSafe ? localSafe.owners.map((localOwner) => localOwner.address) : []
dispatch( dispatch(
updateSafe({ updateSafe({
@ -127,30 +126,27 @@ export const checkAndUpdateSafe = (safeAdd: string) => async (dispatch: Dispatch
) )
// If the remote owners does not contain a local address, we remove that local owner // If the remote owners does not contain a local address, we remove that local owner
if (localOwners) { localOwners.forEach((localAddress) => {
localOwners.forEach((localAddress) => { const remoteOwnerIndex = remoteOwners.findIndex((remoteAddress) => sameAddress(remoteAddress, localAddress))
const remoteOwnerIndex = remoteOwners.findIndex((remoteAddress) => sameAddress(remoteAddress, localAddress)) if (remoteOwnerIndex === -1) {
if (remoteOwnerIndex === -1) { dispatch(removeSafeOwner({ safeAddress, ownerAddress: localAddress }))
dispatch(removeSafeOwner({ safeAddress, ownerAddress: localAddress })) }
} })
})
// If the remote has an owner that we don't have locally, we add it // If the remote has an owner that we don't have locally, we add it
remoteOwners.forEach((remoteAddress) => { remoteOwners.forEach((remoteAddress) => {
const localOwnerIndex = localOwners.findIndex((localAddress) => sameAddress(remoteAddress, localAddress)) const localOwnerIndex = localOwners.findIndex((localAddress) => sameAddress(remoteAddress, localAddress))
if (localOwnerIndex === -1) { if (localOwnerIndex === -1) {
dispatch( dispatch(
addSafeOwner({ addSafeOwner({
safeAddress, safeAddress,
ownerAddress: remoteAddress, ownerAddress: remoteAddress,
ownerName: 'UNKNOWN', ownerName: 'UNKNOWN',
}), }),
) )
} }
}) })
}
} }
export default (safeAdd: string) => async ( export default (safeAdd: string) => async (
dispatch: Dispatch<any>, dispatch: Dispatch<any>,
getState: () => AppReduxState, getState: () => AppReduxState,
@ -161,9 +157,12 @@ export default (safeAdd: string) => async (
const latestMasterContractVersion = latestMasterContractVersionSelector(getState()) const latestMasterContractVersion = latestMasterContractVersionSelector(getState())
const safeProps = await buildSafe(safeAddress, safeName, latestMasterContractVersion) const safeProps = await buildSafe(safeAddress, safeName, latestMasterContractVersion)
dispatch(addSafe(safeProps)) // `updateSafe`, as `loadSafesFromStorage` will populate the store previous to this call
// and `addSafe` will only add a newly non-existent safe
// For the case where the safe does not exist in the localStorage,
// `updateSafe` uses a default `notSetValue` to add the Safe to the store
dispatch(updateSafe(safeProps))
} catch (err) { } catch (err) {
// eslint-disable-next-line
console.error('Error while updating Safe information: ', err) console.error('Error while updating Safe information: ', err)
return Promise.resolve() return Promise.resolve()

View File

@ -1,4 +1,4 @@
import { Map, Set } from 'immutable' import { Map, Set, List } from 'immutable'
import { handleActions } from 'redux-actions' import { handleActions } from 'redux-actions'
import { ACTIVATE_TOKEN_FOR_ALL_SAFES } from 'src/logic/safe/store/actions/activateTokenForAllSafes' import { ACTIVATE_TOKEN_FOR_ALL_SAFES } from 'src/logic/safe/store/actions/activateTokenForAllSafes'
@ -51,7 +51,31 @@ export default handleActions(
return state.updateIn( return state.updateIn(
['safes', safeAddress], ['safes', safeAddress],
makeSafe({ name: 'LOADED SAFE', address: safeAddress }), makeSafe({ name: 'LOADED SAFE', address: safeAddress }),
(prevSafe) => prevSafe.merge(safe), (prevSafe) => {
return prevSafe.withMutations((record) => {
// Every property is updated individually to overcome the issue with nested data being overwritten
const safeProperties = Object.keys(safe)
// We check each safe property sent in action.payload
safeProperties.forEach((key) => {
if (safe[key] && typeof safe[key] === 'object') {
if (safe[key].length) {
// If type is array we update the array
record.update(key, () => safe[key])
} else if (safe[key].size) {
// If type is Immutable List we replace current List
// If type is Object we do a merge
List.isList(safe[key])
? record.update(key, (current) => current.set(safe[key]))
: record.update(key, (current) => current.merge(safe[key]))
}
} else {
// By default we overwrite the value. This is for strings, numbers and unset values
record.set(key, safe[key])
}
})
})
},
) )
}, },
[ACTIVATE_TOKEN_FOR_ALL_SAFES]: (state: SafeReducerMap, action) => { [ACTIVATE_TOKEN_FOR_ALL_SAFES]: (state: SafeReducerMap, action) => {