@provex/utils 1.0.16 → 1.2.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.
- package/dist/src/chain.d.ts +436 -4
- package/dist/src/chain.js +80 -0
- package/dist/src/chain.test.d.ts +1 -0
- package/dist/src/chain.test.js +112 -0
- package/dist/src/contracts.d.ts +196 -20
- package/dist/src/contracts.js +245 -17
- package/dist/src/contracts.test.d.ts +1 -0
- package/dist/src/contracts.test.js +243 -0
- package/dist/src/conversionRates.d.ts +99 -8
- package/dist/src/conversionRates.js +74 -4
- package/dist/src/conversionRates.test.d.ts +1 -0
- package/dist/src/conversionRates.test.js +473 -0
- package/dist/src/currencies.d.ts +113 -9
- package/dist/src/currencies.js +119 -38
- package/dist/src/currencies.test.d.ts +1 -0
- package/dist/src/currencies.test.js +343 -0
- package/dist/src/data/countries.json +166 -166
- package/dist/src/deposit.d.ts +69 -1
- package/dist/src/deposit.js +52 -1
- package/dist/src/depositKeys.d.ts +154 -0
- package/dist/src/depositKeys.js +104 -0
- package/dist/src/depositKeys.test.d.ts +1 -0
- package/dist/src/depositKeys.test.js +231 -0
- package/dist/src/endpoints.d.ts +51 -0
- package/dist/src/endpoints.js +70 -0
- package/dist/src/endpoints.test.d.ts +1 -0
- package/dist/src/endpoints.test.js +54 -0
- package/dist/src/events.d.ts +40 -2
- package/dist/src/events.js +86 -10
- package/dist/src/events.test.d.ts +1 -0
- package/dist/src/events.test.js +111 -0
- package/dist/src/fees.d.ts +136 -0
- package/dist/src/fees.js +178 -0
- package/dist/src/fees.test.d.ts +1 -0
- package/dist/src/fees.test.js +165 -0
- package/dist/src/ids.d.ts +125 -22
- package/dist/src/ids.js +211 -61
- package/dist/src/ids.test.d.ts +1 -0
- package/dist/src/ids.test.js +607 -0
- package/dist/src/index.d.ts +30 -1
- package/dist/src/index.js +30 -1
- package/dist/src/input.d.ts +23 -4
- package/dist/src/input.js +23 -4
- package/dist/src/intent.d.ts +38 -2
- package/dist/src/intent.js +28 -1
- package/dist/src/intent.test.d.ts +1 -0
- package/dist/src/intent.test.js +119 -0
- package/dist/src/json.d.ts +38 -1
- package/dist/src/json.js +38 -2
- package/dist/src/normalization.d.ts +224 -0
- package/dist/src/normalization.js +119 -0
- package/dist/src/normalization.test.d.ts +1 -0
- package/dist/src/normalization.test.js +284 -0
- package/dist/src/participants.d.ts +19 -4
- package/dist/src/participants.js +19 -4
- package/dist/src/payment.d.ts +333 -16
- package/dist/src/payment.js +768 -58
- package/dist/src/payment.test.d.ts +1 -0
- package/dist/src/payment.test.js +272 -0
- package/dist/src/peer-types.d.ts +327 -0
- package/dist/src/peer-types.js +169 -0
- package/dist/src/profit.d.ts +26 -5
- package/dist/src/profit.js +31 -8
- package/dist/src/profit.test.d.ts +1 -0
- package/dist/src/profit.test.js +315 -0
- package/dist/src/reputation.d.ts +125 -0
- package/dist/src/reputation.js +188 -0
- package/dist/src/reputation.test.d.ts +1 -0
- package/dist/src/reputation.test.js +156 -0
- package/dist/src/sort.d.ts +49 -10
- package/dist/src/sort.js +40 -9
- package/dist/src/sort.test.d.ts +1 -0
- package/dist/src/sort.test.js +216 -0
- package/dist/src/stateKey.d.ts +35 -0
- package/dist/src/stateKey.js +36 -0
- package/dist/src/stats.d.ts +55 -1
- package/dist/src/stats.js +46 -1
- package/dist/src/stats.test.d.ts +1 -0
- package/dist/src/stats.test.js +104 -0
- package/dist/src/time.d.ts +59 -9
- package/dist/src/time.js +71 -15
- package/dist/src/time.test.d.ts +1 -0
- package/dist/src/time.test.js +231 -0
- package/dist/src/tokens.d.ts +11972 -145
- package/dist/src/tokens.js +98 -17
- package/dist/src/tokens.test.d.ts +1 -0
- package/dist/src/tokens.test.js +193 -0
- package/dist/src/units.d.ts +183 -0
- package/dist/src/units.js +206 -6
- package/dist/src/units.test.d.ts +1 -0
- package/dist/src/units.test.js +327 -0
- package/dist/src/zkp2p-types.d.ts +273 -4
- package/dist/src/zkp2p-types.js +169 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +116 -26
- 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/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
|
-
}
|