@provex/utils 1.0.16 → 1.2.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/chain.d.ts +446 -0
- package/dist/chain.js +98 -0
- package/dist/chain.test.js +112 -0
- package/dist/contracts.d.ts +206 -0
- package/dist/contracts.js +292 -0
- package/dist/contracts.test.js +243 -0
- package/dist/conversionRates.d.ts +215 -0
- package/dist/{src/conversionRates.js → conversionRates.js} +74 -4
- package/dist/conversionRates.test.js +473 -0
- package/dist/currencies.d.ts +248 -0
- package/dist/{src/currencies.js → currencies.js} +119 -38
- package/dist/currencies.test.js +343 -0
- package/dist/data/countries.json +1099 -0
- package/dist/deposit.d.ts +69 -0
- package/dist/deposit.js +52 -0
- package/dist/depositKeys.d.ts +154 -0
- package/dist/depositKeys.js +104 -0
- package/dist/depositKeys.test.d.ts +1 -0
- package/dist/depositKeys.test.js +231 -0
- package/dist/events.d.ts +41 -0
- package/dist/events.js +92 -0
- package/dist/events.test.d.ts +1 -0
- package/dist/events.test.js +111 -0
- package/dist/fees.d.ts +136 -0
- package/dist/fees.js +178 -0
- package/dist/fees.test.d.ts +1 -0
- package/dist/fees.test.js +165 -0
- package/dist/ids.d.ts +175 -0
- package/dist/ids.js +231 -0
- package/dist/ids.test.d.ts +1 -0
- package/dist/ids.test.js +607 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +45 -0
- package/dist/input.d.ts +26 -0
- package/dist/{src/input.js → input.js} +23 -4
- package/dist/intent.d.ts +50 -0
- package/dist/intent.js +28 -0
- package/dist/intent.test.d.ts +1 -0
- package/dist/intent.test.js +119 -0
- package/dist/json.d.ts +55 -0
- package/dist/json.js +80 -0
- package/dist/participants.d.ts +22 -0
- package/dist/participants.js +28 -0
- package/dist/payment.d.ts +481 -0
- package/dist/payment.js +1149 -0
- package/dist/payment.test.d.ts +1 -0
- package/dist/payment.test.js +272 -0
- package/dist/peer-types.d.ts +327 -0
- package/dist/peer-types.js +169 -0
- package/dist/profit.d.ts +49 -0
- package/dist/profit.js +57 -0
- package/dist/profit.test.d.ts +1 -0
- package/dist/profit.test.js +315 -0
- package/dist/reputation.d.ts +125 -0
- package/dist/reputation.js +188 -0
- package/dist/reputation.test.d.ts +1 -0
- package/dist/reputation.test.js +156 -0
- package/dist/sort.d.ts +75 -0
- package/dist/sort.js +70 -0
- package/dist/sort.test.d.ts +1 -0
- package/dist/sort.test.js +216 -0
- package/dist/stateKey.d.ts +35 -0
- package/dist/stateKey.js +36 -0
- package/dist/stats.d.ts +61 -0
- package/dist/stats.js +53 -0
- package/dist/stats.test.d.ts +1 -0
- package/dist/stats.test.js +104 -0
- package/dist/time.d.ts +104 -0
- package/dist/time.js +162 -0
- package/dist/time.test.d.ts +1 -0
- package/dist/time.test.js +231 -0
- package/dist/tokens.d.ts +15502 -0
- package/dist/{src/tokens.js → tokens.js} +98 -17
- package/dist/tokens.test.d.ts +1 -0
- package/dist/tokens.test.js +193 -0
- package/dist/units.d.ts +200 -0
- package/dist/units.js +279 -0
- package/dist/units.test.d.ts +1 -0
- package/dist/units.test.js +327 -0
- package/package.json +119 -29
- package/dist/scripts/convert-svg.js +0 -17
- package/dist/src/chain.d.ts +0 -14
- package/dist/src/chain.js +0 -18
- package/dist/src/contracts.d.ts +0 -30
- package/dist/src/contracts.js +0 -64
- package/dist/src/conversionRates.d.ts +0 -124
- package/dist/src/currencies.d.ts +0 -144
- package/dist/src/data/countries.json +0 -1099
- package/dist/src/deposit.d.ts +0 -1
- package/dist/src/events.d.ts +0 -3
- package/dist/src/events.js +0 -16
- package/dist/src/ids.d.ts +0 -72
- package/dist/src/ids.js +0 -81
- package/dist/src/index.d.ts +0 -16
- package/dist/src/index.js +0 -16
- package/dist/src/input.d.ts +0 -7
- package/dist/src/intent.d.ts +0 -14
- package/dist/src/json.d.ts +0 -18
- package/dist/src/json.js +0 -44
- package/dist/src/participants.d.ts +0 -7
- package/dist/src/participants.js +0 -13
- package/dist/src/payment.d.ts +0 -164
- package/dist/src/payment.js +0 -439
- package/dist/src/profit.d.ts +0 -28
- package/dist/src/profit.js +0 -34
- package/dist/src/sort.d.ts +0 -36
- package/dist/src/sort.js +0 -39
- package/dist/src/stats.d.ts +0 -7
- package/dist/src/stats.js +0 -8
- package/dist/src/time.d.ts +0 -54
- package/dist/src/time.js +0 -106
- package/dist/src/tokens.d.ts +0 -3675
- package/dist/src/units.d.ts +0 -17
- package/dist/src/units.js +0 -79
- package/dist/src/zkp2p-types.d.ts +0 -48
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/scripts/convert-svg.ts +0 -22
- package/src/chain.ts +0 -27
- package/src/contracts.ts +0 -89
- package/src/conversionRates.ts +0 -356
- package/src/currencies.ts +0 -286
- package/src/data/countries.json +0 -1099
- package/src/deposit.ts +0 -2
- package/src/events.ts +0 -21
- package/src/flags/eur.svg +0 -4
- package/src/flags/usd.svg +0 -25
- package/src/flags.ts +0 -4
- package/src/global.d.ts +0 -9
- package/src/ids.ts +0 -137
- package/src/index.ts +0 -16
- package/src/input.ts +0 -35
- package/src/intent.ts +0 -15
- package/src/json.ts +0 -42
- package/src/participants.ts +0 -14
- package/src/payment.ts +0 -573
- package/src/profit.ts +0 -55
- package/src/sort.ts +0 -49
- package/src/stats.ts +0 -10
- package/src/time.ts +0 -123
- package/src/tokens.ts +0 -159
- package/src/units.ts +0 -120
- package/src/zkp2p-types.ts +0 -55
- package/tsconfig.json +0 -18
- /package/dist/{scripts/convert-svg.d.ts → chain.test.d.ts} +0 -0
- /package/dist/{src/deposit.js → contracts.test.d.ts} +0 -0
- /package/dist/{src/intent.js → conversionRates.test.d.ts} +0 -0
- /package/dist/{src/zkp2p-types.js → currencies.test.d.ts} +0 -0
- /package/dist/{src/flags.d.ts → flags.d.ts} +0 -0
- /package/dist/{src/flags.js → flags.js} +0 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Fiat currency definitions and utilities for the Provex protocol.
|
|
3
|
+
*
|
|
4
|
+
* This module defines supported fiat currencies with their symbols, decimals,
|
|
5
|
+
* and contract IDs (keccak256 hashes used for on-chain identification).
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* - Currency contract IDs are keccak256(currencyCode) used in smart contracts
|
|
9
|
+
* - Most currencies use 2 decimal places, but JPY/KRW/VND/UGX use 0
|
|
10
|
+
* - The Currency class provides parsing/formatting utilities for amounts
|
|
11
|
+
*
|
|
12
|
+
* @see {@link payment} for payment provider currency support
|
|
13
|
+
* @see {@link conversionRates} for exchange rate calculations
|
|
14
|
+
*/
|
|
15
|
+
import { type Hex } from 'viem';
|
|
16
|
+
import { type FormatRateDisplayOptions } from './units.js';
|
|
17
|
+
/**
|
|
18
|
+
* Currency information interface for display and calculations.
|
|
19
|
+
* Used for both the Currency class instances and serialized JSON representations.
|
|
20
|
+
*/
|
|
21
|
+
export interface CurrencyInfo {
|
|
22
|
+
/** ISO 4217 currency code (e.g., 'USD', 'EUR') */
|
|
23
|
+
ticker: string;
|
|
24
|
+
/** Currency symbol for display (e.g., '$', '€', '£') */
|
|
25
|
+
symbol: string;
|
|
26
|
+
/** Full currency name (e.g., 'US Dollar') */
|
|
27
|
+
name: string;
|
|
28
|
+
/** Number of decimal places (2 for most, 0 for JPY/KRW) */
|
|
29
|
+
decimals: number;
|
|
30
|
+
/** keccak256 hash of the currency code, used in smart contracts */
|
|
31
|
+
contractId: Hex;
|
|
32
|
+
/** Flag image URL or data URL for display */
|
|
33
|
+
flag: string;
|
|
34
|
+
/** Zoom level for flag display (1 for square flags USD/EUR, 1.5 for rectangular) */
|
|
35
|
+
zoom: number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Country info from the countries.json file
|
|
39
|
+
*/
|
|
40
|
+
export interface CountryInfo {
|
|
41
|
+
code: string;
|
|
42
|
+
name: string;
|
|
43
|
+
country: string;
|
|
44
|
+
countryCode: string;
|
|
45
|
+
flag?: string;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Convert a ticker to a contract ID
|
|
49
|
+
* @param ticker - The ticker to convert
|
|
50
|
+
* @returns The contract ID
|
|
51
|
+
*/
|
|
52
|
+
export declare const tickerToContractId: (ticker: string) => Hex;
|
|
53
|
+
/**
|
|
54
|
+
* Currency class representing a fiat currency with parsing and formatting utilities.
|
|
55
|
+
*
|
|
56
|
+
* Each currency has:
|
|
57
|
+
* - A unique ticker (ISO 4217 code)
|
|
58
|
+
* - A display symbol
|
|
59
|
+
* - A contract ID (keccak256 hash for on-chain use)
|
|
60
|
+
* - Decimal precision for amount formatting
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* const usd = SUPPORTED_CURRENCIES.USD
|
|
65
|
+
* const amount = usd.parseAmount('100.50') // 10050n (2 decimals)
|
|
66
|
+
* const display = usd.formatAmount(10050n) // '100.50'
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export declare class Currency implements CurrencyInfo {
|
|
70
|
+
readonly ticker: string;
|
|
71
|
+
readonly symbol: string;
|
|
72
|
+
readonly name: string;
|
|
73
|
+
readonly decimals: number;
|
|
74
|
+
readonly contractId: Hex;
|
|
75
|
+
readonly flag: string;
|
|
76
|
+
readonly zoom: number;
|
|
77
|
+
/**
|
|
78
|
+
* Create a new Currency instance.
|
|
79
|
+
*
|
|
80
|
+
* @param code - ISO 4217 currency code (converted to uppercase)
|
|
81
|
+
* @param symbol - Display symbol (e.g., '$')
|
|
82
|
+
* @param name - Full currency name
|
|
83
|
+
* @param decimals - Number of decimal places
|
|
84
|
+
* @param flag - Optional flag image URL
|
|
85
|
+
* @param zoom - Optional zoom level for flag display (default: 1.5)
|
|
86
|
+
*/
|
|
87
|
+
constructor(code: string, symbol: string, name: string, decimals: number, flag?: string, zoom?: number);
|
|
88
|
+
/**
|
|
89
|
+
* Generate a contract ID for the currency
|
|
90
|
+
* @returns The contract ID
|
|
91
|
+
*/
|
|
92
|
+
private generateContractId;
|
|
93
|
+
/**
|
|
94
|
+
* Parse an amount to a bigint
|
|
95
|
+
* @param amount - The amount to parse
|
|
96
|
+
* @returns The amount as a bigint
|
|
97
|
+
*/
|
|
98
|
+
parseAmount(amount: string): bigint | null;
|
|
99
|
+
/**
|
|
100
|
+
* Format an amount to a string
|
|
101
|
+
* @param amount - The amount to format
|
|
102
|
+
* @returns The amount as a string
|
|
103
|
+
*/
|
|
104
|
+
formatAmount(amount: bigint | null, options?: Partial<FormatRateDisplayOptions>): string | null;
|
|
105
|
+
/**
|
|
106
|
+
* Convert to plain object for compatibility
|
|
107
|
+
* @returns The currency info
|
|
108
|
+
*/
|
|
109
|
+
toJSON(): CurrencyInfo;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Generate contract ID for a currency code (standalone function for backward compatibility)
|
|
113
|
+
* @param currencyCode - The currency code
|
|
114
|
+
*/
|
|
115
|
+
export declare function generateCurrencyId(currencyCode: string): string;
|
|
116
|
+
/**
|
|
117
|
+
* Registry of all supported fiat currencies.
|
|
118
|
+
*
|
|
119
|
+
* Each currency is a Currency instance with:
|
|
120
|
+
* - Correct decimal precision (0 for JPY/KRW/VND/UGX, 2 for others)
|
|
121
|
+
* - Unique contract ID (keccak256 hash)
|
|
122
|
+
* - Flag image for UI display
|
|
123
|
+
*
|
|
124
|
+
* @remarks
|
|
125
|
+
* Payment providers support different subsets of currencies.
|
|
126
|
+
* Use `getCurrenciesForProvider()` in payment.ts to get supported currencies
|
|
127
|
+
* for a specific payment method.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* // Get USD currency info
|
|
132
|
+
* const usd = SUPPORTED_CURRENCIES.USD
|
|
133
|
+
* console.log(usd.symbol) // '$'
|
|
134
|
+
* console.log(usd.contractId) // '0x...' (keccak256('USD'))
|
|
135
|
+
*
|
|
136
|
+
* // Parse user input
|
|
137
|
+
* const amount = usd.parseAmount('100.50')
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
export declare const SUPPORTED_CURRENCIES: {
|
|
141
|
+
readonly USD: Currency;
|
|
142
|
+
readonly EUR: Currency;
|
|
143
|
+
readonly GBP: Currency;
|
|
144
|
+
readonly AUD: Currency;
|
|
145
|
+
readonly CAD: Currency;
|
|
146
|
+
readonly NZD: Currency;
|
|
147
|
+
readonly CHF: Currency;
|
|
148
|
+
readonly SEK: Currency;
|
|
149
|
+
readonly NOK: Currency;
|
|
150
|
+
readonly DKK: Currency;
|
|
151
|
+
readonly PLN: Currency;
|
|
152
|
+
readonly CZK: Currency;
|
|
153
|
+
readonly ARS: Currency;
|
|
154
|
+
readonly CNY: Currency;
|
|
155
|
+
readonly MXN: Currency;
|
|
156
|
+
readonly JPY: Currency;
|
|
157
|
+
readonly THB: Currency;
|
|
158
|
+
readonly RUB: Currency;
|
|
159
|
+
readonly AED: Currency;
|
|
160
|
+
readonly ZAR: Currency;
|
|
161
|
+
readonly HKD: Currency;
|
|
162
|
+
readonly SGD: Currency;
|
|
163
|
+
readonly SAR: Currency;
|
|
164
|
+
readonly ILS: Currency;
|
|
165
|
+
readonly IDR: Currency;
|
|
166
|
+
readonly MYR: Currency;
|
|
167
|
+
readonly PHP: Currency;
|
|
168
|
+
readonly VND: Currency;
|
|
169
|
+
readonly KRW: Currency;
|
|
170
|
+
readonly INR: Currency;
|
|
171
|
+
readonly BRL: Currency;
|
|
172
|
+
readonly KES: Currency;
|
|
173
|
+
readonly TRY: Currency;
|
|
174
|
+
readonly UGX: Currency;
|
|
175
|
+
readonly RON: Currency;
|
|
176
|
+
readonly HUF: Currency;
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* Union type of all supported currency codes.
|
|
180
|
+
* Use for type-safe currency code parameters.
|
|
181
|
+
*/
|
|
182
|
+
export type SupportedCurrencyCode = keyof typeof SUPPORTED_CURRENCIES;
|
|
183
|
+
/**
|
|
184
|
+
* Map from contract ID (keccak256 hash) to Currency instance.
|
|
185
|
+
* Used for O(1) lookup of currency info from on-chain data.
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```ts
|
|
189
|
+
* const contractId = '0x...' // from blockchain event
|
|
190
|
+
* const currency = currencyById.get(contractId)
|
|
191
|
+
* if (currency) {
|
|
192
|
+
* console.log(currency.ticker) // 'USD'
|
|
193
|
+
* }
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
export declare const currencyById: Map<`0x${string}`, Currency>;
|
|
197
|
+
/**
|
|
198
|
+
* Get the currency name/ticker by contract ID
|
|
199
|
+
* @param contractId - The contract ID (keccak256 hash)
|
|
200
|
+
* @returns The currency ticker or the original contractId if not found
|
|
201
|
+
*/
|
|
202
|
+
export declare function getCurrencyNameByContractId(contractId: string): string;
|
|
203
|
+
/**
|
|
204
|
+
* Get the currency info for a currency code
|
|
205
|
+
* @param currencyCode - The currency code
|
|
206
|
+
* @returns The currency info
|
|
207
|
+
*/
|
|
208
|
+
export declare function getCurrencyInfo(currencyCode: string): CurrencyInfo | null;
|
|
209
|
+
/**
|
|
210
|
+
* Get the currency info for a contract ID
|
|
211
|
+
* @param contractId - The contract ID
|
|
212
|
+
* @returns The currency info
|
|
213
|
+
*/
|
|
214
|
+
export declare function getCurrencyByContractId(contractId: string): Currency | null;
|
|
215
|
+
/**
|
|
216
|
+
* Format a currency amount
|
|
217
|
+
* @param amount - The amount to format
|
|
218
|
+
* @param currencyCode - The currency code
|
|
219
|
+
* @returns The formatted currency amount
|
|
220
|
+
*/
|
|
221
|
+
export declare function formatCurrencyAmount(amount: string | number, currencyCode: string): string;
|
|
222
|
+
/**
|
|
223
|
+
* Get all supported currencies
|
|
224
|
+
* @returns All supported currencies
|
|
225
|
+
*/
|
|
226
|
+
export declare function getAllSupportedCurrencies(): CurrencyInfo[];
|
|
227
|
+
/**
|
|
228
|
+
* Check if a currency is supported
|
|
229
|
+
* @param currencyCode - The currency code
|
|
230
|
+
* @returns True if the currency is supported, false otherwise
|
|
231
|
+
*/
|
|
232
|
+
export declare function isSupportedCurrency(currencyCode: string): boolean;
|
|
233
|
+
/**
|
|
234
|
+
* Format the default conversion rate based on currency decimals.
|
|
235
|
+
* Returns "1" for zero-decimal currencies (JPY, KRW, VND, UGX),
|
|
236
|
+
* or "1.00" for standard 2-decimal currencies (USD, EUR, etc.).
|
|
237
|
+
*
|
|
238
|
+
* @param currency - The currency to format the default rate for
|
|
239
|
+
* @returns Formatted default rate string matching the currency's decimal precision
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* ```ts
|
|
243
|
+
* formatDefaultRate(SUPPORTED_CURRENCIES.JPY) // "1" (0 decimals)
|
|
244
|
+
* formatDefaultRate(SUPPORTED_CURRENCIES.USD) // "1.00" (2 decimals)
|
|
245
|
+
* formatDefaultRate(SUPPORTED_CURRENCIES.EUR) // "1.00" (2 decimals)
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
export declare function formatDefaultRate(currency: Currency | CurrencyInfo): string;
|
|
@@ -1,9 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Fiat currency definitions and utilities for the Provex protocol.
|
|
3
|
+
*
|
|
4
|
+
* This module defines supported fiat currencies with their symbols, decimals,
|
|
5
|
+
* and contract IDs (keccak256 hashes used for on-chain identification).
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* - Currency contract IDs are keccak256(currencyCode) used in smart contracts
|
|
9
|
+
* - Most currencies use 2 decimal places, but JPY/KRW/VND/UGX use 0
|
|
10
|
+
* - The Currency class provides parsing/formatting utilities for amounts
|
|
11
|
+
*
|
|
12
|
+
* @see {@link payment} for payment provider currency support
|
|
13
|
+
* @see {@link conversionRates} for exchange rate calculations
|
|
14
|
+
*/
|
|
1
15
|
import { isHash, keccak256, toBytes } from 'viem';
|
|
2
16
|
import { addThousandsSeparator, formatUnits, parseUnits } from './units.js';
|
|
3
17
|
import countriesData from './data/countries.json' with { type: 'json' };
|
|
4
18
|
import { USD_FLAG_SVG, EUR_FLAG_SVG } from './flags.js';
|
|
5
|
-
|
|
19
|
+
/** Pre-computed data URL for USD flag (custom square design) */
|
|
6
20
|
const USD_FLAG_DATA_URL = `data:image/svg+xml;base64,${btoa(USD_FLAG_SVG)}`;
|
|
21
|
+
/** Pre-computed data URL for EUR flag (custom square design) */
|
|
7
22
|
const EUR_FLAG_DATA_URL = `data:image/svg+xml;base64,${btoa(EUR_FLAG_SVG)}`;
|
|
8
23
|
const countries = countriesData;
|
|
9
24
|
/**
|
|
@@ -15,12 +30,20 @@ export const tickerToContractId = (ticker) => {
|
|
|
15
30
|
return keccak256(toBytes(ticker.toUpperCase()));
|
|
16
31
|
};
|
|
17
32
|
/**
|
|
18
|
-
* Currency class
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
33
|
+
* Currency class representing a fiat currency with parsing and formatting utilities.
|
|
34
|
+
*
|
|
35
|
+
* Each currency has:
|
|
36
|
+
* - A unique ticker (ISO 4217 code)
|
|
37
|
+
* - A display symbol
|
|
38
|
+
* - A contract ID (keccak256 hash for on-chain use)
|
|
39
|
+
* - Decimal precision for amount formatting
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const usd = SUPPORTED_CURRENCIES.USD
|
|
44
|
+
* const amount = usd.parseAmount('100.50') // 10050n (2 decimals)
|
|
45
|
+
* const display = usd.formatAmount(10050n) // '100.50'
|
|
46
|
+
* ```
|
|
24
47
|
*/
|
|
25
48
|
export class Currency {
|
|
26
49
|
ticker;
|
|
@@ -29,12 +52,24 @@ export class Currency {
|
|
|
29
52
|
decimals;
|
|
30
53
|
contractId;
|
|
31
54
|
flag;
|
|
32
|
-
|
|
55
|
+
zoom;
|
|
56
|
+
/**
|
|
57
|
+
* Create a new Currency instance.
|
|
58
|
+
*
|
|
59
|
+
* @param code - ISO 4217 currency code (converted to uppercase)
|
|
60
|
+
* @param symbol - Display symbol (e.g., '$')
|
|
61
|
+
* @param name - Full currency name
|
|
62
|
+
* @param decimals - Number of decimal places
|
|
63
|
+
* @param flag - Optional flag image URL
|
|
64
|
+
* @param zoom - Optional zoom level for flag display (default: 1.5)
|
|
65
|
+
*/
|
|
66
|
+
constructor(code, symbol, name, decimals, flag, zoom) {
|
|
33
67
|
this.ticker = code.toUpperCase();
|
|
34
68
|
this.symbol = symbol;
|
|
35
69
|
this.name = name;
|
|
36
70
|
this.decimals = decimals;
|
|
37
|
-
this.flag = flag || '';
|
|
71
|
+
this.flag = flag || '';
|
|
72
|
+
this.zoom = zoom ?? 1.5;
|
|
38
73
|
this.contractId = this.generateContractId();
|
|
39
74
|
}
|
|
40
75
|
/**
|
|
@@ -64,21 +99,9 @@ export class Currency {
|
|
|
64
99
|
* @returns The amount as a string
|
|
65
100
|
*/
|
|
66
101
|
formatAmount(amount, options = {}) {
|
|
67
|
-
|
|
68
|
-
if (amount === null) {
|
|
69
|
-
return null;
|
|
70
|
-
}
|
|
71
|
-
return formatUnits(amount, {
|
|
72
|
-
decimals: this.decimals,
|
|
73
|
-
truncateDecimals: 2,
|
|
74
|
-
padDecimals: 2,
|
|
75
|
-
...options,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
catch (err) {
|
|
79
|
-
console.error('Error formatting amount: %o', amount, err);
|
|
102
|
+
if (amount === null)
|
|
80
103
|
return null;
|
|
81
|
-
}
|
|
104
|
+
return formatUnits(amount, { decimals: this.decimals, truncateDecimals: 2, padDecimals: 2, ...options });
|
|
82
105
|
}
|
|
83
106
|
/**
|
|
84
107
|
* Convert to plain object for compatibility
|
|
@@ -92,6 +115,7 @@ export class Currency {
|
|
|
92
115
|
decimals: this.decimals,
|
|
93
116
|
contractId: this.contractId,
|
|
94
117
|
flag: this.flag,
|
|
118
|
+
zoom: this.zoom,
|
|
95
119
|
};
|
|
96
120
|
}
|
|
97
121
|
}
|
|
@@ -109,13 +133,35 @@ export function generateCurrencyId(currencyCode) {
|
|
|
109
133
|
* @returns The flag image path
|
|
110
134
|
*/
|
|
111
135
|
const flagFromCountries = (countryCode) => {
|
|
112
|
-
|
|
113
|
-
return country?.flag || '';
|
|
136
|
+
return countries.find((country) => country.code === countryCode)?.flag ?? '';
|
|
114
137
|
};
|
|
115
|
-
|
|
138
|
+
/**
|
|
139
|
+
* Registry of all supported fiat currencies.
|
|
140
|
+
*
|
|
141
|
+
* Each currency is a Currency instance with:
|
|
142
|
+
* - Correct decimal precision (0 for JPY/KRW/VND/UGX, 2 for others)
|
|
143
|
+
* - Unique contract ID (keccak256 hash)
|
|
144
|
+
* - Flag image for UI display
|
|
145
|
+
*
|
|
146
|
+
* @remarks
|
|
147
|
+
* Payment providers support different subsets of currencies.
|
|
148
|
+
* Use `getCurrenciesForProvider()` in payment.ts to get supported currencies
|
|
149
|
+
* for a specific payment method.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```ts
|
|
153
|
+
* // Get USD currency info
|
|
154
|
+
* const usd = SUPPORTED_CURRENCIES.USD
|
|
155
|
+
* console.log(usd.symbol) // '$'
|
|
156
|
+
* console.log(usd.contractId) // '0x...' (keccak256('USD'))
|
|
157
|
+
*
|
|
158
|
+
* // Parse user input
|
|
159
|
+
* const amount = usd.parseAmount('100.50')
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
116
162
|
export const SUPPORTED_CURRENCIES = {
|
|
117
|
-
USD: new Currency('USD', '$', 'US Dollar', 2, USD_FLAG_DATA_URL),
|
|
118
|
-
EUR: new Currency('EUR', '€', 'Euro', 2, EUR_FLAG_DATA_URL),
|
|
163
|
+
USD: new Currency('USD', '$', 'US Dollar', 2, USD_FLAG_DATA_URL, 1),
|
|
164
|
+
EUR: new Currency('EUR', '€', 'Euro', 2, EUR_FLAG_DATA_URL, 1),
|
|
119
165
|
GBP: new Currency('GBP', '£', 'British Pound', 2, flagFromCountries('GBP')),
|
|
120
166
|
AUD: new Currency('AUD', 'A$', 'Australian Dollar', 2, flagFromCountries('AUD')),
|
|
121
167
|
CAD: new Currency('CAD', 'C$', 'Canadian Dollar', 2, flagFromCountries('CAD')),
|
|
@@ -151,17 +197,33 @@ export const SUPPORTED_CURRENCIES = {
|
|
|
151
197
|
RON: new Currency('RON', 'RON', 'Romanian Leu', 2, flagFromCountries('RON')),
|
|
152
198
|
HUF: new Currency('HUF', 'Ft', 'Hungarian Forint', 2, flagFromCountries('HUF')),
|
|
153
199
|
};
|
|
154
|
-
/**
|
|
200
|
+
/** Sorted entries for consistent iteration order */
|
|
155
201
|
const entries = Object.entries(SUPPORTED_CURRENCIES).sort(([a], [b]) => a.localeCompare(b));
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
202
|
+
/**
|
|
203
|
+
* Map from contract ID (keccak256 hash) to Currency instance.
|
|
204
|
+
* Used for O(1) lookup of currency info from on-chain data.
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* ```ts
|
|
208
|
+
* const contractId = '0x...' // from blockchain event
|
|
209
|
+
* const currency = currencyById.get(contractId)
|
|
210
|
+
* if (currency) {
|
|
211
|
+
* console.log(currency.ticker) // 'USD'
|
|
212
|
+
* }
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
162
215
|
export const currencyById = new Map(entries.map(([, currency]) => {
|
|
163
216
|
return [currency.contractId, currency];
|
|
164
217
|
}));
|
|
218
|
+
/**
|
|
219
|
+
* Get the currency name/ticker by contract ID
|
|
220
|
+
* @param contractId - The contract ID (keccak256 hash)
|
|
221
|
+
* @returns The currency ticker or the original contractId if not found
|
|
222
|
+
*/
|
|
223
|
+
export function getCurrencyNameByContractId(contractId) {
|
|
224
|
+
const currency = currencyById.get(contractId);
|
|
225
|
+
return currency?.ticker ?? contractId;
|
|
226
|
+
}
|
|
165
227
|
/**
|
|
166
228
|
* Get the currency info for a currency code
|
|
167
229
|
* @param currencyCode - The currency code
|
|
@@ -169,7 +231,7 @@ export const currencyById = new Map(entries.map(([, currency]) => {
|
|
|
169
231
|
*/
|
|
170
232
|
export function getCurrencyInfo(currencyCode) {
|
|
171
233
|
const currency = SUPPORTED_CURRENCIES[currencyCode.toUpperCase()];
|
|
172
|
-
return currency
|
|
234
|
+
return currency?.toJSON() ?? null;
|
|
173
235
|
}
|
|
174
236
|
/**
|
|
175
237
|
* Get the currency info for a contract ID
|
|
@@ -180,8 +242,7 @@ export function getCurrencyByContractId(contractId) {
|
|
|
180
242
|
if (!isHash(contractId)) {
|
|
181
243
|
throw new Error('Invalid contract ID');
|
|
182
244
|
}
|
|
183
|
-
|
|
184
|
-
return currency ? currency : null;
|
|
245
|
+
return currencyById.get(contractId) ?? null;
|
|
185
246
|
}
|
|
186
247
|
/**
|
|
187
248
|
* Format currency amount safely without Number() constructor, preserving decimal places
|
|
@@ -232,3 +293,23 @@ export function getAllSupportedCurrencies() {
|
|
|
232
293
|
export function isSupportedCurrency(currencyCode) {
|
|
233
294
|
return currencyCode.toUpperCase() in SUPPORTED_CURRENCIES;
|
|
234
295
|
}
|
|
296
|
+
/**
|
|
297
|
+
* Format the default conversion rate based on currency decimals.
|
|
298
|
+
* Returns "1" for zero-decimal currencies (JPY, KRW, VND, UGX),
|
|
299
|
+
* or "1.00" for standard 2-decimal currencies (USD, EUR, etc.).
|
|
300
|
+
*
|
|
301
|
+
* @param currency - The currency to format the default rate for
|
|
302
|
+
* @returns Formatted default rate string matching the currency's decimal precision
|
|
303
|
+
*
|
|
304
|
+
* @example
|
|
305
|
+
* ```ts
|
|
306
|
+
* formatDefaultRate(SUPPORTED_CURRENCIES.JPY) // "1" (0 decimals)
|
|
307
|
+
* formatDefaultRate(SUPPORTED_CURRENCIES.USD) // "1.00" (2 decimals)
|
|
308
|
+
* formatDefaultRate(SUPPORTED_CURRENCIES.EUR) // "1.00" (2 decimals)
|
|
309
|
+
* ```
|
|
310
|
+
*/
|
|
311
|
+
export function formatDefaultRate(currency) {
|
|
312
|
+
if (currency.decimals === 0)
|
|
313
|
+
return '1';
|
|
314
|
+
return `1.${'0'.repeat(currency.decimals)}`;
|
|
315
|
+
}
|