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
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;
sahar-fehri marked this conversation as resolved.
Show resolved Hide resolved
};

/**
* @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 };
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 @@ -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
Loading