@talismn/balances 0.0.0-pr524-20230223175953 → 0.0.0-pr524-20230310034933
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 +8 -5
- 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/talismn-balances.cjs.dev.js +55 -76
- package/dist/talismn-balances.cjs.prod.js +55 -76
- package/dist/talismn-balances.esm.js +56 -77
- package/package.json +6 -8
package/CHANGELOG.md
CHANGED
@@ -1,14 +1,17 @@
|
|
1
1
|
# @talismn/balances
|
2
2
|
|
3
|
-
## 0.0.0-pr524-
|
3
|
+
## 0.0.0-pr524-20230310034933
|
4
4
|
|
5
5
|
### Patch Changes
|
6
6
|
|
7
|
+
- 6643a4e: fix: tokenRates in @talismn/balances-react
|
8
|
+
- 6643a4e: fix: ported useDbCache related perf fixes to @talismn/balances-react
|
9
|
+
- Updated dependencies [6643a4e]
|
7
10
|
- Updated dependencies [87a1348]
|
8
|
-
- @talismn/
|
9
|
-
- @talismn/
|
10
|
-
- @talismn/chain-connector
|
11
|
-
- @talismn/
|
11
|
+
- @talismn/token-rates@0.0.0-pr524-20230310034933
|
12
|
+
- @talismn/chaindata-provider@0.0.0-pr524-20230310034933
|
13
|
+
- @talismn/chain-connector@0.0.0-pr524-20230310034933
|
14
|
+
- @talismn/chain-connector-evm@0.0.0-pr524-20230310034933
|
12
15
|
|
13
16
|
## 0.3.3
|
14
17
|
|
@@ -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;
|
@@ -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.0.0-pr524-
|
70
|
+
version: "0.0.0-pr524-20230310034933",
|
75
71
|
author: "Talisman",
|
76
72
|
homepage: "https://talisman.xyz",
|
77
73
|
license: "UNLICENSED",
|
@@ -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.3"
|
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,16 +131,34 @@ 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
160
|
var _balance$token;
|
149
|
-
// TODO implement a mirrorOf property, which should be set from chaindata
|
161
|
+
// TODO: implement a mirrorOf property, which should be set from chaindata
|
150
162
|
const mirrorOf = (_balance$token = balance.token) === null || _balance$token === void 0 ? void 0 : _balance$token.mirrorOf;
|
151
163
|
return !mirrorOf || !balances.find(b => b.tokenId === mirrorOf);
|
152
164
|
};
|
@@ -233,30 +245,6 @@ class StorageHelper {
|
|
233
245
|
}
|
234
246
|
}
|
235
247
|
|
236
|
-
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
|
237
|
-
var desc = {};
|
238
|
-
Object.keys(descriptor).forEach(function (key) {
|
239
|
-
desc[key] = descriptor[key];
|
240
|
-
});
|
241
|
-
desc.enumerable = !!desc.enumerable;
|
242
|
-
desc.configurable = !!desc.configurable;
|
243
|
-
if ('value' in desc || desc.initializer) {
|
244
|
-
desc.writable = true;
|
245
|
-
}
|
246
|
-
desc = decorators.slice().reverse().reduce(function (desc, decorator) {
|
247
|
-
return decorator(target, property, desc) || desc;
|
248
|
-
}, desc);
|
249
|
-
if (context && desc.initializer !== void 0) {
|
250
|
-
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
|
251
|
-
desc.initializer = undefined;
|
252
|
-
}
|
253
|
-
if (desc.initializer === void 0) {
|
254
|
-
Object.defineProperty(target, property, desc);
|
255
|
-
desc = null;
|
256
|
-
}
|
257
|
-
return desc;
|
258
|
-
}
|
259
|
-
|
260
248
|
function excludeFromTransferableAmount(locks) {
|
261
249
|
if (typeof locks === "string") return BigInt(locks);
|
262
250
|
if (!Array.isArray(locks)) locks = [locks];
|
@@ -278,8 +266,6 @@ function includeInTotalExtraAmount(extra) {
|
|
278
266
|
|
279
267
|
/** Used by plugins to help define their custom `BalanceType` */
|
280
268
|
|
281
|
-
var _dec, _dec2, _dec3, _class, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _class2, _dec11, _class3, _dec12, _dec13, _dec14, _dec15, _dec16, _dec17, _dec18, _class4;
|
282
|
-
|
283
269
|
/**
|
284
270
|
* Have the importing library define its Token and BalanceJson enums (as a sum type of all plugins) and pass them into some
|
285
271
|
* internal global typescript context, which is then picked up on by this module.
|
@@ -290,12 +276,12 @@ var _dec, _dec2, _dec3, _class, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10
|
|
290
276
|
/**
|
291
277
|
* A collection of balances.
|
292
278
|
*/
|
293
|
-
|
279
|
+
class Balances {
|
294
280
|
//
|
295
281
|
// Properties
|
296
282
|
//
|
297
283
|
|
298
|
-
#balances =
|
284
|
+
#balances = [];
|
299
285
|
|
300
286
|
//
|
301
287
|
// Methods
|
@@ -303,7 +289,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
303
289
|
|
304
290
|
constructor(balances, hydrate) {
|
305
291
|
// handle Balances (convert to Balance[])
|
306
|
-
if (balances instanceof Balances) return new Balances(
|
292
|
+
if (balances instanceof Balances) return new Balances(balances.each, hydrate);
|
307
293
|
|
308
294
|
// handle Balance (convert to Balance[])
|
309
295
|
if (balances instanceof Balance) return new Balances([balances], hydrate);
|
@@ -318,19 +304,19 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
318
304
|
if (!util.isArrayOf(balances, Balance)) return new Balances(balances.map(storage => new Balance(storage)), hydrate);
|
319
305
|
|
320
306
|
// handle Balance[]
|
321
|
-
this.#balances =
|
307
|
+
this.#balances = balances;
|
322
308
|
if (hydrate !== undefined) this.hydrate(hydrate);
|
323
309
|
}
|
324
310
|
|
325
311
|
/**
|
326
312
|
* Calling toJSON on a collection of balances will return the underlying BalanceJsonList.
|
327
313
|
*/
|
328
|
-
toJSON = () => Object.fromEntries(
|
314
|
+
toJSON = () => Object.fromEntries(this.#balances.map(balance => {
|
329
315
|
try {
|
330
|
-
return [id, balance.toJSON()];
|
316
|
+
return [balance.id, balance.toJSON()];
|
331
317
|
} catch (error) {
|
332
318
|
log.error("Failed to convert balance to JSON", error, {
|
333
|
-
id,
|
319
|
+
id: balance.id,
|
334
320
|
balance
|
335
321
|
});
|
336
322
|
return null;
|
@@ -350,7 +336,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
350
336
|
*/
|
351
337
|
[Symbol.iterator] = () =>
|
352
338
|
// Create an array of the balances in this collection and return the result of its iterator.
|
353
|
-
|
339
|
+
this.#balances[Symbol.iterator]();
|
354
340
|
|
355
341
|
/**
|
356
342
|
* Hydrates all balances in this collection.
|
@@ -358,7 +344,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
358
344
|
* @param sources - The sources to hydrate from.
|
359
345
|
*/
|
360
346
|
hydrate = sources => {
|
361
|
-
|
347
|
+
this.#balances.map(balance => balance.hydrate(sources));
|
362
348
|
};
|
363
349
|
|
364
350
|
/**
|
@@ -367,7 +353,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
367
353
|
* @param id - The id of the balance to fetch.
|
368
354
|
* @returns The balance if one exists, or none.
|
369
355
|
*/
|
370
|
-
get = id => this.#balances
|
356
|
+
get = id => this.#balances.find(balance => balance.id === id) ?? null;
|
371
357
|
|
372
358
|
/**
|
373
359
|
* Retrieve balances from this collection by search query.
|
@@ -400,10 +386,8 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
400
386
|
if (balances instanceof Balance) return this.add(new Balances(balances));
|
401
387
|
|
402
388
|
// merge balances
|
403
|
-
const mergedBalances =
|
404
|
-
|
405
|
-
};
|
406
|
-
[...balances].forEach(balance => mergedBalances[balance.id] = balance);
|
389
|
+
const mergedBalances = Object.fromEntries(this.#balances.map(balance => [balance.id, balance]));
|
390
|
+
balances.each.forEach(balance => mergedBalances[balance.id] = balance);
|
407
391
|
|
408
392
|
// return new balances
|
409
393
|
return new Balances(Object.values(mergedBalances));
|
@@ -421,18 +405,16 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
421
405
|
// handle single id
|
422
406
|
if (!Array.isArray(ids)) return this.remove([ids]);
|
423
407
|
|
424
|
-
// merge balances
|
425
|
-
|
426
|
-
...this.#balances
|
427
|
-
};
|
428
|
-
ids.forEach(id => delete removedBalances[id]);
|
429
|
-
|
430
|
-
// return new balances
|
431
|
-
return new Balances(Object.values(removedBalances));
|
408
|
+
// merge and return new balances
|
409
|
+
return new Balances(this.#balances.filter(balance => !ids.includes(balance.id)));
|
432
410
|
};
|
433
411
|
|
434
412
|
// TODO: Add some more useful aggregator methods
|
435
413
|
|
414
|
+
get each() {
|
415
|
+
return [...this];
|
416
|
+
}
|
417
|
+
|
436
418
|
/**
|
437
419
|
* Get an array of balances in this collection, sorted by chain sortIndex.
|
438
420
|
*
|
@@ -441,7 +423,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
441
423
|
get sorted() {
|
442
424
|
return [...this].sort((a, b) => {
|
443
425
|
var _ref, _ref2;
|
444
|
-
return (((_ref = a.chain || a.evmNetwork) === null || _ref === void 0 ? void 0 : _ref.sortIndex)
|
426
|
+
return (((_ref = a.chain || a.evmNetwork) === null || _ref === void 0 ? void 0 : _ref.sortIndex) ?? Number.MAX_SAFE_INTEGER) - (((_ref2 = b.chain || b.evmNetwork) === null || _ref2 === void 0 ? void 0 : _ref2.sortIndex) ?? Number.MAX_SAFE_INTEGER);
|
445
427
|
});
|
446
428
|
}
|
447
429
|
|
@@ -465,12 +447,12 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
465
447
|
get sum() {
|
466
448
|
return new SumBalancesFormatter(this);
|
467
449
|
}
|
468
|
-
}
|
450
|
+
}
|
469
451
|
|
470
452
|
/**
|
471
453
|
* An individual balance.
|
472
454
|
*/
|
473
|
-
|
455
|
+
class Balance {
|
474
456
|
//
|
475
457
|
// Properties
|
476
458
|
//
|
@@ -484,7 +466,6 @@ let Balance = (_dec4 = typescriptMemoize.Memoize(), _dec5 = typescriptMemoize.Me
|
|
484
466
|
//
|
485
467
|
|
486
468
|
constructor(storage, hydrate) {
|
487
|
-
this.#format = memoize__default["default"](this.#format);
|
488
469
|
this.#storage = storage;
|
489
470
|
if (hydrate !== undefined) this.hydrate(hydrate);
|
490
471
|
}
|
@@ -618,8 +599,8 @@ let Balance = (_dec4 = typescriptMemoize.Memoize(), _dec5 = typescriptMemoize.Me
|
|
618
599
|
// subtract the lock from the free amount (but don't go below 0)
|
619
600
|
return this.#format(util.BigMath.max(this.free.planck - excludeAmount, BigInt("0")));
|
620
601
|
}
|
621
|
-
}
|
622
|
-
|
602
|
+
}
|
603
|
+
class BalanceFormatter {
|
623
604
|
#planck;
|
624
605
|
#decimals;
|
625
606
|
#fiatRatios;
|
@@ -627,7 +608,6 @@ let BalanceFormatter = (_dec11 = typescriptMemoize.Memoize(), (_class3 = class B
|
|
627
608
|
this.#planck = typeof planck === "bigint" ? planck.toString() : planck ?? "0";
|
628
609
|
this.#decimals = decimals || 0;
|
629
610
|
this.#fiatRatios = fiatRatios || null;
|
630
|
-
this.fiat = memoize__default["default"](this.fiat);
|
631
611
|
}
|
632
612
|
toJSON = () => this.#planck;
|
633
613
|
get planck() {
|
@@ -642,14 +622,13 @@ let BalanceFormatter = (_dec11 = typescriptMemoize.Memoize(), (_class3 = class B
|
|
642
622
|
if (!ratio) return null;
|
643
623
|
return parseFloat(this.tokens) * ratio;
|
644
624
|
}
|
645
|
-
}
|
646
|
-
|
625
|
+
}
|
626
|
+
class FiatSumBalancesFormatter {
|
647
627
|
#balances;
|
648
628
|
#currency;
|
649
629
|
constructor(balances, currency) {
|
650
630
|
this.#balances = balances;
|
651
631
|
this.#currency = currency;
|
652
|
-
this.#sum = memoize__default["default"](this.#sum);
|
653
632
|
}
|
654
633
|
#sum = balanceField => {
|
655
634
|
// a function to get a fiat amount from a balance
|
@@ -696,12 +675,11 @@ let FiatSumBalancesFormatter = (_dec12 = typescriptMemoize.Memoize(), _dec13 = t
|
|
696
675
|
get feePayable() {
|
697
676
|
return this.#sum("feePayable");
|
698
677
|
}
|
699
|
-
}
|
678
|
+
}
|
700
679
|
class SumBalancesFormatter {
|
701
680
|
#balances;
|
702
681
|
constructor(balances) {
|
703
682
|
this.#balances = balances;
|
704
|
-
this.fiat = memoize__default["default"](this.fiat);
|
705
683
|
}
|
706
684
|
fiat(currency) {
|
707
685
|
return new FiatSumBalancesFormatter(this.#balances, currency);
|
@@ -717,6 +695,7 @@ exports.StorageHelper = StorageHelper;
|
|
717
695
|
exports.SumBalancesFormatter = SumBalancesFormatter;
|
718
696
|
exports.TalismanBalancesDatabase = TalismanBalancesDatabase;
|
719
697
|
exports.balances = balances;
|
698
|
+
exports.createTypeRegistryCache = createTypeRegistryCache;
|
720
699
|
exports.db = db;
|
721
700
|
exports.excludeFromFeePayableLocks = excludeFromFeePayableLocks;
|
722
701
|
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.0.0-pr524-
|
70
|
+
version: "0.0.0-pr524-20230310034933",
|
75
71
|
author: "Talisman",
|
76
72
|
homepage: "https://talisman.xyz",
|
77
73
|
license: "UNLICENSED",
|
@@ -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.3"
|
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,16 +131,34 @@ 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
160
|
var _balance$token;
|
149
|
-
// TODO implement a mirrorOf property, which should be set from chaindata
|
161
|
+
// TODO: implement a mirrorOf property, which should be set from chaindata
|
150
162
|
const mirrorOf = (_balance$token = balance.token) === null || _balance$token === void 0 ? void 0 : _balance$token.mirrorOf;
|
151
163
|
return !mirrorOf || !balances.find(b => b.tokenId === mirrorOf);
|
152
164
|
};
|
@@ -233,30 +245,6 @@ class StorageHelper {
|
|
233
245
|
}
|
234
246
|
}
|
235
247
|
|
236
|
-
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
|
237
|
-
var desc = {};
|
238
|
-
Object.keys(descriptor).forEach(function (key) {
|
239
|
-
desc[key] = descriptor[key];
|
240
|
-
});
|
241
|
-
desc.enumerable = !!desc.enumerable;
|
242
|
-
desc.configurable = !!desc.configurable;
|
243
|
-
if ('value' in desc || desc.initializer) {
|
244
|
-
desc.writable = true;
|
245
|
-
}
|
246
|
-
desc = decorators.slice().reverse().reduce(function (desc, decorator) {
|
247
|
-
return decorator(target, property, desc) || desc;
|
248
|
-
}, desc);
|
249
|
-
if (context && desc.initializer !== void 0) {
|
250
|
-
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
|
251
|
-
desc.initializer = undefined;
|
252
|
-
}
|
253
|
-
if (desc.initializer === void 0) {
|
254
|
-
Object.defineProperty(target, property, desc);
|
255
|
-
desc = null;
|
256
|
-
}
|
257
|
-
return desc;
|
258
|
-
}
|
259
|
-
|
260
248
|
function excludeFromTransferableAmount(locks) {
|
261
249
|
if (typeof locks === "string") return BigInt(locks);
|
262
250
|
if (!Array.isArray(locks)) locks = [locks];
|
@@ -278,8 +266,6 @@ function includeInTotalExtraAmount(extra) {
|
|
278
266
|
|
279
267
|
/** Used by plugins to help define their custom `BalanceType` */
|
280
268
|
|
281
|
-
var _dec, _dec2, _dec3, _class, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _class2, _dec11, _class3, _dec12, _dec13, _dec14, _dec15, _dec16, _dec17, _dec18, _class4;
|
282
|
-
|
283
269
|
/**
|
284
270
|
* Have the importing library define its Token and BalanceJson enums (as a sum type of all plugins) and pass them into some
|
285
271
|
* internal global typescript context, which is then picked up on by this module.
|
@@ -290,12 +276,12 @@ var _dec, _dec2, _dec3, _class, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10
|
|
290
276
|
/**
|
291
277
|
* A collection of balances.
|
292
278
|
*/
|
293
|
-
|
279
|
+
class Balances {
|
294
280
|
//
|
295
281
|
// Properties
|
296
282
|
//
|
297
283
|
|
298
|
-
#balances =
|
284
|
+
#balances = [];
|
299
285
|
|
300
286
|
//
|
301
287
|
// Methods
|
@@ -303,7 +289,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
303
289
|
|
304
290
|
constructor(balances, hydrate) {
|
305
291
|
// handle Balances (convert to Balance[])
|
306
|
-
if (balances instanceof Balances) return new Balances(
|
292
|
+
if (balances instanceof Balances) return new Balances(balances.each, hydrate);
|
307
293
|
|
308
294
|
// handle Balance (convert to Balance[])
|
309
295
|
if (balances instanceof Balance) return new Balances([balances], hydrate);
|
@@ -318,19 +304,19 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
318
304
|
if (!util.isArrayOf(balances, Balance)) return new Balances(balances.map(storage => new Balance(storage)), hydrate);
|
319
305
|
|
320
306
|
// handle Balance[]
|
321
|
-
this.#balances =
|
307
|
+
this.#balances = balances;
|
322
308
|
if (hydrate !== undefined) this.hydrate(hydrate);
|
323
309
|
}
|
324
310
|
|
325
311
|
/**
|
326
312
|
* Calling toJSON on a collection of balances will return the underlying BalanceJsonList.
|
327
313
|
*/
|
328
|
-
toJSON = () => Object.fromEntries(
|
314
|
+
toJSON = () => Object.fromEntries(this.#balances.map(balance => {
|
329
315
|
try {
|
330
|
-
return [id, balance.toJSON()];
|
316
|
+
return [balance.id, balance.toJSON()];
|
331
317
|
} catch (error) {
|
332
318
|
log.error("Failed to convert balance to JSON", error, {
|
333
|
-
id,
|
319
|
+
id: balance.id,
|
334
320
|
balance
|
335
321
|
});
|
336
322
|
return null;
|
@@ -350,7 +336,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
350
336
|
*/
|
351
337
|
[Symbol.iterator] = () =>
|
352
338
|
// Create an array of the balances in this collection and return the result of its iterator.
|
353
|
-
|
339
|
+
this.#balances[Symbol.iterator]();
|
354
340
|
|
355
341
|
/**
|
356
342
|
* Hydrates all balances in this collection.
|
@@ -358,7 +344,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
358
344
|
* @param sources - The sources to hydrate from.
|
359
345
|
*/
|
360
346
|
hydrate = sources => {
|
361
|
-
|
347
|
+
this.#balances.map(balance => balance.hydrate(sources));
|
362
348
|
};
|
363
349
|
|
364
350
|
/**
|
@@ -367,7 +353,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
367
353
|
* @param id - The id of the balance to fetch.
|
368
354
|
* @returns The balance if one exists, or none.
|
369
355
|
*/
|
370
|
-
get = id => this.#balances
|
356
|
+
get = id => this.#balances.find(balance => balance.id === id) ?? null;
|
371
357
|
|
372
358
|
/**
|
373
359
|
* Retrieve balances from this collection by search query.
|
@@ -400,10 +386,8 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
400
386
|
if (balances instanceof Balance) return this.add(new Balances(balances));
|
401
387
|
|
402
388
|
// merge balances
|
403
|
-
const mergedBalances =
|
404
|
-
|
405
|
-
};
|
406
|
-
[...balances].forEach(balance => mergedBalances[balance.id] = balance);
|
389
|
+
const mergedBalances = Object.fromEntries(this.#balances.map(balance => [balance.id, balance]));
|
390
|
+
balances.each.forEach(balance => mergedBalances[balance.id] = balance);
|
407
391
|
|
408
392
|
// return new balances
|
409
393
|
return new Balances(Object.values(mergedBalances));
|
@@ -421,18 +405,16 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
421
405
|
// handle single id
|
422
406
|
if (!Array.isArray(ids)) return this.remove([ids]);
|
423
407
|
|
424
|
-
// merge balances
|
425
|
-
|
426
|
-
...this.#balances
|
427
|
-
};
|
428
|
-
ids.forEach(id => delete removedBalances[id]);
|
429
|
-
|
430
|
-
// return new balances
|
431
|
-
return new Balances(Object.values(removedBalances));
|
408
|
+
// merge and return new balances
|
409
|
+
return new Balances(this.#balances.filter(balance => !ids.includes(balance.id)));
|
432
410
|
};
|
433
411
|
|
434
412
|
// TODO: Add some more useful aggregator methods
|
435
413
|
|
414
|
+
get each() {
|
415
|
+
return [...this];
|
416
|
+
}
|
417
|
+
|
436
418
|
/**
|
437
419
|
* Get an array of balances in this collection, sorted by chain sortIndex.
|
438
420
|
*
|
@@ -441,7 +423,7 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
441
423
|
get sorted() {
|
442
424
|
return [...this].sort((a, b) => {
|
443
425
|
var _ref, _ref2;
|
444
|
-
return (((_ref = a.chain || a.evmNetwork) === null || _ref === void 0 ? void 0 : _ref.sortIndex)
|
426
|
+
return (((_ref = a.chain || a.evmNetwork) === null || _ref === void 0 ? void 0 : _ref.sortIndex) ?? Number.MAX_SAFE_INTEGER) - (((_ref2 = b.chain || b.evmNetwork) === null || _ref2 === void 0 ? void 0 : _ref2.sortIndex) ?? Number.MAX_SAFE_INTEGER);
|
445
427
|
});
|
446
428
|
}
|
447
429
|
|
@@ -465,12 +447,12 @@ let Balances = (_dec = typescriptMemoize.Memoize(), _dec2 = typescriptMemoize.Me
|
|
465
447
|
get sum() {
|
466
448
|
return new SumBalancesFormatter(this);
|
467
449
|
}
|
468
|
-
}
|
450
|
+
}
|
469
451
|
|
470
452
|
/**
|
471
453
|
* An individual balance.
|
472
454
|
*/
|
473
|
-
|
455
|
+
class Balance {
|
474
456
|
//
|
475
457
|
// Properties
|
476
458
|
//
|
@@ -484,7 +466,6 @@ let Balance = (_dec4 = typescriptMemoize.Memoize(), _dec5 = typescriptMemoize.Me
|
|
484
466
|
//
|
485
467
|
|
486
468
|
constructor(storage, hydrate) {
|
487
|
-
this.#format = memoize__default["default"](this.#format);
|
488
469
|
this.#storage = storage;
|
489
470
|
if (hydrate !== undefined) this.hydrate(hydrate);
|
490
471
|
}
|
@@ -618,8 +599,8 @@ let Balance = (_dec4 = typescriptMemoize.Memoize(), _dec5 = typescriptMemoize.Me
|
|
618
599
|
// subtract the lock from the free amount (but don't go below 0)
|
619
600
|
return this.#format(util.BigMath.max(this.free.planck - excludeAmount, BigInt("0")));
|
620
601
|
}
|
621
|
-
}
|
622
|
-
|
602
|
+
}
|
603
|
+
class BalanceFormatter {
|
623
604
|
#planck;
|
624
605
|
#decimals;
|
625
606
|
#fiatRatios;
|
@@ -627,7 +608,6 @@ let BalanceFormatter = (_dec11 = typescriptMemoize.Memoize(), (_class3 = class B
|
|
627
608
|
this.#planck = typeof planck === "bigint" ? planck.toString() : planck ?? "0";
|
628
609
|
this.#decimals = decimals || 0;
|
629
610
|
this.#fiatRatios = fiatRatios || null;
|
630
|
-
this.fiat = memoize__default["default"](this.fiat);
|
631
611
|
}
|
632
612
|
toJSON = () => this.#planck;
|
633
613
|
get planck() {
|
@@ -642,14 +622,13 @@ let BalanceFormatter = (_dec11 = typescriptMemoize.Memoize(), (_class3 = class B
|
|
642
622
|
if (!ratio) return null;
|
643
623
|
return parseFloat(this.tokens) * ratio;
|
644
624
|
}
|
645
|
-
}
|
646
|
-
|
625
|
+
}
|
626
|
+
class FiatSumBalancesFormatter {
|
647
627
|
#balances;
|
648
628
|
#currency;
|
649
629
|
constructor(balances, currency) {
|
650
630
|
this.#balances = balances;
|
651
631
|
this.#currency = currency;
|
652
|
-
this.#sum = memoize__default["default"](this.#sum);
|
653
632
|
}
|
654
633
|
#sum = balanceField => {
|
655
634
|
// a function to get a fiat amount from a balance
|
@@ -696,12 +675,11 @@ let FiatSumBalancesFormatter = (_dec12 = typescriptMemoize.Memoize(), _dec13 = t
|
|
696
675
|
get feePayable() {
|
697
676
|
return this.#sum("feePayable");
|
698
677
|
}
|
699
|
-
}
|
678
|
+
}
|
700
679
|
class SumBalancesFormatter {
|
701
680
|
#balances;
|
702
681
|
constructor(balances) {
|
703
682
|
this.#balances = balances;
|
704
|
-
this.fiat = memoize__default["default"](this.fiat);
|
705
683
|
}
|
706
684
|
fiat(currency) {
|
707
685
|
return new FiatSumBalancesFormatter(this.#balances, currency);
|
@@ -717,6 +695,7 @@ exports.StorageHelper = StorageHelper;
|
|
717
695
|
exports.SumBalancesFormatter = SumBalancesFormatter;
|
718
696
|
exports.TalismanBalancesDatabase = TalismanBalancesDatabase;
|
719
697
|
exports.balances = balances;
|
698
|
+
exports.createTypeRegistryCache = createTypeRegistryCache;
|
720
699
|
exports.db = db;
|
721
700
|
exports.excludeFromFeePayableLocks = excludeFromFeePayableLocks;
|
722
701
|
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.0.0-pr524-
|
62
|
+
version: "0.0.0-pr524-20230310034933",
|
66
63
|
author: "Talisman",
|
67
64
|
homepage: "https://talisman.xyz",
|
68
65
|
license: "UNLICENSED",
|
@@ -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.3"
|
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,16 +123,34 @@ 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
152
|
var _balance$token;
|
140
|
-
// TODO implement a mirrorOf property, which should be set from chaindata
|
153
|
+
// TODO: implement a mirrorOf property, which should be set from chaindata
|
141
154
|
const mirrorOf = (_balance$token = balance.token) === null || _balance$token === void 0 ? void 0 : _balance$token.mirrorOf;
|
142
155
|
return !mirrorOf || !balances.find(b => b.tokenId === mirrorOf);
|
143
156
|
};
|
@@ -224,30 +237,6 @@ class StorageHelper {
|
|
224
237
|
}
|
225
238
|
}
|
226
239
|
|
227
|
-
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
|
228
|
-
var desc = {};
|
229
|
-
Object.keys(descriptor).forEach(function (key) {
|
230
|
-
desc[key] = descriptor[key];
|
231
|
-
});
|
232
|
-
desc.enumerable = !!desc.enumerable;
|
233
|
-
desc.configurable = !!desc.configurable;
|
234
|
-
if ('value' in desc || desc.initializer) {
|
235
|
-
desc.writable = true;
|
236
|
-
}
|
237
|
-
desc = decorators.slice().reverse().reduce(function (desc, decorator) {
|
238
|
-
return decorator(target, property, desc) || desc;
|
239
|
-
}, desc);
|
240
|
-
if (context && desc.initializer !== void 0) {
|
241
|
-
desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
|
242
|
-
desc.initializer = undefined;
|
243
|
-
}
|
244
|
-
if (desc.initializer === void 0) {
|
245
|
-
Object.defineProperty(target, property, desc);
|
246
|
-
desc = null;
|
247
|
-
}
|
248
|
-
return desc;
|
249
|
-
}
|
250
|
-
|
251
240
|
function excludeFromTransferableAmount(locks) {
|
252
241
|
if (typeof locks === "string") return BigInt(locks);
|
253
242
|
if (!Array.isArray(locks)) locks = [locks];
|
@@ -269,8 +258,6 @@ function includeInTotalExtraAmount(extra) {
|
|
269
258
|
|
270
259
|
/** Used by plugins to help define their custom `BalanceType` */
|
271
260
|
|
272
|
-
var _dec, _dec2, _dec3, _class, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _class2, _dec11, _class3, _dec12, _dec13, _dec14, _dec15, _dec16, _dec17, _dec18, _class4;
|
273
|
-
|
274
261
|
/**
|
275
262
|
* Have the importing library define its Token and BalanceJson enums (as a sum type of all plugins) and pass them into some
|
276
263
|
* internal global typescript context, which is then picked up on by this module.
|
@@ -281,12 +268,12 @@ var _dec, _dec2, _dec3, _class, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10
|
|
281
268
|
/**
|
282
269
|
* A collection of balances.
|
283
270
|
*/
|
284
|
-
|
271
|
+
class Balances {
|
285
272
|
//
|
286
273
|
// Properties
|
287
274
|
//
|
288
275
|
|
289
|
-
#balances =
|
276
|
+
#balances = [];
|
290
277
|
|
291
278
|
//
|
292
279
|
// Methods
|
@@ -294,7 +281,7 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
|
|
294
281
|
|
295
282
|
constructor(balances, hydrate) {
|
296
283
|
// handle Balances (convert to Balance[])
|
297
|
-
if (balances instanceof Balances) return new Balances(
|
284
|
+
if (balances instanceof Balances) return new Balances(balances.each, hydrate);
|
298
285
|
|
299
286
|
// handle Balance (convert to Balance[])
|
300
287
|
if (balances instanceof Balance) return new Balances([balances], hydrate);
|
@@ -309,19 +296,19 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
|
|
309
296
|
if (!isArrayOf(balances, Balance)) return new Balances(balances.map(storage => new Balance(storage)), hydrate);
|
310
297
|
|
311
298
|
// handle Balance[]
|
312
|
-
this.#balances =
|
299
|
+
this.#balances = balances;
|
313
300
|
if (hydrate !== undefined) this.hydrate(hydrate);
|
314
301
|
}
|
315
302
|
|
316
303
|
/**
|
317
304
|
* Calling toJSON on a collection of balances will return the underlying BalanceJsonList.
|
318
305
|
*/
|
319
|
-
toJSON = () => Object.fromEntries(
|
306
|
+
toJSON = () => Object.fromEntries(this.#balances.map(balance => {
|
320
307
|
try {
|
321
|
-
return [id, balance.toJSON()];
|
308
|
+
return [balance.id, balance.toJSON()];
|
322
309
|
} catch (error) {
|
323
310
|
log.error("Failed to convert balance to JSON", error, {
|
324
|
-
id,
|
311
|
+
id: balance.id,
|
325
312
|
balance
|
326
313
|
});
|
327
314
|
return null;
|
@@ -341,7 +328,7 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
|
|
341
328
|
*/
|
342
329
|
[Symbol.iterator] = () =>
|
343
330
|
// Create an array of the balances in this collection and return the result of its iterator.
|
344
|
-
|
331
|
+
this.#balances[Symbol.iterator]();
|
345
332
|
|
346
333
|
/**
|
347
334
|
* Hydrates all balances in this collection.
|
@@ -349,7 +336,7 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
|
|
349
336
|
* @param sources - The sources to hydrate from.
|
350
337
|
*/
|
351
338
|
hydrate = sources => {
|
352
|
-
|
339
|
+
this.#balances.map(balance => balance.hydrate(sources));
|
353
340
|
};
|
354
341
|
|
355
342
|
/**
|
@@ -358,7 +345,7 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
|
|
358
345
|
* @param id - The id of the balance to fetch.
|
359
346
|
* @returns The balance if one exists, or none.
|
360
347
|
*/
|
361
|
-
get = id => this.#balances
|
348
|
+
get = id => this.#balances.find(balance => balance.id === id) ?? null;
|
362
349
|
|
363
350
|
/**
|
364
351
|
* Retrieve balances from this collection by search query.
|
@@ -391,10 +378,8 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
|
|
391
378
|
if (balances instanceof Balance) return this.add(new Balances(balances));
|
392
379
|
|
393
380
|
// merge balances
|
394
|
-
const mergedBalances =
|
395
|
-
|
396
|
-
};
|
397
|
-
[...balances].forEach(balance => mergedBalances[balance.id] = balance);
|
381
|
+
const mergedBalances = Object.fromEntries(this.#balances.map(balance => [balance.id, balance]));
|
382
|
+
balances.each.forEach(balance => mergedBalances[balance.id] = balance);
|
398
383
|
|
399
384
|
// return new balances
|
400
385
|
return new Balances(Object.values(mergedBalances));
|
@@ -412,18 +397,16 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
|
|
412
397
|
// handle single id
|
413
398
|
if (!Array.isArray(ids)) return this.remove([ids]);
|
414
399
|
|
415
|
-
// merge balances
|
416
|
-
|
417
|
-
...this.#balances
|
418
|
-
};
|
419
|
-
ids.forEach(id => delete removedBalances[id]);
|
420
|
-
|
421
|
-
// return new balances
|
422
|
-
return new Balances(Object.values(removedBalances));
|
400
|
+
// merge and return new balances
|
401
|
+
return new Balances(this.#balances.filter(balance => !ids.includes(balance.id)));
|
423
402
|
};
|
424
403
|
|
425
404
|
// TODO: Add some more useful aggregator methods
|
426
405
|
|
406
|
+
get each() {
|
407
|
+
return [...this];
|
408
|
+
}
|
409
|
+
|
427
410
|
/**
|
428
411
|
* Get an array of balances in this collection, sorted by chain sortIndex.
|
429
412
|
*
|
@@ -432,7 +415,7 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
|
|
432
415
|
get sorted() {
|
433
416
|
return [...this].sort((a, b) => {
|
434
417
|
var _ref, _ref2;
|
435
|
-
return (((_ref = a.chain || a.evmNetwork) === null || _ref === void 0 ? void 0 : _ref.sortIndex)
|
418
|
+
return (((_ref = a.chain || a.evmNetwork) === null || _ref === void 0 ? void 0 : _ref.sortIndex) ?? Number.MAX_SAFE_INTEGER) - (((_ref2 = b.chain || b.evmNetwork) === null || _ref2 === void 0 ? void 0 : _ref2.sortIndex) ?? Number.MAX_SAFE_INTEGER);
|
436
419
|
});
|
437
420
|
}
|
438
421
|
|
@@ -456,12 +439,12 @@ let Balances = (_dec = Memoize(), _dec2 = Memoize(), _dec3 = Memoize(), (_class
|
|
456
439
|
get sum() {
|
457
440
|
return new SumBalancesFormatter(this);
|
458
441
|
}
|
459
|
-
}
|
442
|
+
}
|
460
443
|
|
461
444
|
/**
|
462
445
|
* An individual balance.
|
463
446
|
*/
|
464
|
-
|
447
|
+
class Balance {
|
465
448
|
//
|
466
449
|
// Properties
|
467
450
|
//
|
@@ -475,7 +458,6 @@ let Balance = (_dec4 = Memoize(), _dec5 = Memoize(), _dec6 = Memoize(), _dec7 =
|
|
475
458
|
//
|
476
459
|
|
477
460
|
constructor(storage, hydrate) {
|
478
|
-
this.#format = memoize(this.#format);
|
479
461
|
this.#storage = storage;
|
480
462
|
if (hydrate !== undefined) this.hydrate(hydrate);
|
481
463
|
}
|
@@ -609,8 +591,8 @@ let Balance = (_dec4 = Memoize(), _dec5 = Memoize(), _dec6 = Memoize(), _dec7 =
|
|
609
591
|
// subtract the lock from the free amount (but don't go below 0)
|
610
592
|
return this.#format(BigMath.max(this.free.planck - excludeAmount, BigInt("0")));
|
611
593
|
}
|
612
|
-
}
|
613
|
-
|
594
|
+
}
|
595
|
+
class BalanceFormatter {
|
614
596
|
#planck;
|
615
597
|
#decimals;
|
616
598
|
#fiatRatios;
|
@@ -618,7 +600,6 @@ let BalanceFormatter = (_dec11 = Memoize(), (_class3 = class BalanceFormatter {
|
|
618
600
|
this.#planck = typeof planck === "bigint" ? planck.toString() : planck ?? "0";
|
619
601
|
this.#decimals = decimals || 0;
|
620
602
|
this.#fiatRatios = fiatRatios || null;
|
621
|
-
this.fiat = memoize(this.fiat);
|
622
603
|
}
|
623
604
|
toJSON = () => this.#planck;
|
624
605
|
get planck() {
|
@@ -633,14 +614,13 @@ let BalanceFormatter = (_dec11 = Memoize(), (_class3 = class BalanceFormatter {
|
|
633
614
|
if (!ratio) return null;
|
634
615
|
return parseFloat(this.tokens) * ratio;
|
635
616
|
}
|
636
|
-
}
|
637
|
-
|
617
|
+
}
|
618
|
+
class FiatSumBalancesFormatter {
|
638
619
|
#balances;
|
639
620
|
#currency;
|
640
621
|
constructor(balances, currency) {
|
641
622
|
this.#balances = balances;
|
642
623
|
this.#currency = currency;
|
643
|
-
this.#sum = memoize(this.#sum);
|
644
624
|
}
|
645
625
|
#sum = balanceField => {
|
646
626
|
// a function to get a fiat amount from a balance
|
@@ -687,16 +667,15 @@ let FiatSumBalancesFormatter = (_dec12 = Memoize(), _dec13 = Memoize(), _dec14 =
|
|
687
667
|
get feePayable() {
|
688
668
|
return this.#sum("feePayable");
|
689
669
|
}
|
690
|
-
}
|
670
|
+
}
|
691
671
|
class SumBalancesFormatter {
|
692
672
|
#balances;
|
693
673
|
constructor(balances) {
|
694
674
|
this.#balances = balances;
|
695
|
-
this.fiat = memoize(this.fiat);
|
696
675
|
}
|
697
676
|
fiat(currency) {
|
698
677
|
return new FiatSumBalancesFormatter(this.#balances, currency);
|
699
678
|
}
|
700
679
|
}
|
701
680
|
|
702
|
-
export { Balance, BalanceFormatter, Balances, DefaultBalanceModule, FiatSumBalancesFormatter, StorageHelper, SumBalancesFormatter, TalismanBalancesDatabase, balances, db, excludeFromFeePayableLocks, excludeFromTransferableAmount, filterMirrorTokens, includeInTotalExtraAmount };
|
681
|
+
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.0.0-pr524-
|
3
|
+
"version": "0.0.0-pr524-20230310034933",
|
4
4
|
"author": "Talisman",
|
5
5
|
"homepage": "https://talisman.xyz",
|
6
6
|
"license": "UNLICENSED",
|
@@ -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.0.0-pr524-
|
31
|
-
"@talismn/chain-connector-evm": "^0.0.0-pr524-
|
32
|
-
"@talismn/chaindata-provider": "^0.0.0-pr524-
|
33
|
-
"@talismn/token-rates": "^0.0.0-pr524-
|
30
|
+
"@talismn/chain-connector": "^0.0.0-pr524-20230310034933",
|
31
|
+
"@talismn/chain-connector-evm": "^0.0.0-pr524-20230310034933",
|
32
|
+
"@talismn/chaindata-provider": "^0.0.0-pr524-20230310034933",
|
33
|
+
"@talismn/token-rates": "^0.0.0-pr524-20230310034933",
|
34
34
|
"@talismn/util": "^0.1.7",
|
35
35
|
"anylogger": "^1.0.11",
|
36
|
-
"dexie": "^3.2.3"
|
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",
|