Skip to content

Commit

Permalink
upgrade NetworkController to V20 (#395)
Browse files Browse the repository at this point in the history
* upgrade NetworkController to V20

* fix lint

* refactor onNetworkStateChange

* revert the refactor

* refactor onNetworkStateChange
  • Loading branch information
kanthesha authored Jul 30, 2024
1 parent 5f597cd commit b55fa2d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 38 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@metamask/controller-utils": "^11.0.0",
"@metamask/eth-json-rpc-provider": "^4.1.0",
"@metamask/eth-query": "^4.0.0",
"@metamask/network-controller": "^19.0.0",
"@metamask/network-controller": "^20.0.0",
"@metamask/polling-controller": "^8.0.0",
"@metamask/transaction-controller": "^34.0.0",
"bignumber.js": "^9.0.1",
Expand Down
39 changes: 10 additions & 29 deletions src/SmartTransactionsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,8 @@ const defaultState = {
},
};

const mockProviderConfig = {
chainId: ChainId.mainnet,
provider: getFakeProvider(),
type: NetworkType.mainnet,
ticker: 'ticker',
};

const mockNetworkState = {
providerConfig: mockProviderConfig,
selectedNetworkClientId: 'id',
selectedNetworkClientId: NetworkType.mainnet,
networkConfigurations: {
id: {
id: 'id',
Expand Down Expand Up @@ -393,7 +385,6 @@ describe('SmartTransactionsController', () => {
releaseLock: jest.fn(),
};
}),
provider: getFakeProvider(),
confirmExternalTransaction: jest.fn(),
getTransactions: jest.fn(),
trackMetaMetricsEvent: trackMetaMetricsEventSpy,
Expand All @@ -404,12 +395,14 @@ describe('SmartTransactionsController', () => {
configuration: {
chainId: ChainId.mainnet,
},
provider: getFakeProvider(),
};
case NetworkType.sepolia:
return {
configuration: {
chainId: ChainId.sepolia,
},
provider: getFakeProvider(),
};
default:
throw new Error('Invalid network client id');
Expand Down Expand Up @@ -451,19 +444,17 @@ describe('SmartTransactionsController', () => {
describe('onNetworkChange', () => {
it('is triggered', () => {
networkListener({
providerConfig: { chainId: '0x32', type: 'rpc', ticker: 'CET' },
selectedNetworkClientId: 'networkClientId',
selectedNetworkClientId: NetworkType.sepolia,
networkConfigurations: {},
networksMetadata: {},
} as NetworkState);
expect(smartTransactionsController.config.chainId).toBe('0x32');
expect(smartTransactionsController.config.chainId).toBe(ChainId.sepolia);
});

it('calls poll', () => {
const checkPollSpy = jest.spyOn(smartTransactionsController, 'checkPoll');
networkListener({
providerConfig: { chainId: '0x32', type: 'rpc', ticker: 'CET' },
selectedNetworkClientId: 'networkClientId',
selectedNetworkClientId: NetworkType.sepolia,
networkConfigurations: {},
networksMetadata: {},
} as NetworkState);
Expand All @@ -489,12 +480,7 @@ describe('SmartTransactionsController', () => {
});
smartTransactionsController.config.chainId = ChainId.sepolia;
networkListener({
providerConfig: {
chainId: ChainId.mainnet,
type: 'rpc',
ticker: 'ETH',
},
selectedNetworkClientId: 'networkClientId',
selectedNetworkClientId: NetworkType.mainnet,
networkConfigurations: {},
networksMetadata: {},
} as NetworkState);
Expand Down Expand Up @@ -525,12 +511,7 @@ describe('SmartTransactionsController', () => {
},
});
networkListener({
providerConfig: {
chainId: ChainId.mainnet,
type: 'rpc',
ticker: 'ETH',
},
selectedNetworkClientId: 'networkClientId',
selectedNetworkClientId: NetworkType.mainnet,
networkConfigurations: {},
networksMetadata: {},
} as NetworkState);
Expand Down Expand Up @@ -580,9 +561,9 @@ describe('SmartTransactionsController', () => {
'updateSmartTransactions',
);
expect(updateSmartTransactionsSpy).not.toHaveBeenCalled();
smartTransactionsController.config.supportedChainIds = [ChainId.mainnet];
networkListener({
providerConfig: { chainId: '0x32', type: 'rpc', ticker: 'CET' },
selectedNetworkClientId: 'networkClientId',
selectedNetworkClientId: NetworkType.sepolia,
networkConfigurations: {},
networksMetadata: {},
} as NetworkState);
Expand Down
10 changes: 5 additions & 5 deletions src/SmartTransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { hexlify } from '@ethersproject/bytes';
import type { BaseConfig, BaseState } from '@metamask/base-controller';
import { query, safelyExecute, ChainId } from '@metamask/controller-utils';
import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';
import EthQuery from '@metamask/eth-query';
import type {
NetworkClientId,
Expand Down Expand Up @@ -122,7 +121,6 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
{
onNetworkStateChange,
getNonceLock,
provider,
confirmExternalTransaction,
getTransactions,
trackMetaMetricsEvent,
Expand All @@ -133,7 +131,6 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
listener: (networkState: NetworkState) => void,
) => void;
getNonceLock: any;
provider: SafeEventEmitterProvider;
confirmExternalTransaction: any;
getTransactions: (options?: GetTransactionsOptions) => TransactionMeta[];
trackMetaMetricsEvent: any;
Expand Down Expand Up @@ -192,8 +189,11 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
this.initializeSmartTransactionsForChainId();
this.#ensureUniqueSmartTransactions();

onNetworkStateChange(({ providerConfig: newProvider }) => {
const { chainId } = newProvider;
onNetworkStateChange(({ selectedNetworkClientId }) => {
const {
configuration: { chainId },
provider,
} = this.getNetworkClientById(selectedNetworkClientId);
const isNewChainId = chainId !== this.config.chainId;
this.configure({ chainId });
this.initializeSmartTransactionsForChainId();
Expand Down
2 changes: 0 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import DefaultExport from '.';
import SmartTransactionsController from './SmartTransactionsController';
import { getFakeProvider } from './test-helpers';

describe('default export', () => {
it('exports SmartTransactionsController', () => {
jest.useFakeTimers();
const controller = new DefaultExport({
onNetworkStateChange: jest.fn(),
getNonceLock: null,
provider: getFakeProvider(),
confirmExternalTransaction: jest.fn(),
getTransactions: jest.fn(),
trackMetaMetricsEvent: jest.fn(),
Expand Down
32 changes: 31 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1893,6 +1893,29 @@ __metadata:
languageName: node
linkType: hard

"@metamask/network-controller@npm:^20.0.0":
version: 20.0.0
resolution: "@metamask/network-controller@npm:20.0.0"
dependencies:
"@metamask/base-controller": ^6.0.0
"@metamask/controller-utils": ^11.0.0
"@metamask/eth-block-tracker": ^9.0.3
"@metamask/eth-json-rpc-infura": ^9.1.0
"@metamask/eth-json-rpc-middleware": ^12.1.1
"@metamask/eth-json-rpc-provider": ^4.1.0
"@metamask/eth-query": ^4.0.0
"@metamask/json-rpc-engine": ^9.0.0
"@metamask/rpc-errors": ^6.2.1
"@metamask/swappable-obj-proxy": ^2.2.0
"@metamask/utils": ^8.3.0
async-mutex: ^0.5.0
immer: ^9.0.6
loglevel: ^1.8.1
uuid: ^8.3.2
checksum: 27a4b669655d4566045de5489d9bc8fee8454f2e74ddc844c0c978a5af6b821c0bdf020b9b649db32f07a298006c021e33e6d29ccf64a6f6301a8e80de21000c
languageName: node
linkType: hard

"@metamask/nonce-tracker@npm:^5.0.0":
version: 5.0.0
resolution: "@metamask/nonce-tracker@npm:5.0.0"
Expand Down Expand Up @@ -2087,7 +2110,7 @@ __metadata:
"@metamask/eth-json-rpc-provider": ^4.1.0
"@metamask/eth-query": ^4.0.0
"@metamask/json-rpc-engine": ^9.0.0
"@metamask/network-controller": ^19.0.0
"@metamask/network-controller": ^20.0.0
"@metamask/polling-controller": ^8.0.0
"@metamask/transaction-controller": ^34.0.0
"@types/jest": ^26.0.24
Expand Down Expand Up @@ -6738,6 +6761,13 @@ __metadata:
languageName: node
linkType: hard

"loglevel@npm:^1.8.1":
version: 1.9.1
resolution: "loglevel@npm:1.9.1"
checksum: e1c8586108c4d566122e91f8a79c8df728920e3a714875affa5120566761a24077ec8ec9e5fc388b022e39fc411ec6e090cde1b5775871241b045139771eeb06
languageName: node
linkType: hard

"lru-cache@npm:^5.1.1":
version: 5.1.1
resolution: "lru-cache@npm:5.1.1"
Expand Down

0 comments on commit b55fa2d

Please sign in to comment.