@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 +37 -37
- package/dist/index.d.ts +11 -11
- package/dist/index.js +75 -22
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +75 -22
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.mjs
CHANGED
|
@@ -3960,6 +3960,43 @@ const findDTaoRootStakeHold = (balance) => {
|
|
|
3960
3960
|
}
|
|
3961
3961
|
return null;
|
|
3962
3962
|
};
|
|
3963
|
+
const hasRootStakeHoldStorage = (unifiedMetadata) => hasStorageItems(unifiedMetadata, "SubtensorModule", ["RootStakeUnlockInterval", "LastColdkeyHotkeyStakeBlock"]);
|
|
3964
|
+
/**
|
|
3965
|
+
* How long a `RootStakeUnlockInterval` read is reused. The chain has no extrinsic that sets
|
|
3966
|
+
* it: it changes with a runtime upgrade, which brings new metadata and so a new cache entry,
|
|
3967
|
+
* or through root's `set_storage`, which this bounds.
|
|
3968
|
+
*/
|
|
3969
|
+
const UNLOCK_INTERVAL_CACHE_MS = 6e5;
|
|
3970
|
+
/** cached reads by network, per parsed runtime metadata */
|
|
3971
|
+
const unlockIntervalCache = /* @__PURE__ */ new WeakMap();
|
|
3972
|
+
/**
|
|
3973
|
+
* The chain's `RootStakeUnlockInterval`: how many blocks root stake stays held after the
|
|
3974
|
+
* pair's last root stake operation. 0 (disabled) unless governance enables it, either by
|
|
3975
|
+
* setting the storage entry or with a runtime default that leaves it unset (metadata
|
|
3976
|
+
* fallback). Null on chains without root stake holds.
|
|
3977
|
+
*
|
|
3978
|
+
* Read at most once per `UNLOCK_INTERVAL_CACHE_MS` for each runtime, so that the 6s balance
|
|
3979
|
+
* polls don't spend a request on a value that hardly ever changes.
|
|
3980
|
+
*/
|
|
3981
|
+
const fetchRootStakeUnlockInterval = async (connector, networkId, metadataRpc, at) => {
|
|
3982
|
+
const { unifiedMetadata, builder } = parseMetadataRpcCached(metadataRpc);
|
|
3983
|
+
if (!hasRootStakeHoldStorage(unifiedMetadata)) return null;
|
|
3984
|
+
const byNetwork = unlockIntervalCache.get(builder) ?? /* @__PURE__ */ new Map();
|
|
3985
|
+
unlockIntervalCache.set(builder, byNetwork);
|
|
3986
|
+
const cached = byNetwork.get(networkId);
|
|
3987
|
+
if (cached && Date.now() - cached.readAt < UNLOCK_INTERVAL_CACHE_MS) return cached.interval;
|
|
3988
|
+
try {
|
|
3989
|
+
const interval = await fetchUnlockInterval(connector, networkId, unifiedMetadata, builder, at);
|
|
3990
|
+
byNetwork.set(networkId, {
|
|
3991
|
+
interval,
|
|
3992
|
+
readAt: Date.now()
|
|
3993
|
+
});
|
|
3994
|
+
return interval;
|
|
3995
|
+
} catch (cause) {
|
|
3996
|
+
log_default.warn(`Failed to fetch root stake unlock interval on ${networkId}`, { cause });
|
|
3997
|
+
throw cause;
|
|
3998
|
+
}
|
|
3999
|
+
};
|
|
3963
4000
|
/**
|
|
3964
4001
|
* Fetches active root-stake hold windows (spec 441): when `RootStakeUnlockInterval` is
|
|
3965
4002
|
* non-zero, root stake cannot leave root (remove/move/swap/transfer) until `interval`
|
|
@@ -3967,17 +4004,15 @@ const findDTaoRootStakeHold = (balance) => {
|
|
|
3967
4004
|
* Pairs already past their window are omitted — a returned hold means the pair's root
|
|
3968
4005
|
* stake is currently unremovable.
|
|
3969
4006
|
*
|
|
3970
|
-
* The
|
|
3971
|
-
*
|
|
3972
|
-
* per-pair queries only run when it is non-zero.
|
|
4007
|
+
* The per-pair queries only run when the interval (`fetchRootStakeUnlockInterval`, cached)
|
|
4008
|
+
* is non-zero.
|
|
3973
4009
|
*/
|
|
3974
4010
|
const fetchRootStakeHolds = async (connector, networkId, metadataRpc, rootPairs, at) => {
|
|
3975
4011
|
if (!rootPairs.length) return [];
|
|
3976
|
-
const
|
|
3977
|
-
if (!
|
|
4012
|
+
const interval = await fetchRootStakeUnlockInterval(connector, networkId, metadataRpc, at);
|
|
4013
|
+
if (!interval) return [];
|
|
4014
|
+
const { builder } = parseMetadataRpcCached(metadataRpc);
|
|
3978
4015
|
try {
|
|
3979
|
-
const interval = await fetchUnlockInterval(connector, networkId, unifiedMetadata, builder, at);
|
|
3980
|
-
if (interval === 0n) return [];
|
|
3981
4016
|
const { currentBlock, lastStakeBlocks } = await fetchLastStakeBlocks(connector, networkId, builder, rootPairs, at);
|
|
3982
4017
|
return lastStakeBlocks.flatMap(({ address, hotkey, lastStakeBlock }) => {
|
|
3983
4018
|
if (lastStakeBlock === 0n) return [];
|
|
@@ -4063,7 +4098,12 @@ const fetchLastStakeBlocks = async (connector, networkId, builder, rootPairs, at
|
|
|
4063
4098
|
};
|
|
4064
4099
|
};
|
|
4065
4100
|
// src/modules/substrate-dtao/fetchBalances.ts
|
|
4066
|
-
const fetchBalances$5 =
|
|
4101
|
+
const fetchBalances$5 = (args) => fetchBalancesAt(args);
|
|
4102
|
+
/**
|
|
4103
|
+
* Fetches the balances as of block `at`, or as of the best block when `at` is not given.
|
|
4104
|
+
* Every read of the fetch is pinned to that one block.
|
|
4105
|
+
*/
|
|
4106
|
+
const fetchBalancesAt = async ({ networkId, tokensWithAddresses, connector, miniMetadata, signal }, at) => {
|
|
4067
4107
|
if (!tokensWithAddresses.length) return {
|
|
4068
4108
|
success: [],
|
|
4069
4109
|
errors: []
|
|
@@ -4103,10 +4143,16 @@ const fetchBalances$5 = async ({ networkId, tokensWithAddresses, connector, mini
|
|
|
4103
4143
|
};
|
|
4104
4144
|
}
|
|
4105
4145
|
const addresses = uniq(balanceDefs.map((def) => def.address));
|
|
4146
|
+
const metadataRpc = miniMetadata.data;
|
|
4106
4147
|
try {
|
|
4107
|
-
const { builder } = parseMetadataRpcCached(
|
|
4108
|
-
const
|
|
4109
|
-
const stakeInfos
|
|
4148
|
+
const { builder } = parseMetadataRpcCached(metadataRpc);
|
|
4149
|
+
const blockHash = await (at ?? fetchBestBlockHash(connector, networkId));
|
|
4150
|
+
const [stakeInfos, convictionLocks, basketClaims] = await Promise.all([
|
|
4151
|
+
fetchRuntimeCallResult(connector, networkId, builder, "StakeInfoRuntimeApi", "get_stake_info_for_coldkeys", [addresses], blockHash),
|
|
4152
|
+
fetchConvictionLocks(connector, networkId, metadataRpc, addresses, blockHash),
|
|
4153
|
+
fetchBasketClaims(connector, networkId, metadataRpc, addresses, blockHash),
|
|
4154
|
+
fetchRootStakeUnlockInterval(connector, networkId, metadataRpc, blockHash).catch(() => null)
|
|
4155
|
+
]);
|
|
4110
4156
|
const rootPairs = [];
|
|
4111
4157
|
const seenRootPairs = /* @__PURE__ */ new Set();
|
|
4112
4158
|
for (const [address, stakes] of stakeInfos) for (const stake of stakes) {
|
|
@@ -4119,15 +4165,7 @@ const fetchBalances$5 = async ({ networkId, tokensWithAddresses, connector, mini
|
|
|
4119
4165
|
hotkey: stake.hotkey
|
|
4120
4166
|
});
|
|
4121
4167
|
}
|
|
4122
|
-
const
|
|
4123
|
-
fetchConvictionLocks(connector, networkId, miniMetadata.data, addresses, at),
|
|
4124
|
-
fetchBasketClaims(connector, networkId, miniMetadata.data, addresses, at),
|
|
4125
|
-
fetchRootStakeHolds(connector, networkId, miniMetadata.data, rootPairs, at)
|
|
4126
|
-
]) : [
|
|
4127
|
-
[],
|
|
4128
|
-
[],
|
|
4129
|
-
[]
|
|
4130
|
-
];
|
|
4168
|
+
const rootStakeHolds = await fetchRootStakeHolds(connector, networkId, metadataRpc, rootPairs, blockHash);
|
|
4131
4169
|
const upsertBalance = (acc, address, tokenId, balance) => {
|
|
4132
4170
|
const key = `${address}:${tokenId}`;
|
|
4133
4171
|
const recordedBalance = acc[key];
|
|
@@ -4488,6 +4526,10 @@ const isEffectivelyEqualDTaoBalance = (previous, next) => {
|
|
|
4488
4526
|
return result.equivalence;
|
|
4489
4527
|
};
|
|
4490
4528
|
// src/modules/substrate-dtao/subscribeBalances.ts
|
|
4529
|
+
/**
|
|
4530
|
+
* Half of Bittensor's 12s block time, so a new block is read within 6s of being produced.
|
|
4531
|
+
* A poll that finds the same best block as the last good poll ends after that one request.
|
|
4532
|
+
*/
|
|
4491
4533
|
const SUBSCRIPTION_INTERVAL = 6e3;
|
|
4492
4534
|
const subscribeBalances$5 = ({ networkId, tokensWithAddresses, connector, miniMetadata }) => {
|
|
4493
4535
|
if (!tokensWithAddresses.length) return of({
|
|
@@ -4497,21 +4539,32 @@ const subscribeBalances$5 = ({ networkId, tokensWithAddresses, connector, miniMe
|
|
|
4497
4539
|
const balanceDefs = getBalanceDefs(tokensWithAddresses);
|
|
4498
4540
|
return new Observable((subscriber) => {
|
|
4499
4541
|
const abortController = new AbortController();
|
|
4542
|
+
/** the block the last poll read without errors */
|
|
4543
|
+
let lastGoodBlockHash = null;
|
|
4500
4544
|
const poll = async () => {
|
|
4501
4545
|
try {
|
|
4502
4546
|
if (abortController.signal.aborted) return;
|
|
4503
|
-
const
|
|
4547
|
+
const bestBlock = fetchBestBlockHash(connector, networkId);
|
|
4548
|
+
const blockHash = await bestBlock.catch(() => void 0);
|
|
4549
|
+
if (abortController.signal.aborted) return;
|
|
4550
|
+
if (blockHash && blockHash === lastGoodBlockHash) {
|
|
4551
|
+
setTimeout(poll, SUBSCRIPTION_INTERVAL);
|
|
4552
|
+
return;
|
|
4553
|
+
}
|
|
4554
|
+
const balances = await fetchBalancesAt({
|
|
4504
4555
|
networkId,
|
|
4505
4556
|
tokensWithAddresses,
|
|
4506
4557
|
connector,
|
|
4507
4558
|
miniMetadata,
|
|
4508
4559
|
signal: abortController.signal
|
|
4509
|
-
});
|
|
4560
|
+
}, blockHash ?? bestBlock);
|
|
4510
4561
|
if (abortController.signal.aborted) return;
|
|
4562
|
+
lastGoodBlockHash = balances.errors.length ? null : blockHash ?? null;
|
|
4511
4563
|
reportJsActivity(`poll ${MODULE_TYPE$5} ${networkId} (${balances.success.length})`);
|
|
4512
4564
|
subscriber.next(balances);
|
|
4513
4565
|
} catch (error) {
|
|
4514
4566
|
if (abortController.signal.aborted) return;
|
|
4567
|
+
lastGoodBlockHash = null;
|
|
4515
4568
|
log_default.error("Error", {
|
|
4516
4569
|
module: MODULE_TYPE$5,
|
|
4517
4570
|
networkId,
|