Skip to content

Commit

Permalink
add rollbackToPreviousProvider method (#1132)
Browse files Browse the repository at this point in the history
* add rollbackToPreviousProvider method
  • Loading branch information
adonesky1 authored Mar 22, 2023
1 parent c44fbbd commit 99df8b5
Show file tree
Hide file tree
Showing 20 changed files with 1,653 additions and 462 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import HttpProvider from 'ethjs-provider-http';
import { IPFS_DEFAULT_GATEWAY_URL } from '@metamask/controller-utils';
import {
IPFS_DEFAULT_GATEWAY_URL,
NetworkType,
} from '@metamask/controller-utils';
import { PreferencesController } from '@metamask/preferences-controller';
import {
NetworkController,
Expand Down Expand Up @@ -295,7 +298,7 @@ describe('AssetsContractController', () => {
);
expect(balances[ERC20_DAI_ADDRESS]).not.toBeUndefined();

network.setProviderType('localhost');
network.setProviderType(NetworkType.localhost);

const noBalances = await assetsContract.getBalancesInSingleCall(
ERC20_DAI_ADDRESS,
Expand Down
5 changes: 3 additions & 2 deletions packages/assets-controllers/src/NftController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
OPENSEA_API_URL,
ERC721,
NetworksChainId,
NetworkType,
} from '@metamask/controller-utils';
import { Network } from '@ethersproject/providers';
import { AssetsContractController } from './AssetsContractController';
Expand Down Expand Up @@ -46,8 +47,8 @@ const DEPRESSIONIST_CLOUDFLARE_IPFS_SUBDOMAIN_PATH = getFormattedIpfsUrl(
true,
);

const SEPOLIA = { chainId: '11155111', type: 'sepolia' as const };
const GOERLI = { chainId: '5', type: 'goerli' as const };
const SEPOLIA = { chainId: '11155111', type: NetworkType.sepolia };
const GOERLI = { chainId: '5', type: NetworkType.goerli };

// Mock out detectNetwork function for cleaner tests, Ethers calls this a bunch of times because the Web3Provider is paranoid.
jest.mock('@ethersproject/providers', () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/assets-controllers/src/NftController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
toChecksumHexAddress,
BNToHex,
fetchWithErrorHandling,
MAINNET,
IPFS_DEFAULT_GATEWAY_URL,
ERC721,
ERC1155,
Expand Down Expand Up @@ -911,7 +910,7 @@ export class NftController extends BaseController<NftConfig, NftState> {
) {
super(config, state);
this.defaultConfig = {
networkType: MAINNET,
networkType: NetworkType.mainnet,
selectedAddress: '',
chainId: '',
ipfsGateway: IPFS_DEFAULT_GATEWAY_URL,
Expand Down
38 changes: 18 additions & 20 deletions packages/assets-controllers/src/NftDetectionController.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import * as sinon from 'sinon';
import nock from 'nock';
import { PreferencesController } from '@metamask/preferences-controller';
import { OPENSEA_PROXY_URL } from '@metamask/controller-utils';
import { OPENSEA_PROXY_URL, NetworkType } from '@metamask/controller-utils';
import { NftController } from './NftController';
import { AssetsContractController } from './AssetsContractController';
import { NftDetectionController } from './NftDetectionController';

const DEFAULT_INTERVAL = 180000;
const MAINNET = 'mainnet';
const GOERLI = 'goerli';

describe('NftDetectionController', () => {
let nftDetection: NftDetectionController;
Expand Down Expand Up @@ -236,9 +234,9 @@ describe('NftDetectionController', () => {
});

it('should detect mainnet correctly', () => {
nftDetection.configure({ networkType: MAINNET });
nftDetection.configure({ networkType: NetworkType.mainnet });
expect(nftDetection.isMainnet()).toStrictEqual(true);
nftDetection.configure({ networkType: GOERLI });
nftDetection.configure({ networkType: NetworkType.goerli });
expect(nftDetection.isMainnet()).toStrictEqual(false);
});

Expand All @@ -258,7 +256,7 @@ describe('NftDetectionController', () => {
addNft: nftController.addNft.bind(nftController),
getNftState: () => nftController.state,
},
{ interval: 10, networkType: GOERLI },
{ interval: 10, networkType: NetworkType.goerli },
);
expect(mockNfts.called).toBe(false);
resolve('');
Expand All @@ -269,12 +267,12 @@ describe('NftDetectionController', () => {
const selectedAddress = '0x1';

nftDetection.configure({
networkType: MAINNET,
networkType: NetworkType.mainnet,
selectedAddress,
});

nftController.configure({
networkType: MAINNET,
networkType: NetworkType.mainnet,
selectedAddress,
});
const { chainId } = nftDetection.config;
Expand All @@ -299,7 +297,7 @@ describe('NftDetectionController', () => {
it('should detect, add NFTs and do nor remove not detected NFTs correctly', async () => {
const selectedAddress = '0x1';
nftDetection.configure({
networkType: MAINNET,
networkType: NetworkType.mainnet,
selectedAddress,
});
nftController.configure({ selectedAddress });
Expand Down Expand Up @@ -348,7 +346,7 @@ describe('NftDetectionController', () => {
it('should not autodetect NFTs that exist in the ignoreList', async () => {
const selectedAddress = '0x2';
nftDetection.configure({
networkType: MAINNET,
networkType: NetworkType.mainnet,
selectedAddress: '0x2',
});
nftController.configure({ selectedAddress });
Expand All @@ -375,7 +373,7 @@ describe('NftDetectionController', () => {
it('should not detect and add NFTs if there is no selectedAddress', async () => {
const selectedAddress = '';
nftDetection.configure({
networkType: MAINNET,
networkType: NetworkType.mainnet,
selectedAddress,
});
const { chainId } = nftDetection.config;
Expand All @@ -386,7 +384,7 @@ describe('NftDetectionController', () => {

it('should not detect and add NFTs to the wrong selectedAddress', async () => {
nftDetection.configure({
networkType: MAINNET,
networkType: NetworkType.mainnet,
selectedAddress: '0x9',
});
const { chainId } = nftDetection.config;
Expand All @@ -409,7 +407,7 @@ describe('NftDetectionController', () => {
preferences.setUseNftDetection(false);
const selectedAddress = '0x9';
nftDetection.configure({
networkType: MAINNET,
networkType: NetworkType.mainnet,
selectedAddress,
});
const { chainId } = nftController.config;
Expand All @@ -423,7 +421,7 @@ describe('NftDetectionController', () => {
preferences.setOpenSeaEnabled(false);
const selectedAddress = '0x9';
nftDetection.configure({
networkType: MAINNET,
networkType: NetworkType.mainnet,
selectedAddress,
});
const { chainId } = nftController.config;
Expand Down Expand Up @@ -492,12 +490,12 @@ describe('NftDetectionController', () => {
const selectedAddress = '0x1';
nftDetection.configure({
selectedAddress,
networkType: MAINNET,
networkType: NetworkType.mainnet,
});

nftController.configure({
selectedAddress,
networkType: MAINNET,
networkType: NetworkType.mainnet,
});

const { chainId } = nftDetection.config;
Expand Down Expand Up @@ -660,12 +658,12 @@ describe('NftDetectionController', () => {
});

nftDetection.configure({
networkType: MAINNET,
networkType: NetworkType.mainnet,
selectedAddress,
});

nftController.configure({
networkType: MAINNET,
networkType: NetworkType.mainnet,
selectedAddress,
});

Expand Down Expand Up @@ -698,12 +696,12 @@ describe('NftDetectionController', () => {
.replyWithError(new Error('UNEXPECTED ERROR'));

nftDetection.configure({
networkType: MAINNET,
networkType: NetworkType.mainnet,
selectedAddress,
});

nftController.configure({
networkType: MAINNET,
networkType: NetworkType.mainnet,
selectedAddress,
});

Expand Down
5 changes: 2 additions & 3 deletions packages/assets-controllers/src/NftDetectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
import type { NetworkState } from '@metamask/network-controller';
import type { PreferencesState } from '@metamask/preferences-controller';
import {
MAINNET,
OPENSEA_PROXY_URL,
OPENSEA_API_URL,
NetworkType,
Expand Down Expand Up @@ -240,7 +239,7 @@ export class NftDetectionController extends BaseController<
super(config, state);
this.defaultConfig = {
interval: DEFAULT_INTERVAL,
networkType: MAINNET,
networkType: NetworkType.mainnet,
chainId: '1',
selectedAddress: '',
disabled: true,
Expand Down Expand Up @@ -320,7 +319,7 @@ export class NftDetectionController extends BaseController<
*
* @returns Whether current network is mainnet.
*/
isMainnet = (): boolean => this.config.networkType === MAINNET;
isMainnet = (): boolean => this.config.networkType === NetworkType.mainnet;

/**
* Triggers asset ERC721 token auto detection on mainnet. Any newly detected NFTs are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import {
NetworkState,
ProviderConfig,
} from '@metamask/network-controller';
import {
NetworksChainId,
MAINNET,
NetworkType,
} from '@metamask/controller-utils';
import { NetworksChainId, NetworkType } from '@metamask/controller-utils';
import { PreferencesController } from '@metamask/preferences-controller';
import { ControllerMessenger } from '@metamask/base-controller';
import { TokensController } from './TokensController';
Expand Down Expand Up @@ -142,7 +138,7 @@ describe('TokenDetectionController', () => {
};
const mainnet = {
chainId: NetworksChainId.mainnet,
type: MAINNET as NetworkType,
type: NetworkType.mainnet,
};

beforeEach(async () => {
Expand Down
14 changes: 7 additions & 7 deletions packages/assets-controllers/src/TokenListController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
NetworkState,
ProviderConfig,
} from '@metamask/network-controller';
import { NetworksChainId } from '@metamask/controller-utils';
import { NetworksChainId, NetworkType } from '@metamask/controller-utils';
import {
TokenListController,
TokenListStateChange,
Expand Down Expand Up @@ -629,8 +629,8 @@ describe('TokenListController', () => {
sampleSingleChainState.tokenList,
);
onNetworkStateChangeCallback({
chainId: '5',
type: 'rpc',
chainId: NetworksChainId.goerli,
type: NetworkType.goerli,
});
await new Promise<void>((resolve) => setTimeout(() => resolve(), 500));

Expand Down Expand Up @@ -1020,7 +1020,7 @@ describe('TokenListController', () => {
);

controllerMessenger.publish('NetworkController:providerConfigChange', {
type: 'goerli',
type: NetworkType.goerli,
chainId: NetworksChainId.goerli,
});

Expand All @@ -1034,7 +1034,7 @@ describe('TokenListController', () => {
);

controllerMessenger.publish('NetworkController:providerConfigChange', {
type: 'rpc',
type: NetworkType.rpc,
chainId: '56',
rpcTarget: 'http://localhost:8545',
});
Expand Down Expand Up @@ -1095,7 +1095,7 @@ describe('TokenListController', () => {
});
await controller.start();
controllerMessenger.publish('NetworkController:providerConfigChange', {
type: 'mainnet',
type: NetworkType.mainnet,
chainId: NetworksChainId.mainnet,
});

Expand Down Expand Up @@ -1136,7 +1136,7 @@ describe('TokenListController', () => {
});

controllerMessenger.publish('NetworkController:providerConfigChange', {
type: 'rpc',
type: NetworkType.rpc,
chainId: '56',
rpcTarget: 'http://localhost:8545',
});
Expand Down
6 changes: 3 additions & 3 deletions packages/assets-controllers/src/TokensController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as sinon from 'sinon';
import nock from 'nock';
import contractMaps from '@metamask/contract-metadata';
import { PreferencesController } from '@metamask/preferences-controller';
import { NetworksChainId } from '@metamask/controller-utils';
import { NetworksChainId, NetworkType } from '@metamask/controller-utils';
import {
NetworkState,
ProviderConfig,
Expand All @@ -27,8 +27,8 @@ const stubCreateEthers = (ctrl: TokensController, res: boolean) => {
});
};

const SEPOLIA = { chainId: '11155111', type: 'sepolia' as const };
const GOERLI = { chainId: '5', type: 'goerli' as const };
const SEPOLIA = { chainId: '11155111', type: NetworkType.sepolia };
const GOERLI = { chainId: '5', type: NetworkType.goerli };

describe('TokensController', () => {
let tokensController: TokensController;
Expand Down
3 changes: 1 addition & 2 deletions packages/assets-controllers/src/TokensController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import type { NetworkState } from '@metamask/network-controller';
import {
NetworkType,
toChecksumHexAddress,
MAINNET,
ERC721_INTERFACE_ID,
} from '@metamask/controller-utils';
import type { Token } from './TokenRatesController';
Expand Down Expand Up @@ -206,7 +205,7 @@ export class TokensController extends BaseController<
super(config, state);

this.defaultConfig = {
networkType: MAINNET,
networkType: NetworkType.mainnet,
selectedAddress: '',
chainId: '',
provider: undefined,
Expand Down
Loading

0 comments on commit 99df8b5

Please sign in to comment.