From 18508c3608643c957f18f77c0c0e4d5e3f454f00 Mon Sep 17 00:00:00 2001 From: Agustin Pane Date: Mon, 30 Nov 2020 16:02:31 -0300 Subject: [PATCH 1/6] Makes getGasEstimationTxResponse exportable --- src/logic/safe/transactions/gas.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logic/safe/transactions/gas.ts b/src/logic/safe/transactions/gas.ts index 92982f12c9..1af94841d1 100644 --- a/src/logic/safe/transactions/gas.ts +++ b/src/logic/safe/transactions/gas.ts @@ -130,7 +130,7 @@ export const getDataFromNodeErrorMessage = (errorMessage: string): string | unde } } -const getGasEstimationTxResponse = async (txConfig: { +export const getGasEstimationTxResponse = async (txConfig: { to: string from: string data: string From 175ed4e5ead8d864d5d8da99afd1c303bdd60082 Mon Sep 17 00:00:00 2001 From: Agustin Pane Date: Thu, 10 Dec 2020 11:11:55 -0300 Subject: [PATCH 2/6] Removes the race condition between useLoadSafe and useSafeScheduledUpdates --- src/components/App/index.tsx | 4 ++-- src/logic/safe/hooks/useLoadSafe.tsx | 13 +++++++++---- src/logic/safe/hooks/useSafeScheduledUpdates.tsx | 4 ++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/components/App/index.tsx b/src/components/App/index.tsx index f0fa31f408..8b50ae681f 100644 --- a/src/components/App/index.tsx +++ b/src/components/App/index.tsx @@ -80,8 +80,8 @@ const App: React.FC = ({ children }) => { const granted = useSelector(grantedSelector) const sidebarItems = useSidebarItems() - useLoadSafe(safeAddress) - useSafeScheduledUpdates(safeAddress) + const safeLoaded = useLoadSafe(safeAddress) + useSafeScheduledUpdates(safeLoaded, safeAddress) const sendFunds = safeActionsState.sendFunds const formattedTotalBalance = currentSafeBalance ? formatAmountInUsFormat(currentSafeBalance) : '' diff --git a/src/logic/safe/hooks/useLoadSafe.tsx b/src/logic/safe/hooks/useLoadSafe.tsx index 6a44a7c499..74e8698f10 100644 --- a/src/logic/safe/hooks/useLoadSafe.tsx +++ b/src/logic/safe/hooks/useLoadSafe.tsx @@ -1,4 +1,4 @@ -import { useEffect } from 'react' +import { useEffect, useState } from 'react' import { useDispatch } from 'react-redux' import loadAddressBookFromStorage from 'src/logic/addressBook/store/actions/loadAddressBookFromStorage' @@ -10,11 +10,12 @@ import fetchTransactions from 'src/logic/safe/store/actions/transactions/fetchTr import fetchSafeCreationTx from 'src/logic/safe/store/actions/fetchSafeCreationTx' import { Dispatch } from 'src/logic/safe/store/actions/types.d' -export const useLoadSafe = (safeAddress?: string): void => { +export const useLoadSafe = (safeAddress?: string): boolean => { const dispatch = useDispatch() + const [safeLoaded, setSafeLoaded] = useState(false) useEffect(() => { - const fetchData = () => { + const fetchData = async () => { if (safeAddress) { dispatch(fetchLatestMasterContractVersion()) .then(() => { @@ -24,7 +25,9 @@ export const useLoadSafe = (safeAddress?: string): void => { .then(() => { dispatch(fetchSafeCreationTx(safeAddress)) dispatch(fetchTransactions(safeAddress)) - return dispatch(addViewedSafe(safeAddress)) + dispatch(addViewedSafe(safeAddress)) + setSafeLoaded(true) + return }) } } @@ -32,4 +35,6 @@ export const useLoadSafe = (safeAddress?: string): void => { fetchData() }, [dispatch, safeAddress]) + + return safeLoaded } diff --git a/src/logic/safe/hooks/useSafeScheduledUpdates.tsx b/src/logic/safe/hooks/useSafeScheduledUpdates.tsx index 61c1ae2dac..fdbe35fa92 100644 --- a/src/logic/safe/hooks/useSafeScheduledUpdates.tsx +++ b/src/logic/safe/hooks/useSafeScheduledUpdates.tsx @@ -8,7 +8,7 @@ import { checkAndUpdateSafe } from 'src/logic/safe/store/actions/fetchSafe' import fetchTransactions from 'src/logic/safe/store/actions/transactions/fetchTransactions' import { TIMEOUT } from 'src/utils/constants' -export const useSafeScheduledUpdates = (safeAddress?: string): void => { +export const useSafeScheduledUpdates = (safeLoaded: boolean, safeAddress?: string): void => { const dispatch = useDispatch() const timer = useRef() @@ -34,7 +34,7 @@ export const useSafeScheduledUpdates = (safeAddress?: string): void => { } } - if (safeAddress) { + if (safeAddress && safeLoaded) { fetchSafeData(safeAddress) } From 51f7d66d46785277a76dcdff2192558290801db6 Mon Sep 17 00:00:00 2001 From: Agustin Pane Date: Thu, 10 Dec 2020 13:10:15 -0300 Subject: [PATCH 3/6] Remove export --- src/logic/safe/transactions/gas.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logic/safe/transactions/gas.ts b/src/logic/safe/transactions/gas.ts index 1af94841d1..92982f12c9 100644 --- a/src/logic/safe/transactions/gas.ts +++ b/src/logic/safe/transactions/gas.ts @@ -130,7 +130,7 @@ export const getDataFromNodeErrorMessage = (errorMessage: string): string | unde } } -export const getGasEstimationTxResponse = async (txConfig: { +const getGasEstimationTxResponse = async (txConfig: { to: string from: string data: string From b3be308729d4857d0e9a3aee638080c154bbb5fc Mon Sep 17 00:00:00 2001 From: Agustin Pane Date: Thu, 10 Dec 2020 16:23:25 -0300 Subject: [PATCH 4/6] Reword safeLoaded --- src/logic/safe/hooks/useLoadSafe.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/logic/safe/hooks/useLoadSafe.tsx b/src/logic/safe/hooks/useLoadSafe.tsx index 74e8698f10..97702ef536 100644 --- a/src/logic/safe/hooks/useLoadSafe.tsx +++ b/src/logic/safe/hooks/useLoadSafe.tsx @@ -12,7 +12,7 @@ import { Dispatch } from 'src/logic/safe/store/actions/types.d' export const useLoadSafe = (safeAddress?: string): boolean => { const dispatch = useDispatch() - const [safeLoaded, setSafeLoaded] = useState(false) + const [isSafeLoaded, setIsSafeLoaded] = useState(false) useEffect(() => { const fetchData = async () => { @@ -26,7 +26,7 @@ export const useLoadSafe = (safeAddress?: string): boolean => { dispatch(fetchSafeCreationTx(safeAddress)) dispatch(fetchTransactions(safeAddress)) dispatch(addViewedSafe(safeAddress)) - setSafeLoaded(true) + setIsSafeLoaded(true) return }) } @@ -36,5 +36,5 @@ export const useLoadSafe = (safeAddress?: string): boolean => { fetchData() }, [dispatch, safeAddress]) - return safeLoaded + return isSafeLoaded } From 45f128480af9b2ff5d4b98dcbbc0287f659c6517 Mon Sep 17 00:00:00 2001 From: Agustin Pane Date: Thu, 10 Dec 2020 16:24:10 -0300 Subject: [PATCH 5/6] Fix warning --- src/logic/safe/hooks/useLoadSafe.tsx | 2 +- src/logic/safe/hooks/useSafeScheduledUpdates.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/logic/safe/hooks/useLoadSafe.tsx b/src/logic/safe/hooks/useLoadSafe.tsx index 97702ef536..4a6590f271 100644 --- a/src/logic/safe/hooks/useLoadSafe.tsx +++ b/src/logic/safe/hooks/useLoadSafe.tsx @@ -15,7 +15,7 @@ export const useLoadSafe = (safeAddress?: string): boolean => { const [isSafeLoaded, setIsSafeLoaded] = useState(false) useEffect(() => { - const fetchData = async () => { + const fetchData = () => { if (safeAddress) { dispatch(fetchLatestMasterContractVersion()) .then(() => { diff --git a/src/logic/safe/hooks/useSafeScheduledUpdates.tsx b/src/logic/safe/hooks/useSafeScheduledUpdates.tsx index fdbe35fa92..0073fd65fa 100644 --- a/src/logic/safe/hooks/useSafeScheduledUpdates.tsx +++ b/src/logic/safe/hooks/useSafeScheduledUpdates.tsx @@ -42,5 +42,5 @@ export const useSafeScheduledUpdates = (safeLoaded: boolean, safeAddress?: strin mounted = false clearTimeout(timer.current) } - }, [dispatch, safeAddress]) + }, [dispatch, safeAddress, safeLoaded]) } From 231e60fd18d00f80778f8a89009f613d58e8c615 Mon Sep 17 00:00:00 2001 From: Agustin Pane Date: Thu, 10 Dec 2020 17:15:44 -0300 Subject: [PATCH 6/6] Improve check for setIsSafeLoaded --- src/logic/safe/hooks/useLoadSafe.tsx | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/logic/safe/hooks/useLoadSafe.tsx b/src/logic/safe/hooks/useLoadSafe.tsx index 4a6590f271..d847d2a941 100644 --- a/src/logic/safe/hooks/useLoadSafe.tsx +++ b/src/logic/safe/hooks/useLoadSafe.tsx @@ -15,20 +15,15 @@ export const useLoadSafe = (safeAddress?: string): boolean => { const [isSafeLoaded, setIsSafeLoaded] = useState(false) useEffect(() => { - const fetchData = () => { + const fetchData = async () => { if (safeAddress) { - dispatch(fetchLatestMasterContractVersion()) - .then(() => { - dispatch(fetchSafe(safeAddress)) - return dispatch(fetchSafeTokens(safeAddress)) - }) - .then(() => { - dispatch(fetchSafeCreationTx(safeAddress)) - dispatch(fetchTransactions(safeAddress)) - dispatch(addViewedSafe(safeAddress)) - setIsSafeLoaded(true) - return - }) + await dispatch(fetchLatestMasterContractVersion()) + await dispatch(fetchSafe(safeAddress)) + setIsSafeLoaded(true) + await dispatch(fetchSafeTokens(safeAddress)) + dispatch(fetchSafeCreationTx(safeAddress)) + dispatch(fetchTransactions(safeAddress)) + dispatch(addViewedSafe(safeAddress)) } } dispatch(loadAddressBookFromStorage())