Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(suite): optimize how often fees are fetched in send form #15469

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/suite/src/hooks/wallet/useFees.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useDispatch } from 'react-redux';
import { useCallback, useEffect } from 'react';

import { NetworkSymbol } from "@suite-common/wallet-config";
import {
updateFeeInfoThunk,
} from '@suite-common/wallet-core';

const networkToInterval = (symbol: NetworkSymbol): number => {
switch (symbol) {
case 'pol':
return 1000;
default:
return 10000;
}
}

export const useFees = (symbol: NetworkSymbol) => {
const dispatch = useDispatch();

const refreshFees = useCallback(() => {
dispatch(updateFeeInfoThunk(symbol))
}, [dispatch, symbol])

useEffect(() => {
refreshFees();
const interval = setInterval(refreshFees, networkToInterval(symbol));

return () => {
clearInterval(interval)
}
}, [refreshFees, symbol])
}
3 changes: 3 additions & 0 deletions packages/suite/src/views/wallet/send/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { spacingsPx } from '@trezor/theme';
import { useSelector } from 'src/hooks/suite';
import { WalletLayout } from 'src/components/wallet';
import { useSendForm, SendContext, UseSendFormProps } from 'src/hooks/wallet/useSendForm';
import { useFees } from 'src/hooks/wallet/useFees';
import {
selectTargetAnonymityByAccountKey,
selectRegisteredUtxosByAccountKey,
Expand Down Expand Up @@ -78,6 +79,8 @@ const SendLoaded = ({ children, selectedAccount }: SendLoadedProps) => {

const { symbol } = selectedAccount.account;

useFees(symbol);

if (props.sendRaw) {
return (
<WalletLayout title="TR_NAV_SEND" isSubpage account={selectedAccount}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
onBlockchainConnectThunk,
onBlockMinedThunk,
onBlockchainNotificationThunk,
updateFeeInfoThunk,
onBlockchainDisconnectThunk,
} from './blockchainThunks';

Expand All @@ -31,7 +30,6 @@ export const prepareBlockchainMiddleware = createMiddlewareWithExtraDeps(
}
break;
case TREZOR_CONNECT_BLOCKCHAIN_ACTIONS.BLOCK:
dispatch(updateFeeInfoThunk(action.payload.coin.shortcut));
dispatch(onBlockMinedThunk(action.payload));
// cardano stuff
dispatch(
Expand Down
9 changes: 0 additions & 9 deletions suite-common/wallet-core/src/blockchain/blockchainThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import { selectAccounts } from '../accounts/accountsReducer';
import { fetchAndUpdateAccountThunk } from '../accounts/accountsThunks';
import { BLOCKCHAIN_MODULE_PREFIX, blockchainActions } from './blockchainActions';
import { selectBlockchainState, selectNetworkBlockchainInfo } from './blockchainReducer';
import { selectNetworkFeeInfo } from '../fees/feesReducer';

const DEFAULT_ACCOUNT_SYNC_INTERVAL = 60 * 1000;

Expand Down Expand Up @@ -108,14 +107,6 @@ export const updateFeeInfoThunk = createThunk(
const network = getNetworkOptional(symbol.toLowerCase());
if (!network) return;
const blockchainInfo = selectNetworkBlockchainInfo(getState(), network.symbol);
const feeInfo = selectNetworkFeeInfo(getState(), network.symbol);

if (
feeInfo &&
feeInfo.blockHeight > 0 &&
blockchainInfo.blockHeight - feeInfo.blockHeight < 10
)
return;

let newFeeInfo;

Expand Down
Loading