@b3dotfun/sdk 0.0.42-alpha.1 → 0.0.42-alpha.3

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.
Files changed (111) hide show
  1. package/dist/cjs/anyspend/react/components/AnySpend.d.ts +1 -0
  2. package/dist/cjs/anyspend/react/components/AnySpend.js +2 -2
  3. package/dist/cjs/anyspend/react/components/AnyspendDepositHype.d.ts +1 -0
  4. package/dist/cjs/anyspend/react/components/AnyspendDepositHype.js +2 -2
  5. package/dist/cjs/anyspend/react/components/common/PanelOnramp.d.ts +2 -1
  6. package/dist/cjs/anyspend/react/components/common/PanelOnramp.js +5 -3
  7. package/dist/cjs/global-account/react/components/SignInWithB3/SignIn.js +1 -1
  8. package/dist/cjs/global-account/react/components/index.d.ts +8 -7
  9. package/dist/cjs/global-account/react/components/index.js +29 -23
  10. package/dist/cjs/global-account/react/components/ui/dropdown-menu.d.ts +27 -0
  11. package/dist/cjs/global-account/react/components/ui/dropdown-menu.js +100 -0
  12. package/dist/cjs/global-account/react/stores/useModalStore.d.ts +4 -0
  13. package/dist/cjs/shared/constants/currency.d.ts +1 -0
  14. package/dist/cjs/shared/constants/currency.js +5 -0
  15. package/dist/cjs/shared/constants/index.d.ts +1 -0
  16. package/dist/cjs/shared/constants/index.js +15 -0
  17. package/dist/cjs/shared/react/components/CurrencySelector.d.ts +7 -0
  18. package/dist/cjs/shared/react/components/CurrencySelector.js +14 -0
  19. package/dist/cjs/shared/react/components/FormattedCurrency.d.ts +12 -0
  20. package/dist/cjs/shared/react/components/FormattedCurrency.js +60 -0
  21. package/dist/cjs/shared/react/components/index.d.ts +2 -0
  22. package/dist/cjs/shared/react/components/index.js +18 -0
  23. package/dist/cjs/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts +1 -0
  24. package/dist/cjs/shared/react/hooks/__tests__/useCurrencyConversion.test.js +245 -0
  25. package/dist/cjs/shared/react/hooks/index.d.ts +1 -0
  26. package/dist/cjs/shared/react/hooks/index.js +1 -0
  27. package/dist/cjs/shared/react/hooks/useCurrencyConversion.d.ts +35 -0
  28. package/dist/cjs/shared/react/hooks/useCurrencyConversion.js +200 -0
  29. package/dist/cjs/shared/react/index.d.ts +2 -0
  30. package/dist/cjs/shared/react/index.js +2 -0
  31. package/dist/cjs/shared/react/stores/currencyModalStore.d.ts +7 -0
  32. package/dist/cjs/shared/react/stores/currencyModalStore.js +9 -0
  33. package/dist/cjs/shared/react/stores/currencyStore.d.ts +51 -0
  34. package/dist/cjs/shared/react/stores/currencyStore.js +57 -0
  35. package/dist/cjs/shared/react/stores/index.d.ts +2 -0
  36. package/dist/cjs/shared/react/stores/index.js +18 -0
  37. package/dist/esm/anyspend/react/components/AnySpend.d.ts +1 -0
  38. package/dist/esm/anyspend/react/components/AnySpend.js +2 -2
  39. package/dist/esm/anyspend/react/components/AnyspendDepositHype.d.ts +1 -0
  40. package/dist/esm/anyspend/react/components/AnyspendDepositHype.js +2 -2
  41. package/dist/esm/anyspend/react/components/common/PanelOnramp.d.ts +2 -1
  42. package/dist/esm/anyspend/react/components/common/PanelOnramp.js +5 -3
  43. package/dist/esm/global-account/react/components/SignInWithB3/SignIn.js +1 -1
  44. package/dist/esm/global-account/react/components/index.d.ts +8 -7
  45. package/dist/esm/global-account/react/components/index.js +8 -7
  46. package/dist/esm/global-account/react/components/ui/dropdown-menu.d.ts +27 -0
  47. package/dist/esm/global-account/react/components/ui/dropdown-menu.js +60 -0
  48. package/dist/esm/global-account/react/stores/useModalStore.d.ts +4 -0
  49. package/dist/esm/shared/constants/currency.d.ts +1 -0
  50. package/dist/esm/shared/constants/currency.js +2 -0
  51. package/dist/esm/shared/constants/index.d.ts +1 -0
  52. package/dist/esm/shared/constants/index.js +1 -0
  53. package/dist/esm/shared/react/components/CurrencySelector.d.ts +7 -0
  54. package/dist/esm/shared/react/components/CurrencySelector.js +11 -0
  55. package/dist/esm/shared/react/components/FormattedCurrency.d.ts +12 -0
  56. package/dist/esm/shared/react/components/FormattedCurrency.js +57 -0
  57. package/dist/esm/shared/react/components/index.d.ts +2 -0
  58. package/dist/esm/shared/react/components/index.js +2 -0
  59. package/dist/esm/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts +1 -0
  60. package/dist/esm/shared/react/hooks/__tests__/useCurrencyConversion.test.js +243 -0
  61. package/dist/esm/shared/react/hooks/index.d.ts +1 -0
  62. package/dist/esm/shared/react/hooks/index.js +1 -0
  63. package/dist/esm/shared/react/hooks/useCurrencyConversion.d.ts +35 -0
  64. package/dist/esm/shared/react/hooks/useCurrencyConversion.js +197 -0
  65. package/dist/esm/shared/react/index.d.ts +2 -0
  66. package/dist/esm/shared/react/index.js +2 -0
  67. package/dist/esm/shared/react/stores/currencyModalStore.d.ts +7 -0
  68. package/dist/esm/shared/react/stores/currencyModalStore.js +6 -0
  69. package/dist/esm/shared/react/stores/currencyStore.d.ts +51 -0
  70. package/dist/esm/shared/react/stores/currencyStore.js +54 -0
  71. package/dist/esm/shared/react/stores/index.d.ts +2 -0
  72. package/dist/esm/shared/react/stores/index.js +2 -0
  73. package/dist/styles/index.css +1 -1
  74. package/dist/types/anyspend/react/components/AnySpend.d.ts +1 -0
  75. package/dist/types/anyspend/react/components/AnyspendDepositHype.d.ts +1 -0
  76. package/dist/types/anyspend/react/components/common/PanelOnramp.d.ts +2 -1
  77. package/dist/types/global-account/react/components/index.d.ts +8 -7
  78. package/dist/types/global-account/react/components/ui/dropdown-menu.d.ts +27 -0
  79. package/dist/types/global-account/react/stores/useModalStore.d.ts +4 -0
  80. package/dist/types/shared/constants/currency.d.ts +1 -0
  81. package/dist/types/shared/constants/index.d.ts +1 -0
  82. package/dist/types/shared/react/components/CurrencySelector.d.ts +7 -0
  83. package/dist/types/shared/react/components/FormattedCurrency.d.ts +12 -0
  84. package/dist/types/shared/react/components/index.d.ts +2 -0
  85. package/dist/types/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts +1 -0
  86. package/dist/types/shared/react/hooks/index.d.ts +1 -0
  87. package/dist/types/shared/react/hooks/useCurrencyConversion.d.ts +35 -0
  88. package/dist/types/shared/react/index.d.ts +2 -0
  89. package/dist/types/shared/react/stores/currencyModalStore.d.ts +7 -0
  90. package/dist/types/shared/react/stores/currencyStore.d.ts +51 -0
  91. package/dist/types/shared/react/stores/index.d.ts +2 -0
  92. package/package.json +29 -3
  93. package/src/anyspend/react/components/AnySpend.tsx +4 -0
  94. package/src/anyspend/react/components/AnyspendDepositHype.tsx +3 -0
  95. package/src/anyspend/react/components/common/PanelOnramp.tsx +19 -15
  96. package/src/global-account/react/components/SignInWithB3/SignIn.tsx +2 -7
  97. package/src/global-account/react/components/index.ts +19 -12
  98. package/src/global-account/react/components/ui/dropdown-menu.tsx +132 -0
  99. package/src/global-account/react/stores/useModalStore.ts +4 -0
  100. package/src/shared/constants/currency.ts +2 -0
  101. package/src/shared/constants/index.ts +2 -0
  102. package/src/shared/react/components/CurrencySelector.tsx +71 -0
  103. package/src/shared/react/components/FormattedCurrency.tsx +106 -0
  104. package/src/shared/react/components/index.ts +2 -0
  105. package/src/shared/react/hooks/__tests__/useCurrencyConversion.test.ts +308 -0
  106. package/src/shared/react/hooks/index.ts +1 -0
  107. package/src/shared/react/hooks/useCurrencyConversion.ts +211 -0
  108. package/src/shared/react/index.ts +2 -0
  109. package/src/shared/react/stores/currencyModalStore.ts +13 -0
  110. package/src/shared/react/stores/currencyStore.ts +82 -0
  111. package/src/shared/react/stores/index.ts +2 -0
@@ -0,0 +1,243 @@
1
+ import { describe, it, expect, vi, beforeEach } from "vitest";
2
+ import { renderHook } from "@testing-library/react";
3
+ import { useCurrencyConversion } from "../useCurrencyConversion.js";
4
+ // Mock the external dependencies
5
+ // Store mock rates for different quote currencies
6
+ const mockRates = {};
7
+ // Mock store state
8
+ const mockStoreState = {
9
+ selectedCurrency: "B3",
10
+ baseCurrency: "B3",
11
+ setSelectedCurrency: vi.fn(),
12
+ setBaseCurrency: vi.fn(),
13
+ };
14
+ vi.mock("@b3dotfun/sdk/global-account/react", () => ({
15
+ useExchangeRate: vi.fn((params) => {
16
+ const rate = mockRates[params?.quoteCurrency];
17
+ return { rate };
18
+ }),
19
+ }));
20
+ vi.mock("@b3dotfun/sdk/shared/utils/number", () => ({
21
+ formatDisplayNumber: vi.fn((value) => {
22
+ const num = Number(value);
23
+ if (isNaN(num))
24
+ return "0";
25
+ return num.toLocaleString("en-US", { maximumFractionDigits: 6 });
26
+ }),
27
+ }));
28
+ vi.mock("../../stores/currencyStore", () => ({
29
+ useCurrencyStore: vi.fn((selector) => {
30
+ if (selector) {
31
+ return selector(mockStoreState);
32
+ }
33
+ return mockStoreState;
34
+ }),
35
+ CURRENCY_SYMBOLS: {
36
+ B3: "B3",
37
+ USD: "$",
38
+ EUR: "€",
39
+ GBP: "£",
40
+ JPY: "¥",
41
+ CAD: "C$",
42
+ AUD: "A$",
43
+ ETH: "ETH",
44
+ SOL: "SOL",
45
+ KRW: "₩",
46
+ },
47
+ }));
48
+ describe("useCurrencyConversion", () => {
49
+ beforeEach(() => {
50
+ vi.clearAllMocks();
51
+ // Reset mock rates to default
52
+ Object.keys(mockRates).forEach(key => delete mockRates[key]);
53
+ mockRates.USD = 1.0;
54
+ // Reset store state
55
+ mockStoreState.selectedCurrency = "B3";
56
+ mockStoreState.baseCurrency = "B3";
57
+ });
58
+ describe("formatCurrencyValue", () => {
59
+ it("should format base currency (B3) without conversion", () => {
60
+ mockStoreState.selectedCurrency = "B3";
61
+ mockStoreState.baseCurrency = "B3";
62
+ const { result } = renderHook(() => useCurrencyConversion());
63
+ const formatted = result.current.formatCurrencyValue(100);
64
+ expect(formatted).toContain("B3");
65
+ expect(formatted).toContain("100");
66
+ });
67
+ it("should show base currency when exchange rate is unavailable", () => {
68
+ mockRates.USD = undefined;
69
+ mockStoreState.selectedCurrency = "USD";
70
+ mockStoreState.baseCurrency = "B3";
71
+ const { result } = renderHook(() => useCurrencyConversion());
72
+ const formatted = result.current.formatCurrencyValue(100);
73
+ expect(formatted).toContain("B3");
74
+ expect(formatted).not.toContain("$");
75
+ });
76
+ it("should format USD with prefix symbol", () => {
77
+ mockRates.USD = 2.0;
78
+ mockStoreState.selectedCurrency = "USD";
79
+ mockStoreState.baseCurrency = "B3";
80
+ const { result } = renderHook(() => useCurrencyConversion());
81
+ const formatted = result.current.formatCurrencyValue(100);
82
+ expect(formatted).toMatch(/^\$/);
83
+ expect(formatted).toContain("200");
84
+ });
85
+ it("should format EUR with prefix symbol", () => {
86
+ mockRates.EUR = 1.8;
87
+ mockRates.USD = 2.0;
88
+ mockStoreState.selectedCurrency = "EUR";
89
+ mockStoreState.baseCurrency = "B3";
90
+ const { result } = renderHook(() => useCurrencyConversion());
91
+ const formatted = result.current.formatCurrencyValue(100);
92
+ expect(formatted).toMatch(/^€/);
93
+ });
94
+ it("should format JPY without decimals", () => {
95
+ mockRates.JPY = 150;
96
+ mockRates.USD = 2.0;
97
+ mockStoreState.selectedCurrency = "JPY";
98
+ mockStoreState.baseCurrency = "B3";
99
+ const { result } = renderHook(() => useCurrencyConversion());
100
+ const formatted = result.current.formatCurrencyValue(100);
101
+ expect(formatted).toContain("¥");
102
+ expect(formatted).not.toContain(".");
103
+ });
104
+ it("should format KRW without decimals", () => {
105
+ mockRates.KRW = 1300;
106
+ mockRates.USD = 2.0;
107
+ mockStoreState.selectedCurrency = "KRW";
108
+ mockStoreState.baseCurrency = "B3";
109
+ const { result } = renderHook(() => useCurrencyConversion());
110
+ const formatted = result.current.formatCurrencyValue(100);
111
+ expect(formatted).toContain("₩");
112
+ expect(formatted).not.toContain(".");
113
+ });
114
+ it("should format ETH with suffix symbol", () => {
115
+ mockRates.ETH = 0.0005;
116
+ mockRates.USD = 2.0;
117
+ mockStoreState.selectedCurrency = "ETH";
118
+ mockStoreState.baseCurrency = "B3";
119
+ const { result } = renderHook(() => useCurrencyConversion());
120
+ const formatted = result.current.formatCurrencyValue(100);
121
+ expect(formatted).toContain("ETH");
122
+ expect(formatted).not.toMatch(/^ETH/);
123
+ });
124
+ it("should format SOL with suffix symbol", () => {
125
+ mockRates.SOL = 0.05;
126
+ mockRates.USD = 2.0;
127
+ mockStoreState.selectedCurrency = "SOL";
128
+ mockStoreState.baseCurrency = "B3";
129
+ const { result } = renderHook(() => useCurrencyConversion());
130
+ const formatted = result.current.formatCurrencyValue(100);
131
+ expect(formatted).toContain("SOL");
132
+ expect(formatted).not.toMatch(/^SOL/);
133
+ });
134
+ it("should handle small USD amounts with proper conversion", () => {
135
+ mockRates.USD = 1.5;
136
+ mockStoreState.selectedCurrency = "USD";
137
+ mockStoreState.baseCurrency = "B3";
138
+ const { result } = renderHook(() => useCurrencyConversion());
139
+ const formatted = result.current.formatCurrencyValue(10);
140
+ // 10 * 1.5 = 15
141
+ expect(formatted).toMatch(/^\$/);
142
+ expect(formatted).toContain("15");
143
+ });
144
+ it("should apply correct exchange rate conversion", () => {
145
+ const testRate = 3.5;
146
+ mockRates.USD = testRate;
147
+ mockStoreState.selectedCurrency = "USD";
148
+ mockStoreState.baseCurrency = "B3";
149
+ const { result } = renderHook(() => useCurrencyConversion());
150
+ const inputValue = 100;
151
+ const formatted = result.current.formatCurrencyValue(inputValue);
152
+ expect(formatted).toContain("350");
153
+ });
154
+ });
155
+ describe("return values", () => {
156
+ it("should return selected currency", () => {
157
+ mockStoreState.selectedCurrency = "USD";
158
+ mockStoreState.baseCurrency = "B3";
159
+ const { result } = renderHook(() => useCurrencyConversion());
160
+ expect(result.current.selectedCurrency).toBe("USD");
161
+ });
162
+ it("should return base currency", () => {
163
+ mockStoreState.selectedCurrency = "USD";
164
+ mockStoreState.baseCurrency = "B3";
165
+ const { result } = renderHook(() => useCurrencyConversion());
166
+ expect(result.current.baseCurrency).toBe("B3");
167
+ });
168
+ it("should return exchange rate", () => {
169
+ const testRate = 2.5;
170
+ mockRates.USD = testRate;
171
+ mockStoreState.selectedCurrency = "USD";
172
+ mockStoreState.baseCurrency = "B3";
173
+ const { result } = renderHook(() => useCurrencyConversion());
174
+ expect(result.current.exchangeRate).toBe(testRate);
175
+ });
176
+ it("should return correct currency symbols", () => {
177
+ mockStoreState.selectedCurrency = "EUR";
178
+ mockStoreState.baseCurrency = "B3";
179
+ const { result } = renderHook(() => useCurrencyConversion());
180
+ expect(result.current.selectedCurrencySymbol).toBe("€");
181
+ expect(result.current.baseCurrencySymbol).toBe("B3");
182
+ });
183
+ });
184
+ describe("formatTooltipValue", () => {
185
+ it("should show USD equivalent when displaying base currency", () => {
186
+ mockRates.USD = 1.5;
187
+ mockStoreState.selectedCurrency = "B3";
188
+ mockStoreState.baseCurrency = "B3";
189
+ const { result } = renderHook(() => useCurrencyConversion());
190
+ const tooltip = result.current.formatTooltipValue(100);
191
+ expect(tooltip).toContain("USD");
192
+ expect(tooltip).toContain("150");
193
+ });
194
+ it("should show base currency when displaying other currency", () => {
195
+ mockRates.EUR = 0.9;
196
+ mockRates.USD = 1.2;
197
+ mockStoreState.selectedCurrency = "EUR";
198
+ mockStoreState.baseCurrency = "B3";
199
+ const { result } = renderHook(() => useCurrencyConversion());
200
+ const tooltip = result.current.formatTooltipValue(100);
201
+ expect(tooltip).toContain("B3");
202
+ expect(tooltip).toContain("100");
203
+ });
204
+ it("should handle custom currency for base currency", () => {
205
+ mockRates.USD = 2.0;
206
+ mockRates.EUR = 1.8;
207
+ mockStoreState.selectedCurrency = "EUR";
208
+ mockStoreState.baseCurrency = "B3";
209
+ const { result } = renderHook(() => useCurrencyConversion());
210
+ const tooltip = result.current.formatTooltipValue(100, "B3");
211
+ expect(tooltip).toContain("USD");
212
+ expect(tooltip).toContain("200");
213
+ });
214
+ it("should handle custom currency for non-base currency", () => {
215
+ mockRates.USD = 2.0;
216
+ mockStoreState.selectedCurrency = "USD";
217
+ mockStoreState.baseCurrency = "B3";
218
+ const { result } = renderHook(() => useCurrencyConversion());
219
+ const tooltip = result.current.formatTooltipValue(50, "ETH");
220
+ expect(tooltip).toContain("ETH");
221
+ expect(tooltip).toContain("50");
222
+ });
223
+ it("should handle absolute values for negative amounts", () => {
224
+ mockRates.USD = 1.5;
225
+ mockStoreState.selectedCurrency = "B3";
226
+ mockStoreState.baseCurrency = "B3";
227
+ const { result } = renderHook(() => useCurrencyConversion());
228
+ const tooltip = result.current.formatTooltipValue(-100);
229
+ expect(tooltip).toContain("USD");
230
+ expect(tooltip).toContain("150");
231
+ expect(tooltip).not.toContain("-");
232
+ });
233
+ it("should handle exchange rate unavailable", () => {
234
+ mockRates.USD = undefined;
235
+ mockStoreState.selectedCurrency = "B3";
236
+ mockStoreState.baseCurrency = "B3";
237
+ const { result } = renderHook(() => useCurrencyConversion());
238
+ const tooltip = result.current.formatTooltipValue(100);
239
+ expect(tooltip).toContain("USD");
240
+ expect(tooltip).toContain("100");
241
+ });
242
+ });
243
+ });
@@ -1 +1,2 @@
1
+ export * from "./useCurrencyConversion";
1
2
  export * from "./useNavigation";
@@ -1 +1,2 @@
1
+ export * from "./useCurrencyConversion.js";
1
2
  export * from "./useNavigation.js";
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Hook for currency conversion and formatting with real-time exchange rates.
3
+ *
4
+ * This hook provides currency conversion functionality using live exchange rates
5
+ * and formats values according to currency-specific rules (decimals, symbols, etc.).
6
+ *
7
+ * @returns Currency conversion utilities and state
8
+ *
9
+ * @example
10
+ * ```tsx
11
+ * function PriceDisplay({ amount }: { amount: number }) {
12
+ * const { formatCurrencyValue, selectedCurrency } = useCurrencyConversion();
13
+ * return <div>{formatCurrencyValue(amount)}</div>;
14
+ * }
15
+ * ```
16
+ */
17
+ export declare function useCurrencyConversion(): {
18
+ /** Currently selected display currency */
19
+ selectedCurrency: import("../stores/currencyStore").SupportedCurrency;
20
+ /** Base currency used for conversion (typically B3) */
21
+ baseCurrency: import("../stores/currencyStore").SupportedCurrency;
22
+ /** Current exchange rate from base to selected currency (undefined while loading) */
23
+ exchangeRate: number;
24
+ /** Format a value with currency conversion and proper symbol/decimal handling */
25
+ formatCurrencyValue: (value: number, options?: {
26
+ decimals?: number;
27
+ currency?: string;
28
+ }) => string;
29
+ /** Format a tooltip value showing alternate currency representation */
30
+ formatTooltipValue: (value: number, customCurrency?: string) => string;
31
+ /** Symbol for the currently selected currency (e.g., "$", "€", "ETH") */
32
+ selectedCurrencySymbol: string;
33
+ /** Symbol for the base currency */
34
+ baseCurrencySymbol: string;
35
+ };
@@ -0,0 +1,197 @@
1
+ import { useExchangeRate } from "../../../global-account/react/index.js";
2
+ import { formatDisplayNumber } from "../../../shared/utils/number.js";
3
+ import { CURRENCY_SYMBOLS, useCurrencyStore } from "../stores/currencyStore.js";
4
+ /**
5
+ * Hook for currency conversion and formatting with real-time exchange rates.
6
+ *
7
+ * This hook provides currency conversion functionality using live exchange rates
8
+ * and formats values according to currency-specific rules (decimals, symbols, etc.).
9
+ *
10
+ * @returns Currency conversion utilities and state
11
+ *
12
+ * @example
13
+ * ```tsx
14
+ * function PriceDisplay({ amount }: { amount: number }) {
15
+ * const { formatCurrencyValue, selectedCurrency } = useCurrencyConversion();
16
+ * return <div>{formatCurrencyValue(amount)}</div>;
17
+ * }
18
+ * ```
19
+ */
20
+ export function useCurrencyConversion() {
21
+ const selectedCurrency = useCurrencyStore(state => state.selectedCurrency);
22
+ const baseCurrency = useCurrencyStore(state => state.baseCurrency);
23
+ // Get exchange rate for the selected currency
24
+ const { rate: exchangeRate } = useExchangeRate({
25
+ baseCurrency,
26
+ quoteCurrency: selectedCurrency === baseCurrency ? "USD" : selectedCurrency,
27
+ });
28
+ // Always fetch USD rate for tooltip purposes
29
+ const { rate: usdRate } = useExchangeRate({
30
+ baseCurrency,
31
+ quoteCurrency: "USD",
32
+ });
33
+ /**
34
+ * Formats a numeric value as a currency string with proper conversion and formatting.
35
+ *
36
+ * Behavior:
37
+ * - When exchange rate is unavailable, displays value in base currency
38
+ * - Applies currency-specific formatting rules:
39
+ * - JPY/KRW: No decimal places
40
+ * - ETH/SOL: 6 significant digits with subscript notation for small values
41
+ * - Fiat (USD/EUR/GBP/CAD/AUD): 2 decimal places minimum for values < 1000
42
+ * - Handles symbol positioning (prefix for fiat, suffix for crypto)
43
+ *
44
+ * @param value - The numeric value to format (in base currency)
45
+ * @param options - Optional formatting overrides
46
+ * @param options.decimals - Override number of decimal places
47
+ * @param options.currency - Override currency (bypasses conversion)
48
+ * @returns Formatted currency string with appropriate symbol and decimal places
49
+ *
50
+ * @example
51
+ * ```tsx
52
+ * formatCurrencyValue(100) // Returns "$100.00" if USD is selected
53
+ * formatCurrencyValue(0.0001) // Returns "0.0₄1 ETH" if ETH is selected
54
+ * formatCurrencyValue(1500) // Returns "¥1,500" if JPY is selected
55
+ * formatCurrencyValue(100, { decimals: 4, currency: "ETH" }) // Returns "100.0000 ETH"
56
+ * ```
57
+ */
58
+ const formatCurrencyValue = (value, options) => {
59
+ const overrideCurrency = options?.currency;
60
+ const overrideDecimals = options?.decimals;
61
+ // Custom currency provided - bypass conversion and use simple formatting
62
+ if (overrideCurrency) {
63
+ const decimalsToUse = overrideDecimals !== undefined ? overrideDecimals : overrideCurrency === "B3" ? 0 : 2;
64
+ const formatted = formatDisplayNumber(value, {
65
+ fractionDigits: decimalsToUse,
66
+ showSubscripts: false,
67
+ });
68
+ return `${formatted} ${overrideCurrency}`;
69
+ }
70
+ // Custom decimals for base currency without conversion
71
+ if (overrideDecimals !== undefined && selectedCurrency === baseCurrency) {
72
+ const formatted = formatDisplayNumber(value, {
73
+ fractionDigits: overrideDecimals,
74
+ showSubscripts: false,
75
+ });
76
+ return `${formatted} ${baseCurrency}`;
77
+ }
78
+ // If no exchange rate available, show base currency to prevent showing
79
+ // incorrect values with wrong currency symbols during rate fetching
80
+ if (selectedCurrency === baseCurrency || !exchangeRate) {
81
+ const formatted = formatDisplayNumber(value, {
82
+ significantDigits: baseCurrency === "B3" ? 6 : 8,
83
+ showSubscripts: true,
84
+ });
85
+ return `${formatted} ${baseCurrency}`;
86
+ }
87
+ // Convert value using current exchange rate
88
+ const convertedValue = value * exchangeRate;
89
+ const symbol = CURRENCY_SYMBOLS[selectedCurrency];
90
+ // Currencies that display symbol before the number (e.g., $100.00)
91
+ const prefixCurrencies = ["USD", "EUR", "GBP", "CAD", "AUD"];
92
+ let formatted;
93
+ if (selectedCurrency === "JPY" || selectedCurrency === "KRW") {
94
+ // Japanese Yen and Korean Won don't use decimal places
95
+ formatted = formatDisplayNumber(convertedValue, {
96
+ fractionDigits: 0,
97
+ showSubscripts: false,
98
+ });
99
+ }
100
+ else if (selectedCurrency === "ETH" || selectedCurrency === "SOL") {
101
+ // Crypto currencies use more precision and subscript notation
102
+ // for very small amounts (e.g., 0.0₃45 ETH)
103
+ formatted = formatDisplayNumber(convertedValue, {
104
+ significantDigits: 6,
105
+ showSubscripts: true,
106
+ });
107
+ }
108
+ else {
109
+ // Standard fiat currencies (USD, EUR, GBP, CAD, AUD)
110
+ // Use 2 decimal places minimum for amounts under 1000
111
+ formatted = formatDisplayNumber(convertedValue, {
112
+ significantDigits: 6,
113
+ fractionDigits: convertedValue < 1000 ? 2 : undefined,
114
+ showSubscripts: true,
115
+ });
116
+ }
117
+ // Apply currency symbol with correct positioning
118
+ if (prefixCurrencies.includes(selectedCurrency)) {
119
+ return `${symbol}${formatted}`;
120
+ }
121
+ else {
122
+ // Suffix currencies: JPY, KRW, ETH, SOL, B3
123
+ return `${formatted} ${symbol}`;
124
+ }
125
+ };
126
+ /**
127
+ * Formats a tooltip value showing the alternate currency representation.
128
+ *
129
+ * Behavior:
130
+ * - When displaying base currency: Shows USD equivalent
131
+ * - When displaying other currency: Shows base currency equivalent
132
+ * - For custom currencies: Shows appropriate conversion or original value
133
+ *
134
+ * @param value - The numeric value to format
135
+ * @param customCurrency - Optional custom currency override
136
+ * @returns Formatted tooltip string
137
+ *
138
+ * @example
139
+ * ```tsx
140
+ * formatTooltipValue(100) // Returns "$150.00 USD" if displaying B3 with rate 1.5
141
+ * formatTooltipValue(100, "ETH") // Returns "100.0000 ETH" if custom currency
142
+ * ```
143
+ */
144
+ const formatTooltipValue = (value, customCurrency) => {
145
+ const displayCurrency = customCurrency || selectedCurrency;
146
+ const absoluteValue = Math.abs(value);
147
+ // Custom currency provided
148
+ if (customCurrency) {
149
+ if (customCurrency === baseCurrency) {
150
+ // Show USD equivalent for base currency using USD rate
151
+ const usdValue = usdRate ? absoluteValue * usdRate : absoluteValue;
152
+ const formatted = formatDisplayNumber(usdValue, {
153
+ significantDigits: 6,
154
+ fractionDigits: usdValue < 1000 ? 2 : undefined,
155
+ showSubscripts: true,
156
+ });
157
+ return `$${formatted} USD`;
158
+ }
159
+ else {
160
+ // Show as-is for other custom currencies
161
+ return `${formatDisplayNumber(absoluteValue, { significantDigits: 6 })} ${customCurrency}`;
162
+ }
163
+ }
164
+ // Showing base currency - display USD equivalent
165
+ if (displayCurrency === baseCurrency) {
166
+ const usdValue = usdRate ? absoluteValue * usdRate : absoluteValue;
167
+ const formatted = formatDisplayNumber(usdValue, {
168
+ significantDigits: 6,
169
+ fractionDigits: usdValue < 1000 ? 2 : undefined,
170
+ showSubscripts: true,
171
+ });
172
+ return `$${formatted} USD`;
173
+ }
174
+ // Showing other currency - display base currency equivalent
175
+ const formatted = formatDisplayNumber(absoluteValue, {
176
+ significantDigits: baseCurrency === "B3" ? 6 : 8,
177
+ showSubscripts: true,
178
+ });
179
+ return `${formatted} ${baseCurrency}`;
180
+ };
181
+ return {
182
+ /** Currently selected display currency */
183
+ selectedCurrency,
184
+ /** Base currency used for conversion (typically B3) */
185
+ baseCurrency,
186
+ /** Current exchange rate from base to selected currency (undefined while loading) */
187
+ exchangeRate,
188
+ /** Format a value with currency conversion and proper symbol/decimal handling */
189
+ formatCurrencyValue,
190
+ /** Format a tooltip value showing alternate currency representation */
191
+ formatTooltipValue,
192
+ /** Symbol for the currently selected currency (e.g., "$", "€", "ETH") */
193
+ selectedCurrencySymbol: CURRENCY_SYMBOLS[selectedCurrency],
194
+ /** Symbol for the base currency */
195
+ baseCurrencySymbol: CURRENCY_SYMBOLS[baseCurrency],
196
+ };
197
+ }
@@ -1 +1,3 @@
1
+ export * from "./components";
1
2
  export * from "./hooks";
3
+ export * from "./stores";
@@ -1 +1,3 @@
1
+ export * from "./components/index.js";
1
2
  export * from "./hooks/index.js";
3
+ export * from "./stores/index.js";
@@ -0,0 +1,7 @@
1
+ interface CurrencyModalState {
2
+ isOpen: boolean;
3
+ openModal: () => void;
4
+ closeModal: () => void;
5
+ }
6
+ export declare const useCurrencyModalStore: import("zustand").UseBoundStore<import("zustand").StoreApi<CurrencyModalState>>;
7
+ export {};
@@ -0,0 +1,6 @@
1
+ import { create } from "zustand";
2
+ export const useCurrencyModalStore = create(set => ({
3
+ isOpen: false,
4
+ openModal: () => set({ isOpen: true }),
5
+ closeModal: () => set({ isOpen: false }),
6
+ }));
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Supported currencies for display and conversion.
3
+ * Includes fiat currencies (USD, EUR, GBP, JPY, CAD, AUD, KRW) and crypto (ETH, SOL, B3).
4
+ */
5
+ export type SupportedCurrency = "ETH" | "USD" | "EUR" | "GBP" | "JPY" | "CAD" | "AUD" | "B3" | "SOL" | "KRW";
6
+ /**
7
+ * Currency symbols used for display formatting.
8
+ * Prefix currencies (USD, EUR, GBP, CAD, AUD) show symbol before the amount.
9
+ * Suffix currencies (JPY, KRW, ETH, SOL, B3) show symbol after the amount.
10
+ */
11
+ export declare const CURRENCY_SYMBOLS: Record<SupportedCurrency, string>;
12
+ /**
13
+ * Human-readable currency names for display in selectors and labels.
14
+ */
15
+ export declare const CURRENCY_NAMES: Record<SupportedCurrency, string>;
16
+ /**
17
+ * Currency store state interface.
18
+ * @property selectedCurrency - The currency currently selected for display
19
+ * @property baseCurrency - The base currency for conversion (typically B3)
20
+ * @property setSelectedCurrency - Update the selected display currency
21
+ * @property setBaseCurrency - Update the base currency for conversions
22
+ */
23
+ interface CurrencyState {
24
+ selectedCurrency: SupportedCurrency;
25
+ baseCurrency: SupportedCurrency;
26
+ setSelectedCurrency: (currency: SupportedCurrency) => void;
27
+ setBaseCurrency: (currency: SupportedCurrency) => void;
28
+ }
29
+ /**
30
+ * Zustand store for managing currency selection and conversion.
31
+ * Persists user's selected currency preference in localStorage.
32
+ *
33
+ * @example
34
+ * ```tsx
35
+ * const { selectedCurrency, setSelectedCurrency } = useCurrencyStore();
36
+ * // Change display currency to USD
37
+ * setSelectedCurrency('USD');
38
+ * ```
39
+ */
40
+ export declare const useCurrencyStore: import("zustand").UseBoundStore<Omit<import("zustand").StoreApi<CurrencyState>, "persist"> & {
41
+ persist: {
42
+ setOptions: (options: Partial<import("zustand/middleware").PersistOptions<CurrencyState, CurrencyState>>) => void;
43
+ clearStorage: () => void;
44
+ rehydrate: () => Promise<void> | void;
45
+ hasHydrated: () => boolean;
46
+ onHydrate: (fn: (state: CurrencyState) => void) => () => void;
47
+ onFinishHydration: (fn: (state: CurrencyState) => void) => () => void;
48
+ getOptions: () => Partial<import("zustand/middleware").PersistOptions<CurrencyState, CurrencyState>>;
49
+ };
50
+ }>;
51
+ export {};
@@ -0,0 +1,54 @@
1
+ import { create } from "zustand";
2
+ import { persist } from "zustand/middleware";
3
+ /**
4
+ * Currency symbols used for display formatting.
5
+ * Prefix currencies (USD, EUR, GBP, CAD, AUD) show symbol before the amount.
6
+ * Suffix currencies (JPY, KRW, ETH, SOL, B3) show symbol after the amount.
7
+ */
8
+ export const CURRENCY_SYMBOLS = {
9
+ ETH: "ETH",
10
+ USD: "$",
11
+ EUR: "€",
12
+ GBP: "£",
13
+ JPY: "¥",
14
+ CAD: "C$",
15
+ AUD: "A$",
16
+ B3: "B3",
17
+ SOL: "SOL",
18
+ KRW: "₩",
19
+ };
20
+ /**
21
+ * Human-readable currency names for display in selectors and labels.
22
+ */
23
+ export const CURRENCY_NAMES = {
24
+ ETH: "Ethereum",
25
+ USD: "US Dollar",
26
+ EUR: "Euro",
27
+ GBP: "British Pound",
28
+ JPY: "Japanese Yen",
29
+ CAD: "Canadian Dollar",
30
+ AUD: "Australian Dollar",
31
+ B3: "B3",
32
+ SOL: "Solana",
33
+ KRW: "Korean Won",
34
+ };
35
+ /**
36
+ * Zustand store for managing currency selection and conversion.
37
+ * Persists user's selected currency preference in localStorage.
38
+ *
39
+ * @example
40
+ * ```tsx
41
+ * const { selectedCurrency, setSelectedCurrency } = useCurrencyStore();
42
+ * // Change display currency to USD
43
+ * setSelectedCurrency('USD');
44
+ * ```
45
+ */
46
+ export const useCurrencyStore = create()(persist(set => ({
47
+ selectedCurrency: "B3",
48
+ baseCurrency: "B3",
49
+ setSelectedCurrency: currency => set({ selectedCurrency: currency }),
50
+ setBaseCurrency: currency => set({ baseCurrency: currency }),
51
+ }), {
52
+ name: "currency-storage",
53
+ version: 2,
54
+ }));
@@ -0,0 +1,2 @@
1
+ export * from "./currencyModalStore";
2
+ export * from "./currencyStore";
@@ -0,0 +1,2 @@
1
+ export * from "./currencyModalStore.js";
2
+ export * from "./currencyStore.js";