Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion packages/assets-controllers/src/NftController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ describe('NftController', () => {

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

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

Expand Down Expand Up @@ -3918,4 +3918,27 @@ describe('NftController', () => {
expect(spy).toHaveBeenCalledTimes(1);
});
});

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

nftController.updateNftFetchingProgressStatus(true);

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

export type NftFetchStatus = {
isFetchingInProgress: boolean;
};

/**
* @type NftContract
*
Expand Down Expand Up @@ -169,6 +173,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 @@ -182,16 +187,21 @@ export type NftControllerState = {
};
};
ignoredNfts: Nft[];
isNftFetchingInProgress: {
[key: string]: { [chainId: Hex]: NftFetchStatus };
};
};

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 @@ -242,6 +252,7 @@ export const getDefaultNftControllerState = (): NftControllerState => ({
allNftContracts: {},
allNfts: {},
ignoredNfts: [],
isNftFetchingInProgress: {},
});

/**
Expand Down Expand Up @@ -480,6 +491,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 @@ -1812,6 +1851,29 @@ 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,
{
userAddress = this.#selectedAddress,
networkClientId,
}: { networkClientId?: NetworkClientId; userAddress?: string } = {
userAddress: this.#selectedAddress,
},
) {
const chainId = this.#getCorrectChainId({ networkClientId });
this.#updateNestedNftFetchingProgressStatus(newValue, {
userAddress,
chainId,
});
}

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