@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.
Files changed (149) hide show
  1. package/dist/chain.d.ts +446 -0
  2. package/dist/chain.js +98 -0
  3. package/dist/chain.test.js +112 -0
  4. package/dist/contracts.d.ts +206 -0
  5. package/dist/contracts.js +292 -0
  6. package/dist/contracts.test.js +243 -0
  7. package/dist/conversionRates.d.ts +215 -0
  8. package/dist/{src/conversionRates.js → conversionRates.js} +74 -4
  9. package/dist/conversionRates.test.js +473 -0
  10. package/dist/currencies.d.ts +248 -0
  11. package/dist/{src/currencies.js → currencies.js} +119 -38
  12. package/dist/currencies.test.js +343 -0
  13. package/dist/data/countries.json +1099 -0
  14. package/dist/deposit.d.ts +69 -0
  15. package/dist/deposit.js +52 -0
  16. package/dist/depositKeys.d.ts +154 -0
  17. package/dist/depositKeys.js +104 -0
  18. package/dist/depositKeys.test.d.ts +1 -0
  19. package/dist/depositKeys.test.js +231 -0
  20. package/dist/events.d.ts +41 -0
  21. package/dist/events.js +92 -0
  22. package/dist/events.test.d.ts +1 -0
  23. package/dist/events.test.js +111 -0
  24. package/dist/fees.d.ts +136 -0
  25. package/dist/fees.js +178 -0
  26. package/dist/fees.test.d.ts +1 -0
  27. package/dist/fees.test.js +165 -0
  28. package/dist/ids.d.ts +175 -0
  29. package/dist/ids.js +231 -0
  30. package/dist/ids.test.d.ts +1 -0
  31. package/dist/ids.test.js +607 -0
  32. package/dist/index.d.ts +45 -0
  33. package/dist/index.js +45 -0
  34. package/dist/input.d.ts +26 -0
  35. package/dist/{src/input.js → input.js} +23 -4
  36. package/dist/intent.d.ts +50 -0
  37. package/dist/intent.js +28 -0
  38. package/dist/intent.test.d.ts +1 -0
  39. package/dist/intent.test.js +119 -0
  40. package/dist/json.d.ts +55 -0
  41. package/dist/json.js +80 -0
  42. package/dist/participants.d.ts +22 -0
  43. package/dist/participants.js +28 -0
  44. package/dist/payment.d.ts +481 -0
  45. package/dist/payment.js +1149 -0
  46. package/dist/payment.test.d.ts +1 -0
  47. package/dist/payment.test.js +272 -0
  48. package/dist/peer-types.d.ts +327 -0
  49. package/dist/peer-types.js +169 -0
  50. package/dist/profit.d.ts +49 -0
  51. package/dist/profit.js +57 -0
  52. package/dist/profit.test.d.ts +1 -0
  53. package/dist/profit.test.js +315 -0
  54. package/dist/reputation.d.ts +125 -0
  55. package/dist/reputation.js +188 -0
  56. package/dist/reputation.test.d.ts +1 -0
  57. package/dist/reputation.test.js +156 -0
  58. package/dist/sort.d.ts +75 -0
  59. package/dist/sort.js +70 -0
  60. package/dist/sort.test.d.ts +1 -0
  61. package/dist/sort.test.js +216 -0
  62. package/dist/stateKey.d.ts +35 -0
  63. package/dist/stateKey.js +36 -0
  64. package/dist/stats.d.ts +61 -0
  65. package/dist/stats.js +53 -0
  66. package/dist/stats.test.d.ts +1 -0
  67. package/dist/stats.test.js +104 -0
  68. package/dist/time.d.ts +104 -0
  69. package/dist/time.js +162 -0
  70. package/dist/time.test.d.ts +1 -0
  71. package/dist/time.test.js +231 -0
  72. package/dist/tokens.d.ts +15502 -0
  73. package/dist/{src/tokens.js → tokens.js} +98 -17
  74. package/dist/tokens.test.d.ts +1 -0
  75. package/dist/tokens.test.js +193 -0
  76. package/dist/units.d.ts +200 -0
  77. package/dist/units.js +279 -0
  78. package/dist/units.test.d.ts +1 -0
  79. package/dist/units.test.js +327 -0
  80. package/package.json +119 -29
  81. package/dist/scripts/convert-svg.js +0 -17
  82. package/dist/src/chain.d.ts +0 -14
  83. package/dist/src/chain.js +0 -18
  84. package/dist/src/contracts.d.ts +0 -30
  85. package/dist/src/contracts.js +0 -64
  86. package/dist/src/conversionRates.d.ts +0 -124
  87. package/dist/src/currencies.d.ts +0 -144
  88. package/dist/src/data/countries.json +0 -1099
  89. package/dist/src/deposit.d.ts +0 -1
  90. package/dist/src/events.d.ts +0 -3
  91. package/dist/src/events.js +0 -16
  92. package/dist/src/ids.d.ts +0 -72
  93. package/dist/src/ids.js +0 -81
  94. package/dist/src/index.d.ts +0 -16
  95. package/dist/src/index.js +0 -16
  96. package/dist/src/input.d.ts +0 -7
  97. package/dist/src/intent.d.ts +0 -14
  98. package/dist/src/json.d.ts +0 -18
  99. package/dist/src/json.js +0 -44
  100. package/dist/src/participants.d.ts +0 -7
  101. package/dist/src/participants.js +0 -13
  102. package/dist/src/payment.d.ts +0 -164
  103. package/dist/src/payment.js +0 -439
  104. package/dist/src/profit.d.ts +0 -28
  105. package/dist/src/profit.js +0 -34
  106. package/dist/src/sort.d.ts +0 -36
  107. package/dist/src/sort.js +0 -39
  108. package/dist/src/stats.d.ts +0 -7
  109. package/dist/src/stats.js +0 -8
  110. package/dist/src/time.d.ts +0 -54
  111. package/dist/src/time.js +0 -106
  112. package/dist/src/tokens.d.ts +0 -3675
  113. package/dist/src/units.d.ts +0 -17
  114. package/dist/src/units.js +0 -79
  115. package/dist/src/zkp2p-types.d.ts +0 -48
  116. package/dist/tsconfig.tsbuildinfo +0 -1
  117. package/scripts/convert-svg.ts +0 -22
  118. package/src/chain.ts +0 -27
  119. package/src/contracts.ts +0 -89
  120. package/src/conversionRates.ts +0 -356
  121. package/src/currencies.ts +0 -286
  122. package/src/data/countries.json +0 -1099
  123. package/src/deposit.ts +0 -2
  124. package/src/events.ts +0 -21
  125. package/src/flags/eur.svg +0 -4
  126. package/src/flags/usd.svg +0 -25
  127. package/src/flags.ts +0 -4
  128. package/src/global.d.ts +0 -9
  129. package/src/ids.ts +0 -137
  130. package/src/index.ts +0 -16
  131. package/src/input.ts +0 -35
  132. package/src/intent.ts +0 -15
  133. package/src/json.ts +0 -42
  134. package/src/participants.ts +0 -14
  135. package/src/payment.ts +0 -573
  136. package/src/profit.ts +0 -55
  137. package/src/sort.ts +0 -49
  138. package/src/stats.ts +0 -10
  139. package/src/time.ts +0 -123
  140. package/src/tokens.ts +0 -159
  141. package/src/units.ts +0 -120
  142. package/src/zkp2p-types.ts +0 -55
  143. package/tsconfig.json +0 -18
  144. /package/dist/{scripts/convert-svg.d.ts → chain.test.d.ts} +0 -0
  145. /package/dist/{src/deposit.js → contracts.test.d.ts} +0 -0
  146. /package/dist/{src/intent.js → conversionRates.test.d.ts} +0 -0
  147. /package/dist/{src/zkp2p-types.js → currencies.test.d.ts} +0 -0
  148. /package/dist/{src/flags.d.ts → flags.d.ts} +0 -0
  149. /package/dist/{src/flags.js → flags.js} +0 -0
package/src/currencies.ts DELETED
@@ -1,286 +0,0 @@
1
- import { isHash, keccak256, toBytes, type Hex } from 'viem'
2
- import { addThousandsSeparator, type FormatRateDisplayOptions, formatUnits, parseUnits } from './units.js'
3
- import countriesData from './data/countries.json' with { type: 'json' }
4
- import { USD_FLAG_SVG, EUR_FLAG_SVG } from './flags.js'
5
-
6
- // Convert SVG strings to data URLs
7
- const USD_FLAG_DATA_URL = `data:image/svg+xml;base64,${btoa(USD_FLAG_SVG)}`
8
- const EUR_FLAG_DATA_URL = `data:image/svg+xml;base64,${btoa(EUR_FLAG_SVG)}`
9
-
10
- const countries = countriesData as CountryInfo[]
11
-
12
- /**
13
- * Relevant info for a currency
14
- */
15
- export interface CurrencyInfo {
16
- ticker: string
17
- symbol: string
18
- name: string
19
- decimals: number
20
- contractId: Hex // bytes32 hash of the currency code
21
- flag: string // flag image path
22
- }
23
-
24
- /**
25
- * Country info from the countries.json file
26
- */
27
- export interface CountryInfo {
28
- code: string
29
- name: string
30
- country: string
31
- countryCode: string
32
- flag?: string
33
- }
34
-
35
- /**
36
- * Convert a ticker to a contract ID
37
- * @param ticker - The ticker to convert
38
- * @returns The contract ID
39
- */
40
- export const tickerToContractId = (ticker: string): Hex => {
41
- return keccak256(toBytes(ticker.toUpperCase()))
42
- }
43
-
44
- /**
45
- * Currency class that automatically generates contract ID
46
- * @param code - The currency code
47
- * @param symbol - The currency symbol
48
- * @param name - The currency name
49
- * @param decimals - The number of decimals
50
- * @param flag - The flag image path
51
- */
52
- export class Currency implements CurrencyInfo {
53
- public readonly ticker: string
54
- public readonly symbol: string
55
- public readonly name: string
56
- public readonly decimals: number
57
- public readonly contractId: Hex
58
- public readonly flag: string
59
-
60
- constructor(code: string, symbol: string, name: string, decimals: number, flag?: string) {
61
- this.ticker = code.toUpperCase()
62
- this.symbol = symbol
63
- this.name = name
64
- this.decimals = decimals
65
- this.flag = flag || '' // figure out a fallback flag
66
- this.contractId = this.generateContractId()
67
- }
68
-
69
- /**
70
- * Generate a contract ID for the currency
71
- * @returns The contract ID
72
- */
73
- private generateContractId(): Hex {
74
- return tickerToContractId(this.ticker)
75
- }
76
-
77
- /**
78
- * Parse an amount to a bigint
79
- * @param amount - The amount to parse
80
- * @returns The amount as a bigint
81
- */
82
- parseAmount(amount: string): bigint | null {
83
- try {
84
- return parseUnits(amount, { decimals: this.decimals })
85
- } catch (error) {
86
- console.error('Error parsing amount: %o', amount, error)
87
- return null
88
- }
89
- }
90
-
91
- /**
92
- * Format an amount to a string
93
- * @param amount - The amount to format
94
- * @returns The amount as a string
95
- */
96
- formatAmount(amount: bigint | null, options: Partial<FormatRateDisplayOptions> = {}): string | null {
97
- try {
98
- if (amount === null) {
99
- return null
100
- }
101
- return formatUnits(amount, {
102
- decimals: this.decimals,
103
- truncateDecimals: 2,
104
- padDecimals: 2,
105
- ...options,
106
- })
107
- } catch (err) {
108
- console.error('Error formatting amount: %o', amount, err)
109
- return null
110
- }
111
- }
112
-
113
- /**
114
- * Convert to plain object for compatibility
115
- * @returns The currency info
116
- */
117
- toJSON(): CurrencyInfo {
118
- return {
119
- ticker: this.ticker,
120
- symbol: this.symbol,
121
- name: this.name,
122
- decimals: this.decimals,
123
- contractId: this.contractId,
124
- flag: this.flag,
125
- }
126
- }
127
- }
128
-
129
- /**
130
- * Generate contract ID for a currency code (standalone function for backward compatibility)
131
- * @param currencyCode - The currency code
132
- */
133
- export function generateCurrencyId(currencyCode: string): string {
134
- const bytes = toBytes(currencyCode.toUpperCase())
135
- return keccak256(bytes)
136
- }
137
-
138
- /**
139
- * Get the flag for a country
140
- * @param countryCode - The country code
141
- * @returns The flag image path
142
- */
143
- const flagFromCountries = (countryCode: string): string => {
144
- const country = countries.find((country) => country.code === countryCode)
145
- return country?.flag || ''
146
- }
147
-
148
- // Supported fiat currencies with local flag files for major ones
149
- export const SUPPORTED_CURRENCIES = {
150
- USD: new Currency('USD', '$', 'US Dollar', 2, USD_FLAG_DATA_URL),
151
- EUR: new Currency('EUR', '€', 'Euro', 2, EUR_FLAG_DATA_URL),
152
- GBP: new Currency('GBP', '£', 'British Pound', 2, flagFromCountries('GBP')),
153
- AUD: new Currency('AUD', 'A$', 'Australian Dollar', 2, flagFromCountries('AUD')),
154
- CAD: new Currency('CAD', 'C$', 'Canadian Dollar', 2, flagFromCountries('CAD')),
155
- NZD: new Currency('NZD', 'NZ$', 'New Zealand Dollar', 2, flagFromCountries('NZD')),
156
- CHF: new Currency('CHF', 'CHF', 'Swiss Franc', 2, flagFromCountries('CHF')),
157
- SEK: new Currency('SEK', 'kr', 'Swedish Krona', 2, flagFromCountries('SEK')),
158
- NOK: new Currency('NOK', 'kr', 'Norwegian Krone', 2, flagFromCountries('NOK')),
159
- DKK: new Currency('DKK', 'kr', 'Danish Krone', 2, flagFromCountries('DKK')),
160
- PLN: new Currency('PLN', 'zł', 'Polish Złoty', 2, flagFromCountries('PLN')),
161
- CZK: new Currency('CZK', 'Kč', 'Czech Koruna', 2, flagFromCountries('CZK')),
162
- ARS: new Currency('ARS', 'ARS', 'Argentine Peso', 2, flagFromCountries('ARS')),
163
- CNY: new Currency('CNY', '¥', 'Chinese Yuan', 2, flagFromCountries('CNY')),
164
- MXN: new Currency('MXN', 'MX$', 'Mexican Peso', 2, flagFromCountries('MXN')),
165
- JPY: new Currency('JPY', '¥', 'Japanese Yen', 0, flagFromCountries('JPY')),
166
- THB: new Currency('THB', '฿', 'Thai Baht', 2, flagFromCountries('THB')),
167
- RUB: new Currency('RUB', '₽', 'Russian Ruble', 2, flagFromCountries('RUB')),
168
- AED: new Currency('AED', 'AED', 'United Arab Emirates Dirham', 2, flagFromCountries('AED')),
169
- ZAR: new Currency('ZAR', 'R', 'South African Rand', 2, flagFromCountries('ZAR')),
170
- HKD: new Currency('HKD', 'HK$', 'Hong Kong Dollar', 2, flagFromCountries('HKD')),
171
- SGD: new Currency('SGD', 'S$', 'Singapore Dollar', 2, flagFromCountries('SGD')),
172
- SAR: new Currency('SAR', 'SAR', 'Saudi Riyal', 2, flagFromCountries('SAR')),
173
- ILS: new Currency('ILS', '₪', 'Israeli New Shekel', 2, flagFromCountries('ILS')),
174
- IDR: new Currency('IDR', 'Rp', 'Indonesian Rupiah', 2, flagFromCountries('IDR')),
175
- MYR: new Currency('MYR', 'RM', 'Malaysian Ringgit', 2, flagFromCountries('MYR')),
176
- PHP: new Currency('PHP', '₱', 'Philippine Peso', 2, flagFromCountries('PHP')),
177
- VND: new Currency('VND', '₫', 'Vietnamese Dong', 0, flagFromCountries('VND')),
178
- KRW: new Currency('KRW', '₩', 'South Korean Won', 0, flagFromCountries('KRW')),
179
- INR: new Currency('INR', '₹', 'Indian Rupee', 2, flagFromCountries('INR')),
180
- BRL: new Currency('BRL', 'R$', 'Brazilian Real', 2, flagFromCountries('BRL')),
181
- KES: new Currency('KES', 'KES', 'Kenyan Shilling', 2, flagFromCountries('KES')),
182
- TRY: new Currency('TRY', '₺', 'Turkish Lira', 2, flagFromCountries('TRY')),
183
- UGX: new Currency('UGX', 'USh', 'Ugandan Shilling', 0, flagFromCountries('UGX')),
184
- RON: new Currency('RON', 'RON', 'Romanian Leu', 2, flagFromCountries('RON')),
185
- HUF: new Currency('HUF', 'Ft', 'Hungarian Forint', 2, flagFromCountries('HUF')),
186
- } as const
187
-
188
- /** Supported currency code */
189
- export type SupportedCurrencyCode = keyof typeof SUPPORTED_CURRENCIES
190
-
191
- /** Entries for the supported currencies */
192
- const entries = Object.entries(SUPPORTED_CURRENCIES).sort(([a], [b]) => a.localeCompare(b))
193
-
194
- entries.forEach(([, currency]) => {
195
- // console.log(currency.ticker, currency.contractId)
196
- if (!currency.flag) {
197
- console.error(`missing flag for ${currency.ticker}`)
198
- }
199
- })
200
-
201
- export const currencyById = new Map(entries.map(([, currency]) => {
202
- return [currency.contractId, currency] as const
203
- }))
204
-
205
- /**
206
- * Get the currency info for a currency code
207
- * @param currencyCode - The currency code
208
- * @returns The currency info
209
- */
210
- export function getCurrencyInfo(currencyCode: string): CurrencyInfo | null {
211
- const currency = SUPPORTED_CURRENCIES[currencyCode.toUpperCase() as SupportedCurrencyCode]
212
- return currency ? currency.toJSON() : null
213
- }
214
-
215
- /**
216
- * Get the currency info for a contract ID
217
- * @param contractId - The contract ID
218
- * @returns The currency info
219
- */
220
- export function getCurrencyByContractId(contractId: string): Currency | null {
221
- if (!isHash(contractId)) {
222
- throw new Error('Invalid contract ID')
223
- }
224
- const currency = currencyById.get(contractId)
225
- return currency ? currency : null
226
- }
227
-
228
- /**
229
- * Format currency amount safely without Number() constructor, preserving decimal places
230
- * @param value - The value to format
231
- * @param decimals - The number of decimals
232
- * @returns The formatted currency amount
233
- */
234
- function formatCurrencyToFixedDecimals(value: string | number, decimals: number): string {
235
- const stringValue = value.toString()
236
- const [integerPart, fractionalPart = ''] = stringValue.split('.')
237
-
238
- if (decimals === 0) {
239
- return integerPart
240
- }
241
-
242
- // Pad or truncate fractional part to desired decimals
243
- const paddedFractional = fractionalPart.padEnd(decimals, '0').slice(0, decimals)
244
-
245
- // For currency formatting, always preserve trailing zeros
246
- return `${integerPart}.${paddedFractional}`
247
- }
248
-
249
- /**
250
- * Format a currency amount
251
- * @param amount - The amount to format
252
- * @param currencyCode - The currency code
253
- * @returns The formatted currency amount
254
- */
255
- export function formatCurrencyAmount(amount: string | number, currencyCode: string): string {
256
- const currencyInfo = getCurrencyInfo(currencyCode)
257
-
258
- if (!currencyInfo) {
259
- return `${amount} ${currencyCode}`
260
- }
261
-
262
- // Use currency-specific formatting that preserves decimal places
263
- const formatted = formatCurrencyToFixedDecimals(amount, currencyInfo.decimals)
264
-
265
- // Add thousands separators for better readability
266
- const formattedWithSeparators = addThousandsSeparator(formatted)
267
-
268
- return `${currencyInfo.symbol}${formattedWithSeparators}`
269
- }
270
-
271
- /**
272
- * Get all supported currencies
273
- * @returns All supported currencies
274
- */
275
- export function getAllSupportedCurrencies(): CurrencyInfo[] {
276
- return entries.map(([, currency]) => currency.toJSON())
277
- }
278
-
279
- /**
280
- * Check if a currency is supported
281
- * @param currencyCode - The currency code
282
- * @returns True if the currency is supported, false otherwise
283
- */
284
- export function isSupportedCurrency(currencyCode: string): boolean {
285
- return currencyCode.toUpperCase() in SUPPORTED_CURRENCIES
286
- }