@talismn/balances 0.0.0-pr2093-20250715143904 → 0.0.0-pr2094-20250715164740
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.
@@ -21,9 +21,7 @@ export declare class BalancesProvider {
|
|
21
21
|
private getNetworkBalances$;
|
22
22
|
private getPolkadotNetworkModuleBalances$;
|
23
23
|
private getEthereumNetworkModuleBalances$;
|
24
|
-
private
|
25
|
-
private updateStoredMiniMetadatas;
|
26
|
-
private processStorageUpdatesQueue;
|
24
|
+
private updateStorage$;
|
27
25
|
private getNetworkMiniMetadatas$;
|
28
26
|
private getNetworkSpecVersion$;
|
29
27
|
private getMiniMetadatas$;
|
@@ -1502,8 +1502,23 @@ const getRpcQueryPack$ = (connector, networkId, queries, timeout = false) => {
|
|
1502
1502
|
decodeResult
|
1503
1503
|
}) => decodeResult(stateKeys.map(() => null))));
|
1504
1504
|
return new rxjs.Observable(subscriber => {
|
1505
|
+
// first subscription callback includes results for all state keys, but further callbacks will only include the ones that changed
|
1506
|
+
// => we need to keep all results in memory and update them after each callback, so we can emit the full result set each time
|
1507
|
+
const dicChangesCache = {};
|
1505
1508
|
const promUnsub = connector.subscribe(networkId, "state_subscribeStorage", "state_storage", [allStateKeys], (error, result) => {
|
1506
|
-
if (error) subscriber.error(error);else
|
1509
|
+
if (error) subscriber.error(error);else if (result) {
|
1510
|
+
// update the cache
|
1511
|
+
for (const [stateKey, encodedResult] of result.changes) dicChangesCache[stateKey] = encodedResult;
|
1512
|
+
|
1513
|
+
// regenerate the full changes array
|
1514
|
+
const changes = lodashEs.toPairs(dicChangesCache);
|
1515
|
+
|
1516
|
+
// decode and emit results for all queries
|
1517
|
+
subscriber.next(decodeRpcQueryPack(queries, {
|
1518
|
+
block: result.block,
|
1519
|
+
changes
|
1520
|
+
}));
|
1521
|
+
}
|
1507
1522
|
}, timeout);
|
1508
1523
|
return () => {
|
1509
1524
|
promUnsub.then(unsub => unsub("state_unsubscribeStorage"));
|
@@ -6238,10 +6253,6 @@ class BalancesProvider {
|
|
6238
6253
|
#chaindataProvider;
|
6239
6254
|
#chainConnectors;
|
6240
6255
|
#storage;
|
6241
|
-
|
6242
|
-
// storage is updated at high frequency, use a queue to avoid race conditions
|
6243
|
-
#updateStorageQueue = [];
|
6244
|
-
#isProcessingStorageUpdates = false;
|
6245
6256
|
constructor(chaindataProvider, chainConnectors, storage = DEFAULT_STORAGE) {
|
6246
6257
|
this.#chaindataProvider = chaindataProvider;
|
6247
6258
|
this.#chainConnectors = chainConnectors;
|
@@ -6368,7 +6379,9 @@ class BalancesProvider {
|
|
6368
6379
|
status: "live",
|
6369
6380
|
// exclude zero balances
|
6370
6381
|
balances: results.success.filter(b => new Balance(b).total.planck > 0n)
|
6371
|
-
})), rxjs.tap(results =>
|
6382
|
+
})), rxjs.tap(results => {
|
6383
|
+
this.updateStorage$(balanceIds, results);
|
6384
|
+
}),
|
6372
6385
|
// shareReplay + keepAlive(0) keep the subscription alive while root observable is being unsubscribed+resubscribed, in case any input change
|
6373
6386
|
rxjs.shareReplay({
|
6374
6387
|
refCount: true,
|
@@ -6416,7 +6429,9 @@ class BalancesProvider {
|
|
6416
6429
|
status: "live",
|
6417
6430
|
// exclude zero balances
|
6418
6431
|
balances: results.success.filter(b => new Balance(b).total.planck > 0n)
|
6419
|
-
})), rxjs.tap(results =>
|
6432
|
+
})), rxjs.tap(results => {
|
6433
|
+
this.updateStorage$(balanceIds, results);
|
6434
|
+
}),
|
6420
6435
|
// shareReplay + keepAlive(0) keep the subscription alive while root observable is being unsubscribed+resubscribed, in case any input change
|
6421
6436
|
rxjs.shareReplay({
|
6422
6437
|
refCount: true,
|
@@ -6430,72 +6445,20 @@ class BalancesProvider {
|
|
6430
6445
|
})));
|
6431
6446
|
});
|
6432
6447
|
}
|
6433
|
-
|
6448
|
+
updateStorage$(balanceIds, balancesResult) {
|
6434
6449
|
if (balancesResult.status !== "live") return;
|
6435
|
-
this.#updateStorageQueue.push({
|
6436
|
-
type: "balances",
|
6437
|
-
balanceIds,
|
6438
|
-
balances: balancesResult.balances
|
6439
|
-
});
|
6440
|
-
this.processStorageUpdatesQueue();
|
6441
|
-
}
|
6442
|
-
updateStoredMiniMetadatas(networkId, miniMetadatas) {
|
6443
|
-
this.#updateStorageQueue.push({
|
6444
|
-
type: "miniMetadatas",
|
6445
|
-
networkId,
|
6446
|
-
miniMetadatas
|
6447
|
-
});
|
6448
|
-
this.processStorageUpdatesQueue();
|
6449
|
-
}
|
6450
|
-
processStorageUpdatesQueue() {
|
6451
|
-
if (this.#updateStorageQueue.length > 1) log.warn("[balances] this message is the proof that there are sometimes racing conditions, and that we avoided them :)");
|
6452
|
-
if (this.#isProcessingStorageUpdates || !this.#updateStorageQueue.length) return;
|
6453
|
-
this.#isProcessingStorageUpdates = true;
|
6454
6450
|
const storage = this.#storage.getValue();
|
6455
|
-
const balances = {
|
6456
|
-
|
6457
|
-
|
6458
|
-
|
6459
|
-
|
6460
|
-
|
6461
|
-
|
6462
|
-
|
6463
|
-
|
6464
|
-
|
6465
|
-
|
6466
|
-
const {
|
6467
|
-
networkId,
|
6468
|
-
miniMetadatas: newMiniMetadatas
|
6469
|
-
} = queueItem;
|
6470
|
-
|
6471
|
-
// remove old miniMetadatas for the network
|
6472
|
-
for (const miniMetadata of lodashEs.values(miniMetadatas)) if (miniMetadata.chainId === networkId) delete miniMetadatas[miniMetadata.id];
|
6473
|
-
|
6474
|
-
// add new ones
|
6475
|
-
for (const miniMetadata of newMiniMetadatas) miniMetadatas[miniMetadata.id] = miniMetadata;
|
6476
|
-
break;
|
6477
|
-
}
|
6478
|
-
case "balances":
|
6479
|
-
{
|
6480
|
-
const {
|
6481
|
-
balanceIds,
|
6482
|
-
balances: newBalances
|
6483
|
-
} = queueItem;
|
6484
|
-
|
6485
|
-
// remove all balances expected in the result set
|
6486
|
-
for (const balanceId of balanceIds) delete balances[balanceId];
|
6487
|
-
|
6488
|
-
// add new ones
|
6489
|
-
for (const balance of newBalances) balances[getBalanceId(balance)] = balance;
|
6490
|
-
break;
|
6491
|
-
}
|
6492
|
-
}
|
6493
|
-
this.#storage.next({
|
6494
|
-
balances,
|
6495
|
-
miniMetadatas
|
6496
|
-
});
|
6497
|
-
this.#isProcessingStorageUpdates = false;
|
6498
|
-
}
|
6451
|
+
const balances = lodashEs.assign({}, storage.balances,
|
6452
|
+
// delete all balances expected in the result set. because if they are not present it means they are empty.
|
6453
|
+
lodashEs.fromPairs(balanceIds.map(balanceId => [balanceId, undefined])), lodashEs.keyBy(
|
6454
|
+
// storage balances must have status "cache", because they are used as start value when initialising subsequent subscriptions
|
6455
|
+
balancesResult.balances.map(b => ({
|
6456
|
+
...b,
|
6457
|
+
status: "cache"
|
6458
|
+
})), b => getBalanceId(b)));
|
6459
|
+
this.#storage.next(lodashEs.assign({}, storage, {
|
6460
|
+
balances
|
6461
|
+
}));
|
6499
6462
|
}
|
6500
6463
|
getNetworkMiniMetadatas$(networkId) {
|
6501
6464
|
return util.getSharedObservable(`BalancesProvider.getNetworkMiniMetadatas$`, {
|
@@ -6547,7 +6510,15 @@ class BalancesProvider {
|
|
6547
6510
|
// and persist in storage for later reuse
|
6548
6511
|
rxjs.tap(newMiniMetadatas => {
|
6549
6512
|
if (!newMiniMetadatas.length) return;
|
6550
|
-
this.
|
6513
|
+
const storage = this.#storage.getValue();
|
6514
|
+
const miniMetadatas = lodashEs.assign(
|
6515
|
+
// keep minimetadatas of other networks
|
6516
|
+
lodashEs.keyBy(lodashEs.values(storage.miniMetadatas).filter(m => m.chainId !== networkId), m => m.id),
|
6517
|
+
// add the ones for our network
|
6518
|
+
lodashEs.keyBy(newMiniMetadatas, m => m.id));
|
6519
|
+
this.#storage.next(lodashEs.assign({}, storage, {
|
6520
|
+
miniMetadatas
|
6521
|
+
}));
|
6551
6522
|
}));
|
6552
6523
|
}),
|
6553
6524
|
// emit only when mini metadata changes, as a change here would restart all subscriptions for the network
|
@@ -1502,8 +1502,23 @@ const getRpcQueryPack$ = (connector, networkId, queries, timeout = false) => {
|
|
1502
1502
|
decodeResult
|
1503
1503
|
}) => decodeResult(stateKeys.map(() => null))));
|
1504
1504
|
return new rxjs.Observable(subscriber => {
|
1505
|
+
// first subscription callback includes results for all state keys, but further callbacks will only include the ones that changed
|
1506
|
+
// => we need to keep all results in memory and update them after each callback, so we can emit the full result set each time
|
1507
|
+
const dicChangesCache = {};
|
1505
1508
|
const promUnsub = connector.subscribe(networkId, "state_subscribeStorage", "state_storage", [allStateKeys], (error, result) => {
|
1506
|
-
if (error) subscriber.error(error);else
|
1509
|
+
if (error) subscriber.error(error);else if (result) {
|
1510
|
+
// update the cache
|
1511
|
+
for (const [stateKey, encodedResult] of result.changes) dicChangesCache[stateKey] = encodedResult;
|
1512
|
+
|
1513
|
+
// regenerate the full changes array
|
1514
|
+
const changes = lodashEs.toPairs(dicChangesCache);
|
1515
|
+
|
1516
|
+
// decode and emit results for all queries
|
1517
|
+
subscriber.next(decodeRpcQueryPack(queries, {
|
1518
|
+
block: result.block,
|
1519
|
+
changes
|
1520
|
+
}));
|
1521
|
+
}
|
1507
1522
|
}, timeout);
|
1508
1523
|
return () => {
|
1509
1524
|
promUnsub.then(unsub => unsub("state_unsubscribeStorage"));
|
@@ -6238,10 +6253,6 @@ class BalancesProvider {
|
|
6238
6253
|
#chaindataProvider;
|
6239
6254
|
#chainConnectors;
|
6240
6255
|
#storage;
|
6241
|
-
|
6242
|
-
// storage is updated at high frequency, use a queue to avoid race conditions
|
6243
|
-
#updateStorageQueue = [];
|
6244
|
-
#isProcessingStorageUpdates = false;
|
6245
6256
|
constructor(chaindataProvider, chainConnectors, storage = DEFAULT_STORAGE) {
|
6246
6257
|
this.#chaindataProvider = chaindataProvider;
|
6247
6258
|
this.#chainConnectors = chainConnectors;
|
@@ -6368,7 +6379,9 @@ class BalancesProvider {
|
|
6368
6379
|
status: "live",
|
6369
6380
|
// exclude zero balances
|
6370
6381
|
balances: results.success.filter(b => new Balance(b).total.planck > 0n)
|
6371
|
-
})), rxjs.tap(results =>
|
6382
|
+
})), rxjs.tap(results => {
|
6383
|
+
this.updateStorage$(balanceIds, results);
|
6384
|
+
}),
|
6372
6385
|
// shareReplay + keepAlive(0) keep the subscription alive while root observable is being unsubscribed+resubscribed, in case any input change
|
6373
6386
|
rxjs.shareReplay({
|
6374
6387
|
refCount: true,
|
@@ -6416,7 +6429,9 @@ class BalancesProvider {
|
|
6416
6429
|
status: "live",
|
6417
6430
|
// exclude zero balances
|
6418
6431
|
balances: results.success.filter(b => new Balance(b).total.planck > 0n)
|
6419
|
-
})), rxjs.tap(results =>
|
6432
|
+
})), rxjs.tap(results => {
|
6433
|
+
this.updateStorage$(balanceIds, results);
|
6434
|
+
}),
|
6420
6435
|
// shareReplay + keepAlive(0) keep the subscription alive while root observable is being unsubscribed+resubscribed, in case any input change
|
6421
6436
|
rxjs.shareReplay({
|
6422
6437
|
refCount: true,
|
@@ -6430,72 +6445,20 @@ class BalancesProvider {
|
|
6430
6445
|
})));
|
6431
6446
|
});
|
6432
6447
|
}
|
6433
|
-
|
6448
|
+
updateStorage$(balanceIds, balancesResult) {
|
6434
6449
|
if (balancesResult.status !== "live") return;
|
6435
|
-
this.#updateStorageQueue.push({
|
6436
|
-
type: "balances",
|
6437
|
-
balanceIds,
|
6438
|
-
balances: balancesResult.balances
|
6439
|
-
});
|
6440
|
-
this.processStorageUpdatesQueue();
|
6441
|
-
}
|
6442
|
-
updateStoredMiniMetadatas(networkId, miniMetadatas) {
|
6443
|
-
this.#updateStorageQueue.push({
|
6444
|
-
type: "miniMetadatas",
|
6445
|
-
networkId,
|
6446
|
-
miniMetadatas
|
6447
|
-
});
|
6448
|
-
this.processStorageUpdatesQueue();
|
6449
|
-
}
|
6450
|
-
processStorageUpdatesQueue() {
|
6451
|
-
if (this.#updateStorageQueue.length > 1) log.warn("[balances] this message is the proof that there are sometimes racing conditions, and that we avoided them :)");
|
6452
|
-
if (this.#isProcessingStorageUpdates || !this.#updateStorageQueue.length) return;
|
6453
|
-
this.#isProcessingStorageUpdates = true;
|
6454
6450
|
const storage = this.#storage.getValue();
|
6455
|
-
const balances = {
|
6456
|
-
|
6457
|
-
|
6458
|
-
|
6459
|
-
|
6460
|
-
|
6461
|
-
|
6462
|
-
|
6463
|
-
|
6464
|
-
|
6465
|
-
|
6466
|
-
const {
|
6467
|
-
networkId,
|
6468
|
-
miniMetadatas: newMiniMetadatas
|
6469
|
-
} = queueItem;
|
6470
|
-
|
6471
|
-
// remove old miniMetadatas for the network
|
6472
|
-
for (const miniMetadata of lodashEs.values(miniMetadatas)) if (miniMetadata.chainId === networkId) delete miniMetadatas[miniMetadata.id];
|
6473
|
-
|
6474
|
-
// add new ones
|
6475
|
-
for (const miniMetadata of newMiniMetadatas) miniMetadatas[miniMetadata.id] = miniMetadata;
|
6476
|
-
break;
|
6477
|
-
}
|
6478
|
-
case "balances":
|
6479
|
-
{
|
6480
|
-
const {
|
6481
|
-
balanceIds,
|
6482
|
-
balances: newBalances
|
6483
|
-
} = queueItem;
|
6484
|
-
|
6485
|
-
// remove all balances expected in the result set
|
6486
|
-
for (const balanceId of balanceIds) delete balances[balanceId];
|
6487
|
-
|
6488
|
-
// add new ones
|
6489
|
-
for (const balance of newBalances) balances[getBalanceId(balance)] = balance;
|
6490
|
-
break;
|
6491
|
-
}
|
6492
|
-
}
|
6493
|
-
this.#storage.next({
|
6494
|
-
balances,
|
6495
|
-
miniMetadatas
|
6496
|
-
});
|
6497
|
-
this.#isProcessingStorageUpdates = false;
|
6498
|
-
}
|
6451
|
+
const balances = lodashEs.assign({}, storage.balances,
|
6452
|
+
// delete all balances expected in the result set. because if they are not present it means they are empty.
|
6453
|
+
lodashEs.fromPairs(balanceIds.map(balanceId => [balanceId, undefined])), lodashEs.keyBy(
|
6454
|
+
// storage balances must have status "cache", because they are used as start value when initialising subsequent subscriptions
|
6455
|
+
balancesResult.balances.map(b => ({
|
6456
|
+
...b,
|
6457
|
+
status: "cache"
|
6458
|
+
})), b => getBalanceId(b)));
|
6459
|
+
this.#storage.next(lodashEs.assign({}, storage, {
|
6460
|
+
balances
|
6461
|
+
}));
|
6499
6462
|
}
|
6500
6463
|
getNetworkMiniMetadatas$(networkId) {
|
6501
6464
|
return util.getSharedObservable(`BalancesProvider.getNetworkMiniMetadatas$`, {
|
@@ -6547,7 +6510,15 @@ class BalancesProvider {
|
|
6547
6510
|
// and persist in storage for later reuse
|
6548
6511
|
rxjs.tap(newMiniMetadatas => {
|
6549
6512
|
if (!newMiniMetadatas.length) return;
|
6550
|
-
this.
|
6513
|
+
const storage = this.#storage.getValue();
|
6514
|
+
const miniMetadatas = lodashEs.assign(
|
6515
|
+
// keep minimetadatas of other networks
|
6516
|
+
lodashEs.keyBy(lodashEs.values(storage.miniMetadatas).filter(m => m.chainId !== networkId), m => m.id),
|
6517
|
+
// add the ones for our network
|
6518
|
+
lodashEs.keyBy(newMiniMetadatas, m => m.id));
|
6519
|
+
this.#storage.next(lodashEs.assign({}, storage, {
|
6520
|
+
miniMetadatas
|
6521
|
+
}));
|
6551
6522
|
}));
|
6552
6523
|
}),
|
6553
6524
|
// emit only when mini metadata changes, as a change here would restart all subscriptions for the network
|
@@ -2,7 +2,7 @@ import { EvmErc20TokenSchema, parseTokenId, parseEvmErc20TokenId, evmErc20TokenI
|
|
2
2
|
export { MINIMETADATA_VERSION } from '@talismn/chaindata-provider';
|
3
3
|
import { isEthereumAddress, isNotNil, BigMath, isArrayOf, isBigInt, planckToTokens, isAbortError, getSharedObservable, keepAlive, isTruthy, normalizeAddress } from '@talismn/util';
|
4
4
|
import { parseAbi, erc20Abi, getContract, ContractFunctionExecutionError, hexToString, erc20Abi_bytes32, encodeFunctionData, withRetry } from 'viem';
|
5
|
-
import { assign, omit, isEqual, uniq, keyBy, keys, fromPairs, values
|
5
|
+
import { assign, omit, isEqual, uniq, keyBy, toPairs, keys, fromPairs, values } from 'lodash-es';
|
6
6
|
import z from 'zod/v4';
|
7
7
|
import anylogger from 'anylogger';
|
8
8
|
import { of, Observable, distinctUntilChanged, map, timer, switchMap, from, firstValueFrom, combineLatest, BehaviorSubject, shareReplay, startWith, filter, defer, catchError, EMPTY, tap } from 'rxjs';
|
@@ -1493,8 +1493,23 @@ const getRpcQueryPack$ = (connector, networkId, queries, timeout = false) => {
|
|
1493
1493
|
decodeResult
|
1494
1494
|
}) => decodeResult(stateKeys.map(() => null))));
|
1495
1495
|
return new Observable(subscriber => {
|
1496
|
+
// first subscription callback includes results for all state keys, but further callbacks will only include the ones that changed
|
1497
|
+
// => we need to keep all results in memory and update them after each callback, so we can emit the full result set each time
|
1498
|
+
const dicChangesCache = {};
|
1496
1499
|
const promUnsub = connector.subscribe(networkId, "state_subscribeStorage", "state_storage", [allStateKeys], (error, result) => {
|
1497
|
-
if (error) subscriber.error(error);else
|
1500
|
+
if (error) subscriber.error(error);else if (result) {
|
1501
|
+
// update the cache
|
1502
|
+
for (const [stateKey, encodedResult] of result.changes) dicChangesCache[stateKey] = encodedResult;
|
1503
|
+
|
1504
|
+
// regenerate the full changes array
|
1505
|
+
const changes = toPairs(dicChangesCache);
|
1506
|
+
|
1507
|
+
// decode and emit results for all queries
|
1508
|
+
subscriber.next(decodeRpcQueryPack(queries, {
|
1509
|
+
block: result.block,
|
1510
|
+
changes
|
1511
|
+
}));
|
1512
|
+
}
|
1498
1513
|
}, timeout);
|
1499
1514
|
return () => {
|
1500
1515
|
promUnsub.then(unsub => unsub("state_unsubscribeStorage"));
|
@@ -6229,10 +6244,6 @@ class BalancesProvider {
|
|
6229
6244
|
#chaindataProvider;
|
6230
6245
|
#chainConnectors;
|
6231
6246
|
#storage;
|
6232
|
-
|
6233
|
-
// storage is updated at high frequency, use a queue to avoid race conditions
|
6234
|
-
#updateStorageQueue = [];
|
6235
|
-
#isProcessingStorageUpdates = false;
|
6236
6247
|
constructor(chaindataProvider, chainConnectors, storage = DEFAULT_STORAGE) {
|
6237
6248
|
this.#chaindataProvider = chaindataProvider;
|
6238
6249
|
this.#chainConnectors = chainConnectors;
|
@@ -6359,7 +6370,9 @@ class BalancesProvider {
|
|
6359
6370
|
status: "live",
|
6360
6371
|
// exclude zero balances
|
6361
6372
|
balances: results.success.filter(b => new Balance(b).total.planck > 0n)
|
6362
|
-
})), tap(results =>
|
6373
|
+
})), tap(results => {
|
6374
|
+
this.updateStorage$(balanceIds, results);
|
6375
|
+
}),
|
6363
6376
|
// shareReplay + keepAlive(0) keep the subscription alive while root observable is being unsubscribed+resubscribed, in case any input change
|
6364
6377
|
shareReplay({
|
6365
6378
|
refCount: true,
|
@@ -6407,7 +6420,9 @@ class BalancesProvider {
|
|
6407
6420
|
status: "live",
|
6408
6421
|
// exclude zero balances
|
6409
6422
|
balances: results.success.filter(b => new Balance(b).total.planck > 0n)
|
6410
|
-
})), tap(results =>
|
6423
|
+
})), tap(results => {
|
6424
|
+
this.updateStorage$(balanceIds, results);
|
6425
|
+
}),
|
6411
6426
|
// shareReplay + keepAlive(0) keep the subscription alive while root observable is being unsubscribed+resubscribed, in case any input change
|
6412
6427
|
shareReplay({
|
6413
6428
|
refCount: true,
|
@@ -6421,72 +6436,20 @@ class BalancesProvider {
|
|
6421
6436
|
})));
|
6422
6437
|
});
|
6423
6438
|
}
|
6424
|
-
|
6439
|
+
updateStorage$(balanceIds, balancesResult) {
|
6425
6440
|
if (balancesResult.status !== "live") return;
|
6426
|
-
this.#updateStorageQueue.push({
|
6427
|
-
type: "balances",
|
6428
|
-
balanceIds,
|
6429
|
-
balances: balancesResult.balances
|
6430
|
-
});
|
6431
|
-
this.processStorageUpdatesQueue();
|
6432
|
-
}
|
6433
|
-
updateStoredMiniMetadatas(networkId, miniMetadatas) {
|
6434
|
-
this.#updateStorageQueue.push({
|
6435
|
-
type: "miniMetadatas",
|
6436
|
-
networkId,
|
6437
|
-
miniMetadatas
|
6438
|
-
});
|
6439
|
-
this.processStorageUpdatesQueue();
|
6440
|
-
}
|
6441
|
-
processStorageUpdatesQueue() {
|
6442
|
-
if (this.#updateStorageQueue.length > 1) log.warn("[balances] this message is the proof that there are sometimes racing conditions, and that we avoided them :)");
|
6443
|
-
if (this.#isProcessingStorageUpdates || !this.#updateStorageQueue.length) return;
|
6444
|
-
this.#isProcessingStorageUpdates = true;
|
6445
6441
|
const storage = this.#storage.getValue();
|
6446
|
-
const balances = {
|
6447
|
-
|
6448
|
-
|
6449
|
-
|
6450
|
-
|
6451
|
-
|
6452
|
-
|
6453
|
-
|
6454
|
-
|
6455
|
-
|
6456
|
-
|
6457
|
-
const {
|
6458
|
-
networkId,
|
6459
|
-
miniMetadatas: newMiniMetadatas
|
6460
|
-
} = queueItem;
|
6461
|
-
|
6462
|
-
// remove old miniMetadatas for the network
|
6463
|
-
for (const miniMetadata of values(miniMetadatas)) if (miniMetadata.chainId === networkId) delete miniMetadatas[miniMetadata.id];
|
6464
|
-
|
6465
|
-
// add new ones
|
6466
|
-
for (const miniMetadata of newMiniMetadatas) miniMetadatas[miniMetadata.id] = miniMetadata;
|
6467
|
-
break;
|
6468
|
-
}
|
6469
|
-
case "balances":
|
6470
|
-
{
|
6471
|
-
const {
|
6472
|
-
balanceIds,
|
6473
|
-
balances: newBalances
|
6474
|
-
} = queueItem;
|
6475
|
-
|
6476
|
-
// remove all balances expected in the result set
|
6477
|
-
for (const balanceId of balanceIds) delete balances[balanceId];
|
6478
|
-
|
6479
|
-
// add new ones
|
6480
|
-
for (const balance of newBalances) balances[getBalanceId(balance)] = balance;
|
6481
|
-
break;
|
6482
|
-
}
|
6483
|
-
}
|
6484
|
-
this.#storage.next({
|
6485
|
-
balances,
|
6486
|
-
miniMetadatas
|
6487
|
-
});
|
6488
|
-
this.#isProcessingStorageUpdates = false;
|
6489
|
-
}
|
6442
|
+
const balances = assign({}, storage.balances,
|
6443
|
+
// delete all balances expected in the result set. because if they are not present it means they are empty.
|
6444
|
+
fromPairs(balanceIds.map(balanceId => [balanceId, undefined])), keyBy(
|
6445
|
+
// storage balances must have status "cache", because they are used as start value when initialising subsequent subscriptions
|
6446
|
+
balancesResult.balances.map(b => ({
|
6447
|
+
...b,
|
6448
|
+
status: "cache"
|
6449
|
+
})), b => getBalanceId(b)));
|
6450
|
+
this.#storage.next(assign({}, storage, {
|
6451
|
+
balances
|
6452
|
+
}));
|
6490
6453
|
}
|
6491
6454
|
getNetworkMiniMetadatas$(networkId) {
|
6492
6455
|
return getSharedObservable(`BalancesProvider.getNetworkMiniMetadatas$`, {
|
@@ -6538,7 +6501,15 @@ class BalancesProvider {
|
|
6538
6501
|
// and persist in storage for later reuse
|
6539
6502
|
tap(newMiniMetadatas => {
|
6540
6503
|
if (!newMiniMetadatas.length) return;
|
6541
|
-
this.
|
6504
|
+
const storage = this.#storage.getValue();
|
6505
|
+
const miniMetadatas = assign(
|
6506
|
+
// keep minimetadatas of other networks
|
6507
|
+
keyBy(values(storage.miniMetadatas).filter(m => m.chainId !== networkId), m => m.id),
|
6508
|
+
// add the ones for our network
|
6509
|
+
keyBy(newMiniMetadatas, m => m.id));
|
6510
|
+
this.#storage.next(assign({}, storage, {
|
6511
|
+
miniMetadatas
|
6512
|
+
}));
|
6542
6513
|
}));
|
6543
6514
|
}),
|
6544
6515
|
// emit only when mini metadata changes, as a change here would restart all subscriptions for the network
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@talismn/balances",
|
3
|
-
"version": "0.0.0-
|
3
|
+
"version": "0.0.0-pr2094-20250715164740",
|
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/chain-connector": "0.0.0-
|
38
|
-
"@talismn/chain-connector-evm": "0.0.0-
|
39
|
-
"@talismn/chaindata-provider": "0.0.0-
|
40
|
-
"@talismn/sapi": "0.0.0-
|
41
|
-
"@talismn/
|
42
|
-
"@talismn/token-rates": "0.0.0-
|
43
|
-
"@talismn/
|
37
|
+
"@talismn/chain-connector": "0.0.0-pr2094-20250715164740",
|
38
|
+
"@talismn/chain-connector-evm": "0.0.0-pr2094-20250715164740",
|
39
|
+
"@talismn/chaindata-provider": "0.0.0-pr2094-20250715164740",
|
40
|
+
"@talismn/sapi": "0.0.0-pr2094-20250715164740",
|
41
|
+
"@talismn/util": "0.0.0-pr2094-20250715164740",
|
42
|
+
"@talismn/token-rates": "0.0.0-pr2094-20250715164740",
|
43
|
+
"@talismn/scale": "0.0.0-pr2094-20250715164740"
|
44
44
|
},
|
45
45
|
"devDependencies": {
|
46
46
|
"@polkadot/api-contract": "16.1.2",
|