@talismn/balances 0.0.0-pr2091-20250715125148 → 0.0.0-pr2092-20250715131755

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.
@@ -1,15 +1,13 @@
1
1
  'use strict';
2
2
 
3
3
  var chaindataProvider = require('@talismn/chaindata-provider');
4
- var crypto = require('@talismn/crypto');
4
+ var util = require('@talismn/util');
5
5
  var viem = require('viem');
6
6
  var lodashEs = require('lodash-es');
7
7
  var z = require('zod/v4');
8
8
  var anylogger = require('anylogger');
9
9
  var rxjs = require('rxjs');
10
10
  var BigNumber = require('bignumber.js');
11
- var web3_js = require('@solana/web3.js');
12
- var util = require('@talismn/util');
13
11
  var scale = require('@talismn/scale');
14
12
  var tokenRates = require('@talismn/token-rates');
15
13
  var utils = require('@polkadot-api/utils');
@@ -30,8 +28,8 @@ var BigNumber__default = /*#__PURE__*/_interopDefault(BigNumber);
30
28
  var upperFirst__default = /*#__PURE__*/_interopDefault(upperFirst);
31
29
  var PQueue__default = /*#__PURE__*/_interopDefault(PQueue);
32
30
 
33
- const MODULE_TYPE$9 = chaindataProvider.EvmErc20TokenSchema.shape.type.value;
34
- const PLATFORM$9 = chaindataProvider.EvmErc20TokenSchema.shape.platform.value;
31
+ const MODULE_TYPE$8 = chaindataProvider.EvmErc20TokenSchema.shape.type.value;
32
+ const PLATFORM$8 = chaindataProvider.EvmErc20TokenSchema.shape.platform.value;
35
33
 
36
34
  const abiMulticall = viem.parseAbi(["struct Call { address target; bytes callData; }", "struct Call3 { address target; bool allowFailure; bytes callData; }", "struct Call3Value { address target; bool allowFailure; uint256 value; bytes callData; }", "struct Result { bool success; bytes returnData; }", "function aggregate(Call[] calldata calls) public payable returns (uint256 blockNumber, bytes[] memory returnData)", "function aggregate3(Call3[] calldata calls) public payable returns (Result[] memory returnData)", "function aggregate3Value(Call3Value[] calldata calls) public payable returns (Result[] memory returnData)", "function blockAndAggregate(Call[] calldata calls) public payable returns (uint256 blockNumber, bytes32 blockHash, Result[] memory returnData)", "function getBasefee() view returns (uint256 basefee)", "function getBlockHash(uint256 blockNumber) view returns (bytes32 blockHash)", "function getBlockNumber() view returns (uint256 blockNumber)", "function getChainId() view returns (uint256 chainid)", "function getCurrentBlockCoinbase() view returns (address coinbase)", "function getCurrentBlockDifficulty() view returns (uint256 difficulty)", "function getCurrentBlockGasLimit() view returns (uint256 gaslimit)", "function getCurrentBlockTimestamp() view returns (uint256 timestamp)", "function getEthBalance(address addr) view returns (uint256 balance)", "function getLastBlockHash() view returns (bytes32 blockHash)", "function tryAggregate(bool requireSuccess, Call[] calldata calls) public payable returns (Result[] memory returnData)", "function tryBlockAndAggregate(bool requireSuccess, Call[] calldata calls) public payable returns (uint256 blockNumber, bytes32 blockHash, Result[] memory returnData)"]);
37
35
 
@@ -627,7 +625,7 @@ const getBalanceDefs = addressesByToken => {
627
625
  // if there is at least one storage entry, the results will be an array with a single object
628
626
  // if the storage has no entries in it (ex: Assets on ewx or moonbeam), the response will be an empty array
629
627
 
630
- const fetchBalances$9 = async ({
628
+ const fetchBalances$8 = async ({
631
629
  networkId,
632
630
  tokensWithAddresses,
633
631
  connector
@@ -639,8 +637,8 @@ const fetchBalances$9 = async ({
639
637
  const client = await connector.getPublicClientForEvmNetwork(networkId);
640
638
  if (!client) throw new Error(`Could not get rpc provider for evm network ${networkId}`);
641
639
  for (const [token, addresses] of tokensWithAddresses) {
642
- if (token.type !== MODULE_TYPE$9 || token.networkId !== networkId) throw new Error(`Invalid token type or networkId for EVM ERC20 balance module: ${token.type} on ${token.networkId}`);
643
- for (const address of addresses) if (!crypto.isEthereumAddress(address)) throw new Error(`Invalid ethereum address for EVM ERC20 balance module: ${address} for token ${token.id}`);
640
+ if (token.type !== MODULE_TYPE$8 || token.networkId !== networkId) throw new Error(`Invalid token type or networkId for EVM ERC20 balance module: ${token.type} on ${token.networkId}`);
641
+ for (const address of addresses) if (!util.isEthereumAddress(address)) throw new Error(`Invalid ethereum address for EVM ERC20 balance module: ${address} for token ${token.id}`);
644
642
  }
645
643
  const balanceDefs = getBalanceDefs(tokensWithAddresses);
646
644
  if (client.chain?.contracts?.erc20Aggregator && balanceDefs.length > 1) {
@@ -669,7 +667,7 @@ const fetchWithoutAggregator = async (client, balanceDefs) => {
669
667
  address,
670
668
  tokenId: token.id,
671
669
  value: result.toString(),
672
- source: MODULE_TYPE$9,
670
+ source: MODULE_TYPE$8,
673
671
  networkId: chaindataProvider.parseEvmErc20TokenId(token.id).networkId,
674
672
  status: "live"
675
673
  };
@@ -712,7 +710,7 @@ const fetchWithAggregator = async (client, balanceDefs, erc20BalancesAggregatorA
712
710
  address: balanceDef.address,
713
711
  tokenId: balanceDef.token.id,
714
712
  value: erc20Balances[index].toString(),
715
- source: MODULE_TYPE$9,
713
+ source: MODULE_TYPE$8,
716
714
  networkId: chaindataProvider.parseTokenId(balanceDef.token.id).networkId,
717
715
  status: "live"
718
716
  }));
@@ -781,7 +779,7 @@ const TokenCacheSchema$1 = z__default.default.discriminatedUnion("isValid", [z__
781
779
  id: chaindataProvider.EvmErc20TokenSchema.shape.id,
782
780
  isValid: z__default.default.literal(false)
783
781
  })]);
784
- const fetchTokens$9 = async ({
782
+ const fetchTokens$8 = async ({
785
783
  networkId,
786
784
  tokens,
787
785
  connector,
@@ -821,8 +819,8 @@ const fetchTokens$9 = async ({
821
819
  }
822
820
  const base = {
823
821
  id: tokenId,
824
- type: MODULE_TYPE$9,
825
- platform: PLATFORM$9,
822
+ type: MODULE_TYPE$8,
823
+ platform: PLATFORM$8,
826
824
  networkId
827
825
  };
828
826
  const cached2 = cache[tokenId] && TokenCacheSchema$1.safeParse(cache[tokenId]).data;
@@ -840,19 +838,19 @@ const fetchTokens$9 = async ({
840
838
  return result;
841
839
  };
842
840
 
843
- const getMiniMetadata$9 = () => {
841
+ const getMiniMetadata$8 = () => {
844
842
  throw new Error("MiniMetadata is not supported for ethereum tokens");
845
843
  };
846
844
 
847
- const getTransferCallData$9 = ({
845
+ const getTransferCallData$8 = ({
848
846
  from,
849
847
  to,
850
848
  value,
851
849
  token
852
850
  }) => {
853
- if (!chaindataProvider.isTokenOfType(token, MODULE_TYPE$9)) throw new Error(`Token type ${token.type} is not ${MODULE_TYPE$9}.`);
854
- if (!crypto.isEthereumAddress(from)) throw new Error("Invalid from address");
855
- if (!crypto.isEthereumAddress(to)) throw new Error("Invalid to address");
851
+ if (!chaindataProvider.isTokenOfType(token, MODULE_TYPE$8)) throw new Error(`Token type ${token.type} is not ${MODULE_TYPE$8}.`);
852
+ if (!util.isEthereumAddress(from)) throw new Error("Invalid from address");
853
+ if (!util.isEthereumAddress(to)) throw new Error("Invalid to address");
856
854
  const data = viem.encodeFunctionData({
857
855
  abi: viem.erc20Abi,
858
856
  functionName: "transfer",
@@ -865,8 +863,8 @@ const getTransferCallData$9 = ({
865
863
  };
866
864
  };
867
865
 
868
- const SUBSCRIPTION_INTERVAL$5 = 6_000;
869
- const subscribeBalances$9 = ({
866
+ const SUBSCRIPTION_INTERVAL$4 = 6_000;
867
+ const subscribeBalances$8 = ({
870
868
  networkId,
871
869
  tokensWithAddresses,
872
870
  connector
@@ -880,17 +878,17 @@ const subscribeBalances$9 = ({
880
878
  const poll = async () => {
881
879
  try {
882
880
  if (abortController.signal.aborted) return;
883
- const balances = await fetchBalances$9({
881
+ const balances = await fetchBalances$8({
884
882
  networkId,
885
883
  tokensWithAddresses: tokensWithAddresses,
886
884
  connector
887
885
  });
888
886
  if (abortController.signal.aborted) return;
889
887
  subscriber.next(balances);
890
- setTimeout(poll, SUBSCRIPTION_INTERVAL$5);
888
+ setTimeout(poll, SUBSCRIPTION_INTERVAL$4);
891
889
  } catch (error) {
892
890
  log.error("Error", {
893
- module: MODULE_TYPE$9,
891
+ module: MODULE_TYPE$8,
894
892
  networkId,
895
893
  addressesByToken: tokensWithAddresses,
896
894
  error
@@ -906,13 +904,13 @@ const subscribeBalances$9 = ({
906
904
  };
907
905
 
908
906
  const EvmErc20BalanceModule = {
909
- type: MODULE_TYPE$9,
910
- platform: PLATFORM$9,
911
- getMiniMetadata: getMiniMetadata$9,
912
- fetchTokens: fetchTokens$9,
913
- fetchBalances: fetchBalances$9,
914
- subscribeBalances: subscribeBalances$9,
915
- getTransferCallData: getTransferCallData$9
907
+ type: MODULE_TYPE$8,
908
+ platform: PLATFORM$8,
909
+ getMiniMetadata: getMiniMetadata$8,
910
+ fetchTokens: fetchTokens$8,
911
+ fetchBalances: fetchBalances$8,
912
+ subscribeBalances: subscribeBalances$8,
913
+ getTransferCallData: getTransferCallData$8
916
914
  };
917
915
 
918
916
  const TokenConfigBaseSchema = chaindataProvider.TokenBaseSchema.partial().omit({
@@ -925,10 +923,10 @@ const EvmErc20TokenConfigSchema = z__default.default.strictObject({
925
923
  ...TokenConfigBaseSchema.shape
926
924
  });
927
925
 
928
- const MODULE_TYPE$8 = chaindataProvider.EvmNativeTokenSchema.shape.type.value;
929
- const PLATFORM$8 = chaindataProvider.EvmNativeTokenSchema.shape.platform.value;
926
+ const MODULE_TYPE$7 = chaindataProvider.EvmNativeTokenSchema.shape.type.value;
927
+ const PLATFORM$7 = chaindataProvider.EvmNativeTokenSchema.shape.platform.value;
930
928
 
931
- const fetchBalances$8 = async ({
929
+ const fetchBalances$7 = async ({
932
930
  networkId,
933
931
  tokensWithAddresses,
934
932
  connector
@@ -940,8 +938,8 @@ const fetchBalances$8 = async ({
940
938
  const client = await connector.getPublicClientForEvmNetwork(networkId);
941
939
  if (!client) throw new Error(`Could not get rpc provider for evm network ${networkId}`);
942
940
  for (const [token, addresses] of tokensWithAddresses) {
943
- if (token.type !== MODULE_TYPE$8 || token.networkId !== networkId) throw new Error(`Invalid token type or networkId for EVM ERC20 balance module: ${token.type} on ${token.networkId}`);
944
- for (const address of addresses) if (!crypto.isEthereumAddress(address)) throw new Error(`Invalid ethereum address for EVM ERC20 balance module: ${address} for token ${token.id}`);
941
+ if (token.type !== MODULE_TYPE$7 || token.networkId !== networkId) throw new Error(`Invalid token type or networkId for EVM ERC20 balance module: ${token.type} on ${token.networkId}`);
942
+ for (const address of addresses) if (!util.isEthereumAddress(address)) throw new Error(`Invalid ethereum address for EVM ERC20 balance module: ${address} for token ${token.id}`);
945
943
  }
946
944
  const balanceDefs = getBalanceDefs(tokensWithAddresses);
947
945
  if (client.chain?.contracts?.multicall3 && balanceDefs.length > 1) {
@@ -967,7 +965,7 @@ const fetchWithoutMulticall = async (client, balanceDefs) => {
967
965
  address,
968
966
  tokenId: token.id,
969
967
  value: result.toString(),
970
- source: MODULE_TYPE$8,
968
+ source: MODULE_TYPE$7,
971
969
  networkId: chaindataProvider.parseTokenId(token.id).networkId,
972
970
  status: "live"
973
971
  };
@@ -1013,7 +1011,7 @@ const fetchWithMulticall = async (client, balanceDefs, multicall3Address) => {
1013
1011
  address: balanceDefs[index].address,
1014
1012
  tokenId: balanceDefs[index].token.id,
1015
1013
  value: result.result.toString(),
1016
- source: MODULE_TYPE$8,
1014
+ source: MODULE_TYPE$7,
1017
1015
  networkId: chaindataProvider.parseTokenId(balanceDefs[index].token.id).networkId,
1018
1016
  status: "live"
1019
1017
  });
@@ -1043,7 +1041,7 @@ const fetchWithMulticall = async (client, balanceDefs, multicall3Address) => {
1043
1041
  }
1044
1042
  };
1045
1043
 
1046
- const fetchTokens$8 = async ({
1044
+ const fetchTokens$7 = async ({
1047
1045
  networkId,
1048
1046
  tokens
1049
1047
  }) => {
@@ -1051,8 +1049,8 @@ const fetchTokens$8 = async ({
1051
1049
  if (tokens.length !== 1) throw new Error("EVM Native module expects the nativeCurrency to be passed as a single token in the array");
1052
1050
  const token = lodashEs.assign({
1053
1051
  id: chaindataProvider.evmNativeTokenId(networkId),
1054
- type: MODULE_TYPE$8,
1055
- platform: PLATFORM$8,
1052
+ type: MODULE_TYPE$7,
1053
+ platform: PLATFORM$7,
1056
1054
  networkId,
1057
1055
  isDefault: true
1058
1056
  }, tokens[0]);
@@ -1064,19 +1062,19 @@ const fetchTokens$8 = async ({
1064
1062
  return [parsed.data];
1065
1063
  };
1066
1064
 
1067
- const getMiniMetadata$8 = () => {
1065
+ const getMiniMetadata$7 = () => {
1068
1066
  throw new Error("MiniMetadata is not supported for ethereum tokens");
1069
1067
  };
1070
1068
 
1071
- const getTransferCallData$8 = ({
1069
+ const getTransferCallData$7 = ({
1072
1070
  from,
1073
1071
  to,
1074
1072
  value,
1075
1073
  token
1076
1074
  }) => {
1077
- if (!chaindataProvider.isTokenOfType(token, MODULE_TYPE$8)) throw new Error(`Token type ${token.type} is not ${MODULE_TYPE$8}.`);
1078
- if (!crypto.isEthereumAddress(from)) throw new Error("Invalid from address");
1079
- if (!crypto.isEthereumAddress(to)) throw new Error("Invalid to address");
1075
+ if (!chaindataProvider.isTokenOfType(token, MODULE_TYPE$7)) throw new Error(`Token type ${token.type} is not ${MODULE_TYPE$7}.`);
1076
+ if (!util.isEthereumAddress(from)) throw new Error("Invalid from address");
1077
+ if (!util.isEthereumAddress(to)) throw new Error("Invalid to address");
1080
1078
  return {
1081
1079
  from,
1082
1080
  to,
@@ -1085,8 +1083,8 @@ const getTransferCallData$8 = ({
1085
1083
  };
1086
1084
  };
1087
1085
 
1088
- const SUBSCRIPTION_INTERVAL$4 = 6_000;
1089
- const subscribeBalances$8 = ({
1086
+ const SUBSCRIPTION_INTERVAL$3 = 6_000;
1087
+ const subscribeBalances$7 = ({
1090
1088
  networkId,
1091
1089
  tokensWithAddresses,
1092
1090
  connector
@@ -1100,17 +1098,17 @@ const subscribeBalances$8 = ({
1100
1098
  const poll = async () => {
1101
1099
  try {
1102
1100
  if (abortController.signal.aborted) return;
1103
- const balances = await fetchBalances$8({
1101
+ const balances = await fetchBalances$7({
1104
1102
  networkId,
1105
1103
  tokensWithAddresses: tokensWithAddresses,
1106
1104
  connector
1107
1105
  });
1108
1106
  if (abortController.signal.aborted) return;
1109
1107
  subscriber.next(balances);
1110
- setTimeout(poll, SUBSCRIPTION_INTERVAL$4);
1108
+ setTimeout(poll, SUBSCRIPTION_INTERVAL$3);
1111
1109
  } catch (error) {
1112
1110
  log.error("Error", {
1113
- module: MODULE_TYPE$8,
1111
+ module: MODULE_TYPE$7,
1114
1112
  networkId,
1115
1113
  addressesByToken: tokensWithAddresses,
1116
1114
  error
@@ -1126,13 +1124,13 @@ const subscribeBalances$8 = ({
1126
1124
  };
1127
1125
 
1128
1126
  const EvmNativeBalanceModule = {
1129
- type: MODULE_TYPE$8,
1130
- platform: PLATFORM$8,
1131
- getMiniMetadata: getMiniMetadata$8,
1132
- fetchTokens: fetchTokens$8,
1133
- fetchBalances: fetchBalances$8,
1134
- subscribeBalances: subscribeBalances$8,
1135
- getTransferCallData: getTransferCallData$8
1127
+ type: MODULE_TYPE$7,
1128
+ platform: PLATFORM$7,
1129
+ getMiniMetadata: getMiniMetadata$7,
1130
+ fetchTokens: fetchTokens$7,
1131
+ fetchBalances: fetchBalances$7,
1132
+ subscribeBalances: subscribeBalances$7,
1133
+ getTransferCallData: getTransferCallData$7
1136
1134
  };
1137
1135
 
1138
1136
  // to be used by chaindata too
@@ -1140,10 +1138,10 @@ const EvmNativeTokenConfigSchema = z__default.default.strictObject({
1140
1138
  ...TokenConfigBaseSchema.shape
1141
1139
  });
1142
1140
 
1143
- const MODULE_TYPE$7 = chaindataProvider.EvmUniswapV2TokenSchema.shape.type.value;
1144
- const PLATFORM$7 = chaindataProvider.EvmUniswapV2TokenSchema.shape.platform.value;
1141
+ const MODULE_TYPE$6 = chaindataProvider.EvmUniswapV2TokenSchema.shape.type.value;
1142
+ const PLATFORM$6 = chaindataProvider.EvmUniswapV2TokenSchema.shape.platform.value;
1145
1143
 
1146
- const fetchBalances$7 = async ({
1144
+ const fetchBalances$6 = async ({
1147
1145
  networkId,
1148
1146
  tokensWithAddresses,
1149
1147
  connector
@@ -1155,8 +1153,8 @@ const fetchBalances$7 = async ({
1155
1153
  const client = await connector.getPublicClientForEvmNetwork(networkId);
1156
1154
  if (!client) throw new Error(`Could not get rpc provider for evm network ${networkId}`);
1157
1155
  for (const [token, addresses] of tokensWithAddresses) {
1158
- if (token.type !== MODULE_TYPE$7 || token.networkId !== networkId) throw new Error(`Invalid token type or networkId for EVM ERC20 balance module: ${token.type} on ${token.networkId}`);
1159
- for (const address of addresses) if (!crypto.isEthereumAddress(address)) throw new Error(`Invalid ethereum address for EVM ERC20 balance module: ${address} for token ${token.id}`);
1156
+ if (token.type !== MODULE_TYPE$6 || token.networkId !== networkId) throw new Error(`Invalid token type or networkId for EVM ERC20 balance module: ${token.type} on ${token.networkId}`);
1157
+ for (const address of addresses) if (!util.isEthereumAddress(address)) throw new Error(`Invalid ethereum address for EVM ERC20 balance module: ${address} for token ${token.id}`);
1160
1158
  }
1161
1159
  const balanceDefs = getBalanceDefs(tokensWithAddresses);
1162
1160
  return fetchPoolBalances(client, balanceDefs);
@@ -1242,7 +1240,7 @@ const fetchPoolBalances = async (client, balanceDefs) => {
1242
1240
  acc.success.push({
1243
1241
  address,
1244
1242
  tokenId: token.id,
1245
- source: MODULE_TYPE$7,
1243
+ source: MODULE_TYPE$6,
1246
1244
  networkId: chaindataProvider.parseTokenId(token.id).networkId,
1247
1245
  status: "live",
1248
1246
  values: [{
@@ -1320,7 +1318,7 @@ const TokenCacheSchema = z__default.default.discriminatedUnion("isValid", [z__de
1320
1318
  id: chaindataProvider.EvmUniswapV2TokenSchema.shape.id,
1321
1319
  isValid: z__default.default.literal(false)
1322
1320
  })]);
1323
- const fetchTokens$7 = async ({
1321
+ const fetchTokens$6 = async ({
1324
1322
  networkId,
1325
1323
  tokens,
1326
1324
  connector,
@@ -1379,8 +1377,8 @@ const fetchTokens$7 = async ({
1379
1377
  }
1380
1378
  const base = {
1381
1379
  id: tokenId,
1382
- type: MODULE_TYPE$7,
1383
- platform: PLATFORM$7,
1380
+ type: MODULE_TYPE$6,
1381
+ platform: PLATFORM$6,
1384
1382
  networkId
1385
1383
  };
1386
1384
  const cached2 = cache[tokenId] && TokenCacheSchema.safeParse(cache[tokenId]).data;
@@ -1396,19 +1394,19 @@ const fetchTokens$7 = async ({
1396
1394
  return result;
1397
1395
  };
1398
1396
 
1399
- const getMiniMetadata$7 = () => {
1397
+ const getMiniMetadata$6 = () => {
1400
1398
  throw new Error("MiniMetadata is not supported for ethereum tokens");
1401
1399
  };
1402
1400
 
1403
- const getTransferCallData$7 = ({
1401
+ const getTransferCallData$6 = ({
1404
1402
  from,
1405
1403
  to,
1406
1404
  value,
1407
1405
  token
1408
1406
  }) => {
1409
- if (!chaindataProvider.isTokenOfType(token, MODULE_TYPE$7)) throw new Error(`Token type ${token.type} is not ${MODULE_TYPE$7}.`);
1410
- if (!crypto.isEthereumAddress(from)) throw new Error("Invalid from address");
1411
- if (!crypto.isEthereumAddress(to)) throw new Error("Invalid to address");
1407
+ if (!chaindataProvider.isTokenOfType(token, MODULE_TYPE$6)) throw new Error(`Token type ${token.type} is not ${MODULE_TYPE$6}.`);
1408
+ if (!util.isEthereumAddress(from)) throw new Error("Invalid from address");
1409
+ if (!util.isEthereumAddress(to)) throw new Error("Invalid to address");
1412
1410
  const data = viem.encodeFunctionData({
1413
1411
  abi: viem.erc20Abi,
1414
1412
  functionName: "transfer",
@@ -1421,151 +1419,6 @@ const getTransferCallData$7 = ({
1421
1419
  };
1422
1420
  };
1423
1421
 
1424
- const SUBSCRIPTION_INTERVAL$3 = 6_000;
1425
- const subscribeBalances$7 = ({
1426
- networkId,
1427
- tokensWithAddresses,
1428
- connector
1429
- }) => {
1430
- if (!tokensWithAddresses.length) return rxjs.of({
1431
- success: [],
1432
- errors: []
1433
- });
1434
- return new rxjs.Observable(subscriber => {
1435
- const abortController = new AbortController();
1436
- const poll = async () => {
1437
- try {
1438
- if (abortController.signal.aborted) return;
1439
- const balances = await fetchBalances$7({
1440
- networkId,
1441
- tokensWithAddresses: tokensWithAddresses,
1442
- connector
1443
- });
1444
- if (abortController.signal.aborted) return;
1445
- subscriber.next(balances);
1446
- setTimeout(poll, SUBSCRIPTION_INTERVAL$3);
1447
- } catch (error) {
1448
- log.error("Error", {
1449
- module: MODULE_TYPE$7,
1450
- networkId,
1451
- addressesByToken: tokensWithAddresses,
1452
- error
1453
- });
1454
- subscriber.error(error);
1455
- }
1456
- };
1457
- poll();
1458
- return () => {
1459
- abortController.abort();
1460
- };
1461
- }).pipe(rxjs.distinctUntilChanged(lodashEs.isEqual));
1462
- };
1463
-
1464
- const EvmUniswapV2BalanceModule = {
1465
- type: MODULE_TYPE$7,
1466
- platform: PLATFORM$7,
1467
- getMiniMetadata: getMiniMetadata$7,
1468
- fetchTokens: fetchTokens$7,
1469
- fetchBalances: fetchBalances$7,
1470
- subscribeBalances: subscribeBalances$7,
1471
- getTransferCallData: getTransferCallData$7
1472
- };
1473
-
1474
- // to be used by chaindata too
1475
- const EvmUniswapV2TokenConfigSchema = z__default.default.strictObject({
1476
- contractAddress: chaindataProvider.EvmUniswapV2TokenSchema.shape.contractAddress,
1477
- ...TokenConfigBaseSchema.shape
1478
- });
1479
-
1480
- const MODULE_TYPE$6 = chaindataProvider.SolNativeTokenSchema.shape.type.value;
1481
- const PLATFORM$6 = chaindataProvider.SolNativeTokenSchema.shape.platform.value;
1482
-
1483
- const fetchBalances$6 = async ({
1484
- networkId,
1485
- tokensWithAddresses,
1486
- connector
1487
- }) => {
1488
- if (!tokensWithAddresses.length) return {
1489
- success: [],
1490
- errors: []
1491
- };
1492
- const connection = await connector.getConnection(networkId);
1493
- if (!connection) throw new Error(`Could not get rpc provider for sol network ${networkId}`);
1494
- for (const [token, addresses] of tokensWithAddresses) {
1495
- if (token.type !== MODULE_TYPE$6 || token.networkId !== networkId) throw new Error(`Invalid token type or networkId for balance module: ${token.type} on ${token.networkId}`);
1496
- for (const address of addresses) if (!crypto.isSolanaAddress(address)) throw new Error(`Invalid solana address for balance module: ${address} for token ${token.id}`);
1497
- }
1498
- const balanceDefs = getBalanceDefs(tokensWithAddresses);
1499
- const results = await Promise.allSettled(balanceDefs.map(async ({
1500
- token,
1501
- address
1502
- }) => {
1503
- try {
1504
- const publicKey = new web3_js.PublicKey(address);
1505
- const lamports = await connection.getBalance(publicKey);
1506
- return {
1507
- address: address,
1508
- tokenId: token.id,
1509
- value: lamports.toString(),
1510
- source: MODULE_TYPE$6,
1511
- networkId: token.networkId,
1512
- status: "live"
1513
- };
1514
- } catch (err) {
1515
- throw new BalanceFetchError(`Failed to get balance for token ${token.id} and address ${address} on chain ${networkId}`, token.id, address, err);
1516
- }
1517
- }));
1518
- return results.reduce((acc, result) => {
1519
- if (result.status === "fulfilled") acc.success.push(result.value);else {
1520
- const error = result.reason;
1521
- acc.errors.push({
1522
- tokenId: error.tokenId,
1523
- address: error.address,
1524
- error
1525
- });
1526
- }
1527
- return acc;
1528
- }, {
1529
- success: [],
1530
- errors: []
1531
- });
1532
- };
1533
-
1534
- const fetchTokens$6 = async ({
1535
- networkId,
1536
- tokens
1537
- }) => {
1538
- // assume there is one and only one token in the array
1539
- if (tokens.length !== 1) throw new Error("EVM Native module expects the nativeCurrency to be passed as a single token in the array");
1540
- const token = lodashEs.assign({
1541
- id: chaindataProvider.solNativeTokenId(networkId),
1542
- type: MODULE_TYPE$6,
1543
- platform: PLATFORM$6,
1544
- networkId,
1545
- isDefault: true
1546
- }, tokens[0]);
1547
- const parsed = chaindataProvider.SolNativeTokenSchema.safeParse(token);
1548
- if (!parsed.success) {
1549
- log.warn("Ignoring token with invalid schema", token);
1550
- return [];
1551
- }
1552
- return [parsed.data];
1553
- };
1554
-
1555
- const getMiniMetadata$6 = () => {
1556
- throw new Error("MiniMetadata is not supported for sol-native tokens");
1557
- };
1558
-
1559
- const getTransferCallData$6 = ({
1560
- // from,
1561
- // to,
1562
- // value,
1563
- token
1564
- }) => {
1565
- if (!chaindataProvider.isTokenOfType(token, MODULE_TYPE$6)) throw new Error(`Token type ${token.type} is not ${MODULE_TYPE$6}.`);
1566
- throw new Error("Not implemented");
1567
- };
1568
-
1569
1422
  const SUBSCRIPTION_INTERVAL$2 = 6_000;
1570
1423
  const subscribeBalances$6 = ({
1571
1424
  networkId,
@@ -1583,7 +1436,7 @@ const subscribeBalances$6 = ({
1583
1436
  if (abortController.signal.aborted) return;
1584
1437
  const balances = await fetchBalances$6({
1585
1438
  networkId,
1586
- tokensWithAddresses,
1439
+ tokensWithAddresses: tokensWithAddresses,
1587
1440
  connector
1588
1441
  });
1589
1442
  if (abortController.signal.aborted) return;
@@ -1593,7 +1446,7 @@ const subscribeBalances$6 = ({
1593
1446
  log.error("Error", {
1594
1447
  module: MODULE_TYPE$6,
1595
1448
  networkId,
1596
- tokensWithAddresses,
1449
+ addressesByToken: tokensWithAddresses,
1597
1450
  error
1598
1451
  });
1599
1452
  subscriber.error(error);
@@ -1606,7 +1459,7 @@ const subscribeBalances$6 = ({
1606
1459
  }).pipe(rxjs.distinctUntilChanged(lodashEs.isEqual));
1607
1460
  };
1608
1461
 
1609
- const SolNativeBalanceModule = {
1462
+ const EvmUniswapV2BalanceModule = {
1610
1463
  type: MODULE_TYPE$6,
1611
1464
  platform: PLATFORM$6,
1612
1465
  getMiniMetadata: getMiniMetadata$6,
@@ -1617,7 +1470,8 @@ const SolNativeBalanceModule = {
1617
1470
  };
1618
1471
 
1619
1472
  // to be used by chaindata too
1620
- const SolNativeTokenConfigSchema = z__default.default.strictObject({
1473
+ const EvmUniswapV2TokenConfigSchema = z__default.default.strictObject({
1474
+ contractAddress: chaindataProvider.EvmUniswapV2TokenSchema.shape.contractAddress,
1621
1475
  ...TokenConfigBaseSchema.shape
1622
1476
  });
1623
1477
 
@@ -6282,7 +6136,7 @@ const SubTokensMiniMetadataExtraSchema = z__default.default.strictObject({
6282
6136
  palletId: z__default.default.string()
6283
6137
  });
6284
6138
 
6285
- const BALANCE_MODULES = [SubNativeBalanceModule, SubAssetsBalanceModule, SubHydrationBalanceModule, SubForeignAssetsBalanceModule, SubPsp22BalanceModule, SubTokensBalanceModule, EvmErc20BalanceModule, EvmUniswapV2BalanceModule, EvmNativeBalanceModule, SolNativeBalanceModule];
6139
+ const BALANCE_MODULES = [SubNativeBalanceModule, SubAssetsBalanceModule, SubHydrationBalanceModule, SubForeignAssetsBalanceModule, SubPsp22BalanceModule, SubTokensBalanceModule, EvmErc20BalanceModule, EvmUniswapV2BalanceModule, EvmNativeBalanceModule];
6286
6140
 
6287
6141
  // share requests as all modules will call this at once
6288
6142
  const CACHE$1 = new Map();
@@ -6460,25 +6314,10 @@ class BalancesProvider {
6460
6314
  {
6461
6315
  return this.getEthereumNetworkModuleBalances$(networkId, tokensWithAddresses, mod);
6462
6316
  }
6463
- case "solana":
6464
- {
6465
- return this.getSolanaNetworkModuleBalances$(networkId, tokensWithAddresses, mod);
6466
- }
6467
6317
  case "polkadot":
6468
6318
  {
6469
6319
  return this.getPolkadotNetworkModuleBalances$(networkId, tokensWithAddresses, mod);
6470
6320
  }
6471
- default:
6472
- {
6473
- log.warn("[balances] Unsupported network platform for module", {
6474
- networkId,
6475
- mod
6476
- });
6477
- return rxjs.of({
6478
- status: "live",
6479
- balances: []
6480
- }); // no solana modules yet
6481
- }
6482
6321
  }
6483
6322
  }));
6484
6323
  }), rxjs.map(results => {
@@ -6508,7 +6347,7 @@ class BalancesProvider {
6508
6347
  address
6509
6348
  })));
6510
6349
  if (!this.#chainConnectors.substrate) {
6511
- log.warn("[balances] no substrate connector or miniMetadata for module", mod.type);
6350
+ log.debug("[balances] no substrate connector or miniMetadata for module", mod.type);
6512
6351
  return rxjs.defer(() => rxjs.of({
6513
6352
  status: "initialising",
6514
6353
  balances: this.getStoredBalances(moduleAddressesByTokenId)
@@ -6559,7 +6398,7 @@ class BalancesProvider {
6559
6398
  address
6560
6399
  })));
6561
6400
  if (!this.#chainConnectors.evm) {
6562
- log.warn("[balances] no ethereum connector for module", mod.type);
6401
+ log.debug("[balances] no ethereum connector for module", mod.type);
6563
6402
  return rxjs.defer(() => rxjs.of({
6564
6403
  status: "initialising",
6565
6404
  balances: this.getStoredBalances(moduleAddressesByTokenId)
@@ -6591,56 +6430,6 @@ class BalancesProvider {
6591
6430
  })));
6592
6431
  });
6593
6432
  }
6594
- getSolanaNetworkModuleBalances$(networkId, tokensWithAddresses, mod) {
6595
- return util.getSharedObservable(`BalancesProvider.getSolanaNetworkModuleBalances$`, {
6596
- networkId,
6597
- mod,
6598
- tokensWithAddresses
6599
- }, () => {
6600
- if (!tokensWithAddresses.length) return rxjs.of({
6601
- status: "live",
6602
- balances: []
6603
- });
6604
- const moduleAddressesByTokenId = lodashEs.fromPairs(tokensWithAddresses.map(([token, addresses]) => [token.id, addresses]));
6605
-
6606
- // all balance ids expected in result set
6607
- const balanceIds = lodashEs.toPairs(moduleAddressesByTokenId).flatMap(([tokenId, addresses]) => addresses.map(address => getBalanceId({
6608
- tokenId,
6609
- address
6610
- })));
6611
- if (!this.#chainConnectors.solana) {
6612
- log.warn("[balances] no solana connector for module", mod.type);
6613
- return rxjs.defer(() => rxjs.of({
6614
- status: "initialising",
6615
- balances: this.getStoredBalances(moduleAddressesByTokenId)
6616
- }));
6617
- }
6618
- const moduleBalances$ = mod.subscribeBalances({
6619
- networkId,
6620
- tokensWithAddresses,
6621
- connector: this.#chainConnectors.solana
6622
- }).pipe(rxjs.catchError(() => rxjs.EMPTY),
6623
- // don't emit, let provider mark balances stale
6624
- rxjs.map(results => ({
6625
- status: "live",
6626
- // exclude zero balances
6627
- balances: results.success.filter(b => new Balance(b).total.planck > 0n)
6628
- })), rxjs.tap(results => {
6629
- this.updateStorage$(balanceIds, results);
6630
- }),
6631
- // shareReplay + keepAlive(0) keep the subscription alive while root observable is being unsubscribed+resubscribed, in case any input change
6632
- rxjs.shareReplay({
6633
- refCount: true,
6634
- bufferSize: 1
6635
- }), util.keepAlive(0));
6636
-
6637
- // defer the startWith call to start with up to date balances each time the observable is re-subscribed to
6638
- return rxjs.defer(() => moduleBalances$.pipe(rxjs.startWith({
6639
- status: "initialising",
6640
- balances: this.getStoredBalances(moduleAddressesByTokenId)
6641
- })));
6642
- });
6643
- }
6644
6433
  updateStorage$(balanceIds, balancesResult) {
6645
6434
  if (balancesResult.status !== "live") return;
6646
6435
  const storage = this.#storage.getValue();
@@ -6746,46 +6535,22 @@ class BalancesProvider {
6746
6535
  return lodashEs.fromPairs(lodashEs.toPairs(addressesByTokenId).map(([tokenId, addresses]) => {
6747
6536
  const networkId = chaindataProvider.parseTokenId(tokenId).networkId;
6748
6537
  const network = networksById[networkId];
6749
- return [tokenId, lodashEs.uniq(addresses.map(crypto.normalizeAddress)).filter(address => network && isAddressCompatibleWithNetwork(network, address))];
6538
+ return [tokenId, lodashEs.uniq(addresses.map(util.normalizeAddress)).filter(address => network && isAddressCompatibleWithNetwork(network, address))];
6750
6539
  }).filter(([, addresses]) => addresses.length > 0));
6751
6540
  }));
6752
6541
  }
6753
6542
  }
6754
- const isAccountPlatformCompatibleWithNetwork = (network, platform) => {
6543
+ const isAddressCompatibleWithNetwork = (network, address) => {
6755
6544
  switch (network.platform) {
6756
6545
  case "ethereum":
6757
- return platform === "ethereum";
6758
- case "solana":
6759
- return platform === "solana";
6546
+ return util.isEthereumAddress(address);
6760
6547
  case "polkadot":
6761
- {
6762
- switch (network.account) {
6763
- case "secp256k1":
6764
- return platform === "ethereum";
6765
- case "*25519":
6766
- return platform === "polkadot";
6767
- default:
6768
- throw new Error(`Unsupported polkadot network account type ${network.account}`);
6769
- }
6770
- }
6548
+ return util.isEthereumAddress(address) ? network.account === "secp256k1" : network.account !== "secp256k1";
6771
6549
  default:
6772
6550
  log.warn("Unsupported network platform", network);
6773
6551
  throw new Error("Unsupported network platform");
6774
6552
  }
6775
6553
  };
6776
-
6777
- /**
6778
- * If this is the address of an account, use isAccountCompatibleWithChain instead.
6779
- * Otherwise it could lead to a loss of funds
6780
- * @param chain
6781
- * @param address
6782
- * @returns
6783
- */
6784
- const isAddressCompatibleWithNetwork = (network, address) => {
6785
- // TODO try with return true to check if wallet filters correctly upfront
6786
- const accountPlatform = crypto.getAccountPlatformFromAddress(address);
6787
- return isAccountPlatformCompatibleWithNetwork(network, accountPlatform);
6788
- };
6789
6554
  const sortByBalanceId = (a, b) => getBalanceId(a).localeCompare(getBalanceId(b));
6790
6555
  const sortByMiniMetadataId = (a, b) => a.id.localeCompare(b.id);
6791
6556
 
@@ -6812,8 +6577,6 @@ exports.PlanckSumBalancesFormatter = PlanckSumBalancesFormatter;
6812
6577
  exports.SCALE_FACTOR = SCALE_FACTOR;
6813
6578
  exports.SUBTENSOR_MIN_STAKE_AMOUNT_PLANK = SUBTENSOR_MIN_STAKE_AMOUNT_PLANK;
6814
6579
  exports.SUBTENSOR_ROOT_NETUID = SUBTENSOR_ROOT_NETUID;
6815
- exports.SolNativeBalanceModule = SolNativeBalanceModule;
6816
- exports.SolNativeTokenConfigSchema = SolNativeTokenConfigSchema;
6817
6580
  exports.SubAssetsBalanceModule = SubAssetsBalanceModule;
6818
6581
  exports.SubAssetsTokenConfigSchema = SubAssetsTokenConfigSchema;
6819
6582
  exports.SubForeignAssetsBalanceModule = SubForeignAssetsBalanceModule;