@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
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useCurrencyStore = exports.CURRENCY_NAMES = exports.CURRENCY_SYMBOLS = void 0;
|
|
4
|
+
exports.getCurrencySymbol = getCurrencySymbol;
|
|
5
|
+
exports.getCurrencyName = getCurrencyName;
|
|
6
|
+
exports.getCurrencyMetadata = getCurrencyMetadata;
|
|
7
|
+
exports.getCurrencyDecimalPlaces = getCurrencyDecimalPlaces;
|
|
4
8
|
const zustand_1 = require("zustand");
|
|
5
9
|
const middleware_1 = require("zustand/middleware");
|
|
6
10
|
/**
|
|
@@ -38,20 +42,158 @@ exports.CURRENCY_NAMES = {
|
|
|
38
42
|
/**
|
|
39
43
|
* Zustand store for managing currency selection and conversion.
|
|
40
44
|
* Persists user's selected currency preference in localStorage.
|
|
45
|
+
* Supports dynamic currency registration and custom exchange rates.
|
|
41
46
|
*
|
|
42
47
|
* @example
|
|
43
48
|
* ```tsx
|
|
44
|
-
* const { selectedCurrency, setSelectedCurrency } = useCurrencyStore();
|
|
45
|
-
*
|
|
46
|
-
*
|
|
49
|
+
* const { selectedCurrency, setSelectedCurrency, addCurrency, setExchangeRate } = useCurrencyStore();
|
|
50
|
+
*
|
|
51
|
+
* // Add a new currency
|
|
52
|
+
* addCurrency({
|
|
53
|
+
* code: "BTC",
|
|
54
|
+
* symbol: "₿",
|
|
55
|
+
* name: "Bitcoin",
|
|
56
|
+
* showSubscripts: true,
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* // Set exchange rate: 1 BTC = 50000 USD
|
|
60
|
+
* setExchangeRate("BTC", "USD", 50000);
|
|
61
|
+
*
|
|
62
|
+
* // Change display currency
|
|
63
|
+
* setSelectedCurrency('BTC');
|
|
47
64
|
* ```
|
|
48
65
|
*/
|
|
49
|
-
exports.useCurrencyStore = (0, zustand_1.create)()((0, middleware_1.persist)(set => ({
|
|
66
|
+
exports.useCurrencyStore = (0, zustand_1.create)()((0, middleware_1.persist)((set, get) => ({
|
|
50
67
|
selectedCurrency: "B3",
|
|
51
68
|
baseCurrency: "B3",
|
|
69
|
+
customCurrencies: {},
|
|
70
|
+
customExchangeRates: {},
|
|
52
71
|
setSelectedCurrency: currency => set({ selectedCurrency: currency }),
|
|
53
72
|
setBaseCurrency: currency => set({ baseCurrency: currency }),
|
|
73
|
+
addCurrency: metadata => {
|
|
74
|
+
set(state => ({
|
|
75
|
+
customCurrencies: {
|
|
76
|
+
...state.customCurrencies,
|
|
77
|
+
[metadata.code]: metadata,
|
|
78
|
+
},
|
|
79
|
+
}));
|
|
80
|
+
},
|
|
81
|
+
removeCurrency: code => {
|
|
82
|
+
set(state => {
|
|
83
|
+
// Remove the currency
|
|
84
|
+
const { [code]: _removed, ...remaining } = state.customCurrencies;
|
|
85
|
+
// Remove all exchange rates involving this currency
|
|
86
|
+
const filteredRates = {};
|
|
87
|
+
for (const [key, rate] of Object.entries(state.customExchangeRates)) {
|
|
88
|
+
// Key format is "FROM-TO", skip if either matches the removed code
|
|
89
|
+
const [from, to] = key.split("-");
|
|
90
|
+
if (from !== code && to !== code) {
|
|
91
|
+
filteredRates[key] = rate;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
customCurrencies: remaining,
|
|
96
|
+
customExchangeRates: filteredRates,
|
|
97
|
+
};
|
|
98
|
+
});
|
|
99
|
+
},
|
|
100
|
+
setExchangeRate: (from, to, rate) => {
|
|
101
|
+
set(state => {
|
|
102
|
+
const key = `${from}-${to}`;
|
|
103
|
+
const inverseKey = `${to}-${from}`;
|
|
104
|
+
// Only set inverse rate if rate is not 0 (to avoid Infinity)
|
|
105
|
+
const newRates = {
|
|
106
|
+
...state.customExchangeRates,
|
|
107
|
+
[key]: rate,
|
|
108
|
+
};
|
|
109
|
+
if (rate !== 0) {
|
|
110
|
+
newRates[inverseKey] = 1 / rate;
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
customExchangeRates: newRates,
|
|
114
|
+
};
|
|
115
|
+
});
|
|
116
|
+
},
|
|
117
|
+
getExchangeRate: (from, to) => {
|
|
118
|
+
const key = `${from}-${to}`;
|
|
119
|
+
return get().customExchangeRates[key];
|
|
120
|
+
},
|
|
121
|
+
getAllCurrencies: () => {
|
|
122
|
+
const builtIn = Object.keys(exports.CURRENCY_SYMBOLS);
|
|
123
|
+
const custom = Object.keys(get().customCurrencies);
|
|
124
|
+
return [...builtIn, ...custom];
|
|
125
|
+
},
|
|
54
126
|
}), {
|
|
55
127
|
name: "currency-storage",
|
|
56
|
-
version:
|
|
128
|
+
version: 3,
|
|
57
129
|
}));
|
|
130
|
+
/**
|
|
131
|
+
* Get the symbol for any currency (built-in or custom).
|
|
132
|
+
*/
|
|
133
|
+
function getCurrencySymbol(currency) {
|
|
134
|
+
// Check built-in currencies first
|
|
135
|
+
if (currency in exports.CURRENCY_SYMBOLS) {
|
|
136
|
+
return exports.CURRENCY_SYMBOLS[currency];
|
|
137
|
+
}
|
|
138
|
+
// Check custom currencies
|
|
139
|
+
const customCurrencies = exports.useCurrencyStore.getState().customCurrencies;
|
|
140
|
+
const customCurrency = customCurrencies[currency];
|
|
141
|
+
if (customCurrency) {
|
|
142
|
+
return customCurrency.symbol;
|
|
143
|
+
}
|
|
144
|
+
// Fallback to currency code
|
|
145
|
+
return currency;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Get the name for any currency (built-in or custom).
|
|
149
|
+
*/
|
|
150
|
+
function getCurrencyName(currency) {
|
|
151
|
+
// Check built-in currencies first
|
|
152
|
+
if (currency in exports.CURRENCY_NAMES) {
|
|
153
|
+
return exports.CURRENCY_NAMES[currency];
|
|
154
|
+
}
|
|
155
|
+
// Check custom currencies
|
|
156
|
+
const customCurrencies = exports.useCurrencyStore.getState().customCurrencies;
|
|
157
|
+
const customCurrency = customCurrencies[currency];
|
|
158
|
+
if (customCurrency) {
|
|
159
|
+
return customCurrency.name;
|
|
160
|
+
}
|
|
161
|
+
// Fallback to currency code
|
|
162
|
+
return currency;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Get metadata for a custom currency.
|
|
166
|
+
*/
|
|
167
|
+
function getCurrencyMetadata(currency) {
|
|
168
|
+
const customCurrencies = exports.useCurrencyStore.getState().customCurrencies;
|
|
169
|
+
return customCurrencies[currency];
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Get the number of decimal places for a currency (for converting from smallest unit).
|
|
173
|
+
* Used when parsing amounts from wei/smallest unit format.
|
|
174
|
+
*
|
|
175
|
+
* @param currency - Currency code
|
|
176
|
+
* @returns Number of decimal places (e.g., 18 for ETH/wei, 2 for USD cents, 0 for JPY)
|
|
177
|
+
*/
|
|
178
|
+
function getCurrencyDecimalPlaces(currency) {
|
|
179
|
+
// Check custom currencies first
|
|
180
|
+
const customCurrencies = exports.useCurrencyStore.getState().customCurrencies;
|
|
181
|
+
const customMetadata = customCurrencies[currency];
|
|
182
|
+
if (customMetadata?.decimals !== undefined) {
|
|
183
|
+
return customMetadata.decimals;
|
|
184
|
+
}
|
|
185
|
+
// Built-in currencies with 18 decimals (wei-like)
|
|
186
|
+
if (currency === "WIN" || currency === "ETH" || currency === "SOL" || currency === "B3") {
|
|
187
|
+
return 18;
|
|
188
|
+
}
|
|
189
|
+
// Fiat currencies with cent-like decimals
|
|
190
|
+
if (currency === "USD" || currency === "EUR" || currency === "GBP" || currency === "CAD" || currency === "AUD") {
|
|
191
|
+
return 2;
|
|
192
|
+
}
|
|
193
|
+
// Currencies without fractional units
|
|
194
|
+
if (currency === "JPY" || currency === "KRW") {
|
|
195
|
+
return 0;
|
|
196
|
+
}
|
|
197
|
+
// Default to 18 decimals (wei-like)
|
|
198
|
+
return 18;
|
|
199
|
+
}
|