1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 11:22:43 +02:00

MetaMetrics: add 'number_of_nfts' user trait (#14495)

* MetaMetrics: add number_of_nfts trait

* MetaMetrics: memoize _getAllNFTAddresses

* MetaMetrics: fix lint / update JSDoc comments

* MetaMetrics: update nft trait logic
- https://github.com/MetaMask/metamask-extension/pull/14495#discussion_r855450898

* MetaMetrics: rn _getAllNFTs > _getAllNFTsFlattened
This commit is contained in:
Ariella Vu 2022-04-21 18:46:12 -03:00 committed by GitHub
parent fbeae57247
commit d0ed2c1afc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 19 deletions

View File

@ -1,4 +1,13 @@
import { isEqual, merge, omit, omitBy, pickBy, size, sum } from 'lodash';
import {
isEqual,
memoize,
merge,
omit,
omitBy,
pickBy,
size,
sum,
} from 'lodash';
import { ObservableStore } from '@metamask/obs-store';
import { bufferToHex, keccak } from 'ethereumjs-util';
import { generateUUID } from 'pubnub';
@ -540,9 +549,7 @@ export default class MetaMetricsController {
* @returns {MetaMetricsTraits | null} traits that have changed since last update
*/
_buildUserTraitsObject(metamaskState) {
/**
* @type {MetaMetricsTraits}
*/
/** @type {MetaMetricsTraits} */
const currentTraits = {
[TRAITS.ADDRESS_BOOK_ENTRIES]: sum(
Object.values(metamaskState.addressBook).map(size),
@ -554,9 +561,12 @@ export default class MetaMetricsController {
[TRAITS.NFT_AUTODETECTION_ENABLED]: metamaskState.useCollectibleDetection,
[TRAITS.NUMBER_OF_ACCOUNTS]: Object.values(metamaskState.identities)
.length,
[TRAITS.NUMBER_OF_NFT_COLLECTIONS]: this._getNumberOfNFtCollection(
metamaskState,
[TRAITS.NUMBER_OF_NFT_COLLECTIONS]: this._getAllUniqueNFTAddressesLength(
metamaskState.allCollectibles,
),
[TRAITS.NUMBER_OF_NFTS]: this._getAllNFTsFlattened(
metamaskState.allCollectibles,
).length,
[TRAITS.NUMBER_OF_TOKENS]: this._getNumberOfTokens(metamaskState),
[TRAITS.OPENSEA_API_ENABLED]: metamaskState.openSeaEnabled,
[TRAITS.THREE_BOX_ENABLED]: metamaskState.threeBoxSyncingAllowed,
@ -603,22 +613,31 @@ export default class MetaMetricsController {
}
/**
* Returns an array of all of the collectibles/NFTs the user
* possesses across all networks and accounts.
*
* @param {object} metamaskState
* @returns number of unique collectible addresses
* @param {Object} allCollectibles
* @returns {[]}
*/
_getNumberOfNFtCollection(metamaskState) {
const { allCollectibles } = metamaskState;
if (!allCollectibles) {
return 0;
}
_getAllNFTsFlattened = memoize((allCollectibles = {}) => {
return Object.values(allCollectibles)
.flatMap((chainNFTs) => Object.values(chainNFTs))
.flat();
});
const allAddresses = Object.values(allCollectibles)
.flatMap((chainCollectibles) => Object.values(chainCollectibles))
.flat()
.map((collectible) => collectible.address);
const unique = [...new Set(allAddresses)];
return unique.length;
/**
* Returns the number of unique collectible/NFT addresses the user
* possesses across all networks and accounts.
*
* @param {Object} allCollectibles
* @returns {number}
*/
_getAllUniqueNFTAddressesLength(allCollectibles = {}) {
const allNFTAddresses = this._getAllNFTsFlattened(allCollectibles).map(
(nft) => nft.address,
);
const uniqueAddresses = new Set(allNFTAddresses);
return uniqueAddresses.size;
}
/**

View File

@ -691,6 +691,7 @@ describe('MetaMetricsController', function () {
[TRAITS.NFT_AUTODETECTION_ENABLED]: false,
[TRAITS.NUMBER_OF_ACCOUNTS]: 2,
[TRAITS.NUMBER_OF_NFT_COLLECTIONS]: 3,
[TRAITS.NUMBER_OF_NFTS]: 4,
[TRAITS.NUMBER_OF_TOKENS]: 5,
[TRAITS.OPENSEA_API_ENABLED]: true,
[TRAITS.THREE_BOX_ENABLED]: false,

View File

@ -170,6 +170,7 @@
* change, we identify the new number_of_accounts trait
* @property {'number_of_nft_collections'} NUMBER_OF_NFT_COLLECTIONS - user
* trait for number of unique NFT addresses
* @property {'number_of_nfts'} NUMBER_OF_NFTS - user trait for number of all NFT addresses
* @property {'number_of_tokens'} NUMBER_OF_TOKENS - when the number of tokens change, we
* identify the new number_of_tokens trait
* @property {'opensea_api_enabled'} OPENSEA_API_ENABLED - when the OpenSea API is enabled
@ -191,6 +192,7 @@ export const TRAITS = {
NFT_AUTODETECTION_ENABLED: 'nft_autodetection_enabled',
NUMBER_OF_ACCOUNTS: 'number_of_accounts',
NUMBER_OF_NFT_COLLECTIONS: 'number_of_nft_collections',
NUMBER_OF_NFTS: 'number_of_nfts',
NUMBER_OF_TOKENS: 'number_of_tokens',
OPENSEA_API_ENABLED: 'opensea_api_enabled',
THREE_BOX_ENABLED: 'three_box_enabled',
@ -211,6 +213,8 @@ export const TRAITS = {
* of identities(accounts) added to the user's MetaMask.
* @property {number} [number_of_nft_collections] - A number representing the
* amount of different NFT collections the user possesses an NFT from.
* @property {number} [number_of_nfts] - A number representing the
* amount of all NFTs the user possesses across all networks and accounts.
* @property {number} [number_of_tokens] - The total number of token contracts
* the user has across all networks and accounts.
* @property {boolean} [opensea_api_enabled] - does the user have the OpenSea