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

fix: fix update nft metadata when toggles off #4096

Merged
merged 6 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
70 changes: 7 additions & 63 deletions packages/assets-controllers/src/NftController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import { v4 } from 'uuid';

import { getFormattedIpfsUrl } from './assetsUtil';
import { Source } from './constants';
import type { Nft } from './NftController';
import { NftController } from './NftController';

const CRYPTOPUNK_ADDRESS = '0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB';
Expand Down Expand Up @@ -3446,7 +3445,12 @@ describe('NftController', () => {
const spy = jest.spyOn(nftController, 'updateNft');
const testNetworkClientId = 'sepolia';
await nftController.addNft('0xtest', '3', {
nftMetadata: { name: '', description: '', image: '', standard: '' },
nftMetadata: {
name: '',
description: '',
image: '',
standard: 'ERC721',
},
networkClientId: testNetworkClientId,
});
sinon
Expand All @@ -3455,23 +3459,10 @@ describe('NftController', () => {
name: 'name pudgy',
image: 'url pudgy',
description: 'description pudgy',
});
const testInputNfts: Nft[] = [
{
address: '0xtest',
description: null,
favorite: false,
image: null,
isCurrentlyOwned: true,
name: null,
standard: 'ERC721',
tokenId: '3',
tokenURI: 'https://api.pudgypenguins.io/lil/4',
},
];
});

await nftController.updateNftMetadata({
nfts: testInputNfts,
networkClientId: testNetworkClientId,
});
expect(spy).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -3508,21 +3499,8 @@ describe('NftController', () => {
sinon
.stub(nftController, 'getNftInformation' as keyof typeof nftController)
.rejects(new Error('Error'));
const testInputNfts: Nft[] = [
{
address: '0xtest',
description: null,
favorite: false,
image: null,
isCurrentlyOwned: true,
name: null,
standard: 'ERC721',
tokenId: '3',
},
];

await nftController.updateNftMetadata({
nfts: testInputNfts,
networkClientId: testNetworkClientId,
});

Expand Down Expand Up @@ -3591,41 +3569,7 @@ describe('NftController', () => {
.onThirdCall()
.rejects(new Error('Error'));

const testInputNfts: Nft[] = [
{
address: '0xtest1',
description: null,
favorite: false,
image: null,
isCurrentlyOwned: true,
name: null,
standard: 'ERC721',
tokenId: '1',
},
{
address: '0xtest2',
description: null,
favorite: false,
image: null,
isCurrentlyOwned: true,
name: null,
standard: 'ERC721',
tokenId: '2',
},
{
address: '0xtest3',
description: null,
favorite: false,
image: null,
isCurrentlyOwned: true,
name: null,
standard: 'ERC721',
tokenId: '3',
},
];

await nftController.updateNftMetadata({
nfts: testInputNfts,
networkClientId: testNetworkClientId,
});

Expand Down
24 changes: 18 additions & 6 deletions packages/assets-controllers/src/NftController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,13 @@ export class NftController extends BaseControllerV1<NftConfig, NftState> {
openSeaEnabled,
isIpfsGatewayEnabled,
});

const needsUpdateNftMetadata =
(isIpfsGatewayEnabled && ipfsGateway !== '') || openSeaEnabled;

if (needsUpdateNftMetadata) {
this.updateNftMetadata({ userAddress: selectedAddress });
sahar-fehri marked this conversation as resolved.
Show resolved Hide resolved
}
sahar-fehri marked this conversation as resolved.
Show resolved Hide resolved
},
);

Expand Down Expand Up @@ -1497,21 +1504,26 @@ export class NftController extends BaseControllerV1<NftConfig, NftState> {
* Refetches NFT metadata and updates the state
*
* @param options - Options for refetching NFT metadata
* @param options.nfts - Array of nfts
* @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
* @param options.userAddress - The current user address
*/
async updateNftMetadata({
nfts,
networkClientId,
userAddress = this.config.selectedAddress,
networkClientId,
}: {
nfts: Nft[];
networkClientId?: NetworkClientId;
userAddress?: string;
networkClientId?: NetworkClientId;
}) {
const chainId = this.getCorrectChainId({ networkClientId });
const nftsWithChecksumAdr = nfts.map((nft) => {
sahar-fehri marked this conversation as resolved.
Show resolved Hide resolved
const { allNfts } = this.state;
const nfts: Nft[] = allNfts[userAddress]?.[chainId] || [];
// We want to update only nfts missing name/image or description
const nftsToUpdate = nfts.filter(
(singleNft) =>
!singleNft.name && !singleNft.description && !singleNft.image,
bergeron marked this conversation as resolved.
Show resolved Hide resolved
);

const nftsWithChecksumAdr = nftsToUpdate.map((nft) => {
return {
...nft,
address: toChecksumHexAddress(nft.address),
Expand Down
Loading