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

Replace useSafeGasEstimatePolling with usePolling #23010

Merged
merged 93 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
556a765
Added generic polling hook
shanejonas Feb 15, 2024
5706baa
Added fast stable stringify
shanejonas Feb 15, 2024
a9a2303
Removed fast stringify for normal json stringify to not deal with ESM
shanejonas Feb 16, 2024
591c934
Removed fast-json-stable-stringify
shanejonas Feb 16, 2024
d8bb41d
Fixed issue if usePollingOptions.options is not defined
shanejonas Feb 16, 2024
1c6d5ea
Merge branch 'develop' into sj/added-generic-polling-hook
jiexi Feb 16, 2024
cf501b9
Replace useSafeGasEstimatePolling with usePolling
jiexi Feb 16, 2024
57a0469
update specs
jiexi Feb 16, 2024
3d48452
update useGasFeeEstimates to use usePolling hook
jiexi Feb 16, 2024
cabd6db
fix undefined dereference estimatedBaseFee
jiexi Feb 16, 2024
3ec134e
Fix base-fee-input
jiexi Feb 16, 2024
9a641c9
jsdoc
jiexi Feb 16, 2024
cd086c4
remove .tool-versions
jiexi Feb 16, 2024
6398d06
fix advanced-gas-fee-gas-limit.test.js
jiexi Feb 20, 2024
c99ff3b
Fix priority fee
jiexi Feb 20, 2024
db00da5
fix base fee import act
jiexi Feb 20, 2024
40bf680
fix advanced fee
jiexi Feb 20, 2024
3173387
fix priority fee input
jiexi Feb 20, 2024
b2d3f31
fix gas fee popover
jiexi Feb 20, 2024
b3160b8
lint
jiexi Feb 20, 2024
1dc2688
WIP gas-details-item.test.js
jiexi Feb 20, 2024
af58cc9
WIP
jiexi Feb 20, 2024
2e7db99
Fix specs
jiexi Feb 20, 2024
be2afe8
lint
jiexi Feb 20, 2024
a9b0dc6
missed one
jiexi Feb 20, 2024
3d4d00d
Merge branch 'jl/fix-incorrect-gas-estimate-tests' into jl/added-gene…
jiexi Feb 20, 2024
aef9520
restore useGasFeeEstimates
jiexi Feb 20, 2024
c0339b8
fix gas-details-item act
jiexi Feb 20, 2024
93973ce
Merge branch 'develop' into sj/added-generic-polling-hook
shanejonas Feb 21, 2024
8c1d16a
Merge branch 'sj/added-generic-polling-hook' into jl/added-generic-po…
jiexi Feb 21, 2024
ffb7ab4
Fix useGasItemFeeDetails gasFeeEstimates undefined reference
jiexi Feb 21, 2024
c20fb28
Fix base-fee-input.test.js
jiexi Feb 21, 2024
18e5801
fix priority-fee-input.test.js
jiexi Feb 21, 2024
55a76ab
fix transaction-detail.component.test.js
jiexi Feb 21, 2024
35b7f78
Fix ui/pages/swaps test
jiexi Feb 21, 2024
2293f79
fix transaction-list-item-details.component.test.js
jiexi Feb 21, 2024
1123976
fix send-content.component.test.js
jiexi Feb 21, 2024
f565236
Get confirmation container container tests passing and remove act war…
shanejonas Feb 21, 2024
00f30a6
remove snapshot
jiexi Feb 21, 2024
f2c5a94
fix snapshot
jiexi Feb 21, 2024
1ed23fc
fix snapshot attempt 2
jiexi Feb 21, 2024
4919512
fix snapshot attempt 3
jiexi Feb 21, 2024
4cdaaa3
fix edit-gas-fee-button.test.js
jiexi Feb 21, 2024
f27318e
fix edit-gas-item.test.js
jiexi Feb 21, 2024
9f705f5
lint
jiexi Feb 21, 2024
f441ccf
fix fee-details-component.test.js
jiexi Feb 21, 2024
adf57bf
Fix routes.component.test.js
jiexi Feb 22, 2024
b4d3743
update mock-send-state to silence required props errors
jiexi Feb 22, 2024
e03e539
fix edit-gas-fee-icon.test.js
jiexi Feb 22, 2024
4c5a52d
fix edit-gas-tooltip.test.js
jiexi Feb 22, 2024
5b9f557
lint
jiexi Feb 22, 2024
aa0580d
Fixed token allowance tests
shanejonas Feb 22, 2024
eb38f68
Fixed action mocks
shanejonas Feb 22, 2024
b66fc18
fix confirm-gas-display.test
jiexi Feb 22, 2024
a2ac2ff
fix send.test.js
jiexi Feb 22, 2024
13d8eff
Updated snapshot for confirm-send-ether.test.js
shanejonas Feb 22, 2024
9892d45
Fixed action mocks for confirm-transaction-base.test.js
shanejonas Feb 22, 2024
cde5e7a
Fixed action mocks for advanced-gas-fee-input-subtext.test.js
shanejonas Feb 22, 2024
ad7e756
Fixed action mocks for cancel-speedup-popover.test.js
shanejonas Feb 22, 2024
013eebb
fix confirm-transaction-base.test.js
jiexi Feb 22, 2024
d97ddd9
fix advanced-gas-fee-input-subtext.test.js
jiexi Feb 22, 2024
5ed2636
fix edit-gas-fee-popover.test.js
jiexi Feb 23, 2024
86bf912
fix useGasFeeInputs.test.js via test-utils selectors
jiexi Feb 23, 2024
2e710c9
add mock state by chain id to speedup test
jiexi Feb 23, 2024
9422acb
wip
jiexi Feb 23, 2024
170fee4
exit early updateTransactionToTenPercentIncreasedGasFee
jiexi Feb 23, 2024
2142d98
Revert "wip"
jiexi Feb 23, 2024
54519ae
update updateTransactionToTenPercentIncreasedGasFee guard
jiexi Feb 23, 2024
82710b4
Merge branch 'develop' into jl/added-generic-polling-hook-replace-use…
jiexi Feb 23, 2024
47b8b1f
lint
jiexi Feb 23, 2024
98d3230
update blockaid-banner-alert snapshot?..
jiexi Feb 23, 2024
8ec1e7c
fix send-content.component.test.js snapshot
jiexi Feb 23, 2024
df77d8f
Merge branch 'develop' into jl/added-generic-polling-hook-replace-use…
jiexi Feb 26, 2024
a0ba91f
wait for confirm to be enabled before continuing to tx insights in te…
jiexi Feb 26, 2024
45f960e
Merge branch 'develop' into jl/added-generic-polling-hook-replace-use…
jiexi Feb 26, 2024
7d1eca7
Merge branch 'develop' into jl/added-generic-polling-hook-replace-use…
adonesky1 Feb 27, 2024
3f6f272
Merge branch 'develop' into jl/added-generic-polling-hook-replace-use…
jiexi Feb 28, 2024
d0d702f
Add isMounted guard to useGasFeeEstimates
jiexi Mar 4, 2024
97dc6db
Merge branch 'develop' into jl/added-generic-polling-hook-replace-use…
adonesky1 Mar 5, 2024
4f28da6
Merge branch 'develop' into jl/added-generic-polling-hook-replace-use…
adonesky1 Mar 5, 2024
75c91c1
fix test
adonesky1 Mar 5, 2024
1080c91
add isMounted guard to gas-timing-component effect
jiexi Mar 5, 2024
168fc81
Merge branch 'develop' into jl/added-generic-polling-hook-replace-use…
adonesky1 Mar 5, 2024
7cf4cc1
update confirm-send-ether.test.js snapshot
jiexi Mar 5, 2024
ec6c828
Merge branch 'develop' into jl/added-generic-polling-hook-replace-use…
jiexi Mar 5, 2024
c246019
Merge branch 'develop' into jl/added-generic-polling-hook-replace-use…
jiexi Mar 5, 2024
928c60b
Merge branch 'develop' into jl/added-generic-polling-hook-replace-use…
adonesky1 Mar 6, 2024
02a0c73
use renderHookWithProvider
adonesky1 Mar 6, 2024
37ef9ac
fix broken swaps flow
adonesky1 Mar 6, 2024
64c3fd5
Merge branch 'develop' into jl/added-generic-polling-hook-replace-use…
jiexi Mar 6, 2024
974acba
Merge branch 'develop' into jl/added-generic-polling-hook-replace-use…
adonesky1 Mar 6, 2024
56fedb4
Merge branch 'develop' into jl/added-generic-polling-hook-replace-use…
jiexi Mar 7, 2024
d7b1e5e
Merge branch 'develop' into jl/added-generic-polling-hook-replace-use…
adonesky1 Mar 8, 2024
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
6 changes: 6 additions & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3301,6 +3301,12 @@ export default class MetamaskController extends EventEmitter {
),

// GasFeeController
gasFeeStartPollingByNetworkClientId:
gasFeeController.startPollingByNetworkClientId.bind(gasFeeController),

gasFeeStopPollingByPollingToken:
gasFeeController.stopPollingByPollingToken.bind(gasFeeController),

getGasFeeEstimatesAndStartPolling:
gasFeeController.getGasFeeEstimatesAndStartPolling.bind(
gasFeeController,
Expand Down
1 change: 0 additions & 1 deletion development/ts-migration-dashboard/files-to-convert.json
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,6 @@
"ui/hooks/useIncrementedGasFees.js",
"ui/hooks/useOriginMetadata.js",
"ui/hooks/usePrevious.js",
"ui/hooks/useSafeGasEstimatePolling.js",
"ui/hooks/useSegmentContext.js",
"ui/hooks/useShouldAnimateGasEstimations.js",
"ui/hooks/useShouldShowSpeedUp.js",
Expand Down
14 changes: 11 additions & 3 deletions ui/hooks/useGasFeeEstimates.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
getIsGasEstimatesLoading,
getIsNetworkBusy,
} from '../ducks/metamask/metamask';
import { useSafeGasEstimatePolling } from './useSafeGasEstimatePolling';
import usePolling from './usePolling';
import { gasFeeStartPollingByNetworkClientId, gasFeeStopPollingByPollingToken } from '../store/actions';
import { getSelectedNetworkClientId } from '../selectors';

/**
* @typedef {object} GasEstimates
Expand All @@ -27,12 +29,18 @@ import { useSafeGasEstimatePolling } from './useSafeGasEstimatePolling';
*
* @returns {GasEstimates} GasEstimates object
*/
export function useGasFeeEstimates() {
export function useGasFeeEstimates(networkClientId) {
const gasEstimateType = useSelector(getGasEstimateType);
const gasFeeEstimates = useSelector(getGasFeeEstimates, isEqual);
const isGasEstimatesLoading = useSelector(getIsGasEstimatesLoading);
const isNetworkBusy = useSelector(getIsNetworkBusy);
useSafeGasEstimatePolling();
const selectedNetworkClientId = useSelector(getSelectedNetworkClientId);

usePolling({
startPollingByNetworkClientId: gasFeeStartPollingByNetworkClientId,
stopPollingByPollingToken: gasFeeStopPollingByPollingToken,
networkClientId: networkClientId ?? selectedNetworkClientId,
});

return {
gasFeeEstimates,
Expand Down
37 changes: 26 additions & 11 deletions ui/hooks/useGasFeeEstimates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@ import {
getGasFeeEstimates,
getIsGasEstimatesLoading,
} from '../ducks/metamask/metamask';
import { checkNetworkAndAccountSupports1559 } from '../selectors';
import { checkNetworkAndAccountSupports1559, getSelectedNetworkClientId } from '../selectors';
import {
disconnectGasFeeEstimatePoller,
getGasFeeEstimatesAndStartPolling,
gasFeeStopPollingByPollingToken,
gasFeeStartPollingByNetworkClientId,
} from '../store/actions';

import { useGasFeeEstimates } from './useGasFeeEstimates';

jest.mock('../store/actions', () => ({
disconnectGasFeeEstimatePoller: jest.fn(),
getGasFeeEstimatesAndStartPolling: jest.fn(),
addPollingTokenToAppState: jest.fn(),
removePollingTokenFromAppState: jest.fn(),
gasFeeStopPollingByPollingToken: jest.fn(),
gasFeeStartPollingByNetworkClientId: jest.fn(),
}));

jest.mock('react-redux', () => {
Expand Down Expand Up @@ -51,6 +49,9 @@ const generateUseSelectorRouter =
DEFAULT_OPTS.checkNetworkAndAccountSupports1559
);
}
if (selector === getSelectedNetworkClientId) {
return 'selectedNetworkClientId';
}
if (selector === getGasEstimateType) {
return opts.gasEstimateType ?? DEFAULT_OPTS.gasEstimateType;
}
Expand All @@ -68,12 +69,12 @@ describe('useGasFeeEstimates', () => {
beforeEach(() => {
jest.clearAllMocks();
tokens = [];
getGasFeeEstimatesAndStartPolling.mockImplementation(() => {
gasFeeStartPollingByNetworkClientId.mockImplementation(() => {
const token = createRandomId();
tokens.push(token);
return Promise.resolve(token);
});
disconnectGasFeeEstimatePoller.mockImplementation((token) => {
gasFeeStopPollingByPollingToken.mockImplementation((token) => {
tokens = tokens.filter((tkn) => tkn !== token);
});
});
Expand All @@ -90,11 +91,25 @@ describe('useGasFeeEstimates', () => {
expect(tokens).toHaveLength(1);
const expectedToken = tokens[0];
await cleanup();
expect(getGasFeeEstimatesAndStartPolling).toHaveBeenCalledTimes(1);
expect(disconnectGasFeeEstimatePoller).toHaveBeenCalledWith(expectedToken);
expect(gasFeeStartPollingByNetworkClientId).toHaveBeenCalledTimes(1);
expect(gasFeeStopPollingByPollingToken).toHaveBeenCalledWith(expectedToken);
expect(tokens).toHaveLength(0);
});

it('polls the selected networkClientId by default', () => {
useSelector.mockImplementation(generateUseSelectorRouter());
renderHook(() => useGasFeeEstimates());
expect(gasFeeStartPollingByNetworkClientId).toHaveBeenCalledTimes(1);
expect(gasFeeStartPollingByNetworkClientId).toHaveBeenCalledWith('selectedNetworkClientId', undefined)
});

it('polls the passed in networkClientId when provided', () => {
useSelector.mockImplementation(generateUseSelectorRouter());
renderHook(() => useGasFeeEstimates('networkClientId1'));
expect(gasFeeStartPollingByNetworkClientId).toHaveBeenCalledTimes(1);
expect(gasFeeStartPollingByNetworkClientId).toHaveBeenCalledWith('networkClientId1', undefined)
});

it('works with LEGACY gas prices', () => {
useSelector.mockImplementation(
generateUseSelectorRouter({
Expand Down
84 changes: 84 additions & 0 deletions ui/hooks/usePolling.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import React from 'react';
import { renderHook, cleanup } from '@testing-library/react-hooks';
import { Provider } from 'react-redux';
import configureStore from '../store/store';
import usePolling from './usePolling';

describe('usePolling', () => {
// eslint-disable-next-line jest/no-done-callback
it('calls startPollingByNetworkClientId and callback option args with polling token when component instantiating the hook mounts', (done) => {
const mockStart = jest.fn().mockImplementation(() => {
return Promise.resolve('pollingToken');
});
const mockStop = jest.fn();
const networkClientId = 'mainnet';
const options = {};
const mockState = {
metamask: {},
};

const wrapper = ({ children }) => (
<>
<Provider store={configureStore(mockState)}>{children}</Provider>
</>
);

renderHook(
() => {
usePolling({
callback: (pollingToken) => {
expect(mockStart).toHaveBeenCalledWith(networkClientId, options);
expect(pollingToken).toBeDefined();
done();
return (_pollingToken) => {
// noop
};
},
startPollingByNetworkClientId: mockStart,
stopPollingByPollingToken: mockStop,
networkClientId,
options,
});
},
{ wrapper },
);
});
// eslint-disable-next-line jest/no-done-callback
it('calls the cleanup function with the correct pollingToken when unmounted', (done) => {
const mockStart = jest.fn().mockImplementation(() => {
return Promise.resolve('pollingToken');
});
const mockStop = jest.fn();
const networkClientId = 'mainnet';
const options = {};
const mockState = {
metamask: {},
};

const wrapper = ({ children }) => (
<>
<Provider store={configureStore(mockState)}>{children}</Provider>
</>
);

renderHook(
() => {
usePolling({
callback: () => {
return (_pollingToken) => {
expect(mockStop).toHaveBeenCalledWith(_pollingToken);
expect(_pollingToken).toBeDefined();
done();
};
},
startPollingByNetworkClientId: mockStart,
stopPollingByPollingToken: mockStop,
networkClientId,
options,
});
},
{ wrapper },
);
cleanup();
});
});
56 changes: 56 additions & 0 deletions ui/hooks/usePolling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { useEffect, useRef } from 'react';

type UsePollingOptions = {
callback?: (pollingToken: string) => (pollingToken: string) => void;
startPollingByNetworkClientId: (
networkClientId: string,
options: any,
) => Promise<string>;
stopPollingByPollingToken: (pollingToken: string) => void;
networkClientId: string;
options?: any;
};

const usePolling = (usePollingOptions: UsePollingOptions) => {
const pollTokenRef = useRef<null | string>(null);
const cleanupRef = useRef<null | ((pollingToken: string) => void)>(null);
let isMounted = false;
useEffect(() => {
isMounted = true;

const cleanup = () => {
if (pollTokenRef.current) {
usePollingOptions.stopPollingByPollingToken(pollTokenRef.current);
cleanupRef.current?.(pollTokenRef.current);
}
};
// Start polling when the component mounts
usePollingOptions
.startPollingByNetworkClientId(
usePollingOptions.networkClientId,
usePollingOptions.options,
)
.then((pollToken) => {
pollTokenRef.current = pollToken;
cleanupRef.current = usePollingOptions.callback?.(pollToken) || null;
if (!isMounted) {
cleanup();
}
});

// Return a cleanup function to stop polling when the component unmounts
return () => {
isMounted = false;
cleanup();
};
}, [
usePollingOptions.networkClientId,
usePollingOptions.options &&
JSON.stringify(
usePollingOptions.options,
Object.keys(usePollingOptions.options).sort(),
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if its possible to simplify above conditional.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is to ensure that if the usePollingOptions.options is present we can guarantee the order of the keys in the option object so that the hook doesn't rerun unnecessarily. I'm not sure if there is a better way to express that conditional.

]);
};

export default usePolling;
47 changes: 0 additions & 47 deletions ui/hooks/useSafeGasEstimatePolling.js

This file was deleted.

Loading
Loading