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

feat: remove nft detection polling #4281

Merged
merged 15 commits into from
Jun 12, 2024
Merged
24 changes: 23 additions & 1 deletion packages/assets-controllers/src/NftController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,11 @@ describe('NftController', () => {

it('should set default state', () => {
const { nftController } = setupController();

expect(nftController.state).toStrictEqual({
allNftContracts: {},
allNfts: {},
ignoredNfts: [],
isNftFetchingInProgress: {},
});
});

Expand Down Expand Up @@ -4332,4 +4332,26 @@ describe('NftController', () => {

expect(updateNftMetadataSpy).not.toHaveBeenCalled();
});

describe('updateNftFetchingProgressStatus', () => {
it('should update nft fetching status correctly', async () => {
const selectedAddress = OWNER_ADDRESS;
const { nftController } = setupController({
options: {
chainId: ChainId.mainnet,
getERC721AssetName: jest.fn().mockResolvedValue('Name'),
},
});

nftController.updateNftFetchingProgressStatus(true);

expect(
nftController.state.isNftFetchingInProgress[selectedAddress][
ChainId.mainnet
],
).toStrictEqual({
isFetchingInProgress: true,
});
});
});
});
64 changes: 64 additions & 0 deletions packages/assets-controllers/src/NftController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ type NftUpdate = {
newMetadata: NftMetadata;
};

export type NftFetchStatus = {
isFetchingInProgress: boolean;
sahar-fehri marked this conversation as resolved.
Show resolved Hide resolved
};

/**
* @type NftContract
*
Expand Down Expand Up @@ -175,6 +179,7 @@ export type NftMetadata = {
* @property allNftContracts - Object containing NFT contract information
* @property allNfts - Object containing NFTs per account and network
* @property ignoredNfts - List of NFTs that should be ignored
* @property isNftFetchingInProgress - Object containing boolean param indicating whether core finished fetching nfts per account and network
*/
export type NftControllerState = {
allNftContracts: {
Expand All @@ -188,16 +193,21 @@ export type NftControllerState = {
};
};
ignoredNfts: Nft[];
isNftFetchingInProgress: {
[key: string]: { [chainId: Hex]: NftFetchStatus };
sahar-fehri marked this conversation as resolved.
Show resolved Hide resolved
};
};

const nftControllerMetadata = {
allNftContracts: { persist: true, anonymous: false },
allNfts: { persist: true, anonymous: false },
ignoredNfts: { persist: true, anonymous: false },
isNftFetchingInProgress: { persist: true, anonymous: false },
};

const ALL_NFTS_STATE_KEY = 'allNfts';
const ALL_NFTS_CONTRACTS_STATE_KEY = 'allNftContracts';
const IS_NFTS_FETCHING_IN_PROGRESS_KEY = 'isNftFetchingInProgress';

type NftAsset = {
address: string;
Expand Down Expand Up @@ -251,6 +261,7 @@ export const getDefaultNftControllerState = (): NftControllerState => ({
allNftContracts: {},
allNfts: {},
ignoredNfts: [],
isNftFetchingInProgress: {},
});

/**
Expand Down Expand Up @@ -506,6 +517,34 @@ export class NftController extends BaseController<
});
}

/**
* Helper function to update the status of nft fetching for a specific address.
*
* @param newStatus - The new status to set in state
* @param passedConfig - Object containing selectedAddress and chainId
* @param passedConfig.userAddress - the address passed through the detectNfts function
* @param passedConfig.chainId - the chainId passed through the detectNfts function
*/
#updateNestedNftFetchingProgressStatus(
newStatus: boolean,
{ userAddress, chainId }: { userAddress: string; chainId: Hex },
) {
this.update((state) => {
const oldState = state[IS_NFTS_FETCHING_IN_PROGRESS_KEY];
const addressState = oldState[userAddress] || {};
const newAddressState = {
...addressState,
[chainId]: {
isFetchingInProgress: newStatus,
},
};
state[IS_NFTS_FETCHING_IN_PROGRESS_KEY] = {
...oldState,
[userAddress]: newAddressState,
};
});
}

/**
* Request individual NFT information from NFT API.
*
Expand Down Expand Up @@ -1856,6 +1895,31 @@ export class NftController extends BaseController<
});
}

/**
* Update Nft fetching status for a selectedAddress on a specific chain
* @param newValue - fetching status to set in state
* @param passedConfig - Object containing selectedAddress and chainId
* @param passedConfig.userAddress - the address passed through the detectNfts function
* @param passedConfig.networkClientId - the networkClientId passed through the detectNfts function
*/
updateNftFetchingProgressStatus(
newValue: boolean,
{
networkClientId,
userAddress,
}: {
networkClientId?: NetworkClientId;
userAddress?: string;
} = {},
) {
const addressToSearch = this.#getAddressOrSelectedAddress(userAddress);
const chainId = this.#getCorrectChainId({ networkClientId });
this.#updateNestedNftFetchingProgressStatus(newValue, {
userAddress: addressToSearch,
chainId,
});
}

/**
* Resets the transaction status of an NFT.
*
Expand Down
Loading
Loading