@talismn/balances 0.3.2 → 0.4.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,34 @@
1
1
  # @talismn/balances
2
2
 
3
+ ## 0.4.0
4
+
5
+ ### Patch Changes
6
+
7
+ - 3068bd60: feat: stale balances and exponential rpc backoff
8
+ - 6643a4e4: fix: tokenRates in @talismn/balances-react
9
+ - 6643a4e4: fix: ported useDbCache related perf fixes to @talismn/balances-react
10
+ - Updated dependencies [3068bd60]
11
+ - Updated dependencies [6643a4e4]
12
+ - Updated dependencies [79f6ccf6]
13
+ - Updated dependencies [c24dc1fb]
14
+ - @talismn/chain-connector@0.4.3
15
+ - @talismn/util@0.1.8
16
+ - @talismn/token-rates@0.1.15
17
+ - @talismn/chaindata-provider@0.4.3
18
+ - @talismn/chain-connector-evm@0.4.3
19
+
20
+ ## 0.3.3
21
+
22
+ ### Patch Changes
23
+
24
+ - c651551c: build: move `@polkadot` dependencies to `peerDependencies`
25
+ - Updated dependencies [c651551c]
26
+ - @talismn/chain-connector@0.4.2
27
+ - @talismn/util@0.1.7
28
+ - @talismn/chain-connector-evm@0.4.2
29
+ - @talismn/chaindata-provider@0.4.2
30
+ - @talismn/token-rates@0.1.14
31
+
3
32
  ## 0.3.2
4
33
 
5
34
  ## 0.3.1
@@ -1,43 +1,65 @@
1
+ import { UnsignedTransaction } from "@substrate/txwrapper-core";
1
2
  import { ChainConnector } from "@talismn/chain-connector";
2
3
  import { ChainConnectorEvm } from "@talismn/chain-connector-evm";
3
4
  import { ChainId, ChaindataProvider, IToken } from "@talismn/chaindata-provider";
5
+ import { ethers } from "ethers";
4
6
  import { AddressesByToken, Balances, SubscriptionCallback, UnsubscribeFn } from "./types";
5
- export interface BalanceModule<TModuleType extends string, TTokenType extends ExtendableTokenType, TChainMeta extends ExtendableChainMeta = DefaultChainMeta, TModuleConfig extends ExtendableModuleConfig = DefaultModuleConfig> extends BalanceModuleSubstrate<TModuleType, TTokenType, TChainMeta, TModuleConfig>, BalanceModuleEvm<TModuleType, TTokenType, TChainMeta, TModuleConfig> {
6
- }
7
- export declare const DefaultBalanceModule: <TModuleType extends string, TTokenType extends IToken, TChainMeta extends ExtendableChainMeta = undefined, TModuleConfig extends ExtendableModuleConfig = undefined>(type: TModuleType) => BalanceModule<TModuleType, TTokenType, TChainMeta, TModuleConfig>;
8
7
  export type ExtendableTokenType = IToken;
9
8
  export type ExtendableChainMeta = Record<string, unknown> | undefined;
10
9
  export type DefaultChainMeta = undefined;
11
10
  export type ExtendableModuleConfig = Record<string, unknown> | undefined;
12
11
  export type DefaultModuleConfig = undefined;
13
- interface BalanceModuleSubstrate<TModuleType extends string, TTokenType extends ExtendableTokenType, TChainMeta extends ExtendableChainMeta = DefaultChainMeta, TModuleConfig extends ExtendableModuleConfig = DefaultModuleConfig> extends BalanceModuleCommon<TModuleType, TTokenType> {
12
+ export type BaseTransferParams = {
13
+ tokenId: string;
14
+ from: string;
15
+ to: string;
16
+ amount: string;
17
+ };
18
+ export type ExtendableTransferParams = BaseTransferParams | undefined;
19
+ export type DefaultTransferParams = undefined;
20
+ export type NewTransferParamsType<T extends Record<string, unknown>> = BaseTransferParams & T;
21
+ export type TransferTokenTx = {
22
+ type: "substrate";
23
+ tx: UnsignedTransaction;
24
+ } | {
25
+ type: "evm";
26
+ tx: ethers.providers.TransactionRequest;
27
+ };
28
+ export type ChainConnectors = {
29
+ substrate?: ChainConnector;
30
+ evm?: ChainConnectorEvm;
31
+ };
32
+ export type Hydrate = {
33
+ chainConnectors: ChainConnectors;
34
+ chaindataProvider: ChaindataProvider;
35
+ };
36
+ export type NewBalanceModule<TModuleType extends string, TTokenType extends ExtendableTokenType, TChainMeta extends ExtendableChainMeta = DefaultChainMeta, TModuleConfig extends ExtendableModuleConfig = DefaultModuleConfig, TTransferParams extends ExtendableTransferParams = DefaultTransferParams> = (hydrate: Hydrate) => BalanceModule<TModuleType, TTokenType, TChainMeta, TModuleConfig, TTransferParams>;
37
+ export interface BalanceModule<TModuleType extends string, TTokenType extends ExtendableTokenType, TChainMeta extends ExtendableChainMeta = DefaultChainMeta, TModuleConfig extends ExtendableModuleConfig = DefaultModuleConfig, TTransferParams extends ExtendableTransferParams = DefaultTransferParams> extends BalanceModuleSubstrate<TModuleType, TTokenType, TChainMeta, TModuleConfig, TTransferParams>, BalanceModuleEvm<TModuleType, TTokenType, TChainMeta, TModuleConfig, TTransferParams> {
38
+ }
39
+ export declare const DefaultBalanceModule: <TModuleType extends string, TTokenType extends IToken, TChainMeta extends ExtendableChainMeta = undefined, TModuleConfig extends ExtendableModuleConfig = undefined, TTransferParams extends ExtendableTransferParams = undefined>(type: TModuleType) => BalanceModule<TModuleType, TTokenType, TChainMeta, TModuleConfig, TTransferParams>;
40
+ interface BalanceModuleSubstrate<TModuleType extends string, TTokenType extends ExtendableTokenType, TChainMeta extends ExtendableChainMeta = DefaultChainMeta, TModuleConfig extends ExtendableModuleConfig = DefaultModuleConfig, TTransferParams extends ExtendableTransferParams = DefaultTransferParams> extends BalanceModuleCommon<TModuleType, TTokenType, TTransferParams> {
14
41
  /** Pre-processes any substrate chain metadata required by this module ahead of time */
15
- fetchSubstrateChainMeta(chainConnector: ChainConnector, chaindataProvider: ChaindataProvider, chainId: ChainId, moduleConfig: TModuleConfig | undefined): Promise<TChainMeta | null>;
42
+ fetchSubstrateChainMeta(chainId: ChainId, moduleConfig?: TModuleConfig): Promise<TChainMeta | null>;
16
43
  /** Detects which tokens are available on a given substrate chain */
17
- fetchSubstrateChainTokens(chainConnector: ChainConnector, chaindataProvider: ChaindataProvider, chainId: ChainId, chainMeta: TChainMeta, moduleConfig: TModuleConfig | undefined): Promise<Record<TTokenType["id"], TTokenType>>;
44
+ fetchSubstrateChainTokens(chainId: ChainId, chainMeta: TChainMeta, moduleConfig?: TModuleConfig): Promise<Record<TTokenType["id"], TTokenType>>;
18
45
  }
19
- interface BalanceModuleEvm<TModuleType extends string, TTokenType extends ExtendableTokenType, TChainMeta extends ExtendableChainMeta = DefaultChainMeta, TModuleConfig extends ExtendableModuleConfig = DefaultModuleConfig> extends BalanceModuleCommon<TModuleType, TTokenType> {
46
+ interface BalanceModuleEvm<TModuleType extends string, TTokenType extends ExtendableTokenType, TChainMeta extends ExtendableChainMeta = DefaultChainMeta, TModuleConfig extends ExtendableModuleConfig = DefaultModuleConfig, TTransferParams extends ExtendableTransferParams = DefaultTransferParams> extends BalanceModuleCommon<TModuleType, TTokenType, TTransferParams> {
20
47
  /** Pre-processes any evm chain metadata required by this module ahead of time */
21
- fetchEvmChainMeta(chainConnector: ChainConnectorEvm, chaindataProvider: ChaindataProvider, chainId: ChainId, moduleConfig: TModuleConfig | undefined): Promise<TChainMeta | null>;
48
+ fetchEvmChainMeta(chainId: ChainId, moduleConfig?: TModuleConfig): Promise<TChainMeta | null>;
22
49
  /** Detects which tokens are available on a given evm chain */
23
- fetchEvmChainTokens(chainConnector: ChainConnectorEvm, chaindataProvider: ChaindataProvider, chainId: ChainId, chainMeta: TChainMeta, moduleConfig: TModuleConfig | undefined): Promise<Record<TTokenType["id"], TTokenType>>;
50
+ fetchEvmChainTokens(chainId: ChainId, chainMeta: TChainMeta, moduleConfig?: TModuleConfig): Promise<Record<TTokenType["id"], TTokenType>>;
24
51
  }
25
- interface BalanceModuleCommon<TModuleType extends string, TTokenType extends ExtendableTokenType> {
52
+ interface BalanceModuleCommon<TModuleType extends string, TTokenType extends ExtendableTokenType, TTransferParams extends ExtendableTransferParams> {
26
53
  get type(): TModuleType;
27
54
  /**
28
55
  * Subscribe to balances for this module with optional filtering.
29
56
  *
30
57
  * If subscriptions are not possible, this function should poll at some reasonable interval.
31
58
  */
32
- subscribeBalances(chainConnectors: {
33
- substrate?: ChainConnector;
34
- evm?: ChainConnectorEvm;
35
- }, chaindataProvider: ChaindataProvider, addressesByToken: AddressesByToken<TTokenType>, callback: SubscriptionCallback<Balances>): Promise<UnsubscribeFn>;
59
+ subscribeBalances(addressesByToken: AddressesByToken<TTokenType>, callback: SubscriptionCallback<Balances>): Promise<UnsubscribeFn>;
36
60
  /** Fetch balances for this module with optional filtering */
37
- fetchBalances(chainConnectors: {
38
- substrate?: ChainConnector;
39
- evm?: ChainConnectorEvm;
40
- }, chaindataProvider: ChaindataProvider, addressesByToken: AddressesByToken<TTokenType>): Promise<Balances>;
41
- [x: string | number | symbol]: unknown;
61
+ fetchBalances(addressesByToken: AddressesByToken<TTokenType>): Promise<Balances>;
62
+ /** Prepare a tx to transfer some tokens from this module */
63
+ transferToken(transferParams: TTransferParams): Promise<TransferTokenTx | null>;
42
64
  }
43
65
  export {};
@@ -1,21 +1,16 @@
1
- import { TypeRegistry } from "@polkadot/types";
2
- import { ChainConnector } from "@talismn/chain-connector";
3
- import { ChainConnectorEvm } from "@talismn/chain-connector-evm";
4
- import { ChaindataProvider } from "@talismn/chaindata-provider";
5
- import { BalanceModule, DefaultChainMeta, DefaultModuleConfig, ExtendableChainMeta, ExtendableModuleConfig, ExtendableTokenType } from "./BalanceModule";
1
+ import type { Registry } from "@polkadot/types-codec/types";
2
+ import { ChainId } from "@talismn/chaindata-provider";
3
+ import { BalanceModule, DefaultChainMeta, DefaultModuleConfig, DefaultTransferParams, ExtendableChainMeta, ExtendableModuleConfig, ExtendableTokenType, ExtendableTransferParams } from "./BalanceModule";
6
4
  import { AddressesByToken, Balance, Balances, SubscriptionCallback, UnsubscribeFn } from "./types";
7
5
  /**
8
6
  * Wraps a BalanceModule's fetch/subscribe methods with a single `balances` method.
9
7
  * This `balances` method will subscribe if a callback parameter is provided, or otherwise fetch.
10
8
  */
11
- export declare function balances<TModuleType extends string, TTokenType extends ExtendableTokenType, TChainMeta extends ExtendableChainMeta = DefaultChainMeta, TModuleConfig extends ExtendableModuleConfig = DefaultModuleConfig>(balanceModule: BalanceModule<TModuleType, TTokenType, TChainMeta, TModuleConfig>, chainConnectors: {
12
- substrate?: ChainConnector;
13
- evm?: ChainConnectorEvm;
14
- }, chaindataProvider: ChaindataProvider, addressesByToken: AddressesByToken<TTokenType>): Promise<Balances>;
15
- export declare function balances<TModuleType extends string, TTokenType extends ExtendableTokenType, TChainMeta extends ExtendableChainMeta = DefaultChainMeta, TModuleConfig extends ExtendableModuleConfig = DefaultModuleConfig>(balanceModule: BalanceModule<TModuleType, TTokenType, TChainMeta, TModuleConfig>, chainConnectors: {
16
- substrate?: ChainConnector;
17
- evm?: ChainConnectorEvm;
18
- }, chaindataProvider: ChaindataProvider, addressesByToken: AddressesByToken<TTokenType>, callback: SubscriptionCallback<Balances>): Promise<UnsubscribeFn>;
9
+ export declare function balances<TModuleType extends string, TTokenType extends ExtendableTokenType, TChainMeta extends ExtendableChainMeta = DefaultChainMeta, TModuleConfig extends ExtendableModuleConfig = DefaultModuleConfig, TTransferParams extends ExtendableTransferParams = DefaultTransferParams>(balanceModule: BalanceModule<TModuleType, TTokenType, TChainMeta, TModuleConfig, TTransferParams>, addressesByToken: AddressesByToken<TTokenType>): Promise<Balances>;
10
+ export declare function balances<TModuleType extends string, TTokenType extends ExtendableTokenType, TChainMeta extends ExtendableChainMeta = DefaultChainMeta, TModuleConfig extends ExtendableModuleConfig = DefaultModuleConfig, TTransferParams extends ExtendableTransferParams = DefaultTransferParams>(balanceModule: BalanceModule<TModuleType, TTokenType, TChainMeta, TModuleConfig, TTransferParams>, addressesByToken: AddressesByToken<TTokenType>, callback: SubscriptionCallback<Balances>): Promise<UnsubscribeFn>;
11
+ export declare const createTypeRegistryCache: () => {
12
+ getOrCreateTypeRegistry: (chainId: ChainId, metadataRpc?: `0x${string}`) => Registry;
13
+ };
19
14
  export declare const filterMirrorTokens: (balance: Balance, i: number, balances: Balance[]) => boolean;
20
15
  /**
21
16
  * Used by a variety of balance modules to help encode and decode substrate state calls.
@@ -23,7 +18,7 @@ export declare const filterMirrorTokens: (balance: Balance, i: number, balances:
23
18
  export declare class StorageHelper {
24
19
  #private;
25
20
  tags: any;
26
- constructor(registry: TypeRegistry, module: string, method: string, ...parameters: any[]);
21
+ constructor(registry: Registry, module: string, method: string, ...parameters: any[]);
27
22
  get stateKey(): `0x${string}` | undefined;
28
23
  get module(): string;
29
24
  get method(): string;
@@ -80,6 +80,7 @@ export declare class Balances {
80
80
  * @returns The new collection of balances.
81
81
  */
82
82
  remove: (ids: string[] | string) => Balances;
83
+ get each(): Balance[];
83
84
  /**
84
85
  * Get an array of balances in this collection, sorted by chain sortIndex.
85
86
  *
@@ -23,7 +23,7 @@ export type BalanceTypes = {
23
23
  export type BalanceJson = BalanceTypes[keyof BalanceTypes] extends never ? IBalance : BalanceTypes[keyof BalanceTypes];
24
24
  /** A collection of `BalanceJson` objects */
25
25
  export type BalanceJsonList = Record<string, BalanceJson>;
26
- export type BalanceStatus = "live" | "cache";
26
+ export type BalanceStatus = "live" | "cache" | "stale";
27
27
  /** `IBalance` is a common interface which all balance types must implement. */
28
28
  export type IBalance = {
29
29
  /** The module that this balance was retrieved by */
@@ -6,17 +6,10 @@ var dexie = require('dexie');
6
6
  var types = require('@polkadot/types');
7
7
  var anylogger = require('anylogger');
8
8
  var util = require('@talismn/util');
9
- var memoize = require('lodash/memoize');
10
- var typescriptMemoize = require('typescript-memoize');
11
9
 
12
10
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
13
11
 
14
12
  var anylogger__default = /*#__PURE__*/_interopDefault(anylogger);
15
- var memoize__default = /*#__PURE__*/_interopDefault(memoize);
16
-
17
- //
18
- // exported
19
- //
20
13
 
21
14
  // TODO: Document default balances module purpose/usage
22
15
  const DefaultBalanceModule = type => ({
@@ -35,12 +28,15 @@ const DefaultBalanceModule = type => ({
35
28
  async fetchEvmChainTokens() {
36
29
  return Promise.resolve({});
37
30
  },
38
- async subscribeBalances(_chainConnectors, _chaindataProvider, _addressesByToken, callback) {
31
+ async subscribeBalances(_, callback) {
39
32
  callback(new Error("Balance subscriptions are not implemented in this module."));
40
33
  return () => {};
41
34
  },
42
35
  async fetchBalances() {
43
36
  throw new Error("Balance fetching is not implemented in this module.");
37
+ },
38
+ async transferToken() {
39
+ throw new Error("Token transfers are not implemented in this module.");
44
40
  }
45
41
  });
46
42
 
@@ -71,7 +67,7 @@ const db = new TalismanBalancesDatabase();
71
67
 
72
68
  var packageJson = {
73
69
  name: "@talismn/balances",
74
- version: "0.3.2",
70
+ version: "0.4.0",
75
71
  author: "Talisman",
76
72
  homepage: "https://talisman.xyz",
77
73
  license: "UNLICENSED",
@@ -90,7 +86,7 @@ var packageJson = {
90
86
  "/plugins"
91
87
  ],
92
88
  engines: {
93
- node: ">=14"
89
+ node: ">=18"
94
90
  },
95
91
  scripts: {
96
92
  test: "jest",
@@ -98,18 +94,16 @@ var packageJson = {
98
94
  clean: "rm -rf dist && rm -rf .turbo rm -rf node_modules"
99
95
  },
100
96
  dependencies: {
101
- "@polkadot/types": "9.10.5",
102
97
  "@talismn/chain-connector": "workspace:^",
103
98
  "@talismn/chain-connector-evm": "workspace:^",
104
99
  "@talismn/chaindata-provider": "workspace:^",
105
100
  "@talismn/token-rates": "workspace:^",
106
101
  "@talismn/util": "workspace:^",
107
102
  anylogger: "^1.0.11",
108
- dexie: "^3.2.2",
109
- lodash: "^4.17.21",
110
- "typescript-memoize": "^1.1.0"
103
+ dexie: "^3.2.3"
111
104
  },
112
105
  devDependencies: {
106
+ "@polkadot/types": "^9.10.5",
113
107
  "@talismn/eslint-config": "workspace:^",
114
108
  "@talismn/tsconfig": "workspace:^",
115
109
  "@types/jest": "^27.5.1",
@@ -118,6 +112,9 @@ var packageJson = {
118
112
  "ts-jest": "^28.0.2",
119
113
  typescript: "^4.6.4"
120
114
  },
115
+ peerDependencies: {
116
+ "@polkadot/types": "9.x"
117
+ },
121
118
  preconstruct: {
122
119
  entrypoints: [
123
120
  "index.ts",
@@ -134,15 +131,33 @@ var packageJson = {
134
131
 
135
132
  var log = anylogger__default["default"](packageJson.name);
136
133
 
137
- async function balances(balanceModule, chainConnectors, chaindataProvider, addressesByToken, callback) {
134
+ async function balances(balanceModule, addressesByToken, callback) {
138
135
  // subscription request
139
- if (callback !== undefined) return await balanceModule.subscribeBalances(chainConnectors, chaindataProvider, addressesByToken, callback);
136
+ if (callback !== undefined) return await balanceModule.subscribeBalances(addressesByToken, callback);
140
137
 
141
138
  // one-off request
142
- return await balanceModule.fetchBalances(chainConnectors, chaindataProvider, addressesByToken);
139
+ return await balanceModule.fetchBalances(addressesByToken);
143
140
  }
141
+ const createTypeRegistryCache = () => {
142
+ const typeRegistryCache = new Map();
143
+ const getOrCreateTypeRegistry = (chainId, metadataRpc) => {
144
+ // TODO: Delete cache when metadataRpc is different from last time
145
+ const cached = typeRegistryCache.get(chainId);
146
+ if (cached) return cached;
147
+ const typeRegistry = new types.TypeRegistry();
148
+ if (typeof metadataRpc === "string") {
149
+ const metadata = new types.Metadata(typeRegistry, metadataRpc);
150
+ metadata.registry.setMetadata(metadata);
151
+ }
152
+ typeRegistryCache.set(chainId, typeRegistry);
153
+ return typeRegistry;
154
+ };
155
+ return {
156
+ getOrCreateTypeRegistry
157
+ };
158
+ };
144
159
  const filterMirrorTokens = (balance, i, balances) => {
145
- // TODO implement a mirrorOf property, which should be set from chaindata
160
+ // TODO: implement a mirrorOf property, which should be set from chaindata
146
161
  const mirrorOf = balance.token?.mirrorOf;
147
162
  return !mirrorOf || !balances.find(b => b.tokenId === mirrorOf);
148
163
  };
@@ -228,30 +243,6 @@ class StorageHelper {
228
243
  }
229
244
  }
230
245
 
231
- function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
232
- var desc = {};
233
- Object.keys(descriptor).forEach(function (key) {
234
- desc[key] = descriptor[key];
235
- });
236
- desc.enumerable = !!desc.enumerable;
237
- desc.configurable = !!desc.configurable;
238
- if ('value' in desc || desc.initializer) {
239
- desc.writable = true;
240
- }
241
- desc = decorators.slice().reverse().reduce(function (desc, decorator) {
242
- return decorator(target, property, desc) || desc;
243
- }, desc);
244
- if (context && desc.initializer !== void 0) {
245
- desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
246
- desc.initializer = undefined;
247
- }
248
- if (desc.initializer === void 0) {
249
- Object.defineProperty(target, property, desc);
250
- desc = null;
251
- }
252
- return desc;
253
- }
254
-
255
246
  function excludeFromTransferableAmount(locks) {
256
247
  if (typeof locks === "string") return BigInt(locks);
257
248
  if (!Array.isArray(locks)) locks = [locks];
@@ -273,8 +264,6 @@ function includeInTotalExtraAmount(extra) {
273
264
 
274
265
  /** Used by plugins to help define their custom `BalanceType` */
275
266
 
276
- var _dec, _dec2, _dec3, _class, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _class2, _dec11, _class3, _dec12, _dec13, _dec14, _dec15, _dec16, _dec17, _dec18, _class4;
277
-
278
267
  /**
279
268
  * Have the importing library define its Token and BalanceJson enums (as a sum type of all plugins) and pass them into some
280
269
  * internal global typescript context, which is then picked up on by this module.
@@ -285,12 +274,12 @@ var _dec, _dec2, _dec3, _class, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10
285
274
  /**
286
275
  * A collection of balances.
287
276
  */
288
- let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Memoize(), _dec3 = typescriptMemoize.Memoize(), (_class = class Balances {
277
+ class Balances {
289
278
  //
290
279
  // Properties
291
280
  //
292
281
 
293
- #balances = {};
282
+ #balances = [];
294
283
 
295
284
  //
296
285
  // Methods
@@ -298,7 +287,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
298
287
 
299
288
  constructor(balances, hydrate) {
300
289
  // handle Balances (convert to Balance[])
301
- if (balances instanceof Balances) return new Balances([...balances], hydrate);
290
+ if (balances instanceof Balances) return new Balances(balances.each, hydrate);
302
291
 
303
292
  // handle Balance (convert to Balance[])
304
293
  if (balances instanceof Balance) return new Balances([balances], hydrate);
@@ -313,19 +302,19 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
313
302
  if (!util.isArrayOf(balances, Balance)) return new Balances(balances.map(storage => new Balance(storage)), hydrate);
314
303
 
315
304
  // handle Balance[]
316
- this.#balances = Object.fromEntries(balances.map(balance => [balance.id, balance]));
305
+ this.#balances = balances;
317
306
  if (hydrate !== undefined) this.hydrate(hydrate);
318
307
  }
319
308
 
320
309
  /**
321
310
  * Calling toJSON on a collection of balances will return the underlying BalanceJsonList.
322
311
  */
323
- toJSON = () => Object.fromEntries(Object.entries(this.#balances).map(([id, balance]) => {
312
+ toJSON = () => Object.fromEntries(this.#balances.map(balance => {
324
313
  try {
325
- return [id, balance.toJSON()];
314
+ return [balance.id, balance.toJSON()];
326
315
  } catch (error) {
327
316
  log.error("Failed to convert balance to JSON", error, {
328
- id,
317
+ id: balance.id,
329
318
  balance
330
319
  });
331
320
  return null;
@@ -345,7 +334,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
345
334
  */
346
335
  [Symbol.iterator] = () =>
347
336
  // Create an array of the balances in this collection and return the result of its iterator.
348
- Object.values(this.#balances)[Symbol.iterator]();
337
+ this.#balances[Symbol.iterator]();
349
338
 
350
339
  /**
351
340
  * Hydrates all balances in this collection.
@@ -353,7 +342,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
353
342
  * @param sources - The sources to hydrate from.
354
343
  */
355
344
  hydrate = sources => {
356
- Object.values(this.#balances).map(balance => balance.hydrate(sources));
345
+ this.#balances.map(balance => balance.hydrate(sources));
357
346
  };
358
347
 
359
348
  /**
@@ -362,7 +351,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
362
351
  * @param id - The id of the balance to fetch.
363
352
  * @returns The balance if one exists, or none.
364
353
  */
365
- get = id => this.#balances[id] || null;
354
+ get = id => this.#balances.find(balance => balance.id === id) ?? null;
366
355
 
367
356
  /**
368
357
  * Retrieve balances from this collection by search query.
@@ -395,10 +384,8 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
395
384
  if (balances instanceof Balance) return this.add(new Balances(balances));
396
385
 
397
386
  // merge balances
398
- const mergedBalances = {
399
- ...this.#balances
400
- };
401
- [...balances].forEach(balance => mergedBalances[balance.id] = balance);
387
+ const mergedBalances = Object.fromEntries(this.#balances.map(balance => [balance.id, balance]));
388
+ balances.each.forEach(balance => mergedBalances[balance.id] = balance);
402
389
 
403
390
  // return new balances
404
391
  return new Balances(Object.values(mergedBalances));
@@ -416,25 +403,23 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
416
403
  // handle single id
417
404
  if (!Array.isArray(ids)) return this.remove([ids]);
418
405
 
419
- // merge balances
420
- const removedBalances = {
421
- ...this.#balances
422
- };
423
- ids.forEach(id => delete removedBalances[id]);
424
-
425
- // return new balances
426
- return new Balances(Object.values(removedBalances));
406
+ // merge and return new balances
407
+ return new Balances(this.#balances.filter(balance => !ids.includes(balance.id)));
427
408
  };
428
409
 
429
410
  // TODO: Add some more useful aggregator methods
430
411
 
412
+ get each() {
413
+ return [...this];
414
+ }
415
+
431
416
  /**
432
417
  * Get an array of balances in this collection, sorted by chain sortIndex.
433
418
  *
434
419
  * @returns A sorted array of the balances in this collection.
435
420
  */
436
421
  get sorted() {
437
- return [...this].sort((a, b) => ((a.chain || a.evmNetwork)?.sortIndex || Number.MAX_SAFE_INTEGER) - ((b.chain || b.evmNetwork)?.sortIndex || Number.MAX_SAFE_INTEGER));
422
+ return [...this].sort((a, b) => ((a.chain || a.evmNetwork)?.sortIndex ?? Number.MAX_SAFE_INTEGER) - ((b.chain || b.evmNetwork)?.sortIndex ?? Number.MAX_SAFE_INTEGER));
438
423
  }
439
424
 
440
425
  /**
@@ -457,12 +442,12 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
457
442
  get sum() {
458
443
  return new SumBalancesFormatter(this);
459
444
  }
460
- }, (_applyDecoratedDescriptor(_class.prototype, "sorted", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "sorted"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "count", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "count"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "sum", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "sum"), _class.prototype)), _class));
445
+ }
461
446
 
462
447
  /**
463
448
  * An individual balance.
464
449
  */
465
- let Balance = (_dec4 = typescriptMemoize.Memoize(), _dec5 = typescriptMemoize.Memoize(), _dec6 = typescriptMemoize.Memoize(), _dec7 = typescriptMemoize.Memoize(), _dec8 = typescriptMemoize.Memoize(), _dec9 = typescriptMemoize.Memoize(), _dec10 = typescriptMemoize.Memoize(), (_class2 = class Balance {
450
+ class Balance {
466
451
  //
467
452
  // Properties
468
453
  //
@@ -476,7 +461,6 @@ let Balance = (_dec4 = typescriptMemoize.Memoize(), _dec5 = typescriptMemoize.Me
476
461
  //
477
462
 
478
463
  constructor(storage, hydrate) {
479
- this.#format = memoize__default["default"](this.#format);
480
464
  this.#storage = storage;
481
465
  if (hydrate !== undefined) this.hydrate(hydrate);
482
466
  }
@@ -599,8 +583,8 @@ let Balance = (_dec4 = typescriptMemoize.Memoize(), _dec5 = typescriptMemoize.Me
599
583
  // subtract the lock from the free amount (but don't go below 0)
600
584
  return this.#format(util.BigMath.max(this.free.planck - excludeAmount, BigInt("0")));
601
585
  }
602
- }, (_applyDecoratedDescriptor(_class2.prototype, "total", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "total"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "free", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "free"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "reserved", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "reserved"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "locked", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "locked"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "frozen", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "frozen"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "transferable", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "transferable"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "feePayable", [_dec10], Object.getOwnPropertyDescriptor(_class2.prototype, "feePayable"), _class2.prototype)), _class2));
603
- let BalanceFormatter = (_dec11 = typescriptMemoize.Memoize(), (_class3 = class BalanceFormatter {
586
+ }
587
+ class BalanceFormatter {
604
588
  #planck;
605
589
  #decimals;
606
590
  #fiatRatios;
@@ -608,7 +592,6 @@ let BalanceFormatter = (_dec11 = typescriptMemoize.Memoize(), (_class3 = class B
608
592
  this.#planck = typeof planck === "bigint" ? planck.toString() : planck ?? "0";
609
593
  this.#decimals = decimals || 0;
610
594
  this.#fiatRatios = fiatRatios || null;
611
- this.fiat = memoize__default["default"](this.fiat);
612
595
  }
613
596
  toJSON = () => this.#planck;
614
597
  get planck() {
@@ -623,14 +606,13 @@ let BalanceFormatter = (_dec11 = typescriptMemoize.Memoize(), (_class3 = class B
623
606
  if (!ratio) return null;
624
607
  return parseFloat(this.tokens) * ratio;
625
608
  }
626
- }, (_applyDecoratedDescriptor(_class3.prototype, "tokens", [_dec11], Object.getOwnPropertyDescriptor(_class3.prototype, "tokens"), _class3.prototype)), _class3));
627
- let FiatSumBalancesFormatter = (_dec12 = typescriptMemoize.Memoize(), _dec13 = typescriptMemoize.Memoize(), _dec14 = typescriptMemoize.Memoize(), _dec15 = typescriptMemoize.Memoize(), _dec16 = typescriptMemoize.Memoize(), _dec17 = typescriptMemoize.Memoize(), _dec18 = typescriptMemoize.Memoize(), (_class4 = class FiatSumBalancesFormatter {
609
+ }
610
+ class FiatSumBalancesFormatter {
628
611
  #balances;
629
612
  #currency;
630
613
  constructor(balances, currency) {
631
614
  this.#balances = balances;
632
615
  this.#currency = currency;
633
- this.#sum = memoize__default["default"](this.#sum);
634
616
  }
635
617
  #sum = balanceField => {
636
618
  // a function to get a fiat amount from a balance
@@ -677,12 +659,11 @@ let FiatSumBalancesFormatter = (_dec12 = typescriptMemoize.Memoize(), _dec13 = t
677
659
  get feePayable() {
678
660
  return this.#sum("feePayable");
679
661
  }
680
- }, (_applyDecoratedDescriptor(_class4.prototype, "total", [_dec12], Object.getOwnPropertyDescriptor(_class4.prototype, "total"), _class4.prototype), _applyDecoratedDescriptor(_class4.prototype, "free", [_dec13], Object.getOwnPropertyDescriptor(_class4.prototype, "free"), _class4.prototype), _applyDecoratedDescriptor(_class4.prototype, "reserved", [_dec14], Object.getOwnPropertyDescriptor(_class4.prototype, "reserved"), _class4.prototype), _applyDecoratedDescriptor(_class4.prototype, "locked", [_dec15], Object.getOwnPropertyDescriptor(_class4.prototype, "locked"), _class4.prototype), _applyDecoratedDescriptor(_class4.prototype, "frozen", [_dec16], Object.getOwnPropertyDescriptor(_class4.prototype, "frozen"), _class4.prototype), _applyDecoratedDescriptor(_class4.prototype, "transferable", [_dec17], Object.getOwnPropertyDescriptor(_class4.prototype, "transferable"), _class4.prototype), _applyDecoratedDescriptor(_class4.prototype, "feePayable", [_dec18], Object.getOwnPropertyDescriptor(_class4.prototype, "feePayable"), _class4.prototype)), _class4));
662
+ }
681
663
  class SumBalancesFormatter {
682
664
  #balances;
683
665
  constructor(balances) {
684
666
  this.#balances = balances;
685
- this.fiat = memoize__default["default"](this.fiat);
686
667
  }
687
668
  fiat(currency) {
688
669
  return new FiatSumBalancesFormatter(this.#balances, currency);
@@ -698,6 +679,7 @@ exports.StorageHelper = StorageHelper;
698
679
  exports.SumBalancesFormatter = SumBalancesFormatter;
699
680
  exports.TalismanBalancesDatabase = TalismanBalancesDatabase;
700
681
  exports.balances = balances;
682
+ exports.createTypeRegistryCache = createTypeRegistryCache;
701
683
  exports.db = db;
702
684
  exports.excludeFromFeePayableLocks = excludeFromFeePayableLocks;
703
685
  exports.excludeFromTransferableAmount = excludeFromTransferableAmount;
@@ -6,17 +6,10 @@ var dexie = require('dexie');
6
6
  var types = require('@polkadot/types');
7
7
  var anylogger = require('anylogger');
8
8
  var util = require('@talismn/util');
9
- var memoize = require('lodash/memoize');
10
- var typescriptMemoize = require('typescript-memoize');
11
9
 
12
10
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
13
11
 
14
12
  var anylogger__default = /*#__PURE__*/_interopDefault(anylogger);
15
- var memoize__default = /*#__PURE__*/_interopDefault(memoize);
16
-
17
- //
18
- // exported
19
- //
20
13
 
21
14
  // TODO: Document default balances module purpose/usage
22
15
  const DefaultBalanceModule = type => ({
@@ -35,12 +28,15 @@ const DefaultBalanceModule = type => ({
35
28
  async fetchEvmChainTokens() {
36
29
  return Promise.resolve({});
37
30
  },
38
- async subscribeBalances(_chainConnectors, _chaindataProvider, _addressesByToken, callback) {
31
+ async subscribeBalances(_, callback) {
39
32
  callback(new Error("Balance subscriptions are not implemented in this module."));
40
33
  return () => {};
41
34
  },
42
35
  async fetchBalances() {
43
36
  throw new Error("Balance fetching is not implemented in this module.");
37
+ },
38
+ async transferToken() {
39
+ throw new Error("Token transfers are not implemented in this module.");
44
40
  }
45
41
  });
46
42
 
@@ -71,7 +67,7 @@ const db = new TalismanBalancesDatabase();
71
67
 
72
68
  var packageJson = {
73
69
  name: "@talismn/balances",
74
- version: "0.3.2",
70
+ version: "0.4.0",
75
71
  author: "Talisman",
76
72
  homepage: "https://talisman.xyz",
77
73
  license: "UNLICENSED",
@@ -90,7 +86,7 @@ var packageJson = {
90
86
  "/plugins"
91
87
  ],
92
88
  engines: {
93
- node: ">=14"
89
+ node: ">=18"
94
90
  },
95
91
  scripts: {
96
92
  test: "jest",
@@ -98,18 +94,16 @@ var packageJson = {
98
94
  clean: "rm -rf dist && rm -rf .turbo rm -rf node_modules"
99
95
  },
100
96
  dependencies: {
101
- "@polkadot/types": "9.10.5",
102
97
  "@talismn/chain-connector": "workspace:^",
103
98
  "@talismn/chain-connector-evm": "workspace:^",
104
99
  "@talismn/chaindata-provider": "workspace:^",
105
100
  "@talismn/token-rates": "workspace:^",
106
101
  "@talismn/util": "workspace:^",
107
102
  anylogger: "^1.0.11",
108
- dexie: "^3.2.2",
109
- lodash: "^4.17.21",
110
- "typescript-memoize": "^1.1.0"
103
+ dexie: "^3.2.3"
111
104
  },
112
105
  devDependencies: {
106
+ "@polkadot/types": "^9.10.5",
113
107
  "@talismn/eslint-config": "workspace:^",
114
108
  "@talismn/tsconfig": "workspace:^",
115
109
  "@types/jest": "^27.5.1",
@@ -118,6 +112,9 @@ var packageJson = {
118
112
  "ts-jest": "^28.0.2",
119
113
  typescript: "^4.6.4"
120
114
  },
115
+ peerDependencies: {
116
+ "@polkadot/types": "9.x"
117
+ },
121
118
  preconstruct: {
122
119
  entrypoints: [
123
120
  "index.ts",
@@ -134,15 +131,33 @@ var packageJson = {
134
131
 
135
132
  var log = anylogger__default["default"](packageJson.name);
136
133
 
137
- async function balances(balanceModule, chainConnectors, chaindataProvider, addressesByToken, callback) {
134
+ async function balances(balanceModule, addressesByToken, callback) {
138
135
  // subscription request
139
- if (callback !== undefined) return await balanceModule.subscribeBalances(chainConnectors, chaindataProvider, addressesByToken, callback);
136
+ if (callback !== undefined) return await balanceModule.subscribeBalances(addressesByToken, callback);
140
137
 
141
138
  // one-off request
142
- return await balanceModule.fetchBalances(chainConnectors, chaindataProvider, addressesByToken);
139
+ return await balanceModule.fetchBalances(addressesByToken);
143
140
  }
141
+ const createTypeRegistryCache = () => {
142
+ const typeRegistryCache = new Map();
143
+ const getOrCreateTypeRegistry = (chainId, metadataRpc) => {
144
+ // TODO: Delete cache when metadataRpc is different from last time
145
+ const cached = typeRegistryCache.get(chainId);
146
+ if (cached) return cached;
147
+ const typeRegistry = new types.TypeRegistry();
148
+ if (typeof metadataRpc === "string") {
149
+ const metadata = new types.Metadata(typeRegistry, metadataRpc);
150
+ metadata.registry.setMetadata(metadata);
151
+ }
152
+ typeRegistryCache.set(chainId, typeRegistry);
153
+ return typeRegistry;
154
+ };
155
+ return {
156
+ getOrCreateTypeRegistry
157
+ };
158
+ };
144
159
  const filterMirrorTokens = (balance, i, balances) => {
145
- // TODO implement a mirrorOf property, which should be set from chaindata
160
+ // TODO: implement a mirrorOf property, which should be set from chaindata
146
161
  const mirrorOf = balance.token?.mirrorOf;
147
162
  return !mirrorOf || !balances.find(b => b.tokenId === mirrorOf);
148
163
  };
@@ -228,30 +243,6 @@ class StorageHelper {
228
243
  }
229
244
  }
230
245
 
231
- function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
232
- var desc = {};
233
- Object.keys(descriptor).forEach(function (key) {
234
- desc[key] = descriptor[key];
235
- });
236
- desc.enumerable = !!desc.enumerable;
237
- desc.configurable = !!desc.configurable;
238
- if ('value' in desc || desc.initializer) {
239
- desc.writable = true;
240
- }
241
- desc = decorators.slice().reverse().reduce(function (desc, decorator) {
242
- return decorator(target, property, desc) || desc;
243
- }, desc);
244
- if (context && desc.initializer !== void 0) {
245
- desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
246
- desc.initializer = undefined;
247
- }
248
- if (desc.initializer === void 0) {
249
- Object.defineProperty(target, property, desc);
250
- desc = null;
251
- }
252
- return desc;
253
- }
254
-
255
246
  function excludeFromTransferableAmount(locks) {
256
247
  if (typeof locks === "string") return BigInt(locks);
257
248
  if (!Array.isArray(locks)) locks = [locks];
@@ -273,8 +264,6 @@ function includeInTotalExtraAmount(extra) {
273
264
 
274
265
  /** Used by plugins to help define their custom `BalanceType` */
275
266
 
276
- var _dec, _dec2, _dec3, _class, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _class2, _dec11, _class3, _dec12, _dec13, _dec14, _dec15, _dec16, _dec17, _dec18, _class4;
277
-
278
267
  /**
279
268
  * Have the importing library define its Token and BalanceJson enums (as a sum type of all plugins) and pass them into some
280
269
  * internal global typescript context, which is then picked up on by this module.
@@ -285,12 +274,12 @@ var _dec, _dec2, _dec3, _class, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10
285
274
  /**
286
275
  * A collection of balances.
287
276
  */
288
- let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Memoize(), _dec3 = typescriptMemoize.Memoize(), (_class = class Balances {
277
+ class Balances {
289
278
  //
290
279
  // Properties
291
280
  //
292
281
 
293
- #balances = {};
282
+ #balances = [];
294
283
 
295
284
  //
296
285
  // Methods
@@ -298,7 +287,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
298
287
 
299
288
  constructor(balances, hydrate) {
300
289
  // handle Balances (convert to Balance[])
301
- if (balances instanceof Balances) return new Balances([...balances], hydrate);
290
+ if (balances instanceof Balances) return new Balances(balances.each, hydrate);
302
291
 
303
292
  // handle Balance (convert to Balance[])
304
293
  if (balances instanceof Balance) return new Balances([balances], hydrate);
@@ -313,19 +302,19 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
313
302
  if (!util.isArrayOf(balances, Balance)) return new Balances(balances.map(storage => new Balance(storage)), hydrate);
314
303
 
315
304
  // handle Balance[]
316
- this.#balances = Object.fromEntries(balances.map(balance => [balance.id, balance]));
305
+ this.#balances = balances;
317
306
  if (hydrate !== undefined) this.hydrate(hydrate);
318
307
  }
319
308
 
320
309
  /**
321
310
  * Calling toJSON on a collection of balances will return the underlying BalanceJsonList.
322
311
  */
323
- toJSON = () => Object.fromEntries(Object.entries(this.#balances).map(([id, balance]) => {
312
+ toJSON = () => Object.fromEntries(this.#balances.map(balance => {
324
313
  try {
325
- return [id, balance.toJSON()];
314
+ return [balance.id, balance.toJSON()];
326
315
  } catch (error) {
327
316
  log.error("Failed to convert balance to JSON", error, {
328
- id,
317
+ id: balance.id,
329
318
  balance
330
319
  });
331
320
  return null;
@@ -345,7 +334,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
345
334
  */
346
335
  [Symbol.iterator] = () =>
347
336
  // Create an array of the balances in this collection and return the result of its iterator.
348
- Object.values(this.#balances)[Symbol.iterator]();
337
+ this.#balances[Symbol.iterator]();
349
338
 
350
339
  /**
351
340
  * Hydrates all balances in this collection.
@@ -353,7 +342,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
353
342
  * @param sources - The sources to hydrate from.
354
343
  */
355
344
  hydrate = sources => {
356
- Object.values(this.#balances).map(balance => balance.hydrate(sources));
345
+ this.#balances.map(balance => balance.hydrate(sources));
357
346
  };
358
347
 
359
348
  /**
@@ -362,7 +351,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
362
351
  * @param id - The id of the balance to fetch.
363
352
  * @returns The balance if one exists, or none.
364
353
  */
365
- get = id => this.#balances[id] || null;
354
+ get = id => this.#balances.find(balance => balance.id === id) ?? null;
366
355
 
367
356
  /**
368
357
  * Retrieve balances from this collection by search query.
@@ -395,10 +384,8 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
395
384
  if (balances instanceof Balance) return this.add(new Balances(balances));
396
385
 
397
386
  // merge balances
398
- const mergedBalances = {
399
- ...this.#balances
400
- };
401
- [...balances].forEach(balance => mergedBalances[balance.id] = balance);
387
+ const mergedBalances = Object.fromEntries(this.#balances.map(balance => [balance.id, balance]));
388
+ balances.each.forEach(balance => mergedBalances[balance.id] = balance);
402
389
 
403
390
  // return new balances
404
391
  return new Balances(Object.values(mergedBalances));
@@ -416,25 +403,23 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
416
403
  // handle single id
417
404
  if (!Array.isArray(ids)) return this.remove([ids]);
418
405
 
419
- // merge balances
420
- const removedBalances = {
421
- ...this.#balances
422
- };
423
- ids.forEach(id => delete removedBalances[id]);
424
-
425
- // return new balances
426
- return new Balances(Object.values(removedBalances));
406
+ // merge and return new balances
407
+ return new Balances(this.#balances.filter(balance => !ids.includes(balance.id)));
427
408
  };
428
409
 
429
410
  // TODO: Add some more useful aggregator methods
430
411
 
412
+ get each() {
413
+ return [...this];
414
+ }
415
+
431
416
  /**
432
417
  * Get an array of balances in this collection, sorted by chain sortIndex.
433
418
  *
434
419
  * @returns A sorted array of the balances in this collection.
435
420
  */
436
421
  get sorted() {
437
- return [...this].sort((a, b) => ((a.chain || a.evmNetwork)?.sortIndex || Number.MAX_SAFE_INTEGER) - ((b.chain || b.evmNetwork)?.sortIndex || Number.MAX_SAFE_INTEGER));
422
+ return [...this].sort((a, b) => ((a.chain || a.evmNetwork)?.sortIndex ?? Number.MAX_SAFE_INTEGER) - ((b.chain || b.evmNetwork)?.sortIndex ?? Number.MAX_SAFE_INTEGER));
438
423
  }
439
424
 
440
425
  /**
@@ -457,12 +442,12 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
457
442
  get sum() {
458
443
  return new SumBalancesFormatter(this);
459
444
  }
460
- }, (_applyDecoratedDescriptor(_class.prototype, "sorted", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "sorted"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "count", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "count"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "sum", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "sum"), _class.prototype)), _class));
445
+ }
461
446
 
462
447
  /**
463
448
  * An individual balance.
464
449
  */
465
- let Balance = (_dec4 = typescriptMemoize.Memoize(), _dec5 = typescriptMemoize.Memoize(), _dec6 = typescriptMemoize.Memoize(), _dec7 = typescriptMemoize.Memoize(), _dec8 = typescriptMemoize.Memoize(), _dec9 = typescriptMemoize.Memoize(), _dec10 = typescriptMemoize.Memoize(), (_class2 = class Balance {
450
+ class Balance {
466
451
  //
467
452
  // Properties
468
453
  //
@@ -476,7 +461,6 @@ let Balance = (_dec4 = typescriptMemoize.Memoize(), _dec5 = typescriptMemoize.Me
476
461
  //
477
462
 
478
463
  constructor(storage, hydrate) {
479
- this.#format = memoize__default["default"](this.#format);
480
464
  this.#storage = storage;
481
465
  if (hydrate !== undefined) this.hydrate(hydrate);
482
466
  }
@@ -599,8 +583,8 @@ let Balance = (_dec4 = typescriptMemoize.Memoize(), _dec5 = typescriptMemoize.Me
599
583
  // subtract the lock from the free amount (but don't go below 0)
600
584
  return this.#format(util.BigMath.max(this.free.planck - excludeAmount, BigInt("0")));
601
585
  }
602
- }, (_applyDecoratedDescriptor(_class2.prototype, "total", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "total"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "free", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "free"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "reserved", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "reserved"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "locked", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "locked"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "frozen", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "frozen"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "transferable", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "transferable"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "feePayable", [_dec10], Object.getOwnPropertyDescriptor(_class2.prototype, "feePayable"), _class2.prototype)), _class2));
603
- let BalanceFormatter = (_dec11 = typescriptMemoize.Memoize(), (_class3 = class BalanceFormatter {
586
+ }
587
+ class BalanceFormatter {
604
588
  #planck;
605
589
  #decimals;
606
590
  #fiatRatios;
@@ -608,7 +592,6 @@ let BalanceFormatter = (_dec11 = typescriptMemoize.Memoize(), (_class3 = class B
608
592
  this.#planck = typeof planck === "bigint" ? planck.toString() : planck ?? "0";
609
593
  this.#decimals = decimals || 0;
610
594
  this.#fiatRatios = fiatRatios || null;
611
- this.fiat = memoize__default["default"](this.fiat);
612
595
  }
613
596
  toJSON = () => this.#planck;
614
597
  get planck() {
@@ -623,14 +606,13 @@ let BalanceFormatter = (_dec11 = typescriptMemoize.Memoize(), (_class3 = class B
623
606
  if (!ratio) return null;
624
607
  return parseFloat(this.tokens) * ratio;
625
608
  }
626
- }, (_applyDecoratedDescriptor(_class3.prototype, "tokens", [_dec11], Object.getOwnPropertyDescriptor(_class3.prototype, "tokens"), _class3.prototype)), _class3));
627
- let FiatSumBalancesFormatter = (_dec12 = typescriptMemoize.Memoize(), _dec13 = typescriptMemoize.Memoize(), _dec14 = typescriptMemoize.Memoize(), _dec15 = typescriptMemoize.Memoize(), _dec16 = typescriptMemoize.Memoize(), _dec17 = typescriptMemoize.Memoize(), _dec18 = typescriptMemoize.Memoize(), (_class4 = class FiatSumBalancesFormatter {
609
+ }
610
+ class FiatSumBalancesFormatter {
628
611
  #balances;
629
612
  #currency;
630
613
  constructor(balances, currency) {
631
614
  this.#balances = balances;
632
615
  this.#currency = currency;
633
- this.#sum = memoize__default["default"](this.#sum);
634
616
  }
635
617
  #sum = balanceField => {
636
618
  // a function to get a fiat amount from a balance
@@ -677,12 +659,11 @@ let FiatSumBalancesFormatter = (_dec12 = typescriptMemoize.Memoize(), _dec13 = t
677
659
  get feePayable() {
678
660
  return this.#sum("feePayable");
679
661
  }
680
- }, (_applyDecoratedDescriptor(_class4.prototype, "total", [_dec12], Object.getOwnPropertyDescriptor(_class4.prototype, "total"), _class4.prototype), _applyDecoratedDescriptor(_class4.prototype, "free", [_dec13], Object.getOwnPropertyDescriptor(_class4.prototype, "free"), _class4.prototype), _applyDecoratedDescriptor(_class4.prototype, "reserved", [_dec14], Object.getOwnPropertyDescriptor(_class4.prototype, "reserved"), _class4.prototype), _applyDecoratedDescriptor(_class4.prototype, "locked", [_dec15], Object.getOwnPropertyDescriptor(_class4.prototype, "locked"), _class4.prototype), _applyDecoratedDescriptor(_class4.prototype, "frozen", [_dec16], Object.getOwnPropertyDescriptor(_class4.prototype, "frozen"), _class4.prototype), _applyDecoratedDescriptor(_class4.prototype, "transferable", [_dec17], Object.getOwnPropertyDescriptor(_class4.prototype, "transferable"), _class4.prototype), _applyDecoratedDescriptor(_class4.prototype, "feePayable", [_dec18], Object.getOwnPropertyDescriptor(_class4.prototype, "feePayable"), _class4.prototype)), _class4));
662
+ }
681
663
  class SumBalancesFormatter {
682
664
  #balances;
683
665
  constructor(balances) {
684
666
  this.#balances = balances;
685
- this.fiat = memoize__default["default"](this.fiat);
686
667
  }
687
668
  fiat(currency) {
688
669
  return new FiatSumBalancesFormatter(this.#balances, currency);
@@ -698,6 +679,7 @@ exports.StorageHelper = StorageHelper;
698
679
  exports.SumBalancesFormatter = SumBalancesFormatter;
699
680
  exports.TalismanBalancesDatabase = TalismanBalancesDatabase;
700
681
  exports.balances = balances;
682
+ exports.createTypeRegistryCache = createTypeRegistryCache;
701
683
  exports.db = db;
702
684
  exports.excludeFromFeePayableLocks = excludeFromFeePayableLocks;
703
685
  exports.excludeFromTransferableAmount = excludeFromTransferableAmount;
@@ -1,13 +1,7 @@
1
1
  import { Dexie } from 'dexie';
2
- import { decorateStorage, StorageKey } from '@polkadot/types';
2
+ import { decorateStorage, StorageKey, TypeRegistry, Metadata } from '@polkadot/types';
3
3
  import anylogger from 'anylogger';
4
4
  import { BigMath, isArrayOf, planckToTokens } from '@talismn/util';
5
- import memoize from 'lodash/memoize';
6
- import { Memoize } from 'typescript-memoize';
7
-
8
- //
9
- // exported
10
- //
11
5
 
12
6
  // TODO: Document default balances module purpose/usage
13
7
  const DefaultBalanceModule = type => ({
@@ -26,12 +20,15 @@ const DefaultBalanceModule = type => ({
26
20
  async fetchEvmChainTokens() {
27
21
  return Promise.resolve({});
28
22
  },
29
- async subscribeBalances(_chainConnectors, _chaindataProvider, _addressesByToken, callback) {
23
+ async subscribeBalances(_, callback) {
30
24
  callback(new Error("Balance subscriptions are not implemented in this module."));
31
25
  return () => {};
32
26
  },
33
27
  async fetchBalances() {
34
28
  throw new Error("Balance fetching is not implemented in this module.");
29
+ },
30
+ async transferToken() {
31
+ throw new Error("Token transfers are not implemented in this module.");
35
32
  }
36
33
  });
37
34
 
@@ -62,7 +59,7 @@ const db = new TalismanBalancesDatabase();
62
59
 
63
60
  var packageJson = {
64
61
  name: "@talismn/balances",
65
- version: "0.3.2",
62
+ version: "0.4.0",
66
63
  author: "Talisman",
67
64
  homepage: "https://talisman.xyz",
68
65
  license: "UNLICENSED",
@@ -81,7 +78,7 @@ var packageJson = {
81
78
  "/plugins"
82
79
  ],
83
80
  engines: {
84
- node: ">=14"
81
+ node: ">=18"
85
82
  },
86
83
  scripts: {
87
84
  test: "jest",
@@ -89,18 +86,16 @@ var packageJson = {
89
86
  clean: "rm -rf dist && rm -rf .turbo rm -rf node_modules"
90
87
  },
91
88
  dependencies: {
92
- "@polkadot/types": "9.10.5",
93
89
  "@talismn/chain-connector": "workspace:^",
94
90
  "@talismn/chain-connector-evm": "workspace:^",
95
91
  "@talismn/chaindata-provider": "workspace:^",
96
92
  "@talismn/token-rates": "workspace:^",
97
93
  "@talismn/util": "workspace:^",
98
94
  anylogger: "^1.0.11",
99
- dexie: "^3.2.2",
100
- lodash: "^4.17.21",
101
- "typescript-memoize": "^1.1.0"
95
+ dexie: "^3.2.3"
102
96
  },
103
97
  devDependencies: {
98
+ "@polkadot/types": "^9.10.5",
104
99
  "@talismn/eslint-config": "workspace:^",
105
100
  "@talismn/tsconfig": "workspace:^",
106
101
  "@types/jest": "^27.5.1",
@@ -109,6 +104,9 @@ var packageJson = {
109
104
  "ts-jest": "^28.0.2",
110
105
  typescript: "^4.6.4"
111
106
  },
107
+ peerDependencies: {
108
+ "@polkadot/types": "9.x"
109
+ },
112
110
  preconstruct: {
113
111
  entrypoints: [
114
112
  "index.ts",
@@ -125,15 +123,33 @@ var packageJson = {
125
123
 
126
124
  var log = anylogger(packageJson.name);
127
125
 
128
- async function balances(balanceModule, chainConnectors, chaindataProvider, addressesByToken, callback) {
126
+ async function balances(balanceModule, addressesByToken, callback) {
129
127
  // subscription request
130
- if (callback !== undefined) return await balanceModule.subscribeBalances(chainConnectors, chaindataProvider, addressesByToken, callback);
128
+ if (callback !== undefined) return await balanceModule.subscribeBalances(addressesByToken, callback);
131
129
 
132
130
  // one-off request
133
- return await balanceModule.fetchBalances(chainConnectors, chaindataProvider, addressesByToken);
131
+ return await balanceModule.fetchBalances(addressesByToken);
134
132
  }
133
+ const createTypeRegistryCache = () => {
134
+ const typeRegistryCache = new Map();
135
+ const getOrCreateTypeRegistry = (chainId, metadataRpc) => {
136
+ // TODO: Delete cache when metadataRpc is different from last time
137
+ const cached = typeRegistryCache.get(chainId);
138
+ if (cached) return cached;
139
+ const typeRegistry = new TypeRegistry();
140
+ if (typeof metadataRpc === "string") {
141
+ const metadata = new Metadata(typeRegistry, metadataRpc);
142
+ metadata.registry.setMetadata(metadata);
143
+ }
144
+ typeRegistryCache.set(chainId, typeRegistry);
145
+ return typeRegistry;
146
+ };
147
+ return {
148
+ getOrCreateTypeRegistry
149
+ };
150
+ };
135
151
  const filterMirrorTokens = (balance, i, balances) => {
136
- // TODO implement a mirrorOf property, which should be set from chaindata
152
+ // TODO: implement a mirrorOf property, which should be set from chaindata
137
153
  const mirrorOf = balance.token?.mirrorOf;
138
154
  return !mirrorOf || !balances.find(b => b.tokenId === mirrorOf);
139
155
  };
@@ -219,30 +235,6 @@ class StorageHelper {
219
235
  }
220
236
  }
221
237
 
222
- function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
223
- var desc = {};
224
- Object.keys(descriptor).forEach(function (key) {
225
- desc[key] = descriptor[key];
226
- });
227
- desc.enumerable = !!desc.enumerable;
228
- desc.configurable = !!desc.configurable;
229
- if ('value' in desc || desc.initializer) {
230
- desc.writable = true;
231
- }
232
- desc = decorators.slice().reverse().reduce(function (desc, decorator) {
233
- return decorator(target, property, desc) || desc;
234
- }, desc);
235
- if (context && desc.initializer !== void 0) {
236
- desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
237
- desc.initializer = undefined;
238
- }
239
- if (desc.initializer === void 0) {
240
- Object.defineProperty(target, property, desc);
241
- desc = null;
242
- }
243
- return desc;
244
- }
245
-
246
238
  function excludeFromTransferableAmount(locks) {
247
239
  if (typeof locks === "string") return BigInt(locks);
248
240
  if (!Array.isArray(locks)) locks = [locks];
@@ -264,8 +256,6 @@ function includeInTotalExtraAmount(extra) {
264
256
 
265
257
  /** Used by plugins to help define their custom `BalanceType` */
266
258
 
267
- var _dec, _dec2, _dec3, _class, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _class2, _dec11, _class3, _dec12, _dec13, _dec14, _dec15, _dec16, _dec17, _dec18, _class4;
268
-
269
259
  /**
270
260
  * Have the importing library define its Token and BalanceJson enums (as a sum type of all plugins) and pass them into some
271
261
  * internal global typescript context, which is then picked up on by this module.
@@ -276,12 +266,12 @@ var _dec, _dec2, _dec3, _class, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10
276
266
  /**
277
267
  * A collection of balances.
278
268
  */
279
- let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class = class Balances {
269
+ class Balances {
280
270
  //
281
271
  // Properties
282
272
  //
283
273
 
284
- #balances = {};
274
+ #balances = [];
285
275
 
286
276
  //
287
277
  // Methods
@@ -289,7 +279,7 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
289
279
 
290
280
  constructor(balances, hydrate) {
291
281
  // handle Balances (convert to Balance[])
292
- if (balances instanceof Balances) return new Balances([...balances], hydrate);
282
+ if (balances instanceof Balances) return new Balances(balances.each, hydrate);
293
283
 
294
284
  // handle Balance (convert to Balance[])
295
285
  if (balances instanceof Balance) return new Balances([balances], hydrate);
@@ -304,19 +294,19 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
304
294
  if (!isArrayOf(balances, Balance)) return new Balances(balances.map(storage => new Balance(storage)), hydrate);
305
295
 
306
296
  // handle Balance[]
307
- this.#balances = Object.fromEntries(balances.map(balance => [balance.id, balance]));
297
+ this.#balances = balances;
308
298
  if (hydrate !== undefined) this.hydrate(hydrate);
309
299
  }
310
300
 
311
301
  /**
312
302
  * Calling toJSON on a collection of balances will return the underlying BalanceJsonList.
313
303
  */
314
- toJSON = () => Object.fromEntries(Object.entries(this.#balances).map(([id, balance]) => {
304
+ toJSON = () => Object.fromEntries(this.#balances.map(balance => {
315
305
  try {
316
- return [id, balance.toJSON()];
306
+ return [balance.id, balance.toJSON()];
317
307
  } catch (error) {
318
308
  log.error("Failed to convert balance to JSON", error, {
319
- id,
309
+ id: balance.id,
320
310
  balance
321
311
  });
322
312
  return null;
@@ -336,7 +326,7 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
336
326
  */
337
327
  [Symbol.iterator] = () =>
338
328
  // Create an array of the balances in this collection and return the result of its iterator.
339
- Object.values(this.#balances)[Symbol.iterator]();
329
+ this.#balances[Symbol.iterator]();
340
330
 
341
331
  /**
342
332
  * Hydrates all balances in this collection.
@@ -344,7 +334,7 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
344
334
  * @param sources - The sources to hydrate from.
345
335
  */
346
336
  hydrate = sources => {
347
- Object.values(this.#balances).map(balance => balance.hydrate(sources));
337
+ this.#balances.map(balance => balance.hydrate(sources));
348
338
  };
349
339
 
350
340
  /**
@@ -353,7 +343,7 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
353
343
  * @param id - The id of the balance to fetch.
354
344
  * @returns The balance if one exists, or none.
355
345
  */
356
- get = id => this.#balances[id] || null;
346
+ get = id => this.#balances.find(balance => balance.id === id) ?? null;
357
347
 
358
348
  /**
359
349
  * Retrieve balances from this collection by search query.
@@ -386,10 +376,8 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
386
376
  if (balances instanceof Balance) return this.add(new Balances(balances));
387
377
 
388
378
  // merge balances
389
- const mergedBalances = {
390
- ...this.#balances
391
- };
392
- [...balances].forEach(balance => mergedBalances[balance.id] = balance);
379
+ const mergedBalances = Object.fromEntries(this.#balances.map(balance => [balance.id, balance]));
380
+ balances.each.forEach(balance => mergedBalances[balance.id] = balance);
393
381
 
394
382
  // return new balances
395
383
  return new Balances(Object.values(mergedBalances));
@@ -407,25 +395,23 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
407
395
  // handle single id
408
396
  if (!Array.isArray(ids)) return this.remove([ids]);
409
397
 
410
- // merge balances
411
- const removedBalances = {
412
- ...this.#balances
413
- };
414
- ids.forEach(id => delete removedBalances[id]);
415
-
416
- // return new balances
417
- return new Balances(Object.values(removedBalances));
398
+ // merge and return new balances
399
+ return new Balances(this.#balances.filter(balance => !ids.includes(balance.id)));
418
400
  };
419
401
 
420
402
  // TODO: Add some more useful aggregator methods
421
403
 
404
+ get each() {
405
+ return [...this];
406
+ }
407
+
422
408
  /**
423
409
  * Get an array of balances in this collection, sorted by chain sortIndex.
424
410
  *
425
411
  * @returns A sorted array of the balances in this collection.
426
412
  */
427
413
  get sorted() {
428
- return [...this].sort((a, b) => ((a.chain || a.evmNetwork)?.sortIndex || Number.MAX_SAFE_INTEGER) - ((b.chain || b.evmNetwork)?.sortIndex || Number.MAX_SAFE_INTEGER));
414
+ return [...this].sort((a, b) => ((a.chain || a.evmNetwork)?.sortIndex ?? Number.MAX_SAFE_INTEGER) - ((b.chain || b.evmNetwork)?.sortIndex ?? Number.MAX_SAFE_INTEGER));
429
415
  }
430
416
 
431
417
  /**
@@ -448,12 +434,12 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
448
434
  get sum() {
449
435
  return new SumBalancesFormatter(this);
450
436
  }
451
- }, (_applyDecoratedDescriptor(_class.prototype, "sorted", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "sorted"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "count", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "count"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "sum", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "sum"), _class.prototype)), _class));
437
+ }
452
438
 
453
439
  /**
454
440
  * An individual balance.
455
441
  */
456
- let Balance = (_dec4 = Memoize(), _dec5 = Memoize(), _dec6 = Memoize(), _dec7 = Memoize(), _dec8 = Memoize(), _dec9 = Memoize(), _dec10 = Memoize(), (_class2 = class Balance {
442
+ class Balance {
457
443
  //
458
444
  // Properties
459
445
  //
@@ -467,7 +453,6 @@ let Balance = (_dec4 = Memoize(), _dec5 = Memoize(), _dec6 = Memoize(), _dec7 =
467
453
  //
468
454
 
469
455
  constructor(storage, hydrate) {
470
- this.#format = memoize(this.#format);
471
456
  this.#storage = storage;
472
457
  if (hydrate !== undefined) this.hydrate(hydrate);
473
458
  }
@@ -590,8 +575,8 @@ let Balance = (_dec4 = Memoize(), _dec5 = Memoize(), _dec6 = Memoize(), _dec7 =
590
575
  // subtract the lock from the free amount (but don't go below 0)
591
576
  return this.#format(BigMath.max(this.free.planck - excludeAmount, BigInt("0")));
592
577
  }
593
- }, (_applyDecoratedDescriptor(_class2.prototype, "total", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "total"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "free", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "free"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "reserved", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "reserved"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "locked", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "locked"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "frozen", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "frozen"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "transferable", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "transferable"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "feePayable", [_dec10], Object.getOwnPropertyDescriptor(_class2.prototype, "feePayable"), _class2.prototype)), _class2));
594
- let BalanceFormatter = (_dec11 = Memoize(), (_class3 = class BalanceFormatter {
578
+ }
579
+ class BalanceFormatter {
595
580
  #planck;
596
581
  #decimals;
597
582
  #fiatRatios;
@@ -599,7 +584,6 @@ let BalanceFormatter = (_dec11 = Memoize(), (_class3 = class BalanceFormatter {
599
584
  this.#planck = typeof planck === "bigint" ? planck.toString() : planck ?? "0";
600
585
  this.#decimals = decimals || 0;
601
586
  this.#fiatRatios = fiatRatios || null;
602
- this.fiat = memoize(this.fiat);
603
587
  }
604
588
  toJSON = () => this.#planck;
605
589
  get planck() {
@@ -614,14 +598,13 @@ let BalanceFormatter = (_dec11 = Memoize(), (_class3 = class BalanceFormatter {
614
598
  if (!ratio) return null;
615
599
  return parseFloat(this.tokens) * ratio;
616
600
  }
617
- }, (_applyDecoratedDescriptor(_class3.prototype, "tokens", [_dec11], Object.getOwnPropertyDescriptor(_class3.prototype, "tokens"), _class3.prototype)), _class3));
618
- let FiatSumBalancesFormatter = (_dec12 = Memoize(), _dec13 = Memoize(), _dec14 = Memoize(), _dec15 = Memoize(), _dec16 = Memoize(), _dec17 = Memoize(), _dec18 = Memoize(), (_class4 = class FiatSumBalancesFormatter {
601
+ }
602
+ class FiatSumBalancesFormatter {
619
603
  #balances;
620
604
  #currency;
621
605
  constructor(balances, currency) {
622
606
  this.#balances = balances;
623
607
  this.#currency = currency;
624
- this.#sum = memoize(this.#sum);
625
608
  }
626
609
  #sum = balanceField => {
627
610
  // a function to get a fiat amount from a balance
@@ -668,16 +651,15 @@ let FiatSumBalancesFormatter = (_dec12 = Memoize(), _dec13 = Memoize(), _dec14 =
668
651
  get feePayable() {
669
652
  return this.#sum("feePayable");
670
653
  }
671
- }, (_applyDecoratedDescriptor(_class4.prototype, "total", [_dec12], Object.getOwnPropertyDescriptor(_class4.prototype, "total"), _class4.prototype), _applyDecoratedDescriptor(_class4.prototype, "free", [_dec13], Object.getOwnPropertyDescriptor(_class4.prototype, "free"), _class4.prototype), _applyDecoratedDescriptor(_class4.prototype, "reserved", [_dec14], Object.getOwnPropertyDescriptor(_class4.prototype, "reserved"), _class4.prototype), _applyDecoratedDescriptor(_class4.prototype, "locked", [_dec15], Object.getOwnPropertyDescriptor(_class4.prototype, "locked"), _class4.prototype), _applyDecoratedDescriptor(_class4.prototype, "frozen", [_dec16], Object.getOwnPropertyDescriptor(_class4.prototype, "frozen"), _class4.prototype), _applyDecoratedDescriptor(_class4.prototype, "transferable", [_dec17], Object.getOwnPropertyDescriptor(_class4.prototype, "transferable"), _class4.prototype), _applyDecoratedDescriptor(_class4.prototype, "feePayable", [_dec18], Object.getOwnPropertyDescriptor(_class4.prototype, "feePayable"), _class4.prototype)), _class4));
654
+ }
672
655
  class SumBalancesFormatter {
673
656
  #balances;
674
657
  constructor(balances) {
675
658
  this.#balances = balances;
676
- this.fiat = memoize(this.fiat);
677
659
  }
678
660
  fiat(currency) {
679
661
  return new FiatSumBalancesFormatter(this.#balances, currency);
680
662
  }
681
663
  }
682
664
 
683
- export { Balance, BalanceFormatter, Balances, DefaultBalanceModule, FiatSumBalancesFormatter, StorageHelper, SumBalancesFormatter, TalismanBalancesDatabase, balances, db, excludeFromFeePayableLocks, excludeFromTransferableAmount, filterMirrorTokens, includeInTotalExtraAmount };
665
+ export { Balance, BalanceFormatter, Balances, DefaultBalanceModule, FiatSumBalancesFormatter, StorageHelper, SumBalancesFormatter, TalismanBalancesDatabase, balances, createTypeRegistryCache, db, excludeFromFeePayableLocks, excludeFromTransferableAmount, filterMirrorTokens, includeInTotalExtraAmount };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@talismn/balances",
3
- "version": "0.3.2",
3
+ "version": "0.4.0",
4
4
  "author": "Talisman",
5
5
  "homepage": "https://talisman.xyz",
6
6
  "license": "UNLICENSED",
@@ -19,7 +19,7 @@
19
19
  "/plugins"
20
20
  ],
21
21
  "engines": {
22
- "node": ">=14"
22
+ "node": ">=18"
23
23
  },
24
24
  "scripts": {
25
25
  "test": "jest",
@@ -27,18 +27,16 @@
27
27
  "clean": "rm -rf dist && rm -rf .turbo rm -rf node_modules"
28
28
  },
29
29
  "dependencies": {
30
- "@polkadot/types": "9.10.5",
31
- "@talismn/chain-connector": "^0.4.1",
32
- "@talismn/chain-connector-evm": "^0.4.1",
33
- "@talismn/chaindata-provider": "^0.4.1",
34
- "@talismn/token-rates": "^0.1.13",
35
- "@talismn/util": "^0.1.6",
30
+ "@talismn/chain-connector": "^0.4.3",
31
+ "@talismn/chain-connector-evm": "^0.4.3",
32
+ "@talismn/chaindata-provider": "^0.4.3",
33
+ "@talismn/token-rates": "^0.1.15",
34
+ "@talismn/util": "^0.1.8",
36
35
  "anylogger": "^1.0.11",
37
- "dexie": "^3.2.2",
38
- "lodash": "^4.17.21",
39
- "typescript-memoize": "^1.1.0"
36
+ "dexie": "^3.2.3"
40
37
  },
41
38
  "devDependencies": {
39
+ "@polkadot/types": "^9.10.5",
42
40
  "@talismn/eslint-config": "^0.0.1",
43
41
  "@talismn/tsconfig": "^0.0.2",
44
42
  "@types/jest": "^27.5.1",
@@ -47,6 +45,9 @@
47
45
  "ts-jest": "^28.0.2",
48
46
  "typescript": "^4.6.4"
49
47
  },
48
+ "peerDependencies": {
49
+ "@polkadot/types": "9.x"
50
+ },
50
51
  "preconstruct": {
51
52
  "entrypoints": [
52
53
  "index.ts",