Skip to content

Commit

Permalink
feat: PortfolioView: Add feature flag check for polling intervals (#2…
Browse files Browse the repository at this point in the history
…8501)

## **Description**

Adds a remote endpoint check to see if the remote API wants us to update
the number of seconds between polling intervals for balances.

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/28501?quickstart=1)

## **Related issues**

Fixes:

## **Manual testing steps**

1. Open the extension
2. See the network request being made in the Networks devtools panel
3. Change conditional in response to `if (true) {` and hardcode a
`pollInterval` value
4. See `this.tokenBalancesController.setIntervalLength` successfully
called.

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
  • Loading branch information
darkwing authored Nov 27, 2024
1 parent a2651c0 commit 068c456
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
27 changes: 26 additions & 1 deletion app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ import {
ExcludedSnapEndowments,
} from '../../shared/constants/permissions';
import { UI_NOTIFICATIONS } from '../../shared/notifications';
import { MILLISECOND, SECOND } from '../../shared/constants/time';
import { MILLISECOND, MINUTE, SECOND } from '../../shared/constants/time';
import {
ORIGIN_METAMASK,
POLLING_TOKEN_ENVIRONMENT_TYPES,
Expand Down Expand Up @@ -244,6 +244,7 @@ import { endTrace, trace } from '../../shared/lib/trace';
// eslint-disable-next-line import/no-restricted-paths
import { isSnapId } from '../../ui/helpers/utils/snaps';
import { BridgeStatusAction } from '../../shared/types/bridge-status';
import fetchWithCache from '../../shared/lib/fetch-with-cache';
import { BalancesController as MultichainBalancesController } from './lib/accounts/BalancesController';
import {
///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
Expand Down Expand Up @@ -2637,6 +2638,29 @@ export default class MetamaskController extends EventEmitter {
}
}

// Provides a method for getting feature flags for the multichain
// initial rollout, such that we can remotely modify polling interval
getInfuraFeatureFlags() {
fetchWithCache({
url: 'https://swap.api.cx.metamask.io/featureFlags',
cacheRefreshTime: MINUTE * 20,
})
.then(this.onFeatureFlagResponseReceived)
.catch((e) => {
// API unreachable (?)
log.warn('Feature flag endpoint is unreachable', e);
});
}

onFeatureFlagResponseReceived(response) {
const { multiChainAssets = {} } = response;
const { pollInterval } = multiChainAssets;
// Polling interval is provided in seconds
if (pollInterval > 0) {
this.tokenBalancesController.setIntervalLength(pollInterval * SECOND);
}
}

postOnboardingInitialization() {
const { usePhishDetect } = this.preferencesController.state;

Expand Down Expand Up @@ -2669,6 +2693,7 @@ export default class MetamaskController extends EventEmitter {
triggerNetworkrequests() {
this.txController.startIncomingTransactionPolling();
this.tokenDetectionController.enable();
this.getInfuraFeatureFlags();
}

stopNetworkRequests() {
Expand Down
51 changes: 51 additions & 0 deletions app/scripts/metamask-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { flushPromises } from '../../test/lib/timer-helpers';
import { ETH_EOA_METHODS } from '../../shared/constants/eth-methods';
import { createMockInternalAccount } from '../../test/jest/mocks';
import { mockNetworkState } from '../../test/stub/networks';
import { SECOND } from '../../shared/constants/time';
import {
BalancesController as MultichainBalancesController,
BTC_BALANCES_UPDATE_TIME as MULTICHAIN_BALANCES_UPDATE_TIME,
Expand Down Expand Up @@ -2632,6 +2633,56 @@ describe('MetaMaskController', () => {
});
});

describe('onFeatureFlagResponseReceived', () => {
const metamaskController = new MetaMaskController({
showUserConfirmation: noop,
encryptor: mockEncryptor,
initState: cloneDeep(firstTimeState),
initLangCode: 'en_US',
platform: {
showTransactionNotification: () => undefined,
getVersion: () => 'foo',
},
browser: browserPolyfillMock,
infuraProjectId: 'foo',
isFirstMetaMaskControllerSetup: true,
});

beforeEach(() => {
jest.spyOn(
metamaskController.tokenBalancesController,
'setIntervalLength',
);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should not set the interval length if the pollInterval is 0', () => {
metamaskController.onFeatureFlagResponseReceived({
multiChainAssets: {
pollInterval: 0,
},
});
expect(
metamaskController.tokenBalancesController.setIntervalLength,
).not.toHaveBeenCalled();
});

it('should set the interval length if the pollInterval is greater than 0', () => {
const pollInterval = 10;
metamaskController.onFeatureFlagResponseReceived({
multiChainAssets: {
pollInterval,
},
});
expect(
metamaskController.tokenBalancesController.setIntervalLength,
).toHaveBeenCalledWith(pollInterval * SECOND);
});
});

describe('MV3 Specific behaviour', () => {
beforeAll(async () => {
mockIsManifestV3.mockReturnValue(true);
Expand Down

0 comments on commit 068c456

Please sign in to comment.