@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/dist/index.mjs CHANGED
@@ -38,7 +38,7 @@ import anylogger from "anylogger";
38
38
  // package.json
39
39
  var package_default = {
40
40
  name: "@talismn/balances",
41
- version: "1.3.2",
41
+ version: "1.3.4",
42
42
  author: "Talisman",
43
43
  homepage: "https://talisman.xyz",
44
44
  license: "GPL-3.0-or-later",
@@ -81,7 +81,7 @@ var package_default = {
81
81
  "bignumber.js": "^9.1.2",
82
82
  "lodash-es": "4.17.21",
83
83
  "p-queue": "8.1.0",
84
- "polkadot-api": "1.13.1",
84
+ "polkadot-api": "1.23.3",
85
85
  rxjs: "^7.8.1",
86
86
  "scale-ts": "^1.6.1",
87
87
  viem: "^2.27.3",
@@ -3413,12 +3413,15 @@ var Balances = class _Balances {
3413
3413
  //
3414
3414
  // Properties
3415
3415
  //
3416
- #balances = [];
3416
+ #balancesMap = /* @__PURE__ */ new Map();
3417
+ #cachedArray = null;
3418
+ #cachedFilteredMirrorTokens = null;
3419
+ #cachedSumFormatter = null;
3417
3420
  //
3418
3421
  // Methods
3419
3422
  //
3420
3423
  constructor(balances, hydrate) {
3421
- if (balances instanceof _Balances) return new _Balances(balances.each, hydrate);
3424
+ if (balances instanceof _Balances) return new _Balances([...balances], hydrate);
3422
3425
  if (balances instanceof Balance) return new _Balances([balances], hydrate);
3423
3426
  if (!Array.isArray(balances)) return new _Balances(Object.values(balances), hydrate);
3424
3427
  if (balances.length === 0) return this;
@@ -3427,14 +3430,14 @@ var Balances = class _Balances {
3427
3430
  balances.map((storage) => new Balance(storage)),
3428
3431
  hydrate
3429
3432
  );
3430
- this.#balances = balances;
3433
+ this.#balancesMap = new Map(balances.map((b) => [b.id, b]));
3431
3434
  if (hydrate !== void 0) this.hydrate(hydrate);
3432
3435
  }
3433
3436
  /**
3434
3437
  * Calling toJSON on a collection of balances will return the underlying BalanceJsonList.
3435
3438
  */
3436
3439
  toJSON = () => Object.fromEntries(
3437
- this.#balances.map((balance) => {
3440
+ [...this.#balancesMap.values()].map((balance) => {
3438
3441
  try {
3439
3442
  return [balance.id, balance.toJSON()];
3440
3443
  } catch (error) {
@@ -3454,17 +3457,17 @@ var Balances = class _Balances {
3454
3457
  * // do something
3455
3458
  * }
3456
3459
  */
3457
- [Symbol.iterator] = () => (
3458
- // Create an array of the balances in this collection and return the result of its iterator.
3459
- this.#balances[Symbol.iterator]()
3460
- );
3460
+ [Symbol.iterator] = () => this.#balancesMap.values()[Symbol.iterator]();
3461
3461
  /**
3462
3462
  * Hydrates all balances in this collection.
3463
3463
  *
3464
3464
  * @param sources - The sources to hydrate from.
3465
3465
  */
3466
3466
  hydrate = (sources) => {
3467
- this.#balances.map((balance) => balance.hydrate(sources));
3467
+ this.#cachedArray = null;
3468
+ this.#cachedFilteredMirrorTokens = null;
3469
+ this.#cachedSumFormatter = null;
3470
+ for (const balance of this.#balancesMap.values()) balance.hydrate(sources);
3468
3471
  };
3469
3472
  /**
3470
3473
  * Retrieve a balance from this collection by id.
@@ -3472,7 +3475,7 @@ var Balances = class _Balances {
3472
3475
  * @param id - The id of the balance to fetch.
3473
3476
  * @returns The balance if one exists, or none.
3474
3477
  */
3475
- get = (id) => this.#balances.find((balance) => balance.id === id) ?? null;
3478
+ get = (id) => this.#balancesMap.get(id) ?? null;
3476
3479
  /**
3477
3480
  * Retrieve balances from this collection by search query.
3478
3481
  *
@@ -3487,13 +3490,28 @@ var Balances = class _Balances {
3487
3490
  const filter2 = (balance) => orQueries.some(
3488
3491
  (query2) => typeof query2 === "function" ? query2(balance) : query2.every(([key, value]) => balance[key] === value)
3489
3492
  );
3490
- return new _Balances([...this].filter(filter2));
3493
+ return new _Balances(this.#toArray().filter(filter2));
3491
3494
  };
3492
3495
  /**
3493
3496
  * Filters this collection to exclude token balances where the token has a `mirrorOf` field
3494
3497
  * and another balance exists in this collection for the token specified by the `mirrorOf` field.
3495
3498
  */
3496
- filterMirrorTokens = () => new _Balances([...this].filter(filterMirrorTokens));
3499
+ filterMirrorTokens = () => {
3500
+ if (!this.#cachedFilteredMirrorTokens) {
3501
+ const balances = this.#toArray();
3502
+ const tokenIds = /* @__PURE__ */ new Set();
3503
+ for (const b of balances) {
3504
+ if (b.tokenId) tokenIds.add(b.tokenId);
3505
+ }
3506
+ this.#cachedFilteredMirrorTokens = new _Balances(
3507
+ balances.filter((balance) => {
3508
+ const mirrorOf = balance.token?.mirrorOf;
3509
+ return !mirrorOf || !tokenIds.has(mirrorOf);
3510
+ })
3511
+ );
3512
+ }
3513
+ return this.#cachedFilteredMirrorTokens;
3514
+ };
3497
3515
  /**
3498
3516
  * Filters this collection to only include balances which are not zero AND have a fiat conversion rate.
3499
3517
  */
@@ -3512,11 +3530,9 @@ var Balances = class _Balances {
3512
3530
  */
3513
3531
  add = (balances) => {
3514
3532
  if (balances instanceof Balance) return this.add(new _Balances(balances));
3515
- const mergedBalances = Object.fromEntries(
3516
- this.#balances.map((balance) => [balance.id, balance])
3517
- );
3518
- balances.each.forEach((balance) => mergedBalances[balance.id] = balance);
3519
- return new _Balances(Object.values(mergedBalances));
3533
+ const mergedMap = new Map(this.#balancesMap);
3534
+ for (const balance of balances) mergedMap.set(balance.id, balance);
3535
+ return new _Balances([...mergedMap.values()]);
3520
3536
  };
3521
3537
  /**
3522
3538
  * Remove balances from this collection by id.
@@ -3528,11 +3544,16 @@ var Balances = class _Balances {
3528
3544
  */
3529
3545
  remove = (ids) => {
3530
3546
  if (!Array.isArray(ids)) return this.remove([ids]);
3531
- return new _Balances(this.#balances.filter((balance) => !ids.includes(balance.id)));
3547
+ const idSet = new Set(ids);
3548
+ return new _Balances(this.#toArray().filter((balance) => !idSet.has(balance.id)));
3532
3549
  };
3533
3550
  // TODO: Add some more useful aggregator methods
3551
+ #toArray = () => {
3552
+ if (!this.#cachedArray) this.#cachedArray = [...this.#balancesMap.values()];
3553
+ return this.#cachedArray;
3554
+ };
3534
3555
  get each() {
3535
- return [...this];
3556
+ return this.#toArray();
3536
3557
  }
3537
3558
  /** @deprecated use each instead */
3538
3559
  get sorted() {
@@ -3544,7 +3565,7 @@ var Balances = class _Balances {
3544
3565
  * @returns The number of balances in this collection.
3545
3566
  */
3546
3567
  get count() {
3547
- return [...this].length;
3568
+ return this.#balancesMap.size;
3548
3569
  }
3549
3570
  /**
3550
3571
  * Get the summed value of balances in this collection.
@@ -3555,7 +3576,8 @@ var Balances = class _Balances {
3555
3576
  * balances.sum.fiat('usd').transferable
3556
3577
  */
3557
3578
  get sum() {
3558
- return new SumBalancesFormatter(this);
3579
+ if (!this.#cachedSumFormatter) this.#cachedSumFormatter = new SumBalancesFormatter(this);
3580
+ return this.#cachedSumFormatter;
3559
3581
  }
3560
3582
  };
3561
3583
  var getBalanceId = (balance) => {
@@ -7715,12 +7737,13 @@ var BalancesProvider = class {
7715
7737
  updateStorage$(balanceIds, balancesResult) {
7716
7738
  if (balancesResult.status !== "live") return;
7717
7739
  const storage = this.#storage.getValue();
7740
+ const failedIds = new Set(balancesResult.failedBalanceIds);
7718
7741
  const balances = assign11(
7719
7742
  {},
7720
7743
  storage.balances,
7721
7744
  // delete all balances expected in the result set (except the ones that failed). because if they are not present it means they are empty.
7722
7745
  fromPairs2(
7723
- balanceIds.filter((bid) => !balancesResult.failedBalanceIds.includes(bid)).map((balanceId) => [balanceId, void 0])
7746
+ balanceIds.filter((bid) => !failedIds.has(bid)).map((balanceId) => [balanceId, void 0])
7724
7747
  ),
7725
7748
  keyBy8(
7726
7749
  // storage balances must have status "cache", because they are used as start value when initialising subsequent subscriptions