@talismn/balances 0.3.3 → 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 +17 -0
- package/dist/declarations/src/BalanceModule.d.ts +41 -19
- package/dist/declarations/src/helpers.d.ts +9 -14
- package/dist/declarations/src/types/balances.d.ts +1 -0
- package/dist/declarations/src/types/balancetypes.d.ts +1 -1
- package/dist/talismn-balances.cjs.dev.js +56 -77
- package/dist/talismn-balances.cjs.prod.js +56 -77
- package/dist/talismn-balances.esm.js +57 -78
- package/package.json +8 -10
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,22 @@
|
|
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
|
+
|
3
20
|
## 0.3.3
|
4
21
|
|
5
22
|
### Patch Changes
|
@@ -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
|
-
|
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(
|
42
|
+
fetchSubstrateChainMeta(chainId: ChainId, moduleConfig?: TModuleConfig): Promise<TChainMeta | null>;
|
16
43
|
/** Detects which tokens are available on a given substrate chain */
|
17
|
-
fetchSubstrateChainTokens(
|
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(
|
48
|
+
fetchEvmChainMeta(chainId: ChainId, moduleConfig?: TModuleConfig): Promise<TChainMeta | null>;
|
22
49
|
/** Detects which tokens are available on a given evm chain */
|
23
|
-
fetchEvmChainTokens(
|
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(
|
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(
|
38
|
-
|
39
|
-
|
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 {
|
2
|
-
import {
|
3
|
-
import {
|
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>,
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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:
|
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;
|
@@ -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(
|
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.
|
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: ">=
|
89
|
+
node: ">=18"
|
94
90
|
},
|
95
91
|
scripts: {
|
96
92
|
test: "jest",
|
@@ -104,9 +100,7 @@ var packageJson = {
|
|
104
100
|
"@talismn/token-rates": "workspace:^",
|
105
101
|
"@talismn/util": "workspace:^",
|
106
102
|
anylogger: "^1.0.11",
|
107
|
-
dexie: "^3.2.
|
108
|
-
lodash: "^4.17.21",
|
109
|
-
"typescript-memoize": "^1.1.0"
|
103
|
+
dexie: "^3.2.3"
|
110
104
|
},
|
111
105
|
devDependencies: {
|
112
106
|
"@polkadot/types": "^9.10.5",
|
@@ -137,15 +131,33 @@ var packageJson = {
|
|
137
131
|
|
138
132
|
var log = anylogger__default["default"](packageJson.name);
|
139
133
|
|
140
|
-
async function balances(balanceModule,
|
134
|
+
async function balances(balanceModule, addressesByToken, callback) {
|
141
135
|
// subscription request
|
142
|
-
if (callback !== undefined) return await balanceModule.subscribeBalances(
|
136
|
+
if (callback !== undefined) return await balanceModule.subscribeBalances(addressesByToken, callback);
|
143
137
|
|
144
138
|
// one-off request
|
145
|
-
return await balanceModule.fetchBalances(
|
139
|
+
return await balanceModule.fetchBalances(addressesByToken);
|
146
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
|
+
};
|
147
159
|
const filterMirrorTokens = (balance, i, balances) => {
|
148
|
-
// TODO implement a mirrorOf property, which should be set from chaindata
|
160
|
+
// TODO: implement a mirrorOf property, which should be set from chaindata
|
149
161
|
const mirrorOf = balance.token?.mirrorOf;
|
150
162
|
return !mirrorOf || !balances.find(b => b.tokenId === mirrorOf);
|
151
163
|
};
|
@@ -231,30 +243,6 @@ class StorageHelper {
|
|
231
243
|
}
|
232
244
|
}
|
233
245
|
|
234
|
-
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
|
235
|
-
var desc = {};
|
236
|
-
Object.keys(descriptor).forEach(function (key) {
|
237
|
-
desc[key] = descriptor[key];
|
238
|
-
});
|
239
|
-
desc.enumerable = !!desc.enumerable;
|
240
|
-
desc.configurable = !!desc.configurable;
|
241
|
-
if ('value' in desc || desc.initializer) {
|
242
|
-
desc.writable = true;
|
243
|
-
}
|
244
|
-
desc = decorators.slice().reverse().reduce(function (desc, decorator) {
|
245
|
-
return decorator(target, property, desc) || desc;
|
246
|
-
}, desc);
|
247
|
-
if (context && desc.initializer !== void 0) {
|
248
|
-
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
|
249
|
-
desc.initializer = undefined;
|
250
|
-
}
|
251
|
-
if (desc.initializer === void 0) {
|
252
|
-
Object.defineProperty(target, property, desc);
|
253
|
-
desc = null;
|
254
|
-
}
|
255
|
-
return desc;
|
256
|
-
}
|
257
|
-
|
258
246
|
function excludeFromTransferableAmount(locks) {
|
259
247
|
if (typeof locks === "string") return BigInt(locks);
|
260
248
|
if (!Array.isArray(locks)) locks = [locks];
|
@@ -276,8 +264,6 @@ function includeInTotalExtraAmount(extra) {
|
|
276
264
|
|
277
265
|
/** Used by plugins to help define their custom `BalanceType` */
|
278
266
|
|
279
|
-
var _dec, _dec2, _dec3, _class, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _class2, _dec11, _class3, _dec12, _dec13, _dec14, _dec15, _dec16, _dec17, _dec18, _class4;
|
280
|
-
|
281
267
|
/**
|
282
268
|
* Have the importing library define its Token and BalanceJson enums (as a sum type of all plugins) and pass them into some
|
283
269
|
* internal global typescript context, which is then picked up on by this module.
|
@@ -288,12 +274,12 @@ var _dec, _dec2, _dec3, _class, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10
|
|
288
274
|
/**
|
289
275
|
* A collection of balances.
|
290
276
|
*/
|
291
|
-
|
277
|
+
class Balances {
|
292
278
|
//
|
293
279
|
// Properties
|
294
280
|
//
|
295
281
|
|
296
|
-
#balances =
|
282
|
+
#balances = [];
|
297
283
|
|
298
284
|
//
|
299
285
|
// Methods
|
@@ -301,7 +287,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
301
287
|
|
302
288
|
constructor(balances, hydrate) {
|
303
289
|
// handle Balances (convert to Balance[])
|
304
|
-
if (balances instanceof Balances) return new Balances(
|
290
|
+
if (balances instanceof Balances) return new Balances(balances.each, hydrate);
|
305
291
|
|
306
292
|
// handle Balance (convert to Balance[])
|
307
293
|
if (balances instanceof Balance) return new Balances([balances], hydrate);
|
@@ -316,19 +302,19 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
316
302
|
if (!util.isArrayOf(balances, Balance)) return new Balances(balances.map(storage => new Balance(storage)), hydrate);
|
317
303
|
|
318
304
|
// handle Balance[]
|
319
|
-
this.#balances =
|
305
|
+
this.#balances = balances;
|
320
306
|
if (hydrate !== undefined) this.hydrate(hydrate);
|
321
307
|
}
|
322
308
|
|
323
309
|
/**
|
324
310
|
* Calling toJSON on a collection of balances will return the underlying BalanceJsonList.
|
325
311
|
*/
|
326
|
-
toJSON = () => Object.fromEntries(
|
312
|
+
toJSON = () => Object.fromEntries(this.#balances.map(balance => {
|
327
313
|
try {
|
328
|
-
return [id, balance.toJSON()];
|
314
|
+
return [balance.id, balance.toJSON()];
|
329
315
|
} catch (error) {
|
330
316
|
log.error("Failed to convert balance to JSON", error, {
|
331
|
-
id,
|
317
|
+
id: balance.id,
|
332
318
|
balance
|
333
319
|
});
|
334
320
|
return null;
|
@@ -348,7 +334,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
348
334
|
*/
|
349
335
|
[Symbol.iterator] = () =>
|
350
336
|
// Create an array of the balances in this collection and return the result of its iterator.
|
351
|
-
|
337
|
+
this.#balances[Symbol.iterator]();
|
352
338
|
|
353
339
|
/**
|
354
340
|
* Hydrates all balances in this collection.
|
@@ -356,7 +342,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
356
342
|
* @param sources - The sources to hydrate from.
|
357
343
|
*/
|
358
344
|
hydrate = sources => {
|
359
|
-
|
345
|
+
this.#balances.map(balance => balance.hydrate(sources));
|
360
346
|
};
|
361
347
|
|
362
348
|
/**
|
@@ -365,7 +351,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
365
351
|
* @param id - The id of the balance to fetch.
|
366
352
|
* @returns The balance if one exists, or none.
|
367
353
|
*/
|
368
|
-
get = id => this.#balances
|
354
|
+
get = id => this.#balances.find(balance => balance.id === id) ?? null;
|
369
355
|
|
370
356
|
/**
|
371
357
|
* Retrieve balances from this collection by search query.
|
@@ -398,10 +384,8 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
398
384
|
if (balances instanceof Balance) return this.add(new Balances(balances));
|
399
385
|
|
400
386
|
// merge balances
|
401
|
-
const mergedBalances =
|
402
|
-
|
403
|
-
};
|
404
|
-
[...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);
|
405
389
|
|
406
390
|
// return new balances
|
407
391
|
return new Balances(Object.values(mergedBalances));
|
@@ -419,25 +403,23 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
419
403
|
// handle single id
|
420
404
|
if (!Array.isArray(ids)) return this.remove([ids]);
|
421
405
|
|
422
|
-
// merge balances
|
423
|
-
|
424
|
-
...this.#balances
|
425
|
-
};
|
426
|
-
ids.forEach(id => delete removedBalances[id]);
|
427
|
-
|
428
|
-
// return new balances
|
429
|
-
return new Balances(Object.values(removedBalances));
|
406
|
+
// merge and return new balances
|
407
|
+
return new Balances(this.#balances.filter(balance => !ids.includes(balance.id)));
|
430
408
|
};
|
431
409
|
|
432
410
|
// TODO: Add some more useful aggregator methods
|
433
411
|
|
412
|
+
get each() {
|
413
|
+
return [...this];
|
414
|
+
}
|
415
|
+
|
434
416
|
/**
|
435
417
|
* Get an array of balances in this collection, sorted by chain sortIndex.
|
436
418
|
*
|
437
419
|
* @returns A sorted array of the balances in this collection.
|
438
420
|
*/
|
439
421
|
get sorted() {
|
440
|
-
return [...this].sort((a, b) => ((a.chain || a.evmNetwork)?.sortIndex
|
422
|
+
return [...this].sort((a, b) => ((a.chain || a.evmNetwork)?.sortIndex ?? Number.MAX_SAFE_INTEGER) - ((b.chain || b.evmNetwork)?.sortIndex ?? Number.MAX_SAFE_INTEGER));
|
441
423
|
}
|
442
424
|
|
443
425
|
/**
|
@@ -460,12 +442,12 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
460
442
|
get sum() {
|
461
443
|
return new SumBalancesFormatter(this);
|
462
444
|
}
|
463
|
-
}
|
445
|
+
}
|
464
446
|
|
465
447
|
/**
|
466
448
|
* An individual balance.
|
467
449
|
*/
|
468
|
-
|
450
|
+
class Balance {
|
469
451
|
//
|
470
452
|
// Properties
|
471
453
|
//
|
@@ -479,7 +461,6 @@ let Balance = (_dec4 = typescriptMemoize.Memoize(), _dec5 = typescriptMemoize.Me
|
|
479
461
|
//
|
480
462
|
|
481
463
|
constructor(storage, hydrate) {
|
482
|
-
this.#format = memoize__default["default"](this.#format);
|
483
464
|
this.#storage = storage;
|
484
465
|
if (hydrate !== undefined) this.hydrate(hydrate);
|
485
466
|
}
|
@@ -602,8 +583,8 @@ let Balance = (_dec4 = typescriptMemoize.Memoize(), _dec5 = typescriptMemoize.Me
|
|
602
583
|
// subtract the lock from the free amount (but don't go below 0)
|
603
584
|
return this.#format(util.BigMath.max(this.free.planck - excludeAmount, BigInt("0")));
|
604
585
|
}
|
605
|
-
}
|
606
|
-
|
586
|
+
}
|
587
|
+
class BalanceFormatter {
|
607
588
|
#planck;
|
608
589
|
#decimals;
|
609
590
|
#fiatRatios;
|
@@ -611,7 +592,6 @@ let BalanceFormatter = (_dec11 = typescriptMemoize.Memoize(), (_class3 = class B
|
|
611
592
|
this.#planck = typeof planck === "bigint" ? planck.toString() : planck ?? "0";
|
612
593
|
this.#decimals = decimals || 0;
|
613
594
|
this.#fiatRatios = fiatRatios || null;
|
614
|
-
this.fiat = memoize__default["default"](this.fiat);
|
615
595
|
}
|
616
596
|
toJSON = () => this.#planck;
|
617
597
|
get planck() {
|
@@ -626,14 +606,13 @@ let BalanceFormatter = (_dec11 = typescriptMemoize.Memoize(), (_class3 = class B
|
|
626
606
|
if (!ratio) return null;
|
627
607
|
return parseFloat(this.tokens) * ratio;
|
628
608
|
}
|
629
|
-
}
|
630
|
-
|
609
|
+
}
|
610
|
+
class FiatSumBalancesFormatter {
|
631
611
|
#balances;
|
632
612
|
#currency;
|
633
613
|
constructor(balances, currency) {
|
634
614
|
this.#balances = balances;
|
635
615
|
this.#currency = currency;
|
636
|
-
this.#sum = memoize__default["default"](this.#sum);
|
637
616
|
}
|
638
617
|
#sum = balanceField => {
|
639
618
|
// a function to get a fiat amount from a balance
|
@@ -680,12 +659,11 @@ let FiatSumBalancesFormatter = (_dec12 = typescriptMemoize.Memoize(), _dec13 = t
|
|
680
659
|
get feePayable() {
|
681
660
|
return this.#sum("feePayable");
|
682
661
|
}
|
683
|
-
}
|
662
|
+
}
|
684
663
|
class SumBalancesFormatter {
|
685
664
|
#balances;
|
686
665
|
constructor(balances) {
|
687
666
|
this.#balances = balances;
|
688
|
-
this.fiat = memoize__default["default"](this.fiat);
|
689
667
|
}
|
690
668
|
fiat(currency) {
|
691
669
|
return new FiatSumBalancesFormatter(this.#balances, currency);
|
@@ -701,6 +679,7 @@ exports.StorageHelper = StorageHelper;
|
|
701
679
|
exports.SumBalancesFormatter = SumBalancesFormatter;
|
702
680
|
exports.TalismanBalancesDatabase = TalismanBalancesDatabase;
|
703
681
|
exports.balances = balances;
|
682
|
+
exports.createTypeRegistryCache = createTypeRegistryCache;
|
704
683
|
exports.db = db;
|
705
684
|
exports.excludeFromFeePayableLocks = excludeFromFeePayableLocks;
|
706
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(
|
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.
|
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: ">=
|
89
|
+
node: ">=18"
|
94
90
|
},
|
95
91
|
scripts: {
|
96
92
|
test: "jest",
|
@@ -104,9 +100,7 @@ var packageJson = {
|
|
104
100
|
"@talismn/token-rates": "workspace:^",
|
105
101
|
"@talismn/util": "workspace:^",
|
106
102
|
anylogger: "^1.0.11",
|
107
|
-
dexie: "^3.2.
|
108
|
-
lodash: "^4.17.21",
|
109
|
-
"typescript-memoize": "^1.1.0"
|
103
|
+
dexie: "^3.2.3"
|
110
104
|
},
|
111
105
|
devDependencies: {
|
112
106
|
"@polkadot/types": "^9.10.5",
|
@@ -137,15 +131,33 @@ var packageJson = {
|
|
137
131
|
|
138
132
|
var log = anylogger__default["default"](packageJson.name);
|
139
133
|
|
140
|
-
async function balances(balanceModule,
|
134
|
+
async function balances(balanceModule, addressesByToken, callback) {
|
141
135
|
// subscription request
|
142
|
-
if (callback !== undefined) return await balanceModule.subscribeBalances(
|
136
|
+
if (callback !== undefined) return await balanceModule.subscribeBalances(addressesByToken, callback);
|
143
137
|
|
144
138
|
// one-off request
|
145
|
-
return await balanceModule.fetchBalances(
|
139
|
+
return await balanceModule.fetchBalances(addressesByToken);
|
146
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
|
+
};
|
147
159
|
const filterMirrorTokens = (balance, i, balances) => {
|
148
|
-
// TODO implement a mirrorOf property, which should be set from chaindata
|
160
|
+
// TODO: implement a mirrorOf property, which should be set from chaindata
|
149
161
|
const mirrorOf = balance.token?.mirrorOf;
|
150
162
|
return !mirrorOf || !balances.find(b => b.tokenId === mirrorOf);
|
151
163
|
};
|
@@ -231,30 +243,6 @@ class StorageHelper {
|
|
231
243
|
}
|
232
244
|
}
|
233
245
|
|
234
|
-
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
|
235
|
-
var desc = {};
|
236
|
-
Object.keys(descriptor).forEach(function (key) {
|
237
|
-
desc[key] = descriptor[key];
|
238
|
-
});
|
239
|
-
desc.enumerable = !!desc.enumerable;
|
240
|
-
desc.configurable = !!desc.configurable;
|
241
|
-
if ('value' in desc || desc.initializer) {
|
242
|
-
desc.writable = true;
|
243
|
-
}
|
244
|
-
desc = decorators.slice().reverse().reduce(function (desc, decorator) {
|
245
|
-
return decorator(target, property, desc) || desc;
|
246
|
-
}, desc);
|
247
|
-
if (context && desc.initializer !== void 0) {
|
248
|
-
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
|
249
|
-
desc.initializer = undefined;
|
250
|
-
}
|
251
|
-
if (desc.initializer === void 0) {
|
252
|
-
Object.defineProperty(target, property, desc);
|
253
|
-
desc = null;
|
254
|
-
}
|
255
|
-
return desc;
|
256
|
-
}
|
257
|
-
|
258
246
|
function excludeFromTransferableAmount(locks) {
|
259
247
|
if (typeof locks === "string") return BigInt(locks);
|
260
248
|
if (!Array.isArray(locks)) locks = [locks];
|
@@ -276,8 +264,6 @@ function includeInTotalExtraAmount(extra) {
|
|
276
264
|
|
277
265
|
/** Used by plugins to help define their custom `BalanceType` */
|
278
266
|
|
279
|
-
var _dec, _dec2, _dec3, _class, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _class2, _dec11, _class3, _dec12, _dec13, _dec14, _dec15, _dec16, _dec17, _dec18, _class4;
|
280
|
-
|
281
267
|
/**
|
282
268
|
* Have the importing library define its Token and BalanceJson enums (as a sum type of all plugins) and pass them into some
|
283
269
|
* internal global typescript context, which is then picked up on by this module.
|
@@ -288,12 +274,12 @@ var _dec, _dec2, _dec3, _class, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10
|
|
288
274
|
/**
|
289
275
|
* A collection of balances.
|
290
276
|
*/
|
291
|
-
|
277
|
+
class Balances {
|
292
278
|
//
|
293
279
|
// Properties
|
294
280
|
//
|
295
281
|
|
296
|
-
#balances =
|
282
|
+
#balances = [];
|
297
283
|
|
298
284
|
//
|
299
285
|
// Methods
|
@@ -301,7 +287,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
301
287
|
|
302
288
|
constructor(balances, hydrate) {
|
303
289
|
// handle Balances (convert to Balance[])
|
304
|
-
if (balances instanceof Balances) return new Balances(
|
290
|
+
if (balances instanceof Balances) return new Balances(balances.each, hydrate);
|
305
291
|
|
306
292
|
// handle Balance (convert to Balance[])
|
307
293
|
if (balances instanceof Balance) return new Balances([balances], hydrate);
|
@@ -316,19 +302,19 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
316
302
|
if (!util.isArrayOf(balances, Balance)) return new Balances(balances.map(storage => new Balance(storage)), hydrate);
|
317
303
|
|
318
304
|
// handle Balance[]
|
319
|
-
this.#balances =
|
305
|
+
this.#balances = balances;
|
320
306
|
if (hydrate !== undefined) this.hydrate(hydrate);
|
321
307
|
}
|
322
308
|
|
323
309
|
/**
|
324
310
|
* Calling toJSON on a collection of balances will return the underlying BalanceJsonList.
|
325
311
|
*/
|
326
|
-
toJSON = () => Object.fromEntries(
|
312
|
+
toJSON = () => Object.fromEntries(this.#balances.map(balance => {
|
327
313
|
try {
|
328
|
-
return [id, balance.toJSON()];
|
314
|
+
return [balance.id, balance.toJSON()];
|
329
315
|
} catch (error) {
|
330
316
|
log.error("Failed to convert balance to JSON", error, {
|
331
|
-
id,
|
317
|
+
id: balance.id,
|
332
318
|
balance
|
333
319
|
});
|
334
320
|
return null;
|
@@ -348,7 +334,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
348
334
|
*/
|
349
335
|
[Symbol.iterator] = () =>
|
350
336
|
// Create an array of the balances in this collection and return the result of its iterator.
|
351
|
-
|
337
|
+
this.#balances[Symbol.iterator]();
|
352
338
|
|
353
339
|
/**
|
354
340
|
* Hydrates all balances in this collection.
|
@@ -356,7 +342,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
356
342
|
* @param sources - The sources to hydrate from.
|
357
343
|
*/
|
358
344
|
hydrate = sources => {
|
359
|
-
|
345
|
+
this.#balances.map(balance => balance.hydrate(sources));
|
360
346
|
};
|
361
347
|
|
362
348
|
/**
|
@@ -365,7 +351,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
365
351
|
* @param id - The id of the balance to fetch.
|
366
352
|
* @returns The balance if one exists, or none.
|
367
353
|
*/
|
368
|
-
get = id => this.#balances
|
354
|
+
get = id => this.#balances.find(balance => balance.id === id) ?? null;
|
369
355
|
|
370
356
|
/**
|
371
357
|
* Retrieve balances from this collection by search query.
|
@@ -398,10 +384,8 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
398
384
|
if (balances instanceof Balance) return this.add(new Balances(balances));
|
399
385
|
|
400
386
|
// merge balances
|
401
|
-
const mergedBalances =
|
402
|
-
|
403
|
-
};
|
404
|
-
[...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);
|
405
389
|
|
406
390
|
// return new balances
|
407
391
|
return new Balances(Object.values(mergedBalances));
|
@@ -419,25 +403,23 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
419
403
|
// handle single id
|
420
404
|
if (!Array.isArray(ids)) return this.remove([ids]);
|
421
405
|
|
422
|
-
// merge balances
|
423
|
-
|
424
|
-
...this.#balances
|
425
|
-
};
|
426
|
-
ids.forEach(id => delete removedBalances[id]);
|
427
|
-
|
428
|
-
// return new balances
|
429
|
-
return new Balances(Object.values(removedBalances));
|
406
|
+
// merge and return new balances
|
407
|
+
return new Balances(this.#balances.filter(balance => !ids.includes(balance.id)));
|
430
408
|
};
|
431
409
|
|
432
410
|
// TODO: Add some more useful aggregator methods
|
433
411
|
|
412
|
+
get each() {
|
413
|
+
return [...this];
|
414
|
+
}
|
415
|
+
|
434
416
|
/**
|
435
417
|
* Get an array of balances in this collection, sorted by chain sortIndex.
|
436
418
|
*
|
437
419
|
* @returns A sorted array of the balances in this collection.
|
438
420
|
*/
|
439
421
|
get sorted() {
|
440
|
-
return [...this].sort((a, b) => ((a.chain || a.evmNetwork)?.sortIndex
|
422
|
+
return [...this].sort((a, b) => ((a.chain || a.evmNetwork)?.sortIndex ?? Number.MAX_SAFE_INTEGER) - ((b.chain || b.evmNetwork)?.sortIndex ?? Number.MAX_SAFE_INTEGER));
|
441
423
|
}
|
442
424
|
|
443
425
|
/**
|
@@ -460,12 +442,12 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
460
442
|
get sum() {
|
461
443
|
return new SumBalancesFormatter(this);
|
462
444
|
}
|
463
|
-
}
|
445
|
+
}
|
464
446
|
|
465
447
|
/**
|
466
448
|
* An individual balance.
|
467
449
|
*/
|
468
|
-
|
450
|
+
class Balance {
|
469
451
|
//
|
470
452
|
// Properties
|
471
453
|
//
|
@@ -479,7 +461,6 @@ let Balance = (_dec4 = typescriptMemoize.Memoize(), _dec5 = typescriptMemoize.Me
|
|
479
461
|
//
|
480
462
|
|
481
463
|
constructor(storage, hydrate) {
|
482
|
-
this.#format = memoize__default["default"](this.#format);
|
483
464
|
this.#storage = storage;
|
484
465
|
if (hydrate !== undefined) this.hydrate(hydrate);
|
485
466
|
}
|
@@ -602,8 +583,8 @@ let Balance = (_dec4 = typescriptMemoize.Memoize(), _dec5 = typescriptMemoize.Me
|
|
602
583
|
// subtract the lock from the free amount (but don't go below 0)
|
603
584
|
return this.#format(util.BigMath.max(this.free.planck - excludeAmount, BigInt("0")));
|
604
585
|
}
|
605
|
-
}
|
606
|
-
|
586
|
+
}
|
587
|
+
class BalanceFormatter {
|
607
588
|
#planck;
|
608
589
|
#decimals;
|
609
590
|
#fiatRatios;
|
@@ -611,7 +592,6 @@ let BalanceFormatter = (_dec11 = typescriptMemoize.Memoize(), (_class3 = class B
|
|
611
592
|
this.#planck = typeof planck === "bigint" ? planck.toString() : planck ?? "0";
|
612
593
|
this.#decimals = decimals || 0;
|
613
594
|
this.#fiatRatios = fiatRatios || null;
|
614
|
-
this.fiat = memoize__default["default"](this.fiat);
|
615
595
|
}
|
616
596
|
toJSON = () => this.#planck;
|
617
597
|
get planck() {
|
@@ -626,14 +606,13 @@ let BalanceFormatter = (_dec11 = typescriptMemoize.Memoize(), (_class3 = class B
|
|
626
606
|
if (!ratio) return null;
|
627
607
|
return parseFloat(this.tokens) * ratio;
|
628
608
|
}
|
629
|
-
}
|
630
|
-
|
609
|
+
}
|
610
|
+
class FiatSumBalancesFormatter {
|
631
611
|
#balances;
|
632
612
|
#currency;
|
633
613
|
constructor(balances, currency) {
|
634
614
|
this.#balances = balances;
|
635
615
|
this.#currency = currency;
|
636
|
-
this.#sum = memoize__default["default"](this.#sum);
|
637
616
|
}
|
638
617
|
#sum = balanceField => {
|
639
618
|
// a function to get a fiat amount from a balance
|
@@ -680,12 +659,11 @@ let FiatSumBalancesFormatter = (_dec12 = typescriptMemoize.Memoize(), _dec13 = t
|
|
680
659
|
get feePayable() {
|
681
660
|
return this.#sum("feePayable");
|
682
661
|
}
|
683
|
-
}
|
662
|
+
}
|
684
663
|
class SumBalancesFormatter {
|
685
664
|
#balances;
|
686
665
|
constructor(balances) {
|
687
666
|
this.#balances = balances;
|
688
|
-
this.fiat = memoize__default["default"](this.fiat);
|
689
667
|
}
|
690
668
|
fiat(currency) {
|
691
669
|
return new FiatSumBalancesFormatter(this.#balances, currency);
|
@@ -701,6 +679,7 @@ exports.StorageHelper = StorageHelper;
|
|
701
679
|
exports.SumBalancesFormatter = SumBalancesFormatter;
|
702
680
|
exports.TalismanBalancesDatabase = TalismanBalancesDatabase;
|
703
681
|
exports.balances = balances;
|
682
|
+
exports.createTypeRegistryCache = createTypeRegistryCache;
|
704
683
|
exports.db = db;
|
705
684
|
exports.excludeFromFeePayableLocks = excludeFromFeePayableLocks;
|
706
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(
|
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.
|
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: ">=
|
81
|
+
node: ">=18"
|
85
82
|
},
|
86
83
|
scripts: {
|
87
84
|
test: "jest",
|
@@ -95,9 +92,7 @@ var packageJson = {
|
|
95
92
|
"@talismn/token-rates": "workspace:^",
|
96
93
|
"@talismn/util": "workspace:^",
|
97
94
|
anylogger: "^1.0.11",
|
98
|
-
dexie: "^3.2.
|
99
|
-
lodash: "^4.17.21",
|
100
|
-
"typescript-memoize": "^1.1.0"
|
95
|
+
dexie: "^3.2.3"
|
101
96
|
},
|
102
97
|
devDependencies: {
|
103
98
|
"@polkadot/types": "^9.10.5",
|
@@ -128,15 +123,33 @@ var packageJson = {
|
|
128
123
|
|
129
124
|
var log = anylogger(packageJson.name);
|
130
125
|
|
131
|
-
async function balances(balanceModule,
|
126
|
+
async function balances(balanceModule, addressesByToken, callback) {
|
132
127
|
// subscription request
|
133
|
-
if (callback !== undefined) return await balanceModule.subscribeBalances(
|
128
|
+
if (callback !== undefined) return await balanceModule.subscribeBalances(addressesByToken, callback);
|
134
129
|
|
135
130
|
// one-off request
|
136
|
-
return await balanceModule.fetchBalances(
|
131
|
+
return await balanceModule.fetchBalances(addressesByToken);
|
137
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
|
+
};
|
138
151
|
const filterMirrorTokens = (balance, i, balances) => {
|
139
|
-
// TODO implement a mirrorOf property, which should be set from chaindata
|
152
|
+
// TODO: implement a mirrorOf property, which should be set from chaindata
|
140
153
|
const mirrorOf = balance.token?.mirrorOf;
|
141
154
|
return !mirrorOf || !balances.find(b => b.tokenId === mirrorOf);
|
142
155
|
};
|
@@ -222,30 +235,6 @@ class StorageHelper {
|
|
222
235
|
}
|
223
236
|
}
|
224
237
|
|
225
|
-
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
|
226
|
-
var desc = {};
|
227
|
-
Object.keys(descriptor).forEach(function (key) {
|
228
|
-
desc[key] = descriptor[key];
|
229
|
-
});
|
230
|
-
desc.enumerable = !!desc.enumerable;
|
231
|
-
desc.configurable = !!desc.configurable;
|
232
|
-
if ('value' in desc || desc.initializer) {
|
233
|
-
desc.writable = true;
|
234
|
-
}
|
235
|
-
desc = decorators.slice().reverse().reduce(function (desc, decorator) {
|
236
|
-
return decorator(target, property, desc) || desc;
|
237
|
-
}, desc);
|
238
|
-
if (context && desc.initializer !== void 0) {
|
239
|
-
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
|
240
|
-
desc.initializer = undefined;
|
241
|
-
}
|
242
|
-
if (desc.initializer === void 0) {
|
243
|
-
Object.defineProperty(target, property, desc);
|
244
|
-
desc = null;
|
245
|
-
}
|
246
|
-
return desc;
|
247
|
-
}
|
248
|
-
|
249
238
|
function excludeFromTransferableAmount(locks) {
|
250
239
|
if (typeof locks === "string") return BigInt(locks);
|
251
240
|
if (!Array.isArray(locks)) locks = [locks];
|
@@ -267,8 +256,6 @@ function includeInTotalExtraAmount(extra) {
|
|
267
256
|
|
268
257
|
/** Used by plugins to help define their custom `BalanceType` */
|
269
258
|
|
270
|
-
var _dec, _dec2, _dec3, _class, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _class2, _dec11, _class3, _dec12, _dec13, _dec14, _dec15, _dec16, _dec17, _dec18, _class4;
|
271
|
-
|
272
259
|
/**
|
273
260
|
* Have the importing library define its Token and BalanceJson enums (as a sum type of all plugins) and pass them into some
|
274
261
|
* internal global typescript context, which is then picked up on by this module.
|
@@ -279,12 +266,12 @@ var _dec, _dec2, _dec3, _class, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10
|
|
279
266
|
/**
|
280
267
|
* A collection of balances.
|
281
268
|
*/
|
282
|
-
|
269
|
+
class Balances {
|
283
270
|
//
|
284
271
|
// Properties
|
285
272
|
//
|
286
273
|
|
287
|
-
#balances =
|
274
|
+
#balances = [];
|
288
275
|
|
289
276
|
//
|
290
277
|
// Methods
|
@@ -292,7 +279,7 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
|
|
292
279
|
|
293
280
|
constructor(balances, hydrate) {
|
294
281
|
// handle Balances (convert to Balance[])
|
295
|
-
if (balances instanceof Balances) return new Balances(
|
282
|
+
if (balances instanceof Balances) return new Balances(balances.each, hydrate);
|
296
283
|
|
297
284
|
// handle Balance (convert to Balance[])
|
298
285
|
if (balances instanceof Balance) return new Balances([balances], hydrate);
|
@@ -307,19 +294,19 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
|
|
307
294
|
if (!isArrayOf(balances, Balance)) return new Balances(balances.map(storage => new Balance(storage)), hydrate);
|
308
295
|
|
309
296
|
// handle Balance[]
|
310
|
-
this.#balances =
|
297
|
+
this.#balances = balances;
|
311
298
|
if (hydrate !== undefined) this.hydrate(hydrate);
|
312
299
|
}
|
313
300
|
|
314
301
|
/**
|
315
302
|
* Calling toJSON on a collection of balances will return the underlying BalanceJsonList.
|
316
303
|
*/
|
317
|
-
toJSON = () => Object.fromEntries(
|
304
|
+
toJSON = () => Object.fromEntries(this.#balances.map(balance => {
|
318
305
|
try {
|
319
|
-
return [id, balance.toJSON()];
|
306
|
+
return [balance.id, balance.toJSON()];
|
320
307
|
} catch (error) {
|
321
308
|
log.error("Failed to convert balance to JSON", error, {
|
322
|
-
id,
|
309
|
+
id: balance.id,
|
323
310
|
balance
|
324
311
|
});
|
325
312
|
return null;
|
@@ -339,7 +326,7 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
|
|
339
326
|
*/
|
340
327
|
[Symbol.iterator] = () =>
|
341
328
|
// Create an array of the balances in this collection and return the result of its iterator.
|
342
|
-
|
329
|
+
this.#balances[Symbol.iterator]();
|
343
330
|
|
344
331
|
/**
|
345
332
|
* Hydrates all balances in this collection.
|
@@ -347,7 +334,7 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
|
|
347
334
|
* @param sources - The sources to hydrate from.
|
348
335
|
*/
|
349
336
|
hydrate = sources => {
|
350
|
-
|
337
|
+
this.#balances.map(balance => balance.hydrate(sources));
|
351
338
|
};
|
352
339
|
|
353
340
|
/**
|
@@ -356,7 +343,7 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
|
|
356
343
|
* @param id - The id of the balance to fetch.
|
357
344
|
* @returns The balance if one exists, or none.
|
358
345
|
*/
|
359
|
-
get = id => this.#balances
|
346
|
+
get = id => this.#balances.find(balance => balance.id === id) ?? null;
|
360
347
|
|
361
348
|
/**
|
362
349
|
* Retrieve balances from this collection by search query.
|
@@ -389,10 +376,8 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
|
|
389
376
|
if (balances instanceof Balance) return this.add(new Balances(balances));
|
390
377
|
|
391
378
|
// merge balances
|
392
|
-
const mergedBalances =
|
393
|
-
|
394
|
-
};
|
395
|
-
[...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);
|
396
381
|
|
397
382
|
// return new balances
|
398
383
|
return new Balances(Object.values(mergedBalances));
|
@@ -410,25 +395,23 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
|
|
410
395
|
// handle single id
|
411
396
|
if (!Array.isArray(ids)) return this.remove([ids]);
|
412
397
|
|
413
|
-
// merge balances
|
414
|
-
|
415
|
-
...this.#balances
|
416
|
-
};
|
417
|
-
ids.forEach(id => delete removedBalances[id]);
|
418
|
-
|
419
|
-
// return new balances
|
420
|
-
return new Balances(Object.values(removedBalances));
|
398
|
+
// merge and return new balances
|
399
|
+
return new Balances(this.#balances.filter(balance => !ids.includes(balance.id)));
|
421
400
|
};
|
422
401
|
|
423
402
|
// TODO: Add some more useful aggregator methods
|
424
403
|
|
404
|
+
get each() {
|
405
|
+
return [...this];
|
406
|
+
}
|
407
|
+
|
425
408
|
/**
|
426
409
|
* Get an array of balances in this collection, sorted by chain sortIndex.
|
427
410
|
*
|
428
411
|
* @returns A sorted array of the balances in this collection.
|
429
412
|
*/
|
430
413
|
get sorted() {
|
431
|
-
return [...this].sort((a, b) => ((a.chain || a.evmNetwork)?.sortIndex
|
414
|
+
return [...this].sort((a, b) => ((a.chain || a.evmNetwork)?.sortIndex ?? Number.MAX_SAFE_INTEGER) - ((b.chain || b.evmNetwork)?.sortIndex ?? Number.MAX_SAFE_INTEGER));
|
432
415
|
}
|
433
416
|
|
434
417
|
/**
|
@@ -451,12 +434,12 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
|
|
451
434
|
get sum() {
|
452
435
|
return new SumBalancesFormatter(this);
|
453
436
|
}
|
454
|
-
}
|
437
|
+
}
|
455
438
|
|
456
439
|
/**
|
457
440
|
* An individual balance.
|
458
441
|
*/
|
459
|
-
|
442
|
+
class Balance {
|
460
443
|
//
|
461
444
|
// Properties
|
462
445
|
//
|
@@ -470,7 +453,6 @@ let Balance = (_dec4 = Memoize(), _dec5 = Memoize(), _dec6 = Memoize(), _dec7 =
|
|
470
453
|
//
|
471
454
|
|
472
455
|
constructor(storage, hydrate) {
|
473
|
-
this.#format = memoize(this.#format);
|
474
456
|
this.#storage = storage;
|
475
457
|
if (hydrate !== undefined) this.hydrate(hydrate);
|
476
458
|
}
|
@@ -593,8 +575,8 @@ let Balance = (_dec4 = Memoize(), _dec5 = Memoize(), _dec6 = Memoize(), _dec7 =
|
|
593
575
|
// subtract the lock from the free amount (but don't go below 0)
|
594
576
|
return this.#format(BigMath.max(this.free.planck - excludeAmount, BigInt("0")));
|
595
577
|
}
|
596
|
-
}
|
597
|
-
|
578
|
+
}
|
579
|
+
class BalanceFormatter {
|
598
580
|
#planck;
|
599
581
|
#decimals;
|
600
582
|
#fiatRatios;
|
@@ -602,7 +584,6 @@ let BalanceFormatter = (_dec11 = Memoize(), (_class3 = class BalanceFormatter {
|
|
602
584
|
this.#planck = typeof planck === "bigint" ? planck.toString() : planck ?? "0";
|
603
585
|
this.#decimals = decimals || 0;
|
604
586
|
this.#fiatRatios = fiatRatios || null;
|
605
|
-
this.fiat = memoize(this.fiat);
|
606
587
|
}
|
607
588
|
toJSON = () => this.#planck;
|
608
589
|
get planck() {
|
@@ -617,14 +598,13 @@ let BalanceFormatter = (_dec11 = Memoize(), (_class3 = class BalanceFormatter {
|
|
617
598
|
if (!ratio) return null;
|
618
599
|
return parseFloat(this.tokens) * ratio;
|
619
600
|
}
|
620
|
-
}
|
621
|
-
|
601
|
+
}
|
602
|
+
class FiatSumBalancesFormatter {
|
622
603
|
#balances;
|
623
604
|
#currency;
|
624
605
|
constructor(balances, currency) {
|
625
606
|
this.#balances = balances;
|
626
607
|
this.#currency = currency;
|
627
|
-
this.#sum = memoize(this.#sum);
|
628
608
|
}
|
629
609
|
#sum = balanceField => {
|
630
610
|
// a function to get a fiat amount from a balance
|
@@ -671,16 +651,15 @@ let FiatSumBalancesFormatter = (_dec12 = Memoize(), _dec13 = Memoize(), _dec14 =
|
|
671
651
|
get feePayable() {
|
672
652
|
return this.#sum("feePayable");
|
673
653
|
}
|
674
|
-
}
|
654
|
+
}
|
675
655
|
class SumBalancesFormatter {
|
676
656
|
#balances;
|
677
657
|
constructor(balances) {
|
678
658
|
this.#balances = balances;
|
679
|
-
this.fiat = memoize(this.fiat);
|
680
659
|
}
|
681
660
|
fiat(currency) {
|
682
661
|
return new FiatSumBalancesFormatter(this.#balances, currency);
|
683
662
|
}
|
684
663
|
}
|
685
664
|
|
686
|
-
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
|
+
"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": ">=
|
22
|
+
"node": ">=18"
|
23
23
|
},
|
24
24
|
"scripts": {
|
25
25
|
"test": "jest",
|
@@ -27,15 +27,13 @@
|
|
27
27
|
"clean": "rm -rf dist && rm -rf .turbo rm -rf node_modules"
|
28
28
|
},
|
29
29
|
"dependencies": {
|
30
|
-
"@talismn/chain-connector": "^0.4.
|
31
|
-
"@talismn/chain-connector-evm": "^0.4.
|
32
|
-
"@talismn/chaindata-provider": "^0.4.
|
33
|
-
"@talismn/token-rates": "^0.1.
|
34
|
-
"@talismn/util": "^0.1.
|
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",
|
35
35
|
"anylogger": "^1.0.11",
|
36
|
-
"dexie": "^3.2.
|
37
|
-
"lodash": "^4.17.21",
|
38
|
-
"typescript-memoize": "^1.1.0"
|
36
|
+
"dexie": "^3.2.3"
|
39
37
|
},
|
40
38
|
"devDependencies": {
|
41
39
|
"@polkadot/types": "^9.10.5",
|