@ledgerhq/live-countervalues-react 0.1.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.
@@ -0,0 +1,181 @@
1
+ import { BigNumber } from "bignumber.js";
2
+ import React, { createContext, useMemo, useContext, useEffect, useReducer, useState, useCallback, } from "react";
3
+ import { getAccountCurrency, getAccountUnit } from "@ledgerhq/coin-framework/account/helpers";
4
+ import { initialState, calculate, loadCountervalues, exportCountervalues, importCountervalues, inferTrackingPairForAccounts, } from "@ledgerhq/live-countervalues/logic";
5
+ import { useDebounce } from "@ledgerhq/live-hooks/useDebounce";
6
+ const CountervaluesPollingContext = createContext({
7
+ wipe: () => { },
8
+ poll: () => { },
9
+ start: () => { },
10
+ stop: () => { },
11
+ pending: false,
12
+ error: null,
13
+ });
14
+ const CountervaluesContext = createContext(initialState);
15
+ function trackingPairsHash(a) {
16
+ return a
17
+ .map(p => `${p.from.ticker}:${p.to.ticker}:${p.startDate.toISOString().slice(0, 10) || ""}`)
18
+ .sort()
19
+ .join("|");
20
+ }
21
+ export function useTrackingPairForAccounts(accounts, countervalue) {
22
+ // first we cache the tracking pairs with its hash
23
+ const c = useMemo(() => {
24
+ const pairs = inferTrackingPairForAccounts(accounts, countervalue);
25
+ return { pairs, hash: trackingPairsHash(pairs) };
26
+ }, [accounts, countervalue]);
27
+ // we only want to return the pairs when the hash changes
28
+ // to not recalculate pairs as fast as accounts resynchronizes
29
+ // eslint-disable-next-line react-hooks/exhaustive-deps
30
+ return useMemo(() => c.pairs, [c.hash]);
31
+ }
32
+ export function Countervalues({ children, userSettings, pollInitDelay = 3 * 1000, autopollInterval = 8 * 60 * 1000, debounceDelay = 1000, savedState, }) {
33
+ const debouncedUserSettings = useDebounce(userSettings, debounceDelay);
34
+ const [{ state, pending, error }, dispatch] = useReducer(fetchReducer, initialFetchState);
35
+ // flag used to trigger a loadCountervalues
36
+ const [triggerLoad, setTriggerLoad] = useState(false);
37
+ // trigger poll only when userSettings changes. in a debounced way.
38
+ useEffect(() => {
39
+ setTriggerLoad(true);
40
+ }, [debouncedUserSettings]);
41
+ // loadCountervalues logic
42
+ useEffect(() => {
43
+ if (pending || !triggerLoad)
44
+ return;
45
+ setTriggerLoad(false);
46
+ dispatch({
47
+ type: "pending",
48
+ });
49
+ loadCountervalues(state, userSettings).then(state => {
50
+ dispatch({
51
+ type: "success",
52
+ payload: state,
53
+ });
54
+ }, error => {
55
+ dispatch({
56
+ type: "error",
57
+ payload: error,
58
+ });
59
+ });
60
+ }, [pending, state, userSettings, triggerLoad]);
61
+ // save the state when it changes
62
+ useEffect(() => {
63
+ if (!(savedState === null || savedState === void 0 ? void 0 : savedState.status) || !Object.keys(savedState.status).length)
64
+ return;
65
+ dispatch({
66
+ type: "setCounterValueState",
67
+ payload: importCountervalues(savedState, userSettings),
68
+ }); // eslint-disable-next-line react-hooks/exhaustive-deps
69
+ }, [savedState]);
70
+ // manage the auto polling loop and the interface for user land to trigger a reload
71
+ const [isPolling, setIsPolling] = useState(true);
72
+ useEffect(() => {
73
+ if (!isPolling)
74
+ return;
75
+ let pollingTimeout;
76
+ function pollingLoop() {
77
+ setTriggerLoad(true);
78
+ pollingTimeout = setTimeout(pollingLoop, autopollInterval);
79
+ }
80
+ pollingTimeout = setTimeout(pollingLoop, pollInitDelay);
81
+ return () => clearTimeout(pollingTimeout);
82
+ }, [autopollInterval, pollInitDelay, isPolling]);
83
+ const polling = useMemo(() => ({
84
+ wipe: () => {
85
+ dispatch({
86
+ type: "wipe",
87
+ });
88
+ },
89
+ poll: () => setTriggerLoad(true),
90
+ start: () => setIsPolling(true),
91
+ stop: () => setIsPolling(false),
92
+ pending,
93
+ error,
94
+ }), [pending, error]);
95
+ return (React.createElement(CountervaluesPollingContext.Provider, { value: polling },
96
+ React.createElement(CountervaluesContext.Provider, { value: state }, children)));
97
+ }
98
+ const initialFetchState = {
99
+ state: initialState,
100
+ pending: false,
101
+ };
102
+ function fetchReducer(state, action) {
103
+ switch (action.type) {
104
+ case "success":
105
+ return {
106
+ state: action.payload,
107
+ pending: false,
108
+ error: undefined,
109
+ };
110
+ case "error":
111
+ return Object.assign(Object.assign({}, state), { pending: false, error: action.payload });
112
+ case "pending":
113
+ return Object.assign(Object.assign({}, state), { pending: true, error: undefined });
114
+ case "wipe":
115
+ return {
116
+ state: initialState,
117
+ pending: false,
118
+ error: undefined,
119
+ };
120
+ case "setCounterValueState":
121
+ return Object.assign(Object.assign({}, state), { state: action.payload });
122
+ default:
123
+ return state;
124
+ }
125
+ }
126
+ export function useCountervaluesPolling() {
127
+ return useContext(CountervaluesPollingContext);
128
+ }
129
+ export function useCountervaluesState() {
130
+ return useContext(CountervaluesContext);
131
+ }
132
+ export function useCountervaluesExport() {
133
+ const state = useContext(CountervaluesContext);
134
+ return useMemo(() => exportCountervalues(state), [state]);
135
+ }
136
+ export function useCalculate(query) {
137
+ const state = useCountervaluesState();
138
+ return calculate(state, query);
139
+ }
140
+ export function useCalculateCountervalueCallback({ to, }) {
141
+ const state = useCountervaluesState();
142
+ return useCallback((from, value) => {
143
+ const countervalue = calculate(state, {
144
+ value: value.toNumber(),
145
+ from,
146
+ to,
147
+ disableRounding: true,
148
+ });
149
+ return typeof countervalue === "number" ? new BigNumber(countervalue) : countervalue;
150
+ }, [to, state]);
151
+ }
152
+ export function useSendAmount({ account, fiatCurrency, cryptoAmount, }) {
153
+ const cryptoCurrency = getAccountCurrency(account);
154
+ const fiatCountervalue = useCalculate({
155
+ from: cryptoCurrency,
156
+ to: fiatCurrency,
157
+ value: cryptoAmount.toNumber(),
158
+ disableRounding: true,
159
+ });
160
+ const fiatAmount = new BigNumber(fiatCountervalue !== null && fiatCountervalue !== void 0 ? fiatCountervalue : 0);
161
+ const fiatUnit = fiatCurrency.units[0];
162
+ const cryptoUnit = getAccountUnit(account);
163
+ const state = useCountervaluesState();
164
+ const calculateCryptoAmount = useCallback((fiatAmount) => {
165
+ var _a;
166
+ const cryptoAmount = new BigNumber((_a = calculate(state, {
167
+ from: cryptoCurrency,
168
+ to: fiatCurrency,
169
+ value: fiatAmount.toNumber(),
170
+ reverse: true,
171
+ })) !== null && _a !== void 0 ? _a : 0);
172
+ return cryptoAmount;
173
+ }, [state, cryptoCurrency, fiatCurrency]);
174
+ return {
175
+ cryptoUnit,
176
+ fiatAmount,
177
+ fiatUnit,
178
+ calculateCryptoAmount,
179
+ };
180
+ }
181
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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,GAC7B,MAAM,oCAAoC,CAAC;AAO5C,OAAO,EAAE,WAAW,EAAE,MAAM,kCAAkC,CAAC;AAiC/D,MAAM,2BAA2B,GAAG,aAAa,CAAU;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,oBAAoB,GAAG,aAAa,CAAqB,YAAY,CAAC,CAAC;AAE7E,SAAS,iBAAiB,CAAC,CAAiB;IAC1C,OAAO,CAAC;SACL,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,IAAI,CAAC,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@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=react.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.test.d.ts","sourceRoot":"","sources":["../src/react.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,105 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { useTrackingPairForAccounts } from ".";
11
+ import { genAccount } from "@ledgerhq/coin-framework/mocks/account";
12
+ import { renderHook, act } from "@testing-library/react";
13
+ import { getFiatCurrencyByTicker } from "@ledgerhq/cryptoassets";
14
+ import { inferTrackingPairForAccounts } from "@ledgerhq/live-countervalues/logic";
15
+ describe("useTrackingPairForAccounts", () => {
16
+ const accounts = Array(20)
17
+ .fill(null)
18
+ .map((_, i) => genAccount("test" + i));
19
+ const usd = getFiatCurrencyByTicker("USD");
20
+ const eur = getFiatCurrencyByTicker("EUR");
21
+ const trackingPairs = inferTrackingPairForAccounts(accounts, usd);
22
+ test("it returns same tracking pairs as when using inferTrackingPairForAccounts", () => __awaiter(void 0, void 0, void 0, function* () {
23
+ const { result } = renderHook(() => useTrackingPairForAccounts(accounts, usd));
24
+ yield act(() => __awaiter(void 0, void 0, void 0, function* () {
25
+ expect(result.current).toEqual(trackingPairs);
26
+ }));
27
+ }));
28
+ test("a re-render preserve the reference", () => __awaiter(void 0, void 0, void 0, function* () {
29
+ const { result, rerender } = renderHook(() => useTrackingPairForAccounts(accounts, usd));
30
+ let initial;
31
+ yield act(() => __awaiter(void 0, void 0, void 0, function* () {
32
+ initial = result.current;
33
+ }));
34
+ rerender();
35
+ yield act(() => __awaiter(void 0, void 0, void 0, function* () {
36
+ expect(result.current).toBe(initial);
37
+ }));
38
+ }));
39
+ test("a re-render preserve the reference even when accounts change", () => __awaiter(void 0, void 0, void 0, function* () {
40
+ const { result, rerender } = renderHook(() => useTrackingPairForAccounts(accounts.slice(0), usd));
41
+ let initial;
42
+ yield act(() => __awaiter(void 0, void 0, void 0, function* () {
43
+ initial = result.current;
44
+ }));
45
+ rerender();
46
+ yield act(() => __awaiter(void 0, void 0, void 0, function* () {
47
+ expect(result.current).toBe(initial);
48
+ }));
49
+ }));
50
+ test("when accounts appears, it properly converge to the trackingPairs", () => __awaiter(void 0, void 0, void 0, function* () {
51
+ const { result, rerender } = renderHook(added => useTrackingPairForAccounts(!added ? [] : accounts, usd));
52
+ yield act(() => __awaiter(void 0, void 0, void 0, function* () {
53
+ expect(result.current).toEqual([]);
54
+ }));
55
+ rerender(true);
56
+ yield act(() => __awaiter(void 0, void 0, void 0, function* () {
57
+ expect(result.current).toEqual(trackingPairs);
58
+ }));
59
+ }));
60
+ test("when accounts changes fundamentally, pairs change", () => __awaiter(void 0, void 0, void 0, function* () {
61
+ const { result, rerender } = renderHook(empty => useTrackingPairForAccounts(empty ? [] : accounts, usd));
62
+ yield act(() => __awaiter(void 0, void 0, void 0, function* () {
63
+ expect(result.current).toEqual(trackingPairs);
64
+ }));
65
+ rerender(true);
66
+ yield act(() => __awaiter(void 0, void 0, void 0, function* () {
67
+ expect(result.current).toEqual([]);
68
+ }));
69
+ }));
70
+ test("when currency changes, pairs change", () => __awaiter(void 0, void 0, void 0, function* () {
71
+ const { result, rerender } = renderHook(usesEur => useTrackingPairForAccounts(accounts, usesEur ? eur : usd));
72
+ yield act(() => __awaiter(void 0, void 0, void 0, function* () {
73
+ expect(result.current).toEqual(trackingPairs);
74
+ }));
75
+ rerender(true);
76
+ yield act(() => __awaiter(void 0, void 0, void 0, function* () {
77
+ expect(result.current).not.toEqual(trackingPairs);
78
+ }));
79
+ }));
80
+ test("if accounts reorder, it doesn't change", () => __awaiter(void 0, void 0, void 0, function* () {
81
+ const reverse = accounts.slice(0).reverse();
82
+ const { result, rerender } = renderHook(rev => useTrackingPairForAccounts(rev ? reverse : accounts, usd));
83
+ let initial;
84
+ yield act(() => __awaiter(void 0, void 0, void 0, function* () {
85
+ initial = result.current;
86
+ }));
87
+ rerender(true);
88
+ yield act(() => __awaiter(void 0, void 0, void 0, function* () {
89
+ expect(result.current).toBe(initial);
90
+ }));
91
+ }));
92
+ test("if accounts doubles, it doesn't change", () => __awaiter(void 0, void 0, void 0, function* () {
93
+ const doubled = accounts.concat(accounts);
94
+ const { result, rerender } = renderHook(d => useTrackingPairForAccounts(d ? doubled : accounts, usd));
95
+ let initial;
96
+ yield act(() => __awaiter(void 0, void 0, void 0, function* () {
97
+ initial = result.current;
98
+ }));
99
+ rerender(true);
100
+ yield act(() => __awaiter(void 0, void 0, void 0, function* () {
101
+ expect(result.current).toBe(initial);
102
+ }));
103
+ }));
104
+ });
105
+ //# sourceMappingURL=react.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.test.js","sourceRoot":"","sources":["../src/react.test.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,GAAG,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,wCAAwC,CAAC;AACpE,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AACjE,OAAO,EAAE,4BAA4B,EAAE,MAAM,oCAAoC,CAAC;AAGlF,QAAQ,CAAC,4BAA4B,EAAE,GAAG,EAAE;IAC1C,MAAM,QAAQ,GAAG,KAAK,CAAC,EAAE,CAAC;SACvB,IAAI,CAAC,IAAI,CAAC;SACV,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,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1D,CAAC;QACF,IAAI,OAAmC,CAAC;QACxC,MAAM,GAAG,CAAC,GAAS,EAAE;YACnB,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;QAC3B,CAAC,CAAA,CAAC,CAAC;QACH,QAAQ,CAAC,IAAI,CAAC,CAAC;QACf,MAAM,GAAG,CAAC,GAAS,EAAE;YACnB,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACvC,CAAC,CAAA,CAAC,CAAC;IACL,CAAC,CAAA,CAAC,CAAC;IAEH,IAAI,CAAC,wCAAwC,EAAE,GAAS,EAAE;QACxD,MAAM,OAAO,GAAG,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAC1C,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,UAAU,CAAC,CAAC,CAAC,EAAE,CAC1C,0BAA0B,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,EAAE,GAAG,CAAC,CACxD,CAAC;QACF,IAAI,OAAmC,CAAC;QACxC,MAAM,GAAG,CAAC,GAAS,EAAE;YACnB,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;QAC3B,CAAC,CAAA,CAAC,CAAC;QACH,QAAQ,CAAC,IAAI,CAAC,CAAC;QACf,MAAM,GAAG,CAAC,GAAS,EAAE;YACnB,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACvC,CAAC,CAAA,CAAC,CAAC;IACL,CAAC,CAAA,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,71 @@
1
+ {
2
+ "name": "@ledgerhq/live-countervalues-react",
3
+ "version": "0.1.0",
4
+ "description": "Ledger Live countervalues React module",
5
+ "keywords": [
6
+ "Ledger"
7
+ ],
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/LedgerHQ/ledger-live.git"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/LedgerHQ/ledger-live/issues"
14
+ },
15
+ "homepage": "https://github.com/LedgerHQ/ledger-live/tree/develop/libs/env",
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "main": "lib/index.js",
20
+ "module": "lib-es/index.js",
21
+ "types": "lib/index.d.ts",
22
+ "license": "Apache-2.0",
23
+ "peerDependencies": {
24
+ "react": ">=16"
25
+ },
26
+ "dependencies": {
27
+ "bignumber.js": "9",
28
+ "@ledgerhq/types-live": "6.44.0",
29
+ "@ledgerhq/types-cryptoassets": "7.9.0",
30
+ "@ledgerhq/cryptoassets": "11.4.0",
31
+ "@ledgerhq/live-countervalues": "0.1.0",
32
+ "@ledgerhq/coin-framework": "0.11.0",
33
+ "@ledgerhq/live-hooks": "0.1.0"
34
+ },
35
+ "devDependencies": {
36
+ "@types/jest": "^29.5.10",
37
+ "@testing-library/react": "14",
38
+ "react": "^18.2.0",
39
+ "react-dom": "^18.2.0",
40
+ "jest": "^29.7.0",
41
+ "jest-environment-jsdom": "^29.7.0",
42
+ "ts-jest": "^29.1.1"
43
+ },
44
+ "typesVersions": {
45
+ "*": {
46
+ "*.json": [
47
+ "*.json"
48
+ ],
49
+ "*": [
50
+ "lib/*"
51
+ ],
52
+ "lib/*": [
53
+ "lib/*"
54
+ ],
55
+ "lib-es/*": [
56
+ "lib-es/*"
57
+ ]
58
+ }
59
+ },
60
+ "scripts": {
61
+ "clean": "rimraf lib lib-es",
62
+ "build": "tsc && tsc -m ES6 --outDir lib-es",
63
+ "prewatch": "pnpm build",
64
+ "watch": "tsc --watch",
65
+ "lint": "eslint ./src --no-error-on-unmatched-pattern --ext .ts,.tsx --cache",
66
+ "lint:fix": "pnpm lint --fix",
67
+ "typecheck": "tsc --noEmit",
68
+ "unimported": "unimported",
69
+ "test": "jest"
70
+ }
71
+ }