@ledgerhq/live-countervalues-react 0.1.3 → 0.1.4-next.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/.unimportedrc.json +1 -1
- package/CHANGELOG.md +11 -0
- package/lib/index.d.ts +10 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +69 -2
- package/lib/index.js.map +1 -1
- package/lib/portfolio.d.ts +53 -0
- package/lib/portfolio.d.ts.map +1 -0
- package/lib/portfolio.js +94 -0
- package/lib/portfolio.js.map +1 -0
- package/lib-es/index.d.ts +10 -0
- package/lib-es/index.d.ts.map +1 -1
- package/lib-es/index.js +62 -2
- package/lib-es/index.js.map +1 -1
- package/lib-es/portfolio.d.ts +53 -0
- package/lib-es/portfolio.d.ts.map +1 -0
- package/lib-es/portfolio.js +86 -0
- package/lib-es/portfolio.js.map +1 -0
- package/package.json +26 -6
- package/src/index.tsx +91 -1
- package/src/portfolio.tsx +163 -0
package/.unimportedrc.json
CHANGED
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# @ledgerhq/live-countervalues-react
|
|
2
2
|
|
|
3
|
+
## 0.1.4-next.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [[`7d5a724`](https://github.com/LedgerHQ/ledger-live/commit/7d5a724f40079a233b159b5231d69f318327e175), [`b3dfed5`](https://github.com/LedgerHQ/ledger-live/commit/b3dfed54bd8d54e62530cb2db92c3c108b43e0d7), [`7887ad9`](https://github.com/LedgerHQ/ledger-live/commit/7887ad9842e59f6fc567f118f06b3e12bdb9073b), [`5ae6d8f`](https://github.com/LedgerHQ/ledger-live/commit/5ae6d8fb9b868dc01724e84ede2708e7a717c3f2), [`21f5c44`](https://github.com/LedgerHQ/ledger-live/commit/21f5c4438bb542a3891f692f4274ee4c28aa76cd), [`a780777`](https://github.com/LedgerHQ/ledger-live/commit/a780777c13e08c1c3cd66ef5f6deac0fe928a894), [`1aa8ef4`](https://github.com/LedgerHQ/ledger-live/commit/1aa8ef404411c31f6ac4cf09fba453042db8b955), [`3c15515`](https://github.com/LedgerHQ/ledger-live/commit/3c155155f2d45fb85f8900e7e77c1b5ab1c7ad67), [`13bea7c`](https://github.com/LedgerHQ/ledger-live/commit/13bea7ced4b8a7ad40fbc5205e3b58ed8a217982), [`d2f8b26`](https://github.com/LedgerHQ/ledger-live/commit/d2f8b26c99551cba902c07e9c544f3c84d74686c), [`5ca7923`](https://github.com/LedgerHQ/ledger-live/commit/5ca79234ccbe66ce22f998fe3ebd2cdec681499a), [`9ada63a`](https://github.com/LedgerHQ/ledger-live/commit/9ada63a05b2d2518af09a9c07937cf94b5b2ea67), [`5848f9e`](https://github.com/LedgerHQ/ledger-live/commit/5848f9e247f169eb7a4aff322253937214b9efdd), [`c7f072f`](https://github.com/LedgerHQ/ledger-live/commit/c7f072f833a950e230137499d4908b792f6b615f), [`2331bae`](https://github.com/LedgerHQ/ledger-live/commit/2331bae7393f822aa64e5d0ab8f51622b6363b33), [`b72c52b`](https://github.com/LedgerHQ/ledger-live/commit/b72c52b3e4ebbb7aaf2142afbf6a9b9172e7ee04), [`ebb45be`](https://github.com/LedgerHQ/ledger-live/commit/ebb45be56c6b1fdb3c36a8c20a16b41600baa264), [`e1df8bc`](https://github.com/LedgerHQ/ledger-live/commit/e1df8bca348287e94970de90c51e98fa277c5364), [`52d5703`](https://github.com/LedgerHQ/ledger-live/commit/52d57039bb015af2616670db480364a2e5fc9966), [`abb1bbb`](https://github.com/LedgerHQ/ledger-live/commit/abb1bbb09c52a3d08577ba622c6cb0f44aab36c1), [`ba5c49b`](https://github.com/LedgerHQ/ledger-live/commit/ba5c49b82af70a2e459720b9cb124546c406b88b)]:
|
|
8
|
+
- @ledgerhq/types-live@6.46.0-next.0
|
|
9
|
+
- @ledgerhq/coin-framework@0.12.0-next.0
|
|
10
|
+
- @ledgerhq/types-cryptoassets@7.11.0-next.0
|
|
11
|
+
- @ledgerhq/cryptoassets@12.1.0-next.0
|
|
12
|
+
- @ledgerhq/live-countervalues@0.1.4-next.0
|
|
13
|
+
|
|
3
14
|
## 0.1.3
|
|
4
15
|
|
|
5
16
|
### Patch Changes
|
package/lib/index.d.ts
CHANGED
|
@@ -19,10 +19,20 @@ export type Props = {
|
|
|
19
19
|
debounceDelay?: number;
|
|
20
20
|
savedState?: CounterValuesStateRaw;
|
|
21
21
|
};
|
|
22
|
+
export declare function useTrackingPairsForTopCoins(marketcapIds: string[], countervalue: Currency, size: number, startDate: Date | undefined): {
|
|
23
|
+
from: import("@ledgerhq/types-cryptoassets").CryptoCurrency;
|
|
24
|
+
to: Currency;
|
|
25
|
+
startDate: Date;
|
|
26
|
+
}[];
|
|
22
27
|
export declare function useTrackingPairForAccounts(accounts: Account[], countervalue: Currency): TrackingPair[];
|
|
28
|
+
export declare function CountervaluesMarketcap({ children }: {
|
|
29
|
+
children: React.ReactNode;
|
|
30
|
+
}): ReactElement;
|
|
23
31
|
export declare function Countervalues({ children, userSettings, pollInitDelay, autopollInterval, debounceDelay, savedState, }: Props): ReactElement;
|
|
24
32
|
export declare function useCountervaluesPolling(): Polling;
|
|
33
|
+
export declare function useCountervaluesUserSettingsContext(): CountervaluesSettings;
|
|
25
34
|
export declare function useCountervaluesState(): CounterValuesState;
|
|
35
|
+
export declare function useMarketcapIds(): string[];
|
|
26
36
|
export declare function useCountervaluesExport(): CounterValuesStateRaw;
|
|
27
37
|
export declare function useCalculate(query: {
|
|
28
38
|
value: number;
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,KAAK,EAAE,EAQZ,YAAY,EACb,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index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,SAAS,CAAC;CAC7D,CAgCA"}
|
package/lib/index.js
CHANGED
|
@@ -22,13 +22,18 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
25
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.useSendAmount = exports.useCalculateCountervalueCallback = exports.useCalculate = exports.useCountervaluesExport = exports.useCountervaluesState = exports.useCountervaluesPolling = exports.Countervalues = exports.useTrackingPairForAccounts = void 0;
|
|
29
|
+
exports.useSendAmount = exports.useCalculateCountervalueCallback = exports.useCalculate = exports.useCountervaluesExport = exports.useMarketcapIds = exports.useCountervaluesState = exports.useCountervaluesUserSettingsContext = exports.useCountervaluesPolling = exports.Countervalues = exports.CountervaluesMarketcap = exports.useTrackingPairForAccounts = exports.useTrackingPairsForTopCoins = void 0;
|
|
27
30
|
const bignumber_js_1 = require("bignumber.js");
|
|
28
31
|
const react_1 = __importStar(require("react"));
|
|
29
32
|
const helpers_1 = require("@ledgerhq/coin-framework/account/helpers");
|
|
30
33
|
const logic_1 = require("@ledgerhq/live-countervalues/logic");
|
|
34
|
+
const index_1 = __importDefault(require("@ledgerhq/live-countervalues/api/index"));
|
|
31
35
|
const useDebounce_1 = require("@ledgerhq/live-hooks/useDebounce");
|
|
36
|
+
const logs_1 = require("@ledgerhq/logs");
|
|
32
37
|
const CountervaluesPollingContext = (0, react_1.createContext)({
|
|
33
38
|
wipe: () => { },
|
|
34
39
|
poll: () => { },
|
|
@@ -37,13 +42,54 @@ const CountervaluesPollingContext = (0, react_1.createContext)({
|
|
|
37
42
|
pending: false,
|
|
38
43
|
error: null,
|
|
39
44
|
});
|
|
45
|
+
const CountervaluesUserSettingsContext = (0, react_1.createContext)({
|
|
46
|
+
trackingPairs: [],
|
|
47
|
+
autofillGaps: true,
|
|
48
|
+
});
|
|
40
49
|
const CountervaluesContext = (0, react_1.createContext)(logic_1.initialState);
|
|
50
|
+
const CountervaluesMarketcapIdsContext = (0, react_1.createContext)([]);
|
|
41
51
|
function trackingPairsHash(a) {
|
|
42
52
|
return a
|
|
43
53
|
.map(p => `${p.from.ticker}:${p.to.ticker}:${p.startDate.toISOString().slice(0, 10) || ""}`)
|
|
44
54
|
.sort()
|
|
45
55
|
.join("|");
|
|
46
56
|
}
|
|
57
|
+
const marketcapRefresh = 30 * 60000;
|
|
58
|
+
const marketcapRefreshOnError = 60000;
|
|
59
|
+
const initialIds = [];
|
|
60
|
+
/**
|
|
61
|
+
* Internal only. fetch the marketcap and keep it in sync.
|
|
62
|
+
* the data is shared through a context, you can useMarketcapIds to get it.
|
|
63
|
+
*/
|
|
64
|
+
function useMarketcap() {
|
|
65
|
+
const [ids, setIds] = (0, react_1.useState)(initialIds);
|
|
66
|
+
const [fetchNonce, setFetchNonce] = (0, react_1.useState)(0);
|
|
67
|
+
(0, react_1.useEffect)(() => {
|
|
68
|
+
let timeout = null;
|
|
69
|
+
index_1.default.fetchIdsSortedByMarketcap().then(ids => {
|
|
70
|
+
setIds(ids);
|
|
71
|
+
timeout = setTimeout(() => setFetchNonce(n => n + 1), marketcapRefresh);
|
|
72
|
+
}, error => {
|
|
73
|
+
(0, logs_1.log)("countervalues", "error fetching marketcap ids " + error);
|
|
74
|
+
timeout = setTimeout(() => setFetchNonce(n => n + 1), marketcapRefreshOnError);
|
|
75
|
+
});
|
|
76
|
+
return () => {
|
|
77
|
+
if (timeout) {
|
|
78
|
+
clearTimeout(timeout);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}, [fetchNonce]);
|
|
82
|
+
return ids;
|
|
83
|
+
}
|
|
84
|
+
// infer the tracking pairs for the top coins that the portfolio needs to display itself
|
|
85
|
+
// if startDate is undefined, the feature is disabled
|
|
86
|
+
function useTrackingPairsForTopCoins(marketcapIds, countervalue, size, startDate) {
|
|
87
|
+
const dateTimestamp = startDate === null || startDate === void 0 ? void 0 : startDate.getTime();
|
|
88
|
+
return (0, react_1.useMemo)(() => dateTimestamp
|
|
89
|
+
? (0, logic_1.trackingPairForTopCoins)(marketcapIds, size, countervalue, new Date(dateTimestamp))
|
|
90
|
+
: [], [marketcapIds, countervalue, dateTimestamp, size]);
|
|
91
|
+
}
|
|
92
|
+
exports.useTrackingPairsForTopCoins = useTrackingPairsForTopCoins;
|
|
47
93
|
function useTrackingPairForAccounts(accounts, countervalue) {
|
|
48
94
|
// first we cache the tracking pairs with its hash
|
|
49
95
|
const c = (0, react_1.useMemo)(() => {
|
|
@@ -56,6 +102,11 @@ function useTrackingPairForAccounts(accounts, countervalue) {
|
|
|
56
102
|
return (0, react_1.useMemo)(() => c.pairs, [c.hash]);
|
|
57
103
|
}
|
|
58
104
|
exports.useTrackingPairForAccounts = useTrackingPairForAccounts;
|
|
105
|
+
function CountervaluesMarketcap({ children }) {
|
|
106
|
+
const marketcapIds = useMarketcap();
|
|
107
|
+
return (react_1.default.createElement(CountervaluesMarketcapIdsContext.Provider, { value: marketcapIds }, children));
|
|
108
|
+
}
|
|
109
|
+
exports.CountervaluesMarketcap = CountervaluesMarketcap;
|
|
59
110
|
function Countervalues({ children, userSettings, pollInitDelay = 3 * 1000, autopollInterval = 8 * 60 * 1000, debounceDelay = 1000, savedState, }) {
|
|
60
111
|
const debouncedUserSettings = (0, useDebounce_1.useDebounce)(userSettings, debounceDelay);
|
|
61
112
|
const [{ state, pending, error }, dispatch] = (0, react_1.useReducer)(fetchReducer, initialFetchState);
|
|
@@ -120,7 +171,8 @@ function Countervalues({ children, userSettings, pollInitDelay = 3 * 1000, autop
|
|
|
120
171
|
error,
|
|
121
172
|
}), [pending, error]);
|
|
122
173
|
return (react_1.default.createElement(CountervaluesPollingContext.Provider, { value: polling },
|
|
123
|
-
react_1.default.createElement(
|
|
174
|
+
react_1.default.createElement(CountervaluesUserSettingsContext.Provider, { value: userSettings },
|
|
175
|
+
react_1.default.createElement(CountervaluesContext.Provider, { value: state }, children))));
|
|
124
176
|
}
|
|
125
177
|
exports.Countervalues = Countervalues;
|
|
126
178
|
const initialFetchState = {
|
|
@@ -151,24 +203,39 @@ function fetchReducer(state, action) {
|
|
|
151
203
|
return state;
|
|
152
204
|
}
|
|
153
205
|
}
|
|
206
|
+
// allows consumer to access the countervalues polling control object
|
|
154
207
|
function useCountervaluesPolling() {
|
|
155
208
|
return (0, react_1.useContext)(CountervaluesPollingContext);
|
|
156
209
|
}
|
|
157
210
|
exports.useCountervaluesPolling = useCountervaluesPolling;
|
|
211
|
+
// allows consumer to access the user settings that was used to fetch the countervalues
|
|
212
|
+
function useCountervaluesUserSettingsContext() {
|
|
213
|
+
return (0, react_1.useContext)(CountervaluesUserSettingsContext);
|
|
214
|
+
}
|
|
215
|
+
exports.useCountervaluesUserSettingsContext = useCountervaluesUserSettingsContext;
|
|
216
|
+
// allows consumer to access the countervalues state
|
|
158
217
|
function useCountervaluesState() {
|
|
159
218
|
return (0, react_1.useContext)(CountervaluesContext);
|
|
160
219
|
}
|
|
161
220
|
exports.useCountervaluesState = useCountervaluesState;
|
|
221
|
+
// allows consumer to access the coins ids sorted by marketcap. It's basically all the coins that the API supports.
|
|
222
|
+
function useMarketcapIds() {
|
|
223
|
+
return (0, react_1.useContext)(CountervaluesMarketcapIdsContext);
|
|
224
|
+
}
|
|
225
|
+
exports.useMarketcapIds = useMarketcapIds;
|
|
226
|
+
// provides an export of the countervalues state
|
|
162
227
|
function useCountervaluesExport() {
|
|
163
228
|
const state = (0, react_1.useContext)(CountervaluesContext);
|
|
164
229
|
return (0, react_1.useMemo)(() => (0, logic_1.exportCountervalues)(state), [state]);
|
|
165
230
|
}
|
|
166
231
|
exports.useCountervaluesExport = useCountervaluesExport;
|
|
232
|
+
// provides a way to calculate a countervalue from a value
|
|
167
233
|
function useCalculate(query) {
|
|
168
234
|
const state = useCountervaluesState();
|
|
169
235
|
return (0, logic_1.calculate)(state, query);
|
|
170
236
|
}
|
|
171
237
|
exports.useCalculate = useCalculate;
|
|
238
|
+
// provides a way to calculate a countervalue from a value using a callback
|
|
172
239
|
function useCalculateCountervalueCallback({ to, }) {
|
|
173
240
|
const state = useCountervaluesState();
|
|
174
241
|
return (0, react_1.useCallback)((from, value) => {
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+CAAyC;AACzC,+CASe;AACf,sEAA8F;AAC9F,8DAQ4C;AAC5C,mFAAyD;AAOzD,kEAA+D;AAC/D,yCAAqC;AAiCrC,MAAM,2BAA2B,GAAG,IAAA,qBAAa,EAAU;IACzD,IAAI,EAAE,GAAG,EAAE,GAAE,CAAC;IACd,IAAI,EAAE,GAAG,EAAE,GAAE,CAAC;IACd,KAAK,EAAE,GAAG,EAAE,GAAE,CAAC;IACf,IAAI,EAAE,GAAG,EAAE,GAAE,CAAC;IACd,OAAO,EAAE,KAAK;IACd,KAAK,EAAE,IAAI;CACZ,CAAC,CAAC;AAEH,MAAM,gCAAgC,GAAG,IAAA,qBAAa,EAAwB;IAC5E,aAAa,EAAE,EAAE;IACjB,YAAY,EAAE,IAAI;CACnB,CAAC,CAAC;AAEH,MAAM,oBAAoB,GAAG,IAAA,qBAAa,EAAqB,oBAAY,CAAC,CAAC;AAE7E,MAAM,gCAAgC,GAAG,IAAA,qBAAa,EAAW,EAAE,CAAC,CAAC;AAErE,SAAS,iBAAiB,CAAC,CAAiB;IAC1C,OAAO,CAAC;SACL,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC,EAAE,CAAC,MAAM,IAAI,CAAC,CAA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{ Account, AccountLike, AccountPortfolio, AssetsDistribution, CurrencyPortfolio, Portfolio, PortfolioRange } from "@ledgerhq/types-live";
|
|
3
|
+
import { GetPortfolioOptionsType } from "@ledgerhq/live-countervalues/portfolio";
|
|
4
|
+
export declare function useBalanceHistoryWithCountervalue({ account, range, to, }: {
|
|
5
|
+
account: AccountLike;
|
|
6
|
+
range: PortfolioRange;
|
|
7
|
+
to: Currency;
|
|
8
|
+
}): AccountPortfolio;
|
|
9
|
+
/**
|
|
10
|
+
* WARNING: expensive hook, do not use if you don't need all the data
|
|
11
|
+
* that this computes.
|
|
12
|
+
*
|
|
13
|
+
* Computes the data necessary to plot a graph of the user's balance */
|
|
14
|
+
export declare function usePortfolio({ accounts, range, to, options, }: {
|
|
15
|
+
accounts: AccountLike[];
|
|
16
|
+
range: PortfolioRange;
|
|
17
|
+
to: Currency;
|
|
18
|
+
options?: GetPortfolioOptionsType;
|
|
19
|
+
}): Portfolio;
|
|
20
|
+
/**
|
|
21
|
+
* Computes the data necessary to plot of a graph of the user's balance.
|
|
22
|
+
* It throttles that expensive computation by throttling the `accounts` and
|
|
23
|
+
* `countervalues` inputs as they can change very frequently and immediate
|
|
24
|
+
* visual feedback of each of their update is not necessary for a good UX.
|
|
25
|
+
* Initially the throttle delay is low (100ms) and as soon as we get the bare
|
|
26
|
+
* minimum (total balance and the graph data for some of the accounts) we
|
|
27
|
+
* increase the throttle delay to the value of the prop `stableThrottleDelay`.
|
|
28
|
+
*
|
|
29
|
+
* We have to do this throttling as there are too many frequent updates of
|
|
30
|
+
* the `accounts` object and countervaluesState and the computation performed
|
|
31
|
+
* to aggregate all the data to make a graph is very expensive, we don't want
|
|
32
|
+
* to do this on every render as it would cause big frame drops.
|
|
33
|
+
*/
|
|
34
|
+
export declare function usePortfolioThrottled({ accounts, range, to, options, stableThrottleDelay, }: {
|
|
35
|
+
accounts: AccountLike[];
|
|
36
|
+
range: PortfolioRange;
|
|
37
|
+
to: Currency;
|
|
38
|
+
options?: GetPortfolioOptionsType;
|
|
39
|
+
stableThrottleDelay?: number;
|
|
40
|
+
}): Portfolio;
|
|
41
|
+
export declare function useCurrencyPortfolio({ accounts: rawAccounts, range, to, currency, }: {
|
|
42
|
+
accounts: Account[];
|
|
43
|
+
range: PortfolioRange;
|
|
44
|
+
to: Currency;
|
|
45
|
+
currency: CryptoCurrency | TokenCurrency;
|
|
46
|
+
}): CurrencyPortfolio;
|
|
47
|
+
export declare function useDistribution({ accounts, to, showEmptyAccounts, hideEmptyTokenAccount, }: {
|
|
48
|
+
accounts: Account[];
|
|
49
|
+
to: Currency;
|
|
50
|
+
showEmptyAccounts?: boolean;
|
|
51
|
+
hideEmptyTokenAccount?: boolean;
|
|
52
|
+
}): AssetsDistribution;
|
|
53
|
+
//# sourceMappingURL=portfolio.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"portfolio.d.ts","sourceRoot":"","sources":["../src/portfolio.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAC5F,OAAO,KAAK,EACV,OAAO,EACP,WAAW,EACX,gBAAgB,EAChB,kBAAkB,EAClB,iBAAiB,EACjB,SAAS,EACT,cAAc,EACf,MAAM,sBAAsB,CAAC;AAK9B,OAAO,EAML,uBAAuB,EACxB,MAAM,wCAAwC,CAAC;AAEhD,wBAAgB,iCAAiC,CAAC,EAChD,OAAO,EACP,KAAK,EACL,EAAE,GACH,EAAE;IACD,OAAO,EAAE,WAAW,CAAC;IACrB,KAAK,EAAE,cAAc,CAAC;IACtB,EAAE,EAAE,QAAQ,CAAC;CACd,GAAG,gBAAgB,CAInB;AAED;;;;uEAIuE;AACvE,wBAAgB,YAAY,CAAC,EAC3B,QAAQ,EACR,KAAK,EACL,EAAE,EACF,OAAO,GACR,EAAE;IACD,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,KAAK,EAAE,cAAc,CAAC;IACtB,EAAE,EAAE,QAAQ,CAAC;IACb,OAAO,CAAC,EAAE,uBAAuB,CAAC;CACnC,GAAG,SAAS,CAGZ;AAUD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,qBAAqB,CAAC,EACpC,QAAQ,EACR,KAAK,EACL,EAAE,EACF,OAAO,EACP,mBAA0B,GAC3B,EAAE;IACD,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,KAAK,EAAE,cAAc,CAAC;IACtB,EAAE,EAAE,QAAQ,CAAC;IACb,OAAO,CAAC,EAAE,uBAAuB,CAAC;IAClC,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B,GAAG,SAAS,CAkCZ;AAED,wBAAgB,oBAAoB,CAAC,EACnC,QAAQ,EAAE,WAAW,EACrB,KAAK,EACL,EAAE,EACF,QAAQ,GACT,EAAE;IACD,QAAQ,EAAE,OAAO,EAAE,CAAC;IACpB,KAAK,EAAE,cAAc,CAAC;IACtB,EAAE,EAAE,QAAQ,CAAC;IACb,QAAQ,EAAE,cAAc,GAAG,aAAa,CAAC;CAC1C,GAAG,iBAAiB,CAIpB;AAED,wBAAgB,eAAe,CAAC,EAC9B,QAAQ,EACR,EAAE,EACF,iBAAiB,EACjB,qBAAqB,GACtB,EAAE;IACD,QAAQ,EAAE,OAAO,EAAE,CAAC;IACpB,EAAE,EAAE,QAAQ,CAAC;IACb,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,qBAAqB,CAAC,EAAE,OAAO,CAAC;CACjC,GAAG,kBAAkB,CASrB"}
|
package/lib/portfolio.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useDistribution = exports.useCurrencyPortfolio = exports.usePortfolioThrottled = exports.usePortfolio = exports.useBalanceHistoryWithCountervalue = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const index_1 = require("@ledgerhq/coin-framework/account/index");
|
|
6
|
+
const _1 = require(".");
|
|
7
|
+
const useThrottledFunction_1 = require("@ledgerhq/live-hooks/useThrottledFunction");
|
|
8
|
+
const portfolio_1 = require("@ledgerhq/live-countervalues/portfolio");
|
|
9
|
+
function useBalanceHistoryWithCountervalue({ account, range, to, }) {
|
|
10
|
+
const state = (0, _1.useCountervaluesState)();
|
|
11
|
+
const count = (0, portfolio_1.getPortfolioCount)([account], range);
|
|
12
|
+
return (0, portfolio_1.getBalanceHistoryWithCountervalue)(account, range, count, state, to);
|
|
13
|
+
}
|
|
14
|
+
exports.useBalanceHistoryWithCountervalue = useBalanceHistoryWithCountervalue;
|
|
15
|
+
/**
|
|
16
|
+
* WARNING: expensive hook, do not use if you don't need all the data
|
|
17
|
+
* that this computes.
|
|
18
|
+
*
|
|
19
|
+
* Computes the data necessary to plot a graph of the user's balance */
|
|
20
|
+
function usePortfolio({ accounts, range, to, options, }) {
|
|
21
|
+
const state = (0, _1.useCountervaluesState)();
|
|
22
|
+
return (0, portfolio_1.getPortfolio)(accounts, range, state, to, options);
|
|
23
|
+
}
|
|
24
|
+
exports.usePortfolio = usePortfolio;
|
|
25
|
+
/**
|
|
26
|
+
* keep a low throttling until the balance is available
|
|
27
|
+
*/
|
|
28
|
+
const lowThrottleDelay = 100;
|
|
29
|
+
/**
|
|
30
|
+
* keep an intermediate throttle until some additional data is available
|
|
31
|
+
*/
|
|
32
|
+
const intermediateThrottleDelay = 300;
|
|
33
|
+
/**
|
|
34
|
+
* Computes the data necessary to plot of a graph of the user's balance.
|
|
35
|
+
* It throttles that expensive computation by throttling the `accounts` and
|
|
36
|
+
* `countervalues` inputs as they can change very frequently and immediate
|
|
37
|
+
* visual feedback of each of their update is not necessary for a good UX.
|
|
38
|
+
* Initially the throttle delay is low (100ms) and as soon as we get the bare
|
|
39
|
+
* minimum (total balance and the graph data for some of the accounts) we
|
|
40
|
+
* increase the throttle delay to the value of the prop `stableThrottleDelay`.
|
|
41
|
+
*
|
|
42
|
+
* We have to do this throttling as there are too many frequent updates of
|
|
43
|
+
* the `accounts` object and countervaluesState and the computation performed
|
|
44
|
+
* to aggregate all the data to make a graph is very expensive, we don't want
|
|
45
|
+
* to do this on every render as it would cause big frame drops.
|
|
46
|
+
*/
|
|
47
|
+
function usePortfolioThrottled({ accounts, range, to, options, stableThrottleDelay = 5000, }) {
|
|
48
|
+
const [variableThrottleDelay, setVariableThrottleDelay] = (0, react_1.useState)(lowThrottleDelay);
|
|
49
|
+
const cvState = (0, _1.useCountervaluesState)();
|
|
50
|
+
const [throttledCVState, throttledAccounts] = (0, useThrottledFunction_1.useThrottledValues)([cvState, accounts], variableThrottleDelay);
|
|
51
|
+
/**
|
|
52
|
+
* Options are not supposed to change.
|
|
53
|
+
* Ensure a non changing reference for options to avoid an infinite setState
|
|
54
|
+
* loop if the consumer passes a new object literal to this hook at render
|
|
55
|
+
* time.
|
|
56
|
+
* */
|
|
57
|
+
const opts = (0, react_1.useRef)(options);
|
|
58
|
+
const [portfolio, setPortfolio] = (0, react_1.useState)(() => (0, portfolio_1.getPortfolio)(throttledAccounts, range, throttledCVState, to, opts.current));
|
|
59
|
+
const ignoreUseEffectFirstCall = (0, react_1.useRef)(true);
|
|
60
|
+
(0, react_1.useEffect)(() => {
|
|
61
|
+
if (ignoreUseEffectFirstCall.current) {
|
|
62
|
+
ignoreUseEffectFirstCall.current = false;
|
|
63
|
+
// avoid recomputing twice the initial state
|
|
64
|
+
// (once at state initialisation, then once here at mount)
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const portfolio = (0, portfolio_1.getPortfolio)(throttledAccounts, range, throttledCVState, to, opts.current);
|
|
68
|
+
setPortfolio(portfolio);
|
|
69
|
+
if (portfolio.balanceAvailable)
|
|
70
|
+
setVariableThrottleDelay(intermediateThrottleDelay);
|
|
71
|
+
if (throttledAccounts.length > 0 && portfolio.availableAccounts.length > 0)
|
|
72
|
+
setVariableThrottleDelay(stableThrottleDelay);
|
|
73
|
+
}, [throttledAccounts, range, throttledCVState, to, stableThrottleDelay]);
|
|
74
|
+
return portfolio;
|
|
75
|
+
}
|
|
76
|
+
exports.usePortfolioThrottled = usePortfolioThrottled;
|
|
77
|
+
function useCurrencyPortfolio({ accounts: rawAccounts, range, to, currency, }) {
|
|
78
|
+
const accounts = (0, index_1.flattenAccounts)(rawAccounts).filter(a => (0, index_1.getAccountCurrency)(a) === currency);
|
|
79
|
+
const state = (0, _1.useCountervaluesState)();
|
|
80
|
+
return (0, portfolio_1.getCurrencyPortfolio)(accounts, range, state, to);
|
|
81
|
+
}
|
|
82
|
+
exports.useCurrencyPortfolio = useCurrencyPortfolio;
|
|
83
|
+
function useDistribution({ accounts, to, showEmptyAccounts, hideEmptyTokenAccount, }) {
|
|
84
|
+
const state = (0, _1.useCountervaluesState)();
|
|
85
|
+
return (0, portfolio_1.getAssetsDistribution)(accounts, state, to, {
|
|
86
|
+
minShowFirst: 6,
|
|
87
|
+
maxShowFirst: 6,
|
|
88
|
+
showFirstThreshold: 0.95,
|
|
89
|
+
showEmptyAccounts: !!showEmptyAccounts,
|
|
90
|
+
hideEmptyTokenAccount: !!hideEmptyTokenAccount,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
exports.useDistribution = useDistribution;
|
|
94
|
+
//# sourceMappingURL=portfolio.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"portfolio.js","sourceRoot":"","sources":["../src/portfolio.tsx"],"names":[],"mappings":";;;AAUA,iCAAoD;AACpD,kEAA6F;AAC7F,wBAA0C;AAC1C,oFAA+E;AAC/E,sEAOgD;AAEhD,SAAgB,iCAAiC,CAAC,EAChD,OAAO,EACP,KAAK,EACL,EAAE,GAKH;IACC,MAAM,KAAK,GAAG,IAAA,wBAAqB,GAAE,CAAC;IACtC,MAAM,KAAK,GAAG,IAAA,6BAAiB,EAAC,CAAC,OAAO,CAAC,EAAE,KAAK,CAAC,CAAC;IAClD,OAAO,IAAA,6CAAiC,EAAC,OAAO,EAAE,KAAK,EAAE,KA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AAE,KAAK,EAAE,EAAE,EAAE;QAChD,YAAY,EAAE,CAAC;QACf,YAAY,EAAE,CAAC;QACf,kBAAkB,EAAE,IAAI;QACxB,iBAAiB,EAAE,CAAC,CAAC,iBAAiB;QACtC,qBAAqB,EAAE,CAAC,CAAC,qBAAqB;KAC/C,CAAC,CAAC;AACL,CAAC;AAnBD,0CAmBC"}
|
package/lib-es/index.d.ts
CHANGED
|
@@ -19,10 +19,20 @@ export type Props = {
|
|
|
19
19
|
debounceDelay?: number;
|
|
20
20
|
savedState?: CounterValuesStateRaw;
|
|
21
21
|
};
|
|
22
|
+
export declare function useTrackingPairsForTopCoins(marketcapIds: string[], countervalue: Currency, size: number, startDate: Date | undefined): {
|
|
23
|
+
from: import("@ledgerhq/types-cryptoassets").CryptoCurrency;
|
|
24
|
+
to: Currency;
|
|
25
|
+
startDate: Date;
|
|
26
|
+
}[];
|
|
22
27
|
export declare function useTrackingPairForAccounts(accounts: Account[], countervalue: Currency): TrackingPair[];
|
|
28
|
+
export declare function CountervaluesMarketcap({ children }: {
|
|
29
|
+
children: React.ReactNode;
|
|
30
|
+
}): ReactElement;
|
|
23
31
|
export declare function Countervalues({ children, userSettings, pollInitDelay, autopollInterval, debounceDelay, savedState, }: Props): ReactElement;
|
|
24
32
|
export declare function useCountervaluesPolling(): Polling;
|
|
33
|
+
export declare function useCountervaluesUserSettingsContext(): CountervaluesSettings;
|
|
25
34
|
export declare function useCountervaluesState(): CounterValuesState;
|
|
35
|
+
export declare function useMarketcapIds(): string[];
|
|
26
36
|
export declare function useCountervaluesExport(): CounterValuesStateRaw;
|
|
27
37
|
export declare function useCalculate(query: {
|
|
28
38
|
value: number;
|
package/lib-es/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,KAAK,EAAE,EAQZ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CAAC;IACtB,QAAQ,EAAE,IAAI,CAAC;IACf,qBAAqB,EAAE,CAAC,UAAU,EAAE,SAAS,KAAK,SAAS,CAAC;CAC7D,CAgCA"}
|
package/lib-es/index.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { BigNumber } from "bignumber.js";
|
|
2
2
|
import React, { createContext, useMemo, useContext, useEffect, useReducer, useState, useCallback, } from "react";
|
|
3
3
|
import { getAccountCurrency, getAccountUnit } from "@ledgerhq/coin-framework/account/helpers";
|
|
4
|
-
import { initialState, calculate, loadCountervalues, exportCountervalues, importCountervalues, inferTrackingPairForAccounts, } from "@ledgerhq/live-countervalues/logic";
|
|
4
|
+
import { initialState, calculate, loadCountervalues, exportCountervalues, importCountervalues, inferTrackingPairForAccounts, trackingPairForTopCoins, } from "@ledgerhq/live-countervalues/logic";
|
|
5
|
+
import api from "@ledgerhq/live-countervalues/api/index";
|
|
5
6
|
import { useDebounce } from "@ledgerhq/live-hooks/useDebounce";
|
|
7
|
+
import { log } from "@ledgerhq/logs";
|
|
6
8
|
const CountervaluesPollingContext = createContext({
|
|
7
9
|
wipe: () => { },
|
|
8
10
|
poll: () => { },
|
|
@@ -11,13 +13,53 @@ const CountervaluesPollingContext = createContext({
|
|
|
11
13
|
pending: false,
|
|
12
14
|
error: null,
|
|
13
15
|
});
|
|
16
|
+
const CountervaluesUserSettingsContext = createContext({
|
|
17
|
+
trackingPairs: [],
|
|
18
|
+
autofillGaps: true,
|
|
19
|
+
});
|
|
14
20
|
const CountervaluesContext = createContext(initialState);
|
|
21
|
+
const CountervaluesMarketcapIdsContext = createContext([]);
|
|
15
22
|
function trackingPairsHash(a) {
|
|
16
23
|
return a
|
|
17
24
|
.map(p => `${p.from.ticker}:${p.to.ticker}:${p.startDate.toISOString().slice(0, 10) || ""}`)
|
|
18
25
|
.sort()
|
|
19
26
|
.join("|");
|
|
20
27
|
}
|
|
28
|
+
const marketcapRefresh = 30 * 60000;
|
|
29
|
+
const marketcapRefreshOnError = 60000;
|
|
30
|
+
const initialIds = [];
|
|
31
|
+
/**
|
|
32
|
+
* Internal only. fetch the marketcap and keep it in sync.
|
|
33
|
+
* the data is shared through a context, you can useMarketcapIds to get it.
|
|
34
|
+
*/
|
|
35
|
+
function useMarketcap() {
|
|
36
|
+
const [ids, setIds] = useState(initialIds);
|
|
37
|
+
const [fetchNonce, setFetchNonce] = useState(0);
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
let timeout = null;
|
|
40
|
+
api.fetchIdsSortedByMarketcap().then(ids => {
|
|
41
|
+
setIds(ids);
|
|
42
|
+
timeout = setTimeout(() => setFetchNonce(n => n + 1), marketcapRefresh);
|
|
43
|
+
}, error => {
|
|
44
|
+
log("countervalues", "error fetching marketcap ids " + error);
|
|
45
|
+
timeout = setTimeout(() => setFetchNonce(n => n + 1), marketcapRefreshOnError);
|
|
46
|
+
});
|
|
47
|
+
return () => {
|
|
48
|
+
if (timeout) {
|
|
49
|
+
clearTimeout(timeout);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}, [fetchNonce]);
|
|
53
|
+
return ids;
|
|
54
|
+
}
|
|
55
|
+
// infer the tracking pairs for the top coins that the portfolio needs to display itself
|
|
56
|
+
// if startDate is undefined, the feature is disabled
|
|
57
|
+
export function useTrackingPairsForTopCoins(marketcapIds, countervalue, size, startDate) {
|
|
58
|
+
const dateTimestamp = startDate === null || startDate === void 0 ? void 0 : startDate.getTime();
|
|
59
|
+
return useMemo(() => dateTimestamp
|
|
60
|
+
? trackingPairForTopCoins(marketcapIds, size, countervalue, new Date(dateTimestamp))
|
|
61
|
+
: [], [marketcapIds, countervalue, dateTimestamp, size]);
|
|
62
|
+
}
|
|
21
63
|
export function useTrackingPairForAccounts(accounts, countervalue) {
|
|
22
64
|
// first we cache the tracking pairs with its hash
|
|
23
65
|
const c = useMemo(() => {
|
|
@@ -29,6 +71,10 @@ export function useTrackingPairForAccounts(accounts, countervalue) {
|
|
|
29
71
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
30
72
|
return useMemo(() => c.pairs, [c.hash]);
|
|
31
73
|
}
|
|
74
|
+
export function CountervaluesMarketcap({ children }) {
|
|
75
|
+
const marketcapIds = useMarketcap();
|
|
76
|
+
return (React.createElement(CountervaluesMarketcapIdsContext.Provider, { value: marketcapIds }, children));
|
|
77
|
+
}
|
|
32
78
|
export function Countervalues({ children, userSettings, pollInitDelay = 3 * 1000, autopollInterval = 8 * 60 * 1000, debounceDelay = 1000, savedState, }) {
|
|
33
79
|
const debouncedUserSettings = useDebounce(userSettings, debounceDelay);
|
|
34
80
|
const [{ state, pending, error }, dispatch] = useReducer(fetchReducer, initialFetchState);
|
|
@@ -93,7 +139,8 @@ export function Countervalues({ children, userSettings, pollInitDelay = 3 * 1000
|
|
|
93
139
|
error,
|
|
94
140
|
}), [pending, error]);
|
|
95
141
|
return (React.createElement(CountervaluesPollingContext.Provider, { value: polling },
|
|
96
|
-
React.createElement(
|
|
142
|
+
React.createElement(CountervaluesUserSettingsContext.Provider, { value: userSettings },
|
|
143
|
+
React.createElement(CountervaluesContext.Provider, { value: state }, children))));
|
|
97
144
|
}
|
|
98
145
|
const initialFetchState = {
|
|
99
146
|
state: initialState,
|
|
@@ -123,20 +170,33 @@ function fetchReducer(state, action) {
|
|
|
123
170
|
return state;
|
|
124
171
|
}
|
|
125
172
|
}
|
|
173
|
+
// allows consumer to access the countervalues polling control object
|
|
126
174
|
export function useCountervaluesPolling() {
|
|
127
175
|
return useContext(CountervaluesPollingContext);
|
|
128
176
|
}
|
|
177
|
+
// allows consumer to access the user settings that was used to fetch the countervalues
|
|
178
|
+
export function useCountervaluesUserSettingsContext() {
|
|
179
|
+
return useContext(CountervaluesUserSettingsContext);
|
|
180
|
+
}
|
|
181
|
+
// allows consumer to access the countervalues state
|
|
129
182
|
export function useCountervaluesState() {
|
|
130
183
|
return useContext(CountervaluesContext);
|
|
131
184
|
}
|
|
185
|
+
// allows consumer to access the coins ids sorted by marketcap. It's basically all the coins that the API supports.
|
|
186
|
+
export function useMarketcapIds() {
|
|
187
|
+
return useContext(CountervaluesMarketcapIdsContext);
|
|
188
|
+
}
|
|
189
|
+
// provides an export of the countervalues state
|
|
132
190
|
export function useCountervaluesExport() {
|
|
133
191
|
const state = useContext(CountervaluesContext);
|
|
134
192
|
return useMemo(() => exportCountervalues(state), [state]);
|
|
135
193
|
}
|
|
194
|
+
// provides a way to calculate a countervalue from a value
|
|
136
195
|
export function useCalculate(query) {
|
|
137
196
|
const state = useCountervaluesState();
|
|
138
197
|
return calculate(state, query);
|
|
139
198
|
}
|
|
199
|
+
// provides a way to calculate a countervalue from a value using a callback
|
|
140
200
|
export function useCalculateCountervalueCallback({ to, }) {
|
|
141
201
|
const state = useCountervaluesState();
|
|
142
202
|
return useCallback((from, value) => {
|
package/lib-es/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,KAAK,EAAE,EACZ,aAAa,EACb,OAAO,EACP,UAAU,EACV,SAAS,EACT,UAAU,EACV,QAAQ,EACR,WAAW,GAEZ,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,0CAA0C,CAAC;AAC9F,OAAO,EACL,YAAY,EACZ,SAAS,EACT,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,EACnB,4BAA4B,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,KAAK,EAAE,EACZ,aAAa,EACb,OAAO,EACP,UAAU,EACV,SAAS,EACT,UAAU,EACV,QAAQ,EACR,WAAW,GAEZ,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,kBAAkB,EAAE,cAAc,EAAE,M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C,CAAC;IAC3C,MAAM,KAAK,GAAG,qBAAqB,EAAE,CAAC;IACtC,MAAM,qBAAqB,GAAG,WAAW,CACvC,CAAC,UAAqB,EAAE,EAAE;;QACxB,MAAM,YAAY,GAAG,IAAI,SAAS,CAChC,MAAA,SAAS,CAAC,KAAK,EAAE;YACf,IAAI,EAAE,cAAc;YACpB,EAAE,EAAE,YAAY;YAChB,KAAK,EAAE,UAAU,CAAC,QAAQ,EAAE;YAC5B,OAAO,EAAE,IAAI;SACd,CAAC,mCAAI,CAAC,CACR,CAAC;QACF,OAAO,YAAY,CAAC;IACtB,CAAC,EACD,CAAC,KAAK,EAAE,cAAc,EAAE,YAAY,CAAC,CACtC,CAAC;IACF,OAAO;QACL,UAAU;QACV,UAAU;QACV,QAAQ;QACR,qBAAqB;KACtB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { CryptoCurrency, Currency, TokenCurrency } from "@ledgerhq/types-cryptoassets";
|
|
2
|
+
import type { Account, AccountLike, AccountPortfolio, AssetsDistribution, CurrencyPortfolio, Portfolio, PortfolioRange } from "@ledgerhq/types-live";
|
|
3
|
+
import { GetPortfolioOptionsType } from "@ledgerhq/live-countervalues/portfolio";
|
|
4
|
+
export declare function useBalanceHistoryWithCountervalue({ account, range, to, }: {
|
|
5
|
+
account: AccountLike;
|
|
6
|
+
range: PortfolioRange;
|
|
7
|
+
to: Currency;
|
|
8
|
+
}): AccountPortfolio;
|
|
9
|
+
/**
|
|
10
|
+
* WARNING: expensive hook, do not use if you don't need all the data
|
|
11
|
+
* that this computes.
|
|
12
|
+
*
|
|
13
|
+
* Computes the data necessary to plot a graph of the user's balance */
|
|
14
|
+
export declare function usePortfolio({ accounts, range, to, options, }: {
|
|
15
|
+
accounts: AccountLike[];
|
|
16
|
+
range: PortfolioRange;
|
|
17
|
+
to: Currency;
|
|
18
|
+
options?: GetPortfolioOptionsType;
|
|
19
|
+
}): Portfolio;
|
|
20
|
+
/**
|
|
21
|
+
* Computes the data necessary to plot of a graph of the user's balance.
|
|
22
|
+
* It throttles that expensive computation by throttling the `accounts` and
|
|
23
|
+
* `countervalues` inputs as they can change very frequently and immediate
|
|
24
|
+
* visual feedback of each of their update is not necessary for a good UX.
|
|
25
|
+
* Initially the throttle delay is low (100ms) and as soon as we get the bare
|
|
26
|
+
* minimum (total balance and the graph data for some of the accounts) we
|
|
27
|
+
* increase the throttle delay to the value of the prop `stableThrottleDelay`.
|
|
28
|
+
*
|
|
29
|
+
* We have to do this throttling as there are too many frequent updates of
|
|
30
|
+
* the `accounts` object and countervaluesState and the computation performed
|
|
31
|
+
* to aggregate all the data to make a graph is very expensive, we don't want
|
|
32
|
+
* to do this on every render as it would cause big frame drops.
|
|
33
|
+
*/
|
|
34
|
+
export declare function usePortfolioThrottled({ accounts, range, to, options, stableThrottleDelay, }: {
|
|
35
|
+
accounts: AccountLike[];
|
|
36
|
+
range: PortfolioRange;
|
|
37
|
+
to: Currency;
|
|
38
|
+
options?: GetPortfolioOptionsType;
|
|
39
|
+
stableThrottleDelay?: number;
|
|
40
|
+
}): Portfolio;
|
|
41
|
+
export declare function useCurrencyPortfolio({ accounts: rawAccounts, range, to, currency, }: {
|
|
42
|
+
accounts: Account[];
|
|
43
|
+
range: PortfolioRange;
|
|
44
|
+
to: Currency;
|
|
45
|
+
currency: CryptoCurrency | TokenCurrency;
|
|
46
|
+
}): CurrencyPortfolio;
|
|
47
|
+
export declare function useDistribution({ accounts, to, showEmptyAccounts, hideEmptyTokenAccount, }: {
|
|
48
|
+
accounts: Account[];
|
|
49
|
+
to: Currency;
|
|
50
|
+
showEmptyAccounts?: boolean;
|
|
51
|
+
hideEmptyTokenAccount?: boolean;
|
|
52
|
+
}): AssetsDistribution;
|
|
53
|
+
//# sourceMappingURL=portfolio.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"portfolio.d.ts","sourceRoot":"","sources":["../src/portfolio.tsx"],"names":[],"mappings":"AAAA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AC;IACb,QAAQ,EAAE,cAAc,GAAG,aAAa,CAAC;CAC1C,GAAG,iBAAiB,CAIpB;AAED,wBAAgB,eAAe,CAAC,EAC9B,QAAQ,EACR,EAAE,EACF,iBAAiB,EACjB,qBAAqB,GACtB,EAAE;IACD,QAAQ,EAAE,OAAO,EAAE,CAAC;IACpB,EAAE,EAAE,QAAQ,CAAC;IACb,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,qBAAqB,CAAC,EAAE,OAAO,CAAC;CACjC,GAAG,kBAAkB,CASrB"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from "react";
|
|
2
|
+
import { getAccountCurrency, flattenAccounts } from "@ledgerhq/coin-framework/account/index";
|
|
3
|
+
import { useCountervaluesState } from ".";
|
|
4
|
+
import { useThrottledValues } from "@ledgerhq/live-hooks/useThrottledFunction";
|
|
5
|
+
import { getBalanceHistoryWithCountervalue, getPortfolio, getCurrencyPortfolio, getAssetsDistribution, getPortfolioCount, } from "@ledgerhq/live-countervalues/portfolio";
|
|
6
|
+
export function useBalanceHistoryWithCountervalue({ account, range, to, }) {
|
|
7
|
+
const state = useCountervaluesState();
|
|
8
|
+
const count = getPortfolioCount([account], range);
|
|
9
|
+
return getBalanceHistoryWithCountervalue(account, range, count, state, to);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* WARNING: expensive hook, do not use if you don't need all the data
|
|
13
|
+
* that this computes.
|
|
14
|
+
*
|
|
15
|
+
* Computes the data necessary to plot a graph of the user's balance */
|
|
16
|
+
export function usePortfolio({ accounts, range, to, options, }) {
|
|
17
|
+
const state = useCountervaluesState();
|
|
18
|
+
return getPortfolio(accounts, range, state, to, options);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* keep a low throttling until the balance is available
|
|
22
|
+
*/
|
|
23
|
+
const lowThrottleDelay = 100;
|
|
24
|
+
/**
|
|
25
|
+
* keep an intermediate throttle until some additional data is available
|
|
26
|
+
*/
|
|
27
|
+
const intermediateThrottleDelay = 300;
|
|
28
|
+
/**
|
|
29
|
+
* Computes the data necessary to plot of a graph of the user's balance.
|
|
30
|
+
* It throttles that expensive computation by throttling the `accounts` and
|
|
31
|
+
* `countervalues` inputs as they can change very frequently and immediate
|
|
32
|
+
* visual feedback of each of their update is not necessary for a good UX.
|
|
33
|
+
* Initially the throttle delay is low (100ms) and as soon as we get the bare
|
|
34
|
+
* minimum (total balance and the graph data for some of the accounts) we
|
|
35
|
+
* increase the throttle delay to the value of the prop `stableThrottleDelay`.
|
|
36
|
+
*
|
|
37
|
+
* We have to do this throttling as there are too many frequent updates of
|
|
38
|
+
* the `accounts` object and countervaluesState and the computation performed
|
|
39
|
+
* to aggregate all the data to make a graph is very expensive, we don't want
|
|
40
|
+
* to do this on every render as it would cause big frame drops.
|
|
41
|
+
*/
|
|
42
|
+
export function usePortfolioThrottled({ accounts, range, to, options, stableThrottleDelay = 5000, }) {
|
|
43
|
+
const [variableThrottleDelay, setVariableThrottleDelay] = useState(lowThrottleDelay);
|
|
44
|
+
const cvState = useCountervaluesState();
|
|
45
|
+
const [throttledCVState, throttledAccounts] = useThrottledValues([cvState, accounts], variableThrottleDelay);
|
|
46
|
+
/**
|
|
47
|
+
* Options are not supposed to change.
|
|
48
|
+
* Ensure a non changing reference for options to avoid an infinite setState
|
|
49
|
+
* loop if the consumer passes a new object literal to this hook at render
|
|
50
|
+
* time.
|
|
51
|
+
* */
|
|
52
|
+
const opts = useRef(options);
|
|
53
|
+
const [portfolio, setPortfolio] = useState(() => getPortfolio(throttledAccounts, range, throttledCVState, to, opts.current));
|
|
54
|
+
const ignoreUseEffectFirstCall = useRef(true);
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
if (ignoreUseEffectFirstCall.current) {
|
|
57
|
+
ignoreUseEffectFirstCall.current = false;
|
|
58
|
+
// avoid recomputing twice the initial state
|
|
59
|
+
// (once at state initialisation, then once here at mount)
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const portfolio = getPortfolio(throttledAccounts, range, throttledCVState, to, opts.current);
|
|
63
|
+
setPortfolio(portfolio);
|
|
64
|
+
if (portfolio.balanceAvailable)
|
|
65
|
+
setVariableThrottleDelay(intermediateThrottleDelay);
|
|
66
|
+
if (throttledAccounts.length > 0 && portfolio.availableAccounts.length > 0)
|
|
67
|
+
setVariableThrottleDelay(stableThrottleDelay);
|
|
68
|
+
}, [throttledAccounts, range, throttledCVState, to, stableThrottleDelay]);
|
|
69
|
+
return portfolio;
|
|
70
|
+
}
|
|
71
|
+
export function useCurrencyPortfolio({ accounts: rawAccounts, range, to, currency, }) {
|
|
72
|
+
const accounts = flattenAccounts(rawAccounts).filter(a => getAccountCurrency(a) === currency);
|
|
73
|
+
const state = useCountervaluesState();
|
|
74
|
+
return getCurrencyPortfolio(accounts, range, state, to);
|
|
75
|
+
}
|
|
76
|
+
export function useDistribution({ accounts, to, showEmptyAccounts, hideEmptyTokenAccount, }) {
|
|
77
|
+
const state = useCountervaluesState();
|
|
78
|
+
return getAssetsDistribution(accounts, state, to, {
|
|
79
|
+
minShowFirst: 6,
|
|
80
|
+
maxShowFirst: 6,
|
|
81
|
+
showFirstThreshold: 0.95,
|
|
82
|
+
showEmptyAccounts: !!showEmptyAccounts,
|
|
83
|
+
hideEmptyTokenAccount: !!hideEmptyTokenAccount,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=portfolio.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"portfolio.js","sourceRoot":"","sources":["../src/portfolio.tsx"],"names":[],"mappings":"AAUA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACpD,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,wCAAwC,CAAC;AAC7F,OAAO,EAAE,qBAAqB,EAAE,MAAM,GAAG,CAAC;AAC1C,OAAO,EAAE,kBAAkB,EAAE,MAAM,2CAA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AAK,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;AAC1D,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,EAC9B,QAAQ,EACR,EAAE,EACF,iBAAiB,EACjB,qBAAqB,GAMtB;IACC,MAAM,KAAK,GAAG,qBAAqB,EAAE,CAAC;IACtC,OAAO,qBAAqB,CAAC,QAAQ,EAAE,KAAK,EAAE,EAAE,EAAE;QAChD,YAAY,EAAE,CAAC;QACf,YAAY,EAAE,CAAC;QACf,kBAAkB,EAAE,IAAI;QACxB,iBAAiB,EAAE,CAAC,CAAC,iBAAiB;QACtC,qBAAqB,EAAE,CAAC,CAAC,qBAAqB;KAC/C,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ledgerhq/live-countervalues-react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4-next.0",
|
|
4
4
|
"description": "Ledger Live countervalues React module",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Ledger"
|
|
@@ -25,11 +25,12 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"bignumber.js": "9",
|
|
28
|
-
"@ledgerhq/types-live": "6.
|
|
29
|
-
"@ledgerhq/types-cryptoassets": "7.
|
|
30
|
-
"@ledgerhq/cryptoassets": "12.0.0",
|
|
31
|
-
"@ledgerhq/
|
|
32
|
-
"@ledgerhq/
|
|
28
|
+
"@ledgerhq/types-live": "6.46.0-next.0",
|
|
29
|
+
"@ledgerhq/types-cryptoassets": "7.11.0-next.0",
|
|
30
|
+
"@ledgerhq/cryptoassets": "12.1.0-next.0",
|
|
31
|
+
"@ledgerhq/logs": "6.12.0",
|
|
32
|
+
"@ledgerhq/live-countervalues": "0.1.4-next.0",
|
|
33
|
+
"@ledgerhq/coin-framework": "0.12.0-next.0",
|
|
33
34
|
"@ledgerhq/live-hooks": "0.1.0"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
@@ -57,6 +58,25 @@
|
|
|
57
58
|
]
|
|
58
59
|
}
|
|
59
60
|
},
|
|
61
|
+
"exports": {
|
|
62
|
+
".": {
|
|
63
|
+
"require": "./lib/index.js",
|
|
64
|
+
"default": "./lib-es/index.js"
|
|
65
|
+
},
|
|
66
|
+
"./lib/*": "./lib/*.js",
|
|
67
|
+
"./lib/*.js": "./lib/*.js",
|
|
68
|
+
"./lib-es/*": "./lib-es/*.js",
|
|
69
|
+
"./lib-es/*.js": "./lib-es/*.js",
|
|
70
|
+
"./*": {
|
|
71
|
+
"require": "./lib/*.js",
|
|
72
|
+
"default": "./lib-es/*.js"
|
|
73
|
+
},
|
|
74
|
+
"./*.js": {
|
|
75
|
+
"require": "./lib/*.js",
|
|
76
|
+
"default": "./lib-es/*.js"
|
|
77
|
+
},
|
|
78
|
+
"./package.json": "./package.json"
|
|
79
|
+
},
|
|
60
80
|
"scripts": {
|
|
61
81
|
"clean": "rimraf lib lib-es",
|
|
62
82
|
"build": "tsc && tsc -m ES6 --outDir lib-es",
|
package/src/index.tsx
CHANGED
|
@@ -17,7 +17,9 @@ import {
|
|
|
17
17
|
exportCountervalues,
|
|
18
18
|
importCountervalues,
|
|
19
19
|
inferTrackingPairForAccounts,
|
|
20
|
+
trackingPairForTopCoins,
|
|
20
21
|
} from "@ledgerhq/live-countervalues/logic";
|
|
22
|
+
import api from "@ledgerhq/live-countervalues/api/index";
|
|
21
23
|
import type {
|
|
22
24
|
CounterValuesState,
|
|
23
25
|
CounterValuesStateRaw,
|
|
@@ -25,6 +27,7 @@ import type {
|
|
|
25
27
|
TrackingPair,
|
|
26
28
|
} from "@ledgerhq/live-countervalues/types";
|
|
27
29
|
import { useDebounce } from "@ledgerhq/live-hooks/useDebounce";
|
|
30
|
+
import { log } from "@ledgerhq/logs";
|
|
28
31
|
import type { Account, AccountLike } from "@ledgerhq/types-live";
|
|
29
32
|
import type { Currency, Unit } from "@ledgerhq/types-cryptoassets";
|
|
30
33
|
|
|
@@ -66,8 +69,15 @@ const CountervaluesPollingContext = createContext<Polling>({
|
|
|
66
69
|
error: null,
|
|
67
70
|
});
|
|
68
71
|
|
|
72
|
+
const CountervaluesUserSettingsContext = createContext<CountervaluesSettings>({
|
|
73
|
+
trackingPairs: [],
|
|
74
|
+
autofillGaps: true,
|
|
75
|
+
});
|
|
76
|
+
|
|
69
77
|
const CountervaluesContext = createContext<CounterValuesState>(initialState);
|
|
70
78
|
|
|
79
|
+
const CountervaluesMarketcapIdsContext = createContext<string[]>([]);
|
|
80
|
+
|
|
71
81
|
function trackingPairsHash(a: TrackingPair[]) {
|
|
72
82
|
return a
|
|
73
83
|
.map(p => `${p.from.ticker}:${p.to.ticker}:${p.startDate.toISOString().slice(0, 10) || ""}`)
|
|
@@ -75,6 +85,57 @@ function trackingPairsHash(a: TrackingPair[]) {
|
|
|
75
85
|
.join("|");
|
|
76
86
|
}
|
|
77
87
|
|
|
88
|
+
const marketcapRefresh = 30 * 60000;
|
|
89
|
+
const marketcapRefreshOnError = 60000;
|
|
90
|
+
const initialIds: string[] = [];
|
|
91
|
+
/**
|
|
92
|
+
* Internal only. fetch the marketcap and keep it in sync.
|
|
93
|
+
* the data is shared through a context, you can useMarketcapIds to get it.
|
|
94
|
+
*/
|
|
95
|
+
function useMarketcap() {
|
|
96
|
+
const [ids, setIds] = useState<string[]>(initialIds);
|
|
97
|
+
const [fetchNonce, setFetchNonce] = useState(0);
|
|
98
|
+
|
|
99
|
+
useEffect(() => {
|
|
100
|
+
let timeout: NodeJS.Timeout | null = null;
|
|
101
|
+
api.fetchIdsSortedByMarketcap().then(
|
|
102
|
+
ids => {
|
|
103
|
+
setIds(ids);
|
|
104
|
+
timeout = setTimeout(() => setFetchNonce(n => n + 1), marketcapRefresh);
|
|
105
|
+
},
|
|
106
|
+
error => {
|
|
107
|
+
log("countervalues", "error fetching marketcap ids " + error);
|
|
108
|
+
timeout = setTimeout(() => setFetchNonce(n => n + 1), marketcapRefreshOnError);
|
|
109
|
+
},
|
|
110
|
+
);
|
|
111
|
+
return () => {
|
|
112
|
+
if (timeout) {
|
|
113
|
+
clearTimeout(timeout);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}, [fetchNonce]);
|
|
117
|
+
|
|
118
|
+
return ids;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// infer the tracking pairs for the top coins that the portfolio needs to display itself
|
|
122
|
+
// if startDate is undefined, the feature is disabled
|
|
123
|
+
export function useTrackingPairsForTopCoins(
|
|
124
|
+
marketcapIds: string[],
|
|
125
|
+
countervalue: Currency,
|
|
126
|
+
size: number,
|
|
127
|
+
startDate: Date | undefined,
|
|
128
|
+
) {
|
|
129
|
+
const dateTimestamp = startDate?.getTime();
|
|
130
|
+
return useMemo(
|
|
131
|
+
() =>
|
|
132
|
+
dateTimestamp
|
|
133
|
+
? trackingPairForTopCoins(marketcapIds, size, countervalue, new Date(dateTimestamp))
|
|
134
|
+
: [],
|
|
135
|
+
[marketcapIds, countervalue, dateTimestamp, size],
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
78
139
|
export function useTrackingPairForAccounts(
|
|
79
140
|
accounts: Account[],
|
|
80
141
|
countervalue: Currency,
|
|
@@ -90,6 +151,15 @@ export function useTrackingPairForAccounts(
|
|
|
90
151
|
return useMemo(() => c.pairs, [c.hash]);
|
|
91
152
|
}
|
|
92
153
|
|
|
154
|
+
export function CountervaluesMarketcap({ children }: { children: React.ReactNode }): ReactElement {
|
|
155
|
+
const marketcapIds = useMarketcap();
|
|
156
|
+
return (
|
|
157
|
+
<CountervaluesMarketcapIdsContext.Provider value={marketcapIds}>
|
|
158
|
+
{children}
|
|
159
|
+
</CountervaluesMarketcapIdsContext.Provider>
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
93
163
|
export function Countervalues({
|
|
94
164
|
children,
|
|
95
165
|
userSettings,
|
|
@@ -174,7 +244,9 @@ export function Countervalues({
|
|
|
174
244
|
|
|
175
245
|
return (
|
|
176
246
|
<CountervaluesPollingContext.Provider value={polling}>
|
|
177
|
-
<
|
|
247
|
+
<CountervaluesUserSettingsContext.Provider value={userSettings}>
|
|
248
|
+
<CountervaluesContext.Provider value={state}>{children}</CountervaluesContext.Provider>
|
|
249
|
+
</CountervaluesUserSettingsContext.Provider>
|
|
178
250
|
</CountervaluesPollingContext.Provider>
|
|
179
251
|
);
|
|
180
252
|
}
|
|
@@ -239,16 +311,33 @@ function fetchReducer(state: FetchState, action: Action): FetchState {
|
|
|
239
311
|
}
|
|
240
312
|
}
|
|
241
313
|
|
|
314
|
+
// allows consumer to access the countervalues polling control object
|
|
242
315
|
export function useCountervaluesPolling(): Polling {
|
|
243
316
|
return useContext(CountervaluesPollingContext);
|
|
244
317
|
}
|
|
318
|
+
|
|
319
|
+
// allows consumer to access the user settings that was used to fetch the countervalues
|
|
320
|
+
export function useCountervaluesUserSettingsContext(): CountervaluesSettings {
|
|
321
|
+
return useContext(CountervaluesUserSettingsContext);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// allows consumer to access the countervalues state
|
|
245
325
|
export function useCountervaluesState(): CounterValuesState {
|
|
246
326
|
return useContext(CountervaluesContext);
|
|
247
327
|
}
|
|
328
|
+
|
|
329
|
+
// allows consumer to access the coins ids sorted by marketcap. It's basically all the coins that the API supports.
|
|
330
|
+
export function useMarketcapIds(): string[] {
|
|
331
|
+
return useContext(CountervaluesMarketcapIdsContext);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// provides an export of the countervalues state
|
|
248
335
|
export function useCountervaluesExport(): CounterValuesStateRaw {
|
|
249
336
|
const state = useContext(CountervaluesContext);
|
|
250
337
|
return useMemo(() => exportCountervalues(state), [state]);
|
|
251
338
|
}
|
|
339
|
+
|
|
340
|
+
// provides a way to calculate a countervalue from a value
|
|
252
341
|
export function useCalculate(query: {
|
|
253
342
|
value: number;
|
|
254
343
|
from: Currency;
|
|
@@ -261,6 +350,7 @@ export function useCalculate(query: {
|
|
|
261
350
|
return calculate(state, query);
|
|
262
351
|
}
|
|
263
352
|
|
|
353
|
+
// provides a way to calculate a countervalue from a value using a callback
|
|
264
354
|
export function useCalculateCountervalueCallback({
|
|
265
355
|
to,
|
|
266
356
|
}: {
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type { CryptoCurrency, Currency, TokenCurrency } from "@ledgerhq/types-cryptoassets";
|
|
2
|
+
import type {
|
|
3
|
+
Account,
|
|
4
|
+
AccountLike,
|
|
5
|
+
AccountPortfolio,
|
|
6
|
+
AssetsDistribution,
|
|
7
|
+
CurrencyPortfolio,
|
|
8
|
+
Portfolio,
|
|
9
|
+
PortfolioRange,
|
|
10
|
+
} from "@ledgerhq/types-live";
|
|
11
|
+
import { useEffect, useRef, useState } from "react";
|
|
12
|
+
import { getAccountCurrency, flattenAccounts } from "@ledgerhq/coin-framework/account/index";
|
|
13
|
+
import { useCountervaluesState } from ".";
|
|
14
|
+
import { useThrottledValues } from "@ledgerhq/live-hooks/useThrottledFunction";
|
|
15
|
+
import {
|
|
16
|
+
getBalanceHistoryWithCountervalue,
|
|
17
|
+
getPortfolio,
|
|
18
|
+
getCurrencyPortfolio,
|
|
19
|
+
getAssetsDistribution,
|
|
20
|
+
getPortfolioCount,
|
|
21
|
+
GetPortfolioOptionsType,
|
|
22
|
+
} from "@ledgerhq/live-countervalues/portfolio";
|
|
23
|
+
|
|
24
|
+
export function useBalanceHistoryWithCountervalue({
|
|
25
|
+
account,
|
|
26
|
+
range,
|
|
27
|
+
to,
|
|
28
|
+
}: {
|
|
29
|
+
account: AccountLike;
|
|
30
|
+
range: PortfolioRange;
|
|
31
|
+
to: Currency;
|
|
32
|
+
}): AccountPortfolio {
|
|
33
|
+
const state = useCountervaluesState();
|
|
34
|
+
const count = getPortfolioCount([account], range);
|
|
35
|
+
return getBalanceHistoryWithCountervalue(account, range, count, state, to);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* WARNING: expensive hook, do not use if you don't need all the data
|
|
40
|
+
* that this computes.
|
|
41
|
+
*
|
|
42
|
+
* Computes the data necessary to plot a graph of the user's balance */
|
|
43
|
+
export function usePortfolio({
|
|
44
|
+
accounts,
|
|
45
|
+
range,
|
|
46
|
+
to,
|
|
47
|
+
options,
|
|
48
|
+
}: {
|
|
49
|
+
accounts: AccountLike[];
|
|
50
|
+
range: PortfolioRange;
|
|
51
|
+
to: Currency;
|
|
52
|
+
options?: GetPortfolioOptionsType;
|
|
53
|
+
}): Portfolio {
|
|
54
|
+
const state = useCountervaluesState();
|
|
55
|
+
return getPortfolio(accounts, range, state, to, options);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* keep a low throttling until the balance is available
|
|
60
|
+
*/
|
|
61
|
+
const lowThrottleDelay = 100;
|
|
62
|
+
/**
|
|
63
|
+
* keep an intermediate throttle until some additional data is available
|
|
64
|
+
*/
|
|
65
|
+
const intermediateThrottleDelay = 300;
|
|
66
|
+
/**
|
|
67
|
+
* Computes the data necessary to plot of a graph of the user's balance.
|
|
68
|
+
* It throttles that expensive computation by throttling the `accounts` and
|
|
69
|
+
* `countervalues` inputs as they can change very frequently and immediate
|
|
70
|
+
* visual feedback of each of their update is not necessary for a good UX.
|
|
71
|
+
* Initially the throttle delay is low (100ms) and as soon as we get the bare
|
|
72
|
+
* minimum (total balance and the graph data for some of the accounts) we
|
|
73
|
+
* increase the throttle delay to the value of the prop `stableThrottleDelay`.
|
|
74
|
+
*
|
|
75
|
+
* We have to do this throttling as there are too many frequent updates of
|
|
76
|
+
* the `accounts` object and countervaluesState and the computation performed
|
|
77
|
+
* to aggregate all the data to make a graph is very expensive, we don't want
|
|
78
|
+
* to do this on every render as it would cause big frame drops.
|
|
79
|
+
*/
|
|
80
|
+
export function usePortfolioThrottled({
|
|
81
|
+
accounts,
|
|
82
|
+
range,
|
|
83
|
+
to,
|
|
84
|
+
options,
|
|
85
|
+
stableThrottleDelay = 5000,
|
|
86
|
+
}: {
|
|
87
|
+
accounts: AccountLike[];
|
|
88
|
+
range: PortfolioRange;
|
|
89
|
+
to: Currency;
|
|
90
|
+
options?: GetPortfolioOptionsType;
|
|
91
|
+
stableThrottleDelay?: number;
|
|
92
|
+
}): Portfolio {
|
|
93
|
+
const [variableThrottleDelay, setVariableThrottleDelay] = useState(lowThrottleDelay);
|
|
94
|
+
const cvState = useCountervaluesState();
|
|
95
|
+
const [throttledCVState, throttledAccounts] = useThrottledValues(
|
|
96
|
+
[cvState, accounts],
|
|
97
|
+
variableThrottleDelay,
|
|
98
|
+
);
|
|
99
|
+
/**
|
|
100
|
+
* Options are not supposed to change.
|
|
101
|
+
* Ensure a non changing reference for options to avoid an infinite setState
|
|
102
|
+
* loop if the consumer passes a new object literal to this hook at render
|
|
103
|
+
* time.
|
|
104
|
+
* */
|
|
105
|
+
const opts = useRef(options);
|
|
106
|
+
const [portfolio, setPortfolio] = useState(() =>
|
|
107
|
+
getPortfolio(throttledAccounts, range, throttledCVState, to, opts.current),
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
const ignoreUseEffectFirstCall = useRef(true);
|
|
111
|
+
useEffect(() => {
|
|
112
|
+
if (ignoreUseEffectFirstCall.current) {
|
|
113
|
+
ignoreUseEffectFirstCall.current = false;
|
|
114
|
+
// avoid recomputing twice the initial state
|
|
115
|
+
// (once at state initialisation, then once here at mount)
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const portfolio = getPortfolio(throttledAccounts, range, throttledCVState, to, opts.current);
|
|
119
|
+
setPortfolio(portfolio);
|
|
120
|
+
if (portfolio.balanceAvailable) setVariableThrottleDelay(intermediateThrottleDelay);
|
|
121
|
+
if (throttledAccounts.length > 0 && portfolio.availableAccounts.length > 0)
|
|
122
|
+
setVariableThrottleDelay(stableThrottleDelay);
|
|
123
|
+
}, [throttledAccounts, range, throttledCVState, to, stableThrottleDelay]);
|
|
124
|
+
|
|
125
|
+
return portfolio;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function useCurrencyPortfolio({
|
|
129
|
+
accounts: rawAccounts,
|
|
130
|
+
range,
|
|
131
|
+
to,
|
|
132
|
+
currency,
|
|
133
|
+
}: {
|
|
134
|
+
accounts: Account[];
|
|
135
|
+
range: PortfolioRange;
|
|
136
|
+
to: Currency;
|
|
137
|
+
currency: CryptoCurrency | TokenCurrency;
|
|
138
|
+
}): CurrencyPortfolio {
|
|
139
|
+
const accounts = flattenAccounts(rawAccounts).filter(a => getAccountCurrency(a) === currency);
|
|
140
|
+
const state = useCountervaluesState();
|
|
141
|
+
return getCurrencyPortfolio(accounts, range, state, to);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function useDistribution({
|
|
145
|
+
accounts,
|
|
146
|
+
to,
|
|
147
|
+
showEmptyAccounts,
|
|
148
|
+
hideEmptyTokenAccount,
|
|
149
|
+
}: {
|
|
150
|
+
accounts: Account[];
|
|
151
|
+
to: Currency;
|
|
152
|
+
showEmptyAccounts?: boolean;
|
|
153
|
+
hideEmptyTokenAccount?: boolean;
|
|
154
|
+
}): AssetsDistribution {
|
|
155
|
+
const state = useCountervaluesState();
|
|
156
|
+
return getAssetsDistribution(accounts, state, to, {
|
|
157
|
+
minShowFirst: 6,
|
|
158
|
+
maxShowFirst: 6,
|
|
159
|
+
showFirstThreshold: 0.95,
|
|
160
|
+
showEmptyAccounts: !!showEmptyAccounts,
|
|
161
|
+
hideEmptyTokenAccount: !!hideEmptyTokenAccount,
|
|
162
|
+
});
|
|
163
|
+
}
|