@toruslabs/ethereum-controllers 7.2.1 → 7.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22455,23 +22455,6 @@ class src_TokenRatesController extends src_base_controllers_namespaceObject.Base
22455
22455
 
22456
22456
 
22457
22457
 
22458
- function src_getObjectFromArrayBasedonKey(oldArray, key) {
22459
- return oldArray.reduce((acc, x) => {
22460
- const xkey = x[key];
22461
- if (typeof xkey === "boolean") return acc;
22462
- acc[xkey] = x;
22463
- return acc;
22464
- }, {});
22465
- }
22466
- const src_mergeTokenArrays = (oldArray, newArray) => {
22467
- const oldMap = src_getObjectFromArrayBasedonKey(oldArray || [], "tokenAddress");
22468
- const newMap = src_getObjectFromArrayBasedonKey(newArray || [], "tokenAddress");
22469
- const finalArr = newArray;
22470
- Object.keys(oldMap).forEach(x => {
22471
- if (!newMap[x] && oldMap[x].isEtherScan) finalArr.push(oldMap[x]);
22472
- });
22473
- return finalArr;
22474
- };
22475
22458
  const src_TokensController_DEFAULT_INTERVAL = 180 * 1000;
22476
22459
  class src_TokensController extends src_base_controllers_namespaceObject.BaseController {
22477
22460
  constructor({
@@ -22624,45 +22607,58 @@ class src_TokensController extends src_base_controllers_namespaceObject.BaseCont
22624
22607
  const userAddress = this.userSelectedAddress;
22625
22608
  if (userAddress === "") return;
22626
22609
  const oldTokens = [...this.userTokens];
22627
- const tokenAddresses = oldTokens.map(x => x.tokenAddress);
22628
- const nonZeroTokens = [];
22610
+ // - fetch from Etherscan, if failed then fetch from TokenHandler
22611
+ // - if there are remaining tokens, fetch either from SingleAddressContract (if failed then use TokenHandler) or TokenHandler
22629
22612
  try {
22613
+ const nonZeroTokens = [];
22630
22614
  const currentChainId = this.config.chainId;
22631
22615
  if (src_ETHERSCAN_SUPPORTED_CHAINS.includes(currentChainId)) {
22632
22616
  const etherscanBalances = await this.getEtherScanTokens(userAddress, currentChainId, skipCache);
22633
22617
  nonZeroTokens.push(...etherscanBalances);
22634
22618
  }
22635
- if (tokenAddresses.length > 0) {
22619
+
22620
+ // only fetch balances for tokens that are not already in the nonZeroTokens array
22621
+ const remainingTokens = oldTokens.filter(x => !nonZeroTokens.find(y => {
22622
+ var _y$tokenAddress, _x$tokenAddress;
22623
+ return ((_y$tokenAddress = y.tokenAddress) === null || _y$tokenAddress === void 0 ? void 0 : _y$tokenAddress.toLocaleLowerCase()) === ((_x$tokenAddress = x.tokenAddress) === null || _x$tokenAddress === void 0 ? void 0 : _x$tokenAddress.toLocaleLowerCase());
22624
+ }));
22625
+ if (remainingTokens.length > 0) {
22626
+ const remainingTokensAddresses = remainingTokens.map(x => x.tokenAddress);
22636
22627
  const currentSingleCallAddress = src_SINGLE_CALL_BALANCES_ADDRESSES[currentChainId];
22637
22628
  if (currentSingleCallAddress) {
22638
22629
  const ethContract = new src_external_ethers_namespaceObject.Contract(currentSingleCallAddress, src_singleBalanceCheckerAbi, this.ethersProvider);
22639
- const result = await ethContract.balances([userAddress], tokenAddresses);
22640
- tokenAddresses.forEach((_, index) => {
22630
+ const result = await ethContract.balances([userAddress], remainingTokensAddresses);
22631
+ remainingTokensAddresses.forEach((_, index) => {
22641
22632
  const balance = (0,src_external_ethers_namespaceObject.toQuantity)(result[index]);
22642
22633
  if (balance && balance !== "0x0") {
22643
- nonZeroTokens.push(src_objectSpread2_default()(src_objectSpread2_default()({}, oldTokens[index]), {}, {
22634
+ nonZeroTokens.push(src_objectSpread2_default()(src_objectSpread2_default()({}, remainingTokens[index]), {}, {
22644
22635
  balance,
22645
22636
  chainId: currentChainId
22646
22637
  }));
22647
22638
  }
22648
22639
  });
22649
22640
  } else {
22650
- this.getTokenBalancesUsingHandler(oldTokens);
22641
+ const fetchedRemainingTokens = await this.getTokenBalancesUsingHandler(remainingTokens);
22642
+ nonZeroTokens.push(...fetchedRemainingTokens);
22651
22643
  }
22652
22644
  }
22645
+ this.update({
22646
+ tokens: {
22647
+ [userAddress]: nonZeroTokens
22648
+ }
22649
+ });
22653
22650
  } catch (error) {
22654
22651
  src_external_loglevel_default().error(error, "unable to fetch token balances using single call balance address");
22655
- this.getTokenBalancesUsingHandler(oldTokens);
22656
- } finally {
22652
+ const fetchedTokens = await this.getTokenBalancesUsingHandler(oldTokens);
22657
22653
  this.update({
22658
22654
  tokens: {
22659
- [userAddress]: nonZeroTokens
22655
+ [userAddress]: fetchedTokens
22660
22656
  }
22661
22657
  });
22662
22658
  }
22663
22659
  }
22664
22660
  async getTokenBalancesUsingHandler(customTokens) {
22665
- if (!this.userSelectedAddress) return;
22661
+ if (!this.userSelectedAddress) return [];
22666
22662
  const currentNetworkTokens = customTokens;
22667
22663
  const promiseSettledResult = await Promise.allSettled(currentNetworkTokens.map(async x => {
22668
22664
  try {
@@ -22695,11 +22691,7 @@ class src_TokensController extends src_base_controllers_namespaceObject.BaseCont
22695
22691
  if (x.status === "fulfilled" && x.value) acc.push(x.value);
22696
22692
  return acc;
22697
22693
  }, []);
22698
- this.update({
22699
- tokens: {
22700
- [this.userSelectedAddress]: src_mergeTokenArrays(this.userTokens, nonZeroTokens)
22701
- }
22702
- });
22694
+ return nonZeroTokens;
22703
22695
  }
22704
22696
  }
22705
22697
  ;// ./src/Transaction/NonceTracker.ts
@@ -4815,23 +4815,6 @@ class TokenRatesController extends BaseController {
4815
4815
  }
4816
4816
  }
4817
4817
 
4818
- function getObjectFromArrayBasedonKey(oldArray, key) {
4819
- return oldArray.reduce((acc, x) => {
4820
- const xkey = x[key];
4821
- if (typeof xkey === "boolean") return acc;
4822
- acc[xkey] = x;
4823
- return acc;
4824
- }, {});
4825
- }
4826
- const mergeTokenArrays = (oldArray, newArray) => {
4827
- const oldMap = getObjectFromArrayBasedonKey(oldArray || [], "tokenAddress");
4828
- const newMap = getObjectFromArrayBasedonKey(newArray || [], "tokenAddress");
4829
- const finalArr = newArray;
4830
- Object.keys(oldMap).forEach(x => {
4831
- if (!newMap[x] && oldMap[x].isEtherScan) finalArr.push(oldMap[x]);
4832
- });
4833
- return finalArr;
4834
- };
4835
4818
  const DEFAULT_INTERVAL = 180 * 1000;
4836
4819
  class TokensController extends BaseController {
4837
4820
  constructor({
@@ -4984,45 +4967,58 @@ class TokensController extends BaseController {
4984
4967
  const userAddress = this.userSelectedAddress;
4985
4968
  if (userAddress === "") return;
4986
4969
  const oldTokens = [...this.userTokens];
4987
- const tokenAddresses = oldTokens.map(x => x.tokenAddress);
4988
- const nonZeroTokens = [];
4970
+ // - fetch from Etherscan, if failed then fetch from TokenHandler
4971
+ // - if there are remaining tokens, fetch either from SingleAddressContract (if failed then use TokenHandler) or TokenHandler
4989
4972
  try {
4973
+ const nonZeroTokens = [];
4990
4974
  const currentChainId = this.config.chainId;
4991
4975
  if (ETHERSCAN_SUPPORTED_CHAINS.includes(currentChainId)) {
4992
4976
  const etherscanBalances = await this.getEtherScanTokens(userAddress, currentChainId, skipCache);
4993
4977
  nonZeroTokens.push(...etherscanBalances);
4994
4978
  }
4995
- if (tokenAddresses.length > 0) {
4979
+
4980
+ // only fetch balances for tokens that are not already in the nonZeroTokens array
4981
+ const remainingTokens = oldTokens.filter(x => !nonZeroTokens.find(y => {
4982
+ var _y$tokenAddress, _x$tokenAddress;
4983
+ return ((_y$tokenAddress = y.tokenAddress) === null || _y$tokenAddress === void 0 ? void 0 : _y$tokenAddress.toLocaleLowerCase()) === ((_x$tokenAddress = x.tokenAddress) === null || _x$tokenAddress === void 0 ? void 0 : _x$tokenAddress.toLocaleLowerCase());
4984
+ }));
4985
+ if (remainingTokens.length > 0) {
4986
+ const remainingTokensAddresses = remainingTokens.map(x => x.tokenAddress);
4996
4987
  const currentSingleCallAddress = SINGLE_CALL_BALANCES_ADDRESSES[currentChainId];
4997
4988
  if (currentSingleCallAddress) {
4998
4989
  const ethContract = new Contract(currentSingleCallAddress, singleBalanceCheckerAbi, this.ethersProvider);
4999
- const result = await ethContract.balances([userAddress], tokenAddresses);
5000
- tokenAddresses.forEach((_, index) => {
4990
+ const result = await ethContract.balances([userAddress], remainingTokensAddresses);
4991
+ remainingTokensAddresses.forEach((_, index) => {
5001
4992
  const balance = toQuantity(result[index]);
5002
4993
  if (balance && balance !== "0x0") {
5003
- nonZeroTokens.push(_objectSpread(_objectSpread({}, oldTokens[index]), {}, {
4994
+ nonZeroTokens.push(_objectSpread(_objectSpread({}, remainingTokens[index]), {}, {
5004
4995
  balance,
5005
4996
  chainId: currentChainId
5006
4997
  }));
5007
4998
  }
5008
4999
  });
5009
5000
  } else {
5010
- this.getTokenBalancesUsingHandler(oldTokens);
5001
+ const fetchedRemainingTokens = await this.getTokenBalancesUsingHandler(remainingTokens);
5002
+ nonZeroTokens.push(...fetchedRemainingTokens);
5011
5003
  }
5012
5004
  }
5005
+ this.update({
5006
+ tokens: {
5007
+ [userAddress]: nonZeroTokens
5008
+ }
5009
+ });
5013
5010
  } catch (error) {
5014
5011
  log.error(error, "unable to fetch token balances using single call balance address");
5015
- this.getTokenBalancesUsingHandler(oldTokens);
5016
- } finally {
5012
+ const fetchedTokens = await this.getTokenBalancesUsingHandler(oldTokens);
5017
5013
  this.update({
5018
5014
  tokens: {
5019
- [userAddress]: nonZeroTokens
5015
+ [userAddress]: fetchedTokens
5020
5016
  }
5021
5017
  });
5022
5018
  }
5023
5019
  }
5024
5020
  async getTokenBalancesUsingHandler(customTokens) {
5025
- if (!this.userSelectedAddress) return;
5021
+ if (!this.userSelectedAddress) return [];
5026
5022
  const currentNetworkTokens = customTokens;
5027
5023
  const promiseSettledResult = await Promise.allSettled(currentNetworkTokens.map(async x => {
5028
5024
  try {
@@ -5055,11 +5051,7 @@ class TokensController extends BaseController {
5055
5051
  if (x.status === "fulfilled" && x.value) acc.push(x.value);
5056
5052
  return acc;
5057
5053
  }, []);
5058
- this.update({
5059
- tokens: {
5060
- [this.userSelectedAddress]: mergeTokenArrays(this.userTokens, nonZeroTokens)
5061
- }
5062
- });
5054
+ return nonZeroTokens;
5063
5055
  }
5064
5056
  }
5065
5057