@talismn/balances 1.3.2 → 1.3.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/README.md CHANGED
@@ -20,13 +20,6 @@ A quick rundown on each package is given below.
20
20
  - Helpers (utility functions) for balance modules to use
21
21
  - Provides a plugin architecture, which is used by the balance module packages to specify their balance types
22
22
 
23
- ### The react API for wallets & dapps:
24
-
25
- **@talismn/balances-react** includes:
26
-
27
- - React hooks for subscribing to on-chain account token balances
28
- - (soon™): recoil atoms for on-chain account token balances
29
-
30
23
  ### The balance modules:
31
24
 
32
25
  **@talismn/balances-default-modules**
package/dist/index.d.mts CHANGED
@@ -119,7 +119,7 @@ declare class Balances {
119
119
  * // do something
120
120
  * }
121
121
  */
122
- [Symbol.iterator]: () => ArrayIterator<Balance>;
122
+ [Symbol.iterator]: () => MapIterator<Balance>;
123
123
  /**
124
124
  * Hydrates all balances in this collection.
125
125
  *
package/dist/index.d.ts CHANGED
@@ -119,7 +119,7 @@ declare class Balances {
119
119
  * // do something
120
120
  * }
121
121
  */
122
- [Symbol.iterator]: () => ArrayIterator<Balance>;
122
+ [Symbol.iterator]: () => MapIterator<Balance>;
123
123
  /**
124
124
  * Hydrates all balances in this collection.
125
125
  *
package/dist/index.js CHANGED
@@ -108,7 +108,7 @@ var import_anylogger = __toESM(require("anylogger"));
108
108
  // package.json
109
109
  var package_default = {
110
110
  name: "@talismn/balances",
111
- version: "1.3.2",
111
+ version: "1.3.4",
112
112
  author: "Talisman",
113
113
  homepage: "https://talisman.xyz",
114
114
  license: "GPL-3.0-or-later",
@@ -151,7 +151,7 @@ var package_default = {
151
151
  "bignumber.js": "^9.1.2",
152
152
  "lodash-es": "4.17.21",
153
153
  "p-queue": "8.1.0",
154
- "polkadot-api": "1.13.1",
154
+ "polkadot-api": "1.23.3",
155
155
  rxjs: "^7.8.1",
156
156
  "scale-ts": "^1.6.1",
157
157
  viem: "^2.27.3",
@@ -3425,12 +3425,15 @@ var Balances = class _Balances {
3425
3425
  //
3426
3426
  // Properties
3427
3427
  //
3428
- #balances = [];
3428
+ #balancesMap = /* @__PURE__ */ new Map();
3429
+ #cachedArray = null;
3430
+ #cachedFilteredMirrorTokens = null;
3431
+ #cachedSumFormatter = null;
3429
3432
  //
3430
3433
  // Methods
3431
3434
  //
3432
3435
  constructor(balances, hydrate) {
3433
- if (balances instanceof _Balances) return new _Balances(balances.each, hydrate);
3436
+ if (balances instanceof _Balances) return new _Balances([...balances], hydrate);
3434
3437
  if (balances instanceof Balance) return new _Balances([balances], hydrate);
3435
3438
  if (!Array.isArray(balances)) return new _Balances(Object.values(balances), hydrate);
3436
3439
  if (balances.length === 0) return this;
@@ -3439,14 +3442,14 @@ var Balances = class _Balances {
3439
3442
  balances.map((storage) => new Balance(storage)),
3440
3443
  hydrate
3441
3444
  );
3442
- this.#balances = balances;
3445
+ this.#balancesMap = new Map(balances.map((b) => [b.id, b]));
3443
3446
  if (hydrate !== void 0) this.hydrate(hydrate);
3444
3447
  }
3445
3448
  /**
3446
3449
  * Calling toJSON on a collection of balances will return the underlying BalanceJsonList.
3447
3450
  */
3448
3451
  toJSON = () => Object.fromEntries(
3449
- this.#balances.map((balance) => {
3452
+ [...this.#balancesMap.values()].map((balance) => {
3450
3453
  try {
3451
3454
  return [balance.id, balance.toJSON()];
3452
3455
  } catch (error) {
@@ -3466,17 +3469,17 @@ var Balances = class _Balances {
3466
3469
  * // do something
3467
3470
  * }
3468
3471
  */
3469
- [Symbol.iterator] = () => (
3470
- // Create an array of the balances in this collection and return the result of its iterator.
3471
- this.#balances[Symbol.iterator]()
3472
- );
3472
+ [Symbol.iterator] = () => this.#balancesMap.values()[Symbol.iterator]();
3473
3473
  /**
3474
3474
  * Hydrates all balances in this collection.
3475
3475
  *
3476
3476
  * @param sources - The sources to hydrate from.
3477
3477
  */
3478
3478
  hydrate = (sources) => {
3479
- this.#balances.map((balance) => balance.hydrate(sources));
3479
+ this.#cachedArray = null;
3480
+ this.#cachedFilteredMirrorTokens = null;
3481
+ this.#cachedSumFormatter = null;
3482
+ for (const balance of this.#balancesMap.values()) balance.hydrate(sources);
3480
3483
  };
3481
3484
  /**
3482
3485
  * Retrieve a balance from this collection by id.
@@ -3484,7 +3487,7 @@ var Balances = class _Balances {
3484
3487
  * @param id - The id of the balance to fetch.
3485
3488
  * @returns The balance if one exists, or none.
3486
3489
  */
3487
- get = (id) => this.#balances.find((balance) => balance.id === id) ?? null;
3490
+ get = (id) => this.#balancesMap.get(id) ?? null;
3488
3491
  /**
3489
3492
  * Retrieve balances from this collection by search query.
3490
3493
  *
@@ -3499,13 +3502,28 @@ var Balances = class _Balances {
3499
3502
  const filter2 = (balance) => orQueries.some(
3500
3503
  (query2) => typeof query2 === "function" ? query2(balance) : query2.every(([key, value]) => balance[key] === value)
3501
3504
  );
3502
- return new _Balances([...this].filter(filter2));
3505
+ return new _Balances(this.#toArray().filter(filter2));
3503
3506
  };
3504
3507
  /**
3505
3508
  * Filters this collection to exclude token balances where the token has a `mirrorOf` field
3506
3509
  * and another balance exists in this collection for the token specified by the `mirrorOf` field.
3507
3510
  */
3508
- filterMirrorTokens = () => new _Balances([...this].filter(filterMirrorTokens));
3511
+ filterMirrorTokens = () => {
3512
+ if (!this.#cachedFilteredMirrorTokens) {
3513
+ const balances = this.#toArray();
3514
+ const tokenIds = /* @__PURE__ */ new Set();
3515
+ for (const b of balances) {
3516
+ if (b.tokenId) tokenIds.add(b.tokenId);
3517
+ }
3518
+ this.#cachedFilteredMirrorTokens = new _Balances(
3519
+ balances.filter((balance) => {
3520
+ const mirrorOf = balance.token?.mirrorOf;
3521
+ return !mirrorOf || !tokenIds.has(mirrorOf);
3522
+ })
3523
+ );
3524
+ }
3525
+ return this.#cachedFilteredMirrorTokens;
3526
+ };
3509
3527
  /**
3510
3528
  * Filters this collection to only include balances which are not zero AND have a fiat conversion rate.
3511
3529
  */
@@ -3524,11 +3542,9 @@ var Balances = class _Balances {
3524
3542
  */
3525
3543
  add = (balances) => {
3526
3544
  if (balances instanceof Balance) return this.add(new _Balances(balances));
3527
- const mergedBalances = Object.fromEntries(
3528
- this.#balances.map((balance) => [balance.id, balance])
3529
- );
3530
- balances.each.forEach((balance) => mergedBalances[balance.id] = balance);
3531
- return new _Balances(Object.values(mergedBalances));
3545
+ const mergedMap = new Map(this.#balancesMap);
3546
+ for (const balance of balances) mergedMap.set(balance.id, balance);
3547
+ return new _Balances([...mergedMap.values()]);
3532
3548
  };
3533
3549
  /**
3534
3550
  * Remove balances from this collection by id.
@@ -3540,11 +3556,16 @@ var Balances = class _Balances {
3540
3556
  */
3541
3557
  remove = (ids) => {
3542
3558
  if (!Array.isArray(ids)) return this.remove([ids]);
3543
- return new _Balances(this.#balances.filter((balance) => !ids.includes(balance.id)));
3559
+ const idSet = new Set(ids);
3560
+ return new _Balances(this.#toArray().filter((balance) => !idSet.has(balance.id)));
3544
3561
  };
3545
3562
  // TODO: Add some more useful aggregator methods
3563
+ #toArray = () => {
3564
+ if (!this.#cachedArray) this.#cachedArray = [...this.#balancesMap.values()];
3565
+ return this.#cachedArray;
3566
+ };
3546
3567
  get each() {
3547
- return [...this];
3568
+ return this.#toArray();
3548
3569
  }
3549
3570
  /** @deprecated use each instead */
3550
3571
  get sorted() {
@@ -3556,7 +3577,7 @@ var Balances = class _Balances {
3556
3577
  * @returns The number of balances in this collection.
3557
3578
  */
3558
3579
  get count() {
3559
- return [...this].length;
3580
+ return this.#balancesMap.size;
3560
3581
  }
3561
3582
  /**
3562
3583
  * Get the summed value of balances in this collection.
@@ -3567,7 +3588,8 @@ var Balances = class _Balances {
3567
3588
  * balances.sum.fiat('usd').transferable
3568
3589
  */
3569
3590
  get sum() {
3570
- return new SumBalancesFormatter(this);
3591
+ if (!this.#cachedSumFormatter) this.#cachedSumFormatter = new SumBalancesFormatter(this);
3592
+ return this.#cachedSumFormatter;
3571
3593
  }
3572
3594
  };
3573
3595
  var getBalanceId = (balance) => {
@@ -7713,12 +7735,13 @@ var BalancesProvider = class {
7713
7735
  updateStorage$(balanceIds, balancesResult) {
7714
7736
  if (balancesResult.status !== "live") return;
7715
7737
  const storage = this.#storage.getValue();
7738
+ const failedIds = new Set(balancesResult.failedBalanceIds);
7716
7739
  const balances = (0, import_lodash_es27.assign)(
7717
7740
  {},
7718
7741
  storage.balances,
7719
7742
  // delete all balances expected in the result set (except the ones that failed). because if they are not present it means they are empty.
7720
7743
  (0, import_lodash_es27.fromPairs)(
7721
- balanceIds.filter((bid) => !balancesResult.failedBalanceIds.includes(bid)).map((balanceId) => [balanceId, void 0])
7744
+ balanceIds.filter((bid) => !failedIds.has(bid)).map((balanceId) => [balanceId, void 0])
7722
7745
  ),
7723
7746
  (0, import_lodash_es27.keyBy)(
7724
7747
  // storage balances must have status "cache", because they are used as start value when initialising subsequent subscriptions