@toruslabs/ethereum-controllers 7.2.0 → 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.
@@ -21857,7 +21857,8 @@ class src_PreferencesController extends src_base_controllers_namespaceObject.Bas
21857
21857
  loginMode,
21858
21858
  sessionPubKey,
21859
21859
  aaProvider,
21860
- eoaAddress
21860
+ eoaAddress,
21861
+ mainAddress
21861
21862
  } = params;
21862
21863
  const {
21863
21864
  chainId
@@ -21873,7 +21874,8 @@ class src_PreferencesController extends src_base_controllers_namespaceObject.Bas
21873
21874
  network: web3AuthNetwork,
21874
21875
  eoa_address: eoaAddress,
21875
21876
  aa_provider: aaProvider,
21876
- chain_id: chainId
21877
+ chain_id: chainId,
21878
+ main_address: mainAddress
21877
21879
  }
21878
21880
  });
21879
21881
  const {
@@ -22453,23 +22455,6 @@ class src_TokenRatesController extends src_base_controllers_namespaceObject.Base
22453
22455
 
22454
22456
 
22455
22457
 
22456
- function src_getObjectFromArrayBasedonKey(oldArray, key) {
22457
- return oldArray.reduce((acc, x) => {
22458
- const xkey = x[key];
22459
- if (typeof xkey === "boolean") return acc;
22460
- acc[xkey] = x;
22461
- return acc;
22462
- }, {});
22463
- }
22464
- const src_mergeTokenArrays = (oldArray, newArray) => {
22465
- const oldMap = src_getObjectFromArrayBasedonKey(oldArray || [], "tokenAddress");
22466
- const newMap = src_getObjectFromArrayBasedonKey(newArray || [], "tokenAddress");
22467
- const finalArr = newArray;
22468
- Object.keys(oldMap).forEach(x => {
22469
- if (!newMap[x] && oldMap[x].isEtherScan) finalArr.push(oldMap[x]);
22470
- });
22471
- return finalArr;
22472
- };
22473
22458
  const src_TokensController_DEFAULT_INTERVAL = 180 * 1000;
22474
22459
  class src_TokensController extends src_base_controllers_namespaceObject.BaseController {
22475
22460
  constructor({
@@ -22622,45 +22607,58 @@ class src_TokensController extends src_base_controllers_namespaceObject.BaseCont
22622
22607
  const userAddress = this.userSelectedAddress;
22623
22608
  if (userAddress === "") return;
22624
22609
  const oldTokens = [...this.userTokens];
22625
- const tokenAddresses = oldTokens.map(x => x.tokenAddress);
22626
- 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
22627
22612
  try {
22613
+ const nonZeroTokens = [];
22628
22614
  const currentChainId = this.config.chainId;
22629
22615
  if (src_ETHERSCAN_SUPPORTED_CHAINS.includes(currentChainId)) {
22630
22616
  const etherscanBalances = await this.getEtherScanTokens(userAddress, currentChainId, skipCache);
22631
22617
  nonZeroTokens.push(...etherscanBalances);
22632
22618
  }
22633
- 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);
22634
22627
  const currentSingleCallAddress = src_SINGLE_CALL_BALANCES_ADDRESSES[currentChainId];
22635
22628
  if (currentSingleCallAddress) {
22636
22629
  const ethContract = new src_external_ethers_namespaceObject.Contract(currentSingleCallAddress, src_singleBalanceCheckerAbi, this.ethersProvider);
22637
- const result = await ethContract.balances([userAddress], tokenAddresses);
22638
- tokenAddresses.forEach((_, index) => {
22630
+ const result = await ethContract.balances([userAddress], remainingTokensAddresses);
22631
+ remainingTokensAddresses.forEach((_, index) => {
22639
22632
  const balance = (0,src_external_ethers_namespaceObject.toQuantity)(result[index]);
22640
22633
  if (balance && balance !== "0x0") {
22641
- nonZeroTokens.push(src_objectSpread2_default()(src_objectSpread2_default()({}, oldTokens[index]), {}, {
22634
+ nonZeroTokens.push(src_objectSpread2_default()(src_objectSpread2_default()({}, remainingTokens[index]), {}, {
22642
22635
  balance,
22643
22636
  chainId: currentChainId
22644
22637
  }));
22645
22638
  }
22646
22639
  });
22647
22640
  } else {
22648
- this.getTokenBalancesUsingHandler(oldTokens);
22641
+ const fetchedRemainingTokens = await this.getTokenBalancesUsingHandler(remainingTokens);
22642
+ nonZeroTokens.push(...fetchedRemainingTokens);
22649
22643
  }
22650
22644
  }
22645
+ this.update({
22646
+ tokens: {
22647
+ [userAddress]: nonZeroTokens
22648
+ }
22649
+ });
22651
22650
  } catch (error) {
22652
22651
  src_external_loglevel_default().error(error, "unable to fetch token balances using single call balance address");
22653
- this.getTokenBalancesUsingHandler(oldTokens);
22654
- } finally {
22652
+ const fetchedTokens = await this.getTokenBalancesUsingHandler(oldTokens);
22655
22653
  this.update({
22656
22654
  tokens: {
22657
- [userAddress]: nonZeroTokens
22655
+ [userAddress]: fetchedTokens
22658
22656
  }
22659
22657
  });
22660
22658
  }
22661
22659
  }
22662
22660
  async getTokenBalancesUsingHandler(customTokens) {
22663
- if (!this.userSelectedAddress) return;
22661
+ if (!this.userSelectedAddress) return [];
22664
22662
  const currentNetworkTokens = customTokens;
22665
22663
  const promiseSettledResult = await Promise.allSettled(currentNetworkTokens.map(async x => {
22666
22664
  try {
@@ -22693,11 +22691,7 @@ class src_TokensController extends src_base_controllers_namespaceObject.BaseCont
22693
22691
  if (x.status === "fulfilled" && x.value) acc.push(x.value);
22694
22692
  return acc;
22695
22693
  }, []);
22696
- this.update({
22697
- tokens: {
22698
- [this.userSelectedAddress]: src_mergeTokenArrays(this.userTokens, nonZeroTokens)
22699
- }
22700
- });
22694
+ return nonZeroTokens;
22701
22695
  }
22702
22696
  }
22703
22697
  ;// ./src/Transaction/NonceTracker.ts
@@ -4236,7 +4236,8 @@ class PreferencesController extends BasePreferencesController {
4236
4236
  loginMode,
4237
4237
  sessionPubKey,
4238
4238
  aaProvider,
4239
- eoaAddress
4239
+ eoaAddress,
4240
+ mainAddress
4240
4241
  } = params;
4241
4242
  const {
4242
4243
  chainId
@@ -4252,7 +4253,8 @@ class PreferencesController extends BasePreferencesController {
4252
4253
  network: web3AuthNetwork,
4253
4254
  eoa_address: eoaAddress,
4254
4255
  aa_provider: aaProvider,
4255
- chain_id: chainId
4256
+ chain_id: chainId,
4257
+ main_address: mainAddress
4256
4258
  }
4257
4259
  });
4258
4260
  const {
@@ -4813,23 +4815,6 @@ class TokenRatesController extends BaseController {
4813
4815
  }
4814
4816
  }
4815
4817
 
4816
- function getObjectFromArrayBasedonKey(oldArray, key) {
4817
- return oldArray.reduce((acc, x) => {
4818
- const xkey = x[key];
4819
- if (typeof xkey === "boolean") return acc;
4820
- acc[xkey] = x;
4821
- return acc;
4822
- }, {});
4823
- }
4824
- const mergeTokenArrays = (oldArray, newArray) => {
4825
- const oldMap = getObjectFromArrayBasedonKey(oldArray || [], "tokenAddress");
4826
- const newMap = getObjectFromArrayBasedonKey(newArray || [], "tokenAddress");
4827
- const finalArr = newArray;
4828
- Object.keys(oldMap).forEach(x => {
4829
- if (!newMap[x] && oldMap[x].isEtherScan) finalArr.push(oldMap[x]);
4830
- });
4831
- return finalArr;
4832
- };
4833
4818
  const DEFAULT_INTERVAL = 180 * 1000;
4834
4819
  class TokensController extends BaseController {
4835
4820
  constructor({
@@ -4982,45 +4967,58 @@ class TokensController extends BaseController {
4982
4967
  const userAddress = this.userSelectedAddress;
4983
4968
  if (userAddress === "") return;
4984
4969
  const oldTokens = [...this.userTokens];
4985
- const tokenAddresses = oldTokens.map(x => x.tokenAddress);
4986
- 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
4987
4972
  try {
4973
+ const nonZeroTokens = [];
4988
4974
  const currentChainId = this.config.chainId;
4989
4975
  if (ETHERSCAN_SUPPORTED_CHAINS.includes(currentChainId)) {
4990
4976
  const etherscanBalances = await this.getEtherScanTokens(userAddress, currentChainId, skipCache);
4991
4977
  nonZeroTokens.push(...etherscanBalances);
4992
4978
  }
4993
- 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);
4994
4987
  const currentSingleCallAddress = SINGLE_CALL_BALANCES_ADDRESSES[currentChainId];
4995
4988
  if (currentSingleCallAddress) {
4996
4989
  const ethContract = new Contract(currentSingleCallAddress, singleBalanceCheckerAbi, this.ethersProvider);
4997
- const result = await ethContract.balances([userAddress], tokenAddresses);
4998
- tokenAddresses.forEach((_, index) => {
4990
+ const result = await ethContract.balances([userAddress], remainingTokensAddresses);
4991
+ remainingTokensAddresses.forEach((_, index) => {
4999
4992
  const balance = toQuantity(result[index]);
5000
4993
  if (balance && balance !== "0x0") {
5001
- nonZeroTokens.push(_objectSpread(_objectSpread({}, oldTokens[index]), {}, {
4994
+ nonZeroTokens.push(_objectSpread(_objectSpread({}, remainingTokens[index]), {}, {
5002
4995
  balance,
5003
4996
  chainId: currentChainId
5004
4997
  }));
5005
4998
  }
5006
4999
  });
5007
5000
  } else {
5008
- this.getTokenBalancesUsingHandler(oldTokens);
5001
+ const fetchedRemainingTokens = await this.getTokenBalancesUsingHandler(remainingTokens);
5002
+ nonZeroTokens.push(...fetchedRemainingTokens);
5009
5003
  }
5010
5004
  }
5005
+ this.update({
5006
+ tokens: {
5007
+ [userAddress]: nonZeroTokens
5008
+ }
5009
+ });
5011
5010
  } catch (error) {
5012
5011
  log.error(error, "unable to fetch token balances using single call balance address");
5013
- this.getTokenBalancesUsingHandler(oldTokens);
5014
- } finally {
5012
+ const fetchedTokens = await this.getTokenBalancesUsingHandler(oldTokens);
5015
5013
  this.update({
5016
5014
  tokens: {
5017
- [userAddress]: nonZeroTokens
5015
+ [userAddress]: fetchedTokens
5018
5016
  }
5019
5017
  });
5020
5018
  }
5021
5019
  }
5022
5020
  async getTokenBalancesUsingHandler(customTokens) {
5023
- if (!this.userSelectedAddress) return;
5021
+ if (!this.userSelectedAddress) return [];
5024
5022
  const currentNetworkTokens = customTokens;
5025
5023
  const promiseSettledResult = await Promise.allSettled(currentNetworkTokens.map(async x => {
5026
5024
  try {
@@ -5053,11 +5051,7 @@ class TokensController extends BaseController {
5053
5051
  if (x.status === "fulfilled" && x.value) acc.push(x.value);
5054
5052
  return acc;
5055
5053
  }, []);
5056
- this.update({
5057
- tokens: {
5058
- [this.userSelectedAddress]: mergeTokenArrays(this.userTokens, nonZeroTokens)
5059
- }
5060
- });
5054
+ return nonZeroTokens;
5061
5055
  }
5062
5056
  }
5063
5057