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

chore(deps): bump assets controller to v34.0.0 #25540

Merged
merged 12 commits into from
Jun 28, 2024
9 changes: 0 additions & 9 deletions app/scripts/metamask-controller.actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,6 @@ describe('MetaMaskController', function () {
const decimals = 18;

it('two parallel calls with same token details give same result', async function () {
const supportsInterfaceStub = jest.fn().mockResolvedValue(false);
jest
.spyOn(metamaskController.tokensController, '_createEthersContract')
.mockResolvedValue({ supportsInterface: supportsInterfaceStub });

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No longer part of tokens controller

const [token1, token2] = await Promise.all([
metamaskController.getApi().addToken({ address, symbol, decimals }),
metamaskController.getApi().addToken({ address, symbol, decimals }),
Expand All @@ -235,10 +230,6 @@ describe('MetaMaskController', function () {
});

it('networkClientId is used when provided', async function () {
const supportsInterfaceStub = jest.fn().mockResolvedValue(false);
jest
.spyOn(metamaskController.tokensController, '_createEthersContract')
.mockResolvedValue({ supportsInterface: supportsInterfaceStub });
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No longer part of tokens controller

const callSpy = jest
.spyOn(metamaskController.controllerMessenger, 'call')
.mockReturnValue({ configuration: { chainId: '0xa' } });
Expand Down
224 changes: 88 additions & 136 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ export default class MetamaskController extends EventEmitter {

const tokenListMessenger = this.controllerMessenger.getRestricted({
name: 'TokenListController',
allowedActions: ['NetworkController:getNetworkClientById'],
allowedEvents: ['NetworkController:stateChange'],
});

Expand Down Expand Up @@ -603,149 +604,109 @@ export default class MetamaskController extends EventEmitter {
allowedActions: [
'ApprovalController:addRequest',
'NetworkController:getNetworkClientById',
'AccountsController:getSelectedAccount',
'AccountsController:getAccount',
],
allowedEvents: [
'NetworkController:networkDidChange',
'AccountsController:selectedAccountChange',
'AccountsController:selectedEvmAccountChange',
'PreferencesController:stateChange',
'TokenListController:stateChange',
],
});
this.tokensController = new TokensController({
state: initState.TokensController,
provider: this.provider,
messenger: tokensControllerMessenger,
chainId: this.networkController.state.providerConfig.chainId,
// TODO: The tokens controller currently does not support internalAccounts. This is done to match the behavior of the previous tokens controller subscription.
onPreferencesStateChange: (listener) =>
this.controllerMessenger.subscribe(
`AccountsController:selectedAccountChange`,
(newlySelectedInternalAccount) => {
listener({ selectedAddress: newlySelectedInternalAccount.address });
},
),
onNetworkDidChange: (cb) =>
networkControllerMessenger.subscribe(
'NetworkController:networkDidChange',
() => {
const networkState = this.networkController.state;
return cb(networkState);
},
),
onTokenListStateChange: (listener) =>
this.controllerMessenger.subscribe(
`${this.tokenListController.name}:stateChange`,
listener,
),
getNetworkClientById: this.networkController.getNetworkClientById.bind(
this.networkController,
),
config: {
provider: this.provider,
selectedAddress:
initState.AccountsController?.internalAccounts?.accounts[
initState.AccountsController?.internalAccounts?.selectedAccount
]?.address ?? '',
},
state: initState.TokensController,
});
// TODO: Remove once `TokensController` is upgraded to extend from `BaseControllerV2`
this.controllerMessenger.registerActionHandler(
'TokensController:getState',
() => this.tokensController.state,
);

const nftControllerMessenger = this.controllerMessenger.getRestricted({
name: 'NftController',
allowedActions: [
`${this.approvalController.name}:addRequest`,
`${this.networkController.name}:getNetworkClientById`,
'AccountsController:getSelectedAccount',
'AccountsController:getAccount',
],
allowedEvents: [
'PreferencesController:stateChange',
'NetworkController:networkDidChange',
'AccountsController:selectedEvmAccountChange',
],
});
this.nftController = new NftController(
{
messenger: nftControllerMessenger,
chainId: this.networkController.state.providerConfig.chainId,
onPreferencesStateChange:
this.preferencesController.store.subscribe.bind(
this.preferencesController.store,
),
onNetworkStateChange: networkControllerMessenger.subscribe.bind(
networkControllerMessenger,
'NetworkController:stateChange',
),
getERC721AssetName:
this.assetsContractController.getERC721AssetName.bind(
this.assetsContractController,
),
getERC721AssetSymbol:
this.assetsContractController.getERC721AssetSymbol.bind(
this.assetsContractController,
),
getERC721TokenURI: this.assetsContractController.getERC721TokenURI.bind(
this.nftController = new NftController({
state: initState.NftController,
messenger: nftControllerMessenger,
chainId: this.networkController.state.providerConfig.chainId,
getERC721AssetName: this.assetsContractController.getERC721AssetName.bind(
this.assetsContractController,
),
getERC721AssetSymbol:
this.assetsContractController.getERC721AssetSymbol.bind(
this.assetsContractController,
),
getERC721OwnerOf: this.assetsContractController.getERC721OwnerOf.bind(
getERC721TokenURI: this.assetsContractController.getERC721TokenURI.bind(
this.assetsContractController,
),
getERC721OwnerOf: this.assetsContractController.getERC721OwnerOf.bind(
this.assetsContractController,
),
getERC1155BalanceOf:
this.assetsContractController.getERC1155BalanceOf.bind(
this.assetsContractController,
),
getERC1155BalanceOf:
this.assetsContractController.getERC1155BalanceOf.bind(
this.assetsContractController,
),
getERC1155TokenURI:
this.assetsContractController.getERC1155TokenURI.bind(
this.assetsContractController,
),
onNftAdded: ({ address, symbol, tokenId, standard, source }) =>
this.metaMetricsController.trackEvent({
event: MetaMetricsEventName.NftAdded,
category: MetaMetricsEventCategory.Wallet,
sensitiveProperties: {
token_contract_address: address,
token_symbol: symbol,
token_id: tokenId,
token_standard: standard,
asset_type: AssetType.NFT,
source,
},
}),
getNetworkClientById: this.networkController.getNetworkClientById.bind(
this.networkController,
),
},
{},
initState.NftController,
);
getERC1155TokenURI: this.assetsContractController.getERC1155TokenURI.bind(
this.assetsContractController,
),
onNftAdded: ({ address, symbol, tokenId, standard, source }) =>
this.metaMetricsController.trackEvent({
event: MetaMetricsEventName.NftAdded,
category: MetaMetricsEventCategory.Wallet,
sensitiveProperties: {
token_contract_address: address,
token_symbol: symbol,
token_id: tokenId,
token_standard: standard,
asset_type: AssetType.NFT,
source,
},
}),
});

this.nftController.setApiKey(process.env.OPENSEA_KEY);

const nftDetectionControllerMessenger =
this.controllerMessenger.getRestricted({
name: 'NftDetectionController',
allowedEvents: [
'NetworkController:stateChange',
'PreferencesController:stateChange',
],
allowedActions: [
'ApprovalController:addRequest',
'NetworkController:getState',
'NetworkController:getNetworkClientById',
'AccountsController:getSelectedAccount',
],
});

this.nftDetectionController = new NftDetectionController({
messenger: nftDetectionControllerMessenger,
chainId: this.networkController.state.providerConfig.chainId,
onNftsStateChange: (listener) => this.nftController.subscribe(listener),
onPreferencesStateChange: this.preferencesController.store.subscribe.bind(
this.preferencesController.store,
),
onNetworkStateChange: networkControllerMessenger.subscribe.bind(
networkControllerMessenger,
'NetworkController:stateChange',
),
getOpenSeaApiKey: () => this.nftController.openSeaApiKey,
getBalancesInSingleCall:
this.assetsContractController.getBalancesInSingleCall.bind(
this.assetsContractController,
),
addNft: this.nftController.addNft.bind(this.nftController),
getNftApi: this.nftController.getNftApi.bind(this.nftController),
getNftState: () => this.nftController.state,
// added this to track previous value of useNftDetection, should be true on very first initializing of controller[]
disabled:
this.preferencesController.store.getState().useNftDetection ===
undefined
? false // the detection is enabled by default
: !this.preferencesController.store.getState().useNftDetection,
selectedAddress:
this.preferencesController.store.getState().selectedAddress,
getNetworkClientById: this.networkController.getNetworkClientById.bind(
this.networkController,
),
});

this.metaMetricsController = new MetaMetricsController({
Expand Down Expand Up @@ -941,38 +902,29 @@ export default class MetamaskController extends EventEmitter {
fetchMultiExchangeRate,
});

const tokenRatesMessenger = this.controllerMessenger.getRestricted({
name: 'TokenRatesController',
allowedActions: [
'TokensController:getState',
'NetworkController:getNetworkClientById',
'NetworkController:getState',
'AccountsController:getAccount',
'AccountsController:getSelectedAccount',
],
allowedEvents: [
'NetworkController:stateChange',
'AccountsController:selectedEvmAccountChange',
'PreferencesController:stateChange',
'TokensController:stateChange',
],
});

// token exchange rate tracker
this.tokenRatesController = new TokenRatesController(
{
chainId: this.networkController.state.providerConfig.chainId,
ticker: this.networkController.state.providerConfig.ticker,
selectedAddress: this.accountsController.getSelectedAccount().address,
onTokensStateChange: (listener) =>
this.tokensController.subscribe(listener),
onNetworkStateChange: networkControllerMessenger.subscribe.bind(
networkControllerMessenger,
'NetworkController:stateChange',
),
onPreferencesStateChange: (listener) =>
this.controllerMessenger.subscribe(
`AccountsController:selectedAccountChange`,
(newlySelectedInternalAccount) => {
listener({
selectedAddress: newlySelectedInternalAccount.address,
});
},
),
tokenPricesService: new CodefiTokenPricesServiceV2(),
getNetworkClientById: this.networkController.getNetworkClientById.bind(
this.networkController,
),
},
{
allTokens: this.tokensController.state.allTokens,
allDetectedTokens: this.tokensController.state.allDetectedTokens,
},
initState.TokenRatesController,
);
this.tokenRatesController = new TokenRatesController({
state: initState.TokenRatesController,
messenger: tokenRatesMessenger,
tokenPricesService: new CodefiTokenPricesServiceV2(),
});

this.preferencesController.store.subscribe(
previousValueComparator((prevState, currState) => {
Expand Down Expand Up @@ -1599,6 +1551,7 @@ export default class MetamaskController extends EventEmitter {
this.controllerMessenger.getRestricted({
name: 'TokenDetectionController',
allowedActions: [
'AccountsController:getAccount',
'AccountsController:getSelectedAccount',
'KeyringController:getState',
'NetworkController:getNetworkClientById',
Expand All @@ -1610,7 +1563,7 @@ export default class MetamaskController extends EventEmitter {
'TokensController:addDetectedTokens',
],
allowedEvents: [
'AccountsController:selectedAccountChange',
'AccountsController:selectedEvmAccountChange',
'KeyringController:lock',
'KeyringController:unlock',
'NetworkController:networkDidChange',
Expand Down Expand Up @@ -1696,6 +1649,7 @@ export default class MetamaskController extends EventEmitter {
`${this.approvalController.name}:addRequest`,
'NetworkController:findNetworkClientIdByChainId',
'NetworkController:getNetworkClientById',
'AccountsController:getSelectedAccount',
],
allowedEvents: [`NetworkController:stateChange`],
});
Expand All @@ -1722,8 +1676,6 @@ export default class MetamaskController extends EventEmitter {
this.preferencesController.store.getState().advancedGasFee[
this.networkController.state.providerConfig.chainId
],
getSelectedAddress: () =>
this.accountsController.getSelectedAccount().address,
incomingTransactions: {
includeTokenTransfers: false,
isEnabled: () =>
Expand Down
14 changes: 6 additions & 8 deletions app/scripts/metamask-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1588,14 +1588,12 @@ describe('MetaMaskController', () => {
symbol: 'DAI',
};

metamaskController.tokensController.update({
tokens: [
{
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
...tokenData,
},
],
});
metamaskController.tokensController.addTokens([
{
address: '0x6b175474e89094c44da98b954eedeac495271d0f',
...tokenData,
},
]);

metamaskController.provider = provider;
const tokenDetails =
Expand Down
Loading