@talismn/balances 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # @talismn/balances
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - a63dbb3: exclude mirror tokens in sums
8
+
9
+ ### Patch Changes
10
+
11
+ - 4aa691d: feat: new balance modules
12
+ - Updated dependencies [4aa691d]
13
+ - Updated dependencies [cd6a684]
14
+ - Updated dependencies [a63dbb3]
15
+ - @talismn/chain-connector-evm@0.4.0
16
+ - @talismn/chaindata-provider@0.2.1
17
+ - @talismn/chain-connector@0.2.1
18
+ - @talismn/token-rates@0.1.12
19
+ - @talismn/util@0.1.5
20
+
21
+ ## 0.2.3
22
+
3
23
  ## 0.2.2
4
24
 
5
25
  ### Patch Changes
@@ -12,13 +12,13 @@ export declare type ExtendableModuleConfig = Record<string, unknown> | undefined
12
12
  export declare type DefaultModuleConfig = undefined;
13
13
  interface BalanceModuleSubstrate<TModuleType extends string, TTokenType extends ExtendableTokenType, TChainMeta extends ExtendableChainMeta = DefaultChainMeta, TModuleConfig extends ExtendableModuleConfig = DefaultModuleConfig> extends BalanceModuleCommon<TModuleType, TTokenType> {
14
14
  /** Pre-processes any substrate chain metadata required by this module ahead of time */
15
- fetchSubstrateChainMeta(chainConnector: ChainConnector, chaindataProvider: ChaindataProvider, chainId: ChainId): Promise<TChainMeta | null>;
15
+ fetchSubstrateChainMeta(chainConnector: ChainConnector, chaindataProvider: ChaindataProvider, chainId: ChainId, moduleConfig: TModuleConfig | undefined): Promise<TChainMeta | null>;
16
16
  /** Detects which tokens are available on a given substrate chain */
17
17
  fetchSubstrateChainTokens(chainConnector: ChainConnector, chaindataProvider: ChaindataProvider, chainId: ChainId, chainMeta: TChainMeta, moduleConfig: TModuleConfig | undefined): Promise<Record<TTokenType["id"], TTokenType>>;
18
18
  }
19
19
  interface BalanceModuleEvm<TModuleType extends string, TTokenType extends ExtendableTokenType, TChainMeta extends ExtendableChainMeta = DefaultChainMeta, TModuleConfig extends ExtendableModuleConfig = DefaultModuleConfig> extends BalanceModuleCommon<TModuleType, TTokenType> {
20
20
  /** Pre-processes any evm chain metadata required by this module ahead of time */
21
- fetchEvmChainMeta(chainConnector: ChainConnectorEvm, chaindataProvider: ChaindataProvider, chainId: ChainId): Promise<TChainMeta | null>;
21
+ fetchEvmChainMeta(chainConnector: ChainConnectorEvm, chaindataProvider: ChaindataProvider, chainId: ChainId, moduleConfig: TModuleConfig | undefined): Promise<TChainMeta | null>;
22
22
  /** Detects which tokens are available on a given evm chain */
23
23
  fetchEvmChainTokens(chainConnector: ChainConnectorEvm, chaindataProvider: ChaindataProvider, chainId: ChainId, chainMeta: TChainMeta, moduleConfig: TModuleConfig | undefined): Promise<Record<TTokenType["id"], TTokenType>>;
24
24
  }
package/dist/helpers.d.ts CHANGED
@@ -1,8 +1,9 @@
1
+ import { TypeRegistry } from "@polkadot/types";
1
2
  import { ChainConnector } from "@talismn/chain-connector";
2
3
  import { ChainConnectorEvm } from "@talismn/chain-connector-evm";
3
4
  import { ChaindataProvider } from "@talismn/chaindata-provider";
4
5
  import { BalanceModule, DefaultChainMeta, DefaultModuleConfig, ExtendableChainMeta, ExtendableModuleConfig, ExtendableTokenType } from "./BalanceModule";
5
- import { AddressesByToken, Balances, SubscriptionCallback, UnsubscribeFn } from "./types";
6
+ import { AddressesByToken, Balance, Balances, SubscriptionCallback, UnsubscribeFn } from "./types";
6
7
  /**
7
8
  * Wraps a BalanceModule's fetch/subscribe methods with a single `balances` method.
8
9
  * This `balances` method will subscribe if a callback parameter is provided, or otherwise fetch.
@@ -15,3 +16,18 @@ export declare function balances<TModuleType extends string, TTokenType extends
15
16
  substrate?: ChainConnector;
16
17
  evm?: ChainConnectorEvm;
17
18
  }, chaindataProvider: ChaindataProvider, addressesByToken: AddressesByToken<TTokenType>, callback: SubscriptionCallback<Balances>): Promise<UnsubscribeFn>;
19
+ export declare const filterMirrorTokens: (balance: Balance, i: number, balances: Balance[]) => boolean;
20
+ /**
21
+ * Used by a variety of balance modules to help encode and decode substrate state calls.
22
+ */
23
+ export declare class StorageHelper {
24
+ #private;
25
+ tags: any;
26
+ constructor(registry: TypeRegistry, module: string, method: string, ...parameters: any[]);
27
+ get stateKey(): `0x${string}` | undefined;
28
+ get module(): string;
29
+ get method(): string;
30
+ get parameters(): any[];
31
+ tag(tags: any): this;
32
+ decode(input?: string | null): import("@polkadot/types-codec/types").Codec | undefined;
33
+ }
package/dist/helpers.js CHANGED
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.balances = void 0;
6
+ exports.StorageHelper = exports.filterMirrorTokens = exports.balances = void 0;
7
+ const types_1 = require("@polkadot/types");
8
+ const log_1 = __importDefault(require("./log"));
4
9
  async function balances(balanceModule, chainConnectors, chaindataProvider, addressesByToken, callback) {
5
10
  // subscription request
6
11
  if (callback !== undefined)
@@ -9,3 +14,86 @@ async function balances(balanceModule, chainConnectors, chaindataProvider, addre
9
14
  return await balanceModule.fetchBalances(chainConnectors, chaindataProvider, addressesByToken);
10
15
  }
11
16
  exports.balances = balances;
17
+ const filterMirrorTokens = (balance, i, balances) => {
18
+ // TODO implement a mirrorOf property, which should be set from chaindata
19
+ const mirrorOf = balance.token?.mirrorOf;
20
+ return !mirrorOf || !balances.find((b) => b.tokenId === mirrorOf);
21
+ };
22
+ exports.filterMirrorTokens = filterMirrorTokens;
23
+ /**
24
+ * Used by a variety of balance modules to help encode and decode substrate state calls.
25
+ */
26
+ class StorageHelper {
27
+ #registry;
28
+ #storageKey;
29
+ #module;
30
+ #method;
31
+ #parameters;
32
+ tags = null;
33
+ constructor(registry, module, method, ...parameters) {
34
+ this.#registry = registry;
35
+ this.#module = module;
36
+ this.#method = method;
37
+ this.#parameters = parameters;
38
+ const _metadataVersion = 0; // is not used inside the decorateStorage function
39
+ let query;
40
+ try {
41
+ query = (0, types_1.decorateStorage)(registry, registry.metadata, _metadataVersion);
42
+ }
43
+ catch (error) {
44
+ log_1.default.debug(`Failed to decorate storage: ${error.message}`);
45
+ this.#storageKey = null;
46
+ }
47
+ try {
48
+ if (!query)
49
+ throw new Error(`decoratedStorage unavailable`);
50
+ this.#storageKey = new types_1.StorageKey(registry, parameters ? [query[module][method], parameters] : query[module][method]);
51
+ }
52
+ catch (error) {
53
+ log_1.default.debug(`Failed to create storageKey ${module || "unknown"}.${method || "unknown"}: ${error.message}`);
54
+ this.#storageKey = null;
55
+ }
56
+ }
57
+ get stateKey() {
58
+ return this.#storageKey?.toHex();
59
+ }
60
+ get module() {
61
+ return this.#module;
62
+ }
63
+ get method() {
64
+ return this.#method;
65
+ }
66
+ get parameters() {
67
+ return this.#parameters;
68
+ }
69
+ tag(tags) {
70
+ this.tags = tags;
71
+ return this;
72
+ }
73
+ decode(input) {
74
+ if (!this.#storageKey)
75
+ return;
76
+ return this.#decodeStorageScaleResponse(this.#registry, this.#storageKey, input);
77
+ }
78
+ #decodeStorageScaleResponse(typeRegistry, storageKey, input) {
79
+ if (input === undefined || input === null)
80
+ return;
81
+ const type = storageKey.outputType || "Raw";
82
+ const meta = storageKey.meta || {
83
+ fallback: undefined,
84
+ modifier: { isOptional: true },
85
+ type: { asMap: { linked: { isTrue: false } }, isMap: false },
86
+ };
87
+ try {
88
+ return typeRegistry.createTypeUnsafe(type, [
89
+ meta.modifier.isOptional
90
+ ? typeRegistry.createTypeUnsafe(type, [input], { isPedantic: true })
91
+ : input,
92
+ ], { isOptional: meta.modifier.isOptional, isPedantic: !meta.modifier.isOptional });
93
+ }
94
+ catch (error) {
95
+ throw new Error(`Unable to decode storage ${storageKey.section || "unknown"}.${storageKey.method || "unknown"}: ${error.message}`);
96
+ }
97
+ }
98
+ }
99
+ exports.StorageHelper = StorageHelper;
@@ -13,6 +13,7 @@ exports.SumBalancesFormatter = exports.FiatSumBalancesFormatter = exports.Balanc
13
13
  const util_1 = require("@talismn/util");
14
14
  const memoize_1 = __importDefault(require("lodash/memoize"));
15
15
  const typescript_memoize_1 = require("typescript-memoize");
16
+ const helpers_1 = require("../helpers");
16
17
  const log_1 = __importDefault(require("../log"));
17
18
  const balancetypes_1 = require("./balancetypes");
18
19
  /**
@@ -396,7 +397,7 @@ class FiatSumBalancesFormatter {
396
397
  const fiat = (balance) => balance[balanceField].fiat(this.#currency) || 0;
397
398
  // a function to add two amounts
398
399
  const sum = (a, b) => a + b;
399
- return [...this.#balances].reduce((total, balance) => sum(
400
+ return [...this.#balances].filter(helpers_1.filterMirrorTokens).reduce((total, balance) => sum(
400
401
  // add the total amount...
401
402
  total,
402
403
  // ...to the fiat amount of each balance
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@talismn/balances",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "author": "Talisman",
5
5
  "homepage": "https://talisman.xyz",
6
6
  "license": "UNLICENSED",
@@ -31,11 +31,11 @@
31
31
  "clean": "rm -rf dist && rm -rf .turbo rm -rf node_modules"
32
32
  },
33
33
  "dependencies": {
34
- "@talismn/chain-connector": "^0.2.0",
35
- "@talismn/chain-connector-evm": "^0.3.0",
36
- "@talismn/chaindata-provider": "^0.2.0",
37
- "@talismn/token-rates": "^0.1.11",
38
- "@talismn/util": "^0.1.4",
34
+ "@talismn/chain-connector": "^0.2.1",
35
+ "@talismn/chain-connector-evm": "^0.4.0",
36
+ "@talismn/chaindata-provider": "^0.2.1",
37
+ "@talismn/token-rates": "^0.1.12",
38
+ "@talismn/util": "^0.1.5",
39
39
  "lodash": "^4.17.21",
40
40
  "typescript-memoize": "^1.1.0"
41
41
  },