@provex/utils 1.0.16

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 (75) hide show
  1. package/README.md +287 -0
  2. package/dist/scripts/convert-svg.d.ts +1 -0
  3. package/dist/scripts/convert-svg.js +17 -0
  4. package/dist/src/chain.d.ts +14 -0
  5. package/dist/src/chain.js +18 -0
  6. package/dist/src/contracts.d.ts +30 -0
  7. package/dist/src/contracts.js +64 -0
  8. package/dist/src/conversionRates.d.ts +124 -0
  9. package/dist/src/conversionRates.js +219 -0
  10. package/dist/src/currencies.d.ts +144 -0
  11. package/dist/src/currencies.js +234 -0
  12. package/dist/src/data/countries.json +1099 -0
  13. package/dist/src/deposit.d.ts +1 -0
  14. package/dist/src/deposit.js +1 -0
  15. package/dist/src/events.d.ts +3 -0
  16. package/dist/src/events.js +16 -0
  17. package/dist/src/flags.d.ts +2 -0
  18. package/dist/src/flags.js +3 -0
  19. package/dist/src/ids.d.ts +72 -0
  20. package/dist/src/ids.js +81 -0
  21. package/dist/src/index.d.ts +16 -0
  22. package/dist/src/index.js +16 -0
  23. package/dist/src/input.d.ts +7 -0
  24. package/dist/src/input.js +35 -0
  25. package/dist/src/intent.d.ts +14 -0
  26. package/dist/src/intent.js +1 -0
  27. package/dist/src/json.d.ts +18 -0
  28. package/dist/src/json.js +44 -0
  29. package/dist/src/participants.d.ts +7 -0
  30. package/dist/src/participants.js +13 -0
  31. package/dist/src/payment.d.ts +164 -0
  32. package/dist/src/payment.js +439 -0
  33. package/dist/src/profit.d.ts +28 -0
  34. package/dist/src/profit.js +34 -0
  35. package/dist/src/sort.d.ts +36 -0
  36. package/dist/src/sort.js +39 -0
  37. package/dist/src/stats.d.ts +7 -0
  38. package/dist/src/stats.js +8 -0
  39. package/dist/src/time.d.ts +54 -0
  40. package/dist/src/time.js +106 -0
  41. package/dist/src/tokens.d.ts +3675 -0
  42. package/dist/src/tokens.js +134 -0
  43. package/dist/src/units.d.ts +17 -0
  44. package/dist/src/units.js +79 -0
  45. package/dist/src/zkp2p-types.d.ts +48 -0
  46. package/dist/src/zkp2p-types.js +1 -0
  47. package/dist/tsconfig.tsbuildinfo +1 -0
  48. package/package.json +45 -0
  49. package/scripts/convert-svg.ts +22 -0
  50. package/src/chain.ts +27 -0
  51. package/src/contracts.ts +89 -0
  52. package/src/conversionRates.ts +356 -0
  53. package/src/currencies.ts +286 -0
  54. package/src/data/countries.json +1099 -0
  55. package/src/deposit.ts +2 -0
  56. package/src/events.ts +21 -0
  57. package/src/flags/eur.svg +4 -0
  58. package/src/flags/usd.svg +25 -0
  59. package/src/flags.ts +4 -0
  60. package/src/global.d.ts +9 -0
  61. package/src/ids.ts +137 -0
  62. package/src/index.ts +16 -0
  63. package/src/input.ts +35 -0
  64. package/src/intent.ts +15 -0
  65. package/src/json.ts +42 -0
  66. package/src/participants.ts +14 -0
  67. package/src/payment.ts +573 -0
  68. package/src/profit.ts +55 -0
  69. package/src/sort.ts +49 -0
  70. package/src/stats.ts +10 -0
  71. package/src/time.ts +123 -0
  72. package/src/tokens.ts +159 -0
  73. package/src/units.ts +120 -0
  74. package/src/zkp2p-types.ts +55 -0
  75. package/tsconfig.json +18 -0
@@ -0,0 +1,134 @@
1
+ import { createPublicClient, getAddress, http } from 'viem';
2
+ import { base, pulsechain } from 'viem/chains';
3
+ import { chains } from './chain.js';
4
+ const BASE_USDC = {
5
+ address: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
6
+ decimals: 6,
7
+ symbol: 'USDC',
8
+ name: 'USD Coin',
9
+ chainId: base.id,
10
+ };
11
+ export const PULSECHAIN_USDC = {
12
+ address: '0x15D38573d2feeb82e7ad5187aB8c1D52810B1f07',
13
+ decimals: 6,
14
+ symbol: 'USDC',
15
+ name: 'USD Coin from Ethereum',
16
+ chainId: pulsechain.id,
17
+ mainnetAddress: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
18
+ };
19
+ /** All tokens info */
20
+ export const allTokensInfo = {
21
+ [base.id]: {
22
+ // Base Network Tokens
23
+ USDC: BASE_USDC,
24
+ // USDT: {
25
+ // address: '0xfde4C96c8593536E31F229EA8f37b2ADa2699bb2',
26
+ // decimals: 6,
27
+ // symbol: 'USDT',
28
+ // name: 'Tether USD',
29
+ // chainId: base.id,
30
+ // } as TokenInfo,
31
+ // ETH: {
32
+ // address: '0x4200000000000000000000000000000000000006',
33
+ // decimals: 18,
34
+ // symbol: 'WETH',
35
+ // name: 'Wrapped Ether',
36
+ // chainId: base.id,
37
+ // } as TokenInfo,
38
+ },
39
+ // [mainnet.id]: {
40
+ // // Ethereum Mainnet Tokens
41
+ // USDC: {
42
+ // address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
43
+ // decimals: 6,
44
+ // symbol: 'USDC',
45
+ // name: 'USD Coin',
46
+ // chainId: mainnet.id,
47
+ // } as TokenInfo,
48
+ // USDT: {
49
+ // address: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
50
+ // decimals: 6,
51
+ // symbol: 'USDT',
52
+ // name: 'Tether USD',
53
+ // chainId: mainnet.id,
54
+ // } as TokenInfo,
55
+ // WETH: {
56
+ // address: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
57
+ // decimals: 18,
58
+ // symbol: 'WETH',
59
+ // name: 'Wrapped Ether',
60
+ // chainId: mainnet.id,
61
+ // } as TokenInfo,
62
+ // ETH: {
63
+ // address: zeroAddress,
64
+ // decimals: 18,
65
+ // symbol: 'ETH',
66
+ // name: 'Ether',
67
+ // chainId: mainnet.id,
68
+ // } as TokenInfo,
69
+ // DAI: {
70
+ // address: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
71
+ // decimals: 18,
72
+ // symbol: 'DAI',
73
+ // name: 'Dai Stablecoin',
74
+ // chainId: mainnet.id,
75
+ // } as TokenInfo,
76
+ // },
77
+ [pulsechain.id]: {
78
+ USDC: PULSECHAIN_USDC,
79
+ },
80
+ // USDT: {
81
+ // address: '0x0Cb6F5a34ad42ec934882A05265A7d5F59b51A2f',
82
+ // decimals: 6,
83
+ // symbol: 'USDT',
84
+ // name: 'Tether USD from Ethereum',
85
+ // chainId: pulsechain.id,
86
+ // mainnetAddress: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
87
+ // } as TokenInfo,
88
+ // ETH: {
89
+ // address: '0x02DcdD04e3F455D838cd1249292C58f3B79e3C3C',
90
+ // decimals: 18,
91
+ // symbol: 'WETH',
92
+ // name: 'Wrapped Ether from Ethereum',
93
+ // chainId: pulsechain.id,
94
+ // mainnetAddress: '0x0000000000000000000000000000000000000000',
95
+ // } as TokenInfo,
96
+ };
97
+ export const getDefaultToken = (chainId) => {
98
+ return allTokensInfo[chainId ?? pulsechain.id]?.USDC ?? null;
99
+ };
100
+ export const getDefaultTokens = (chainIds) => {
101
+ return chainIds.length ? chainIds.map((chainId) => getDefaultToken(chainId)) : chains.map((chain) => getDefaultToken(chain.id));
102
+ };
103
+ /**
104
+ * Get tokens by chain ID
105
+ * @param chainId - The chain ID
106
+ * @returns Array of tokens for that chain
107
+ */
108
+ export const getTokensByChainId = (chainId) => {
109
+ return Object.values(allTokensInfo[chainId] ?? {});
110
+ };
111
+ /**
112
+ * Get a token by address
113
+ * @param address - The address of the token
114
+ * @returns The token info
115
+ */
116
+ export const tokenByAddress = (chainId, address) => {
117
+ return !address ? null : Object.values(allTokensInfo[chainId] ?? {}).find(token => getAddress(token.address) === getAddress(address)) ?? null;
118
+ };
119
+ /**
120
+ * Create a public client for reading from contracts
121
+ * @param chain - The chain
122
+ * @returns The public client
123
+ */
124
+ export const publicClient = (chain) => createPublicClient({
125
+ chain,
126
+ transport: http()
127
+ });
128
+ // export const pulseChainTokenToMainnet = (pulsechainToken: TokenInfo) => {
129
+ // const tokens = allTokensInfo[mainnet.id]
130
+ // const token = Object.values(tokens).find((t) => (
131
+ // getAddress(t.address) === getAddress(pulsechainToken.mainnetAddress!)
132
+ // ))
133
+ // return token!
134
+ // }
@@ -0,0 +1,17 @@
1
+ import type { TokenInfo } from './tokens.js';
2
+ export declare const oneEther: bigint;
3
+ export declare function addThousandsSeparator(numberString: string, delimiter?: string): string;
4
+ export interface FormatRateDisplayOptions {
5
+ decimals: number;
6
+ truncateDecimals?: number;
7
+ padDecimals?: number;
8
+ cutoff?: bigint;
9
+ delimiter?: string;
10
+ roundUp?: boolean;
11
+ }
12
+ export declare const parseUnits: (amount: string, options: Pick<FormatRateDisplayOptions, "decimals" | "delimiter">) => bigint;
13
+ export declare function formatUnits(amount: bigint, options: FormatRateDisplayOptions): string;
14
+ export declare function formatTokenAmountForDisplay(amount: bigint, options: FormatRateDisplayOptions): string;
15
+ export declare function parseIntegerSafe(value: string): number | null;
16
+ export declare function timestampToDate(timestamp: string | number | bigint | undefined | null): Date | null;
17
+ export declare const formatAmount: (token: TokenInfo, amount: bigint | null, options?: Partial<FormatRateDisplayOptions>) => string | null;
@@ -0,0 +1,79 @@
1
+ import { formatUnits as viemFormatUnits, parseUnits as viemParseUnits } from 'viem';
2
+ export const oneEther = 10n ** 18n;
3
+ // Add thousands separators to a number string
4
+ export function addThousandsSeparator(numberString, delimiter = ',') {
5
+ let [integerPart, fractionalPart] = numberString.split('.');
6
+ integerPart = integerPart || '0';
7
+ fractionalPart = fractionalPart || '';
8
+ // Add thousands separators to integer part
9
+ const formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, delimiter);
10
+ // Return with fractional part if it exists
11
+ return fractionalPart ? `${formattedInteger}.${fractionalPart}` : formattedInteger;
12
+ }
13
+ export const parseUnits = (amount, options) => {
14
+ return viemParseUnits(amount.split(options.delimiter ?? ',').join(''), options.decimals);
15
+ };
16
+ // Format exchange rates with natural precision, minimum 2 decimal places and thousands separators
17
+ export function formatUnits(amount, options) {
18
+ const { decimals, truncateDecimals, padDecimals = 0, delimiter = ',', cutoff, roundUp = false } = options;
19
+ if (cutoff && amount < cutoff) {
20
+ return `<${formatUnits(cutoff, { ...options, truncateDecimals: 0, cutoff: undefined })}`;
21
+ }
22
+ if (roundUp && truncateDecimals !== undefined) {
23
+ // const minimumDecimal = decimals - truncateDecimals
24
+ const minimumDecimal = BigInt(decimals - truncateDecimals);
25
+ const minimalMod = amount % (10n ** minimumDecimal);
26
+ const halfMinimalDecimal = (10n ** minimumDecimal) / 2n;
27
+ if (minimalMod >= halfMinimalDecimal) {
28
+ amount = amount + (10n ** minimumDecimal) - minimalMod;
29
+ }
30
+ }
31
+ const formatted = viemFormatUnits(amount, decimals);
32
+ const [integerPart, fractionalPart = ''] = formatted.split('.');
33
+ // If truncateDecimals is specified, limit decimal places
34
+ let finalFractionalPart = fractionalPart;
35
+ if (truncateDecimals !== undefined) {
36
+ finalFractionalPart = fractionalPart.slice(0, truncateDecimals);
37
+ }
38
+ // Always pad to at least 2 decimal places (or padDecimals)
39
+ if (finalFractionalPart.length < padDecimals) {
40
+ finalFractionalPart = finalFractionalPart.padEnd(padDecimals, '0');
41
+ }
42
+ // If there are no decimals, still show .00
43
+ const numberString = `${integerPart}.${finalFractionalPart}`;
44
+ return addThousandsSeparator(numberString, delimiter);
45
+ }
46
+ export function formatTokenAmountForDisplay(amount, options) {
47
+ return formatUnits(amount, { truncateDecimals: 2, ...options });
48
+ }
49
+ // Safe integer parsing without using Number()
50
+ export function parseIntegerSafe(value) {
51
+ // Use parseInt instead of Number() for integer parsing
52
+ const parsed = parseInt(value, 10);
53
+ return isNaN(parsed) ? null : parsed;
54
+ }
55
+ // // Get display title for limits (shows original user limits on hover)
56
+ // export function getLimitsDisplayTitle(deposit: { formattedMinAmount: string; formattedMaxAmount: string }): string {
57
+ // return `User set: ${deposit.formattedMinAmount} - ${deposit.formattedMaxAmount}`
58
+ // }
59
+ // Convert blockchain timestamp (seconds) to Date object
60
+ export function timestampToDate(timestamp) {
61
+ if (!timestamp)
62
+ return null;
63
+ // Convert seconds to milliseconds for Date constructor
64
+ const timestampMs = Number(timestamp) * 1000;
65
+ return new Date(timestampMs);
66
+ }
67
+ // export function formatTokenAmountForDisplay(amount: bigint, tokenInfo: TokenInfo): string {
68
+ // return formatUnits(amount, { decimals: tokenInfo.decimals })
69
+ // }
70
+ export const formatAmount = (token, amount, options = {}) => {
71
+ if (!amount)
72
+ return null;
73
+ return formatUnits(amount, {
74
+ decimals: token.decimals,
75
+ truncateDecimals: Math.min(token.decimals - 4, 8),
76
+ padDecimals: 2,
77
+ ...options,
78
+ });
79
+ };
@@ -0,0 +1,48 @@
1
+ import type { Hex } from "viem";
2
+ import type { ProviderKey, ValidationDepositData } from "./payment.js";
3
+ export type ValidationOptions = {
4
+ providerKey: ProviderKey;
5
+ telegramHandle: string;
6
+ providerId: string;
7
+ };
8
+ export type Zkp2pResponse<T> = {
9
+ success: boolean;
10
+ message: string;
11
+ statusCode: number;
12
+ responseObject: T;
13
+ };
14
+ export type CreateResponse = {
15
+ id: number;
16
+ processorName: string;
17
+ depositData: string;
18
+ hashedOnchainId: Hex;
19
+ createdAt: string;
20
+ };
21
+ export type PayeeDetails = {
22
+ id: number;
23
+ processorName: string;
24
+ createdAt: string;
25
+ hashedOnchainId: Hex;
26
+ depositData: ValidationDepositData;
27
+ };
28
+ export type IntentValidationOptions = {
29
+ processorName: ProviderKey;
30
+ depositId: string;
31
+ tokenAmount: string;
32
+ payeeDetails: Hex;
33
+ toAddress: Hex;
34
+ fiatCurrencyCode: Hex;
35
+ chainId: string;
36
+ };
37
+ export type IntentValidation = {
38
+ signedIntent: Hex;
39
+ intentData: {
40
+ depositId: string;
41
+ tokenAmount: string;
42
+ recipientAddress: Hex;
43
+ verifierAddress: Hex;
44
+ currencyCodeHash: Hex;
45
+ gatingServiceSignature: Hex;
46
+ };
47
+ depositData: ValidationDepositData;
48
+ };
@@ -0,0 +1 @@
1
+ export {};