Skip to content

Commit

Permalink
Slicing tokensToDetect list to check for balance (#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
NiranjanaBinoy authored and MajorLift committed Oct 11, 2023
1 parent 7a1af89 commit 45389de
Showing 1 changed file with 42 additions and 31 deletions.
73 changes: 42 additions & 31 deletions src/assets/AssetsDetectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,47 +332,58 @@ export class AssetsDetectionController extends BaseController<
tokensToDetect.push(address);
}
}
const sliceOfTokensToDetect = [];
sliceOfTokensToDetect[0] = tokensToDetect.slice(0, 1000);
sliceOfTokensToDetect[1] = tokensToDetect.slice(
1000,
tokensToDetect.length - 1,
);

const { selectedAddress } = this.config;
/* istanbul ignore else */
if (!selectedAddress) {
return;
}

await safelyExecute(async () => {
const balances = await this.getBalancesInSingleCall(
selectedAddress,
tokensToDetect,
);
const tokensToAdd = [];
for (const tokenAddress in balances) {
let ignored;
/* istanbul ignore else */
const { ignoredTokens } = this.getTokensState();
if (ignoredTokens.length) {
ignored = ignoredTokens.find(
(ignoredTokenAddress) =>
ignoredTokenAddress === toChecksumHexAddress(tokenAddress),
);
}
const caseInsensitiveTokenKey =
Object.keys(tokenList).find(
(i) => i.toLowerCase() === tokenAddress.toLowerCase(),
) || '';
for (const tokensSlice of sliceOfTokensToDetect) {
if (tokensSlice.length === 0) {
break;
}
await safelyExecute(async () => {
const balances = await this.getBalancesInSingleCall(
selectedAddress,
tokensSlice,
);
const tokensToAdd = [];
for (const tokenAddress in balances) {
let ignored;
/* istanbul ignore else */
const { ignoredTokens } = this.getTokensState();
if (ignoredTokens.length) {
ignored = ignoredTokens.find(
(ignoredTokenAddress) =>
ignoredTokenAddress === toChecksumHexAddress(tokenAddress),
);
}
const caseInsensitiveTokenKey =
Object.keys(tokenList).find(
(i) => i.toLowerCase() === tokenAddress.toLowerCase(),
) || '';

if (ignored === undefined) {
tokensToAdd.push({
address: tokenAddress,
decimals: tokenList[caseInsensitiveTokenKey].decimals,
symbol: tokenList[caseInsensitiveTokenKey].symbol,
});
if (ignored === undefined) {
tokensToAdd.push({
address: tokenAddress,
decimals: tokenList[caseInsensitiveTokenKey].decimals,
symbol: tokenList[caseInsensitiveTokenKey].symbol,
});
}
}
}

if (tokensToAdd.length) {
await this.addTokens(tokensToAdd);
}
});
if (tokensToAdd.length) {
await this.addTokens(tokensToAdd);
}
});
}
}

/**
Expand Down

0 comments on commit 45389de

Please sign in to comment.