Skip to content

Commit

Permalink
Update @metamask/controllers to v33
Browse files Browse the repository at this point in the history
The controllers package has been updated to v33. The only breaking
change in this release was to rename the term "collectible" to "NFT"
wherever it appeared in the API.

Changes in this PR have been kept minimal; additional renaming can be
done in separate PRs. This PR only updates the controller names,
controller state, controller methods, and any direct references to
these things. NFTs are still called "collectibles" in most places.
  • Loading branch information
Gudahtt committed Nov 14, 2022
1 parent dcf79b0 commit 0123ab2
Show file tree
Hide file tree
Showing 35 changed files with 416 additions and 222 deletions.
4 changes: 2 additions & 2 deletions .storybook/initial-states/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,7 @@ export const MOCK_TRANSACTION_BY_TYPE = {
dappSuggestedGasFees: null,
sendFlowHistory: [
{
entry: 'sendFlow - user set asset type to COLLECTIBLE',
entry: 'sendFlow - user set asset type to NFT',
timestamp: 1653457317999,
},
{
Expand Down Expand Up @@ -1504,7 +1504,7 @@ export const MOCK_TRANSACTION_BY_TYPE = {
dappSuggestedGasFees: null,
sendFlowHistory: [
{
entry: 'sendFlow - user set asset type to COLLECTIBLE',
entry: 'sendFlow - user set asset type to NFT',
timestamp: 1653457317999,
},
{
Expand Down
21 changes: 10 additions & 11 deletions app/scripts/controllers/metametrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,15 +706,14 @@ export default class MetaMetricsController {
},
[],
),
[TRAITS.NFT_AUTODETECTION_ENABLED]: metamaskState.useCollectibleDetection,
[TRAITS.NFT_AUTODETECTION_ENABLED]: metamaskState.useNftDetection,
[TRAITS.NUMBER_OF_ACCOUNTS]: Object.values(metamaskState.identities)
.length,
[TRAITS.NUMBER_OF_NFT_COLLECTIONS]: this._getAllUniqueNFTAddressesLength(
metamaskState.allCollectibles,
metamaskState.allNfts,
),
[TRAITS.NUMBER_OF_NFTS]: this._getAllNFTsFlattened(
metamaskState.allCollectibles,
).length,
[TRAITS.NUMBER_OF_NFTS]: this._getAllNFTsFlattened(metamaskState.allNfts)
.length,
[TRAITS.NUMBER_OF_TOKENS]: this._getNumberOfTokens(metamaskState),
[TRAITS.OPENSEA_API_ENABLED]: metamaskState.openSeaEnabled,
[TRAITS.THREE_BOX_ENABLED]: false, // deprecated, hard-coded as false
Expand Down Expand Up @@ -765,11 +764,11 @@ export default class MetaMetricsController {
* Returns an array of all of the collectibles/NFTs the user
* possesses across all networks and accounts.
*
* @param {object} allCollectibles
* @param {object} allNfts
* @returns {[]}
*/
_getAllNFTsFlattened = memoize((allCollectibles = {}) => {
return Object.values(allCollectibles).reduce((result, chainNFTs) => {
_getAllNFTsFlattened = memoize((allNfts = {}) => {
return Object.values(allNfts).reduce((result, chainNFTs) => {
return result.concat(...Object.values(chainNFTs));
}, []);
});
Expand All @@ -778,11 +777,11 @@ export default class MetaMetricsController {
* Returns the number of unique collectible/NFT addresses the user
* possesses across all networks and accounts.
*
* @param {object} allCollectibles
* @param {object} allNfts
* @returns {number}
*/
_getAllUniqueNFTAddressesLength(allCollectibles = {}) {
const allNFTAddresses = this._getAllNFTsFlattened(allCollectibles).map(
_getAllUniqueNFTAddressesLength(allNfts = {}) {
const allNFTAddresses = this._getAllNFTsFlattened(allNfts).map(
(nft) => nft.address,
);
const uniqueAddresses = new Set(allNFTAddresses);
Expand Down
12 changes: 6 additions & 6 deletions app/scripts/controllers/metametrics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ describe('MetaMetricsController', function () {
[CHAIN_IDS.MAINNET]: [{ address: '0x' }],
[CHAIN_IDS.GOERLI]: [{ address: '0x' }, { address: '0x0' }],
},
allCollectibles: {
allNfts: {
'0xac706cE8A9BF27Afecf080fB298d0ee13cfb978A': {
56: [
{
Expand Down Expand Up @@ -752,7 +752,7 @@ describe('MetaMetricsController', function () {
identities: [{}, {}],
ledgerTransportType: 'web-hid',
openSeaEnabled: true,
useCollectibleDetection: false,
useNftDetection: false,
theme: 'default',
useTokenDetection: true,
});
Expand Down Expand Up @@ -790,7 +790,7 @@ describe('MetaMetricsController', function () {
ledgerTransportType: 'web-hid',
openSeaEnabled: true,
identities: [{}, {}],
useCollectibleDetection: false,
useNftDetection: false,
theme: 'default',
useTokenDetection: true,
});
Expand All @@ -810,7 +810,7 @@ describe('MetaMetricsController', function () {
ledgerTransportType: 'web-hid',
openSeaEnabled: false,
identities: [{}, {}, {}],
useCollectibleDetection: false,
useNftDetection: false,
theme: 'default',
useTokenDetection: true,
});
Expand Down Expand Up @@ -838,7 +838,7 @@ describe('MetaMetricsController', function () {
ledgerTransportType: 'web-hid',
openSeaEnabled: true,
identities: [{}, {}],
useCollectibleDetection: true,
useNftDetection: true,
theme: 'default',
useTokenDetection: true,
});
Expand All @@ -856,7 +856,7 @@ describe('MetaMetricsController', function () {
ledgerTransportType: 'web-hid',
openSeaEnabled: true,
identities: [{}, {}],
useCollectibleDetection: true,
useNftDetection: true,
theme: 'default',
useTokenDetection: true,
});
Expand Down
10 changes: 5 additions & 5 deletions app/scripts/controllers/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class PreferencesController {
// set to true means the dynamic list from the API is being used
// set to false will be using the static list from contract-metadata
useTokenDetection: false,
useCollectibleDetection: false,
useNftDetection: false,
openSeaEnabled: false,
advancedGasFee: null,

Expand Down Expand Up @@ -142,12 +142,12 @@ export default class PreferencesController {
}

/**
* Setter for the `useCollectibleDetection` property
* Setter for the `useNftDetection` property
*
* @param {boolean} useCollectibleDetection - Whether or not the user prefers to autodetect collectibles.
* @param {boolean} useNftDetection - Whether or not the user prefers to autodetect collectibles.
*/
setUseCollectibleDetection(useCollectibleDetection) {
this.store.updateState({ useCollectibleDetection });
setUseNftDetection(useNftDetection) {
this.store.updateState({ useNftDetection });
}

/**
Expand Down
12 changes: 6 additions & 6 deletions app/scripts/controllers/preferences.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,21 +308,21 @@ describe('preferences controller', function () {
});
});

describe('setUseCollectibleDetection', function () {
describe('setUseNftDetection', function () {
it('should default to false', function () {
const state = preferencesController.store.getState();
assert.equal(state.useCollectibleDetection, false);
assert.equal(state.useNftDetection, false);
});

it('should set the useCollectibleDetection property in state', function () {
it('should set the useNftDetection property in state', function () {
assert.equal(
preferencesController.store.getState().useCollectibleDetection,
preferencesController.store.getState().useNftDetection,
false,
);
preferencesController.setOpenSeaEnabled(true);
preferencesController.setUseCollectibleDetection(true);
preferencesController.setUseNftDetection(true);
assert.equal(
preferencesController.store.getState().useCollectibleDetection,
preferencesController.store.getState().useNftDetection,
true,
);
});
Expand Down
115 changes: 46 additions & 69 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import {
TokenListController,
TokensController,
TokenRatesController,
CollectiblesController,
NftController,
AssetsContractController,
CollectibleDetectionController,
NftDetectionController,
PermissionController,
SubjectMetadataController,
PermissionsRequestNotFoundError,
Expand Down Expand Up @@ -314,7 +314,7 @@ export default class MetamaskController extends EventEmitter {
initState.AssetsContractController,
);

this.collectiblesController = new CollectiblesController(
this.nftController = new NftController(
{
onPreferencesStateChange:
this.preferencesController.store.subscribe.bind(
Expand Down Expand Up @@ -345,14 +345,14 @@ export default class MetamaskController extends EventEmitter {
this.assetsContractController.getERC1155TokenURI.bind(
this.assetsContractController,
),
onCollectibleAdded: ({ address, symbol, tokenId, standard, source }) =>
onNftAdded: ({ address, symbol, tokenId, standard, source }) =>
this.metaMetricsController.trackEvent({
event: EVENT_NAMES.NFT_ADDED,
category: EVENT.CATEGORIES.WALLET,
properties: {
token_contract_address: address,
token_symbol: symbol,
asset_type: ASSET_TYPES.COLLECTIBLE,
asset_type: ASSET_TYPES.NFT,
token_standard: standard,
source,
},
Expand All @@ -362,34 +362,29 @@ export default class MetamaskController extends EventEmitter {
}),
},
{},
initState.CollectiblesController,
initState.NftController,
);

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

process.env.COLLECTIBLES_V1 &&
(this.collectibleDetectionController = new CollectibleDetectionController(
{
onCollectiblesStateChange: (listener) =>
this.collectiblesController.subscribe(listener),
onPreferencesStateChange:
this.preferencesController.store.subscribe.bind(
this.preferencesController.store,
),
onNetworkStateChange: this.networkController.store.subscribe.bind(
this.networkController.store,
(this.nftDetectionController = new NftDetectionController({
onNftsStateChange: (listener) => this.nftController.subscribe(listener),
onPreferencesStateChange:
this.preferencesController.store.subscribe.bind(
this.preferencesController.store,
),
getOpenSeaApiKey: () => this.collectiblesController.openSeaApiKey,
getBalancesInSingleCall:
this.assetsContractController.getBalancesInSingleCall.bind(
this.assetsContractController,
),
addCollectible: this.collectiblesController.addCollectible.bind(
this.collectiblesController,
onNetworkStateChange: this.networkController.store.subscribe.bind(
this.networkController.store,
),
getOpenSeaApiKey: () => this.nftController.openSeaApiKey,
getBalancesInSingleCall:
this.assetsContractController.getBalancesInSingleCall.bind(
this.assetsContractController,
),
getCollectiblesState: () => this.collectiblesController.state,
},
));
addNft: this.nftController.addNft.bind(this.nftController),
getNftState: () => this.nftController.state,
}));

this.metaMetricsController = new MetaMetricsController({
segment,
Expand Down Expand Up @@ -897,20 +892,18 @@ export default class MetamaskController extends EventEmitter {
const transactionDataTokenId =
getTokenIdParam(transactionData) ??
getTokenValueParam(transactionData);
const { allCollectibles } = this.collectiblesController.state;
const { allNfts } = this.nftController.state;

// check if its a known collectible
const knownCollectible = allCollectibles?.[userAddress]?.[
chainId
].find(
const knownCollectible = allNfts?.[userAddress]?.[chainId].find(
({ address, tokenId }) =>
isEqualCaseInsensitive(address, contractAddress) &&
tokenId === transactionDataTokenId,
);

// if it is we check and update ownership status.
if (knownCollectible) {
this.collectiblesController.checkAndUpdateSingleCollectibleOwnershipStatus(
this.nftController.checkAndUpdateSingleNftOwnershipStatus(
knownCollectible,
false,
{ userAddress, chainId },
Expand Down Expand Up @@ -1058,7 +1051,7 @@ export default class MetamaskController extends EventEmitter {
TokenListController: this.tokenListController,
TokensController: this.tokensController,
SmartTransactionsController: this.smartTransactionsController,
CollectiblesController: this.collectiblesController,
NftController: this.nftController,
///: BEGIN:ONLY_INCLUDE_IN(flask)
SnapController: this.snapController,
CronjobController: this.cronjobController,
Expand Down Expand Up @@ -1100,7 +1093,7 @@ export default class MetamaskController extends EventEmitter {
TokenListController: this.tokenListController,
TokensController: this.tokensController,
SmartTransactionsController: this.smartTransactionsController,
CollectiblesController: this.collectiblesController,
NftController: this.nftController,
///: BEGIN:ONLY_INCLUDE_IN(flask)
SnapController: this.snapController,
CronjobController: this.cronjobController,
Expand Down Expand Up @@ -1504,8 +1497,8 @@ export default class MetamaskController extends EventEmitter {
addressBookController,
alertController,
appStateController,
collectiblesController,
collectibleDetectionController,
nftController,
nftDetectionController,
currencyRateController,
detectTokensController,
ensController,
Expand Down Expand Up @@ -1544,10 +1537,9 @@ export default class MetamaskController extends EventEmitter {
setUseTokenDetection: preferencesController.setUseTokenDetection.bind(
preferencesController,
),
setUseCollectibleDetection:
preferencesController.setUseCollectibleDetection.bind(
preferencesController,
),
setUseNftDetection: preferencesController.setUseNftDetection.bind(
preferencesController,
),
setOpenSeaEnabled: preferencesController.setOpenSeaEnabled.bind(
preferencesController,
),
Expand Down Expand Up @@ -1657,38 +1649,25 @@ export default class MetamaskController extends EventEmitter {
// AssetsContractController
getTokenStandardAndDetails: this.getTokenStandardAndDetails.bind(this),

// CollectiblesController
addCollectible: collectiblesController.addCollectible.bind(
collectiblesController,
),
// NftController
addNft: nftController.addNft.bind(nftController),

addCollectibleVerifyOwnership:
collectiblesController.addCollectibleVerifyOwnership.bind(
collectiblesController,
),
addNftVerifyOwnership:
nftController.addNftVerifyOwnership.bind(nftController),

removeAndIgnoreCollectible:
collectiblesController.removeAndIgnoreCollectible.bind(
collectiblesController,
),
removeAndIgnoreNft: nftController.removeAndIgnoreNft.bind(nftController),

removeCollectible: collectiblesController.removeCollectible.bind(
collectiblesController,
),
removeNft: nftController.removeNft.bind(nftController),

checkAndUpdateAllCollectiblesOwnershipStatus:
collectiblesController.checkAndUpdateAllCollectiblesOwnershipStatus.bind(
collectiblesController,
),
checkAndUpdateAllNftsOwnershipStatus:
nftController.checkAndUpdateAllNftsOwnershipStatus.bind(nftController),

checkAndUpdateSingleCollectibleOwnershipStatus:
collectiblesController.checkAndUpdateSingleCollectibleOwnershipStatus.bind(
collectiblesController,
checkAndUpdateSingleNftOwnershipStatus:
nftController.checkAndUpdateSingleNftOwnershipStatus.bind(
nftController,
),

isCollectibleOwner: collectiblesController.isCollectibleOwner.bind(
collectiblesController,
),
isNftOwner: nftController.isNftOwner.bind(nftController),

// AddressController
setAddressBook: addressBookController.set.bind(addressBookController),
Expand Down Expand Up @@ -1968,10 +1947,8 @@ export default class MetamaskController extends EventEmitter {
),

// DetectCollectibleController
detectCollectibles: process.env.COLLECTIBLES_V1
? collectibleDetectionController.detectCollectibles.bind(
collectibleDetectionController,
)
detectNfts: process.env.COLLECTIBLES_V1
? nftDetectionController.detectNfts.bind(nftDetectionController)
: null,

/** Token Detection V2 */
Expand Down
Loading

0 comments on commit 0123ab2

Please sign in to comment.