@talismn/balances-react 0.1.17 → 0.1.19

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,24 @@
1
1
  # @talismn/balances-react
2
2
 
3
+ ## 0.1.19
4
+
5
+ ### Patch Changes
6
+
7
+ - @talismn/balances@0.1.19
8
+
9
+ ## 0.1.18
10
+
11
+ ### Patch Changes
12
+
13
+ - fix: a variety of improvements from the wallet integration
14
+ - Updated dependencies
15
+ - @talismn/balances@0.1.18
16
+ - @talismn/chain-connector@0.1.10
17
+ - @talismn/chain-connector-evm@0.1.10
18
+ - @talismn/chaindata-provider@0.1.10
19
+ - @talismn/chaindata-provider-extension@0.1.10
20
+ - @talismn/token-rates@0.1.10
21
+
3
22
  ## 0.1.17
4
23
 
5
24
  ### Patch Changes
@@ -1,2 +1,3 @@
1
1
  export * from "./useBalances";
2
2
  export * from "./useChaindata";
3
+ export * from "./useTokenRates";
@@ -1,2 +1,3 @@
1
1
  export * from "./useBalances";
2
2
  export * from "./useChaindata";
3
+ export * from "./useTokenRates";
@@ -1,8 +1,3 @@
1
- import { AddressesByToken, BalanceJson, BalanceModule, Balances } from "@talismn/balances";
1
+ import { AddressesByToken, BalanceModule, Balances } from "@talismn/balances";
2
2
  import { ChaindataProvider, Token } from "@talismn/chaindata-provider";
3
- import { Dexie } from "dexie";
4
3
  export declare function useBalances(balanceModules: Array<BalanceModule<any, any, any, any>>, chaindataProvider: ChaindataProvider | null, addressesByToken: AddressesByToken<Token> | null): Balances | undefined;
5
- export declare class BalancesDatabase extends Dexie {
6
- balances: Dexie.Table<BalanceJson, string>;
7
- constructor();
8
- }
@@ -8,15 +8,23 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { Balances, balances as balancesFn, } from "@talismn/balances";
11
+ import { db } from "@talismn/balances";
11
12
  import { ChainConnector } from "@talismn/chain-connector";
12
13
  import { ChainConnectorEvm } from "@talismn/chain-connector-evm";
13
- import { Dexie } from "dexie";
14
14
  import { useLiveQuery } from "dexie-react-hooks";
15
15
  import { useEffect, useState } from "react";
16
16
  import { useDebounce } from "react-use";
17
17
  import log from "../log";
18
18
  import { useChains, useEvmNetworks, useTokens } from "./useChaindata";
19
19
  import { useTokenRates } from "./useTokenRates";
20
+ // TODO: Add the equivalent functionalty of `useDbCache` directly to this library.
21
+ //
22
+ // How it will work:
23
+ //
24
+ // useChains/useEvmNetworks/useTokens/useTokenRates will all make use of a
25
+ // useCachedDb hook, which internally subscribes to all of the db tables
26
+ // for everything, and then filters the subscribed data based on what params
27
+ // the caller of useChains/useTokens/etc has provided.
20
28
  export function useBalances(
21
29
  // TODO: Make this array of BalanceModules more type-safe
22
30
  balanceModules, chaindataProvider, addressesByToken) {
@@ -28,17 +36,24 @@ balanceModules, chaindataProvider, addressesByToken) {
28
36
  const balances = useLiveQuery(() => __awaiter(this, void 0, void 0, function* () {
29
37
  return new Balances(yield db.balances
30
38
  .filter((balance) => {
39
+ // check that this balance is included in our queried balance modules
31
40
  if (!balanceModules.map(({ type }) => type).includes(balance.source))
32
41
  return false;
42
+ // check that our query includes some tokens and addresses
33
43
  if (!addressesByToken)
34
44
  return false;
45
+ // check that this balance is included in our queried tokens
35
46
  if (!Object.keys(addressesByToken).includes(balance.tokenId))
36
47
  return false;
48
+ // check that this balance is included in our queried addresses for this token
37
49
  if (!addressesByToken[balance.tokenId].includes(balance.address))
38
50
  return false;
51
+ // keep this balance
39
52
  return true;
40
53
  })
41
- .toArray(), { chains, evmNetworks, tokens, tokenRates });
54
+ .toArray(),
55
+ // hydrate balance chains, evmNetworks, tokens and tokenRates
56
+ { chains, evmNetworks, tokens, tokenRates });
42
57
  }), [balanceModules, addressesByToken, chains, evmNetworks, tokens, tokenRates]);
43
58
  // debounce every 100ms to prevent hammering UI with updates
44
59
  const [debouncedBalances, setDebouncedBalances] = useState(balances);
@@ -105,7 +120,7 @@ balanceModules, chaindataProvider, addressesByToken) {
105
120
  }
106
121
  };
107
122
  const chainConnector = useChainConnector(chaindataProvider);
108
- const chainConnectorEvm = useChainConnectorEvm();
123
+ const chainConnectorEvm = useChainConnectorEvm(chaindataProvider);
109
124
  const tokens = useTokens(chaindataProvider);
110
125
  useEffect(() => {
111
126
  if (chainConnector === null)
@@ -142,26 +157,13 @@ function useChainConnector(chaindataProvider) {
142
157
  }, [chaindataProvider]);
143
158
  return chainConnector;
144
159
  }
145
- function useChainConnectorEvm() {
160
+ // TODO: Allow advanced users of this library to provide their own chain connector
161
+ function useChainConnectorEvm(chaindataProvider) {
146
162
  const [chainConnectorEvm, setChainConnectorEvm] = useState(null);
147
163
  useEffect(() => {
148
- setChainConnectorEvm(new ChainConnectorEvm());
164
+ if (chaindataProvider === null)
165
+ return;
166
+ setChainConnectorEvm(new ChainConnectorEvm(chaindataProvider));
149
167
  }, []);
150
168
  return chainConnectorEvm;
151
169
  }
152
- export class BalancesDatabase extends Dexie {
153
- constructor() {
154
- super("Balances");
155
- // https://dexie.org/docs/Tutorial/Design#database-versioning
156
- this.version(1).stores({
157
- // You only need to specify properties that you wish to index.
158
- // The object store will allow any properties on your stored objects but you can only query them by indexed properties
159
- // https://dexie.org/docs/API-Reference#declare-database
160
- //
161
- // Never index properties containing images, movies or large (huge) strings. Store them in IndexedDB, yes! but just don’t index them!
162
- // https://dexie.org/docs/Version/Version.stores()#warning
163
- balances: "id, source, status, address, tokenId",
164
- });
165
- }
166
- }
167
- const db = new BalancesDatabase();
@@ -1,3 +1,3 @@
1
- import { TokenList } from "@talismn/chaindata-provider";
1
+ import { IToken, TokenId } from "@talismn/chaindata-provider";
2
2
  import { TokenRatesList } from "@talismn/token-rates";
3
- export declare function useTokenRates(tokens?: TokenList): TokenRatesList;
3
+ export declare function useTokenRates(tokens?: Record<TokenId, IToken>): TokenRatesList;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@talismn/balances-react",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
4
4
  "author": "Talisman",
5
5
  "homepage": "https://talisman.xyz",
6
6
  "license": "UNLICENSED",
@@ -30,11 +30,12 @@
30
30
  "clean": "rm -rf dist && rm -rf .turbo rm -rf node_modules"
31
31
  },
32
32
  "dependencies": {
33
- "@talismn/balances": "^0.1.17",
34
- "@talismn/chain-connector": "^0.1.9",
35
- "@talismn/chain-connector-evm": "^0.1.9",
36
- "@talismn/chaindata-provider": "^0.1.9",
37
- "@talismn/chaindata-provider-extension": "^0.1.9",
33
+ "@talismn/balances": "^0.1.19",
34
+ "@talismn/chain-connector": "^0.1.10",
35
+ "@talismn/chain-connector-evm": "^0.1.10",
36
+ "@talismn/chaindata-provider": "^0.1.10",
37
+ "@talismn/chaindata-provider-extension": "^0.1.10",
38
+ "@talismn/token-rates": "^0.1.10",
38
39
  "anylogger": "^1.0.11",
39
40
  "dexie": "^3.2.2",
40
41
  "dexie-react-hooks": "^1.1.1",