@b3dotfun/sdk 0.0.43-alpha.3 → 0.0.43-alpha.4
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/dist/cjs/shared/react/hooks/useCurrencyConversion.d.ts +1 -1
- package/dist/cjs/shared/react/hooks/useCurrencyConversion.js +30 -12
- package/dist/esm/shared/react/hooks/useCurrencyConversion.d.ts +1 -1
- package/dist/esm/shared/react/hooks/useCurrencyConversion.js +30 -12
- package/dist/types/shared/react/hooks/useCurrencyConversion.d.ts +1 -1
- package/package.json +1 -1
- package/src/shared/react/hooks/useCurrencyConversion.ts +39 -12
|
@@ -20,7 +20,7 @@ export declare function useCurrencyConversion(): {
|
|
|
20
20
|
/** Base currency used for conversion (typically B3) */
|
|
21
21
|
baseCurrency: import("../stores/currencyStore").SupportedCurrency;
|
|
22
22
|
/** Current exchange rate from base to selected currency (undefined while loading) */
|
|
23
|
-
exchangeRate: number;
|
|
23
|
+
exchangeRate: number | undefined;
|
|
24
24
|
/** Format a value with currency conversion and proper symbol/decimal handling */
|
|
25
25
|
formatCurrencyValue: (value: number, options?: {
|
|
26
26
|
decimals?: number;
|
|
@@ -1,9 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useCurrencyConversion = useCurrencyConversion;
|
|
4
|
-
const
|
|
4
|
+
const react_query_1 = require("@tanstack/react-query");
|
|
5
5
|
const number_1 = require("../../../shared/utils/number");
|
|
6
6
|
const currencyStore_1 = require("../stores/currencyStore");
|
|
7
|
+
const COINBASE_API_URL = "https://api.coinbase.com/v2/exchange-rates";
|
|
8
|
+
const REFETCH_INTERVAL_MS = 30000;
|
|
9
|
+
/**
|
|
10
|
+
* Fetches all exchange rates for a given base currency from Coinbase API.
|
|
11
|
+
*/
|
|
12
|
+
async function fetchAllExchangeRates(baseCurrency) {
|
|
13
|
+
const response = await fetch(`${COINBASE_API_URL}?currency=${baseCurrency}`);
|
|
14
|
+
if (!response.ok) {
|
|
15
|
+
throw new Error(`Failed to fetch exchange rates for ${baseCurrency}: ${response.status}`);
|
|
16
|
+
}
|
|
17
|
+
const data = await response.json();
|
|
18
|
+
const rates = {};
|
|
19
|
+
for (const [currency, rate] of Object.entries(data.data.rates)) {
|
|
20
|
+
rates[currency] = parseFloat(rate);
|
|
21
|
+
}
|
|
22
|
+
return rates;
|
|
23
|
+
}
|
|
7
24
|
/**
|
|
8
25
|
* Hook for currency conversion and formatting with real-time exchange rates.
|
|
9
26
|
*
|
|
@@ -23,16 +40,18 @@ const currencyStore_1 = require("../stores/currencyStore");
|
|
|
23
40
|
function useCurrencyConversion() {
|
|
24
41
|
const selectedCurrency = (0, currencyStore_1.useCurrencyStore)(state => state.selectedCurrency);
|
|
25
42
|
const baseCurrency = (0, currencyStore_1.useCurrencyStore)(state => state.baseCurrency);
|
|
26
|
-
//
|
|
27
|
-
const {
|
|
28
|
-
baseCurrency,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
quoteCurrency: "USD",
|
|
43
|
+
// Fetch all exchange rates for the base currency
|
|
44
|
+
const { data: exchangeRates } = (0, react_query_1.useQuery)({
|
|
45
|
+
queryKey: ["exchangeRates", baseCurrency],
|
|
46
|
+
queryFn: () => fetchAllExchangeRates(baseCurrency),
|
|
47
|
+
refetchInterval: REFETCH_INTERVAL_MS,
|
|
48
|
+
staleTime: REFETCH_INTERVAL_MS / 2,
|
|
49
|
+
retry: 3,
|
|
50
|
+
retryDelay: attemptIndex => Math.min(1000 * 2 ** attemptIndex, REFETCH_INTERVAL_MS),
|
|
35
51
|
});
|
|
52
|
+
// Extract specific rates from the full rates object
|
|
53
|
+
const exchangeRate = exchangeRates?.[selectedCurrency];
|
|
54
|
+
const usdRate = exchangeRates?.["USD"];
|
|
36
55
|
/**
|
|
37
56
|
* Formats a numeric value as a currency string with proper conversion and formatting.
|
|
38
57
|
*
|
|
@@ -78,8 +97,7 @@ function useCurrencyConversion() {
|
|
|
78
97
|
});
|
|
79
98
|
return `${formatted} ${baseCurrency}`;
|
|
80
99
|
}
|
|
81
|
-
// If
|
|
82
|
-
// incorrect values with wrong currency symbols during rate fetching
|
|
100
|
+
// If showing base currency, no conversion needed
|
|
83
101
|
if (selectedCurrency === baseCurrency || !exchangeRate) {
|
|
84
102
|
const formatted = (0, number_1.formatDisplayNumber)(value, {
|
|
85
103
|
significantDigits: baseCurrency === "B3" ? 6 : 8,
|
|
@@ -20,7 +20,7 @@ export declare function useCurrencyConversion(): {
|
|
|
20
20
|
/** Base currency used for conversion (typically B3) */
|
|
21
21
|
baseCurrency: import("../stores/currencyStore").SupportedCurrency;
|
|
22
22
|
/** Current exchange rate from base to selected currency (undefined while loading) */
|
|
23
|
-
exchangeRate: number;
|
|
23
|
+
exchangeRate: number | undefined;
|
|
24
24
|
/** Format a value with currency conversion and proper symbol/decimal handling */
|
|
25
25
|
formatCurrencyValue: (value: number, options?: {
|
|
26
26
|
decimals?: number;
|
|
@@ -1,6 +1,23 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useQuery } from "@tanstack/react-query";
|
|
2
2
|
import { formatDisplayNumber } from "../../../shared/utils/number.js";
|
|
3
3
|
import { CURRENCY_SYMBOLS, useCurrencyStore } from "../stores/currencyStore.js";
|
|
4
|
+
const COINBASE_API_URL = "https://api.coinbase.com/v2/exchange-rates";
|
|
5
|
+
const REFETCH_INTERVAL_MS = 30000;
|
|
6
|
+
/**
|
|
7
|
+
* Fetches all exchange rates for a given base currency from Coinbase API.
|
|
8
|
+
*/
|
|
9
|
+
async function fetchAllExchangeRates(baseCurrency) {
|
|
10
|
+
const response = await fetch(`${COINBASE_API_URL}?currency=${baseCurrency}`);
|
|
11
|
+
if (!response.ok) {
|
|
12
|
+
throw new Error(`Failed to fetch exchange rates for ${baseCurrency}: ${response.status}`);
|
|
13
|
+
}
|
|
14
|
+
const data = await response.json();
|
|
15
|
+
const rates = {};
|
|
16
|
+
for (const [currency, rate] of Object.entries(data.data.rates)) {
|
|
17
|
+
rates[currency] = parseFloat(rate);
|
|
18
|
+
}
|
|
19
|
+
return rates;
|
|
20
|
+
}
|
|
4
21
|
/**
|
|
5
22
|
* Hook for currency conversion and formatting with real-time exchange rates.
|
|
6
23
|
*
|
|
@@ -20,16 +37,18 @@ import { CURRENCY_SYMBOLS, useCurrencyStore } from "../stores/currencyStore.js";
|
|
|
20
37
|
export function useCurrencyConversion() {
|
|
21
38
|
const selectedCurrency = useCurrencyStore(state => state.selectedCurrency);
|
|
22
39
|
const baseCurrency = useCurrencyStore(state => state.baseCurrency);
|
|
23
|
-
//
|
|
24
|
-
const {
|
|
25
|
-
baseCurrency,
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
quoteCurrency: "USD",
|
|
40
|
+
// Fetch all exchange rates for the base currency
|
|
41
|
+
const { data: exchangeRates } = useQuery({
|
|
42
|
+
queryKey: ["exchangeRates", baseCurrency],
|
|
43
|
+
queryFn: () => fetchAllExchangeRates(baseCurrency),
|
|
44
|
+
refetchInterval: REFETCH_INTERVAL_MS,
|
|
45
|
+
staleTime: REFETCH_INTERVAL_MS / 2,
|
|
46
|
+
retry: 3,
|
|
47
|
+
retryDelay: attemptIndex => Math.min(1000 * 2 ** attemptIndex, REFETCH_INTERVAL_MS),
|
|
32
48
|
});
|
|
49
|
+
// Extract specific rates from the full rates object
|
|
50
|
+
const exchangeRate = exchangeRates?.[selectedCurrency];
|
|
51
|
+
const usdRate = exchangeRates?.["USD"];
|
|
33
52
|
/**
|
|
34
53
|
* Formats a numeric value as a currency string with proper conversion and formatting.
|
|
35
54
|
*
|
|
@@ -75,8 +94,7 @@ export function useCurrencyConversion() {
|
|
|
75
94
|
});
|
|
76
95
|
return `${formatted} ${baseCurrency}`;
|
|
77
96
|
}
|
|
78
|
-
// If
|
|
79
|
-
// incorrect values with wrong currency symbols during rate fetching
|
|
97
|
+
// If showing base currency, no conversion needed
|
|
80
98
|
if (selectedCurrency === baseCurrency || !exchangeRate) {
|
|
81
99
|
const formatted = formatDisplayNumber(value, {
|
|
82
100
|
significantDigits: baseCurrency === "B3" ? 6 : 8,
|
|
@@ -20,7 +20,7 @@ export declare function useCurrencyConversion(): {
|
|
|
20
20
|
/** Base currency used for conversion (typically B3) */
|
|
21
21
|
baseCurrency: import("../stores/currencyStore").SupportedCurrency;
|
|
22
22
|
/** Current exchange rate from base to selected currency (undefined while loading) */
|
|
23
|
-
exchangeRate: number;
|
|
23
|
+
exchangeRate: number | undefined;
|
|
24
24
|
/** Format a value with currency conversion and proper symbol/decimal handling */
|
|
25
25
|
formatCurrencyValue: (value: number, options?: {
|
|
26
26
|
decimals?: number;
|
package/package.json
CHANGED
|
@@ -1,7 +1,33 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useQuery } from "@tanstack/react-query";
|
|
2
2
|
import { formatDisplayNumber } from "@b3dotfun/sdk/shared/utils/number";
|
|
3
3
|
import { CURRENCY_SYMBOLS, useCurrencyStore } from "../stores/currencyStore";
|
|
4
4
|
|
|
5
|
+
const COINBASE_API_URL = "https://api.coinbase.com/v2/exchange-rates";
|
|
6
|
+
const REFETCH_INTERVAL_MS = 30000;
|
|
7
|
+
|
|
8
|
+
interface CoinbaseExchangeRatesResponse {
|
|
9
|
+
data: {
|
|
10
|
+
currency: string;
|
|
11
|
+
rates: Record<string, string>;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Fetches all exchange rates for a given base currency from Coinbase API.
|
|
17
|
+
*/
|
|
18
|
+
async function fetchAllExchangeRates(baseCurrency: string): Promise<Record<string, number>> {
|
|
19
|
+
const response = await fetch(`${COINBASE_API_URL}?currency=${baseCurrency}`);
|
|
20
|
+
if (!response.ok) {
|
|
21
|
+
throw new Error(`Failed to fetch exchange rates for ${baseCurrency}: ${response.status}`);
|
|
22
|
+
}
|
|
23
|
+
const data: CoinbaseExchangeRatesResponse = await response.json();
|
|
24
|
+
const rates: Record<string, number> = {};
|
|
25
|
+
for (const [currency, rate] of Object.entries(data.data.rates)) {
|
|
26
|
+
rates[currency] = parseFloat(rate);
|
|
27
|
+
}
|
|
28
|
+
return rates;
|
|
29
|
+
}
|
|
30
|
+
|
|
5
31
|
/**
|
|
6
32
|
* Hook for currency conversion and formatting with real-time exchange rates.
|
|
7
33
|
*
|
|
@@ -22,17 +48,19 @@ export function useCurrencyConversion() {
|
|
|
22
48
|
const selectedCurrency = useCurrencyStore(state => state.selectedCurrency);
|
|
23
49
|
const baseCurrency = useCurrencyStore(state => state.baseCurrency);
|
|
24
50
|
|
|
25
|
-
//
|
|
26
|
-
const {
|
|
27
|
-
baseCurrency,
|
|
28
|
-
|
|
51
|
+
// Fetch all exchange rates for the base currency
|
|
52
|
+
const { data: exchangeRates } = useQuery({
|
|
53
|
+
queryKey: ["exchangeRates", baseCurrency],
|
|
54
|
+
queryFn: () => fetchAllExchangeRates(baseCurrency),
|
|
55
|
+
refetchInterval: REFETCH_INTERVAL_MS,
|
|
56
|
+
staleTime: REFETCH_INTERVAL_MS / 2,
|
|
57
|
+
retry: 3,
|
|
58
|
+
retryDelay: attemptIndex => Math.min(1000 * 2 ** attemptIndex, REFETCH_INTERVAL_MS),
|
|
29
59
|
});
|
|
30
60
|
|
|
31
|
-
//
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
quoteCurrency: "USD",
|
|
35
|
-
});
|
|
61
|
+
// Extract specific rates from the full rates object
|
|
62
|
+
const exchangeRate = exchangeRates?.[selectedCurrency];
|
|
63
|
+
const usdRate = exchangeRates?.["USD"];
|
|
36
64
|
|
|
37
65
|
/**
|
|
38
66
|
* Formats a numeric value as a currency string with proper conversion and formatting.
|
|
@@ -83,8 +111,7 @@ export function useCurrencyConversion() {
|
|
|
83
111
|
return `${formatted} ${baseCurrency}`;
|
|
84
112
|
}
|
|
85
113
|
|
|
86
|
-
// If
|
|
87
|
-
// incorrect values with wrong currency symbols during rate fetching
|
|
114
|
+
// If showing base currency, no conversion needed
|
|
88
115
|
if (selectedCurrency === baseCurrency || !exchangeRate) {
|
|
89
116
|
const formatted = formatDisplayNumber(value, {
|
|
90
117
|
significantDigits: baseCurrency === "B3" ? 6 : 8,
|