@talismn/balances 0.0.0-pr2075-20250710052843 → 0.0.0-pr2075-20250710071608

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.
@@ -1277,19 +1277,6 @@ const getUniswapV2PairContractData = async (client, contractAddress) => {
1277
1277
  };
1278
1278
  };
1279
1279
 
1280
- // const TokenCacheSchema = EvmUniswapV2TokenSchema.pick({
1281
- // id: true,
1282
- // symbol: true,
1283
- // decimals: true,
1284
- // name: true,
1285
- // tokenAddress0: true,
1286
- // tokenAddress1: true,
1287
- // decimals0: true,
1288
- // decimals1: true,
1289
- // symbol0: true,
1290
- // symbol1: true,
1291
- // })
1292
-
1293
1280
  const TokenCacheSchema = z.discriminatedUnion("isValid", [z.strictObject({
1294
1281
  id: EvmUniswapV2TokenSchema.shape.id,
1295
1282
  isValid: z.literal(true),
@@ -1525,6 +1512,21 @@ const fetchRuntimeCallResult = async (connector, networkId, metadataRpc, apiName
1525
1512
  return call.value.dec(hex);
1526
1513
  };
1527
1514
 
1515
+ const hasStorageItem = (metadata, palletName, itemName) => {
1516
+ const pallet = metadata.pallets.find(p => p.name === palletName);
1517
+ if (!pallet || !pallet.storage) return false;
1518
+ return pallet.storage.items.some(item => item.name === itemName);
1519
+ };
1520
+ const hasStorageItems = (metadata, palletName, itemNames) => {
1521
+ const pallet = metadata.pallets.find(p => p.name === palletName);
1522
+ if (!pallet || !pallet.storage) return false;
1523
+ return itemNames.every(itemName => pallet.storage?.items.some(item => item.name === itemName));
1524
+ };
1525
+ const hasRuntimeApi = (metadata, apiName, method) => {
1526
+ const api = metadata.apis.find(api => api.name === apiName);
1527
+ if (!api || !api.methods) return false;
1528
+ return api.methods.some(m => m.name === method);
1529
+ };
1528
1530
  const getConstantValue = (metadataRpc, pallet, constant) => {
1529
1531
  const {
1530
1532
  unifiedMetadata,
@@ -1539,7 +1541,6 @@ const getConstantValue = (metadataRpc, pallet, constant) => {
1539
1541
  if (!encodedValue) throw new Error(`Constant ${pallet}.${constant} not found`);
1540
1542
  return codec.dec(encodedValue);
1541
1543
  };
1542
-
1543
1544
  const tryGetConstantValue = (metadataRpc, pallet, constant) => {
1544
1545
  const {
1545
1546
  unifiedMetadata,
@@ -1555,22 +1556,6 @@ const tryGetConstantValue = (metadataRpc, pallet, constant) => {
1555
1556
  return codec.dec(encodedValue);
1556
1557
  };
1557
1558
 
1558
- const hasStorageItem = (metadata, palletName, itemName) => {
1559
- const pallet = metadata.pallets.find(p => p.name === palletName);
1560
- if (!pallet || !pallet.storage) return false;
1561
- return pallet.storage.items.some(item => item.name === itemName);
1562
- };
1563
- const hasStorageItems = (metadata, palletName, itemNames) => {
1564
- const pallet = metadata.pallets.find(p => p.name === palletName);
1565
- if (!pallet || !pallet.storage) return false;
1566
- return itemNames.every(itemName => pallet.storage?.items.some(item => item.name === itemName));
1567
- };
1568
- const hasRuntimeApi = (metadata, apiName, method) => {
1569
- const api = metadata.apis.find(api => api.name === apiName);
1570
- if (!api || !api.methods) return false;
1571
- return api.methods.some(m => m.name === method);
1572
- };
1573
-
1574
1559
  const buildNetworkStorageCoders = (chainId, miniMetadata, coders) => {
1575
1560
  if (!miniMetadata.data) return null;
1576
1561
  const metadata = unifyMetadata(decAnyMetadata(miniMetadata.data));
@@ -3748,41 +3733,6 @@ const buildBaseQueries = (networkId, balanceDefs, miniMetadata) => {
3748
3733
  };
3749
3734
  }).filter(isNotNil);
3750
3735
  };
3751
-
3752
- // AccountInfo is the state_storage data format for nativeToken balances
3753
- // Theory: new chains will be at least on metadata v14, and so we won't need to hardcode their AccountInfo type.
3754
- // But for chains we want to support which aren't on metadata v14, hardcode them here:
3755
- // If the chain upgrades to metadata v14, this override will be ignored :)
3756
- // const RegularAccountInfoFallback = Struct({
3757
- // nonce: u32,
3758
- // consumers: u32,
3759
- // providers: u32,
3760
- // sufficients: u32,
3761
- // data: Struct({ free: u128, reserved: u128, miscFrozen: u128, feeFrozen: u128 }),
3762
- // })
3763
- // const NoSufficientsAccountInfoFallback = Struct({
3764
- // nonce: u32,
3765
- // consumers: u32,
3766
- // providers: u32,
3767
- // data: Struct({ free: u128, reserved: u128, miscFrozen: u128, feeFrozen: u128 }),
3768
- // })
3769
- // const AccountInfoOverrides: Record<
3770
- // string,
3771
- // typeof RegularAccountInfoFallback | typeof NoSufficientsAccountInfoFallback | undefined
3772
- // > = {
3773
- // // crown-sterlin is not yet on metadata v14
3774
- // "crown-sterling": NoSufficientsAccountInfoFallback,
3775
-
3776
- // // crust is not yet on metadata v14
3777
- // "crust": NoSufficientsAccountInfoFallback,
3778
-
3779
- // // kulupu is not yet on metadata v14
3780
- // "kulupu": RegularAccountInfoFallback,
3781
-
3782
- // // nftmart is not yet on metadata v14
3783
- // "nftmart": RegularAccountInfoFallback,
3784
- // }
3785
-
3786
3736
  const decodeBaseResult = (coder, value, networkId) => {
3787
3737
  /** NOTE: This type is only a hint for typescript, the chain can actually return whatever it wants to */
3788
3738
 
@@ -3833,15 +3783,6 @@ const decodeLocksResult = (coder, value, networkId) => {
3833
3783
  amount: (lock?.amount ?? 0n).toString()
3834
3784
  })) ?? [];
3835
3785
  return locksQueryLocks;
3836
-
3837
- // // locked values should be replaced entirely, not merged or appended
3838
- // const nonLockValues = balanceJson.values.filter(
3839
- // (v) => v.source !== "substrate-native-locks",
3840
- // )
3841
- // balanceJson.values = nonLockValues.concat(locksQueryLocks)
3842
-
3843
- // // fix any double-counting between Balances.Locks (for staking locks) and Staking.Ledger (for unbonding locks)
3844
- // balanceJson.values = updateStakingLocksUsingUnbondingLocks(balanceJson.values)
3845
3786
  };
3846
3787
  const decodeFreezesResult = (coder, value, networkId) => {
3847
3788
  /** NOTE: This type is only a hint for typescript, the chain can actually return whatever it wants to */
@@ -3854,14 +3795,6 @@ const decodeFreezesResult = (coder, value, networkId) => {
3854
3795
  amount: lock?.amount?.toString?.() ?? "0"
3855
3796
  })) ?? [];
3856
3797
  return freezesValues;
3857
-
3858
- // // freezes values should be replaced entirely, not merged or appended
3859
- // const nonFreezesValues = balanceJson.values.filter(
3860
- // (v) => v.source !== "substrate-native-freezes",
3861
- // )
3862
- // balanceJson.values = nonFreezesValues.concat(freezesQueryLocks)
3863
-
3864
- // return balanceJson
3865
3798
  };
3866
3799
  const decodeHoldsResult = (coder, value, networkId) => {
3867
3800
  /** NOTE: This type is only a hint for typescript, the chain can actually return whatever it wants to */
@@ -3880,14 +3813,6 @@ const decodeHoldsResult = (coder, value, networkId) => {
3880
3813
  }
3881
3814
  })) ?? [];
3882
3815
  return holdsValues;
3883
-
3884
- // // values should be replaced entirely, not merged or appended
3885
- // const nonHoldsValues = balanceJson.values.filter(
3886
- // (v) => v.source !== "substrate-native-holds",
3887
- // )
3888
- // balanceJson.values = nonHoldsValues.concat(holdsQueryLocks)
3889
-
3890
- // return balanceJson
3891
3816
  };
3892
3817
  const decodeStakingLedgerResult = (coder, value, networkId) => {
3893
3818
  /** NOTE: This type is only a hint for typescript, the chain can actually return whatever it wants to */
@@ -3902,29 +3827,6 @@ const decodeStakingLedgerResult = (coder, value, networkId) => {
3902
3827
  amount: totalUnlocking.toString()
3903
3828
  }];
3904
3829
  return stakingLedgerResults;
3905
-
3906
- // if (totalUnlocking <= 0n) unbondingQueryLocks = []
3907
- // else {
3908
- // unbondingQueryLocks = [
3909
- // {
3910
- // type: "locked",
3911
- // source: "substrate-native-unbonding",
3912
- // label: "Unbonding",
3913
- // amount: totalUnlocking.toString(),
3914
- // },
3915
- // ]
3916
- // }
3917
-
3918
- // // unbonding values should be replaced entirely, not merged or appended
3919
- // const nonUnbondingValues = balanceJson.values.filter(
3920
- // (v) => v.source !== "substrate-native-unbonding",
3921
- // )
3922
- // balanceJson.values = nonUnbondingValues.concat(unbondingQueryLocks)
3923
-
3924
- // // fix any double-counting between Balances.Locks (for staking locks) and Staking.Ledger (for unbonding locks)
3925
- // balanceJson.values = updateStakingLocksUsingUnbondingLocks(balanceJson.values)
3926
-
3927
- // return balanceJson
3928
3830
  };
3929
3831
  const decodePoolMemberResult = (coder, value, networkId) => {
3930
3832
  /** NOTE: This type is only a hint for typescript, the chain can actually return whatever it wants to */
@@ -3951,109 +3853,6 @@ const decodePoolMemberResult = (coder, value, networkId) => {
3951
3853
  };
3952
3854
  };
3953
3855
 
3954
- // const getBaseQuery = (
3955
- // networkId: string,
3956
- // address: string,
3957
- // coder: ScaleStorageCoder,
3958
- // ): RpcStateQuery<Array<AmountWithLabel<string>>> | null => {
3959
- // // For chains which are using metadata < v14
3960
- // const getFallbackStateKey = () => {
3961
- // const addressBytes = decodeAnyAddress(address) // TODO replace with modern api, this is slow
3962
- // const addressHash = blake2Concat(addressBytes).replace(/^0x/, "")
3963
- // const moduleHash = "26aa394eea5630e07c48ae0c9558cef7" // util_crypto.xxhashAsHex("System", 128);
3964
- // const storageHash = "b99d880ec681799c0cf30e8886371da9" // util_crypto.xxhashAsHex("Account", 128);
3965
- // const moduleStorageHash = `${moduleHash}${storageHash}` // System.Account is the state_storage key prefix for nativeToken balances
3966
- // return `0x${moduleStorageHash}${addressHash}`
3967
- // }
3968
-
3969
- // // const scaleCoder = chainStorageCoders.get(chainId)?.base
3970
- // // NOTE: Only use fallback key when `scaleCoder` is not defined
3971
- // // i.e. when chain doesn't have metadata v14/v15
3972
- // const stateKey = coder
3973
- // ? encodeStateKey(coder, `Invalid address in ${networkId} base query ${address}`, address)
3974
- // : getFallbackStateKey()
3975
- // if (!stateKey) return null
3976
-
3977
- // const decodeResult = (change: string | null) => {
3978
- // // BEGIN: Handle chains which use metadata < v14
3979
- // let oldChainBalance = null
3980
- // if (!coder) {
3981
- // const scaleAccountInfo = AccountInfoOverrides[networkId]
3982
- // if (scaleAccountInfo === undefined) {
3983
- // // chain metadata version is < 15 and we also don't have an override hardcoded in
3984
- // // the best way to handle this case: log a warning and return an empty balance
3985
- // log.debug(
3986
- // `Native token on chain ${networkId} has no balance type for decoding. Defaulting to a balance of 0 (zero).`,
3987
- // )
3988
- // return []
3989
- // }
3990
-
3991
- // try {
3992
- // // eslint-disable-next-line no-var
3993
- // oldChainBalance = change === null ? null : scaleAccountInfo.dec(change)
3994
- // } catch (error) {
3995
- // log.warn(
3996
- // `Failed to create pre-metadataV14 balance type for native on chain ${networkId}: ${error?.toString()}`,
3997
- // )
3998
- // return []
3999
- // }
4000
- // }
4001
- // // END: Handle chains which use metadata < v14
4002
-
4003
- // /** NOTE: This type is only a hint for typescript, the chain can actually return whatever it wants to */
4004
- // type DecodedType = {
4005
- // data?: {
4006
- // flags?: bigint
4007
- // free?: bigint
4008
- // frozen?: bigint
4009
- // reserved?: bigint
4010
-
4011
- // // deprecated fields (they only show up on old chains)
4012
- // feeFrozen?: bigint
4013
- // miscFrozen?: bigint
4014
- // }
4015
- // }
4016
- // const decoded =
4017
- // decodeScale<DecodedType>(
4018
- // coder,
4019
- // change,
4020
- // `Failed to decode base native balance on chain ${networkId}`,
4021
- // ) ?? oldChainBalance
4022
-
4023
- // const free = (decoded?.data?.free ?? 0n).toString()
4024
- // const reserved = (decoded?.data?.reserved ?? 0n).toString()
4025
- // const miscLock = (
4026
- // (decoded?.data?.miscFrozen ?? 0n) +
4027
- // // new chains don't split their `frozen` amount into `feeFrozen` and `miscFrozen`.
4028
- // // for these chains, we'll use the `frozen` amount as `miscFrozen`.
4029
- // ((decoded?.data as DecodedType["data"])?.frozen ?? 0n)
4030
- // ).toString()
4031
- // const feesLock = (decoded?.data?.feeFrozen ?? 0n).toString()
4032
-
4033
- // // even if these values are 0, we still need to add them to the balanceJson.values array
4034
- // // so that the balance pool can handle newly zeroed balances
4035
- // // const existingValues = Object.fromEntries(
4036
- // // balanceJson.values.map((v) => [getValueId(v), v]),
4037
- // // )
4038
- // const newValues: AmountWithLabel<string>[] = [
4039
- // { type: "free", label: "free", amount: free.toString() },
4040
- // { type: "reserved", label: "reserved", amount: reserved.toString() },
4041
- // { type: "locked", label: "misc", amount: miscLock.toString() },
4042
- // { type: "locked", label: "fees", amount: feesLock.toString() },
4043
- // ]
4044
-
4045
- // return newValues
4046
-
4047
- // // const newValuesObj = Object.fromEntries(newValues.map((v) => [getValueId(v), v]))
4048
-
4049
- // // balanceJson.values = Object.values({ ...existingValues, ...newValuesObj })
4050
-
4051
- // // return balanceJson
4052
- // }
4053
-
4054
- // return { chainId: networkId, stateKey, decodeResult }
4055
- // }
4056
-
4057
3856
  /**
4058
3857
  * Each nominationPool in the nominationPools pallet has access to some accountIds which have no
4059
3858
  * associated private key. Instead, they are derived from this function.
@@ -4101,7 +3900,9 @@ const buildNomPoolQueries = (networkId, partialBalances, miniMetadata) => {
4101
3900
  }) => total + BigInt(amount ?? "0"), 0n);
4102
3901
  return {
4103
3902
  ...balance,
4104
- values: [...(balance.values ?? []), {
3903
+ values: [...(balance.values?.filter(({
3904
+ source
3905
+ }) => source !== "nompools-staking") ?? []), {
4105
3906
  source: "nompools-staking",
4106
3907
  type: "nompool",
4107
3908
  label: "nompools-staking",
@@ -4231,7 +4032,9 @@ const fetchBalances$2 = async ({
4231
4032
  const subtensorBalancesByAddress = await firstValueFrom(subtensorBalances$);
4232
4033
  for (const [address, subtensorBalances] of Object.entries(subtensorBalancesByAddress)) {
4233
4034
  const balance = balances.find(b => b.address === address);
4234
- if (balance?.values) balance.values.push(...subtensorBalances);
4035
+ if (balance?.values) balance.values = [...balance.values.filter(({
4036
+ source
4037
+ }) => source !== "subtensor-staking"), ...subtensorBalances];
4235
4038
  }
4236
4039
  return {
4237
4040
  success: balances,
@@ -4345,13 +4148,9 @@ const getMiniMetadata$2 = ({
4345
4148
  }, {
4346
4149
  pallet: "Staking",
4347
4150
  items: ["Ledger"]
4348
- },
4349
- // TotalColdkeyStake is used until v.2.2.1, then it is replaced by StakingHotkeys+Stake
4350
- // Need to keep TotalColdkeyStake for a while so chaindata keeps including it in miniMetadatas, so it doesnt break old versions of the wallet
4351
- // TODO: Since chaindata v4 this is safe to now delete
4352
- {
4151
+ }, {
4353
4152
  pallet: "SubtensorModule",
4354
- items: ["TotalColdkeyStake", "StakingHotkeys", "Stake"]
4153
+ items: ["StakingHotkeys", "Stake"]
4355
4154
  }], [{
4356
4155
  runtimeApi: "StakeInfoRuntimeApi",
4357
4156
  methods: ["get_stake_info_for_coldkey"]
@@ -4483,7 +4282,9 @@ const subscribeBalances$2 = ({
4483
4282
  // add subtensor balances to base balances
4484
4283
  for (const [address, subtensorBalances] of toPairs(subtensorBalancesByAddress)) {
4485
4284
  const balance = baseBalances.find(b => b.address === address);
4486
- if (balance?.values) balance.values.push(...subtensorBalances);
4285
+ if (balance?.values) balance.values = [...balance.values.filter(({
4286
+ source
4287
+ }) => source !== "subtensor-staking"), ...subtensorBalances];
4487
4288
  }
4488
4289
  return {
4489
4290
  success: baseBalances,
@@ -6338,8 +6139,10 @@ const getMetadataRpc = async (chainConnector, networkId) => {
6338
6139
  if (CACHE$1.has(networkId)) return CACHE$1.get(networkId);
6339
6140
  const pResult = fetchBestMetadata((method, params, isCacheable) => chainConnector.send(networkId, method, params, isCacheable, {
6340
6141
  expectErrors: true
6341
- }), true // allow fallback to 14 as modules dont use any v15 or v16 specifics yet
6342
- );
6142
+ }),
6143
+ // do not allow fallback to v14 unless unavailable.
6144
+ // substrate-native and substrate-hydration need v15 metadata for runtime api calls
6145
+ false);
6343
6146
  CACHE$1.set(networkId, pResult);
6344
6147
  try {
6345
6148
  return await pResult;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@talismn/balances",
3
- "version": "0.0.0-pr2075-20250710052843",
3
+ "version": "0.0.0-pr2075-20250710071608",
4
4
  "author": "Talisman",
5
5
  "homepage": "https://talisman.xyz",
6
6
  "license": "GPL-3.0-or-later",
@@ -34,13 +34,13 @@
34
34
  "scale-ts": "^1.6.1",
35
35
  "viem": "^2.27.3",
36
36
  "zod": "^3.25.62",
37
- "@talismn/chaindata-provider": "0.0.0-pr2075-20250710052843",
38
- "@talismn/chain-connector": "0.0.0-pr2075-20250710052843",
39
- "@talismn/chain-connector-evm": "0.0.0-pr2075-20250710052843",
40
- "@talismn/scale": "0.0.0-pr2075-20250710052843",
41
- "@talismn/util": "0.0.0-pr2075-20250710052843",
42
- "@talismn/sapi": "0.0.0-pr2075-20250710052843",
43
- "@talismn/token-rates": "0.0.0-pr2075-20250710052843"
37
+ "@talismn/chain-connector-evm": "0.0.0-pr2075-20250710071608",
38
+ "@talismn/chaindata-provider": "0.0.0-pr2075-20250710071608",
39
+ "@talismn/chain-connector": "0.0.0-pr2075-20250710071608",
40
+ "@talismn/sapi": "0.0.0-pr2075-20250710071608",
41
+ "@talismn/scale": "0.0.0-pr2075-20250710071608",
42
+ "@talismn/token-rates": "0.0.0-pr2075-20250710071608",
43
+ "@talismn/util": "0.0.0-pr2075-20250710071608"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@polkadot/api-contract": "16.1.2",
@@ -1 +0,0 @@
1
- export declare const getConstantValue: <T>(metadataRpc: `0x${string}`, pallet: string, constant: string) => T;