@talismn/balances 3.1.3 → 3.1.4

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.
package/dist/index.d.mts CHANGED
@@ -415,9 +415,12 @@ export declare class Balance {
415
415
  coingeckoId?: string | undefined;
416
416
  noDiscovery?: boolean | undefined;
417
417
  mirrorOf?: string | undefined;
418
- type: "substrate-psp22";
418
+ type: "substrate-hydration";
419
419
  platform: "polkadot";
420
- contractAddress: string;
420
+ onChainId: number;
421
+ assetType: "Erc20" | "External" | "Token";
422
+ isSufficient: boolean;
423
+ existentialDeposit: string;
421
424
  } | {
422
425
  id: string;
423
426
  networkId: string;
@@ -429,12 +432,18 @@ export declare class Balance {
429
432
  coingeckoId?: string | undefined;
430
433
  noDiscovery?: boolean | undefined;
431
434
  mirrorOf?: string | undefined;
432
- type: "substrate-foreignassets";
433
- platform: "polkadot";
434
- onChainId: string;
435
- isFrozen?: boolean | undefined;
436
- isSufficient: boolean;
437
- existentialDeposit: string;
435
+ type: "evm-uniswapv2";
436
+ platform: "ethereum";
437
+ contractAddress: `0x${string}`;
438
+ isCustom?: boolean | undefined;
439
+ symbol0: string;
440
+ symbol1: string;
441
+ decimals0: number;
442
+ decimals1: number;
443
+ tokenAddress0: `0x${string}`;
444
+ tokenAddress1: `0x${string}`;
445
+ coingeckoId0?: string | undefined;
446
+ coingeckoId1?: string | undefined;
438
447
  } | {
439
448
  id: string;
440
449
  networkId: string;
@@ -446,12 +455,9 @@ export declare class Balance {
446
455
  coingeckoId?: string | undefined;
447
456
  noDiscovery?: boolean | undefined;
448
457
  mirrorOf?: string | undefined;
449
- type: "substrate-dtao";
458
+ type: "substrate-psp22";
450
459
  platform: "polkadot";
451
- netuid: number;
452
- subnetName?: string | undefined;
453
- hotkey?: string | undefined;
454
- isTransferable: boolean;
460
+ contractAddress: string;
455
461
  } | {
456
462
  id: string;
457
463
  networkId: string;
@@ -490,18 +496,12 @@ export declare class Balance {
490
496
  coingeckoId?: string | undefined;
491
497
  noDiscovery?: boolean | undefined;
492
498
  mirrorOf?: string | undefined;
493
- type: "evm-uniswapv2";
494
- platform: "ethereum";
495
- contractAddress: `0x${string}`;
496
- isCustom?: boolean | undefined;
497
- symbol0: string;
498
- symbol1: string;
499
- decimals0: number;
500
- decimals1: number;
501
- tokenAddress0: `0x${string}`;
502
- tokenAddress1: `0x${string}`;
503
- coingeckoId0?: string | undefined;
504
- coingeckoId1?: string | undefined;
499
+ type: "substrate-assets";
500
+ platform: "polkadot";
501
+ assetId: string;
502
+ isFrozen?: boolean | undefined;
503
+ isSufficient: boolean;
504
+ existentialDeposit: string;
505
505
  } | {
506
506
  id: string;
507
507
  networkId: string;
@@ -513,12 +513,12 @@ export declare class Balance {
513
513
  coingeckoId?: string | undefined;
514
514
  noDiscovery?: boolean | undefined;
515
515
  mirrorOf?: string | undefined;
516
- type: "substrate-assets";
516
+ type: "substrate-dtao";
517
517
  platform: "polkadot";
518
- assetId: string;
519
- isFrozen?: boolean | undefined;
520
- isSufficient: boolean;
521
- existentialDeposit: string;
518
+ netuid: number;
519
+ subnetName?: string | undefined;
520
+ hotkey?: string | undefined;
521
+ isTransferable: boolean;
522
522
  } | {
523
523
  id: string;
524
524
  networkId: string;
@@ -530,8 +530,11 @@ export declare class Balance {
530
530
  coingeckoId?: string | undefined;
531
531
  noDiscovery?: boolean | undefined;
532
532
  mirrorOf?: string | undefined;
533
- type: "substrate-native";
533
+ type: "substrate-foreignassets";
534
534
  platform: "polkadot";
535
+ onChainId: string;
536
+ isFrozen?: boolean | undefined;
537
+ isSufficient: boolean;
535
538
  existentialDeposit: string;
536
539
  } | {
537
540
  id: string;
@@ -544,9 +547,8 @@ export declare class Balance {
544
547
  coingeckoId?: string | undefined;
545
548
  noDiscovery?: boolean | undefined;
546
549
  mirrorOf?: string | undefined;
547
- type: "substrate-tokens";
550
+ type: "substrate-native";
548
551
  platform: "polkadot";
549
- onChainId: string | number;
550
552
  existentialDeposit: string;
551
553
  } | {
552
554
  id: string;
@@ -559,11 +561,9 @@ export declare class Balance {
559
561
  coingeckoId?: string | undefined;
560
562
  noDiscovery?: boolean | undefined;
561
563
  mirrorOf?: string | undefined;
562
- type: "substrate-hydration";
564
+ type: "substrate-tokens";
563
565
  platform: "polkadot";
564
- onChainId: number;
565
- assetType: "Erc20" | "External" | "Token";
566
- isSufficient: boolean;
566
+ onChainId: string | number;
567
567
  existentialDeposit: string;
568
568
  } | {
569
569
  id: string;
package/dist/index.d.ts CHANGED
@@ -415,9 +415,18 @@ export declare class Balance {
415
415
  coingeckoId?: string | undefined;
416
416
  noDiscovery?: boolean | undefined;
417
417
  mirrorOf?: string | undefined;
418
- type: "evm-erc20";
418
+ type: "evm-uniswapv2";
419
419
  platform: "ethereum";
420
420
  contractAddress: `0x${string}`;
421
+ isCustom?: boolean | undefined;
422
+ symbol0: string;
423
+ symbol1: string;
424
+ decimals0: number;
425
+ decimals1: number;
426
+ tokenAddress0: `0x${string}`;
427
+ tokenAddress1: `0x${string}`;
428
+ coingeckoId0?: string | undefined;
429
+ coingeckoId1?: string | undefined;
421
430
  } | {
422
431
  id: string;
423
432
  networkId: string;
@@ -442,18 +451,9 @@ export declare class Balance {
442
451
  coingeckoId?: string | undefined;
443
452
  noDiscovery?: boolean | undefined;
444
453
  mirrorOf?: string | undefined;
445
- type: "evm-uniswapv2";
454
+ type: "evm-erc20";
446
455
  platform: "ethereum";
447
456
  contractAddress: `0x${string}`;
448
- isCustom?: boolean | undefined;
449
- symbol0: string;
450
- symbol1: string;
451
- decimals0: number;
452
- decimals1: number;
453
- tokenAddress0: `0x${string}`;
454
- tokenAddress1: `0x${string}`;
455
- coingeckoId0?: string | undefined;
456
- coingeckoId1?: string | undefined;
457
457
  } | {
458
458
  id: string;
459
459
  networkId: string;
package/dist/index.js CHANGED
@@ -3987,6 +3987,43 @@ const findDTaoRootStakeHold = (balance) => {
3987
3987
  }
3988
3988
  return null;
3989
3989
  };
3990
+ const hasRootStakeHoldStorage = (unifiedMetadata) => hasStorageItems(unifiedMetadata, "SubtensorModule", ["RootStakeUnlockInterval", "LastColdkeyHotkeyStakeBlock"]);
3991
+ /**
3992
+ * How long a `RootStakeUnlockInterval` read is reused. The chain has no extrinsic that sets
3993
+ * it: it changes with a runtime upgrade, which brings new metadata and so a new cache entry,
3994
+ * or through root's `set_storage`, which this bounds.
3995
+ */
3996
+ const UNLOCK_INTERVAL_CACHE_MS = 6e5;
3997
+ /** cached reads by network, per parsed runtime metadata */
3998
+ const unlockIntervalCache = /* @__PURE__ */ new WeakMap();
3999
+ /**
4000
+ * The chain's `RootStakeUnlockInterval`: how many blocks root stake stays held after the
4001
+ * pair's last root stake operation. 0 (disabled) unless governance enables it, either by
4002
+ * setting the storage entry or with a runtime default that leaves it unset (metadata
4003
+ * fallback). Null on chains without root stake holds.
4004
+ *
4005
+ * Read at most once per `UNLOCK_INTERVAL_CACHE_MS` for each runtime, so that the 6s balance
4006
+ * polls don't spend a request on a value that hardly ever changes.
4007
+ */
4008
+ const fetchRootStakeUnlockInterval = async (connector, networkId, metadataRpc, at) => {
4009
+ const { unifiedMetadata, builder } = parseMetadataRpcCached(metadataRpc);
4010
+ if (!hasRootStakeHoldStorage(unifiedMetadata)) return null;
4011
+ const byNetwork = unlockIntervalCache.get(builder) ?? /* @__PURE__ */ new Map();
4012
+ unlockIntervalCache.set(builder, byNetwork);
4013
+ const cached = byNetwork.get(networkId);
4014
+ if (cached && Date.now() - cached.readAt < UNLOCK_INTERVAL_CACHE_MS) return cached.interval;
4015
+ try {
4016
+ const interval = await fetchUnlockInterval(connector, networkId, unifiedMetadata, builder, at);
4017
+ byNetwork.set(networkId, {
4018
+ interval,
4019
+ readAt: Date.now()
4020
+ });
4021
+ return interval;
4022
+ } catch (cause) {
4023
+ log_default.warn(`Failed to fetch root stake unlock interval on ${networkId}`, { cause });
4024
+ throw cause;
4025
+ }
4026
+ };
3990
4027
  /**
3991
4028
  * Fetches active root-stake hold windows (spec 441): when `RootStakeUnlockInterval` is
3992
4029
  * non-zero, root stake cannot leave root (remove/move/swap/transfer) until `interval`
@@ -3994,17 +4031,15 @@ const findDTaoRootStakeHold = (balance) => {
3994
4031
  * Pairs already past their window are omitted — a returned hold means the pair's root
3995
4032
  * stake is currently unremovable.
3996
4033
  *
3997
- * The interval is 0 (disabled) unless governance enables it — either by setting the
3998
- * storage entry or via a runtime default that leaves it unset (metadata fallback). The
3999
- * per-pair queries only run when it is non-zero.
4034
+ * The per-pair queries only run when the interval (`fetchRootStakeUnlockInterval`, cached)
4035
+ * is non-zero.
4000
4036
  */
4001
4037
  const fetchRootStakeHolds = async (connector, networkId, metadataRpc, rootPairs, at) => {
4002
4038
  if (!rootPairs.length) return [];
4003
- const { unifiedMetadata, builder } = parseMetadataRpcCached(metadataRpc);
4004
- if (!hasStorageItems(unifiedMetadata, "SubtensorModule", ["RootStakeUnlockInterval", "LastColdkeyHotkeyStakeBlock"])) return [];
4039
+ const interval = await fetchRootStakeUnlockInterval(connector, networkId, metadataRpc, at);
4040
+ if (!interval) return [];
4041
+ const { builder } = parseMetadataRpcCached(metadataRpc);
4005
4042
  try {
4006
- const interval = await fetchUnlockInterval(connector, networkId, unifiedMetadata, builder, at);
4007
- if (interval === 0n) return [];
4008
4043
  const { currentBlock, lastStakeBlocks } = await fetchLastStakeBlocks(connector, networkId, builder, rootPairs, at);
4009
4044
  return lastStakeBlocks.flatMap(({ address, hotkey, lastStakeBlock }) => {
4010
4045
  if (lastStakeBlock === 0n) return [];
@@ -4090,7 +4125,12 @@ const fetchLastStakeBlocks = async (connector, networkId, builder, rootPairs, at
4090
4125
  };
4091
4126
  };
4092
4127
  // src/modules/substrate-dtao/fetchBalances.ts
4093
- const fetchBalances$5 = async ({ networkId, tokensWithAddresses, connector, miniMetadata, signal }) => {
4128
+ const fetchBalances$5 = (args) => fetchBalancesAt(args);
4129
+ /**
4130
+ * Fetches the balances as of block `at`, or as of the best block when `at` is not given.
4131
+ * Every read of the fetch is pinned to that one block.
4132
+ */
4133
+ const fetchBalancesAt = async ({ networkId, tokensWithAddresses, connector, miniMetadata, signal }, at) => {
4094
4134
  if (!tokensWithAddresses.length) return {
4095
4135
  success: [],
4096
4136
  errors: []
@@ -4130,10 +4170,16 @@ const fetchBalances$5 = async ({ networkId, tokensWithAddresses, connector, mini
4130
4170
  };
4131
4171
  }
4132
4172
  const addresses = (0, lodash_es.uniq)(balanceDefs.map((def) => def.address));
4173
+ const metadataRpc = miniMetadata.data;
4133
4174
  try {
4134
- const { builder } = parseMetadataRpcCached(miniMetadata.data);
4135
- const at = await fetchBestBlockHash(connector, networkId);
4136
- const stakeInfos = await fetchRuntimeCallResult(connector, networkId, builder, "StakeInfoRuntimeApi", "get_stake_info_for_coldkeys", [addresses], at);
4175
+ const { builder } = parseMetadataRpcCached(metadataRpc);
4176
+ const blockHash = await (at ?? fetchBestBlockHash(connector, networkId));
4177
+ const [stakeInfos, convictionLocks, basketClaims] = await Promise.all([
4178
+ fetchRuntimeCallResult(connector, networkId, builder, "StakeInfoRuntimeApi", "get_stake_info_for_coldkeys", [addresses], blockHash),
4179
+ fetchConvictionLocks(connector, networkId, metadataRpc, addresses, blockHash),
4180
+ fetchBasketClaims(connector, networkId, metadataRpc, addresses, blockHash),
4181
+ fetchRootStakeUnlockInterval(connector, networkId, metadataRpc, blockHash).catch(() => null)
4182
+ ]);
4137
4183
  const rootPairs = [];
4138
4184
  const seenRootPairs = /* @__PURE__ */ new Set();
4139
4185
  for (const [address, stakes] of stakeInfos) for (const stake of stakes) {
@@ -4146,15 +4192,7 @@ const fetchBalances$5 = async ({ networkId, tokensWithAddresses, connector, mini
4146
4192
  hotkey: stake.hotkey
4147
4193
  });
4148
4194
  }
4149
- const [convictionLocks, basketClaims, rootStakeHolds] = miniMetadata.data ? await Promise.all([
4150
- fetchConvictionLocks(connector, networkId, miniMetadata.data, addresses, at),
4151
- fetchBasketClaims(connector, networkId, miniMetadata.data, addresses, at),
4152
- fetchRootStakeHolds(connector, networkId, miniMetadata.data, rootPairs, at)
4153
- ]) : [
4154
- [],
4155
- [],
4156
- []
4157
- ];
4195
+ const rootStakeHolds = await fetchRootStakeHolds(connector, networkId, metadataRpc, rootPairs, blockHash);
4158
4196
  const upsertBalance = (acc, address, tokenId, balance) => {
4159
4197
  const key = `${address}:${tokenId}`;
4160
4198
  const recordedBalance = acc[key];
@@ -4515,6 +4553,10 @@ const isEffectivelyEqualDTaoBalance = (previous, next) => {
4515
4553
  return result.equivalence;
4516
4554
  };
4517
4555
  // src/modules/substrate-dtao/subscribeBalances.ts
4556
+ /**
4557
+ * Half of Bittensor's 12s block time, so a new block is read within 6s of being produced.
4558
+ * A poll that finds the same best block as the last good poll ends after that one request.
4559
+ */
4518
4560
  const SUBSCRIPTION_INTERVAL = 6e3;
4519
4561
  const subscribeBalances$5 = ({ networkId, tokensWithAddresses, connector, miniMetadata }) => {
4520
4562
  if (!tokensWithAddresses.length) return (0, rxjs.of)({
@@ -4524,21 +4566,32 @@ const subscribeBalances$5 = ({ networkId, tokensWithAddresses, connector, miniMe
4524
4566
  const balanceDefs = getBalanceDefs(tokensWithAddresses);
4525
4567
  return new rxjs.Observable((subscriber) => {
4526
4568
  const abortController = new AbortController();
4569
+ /** the block the last poll read without errors */
4570
+ let lastGoodBlockHash = null;
4527
4571
  const poll = async () => {
4528
4572
  try {
4529
4573
  if (abortController.signal.aborted) return;
4530
- const balances = await fetchBalances$5({
4574
+ const bestBlock = fetchBestBlockHash(connector, networkId);
4575
+ const blockHash = await bestBlock.catch(() => void 0);
4576
+ if (abortController.signal.aborted) return;
4577
+ if (blockHash && blockHash === lastGoodBlockHash) {
4578
+ setTimeout(poll, SUBSCRIPTION_INTERVAL);
4579
+ return;
4580
+ }
4581
+ const balances = await fetchBalancesAt({
4531
4582
  networkId,
4532
4583
  tokensWithAddresses,
4533
4584
  connector,
4534
4585
  miniMetadata,
4535
4586
  signal: abortController.signal
4536
- });
4587
+ }, blockHash ?? bestBlock);
4537
4588
  if (abortController.signal.aborted) return;
4589
+ lastGoodBlockHash = balances.errors.length ? null : blockHash ?? null;
4538
4590
  (0, _talismn_util.reportJsActivity)(`poll ${MODULE_TYPE$5} ${networkId} (${balances.success.length})`);
4539
4591
  subscriber.next(balances);
4540
4592
  } catch (error) {
4541
4593
  if (abortController.signal.aborted) return;
4594
+ lastGoodBlockHash = null;
4542
4595
  log_default.error("Error", {
4543
4596
  module: MODULE_TYPE$5,
4544
4597
  networkId,