#73 Update fetchTransaction action. Fetching info by safe address
This commit is contained in:
parent
f75d022147
commit
b43aa2feaa
|
@ -3,9 +3,9 @@ import type { Dispatch as ReduxDispatch } from 'redux'
|
|||
import { List, Map } from 'immutable'
|
||||
import { type GlobalState } from '~/store/index'
|
||||
import { makeOwner } from '~/routes/safe/store/model/owner'
|
||||
import { type SafeProps, type Safe, makeSafe } from '~/routes/safe/store/model/safe'
|
||||
import { type SafeProps, makeSafe } from '~/routes/safe/store/model/safe'
|
||||
import updateSafe from '~/routes/safe/store/actions/updateSafe'
|
||||
import { getOwners } from '~/utils/localStorage'
|
||||
import { getOwners, getSafeName } from '~/utils/localStorage'
|
||||
import { getGnosisSafeContract } from '~/logic/contracts/safeContracts'
|
||||
import { getWeb3 } from '~/logic/wallets/getWeb3'
|
||||
|
||||
|
@ -16,8 +16,7 @@ const buildOwnersFrom = (safeOwners: string[], storedOwners: Map<string, string>
|
|||
})
|
||||
)
|
||||
|
||||
export const buildSafe = async (storedSafe: Object) => {
|
||||
const safeAddress = storedSafe.address
|
||||
export const buildSafe = async (safeAddress: string, safeName: string) => {
|
||||
const web3 = getWeb3()
|
||||
const GnosisSafe = await getGnosisSafeContract(web3)
|
||||
const gnosisSafe = GnosisSafe.at(safeAddress)
|
||||
|
@ -27,7 +26,7 @@ export const buildSafe = async (storedSafe: Object) => {
|
|||
|
||||
const safe: SafeProps = {
|
||||
address: safeAddress,
|
||||
name: storedSafe.name,
|
||||
name: safeName,
|
||||
threshold,
|
||||
owners,
|
||||
}
|
||||
|
@ -35,9 +34,10 @@ export const buildSafe = async (storedSafe: Object) => {
|
|||
return makeSafe(safe)
|
||||
}
|
||||
|
||||
export default (safe: Safe) => async (dispatch: ReduxDispatch<GlobalState>) => {
|
||||
export default (safeAddress: string) => async (dispatch: ReduxDispatch<GlobalState>) => {
|
||||
try {
|
||||
const safeRecord = await buildSafe(safe.toJSON())
|
||||
const safeName = getSafeName(safeAddress) || 'LOADED SAFE'
|
||||
const safeRecord = await buildSafe(safeAddress, safeName)
|
||||
|
||||
return dispatch(updateSafe(safeRecord))
|
||||
} catch (err) {
|
||||
|
|
|
@ -12,8 +12,8 @@ export type State = Map<string, Safe>
|
|||
|
||||
export const buildSafe = (storedSafe: SafeProps) => {
|
||||
const owners = buildOwnersFrom(
|
||||
storedSafe.owners.map((owner: OwnerProps) => owner.address),
|
||||
storedSafe.owners.map((owner: OwnerProps) => owner.name),
|
||||
storedSafe.owners.map((owner: OwnerProps) => owner.address),
|
||||
)
|
||||
|
||||
const safe: SafeProps = {
|
||||
|
@ -53,9 +53,18 @@ export const safesInitialState = (): State => {
|
|||
|
||||
|
||||
export default handleActions({
|
||||
[UPDATE_SAFE]: (state: State, action: ActionType<typeof updateSafe>): State =>
|
||||
state.update(action.payload.get('address'), prevSafe =>
|
||||
(prevSafe.equals(action.payload) ? prevSafe : action.payload)),
|
||||
[UPDATE_SAFE]: (state: State, action: ActionType<typeof updateSafe>): State => {
|
||||
const safe = action.payload
|
||||
const safeAddress = safe.get('address')
|
||||
|
||||
const hasSafe = !!state.get(safeAddress)
|
||||
if (hasSafe) {
|
||||
return state.update(safeAddress, prevSafe =>
|
||||
(prevSafe.equals(safe) ? prevSafe : safe))
|
||||
}
|
||||
|
||||
return state.set(safeAddress, safe)
|
||||
},
|
||||
[ADD_SAFE]: (state: State, action: ActionType<typeof addSafe>): State => {
|
||||
const safe: Safe = makeSafe(action.payload)
|
||||
setOwners(safe.get('address'), safe.get('owners'))
|
||||
|
|
|
@ -23,6 +23,16 @@ export const load = (key: string) => {
|
|||
}
|
||||
}
|
||||
|
||||
export const getSafeName = (safeAddress: string) => {
|
||||
const safes = load(SAFES_KEY)
|
||||
if (!safes) {
|
||||
return undefined
|
||||
}
|
||||
const safe = safes[safeAddress]
|
||||
|
||||
return safe ? safe.name : undefined
|
||||
}
|
||||
|
||||
export const saveSafes = (safes: Object) => {
|
||||
try {
|
||||
const serializedState = JSON.stringify(safes)
|
||||
|
|
Loading…
Reference in New Issue