WA-234 fetch safe info regularly in safe view

This commit is contained in:
apanizo 2018-06-07 22:25:32 +02:00
parent aec0b50200
commit 2da7eab781
6 changed files with 61 additions and 42 deletions

View File

@ -1,13 +1,13 @@
// @flow // @flow
import fetchSafe from '~/routes/safe/store/actions/fetchSafe'
import fetchBalance from '~/routes/safe/store/actions/fetchBalance' import fetchBalance from '~/routes/safe/store/actions/fetchBalance'
import fetchDailyLimit from '~/routes/safe/store/actions/fetchDailyLimit'
export type Actions = { export type Actions = {
fetchSafe: typeof fetchSafe,
fetchBalance: typeof fetchBalance, fetchBalance: typeof fetchBalance,
fetchDailyLimit: typeof fetchDailyLimit,
} }
export default { export default {
fetchSafe,
fetchBalance, fetchBalance,
fetchDailyLimit,
} }

View File

@ -14,17 +14,12 @@ type Props = Actions & SelectorProps & {
class SafeView extends React.PureComponent<Props> { class SafeView extends React.PureComponent<Props> {
componentDidMount() { componentDidMount() {
this.intervalId = setInterval(() => { this.intervalId = setInterval(() => {
const { safe, fetchBalance } = this.props const { safe, fetchSafe, fetchBalance } = this.props
if (!safe) { return } if (!safe) { return }
const safeAddress: string = safe.get('address') const safeAddress: string = safe.get('address')
fetchBalance(safeAddress) fetchBalance(safeAddress)
fetchSafe(safe)
}, 1500) }, 1500)
const { fetchDailyLimit, safe } = this.props
if (safe) {
fetchDailyLimit(safe.get('address'))
}
} }
componentWillUnmount() { componentWillUnmount() {

View File

@ -0,0 +1,42 @@
// @flow
import type { Dispatch as ReduxDispatch } from 'redux'
import { List } 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 { makeDailyLimit } from '~/routes/safe/store/model/dailyLimit'
import { getDailyLimitFrom } from '~/routes/safe/component/Withdrawn/withdrawn'
import { getGnosisSafeInstanceAt } from '~/wallets/safeContracts'
import updateSafe from '~/routes/safe/store/actions/updateSafe'
const buildOwnersFrom = (safeOwners: string[], storedOwners: Object[]) => (
safeOwners.map((ownerAddress: string) => {
const foundOwner = storedOwners.find(owner => owner.address === ownerAddress)
return makeOwner(foundOwner || { name: 'UNKNOWN', address: ownerAddress })
})
)
export const buildSafe = async (storedSafe: Object) => {
const safeAddress = storedSafe.address
const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress)
const dailyLimit = makeDailyLimit(await getDailyLimitFrom(safeAddress, 0))
const threshold = Number(await gnosisSafe.getThreshold())
const owners = List(buildOwnersFrom(await gnosisSafe.getOwners(), storedSafe.owners))
const safe: SafeProps = {
address: safeAddress,
dailyLimit,
name: storedSafe.name,
threshold,
owners,
}
return makeSafe(safe)
}
export default (safe: Safe) => async (dispatch: ReduxDispatch<GlobalState>) => {
const safeRecord = await buildSafe(safe.toJSON())
return dispatch(updateSafe(safeRecord))
}

View File

@ -1,40 +1,11 @@
// @flow // @flow
import type { Dispatch as ReduxDispatch } from 'redux' import type { Dispatch as ReduxDispatch } from 'redux'
import { List, Map } from 'immutable' import { Map } from 'immutable'
import { type GlobalState } from '~/store/index' 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 { makeDailyLimit } from '~/routes/safe/store/model/dailyLimit'
import { getDailyLimitFrom } from '~/routes/safe/component/Withdrawn/withdrawn'
import { getGnosisSafeInstanceAt } from '~/wallets/safeContracts'
import { load, SAFES_KEY } from '~/utils/localStorage' import { load, SAFES_KEY } from '~/utils/localStorage'
import updateSafes from '~/routes/safe/store/actions/updateSafes' import updateSafes from '~/routes/safe/store/actions/updateSafes'
import { buildSafe } from '~/routes/safe/store/actions/fetchSafe'
const buildOwnersFrom = (safeOwners: string[], storedOwners: Object[]) => ( import { type Safe } from '~/routes/safe/store/model/safe'
safeOwners.map((ownerAddress: string) => {
const foundOwner = storedOwners.find(owner => owner.address === ownerAddress)
return makeOwner(foundOwner || { name: 'UNKNOWN', address: ownerAddress })
})
)
const buildSafe = async (storedSafe: Object) => {
const safeAddress = storedSafe.address
const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress)
const dailyLimit = makeDailyLimit(await getDailyLimitFrom(safeAddress, 0))
const threshold = Number(await gnosisSafe.getThreshold())
const owners = List(buildOwnersFrom(await gnosisSafe.getOwners(), storedSafe.owners))
const safe: SafeProps = {
address: safeAddress,
dailyLimit,
name: storedSafe.name,
threshold,
owners,
}
return makeSafe(safe)
}
const buildSafesFrom = async (loadedSafes: Object): Promise<Map<string, Safe>> => { const buildSafesFrom = async (loadedSafes: Object): Promise<Map<string, Safe>> => {
const safes = Map() const safes = Map()

View File

@ -0,0 +1,8 @@
// @flow
import { createAction } from 'redux-actions'
export const UPDATE_SAFE = 'UPDATE_SAFE'
const updateSafe = createAction(UPDATE_SAFE)
export default updateSafe

View File

@ -8,6 +8,7 @@ import { saveSafes } from '~/utils/localStorage'
import { makeDailyLimit } from '~/routes/safe/store/model/dailyLimit' import { makeDailyLimit } from '~/routes/safe/store/model/dailyLimit'
import updateThreshold, { UPDATE_THRESHOLD } from '~/routes/safe/store/actions/updateThreshold' import updateThreshold, { UPDATE_THRESHOLD } from '~/routes/safe/store/actions/updateThreshold'
import updateSafes, { UPDATE_SAFES } from '~/routes/safe/store/actions/updateSafes' import updateSafes, { UPDATE_SAFES } from '~/routes/safe/store/actions/updateSafes'
import updateSafe, { UPDATE_SAFE } from '~/routes/safe/store/actions/updateSafe'
export const SAFE_REDUCER_ID = 'safes' export const SAFE_REDUCER_ID = 'safes'
@ -25,6 +26,8 @@ action: AddSafeType
*/ */
export default handleActions({ export default handleActions({
[UPDATE_SAFE]: (state: State, action: ActionType<typeof updateSafe>): State =>
state.set(action.payload.get('address'), action.payload),
[UPDATE_SAFES]: (state: State, action: ActionType<typeof updateSafes>): State => [UPDATE_SAFES]: (state: State, action: ActionType<typeof updateSafes>): State =>
action.payload, action.payload,
[ADD_SAFE]: (state: State, action: ActionType<typeof addSafe>): State => { [ADD_SAFE]: (state: State, action: ActionType<typeof addSafe>): State => {