Skip to content

Commit

Permalink
Add case insensitive checks for token addresses in collectibles and t…
Browse files Browse the repository at this point in the history
…okens controllers (#537)
  • Loading branch information
adonesky1 authored and MajorLift committed Oct 11, 2023
1 parent 30a126e commit e0c3b74
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
29 changes: 20 additions & 9 deletions src/assets/CollectiblesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ export class CollectiblesController extends BaseController<
const { chainId, selectedAddress } = this.config;
const existingEntry: Collectible | undefined = collectibles.find(
(collectible) =>
collectible.address === address && collectible.tokenId === tokenId,
collectible.address.toLowerCase() === address.toLowerCase() &&
collectible.tokenId === tokenId,
);
/* istanbul ignore next */
collectibleMetadata =
Expand All @@ -398,7 +399,7 @@ export class CollectiblesController extends BaseController<
if (differentMetadata) {
const indexToRemove = collectibles.findIndex(
(collectible) =>
collectible.address === address &&
collectible.address.toLowerCase() === address.toLowerCase() &&
collectible.tokenId === tokenId,
);
/* istanbul ignore next */
Expand Down Expand Up @@ -452,7 +453,8 @@ export class CollectiblesController extends BaseController<
const { allCollectibleContracts, collectibleContracts } = this.state;
const { chainId, selectedAddress } = this.config;
const existingEntry = collectibleContracts.find(
(collectibleContract) => collectibleContract.address === address,
(collectibleContract) =>
collectibleContract.address.toLowerCase() === address.toLowerCase(),
);
if (existingEntry) {
return collectibleContracts;
Expand Down Expand Up @@ -530,7 +532,10 @@ export class CollectiblesController extends BaseController<
const { chainId, selectedAddress } = this.config;
const newIgnoredCollectibles = [...ignoredCollectibles];
const newCollectibles = collectibles.filter((collectible) => {
if (collectible.address === address && collectible.tokenId === tokenId) {
if (
collectible.address.toLowerCase() === address.toLowerCase() &&
collectible.tokenId === tokenId
) {
const alreadyIgnored = newIgnoredCollectibles.find(
(c) => c.address === address && c.tokenId === tokenId,
);
Expand Down Expand Up @@ -567,7 +572,10 @@ export class CollectiblesController extends BaseController<
const { chainId, selectedAddress } = this.config;
const newCollectibles = collectibles.filter(
(collectible) =>
!(collectible.address === address && collectible.tokenId === tokenId),
!(
collectible.address.toLowerCase() === address.toLowerCase() &&
collectible.tokenId === tokenId
),
);
const addressCollectibles = allCollectibles[selectedAddress];
const newAddressCollectibles = {
Expand Down Expand Up @@ -595,7 +603,8 @@ export class CollectiblesController extends BaseController<
const { allCollectibleContracts, collectibleContracts } = this.state;
const { chainId, selectedAddress } = this.config;
const newCollectibleContracts = collectibleContracts.filter(
(collectibleContract) => !(collectibleContract.address === address),
(collectibleContract) =>
!(collectibleContract.address.toLowerCase() === address.toLowerCase()),
);
const addressCollectibleContracts =
allCollectibleContracts[selectedAddress];
Expand Down Expand Up @@ -744,7 +753,7 @@ export class CollectiblesController extends BaseController<

// If collectible contract was not added, do not add individual collectible
const collectibleContract = newCollectibleContracts.find(
(contract) => contract.address === address,
(contract) => contract.address.toLowerCase() === address.toLowerCase(),
);
// If collectible contract information, add individual collectible
if (collectibleContract) {
Expand All @@ -767,7 +776,8 @@ export class CollectiblesController extends BaseController<
this.removeIndividualCollectible(address, tokenId);
const { collectibles } = this.state;
const remainingCollectible = collectibles.find(
(collectible) => collectible.address === address,
(collectible) =>
collectible.address.toLowerCase() === address.toLowerCase(),
);
if (!remainingCollectible) {
this.removeCollectibleContract(address);
Expand All @@ -785,7 +795,8 @@ export class CollectiblesController extends BaseController<
this.removeAndIgnoreIndividualCollectible(address, tokenId);
const { collectibles } = this.state;
const remainingCollectible = collectibles.find(
(collectible) => collectible.address === address,
(collectible) =>
collectible.address.toLowerCase() === address.toLowerCase(),
);
if (!remainingCollectible) {
this.removeCollectibleContract(address);
Expand Down
19 changes: 13 additions & 6 deletions src/assets/TokensController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ export class TokensController extends BaseController<
const { chainId, selectedAddress } = this.config;
const isERC721 = await this._detectIsERC721(address);
const newEntry: Token = { address, symbol, decimals, image, isERC721 };
const previousEntry = tokens.find((token) => token.address === address);
const previousEntry = tokens.find(
(token) => token.address.toLowerCase() === address.toLowerCase(),
);
if (previousEntry) {
const previousIndex = tokens.indexOf(previousEntry);
tokens[previousIndex] = newEntry;
Expand Down Expand Up @@ -270,7 +272,8 @@ export class TokensController extends BaseController<
isERC721,
};
const previousEntry = tokens.find(
(token) => token.address === checksumAddress,
(token) =>
token.address.toLowerCase() === checksumAddress.toLowerCase(),
);
if (previousEntry) {
const previousIndex = tokens.indexOf(previousEntry);
Expand Down Expand Up @@ -306,7 +309,7 @@ export class TokensController extends BaseController<
const isERC721 = await this._detectIsERC721(tokenAddress);
const { tokens } = this.state;
const tokenIndex = tokens.findIndex((token) => {
return token.address === tokenAddress;
return token.address.toLowerCase() === tokenAddress.toLowerCase();
});
tokens[tokenIndex].isERC721 = isERC721;
this.update({ tokens });
Expand Down Expand Up @@ -491,17 +494,21 @@ export class TokensController extends BaseController<
const { chainId, selectedAddress } = this.config;
const newIgnoredTokens = [...ignoredTokens];
const newTokens = tokens.filter((token) => {
if (token.address === address) {
if (token.address.toLowerCase() === address.toLowerCase()) {
const alreadyIgnored = newIgnoredTokens.find(
(t) => t.address === address,
(t) => t.address.toLowerCase() === address.toLowerCase(),
);
!alreadyIgnored && newIgnoredTokens.push(token);
return false;
}
return true;
});

if (!newIgnoredTokens.find((token: Token) => token.address === address)) {
if (
!newIgnoredTokens.find(
(token: Token) => token.address.toLowerCase() === address.toLowerCase(),
)
) {
newIgnoredTokens.push({ address, decimals: 0, symbol: '' });
}

Expand Down

0 comments on commit e0c3b74

Please sign in to comment.