@b3dotfun/sdk 0.0.58 → 0.0.59-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/anyspend/abis/upsideStaking.d.ts +1 -1030
- package/dist/cjs/anyspend/abis/upsideStaking.js +2 -570
- package/dist/cjs/anyspend/react/components/AnySpendStakeUpside.d.ts +1 -2
- package/dist/cjs/anyspend/react/components/AnySpendStakeUpside.js +8 -18
- package/dist/cjs/anyspend/react/components/common/CryptoPaymentMethod.js +4 -0
- package/dist/cjs/anyspend/utils/accountStore.d.ts +7 -0
- package/dist/cjs/anyspend/utils/accountStore.js +8 -0
- package/dist/cjs/anyspend/utils/index.d.ts +1 -0
- package/dist/cjs/anyspend/utils/index.js +1 -0
- package/dist/cjs/global-account/react/components/B3DynamicModal.js +17 -0
- package/dist/cjs/global-account/react/hooks/useWagmiConfig.d.ts +441 -1
- package/dist/cjs/global-account/react/hooks/useWagmiConfig.js +2 -0
- package/dist/cjs/global-account/react/stores/useModalStore.d.ts +0 -2
- package/dist/cjs/shared/react/components/CurrencySelector.js +8 -3
- package/dist/cjs/shared/react/components/FormattedCurrency.d.ts +3 -3
- package/dist/cjs/shared/react/components/FormattedCurrency.js +31 -26
- package/dist/cjs/shared/react/hooks/useCurrencyConversion.d.ts +8 -5
- package/dist/cjs/shared/react/hooks/useCurrencyConversion.js +153 -94
- package/dist/cjs/shared/react/stores/currencyStore.d.ts +83 -8
- package/dist/cjs/shared/react/stores/currencyStore.js +147 -5
- package/dist/esm/anyspend/abis/upsideStaking.d.ts +1 -1030
- package/dist/esm/anyspend/abis/upsideStaking.js +1 -569
- package/dist/esm/anyspend/react/components/AnySpendStakeUpside.d.ts +1 -2
- package/dist/esm/anyspend/react/components/AnySpendStakeUpside.js +9 -19
- package/dist/esm/anyspend/react/components/common/CryptoPaymentMethod.js +5 -1
- package/dist/esm/anyspend/utils/accountStore.d.ts +7 -0
- package/dist/esm/anyspend/utils/accountStore.js +5 -0
- package/dist/esm/anyspend/utils/index.d.ts +1 -0
- package/dist/esm/anyspend/utils/index.js +1 -0
- package/dist/esm/global-account/react/components/B3DynamicModal.js +17 -0
- package/dist/esm/global-account/react/hooks/useWagmiConfig.d.ts +441 -1
- package/dist/esm/global-account/react/hooks/useWagmiConfig.js +2 -0
- package/dist/esm/global-account/react/stores/useModalStore.d.ts +0 -2
- package/dist/esm/shared/react/components/CurrencySelector.js +10 -5
- package/dist/esm/shared/react/components/FormattedCurrency.d.ts +3 -3
- package/dist/esm/shared/react/components/FormattedCurrency.js +31 -26
- package/dist/esm/shared/react/hooks/useCurrencyConversion.d.ts +8 -5
- package/dist/esm/shared/react/hooks/useCurrencyConversion.js +154 -95
- package/dist/esm/shared/react/stores/currencyStore.d.ts +83 -8
- package/dist/esm/shared/react/stores/currencyStore.js +143 -5
- package/dist/types/anyspend/abis/upsideStaking.d.ts +1 -1030
- package/dist/types/anyspend/react/components/AnySpendStakeUpside.d.ts +1 -2
- package/dist/types/anyspend/utils/accountStore.d.ts +7 -0
- package/dist/types/anyspend/utils/index.d.ts +1 -0
- package/dist/types/global-account/react/hooks/useWagmiConfig.d.ts +441 -1
- package/dist/types/global-account/react/stores/useModalStore.d.ts +0 -2
- package/dist/types/shared/react/components/FormattedCurrency.d.ts +3 -3
- package/dist/types/shared/react/hooks/useCurrencyConversion.d.ts +8 -5
- package/dist/types/shared/react/stores/currencyStore.d.ts +83 -8
- package/package.json +4 -3
- package/src/anyspend/abis/upsideStaking.ts +1 -570
- package/src/anyspend/react/components/AnySpendStakeUpside.tsx +8 -19
- package/src/anyspend/react/components/common/CryptoPaymentMethod.tsx +6 -2
- package/src/anyspend/utils/accountStore.ts +12 -0
- package/src/anyspend/utils/index.ts +1 -0
- package/src/global-account/react/components/B3DynamicModal.tsx +20 -0
- package/src/global-account/react/hooks/useWagmiConfig.tsx +2 -0
- package/src/global-account/react/stores/useModalStore.ts +0 -2
- package/src/shared/react/components/CurrencySelector.tsx +36 -5
- package/src/shared/react/components/FormattedCurrency.tsx +36 -30
- package/src/shared/react/hooks/__tests__/useCurrencyConversion.test.ts +14 -14
- package/src/shared/react/hooks/useCurrencyConversion.ts +163 -96
- package/src/shared/react/stores/currencyStore.ts +216 -10
|
@@ -35,20 +35,158 @@ export const CURRENCY_NAMES = {
|
|
|
35
35
|
/**
|
|
36
36
|
* Zustand store for managing currency selection and conversion.
|
|
37
37
|
* Persists user's selected currency preference in localStorage.
|
|
38
|
+
* Supports dynamic currency registration and custom exchange rates.
|
|
38
39
|
*
|
|
39
40
|
* @example
|
|
40
41
|
* ```tsx
|
|
41
|
-
* const { selectedCurrency, setSelectedCurrency } = useCurrencyStore();
|
|
42
|
-
*
|
|
43
|
-
*
|
|
42
|
+
* const { selectedCurrency, setSelectedCurrency, addCurrency, setExchangeRate } = useCurrencyStore();
|
|
43
|
+
*
|
|
44
|
+
* // Add a new currency
|
|
45
|
+
* addCurrency({
|
|
46
|
+
* code: "BTC",
|
|
47
|
+
* symbol: "₿",
|
|
48
|
+
* name: "Bitcoin",
|
|
49
|
+
* showSubscripts: true,
|
|
50
|
+
* });
|
|
51
|
+
*
|
|
52
|
+
* // Set exchange rate: 1 BTC = 50000 USD
|
|
53
|
+
* setExchangeRate("BTC", "USD", 50000);
|
|
54
|
+
*
|
|
55
|
+
* // Change display currency
|
|
56
|
+
* setSelectedCurrency('BTC');
|
|
44
57
|
* ```
|
|
45
58
|
*/
|
|
46
|
-
export const useCurrencyStore = create()(persist(set => ({
|
|
59
|
+
export const useCurrencyStore = create()(persist((set, get) => ({
|
|
47
60
|
selectedCurrency: "B3",
|
|
48
61
|
baseCurrency: "B3",
|
|
62
|
+
customCurrencies: {},
|
|
63
|
+
customExchangeRates: {},
|
|
49
64
|
setSelectedCurrency: currency => set({ selectedCurrency: currency }),
|
|
50
65
|
setBaseCurrency: currency => set({ baseCurrency: currency }),
|
|
66
|
+
addCurrency: metadata => {
|
|
67
|
+
set(state => ({
|
|
68
|
+
customCurrencies: {
|
|
69
|
+
...state.customCurrencies,
|
|
70
|
+
[metadata.code]: metadata,
|
|
71
|
+
},
|
|
72
|
+
}));
|
|
73
|
+
},
|
|
74
|
+
removeCurrency: code => {
|
|
75
|
+
set(state => {
|
|
76
|
+
// Remove the currency
|
|
77
|
+
const { [code]: _removed, ...remaining } = state.customCurrencies;
|
|
78
|
+
// Remove all exchange rates involving this currency
|
|
79
|
+
const filteredRates = {};
|
|
80
|
+
for (const [key, rate] of Object.entries(state.customExchangeRates)) {
|
|
81
|
+
// Key format is "FROM-TO", skip if either matches the removed code
|
|
82
|
+
const [from, to] = key.split("-");
|
|
83
|
+
if (from !== code && to !== code) {
|
|
84
|
+
filteredRates[key] = rate;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
customCurrencies: remaining,
|
|
89
|
+
customExchangeRates: filteredRates,
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
},
|
|
93
|
+
setExchangeRate: (from, to, rate) => {
|
|
94
|
+
set(state => {
|
|
95
|
+
const key = `${from}-${to}`;
|
|
96
|
+
const inverseKey = `${to}-${from}`;
|
|
97
|
+
// Only set inverse rate if rate is not 0 (to avoid Infinity)
|
|
98
|
+
const newRates = {
|
|
99
|
+
...state.customExchangeRates,
|
|
100
|
+
[key]: rate,
|
|
101
|
+
};
|
|
102
|
+
if (rate !== 0) {
|
|
103
|
+
newRates[inverseKey] = 1 / rate;
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
customExchangeRates: newRates,
|
|
107
|
+
};
|
|
108
|
+
});
|
|
109
|
+
},
|
|
110
|
+
getExchangeRate: (from, to) => {
|
|
111
|
+
const key = `${from}-${to}`;
|
|
112
|
+
return get().customExchangeRates[key];
|
|
113
|
+
},
|
|
114
|
+
getAllCurrencies: () => {
|
|
115
|
+
const builtIn = Object.keys(CURRENCY_SYMBOLS);
|
|
116
|
+
const custom = Object.keys(get().customCurrencies);
|
|
117
|
+
return [...builtIn, ...custom];
|
|
118
|
+
},
|
|
51
119
|
}), {
|
|
52
120
|
name: "currency-storage",
|
|
53
|
-
version:
|
|
121
|
+
version: 3,
|
|
54
122
|
}));
|
|
123
|
+
/**
|
|
124
|
+
* Get the symbol for any currency (built-in or custom).
|
|
125
|
+
*/
|
|
126
|
+
export function getCurrencySymbol(currency) {
|
|
127
|
+
// Check built-in currencies first
|
|
128
|
+
if (currency in CURRENCY_SYMBOLS) {
|
|
129
|
+
return CURRENCY_SYMBOLS[currency];
|
|
130
|
+
}
|
|
131
|
+
// Check custom currencies
|
|
132
|
+
const customCurrencies = useCurrencyStore.getState().customCurrencies;
|
|
133
|
+
const customCurrency = customCurrencies[currency];
|
|
134
|
+
if (customCurrency) {
|
|
135
|
+
return customCurrency.symbol;
|
|
136
|
+
}
|
|
137
|
+
// Fallback to currency code
|
|
138
|
+
return currency;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Get the name for any currency (built-in or custom).
|
|
142
|
+
*/
|
|
143
|
+
export function getCurrencyName(currency) {
|
|
144
|
+
// Check built-in currencies first
|
|
145
|
+
if (currency in CURRENCY_NAMES) {
|
|
146
|
+
return CURRENCY_NAMES[currency];
|
|
147
|
+
}
|
|
148
|
+
// Check custom currencies
|
|
149
|
+
const customCurrencies = useCurrencyStore.getState().customCurrencies;
|
|
150
|
+
const customCurrency = customCurrencies[currency];
|
|
151
|
+
if (customCurrency) {
|
|
152
|
+
return customCurrency.name;
|
|
153
|
+
}
|
|
154
|
+
// Fallback to currency code
|
|
155
|
+
return currency;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Get metadata for a custom currency.
|
|
159
|
+
*/
|
|
160
|
+
export function getCurrencyMetadata(currency) {
|
|
161
|
+
const customCurrencies = useCurrencyStore.getState().customCurrencies;
|
|
162
|
+
return customCurrencies[currency];
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Get the number of decimal places for a currency (for converting from smallest unit).
|
|
166
|
+
* Used when parsing amounts from wei/smallest unit format.
|
|
167
|
+
*
|
|
168
|
+
* @param currency - Currency code
|
|
169
|
+
* @returns Number of decimal places (e.g., 18 for ETH/wei, 2 for USD cents, 0 for JPY)
|
|
170
|
+
*/
|
|
171
|
+
export function getCurrencyDecimalPlaces(currency) {
|
|
172
|
+
// Check custom currencies first
|
|
173
|
+
const customCurrencies = useCurrencyStore.getState().customCurrencies;
|
|
174
|
+
const customMetadata = customCurrencies[currency];
|
|
175
|
+
if (customMetadata?.decimals !== undefined) {
|
|
176
|
+
return customMetadata.decimals;
|
|
177
|
+
}
|
|
178
|
+
// Built-in currencies with 18 decimals (wei-like)
|
|
179
|
+
if (currency === "WIN" || currency === "ETH" || currency === "SOL" || currency === "B3") {
|
|
180
|
+
return 18;
|
|
181
|
+
}
|
|
182
|
+
// Fiat currencies with cent-like decimals
|
|
183
|
+
if (currency === "USD" || currency === "EUR" || currency === "GBP" || currency === "CAD" || currency === "AUD") {
|
|
184
|
+
return 2;
|
|
185
|
+
}
|
|
186
|
+
// Currencies without fractional units
|
|
187
|
+
if (currency === "JPY" || currency === "KRW") {
|
|
188
|
+
return 0;
|
|
189
|
+
}
|
|
190
|
+
// Default to 18 decimals (wei-like)
|
|
191
|
+
return 18;
|
|
192
|
+
}
|