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

Parallelize network requests in assets controllers #1801

Merged
merged 5 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions packages/assets-controllers/src/NftController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2701,6 +2701,7 @@ describe('NftController', () => {
await nftController.addNft(
ERC721_DEPRESSIONIST_ADDRESS,
ERC721_DEPRESSIONIST_ID,
{ nftMetadata: { name: '', description: '', image: '', standard: '' } },
);

nftController.updateNftFavoriteStatus(
Expand All @@ -2727,6 +2728,7 @@ describe('NftController', () => {
await nftController.addNft(
ERC721_DEPRESSIONIST_ADDRESS,
ERC721_DEPRESSIONIST_ID,
{ nftMetadata: { name: '', description: '', image: '', standard: '' } },
);

nftController.updateNftFavoriteStatus(
Expand Down Expand Up @@ -2769,6 +2771,7 @@ describe('NftController', () => {
await nftController.addNft(
ERC721_DEPRESSIONIST_ADDRESS,
ERC721_DEPRESSIONIST_ID,
{ nftMetadata: { name: '', description: '', image: '', standard: '' } },
);

nftController.updateNftFavoriteStatus(
Expand Down Expand Up @@ -2826,6 +2829,7 @@ describe('NftController', () => {
await nftController.addNft(
ERC721_DEPRESSIONIST_ADDRESS,
ERC721_DEPRESSIONIST_ID,
{ nftMetadata: { name: '', description: '', image: '', standard: '' } },
);

expect(
Expand Down
75 changes: 37 additions & 38 deletions packages/assets-controllers/src/NftController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,21 +490,20 @@ export class NftController extends BaseController<NftConfig, NftState> {
this.getNetworkClientById(networkClientId).configuration.chainId;
}

const blockchainMetadata = await safelyExecute(async () => {
return await this.getNftInformationFromTokenURI(
contractAddress,
tokenId,
networkClientId,
);
});

let openSeaMetadata;
// currently we only need to enter this block if we are on mainnet
if (this.config.openSeaEnabled && chainId === '0x1') {
openSeaMetadata = await safelyExecute(async () => {
return await this.getNftInformationFromApi(contractAddress, tokenId);
});
}
const [blockchainMetadata, openSeaMetadata] = await Promise.all([
safelyExecute(() =>
this.getNftInformationFromTokenURI(
contractAddress,
tokenId,
networkClientId,
),
),
this.config.openSeaEnabled && chainId === '0x1'
? safelyExecute(() =>
this.getNftInformationFromApi(contractAddress, tokenId),
)
: undefined,
]);

return {
...openSeaMetadata,
Expand Down Expand Up @@ -573,14 +572,11 @@ export class NftController extends BaseController<NftConfig, NftState> {
Pick<ApiNftContract, 'address'> &
Pick<ApiNftContract, 'collection'>
> {
const name = await this.getERC721AssetName(
contractAddress,
networkClientId,
);
const symbol = await this.getERC721AssetSymbol(
contractAddress,
networkClientId,
);
const [name, symbol] = await Promise.all([
this.getERC721AssetName(contractAddress, networkClientId),
this.getERC721AssetSymbol(contractAddress, networkClientId),
]);
bergeron marked this conversation as resolved.
Show resolved Hide resolved

return {
collection: { name },
symbol,
Expand All @@ -603,27 +599,30 @@ export class NftController extends BaseController<NftConfig, NftState> {
Pick<ApiNftContract, 'address'> &
Pick<ApiNftContract, 'collection'>
> {
const blockchainContractData: Partial<ApiNftContract> &
Pick<ApiNftContract, 'address'> &
Pick<ApiNftContract, 'collection'> = await safelyExecute(async () => {
return await this.getNftContractInformationFromContract(
contractAddress,
networkClientId,
);
});

const { chainId } = this.config;
const getCurrentChainId = this.getCorrectChainId({
chainId,
networkClientId,
});

let openSeaContractData: Partial<ApiNftContract> | undefined;
if (this.config.openSeaEnabled && getCurrentChainId === '0x1') {
openSeaContractData = await safelyExecute(async () => {
return await this.getNftContractInformationFromApi(contractAddress);
});
}
const [blockchainContractData, openSeaContractData]: [
Partial<ApiNftContract> &
Pick<ApiNftContract, 'address'> &
Pick<ApiNftContract, 'collection'>,
Partial<ApiNftContract> | undefined,
] = await Promise.all([
safelyExecute(() =>
this.getNftContractInformationFromContract(
contractAddress,
networkClientId,
),
),
this.config.openSeaEnabled && getCurrentChainId === '0x1'
? safelyExecute(() =>
this.getNftContractInformationFromApi(contractAddress),
)
: undefined,
]);

if (blockchainContractData || openSeaContractData) {
return {
Expand Down
7 changes: 2 additions & 5 deletions packages/assets-controllers/src/Standards/ERC20Standard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,11 @@ export class ERC20Standard {
decimals: string | undefined;
balance: BN | undefined;
}> {
const [decimals, symbol] = await Promise.all([
const [decimals, symbol, balance] = await Promise.all([
bergeron marked this conversation as resolved.
Show resolved Hide resolved
this.getTokenDecimals(address),
this.getTokenSymbol(address),
userAddress ? this.getBalanceOf(address, userAddress) : undefined,
]);
let balance;
if (userAddress) {
balance = await this.getBalanceOf(address, userAddress);
}
return {
decimals,
symbol,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ERC721_METADATA_INTERFACE_ID,
ERC721_ENUMERABLE_INTERFACE_ID,
ERC721,
safelyExecute,
} from '@metamask/controller-utils';
import { abiERC721 } from '@metamask/metamask-eth-abis';

Expand Down Expand Up @@ -176,28 +177,23 @@ export class ERC721Standard {
throw new Error("This isn't a valid ERC721 contract");
}

let tokenURI, image, symbol, name;

// TODO upgrade to use Promise.allSettled for name/symbol when we can refactor to use es2020 in tsconfig
try {
symbol = await this.getAssetSymbol(address);
} catch {
// ignore
}

try {
name = await this.getAssetName(address);
} catch {
// ignore
}

if (tokenId) {
const [symbol, name, tokenURI] = await Promise.all([
bergeron marked this conversation as resolved.
Show resolved Hide resolved
safelyExecute(() => this.getAssetSymbol(address)),
safelyExecute(() => this.getAssetName(address)),
tokenId
? safelyExecute(() =>
this.getTokenURI(address, tokenId).then((uri) =>
uri.startsWith('ipfs://')
? getFormattedIpfsUrl(ipfsGateway, uri, true)
: uri,
),
)
: undefined,
]);

let image;
if (tokenURI) {
try {
tokenURI = await this.getTokenURI(address, tokenId);
if (tokenURI.startsWith('ipfs://')) {
tokenURI = getFormattedIpfsUrl(ipfsGateway, tokenURI, true);
}

const response = await timeoutFetch(tokenURI);
const object = await response.json();
image = object?.image;
Expand Down
Loading