@talismn/balances-react 0.3.0 → 0.3.1

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.
@@ -1,112 +0,0 @@
1
- import { ChaindataProviderExtension } from "@talismn/chaindata-provider-extension";
2
- import { useEffect, useState } from "react";
3
- import log from "../log";
4
- // TODO: Allow user to call useChaindata from multiple places
5
- export function useChaindata() {
6
- const [chaindataProvider, setChaindataProvider] = useState(null);
7
- // this number is incremented each time the chaindataProvider has fetched new data
8
- const [generation, setGeneration] = useState(0);
9
- useEffect(() => {
10
- const chaindataProvider = new ChaindataProviderExtension();
11
- let shouldHydrate = true;
12
- const timer = 300_000; // 300_000ms = 300s = 5 minutes
13
- const hydrate = async () => {
14
- if (!shouldHydrate)
15
- return;
16
- try {
17
- const updated = await chaindataProvider.hydrate();
18
- if (updated)
19
- setGeneration((generation) => (generation + 1) % Number.MAX_SAFE_INTEGER);
20
- setTimeout(hydrate, timer);
21
- }
22
- catch (error) {
23
- const retryTimeout = 5_000; // 5_000ms = 5 seconds
24
- log.error(`Failed to fetch chaindata, retrying in ${Math.round(retryTimeout / 1000)} seconds`, error);
25
- setTimeout(hydrate, retryTimeout);
26
- }
27
- };
28
- setChaindataProvider(chaindataProvider);
29
- hydrate();
30
- return () => {
31
- shouldHydrate = false;
32
- };
33
- }, []);
34
- if (chaindataProvider)
35
- chaindataProvider.generation = generation;
36
- return chaindataProvider;
37
- }
38
- export function useChains(chaindata) {
39
- const [chains, setChains] = useState();
40
- useEffect(() => {
41
- if (!chaindata)
42
- return;
43
- const thisGeneration = chaindata.generation;
44
- chaindata.chains().then((chains) => {
45
- if (thisGeneration !== chaindata.generation)
46
- return;
47
- setChains(chains);
48
- });
49
- }, [chaindata?.generation]);
50
- return chains || {};
51
- }
52
- export function useChain(chaindata, chainId) {
53
- const [chain, setChain] = useState();
54
- useEffect(() => {
55
- if (chaindata === null)
56
- return;
57
- if (!chainId)
58
- return;
59
- chaindata.getChain(chainId).then(setChain);
60
- }, [chaindata?.generation]);
61
- return chain;
62
- }
63
- export function useEvmNetworks(chaindata) {
64
- const [evmNetworks, setEvmNetworks] = useState();
65
- useEffect(() => {
66
- if (!chaindata)
67
- return;
68
- const thisGeneration = chaindata.generation;
69
- chaindata.evmNetworks().then((evmNetworks) => {
70
- if (thisGeneration !== chaindata.generation)
71
- return;
72
- setEvmNetworks(evmNetworks);
73
- });
74
- }, [chaindata?.generation]);
75
- return evmNetworks || {};
76
- }
77
- export function useEvmNetwork(chaindata, evmNetworkId) {
78
- const [evmNetwork, setEvmNetwork] = useState();
79
- useEffect(() => {
80
- if (chaindata === null)
81
- return;
82
- if (!evmNetworkId)
83
- return;
84
- chaindata.getEvmNetwork(evmNetworkId).then(setEvmNetwork);
85
- }, [chaindata?.generation]);
86
- return evmNetwork;
87
- }
88
- export function useTokens(chaindata) {
89
- const [tokens, setTokens] = useState();
90
- useEffect(() => {
91
- if (!chaindata)
92
- return;
93
- const thisGeneration = chaindata.generation;
94
- chaindata.tokens().then((tokens) => {
95
- if (thisGeneration !== chaindata.generation)
96
- return;
97
- setTokens(tokens);
98
- });
99
- }, [chaindata?.generation]);
100
- return tokens || {};
101
- }
102
- export function useToken(chaindata, tokenId) {
103
- const [token, setToken] = useState();
104
- useEffect(() => {
105
- if (chaindata === null)
106
- return;
107
- if (!tokenId)
108
- return;
109
- chaindata.getToken(tokenId).then(setToken);
110
- }, [chaindata?.generation]);
111
- return token;
112
- }
@@ -1,22 +0,0 @@
1
- import { fetchTokenRates } from "@talismn/token-rates";
2
- import { useEffect, useRef, useState } from "react";
3
- export function useTokenRates(tokens) {
4
- const generation = useRef(0);
5
- const [tokenRates, setTokenRates] = useState();
6
- useEffect(() => {
7
- if (!tokens)
8
- return;
9
- if (Object.keys(tokens).length < 1)
10
- return;
11
- // when we make a new request, we want to ignore any old requests which haven't yet completed
12
- // otherwise we risk replacing the most recent data with older data
13
- generation.current = (generation.current + 1) % Number.MAX_SAFE_INTEGER;
14
- const thisGeneration = generation.current;
15
- fetchTokenRates(tokens).then((tokenRates) => {
16
- if (thisGeneration !== generation.current)
17
- return;
18
- setTokenRates(tokenRates);
19
- });
20
- }, [tokens]);
21
- return tokenRates || {};
22
- }
package/dist/index.js DELETED
@@ -1 +0,0 @@
1
- export * from "./hooks";
package/dist/log.js DELETED
@@ -1,5 +0,0 @@
1
- import anylogger from "anylogger";
2
- // eslint-disable-next-line @typescript-eslint/no-var-requires
3
- const { name } = require("../package.json");
4
- // import { name } from "../package.json"
5
- export default anylogger(name);