@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.
Files changed (122) hide show
  1. package/dist/src/chain.d.ts +436 -4
  2. package/dist/src/chain.js +80 -0
  3. package/dist/src/chain.test.d.ts +1 -0
  4. package/dist/src/chain.test.js +112 -0
  5. package/dist/src/contracts.d.ts +196 -20
  6. package/dist/src/contracts.js +245 -17
  7. package/dist/src/contracts.test.d.ts +1 -0
  8. package/dist/src/contracts.test.js +243 -0
  9. package/dist/src/conversionRates.d.ts +99 -8
  10. package/dist/src/conversionRates.js +74 -4
  11. package/dist/src/conversionRates.test.d.ts +1 -0
  12. package/dist/src/conversionRates.test.js +473 -0
  13. package/dist/src/currencies.d.ts +113 -9
  14. package/dist/src/currencies.js +119 -38
  15. package/dist/src/currencies.test.d.ts +1 -0
  16. package/dist/src/currencies.test.js +343 -0
  17. package/dist/src/data/countries.json +166 -166
  18. package/dist/src/deposit.d.ts +69 -1
  19. package/dist/src/deposit.js +52 -1
  20. package/dist/src/depositKeys.d.ts +154 -0
  21. package/dist/src/depositKeys.js +104 -0
  22. package/dist/src/depositKeys.test.d.ts +1 -0
  23. package/dist/src/depositKeys.test.js +231 -0
  24. package/dist/src/endpoints.d.ts +51 -0
  25. package/dist/src/endpoints.js +70 -0
  26. package/dist/src/endpoints.test.d.ts +1 -0
  27. package/dist/src/endpoints.test.js +54 -0
  28. package/dist/src/events.d.ts +40 -2
  29. package/dist/src/events.js +86 -10
  30. package/dist/src/events.test.d.ts +1 -0
  31. package/dist/src/events.test.js +111 -0
  32. package/dist/src/fees.d.ts +136 -0
  33. package/dist/src/fees.js +178 -0
  34. package/dist/src/fees.test.d.ts +1 -0
  35. package/dist/src/fees.test.js +165 -0
  36. package/dist/src/ids.d.ts +125 -22
  37. package/dist/src/ids.js +211 -61
  38. package/dist/src/ids.test.d.ts +1 -0
  39. package/dist/src/ids.test.js +607 -0
  40. package/dist/src/index.d.ts +30 -1
  41. package/dist/src/index.js +30 -1
  42. package/dist/src/input.d.ts +23 -4
  43. package/dist/src/input.js +23 -4
  44. package/dist/src/intent.d.ts +38 -2
  45. package/dist/src/intent.js +28 -1
  46. package/dist/src/intent.test.d.ts +1 -0
  47. package/dist/src/intent.test.js +119 -0
  48. package/dist/src/json.d.ts +38 -1
  49. package/dist/src/json.js +38 -2
  50. package/dist/src/normalization.d.ts +224 -0
  51. package/dist/src/normalization.js +119 -0
  52. package/dist/src/normalization.test.d.ts +1 -0
  53. package/dist/src/normalization.test.js +284 -0
  54. package/dist/src/participants.d.ts +19 -4
  55. package/dist/src/participants.js +19 -4
  56. package/dist/src/payment.d.ts +333 -16
  57. package/dist/src/payment.js +768 -58
  58. package/dist/src/payment.test.d.ts +1 -0
  59. package/dist/src/payment.test.js +272 -0
  60. package/dist/src/peer-types.d.ts +327 -0
  61. package/dist/src/peer-types.js +169 -0
  62. package/dist/src/profit.d.ts +26 -5
  63. package/dist/src/profit.js +31 -8
  64. package/dist/src/profit.test.d.ts +1 -0
  65. package/dist/src/profit.test.js +315 -0
  66. package/dist/src/reputation.d.ts +125 -0
  67. package/dist/src/reputation.js +188 -0
  68. package/dist/src/reputation.test.d.ts +1 -0
  69. package/dist/src/reputation.test.js +156 -0
  70. package/dist/src/sort.d.ts +49 -10
  71. package/dist/src/sort.js +40 -9
  72. package/dist/src/sort.test.d.ts +1 -0
  73. package/dist/src/sort.test.js +216 -0
  74. package/dist/src/stateKey.d.ts +35 -0
  75. package/dist/src/stateKey.js +36 -0
  76. package/dist/src/stats.d.ts +55 -1
  77. package/dist/src/stats.js +46 -1
  78. package/dist/src/stats.test.d.ts +1 -0
  79. package/dist/src/stats.test.js +104 -0
  80. package/dist/src/time.d.ts +59 -9
  81. package/dist/src/time.js +71 -15
  82. package/dist/src/time.test.d.ts +1 -0
  83. package/dist/src/time.test.js +231 -0
  84. package/dist/src/tokens.d.ts +11972 -145
  85. package/dist/src/tokens.js +98 -17
  86. package/dist/src/tokens.test.d.ts +1 -0
  87. package/dist/src/tokens.test.js +193 -0
  88. package/dist/src/units.d.ts +183 -0
  89. package/dist/src/units.js +206 -6
  90. package/dist/src/units.test.d.ts +1 -0
  91. package/dist/src/units.test.js +327 -0
  92. package/dist/src/zkp2p-types.d.ts +273 -4
  93. package/dist/src/zkp2p-types.js +169 -1
  94. package/dist/tsconfig.tsbuildinfo +1 -1
  95. package/package.json +116 -26
  96. package/scripts/convert-svg.ts +0 -22
  97. package/src/chain.ts +0 -27
  98. package/src/contracts.ts +0 -89
  99. package/src/conversionRates.ts +0 -356
  100. package/src/currencies.ts +0 -286
  101. package/src/data/countries.json +0 -1099
  102. package/src/deposit.ts +0 -2
  103. package/src/events.ts +0 -21
  104. package/src/flags/eur.svg +0 -4
  105. package/src/flags/usd.svg +0 -25
  106. package/src/flags.ts +0 -4
  107. package/src/global.d.ts +0 -9
  108. package/src/ids.ts +0 -137
  109. package/src/index.ts +0 -16
  110. package/src/input.ts +0 -35
  111. package/src/intent.ts +0 -15
  112. package/src/json.ts +0 -42
  113. package/src/participants.ts +0 -14
  114. package/src/payment.ts +0 -573
  115. package/src/profit.ts +0 -55
  116. package/src/sort.ts +0 -49
  117. package/src/stats.ts +0 -10
  118. package/src/time.ts +0 -123
  119. package/src/tokens.ts +0 -159
  120. package/src/units.ts +0 -120
  121. package/src/zkp2p-types.ts +0 -55
  122. package/tsconfig.json +0 -18
package/src/deposit.ts DELETED
@@ -1,2 +0,0 @@
1
-
2
- export type StatusMutatingAction = "deposit" | "withdrawal" | "closed" | "exchange"
package/src/events.ts DELETED
@@ -1,21 +0,0 @@
1
- export const eventOrder = [
2
- // deposit open events
3
- 'deposit',
4
- 'verifier_added',
5
- 'currency_added',
6
- // configuration events
7
- 'rate_update',
8
- // intent events
9
- 'intent',
10
- 'exchange',
11
- 'pruned',
12
- // deposit close events
13
- 'withdrawal',
14
- 'closed',
15
- ] as const
16
-
17
- export type EventType = (typeof eventOrder)[number]
18
-
19
- export const eventOrderId = new Map<EventType, number>(
20
- eventOrder.map((event, index) => [event, index] as [EventType, number])
21
- )
package/src/flags/eur.svg DELETED
@@ -1,4 +0,0 @@
1
- <?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="810" height="540"><desc>European flag</desc>
2
- <defs><g id="s"><g id="c"><path id="t" d="M0,0v1h0.5z" transform="translate(0,-1)rotate(18)"/><use xlink:href="#t" transform="scale(-1,1)"/></g><g id="a"><use xlink:href="#c" transform="rotate(72)"/><use xlink:href="#c" transform="rotate(144)"/></g><use xlink:href="#a" transform="scale(-1,1)"/></g></defs>
3
- <rect fill="#039" width="810" height="540"/><g fill="#fc0" transform="scale(30)translate(13.5,9)"><use xlink:href="#s" y="-6"/><use xlink:href="#s" y="6"/><g id="l"><use xlink:href="#s" x="-6"/><use xlink:href="#s" transform="rotate(150)translate(0,6)rotate(66)"/><use xlink:href="#s" transform="rotate(120)translate(0,6)rotate(24)"/><use xlink:href="#s" transform="rotate(60)translate(0,6)rotate(12)"/><use xlink:href="#s" transform="rotate(30)translate(0,6)rotate(42)"/></g><use xlink:href="#l" transform="scale(-1,1)"/></g>
4
- </svg>
package/src/flags/usd.svg DELETED
@@ -1,25 +0,0 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1235" height="650" viewBox="0 0 7410 3900">
2
- <path d="M0,0h7410v3900H0" fill="#b31942"/>
3
- <path d="M0,450H7410m0,600H0m0,600H7410m0,600H0m0,600H7410m0,600H0" stroke="#FFF" stroke-width="300"/>
4
- <path d="M0,0h2964v2100H0" fill="#0a3161"/>
5
- <g fill="#FFF">
6
- <g id="s18">
7
- <g id="s9">
8
- <g id="s5">
9
- <g id="s4">
10
- <path id="s" d="M247,90 317.534230,307.082039 132.873218,172.917961H361.126782L176.465770,307.082039z"/>
11
- <use xlink:href="#s" y="420"/>
12
- <use xlink:href="#s" y="840"/>
13
- <use xlink:href="#s" y="1260"/>
14
- </g>
15
- <use xlink:href="#s" y="1680"/>
16
- </g>
17
- <use xlink:href="#s4" x="247" y="210"/>
18
- </g>
19
- <use xlink:href="#s9" x="494"/>
20
- </g>
21
- <use xlink:href="#s18" x="988"/>
22
- <use xlink:href="#s9" x="1976"/>
23
- <use xlink:href="#s5" x="2470"/>
24
- </g>
25
- </svg>
package/src/flags.ts DELETED
@@ -1,4 +0,0 @@
1
- // Auto-generated from SVG files
2
-
3
- export const USD_FLAG_SVG = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1235" height="650" viewBox="0 0 7410 3900"> <path d="M0,0h7410v3900H0" fill="#b31942"/> <path d="M0,450H7410m0,600H0m0,600H7410m0,600H0m0,600H7410m0,600H0" stroke="#FFF" stroke-width="300"/> <path d="M0,0h2964v2100H0" fill="#0a3161"/> <g fill="#FFF"> <g id="s18"> <g id="s9"> <g id="s5"> <g id="s4"> <path id="s" d="M247,90 317.534230,307.082039 132.873218,172.917961H361.126782L176.465770,307.082039z"/> <use xlink:href="#s" y="420"/> <use xlink:href="#s" y="840"/> <use xlink:href="#s" y="1260"/> </g> <use xlink:href="#s" y="1680"/> </g> <use xlink:href="#s4" x="247" y="210"/> </g> <use xlink:href="#s9" x="494"/> </g> <use xlink:href="#s18" x="988"/> <use xlink:href="#s9" x="1976"/> <use xlink:href="#s5" x="2470"/> </g> </svg>';
4
- export const EUR_FLAG_SVG = '<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="810" height="540"><desc>European flag</desc> <defs><g id="s"><g id="c"><path id="t" d="M0,0v1h0.5z" transform="translate(0,-1)rotate(18)"/><use xlink:href="#t" transform="scale(-1,1)"/></g><g id="a"><use xlink:href="#c" transform="rotate(72)"/><use xlink:href="#c" transform="rotate(144)"/></g><use xlink:href="#a" transform="scale(-1,1)"/></g></defs> <rect fill="#039" width="810" height="540"/><g fill="#fc0" transform="scale(30)translate(13.5,9)"><use xlink:href="#s" y="-6"/><use xlink:href="#s" y="6"/><g id="l"><use xlink:href="#s" x="-6"/><use xlink:href="#s" transform="rotate(150)translate(0,6)rotate(66)"/><use xlink:href="#s" transform="rotate(120)translate(0,6)rotate(24)"/><use xlink:href="#s" transform="rotate(60)translate(0,6)rotate(12)"/><use xlink:href="#s" transform="rotate(30)translate(0,6)rotate(42)"/></g><use xlink:href="#l" transform="scale(-1,1)"/></g> </svg>';
package/src/global.d.ts DELETED
@@ -1,9 +0,0 @@
1
- declare module "*.svg" {
2
- const content: string;
3
- export default content;
4
- }
5
-
6
- declare module "*.json" {
7
- const content: any;
8
- export default content;
9
- }
package/src/ids.ts DELETED
@@ -1,137 +0,0 @@
1
- import { concatHex, keccak256, numberToHex, stringToHex, type Hex } from "viem"
2
- import { eventOrderId, type EventType } from "./events.js"
3
- import { interval, type TimeResolutionKey } from "./time.js"
4
- import type { StatAction } from "./stats.js"
5
-
6
- export type DepositVerifierInputs = {
7
- verifier: Hex,
8
- depositId: Hex,
9
- }
10
-
11
- export type DepositCurrencyInputs = DepositVerifierInputs & {
12
- currency: Hex,
13
- }
14
-
15
- export type ConversionRateUpdatedInputs = DepositCurrencyInputs & {
16
- changeId: number
17
- }
18
-
19
- export type ProviderHashInputs = { chainId: bigint | number; hash: Hex }
20
-
21
- export type OnChainHashInputs = { chainId: bigint | number; hash: Hex }
22
-
23
- export type OnChainAddressInputs = { chainId: bigint | number; address: Hex }
24
-
25
- export const onChainHash = ({ chainId, hash }: OnChainHashInputs) => (
26
- keccak256(concatHex([
27
- numberToHex(chainId, { size: 8 }),
28
- hash,
29
- ]))
30
- )
31
-
32
- export const onChainAddress = ({ chainId, address }: OnChainAddressInputs) => (
33
- onChainHash({ chainId, hash: address })
34
- )
35
-
36
- export const ids = {
37
- block: onChainHash,
38
- transaction: onChainHash,
39
- participant: onChainAddress,
40
- providerHash: onChainHash,
41
- verifier: onChainAddress,
42
- depositVerifierAdded: ({ depositId, verifier }: DepositVerifierInputs) => {
43
- return keccak256(concatHex([depositId, verifier]))
44
- },
45
- depositCurrencyAdded: ({ depositId, currency, verifier }: DepositCurrencyInputs) => {
46
- return keccak256(concatHex([
47
- ids.depositVerifierAdded({ verifier, depositId }),
48
- currency,
49
- ]))
50
- },
51
- depositConversionRateUpdated: ({ currency, verifier, depositId, changeId }: ConversionRateUpdatedInputs) => {
52
- return concatHex([
53
- ids.depositCurrencyAdded({ currency, verifier, depositId }),
54
- numberToHex(changeId, { size: 4 }),
55
- ])
56
- },
57
- };
58
-
59
- export type OrderIdInputs = {
60
- timestamp: bigint
61
- chainId: bigint
62
- }
63
-
64
- export type OrderIdTransactionInputs = OrderIdInputs & {
65
- transactionIndex: bigint
66
- }
67
-
68
- export type OrderIdLogInputs = OrderIdTransactionInputs & {
69
- logIndex: bigint
70
- }
71
-
72
- export type OrderIdOrderInputs = OrderIdLogInputs & { name: EventType }
73
-
74
- export type DepositIdInputs = { chainId: bigint | number; depositId: bigint | number }
75
-
76
- export type LogStatsIdInputs = {
77
- timestamp: bigint,
78
- type: TimeResolutionKey,
79
- action: StatAction,
80
- currency: Hex | null,
81
- token: Hex,
82
- verifier: Hex,
83
- chainId: bigint | number,
84
- }
85
-
86
- const hourFactor = BigInt(interval.hour) / BigInt(interval.second)
87
- const dayFactor = BigInt(interval.day) / BigInt(interval.second)
88
- const monthFactor = BigInt(interval.month) / BigInt(interval.second)
89
-
90
- export const orderId = {
91
- deposit: ({ chainId, depositId }: DepositIdInputs) => concatHex([
92
- numberToHex(depositId, { size: 8 }),
93
- numberToHex(chainId, { size: 4 }),
94
- ]),
95
- block: ({ timestamp, chainId }: OrderIdInputs) => concatHex([
96
- numberToHex(timestamp, { size: 8 }),
97
- numberToHex(chainId, { size: 4 }),
98
- ]),
99
- transaction: ({ timestamp, chainId, transactionIndex }: OrderIdTransactionInputs) => concatHex([
100
- numberToHex(timestamp, { size: 8 }),
101
- numberToHex(transactionIndex, { size: 4 }),
102
- numberToHex(chainId, { size: 4 }),
103
- ]),
104
- log: ({ timestamp, chainId, transactionIndex, logIndex }: OrderIdLogInputs) => concatHex([
105
- numberToHex(timestamp, { size: 8 }),
106
- numberToHex(transactionIndex, { size: 4 }),
107
- numberToHex(logIndex, { size: 4 }),
108
- numberToHex(chainId, { size: 4 }),
109
- ]),
110
- order: ({ timestamp, chainId, transactionIndex, logIndex, name }: OrderIdOrderInputs) => concatHex([
111
- numberToHex(timestamp, { size: 8 }),
112
- numberToHex(transactionIndex!, { size: 4 }),
113
- numberToHex(eventOrderId.get(name)!, { size: 1 }),
114
- numberToHex(logIndex, { size: 4 }),
115
- numberToHex(chainId, { size: 4 }),
116
- ]),
117
- stat: ({ timestamp, type, action, currency, verifier, token, chainId }: LogStatsIdInputs) => {
118
- const hour = (timestamp / hourFactor) * hourFactor
119
- const day = (timestamp / dayFactor) * dayFactor
120
- const month = (timestamp / monthFactor) * monthFactor
121
- let time = timestamp
122
- if (type === "hour") {
123
- time = hour
124
- }
125
- if (type === "day") {
126
- time = day
127
- }
128
- if (type === "month") {
129
- time = month
130
- }
131
- return concatHex([
132
- numberToHex(time, { size: 8 }),
133
- `0x${keccak256(concatHex([stringToHex(type), stringToHex(action), token, currency ?? '0x', verifier])).slice(26)}`,
134
- numberToHex(chainId, { size: 4 }),
135
- ])
136
- },
137
- };
package/src/index.ts DELETED
@@ -1,16 +0,0 @@
1
- export * from './currencies.js'
2
- export * from './units.js'
3
- export * from './contracts.js'
4
- export * from './participants.js'
5
- export * from './chain.js'
6
- export * from './intent.js'
7
- export * from './conversionRates.js'
8
- export * from './payment.js'
9
- export * from './sort.js'
10
- export * from './time.js'
11
- export * from './tokens.js'
12
- export * from './stats.js'
13
- export * from './deposit.js'
14
- export * from './ids.js'
15
- export * from './events.js'
16
- export * from './zkp2p-types.js'
package/src/input.ts DELETED
@@ -1,35 +0,0 @@
1
-
2
- /**
3
- * Adjust the lowest decimal place directly on the string, preserving all trailing zeros
4
- * @param inputVal - The input value
5
- * @param direction - The direction to adjust the decimal place
6
- * @returns The adjusted value
7
- */
8
- export const adjustLowestDecimal = (inputVal: string, direction: 1 | -1) => {
9
- let [whole, decimal = ''] = inputVal.split('.')
10
- // truncate the decimal to 18 decimals
11
- decimal = decimal.slice(0, 18)
12
- // If no decimals, treat as zero decimals
13
- const originalDecimals = decimal.length
14
- // Pad to 18 decimals for calculation
15
- const paddedDecimals = (decimal + '0'.repeat(18)).slice(0, 18)
16
- // Compose the full number as a string
17
- const fullNumberStr = whole + paddedDecimals
18
- let value = BigInt(fullNumberStr)
19
- // Determine which decimal place to operate on (last entered, or 18th if maxed)
20
- // idx will be -1 if it should be incrementing the whole number
21
- const idx = originalDecimals - 1
22
- // The amount to increment/decrement at the correct decimal place
23
- const delta = 10n ** BigInt(17 - idx)
24
- if (direction === 1) {
25
- value += delta
26
- } else if (value >= delta) {
27
- value -= delta
28
- }
29
- // Convert back to string, pad with zeros if needed
30
- let valueStr = value.toString().padStart(whole.length + 18, '0')
31
- let newWhole = BigInt(valueStr.slice(0, valueStr.length - 18) || '0').toString()
32
- let newDecimal = valueStr.slice(-18, -18 + originalDecimals)
33
- // If originalDecimals was 0, don't add a decimal point
34
- return originalDecimals > 0 ? `${newWhole}.${newDecimal}` : newWhole
35
- }
package/src/intent.ts DELETED
@@ -1,15 +0,0 @@
1
- import type { Hex } from "viem"
2
-
3
- /**
4
- * Structure for an intent
5
- */
6
- export interface IntentStruct {
7
- owner: Hex
8
- to: Hex
9
- depositId: bigint
10
- amount: bigint
11
- timestamp: bigint
12
- paymentVerifier: Hex
13
- fiatCurrency: Hex
14
- conversionRate: bigint
15
- }
package/src/json.ts DELETED
@@ -1,42 +0,0 @@
1
- /**
2
- * JSON serializer
3
- */
4
- export const jsonSerializer = {
5
- /**
6
- * Parse a JSON string
7
- * @param str - The JSON string to parse
8
- * @returns The parsed object
9
- */
10
- parse: JSON.parse,
11
- /**
12
- * Stringify an object
13
- * @param obj - The object to stringify
14
- * @returns The stringified object
15
- */
16
- stringify: (obj: unknown) => JSON.stringify(obj, (_key, value) => {
17
- if (typeof value === 'bigint') {
18
- return value.toString()
19
- }
20
- return value
21
- })
22
- }
23
- // Recursively convert specified fields to BigInt in an object or array
24
- export function mapBigIntFields<T>(obj: T, fields: string[]): T {
25
- if (Array.isArray(obj)) {
26
- return obj.map(item => mapBigIntFields(item, fields)) as any;
27
- }
28
- if (obj && typeof obj === 'object') {
29
- const result: any = {};
30
- for (const [key, value] of Object.entries(obj)) {
31
- if (fields.includes(key) && typeof value === 'string' && value.match(/^-?\d+$/)) {
32
- result[key] = BigInt(value);
33
- } else if (Array.isArray(value) || (value && typeof value === 'object')) {
34
- result[key] = mapBigIntFields(value, fields);
35
- } else {
36
- result[key] = value;
37
- }
38
- }
39
- return result;
40
- }
41
- return obj;
42
- }
@@ -1,14 +0,0 @@
1
- import { concatHex, keccak256, numberToHex, type Hex } from 'viem'
2
-
3
- /**
4
- * Generate a participantId from chainId and address using the same logic as the indexer
5
- * @param chainId - The chain ID
6
- * @param address - The address
7
- * @returns The participant ID
8
- */
9
- export function generateParticipantId(chainId: number | bigint, address: string): string {
10
- return keccak256(concatHex([
11
- numberToHex(chainId, { size: 8 }),
12
- address as Hex
13
- ]))
14
- }