@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
@@ -0,0 +1,607 @@
1
+ import { describe, it } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { onChainHash, onChainAddress, ids, orderId, chunkHex } from './ids.js';
4
+ describe('ids', () => {
5
+ const TEST_CHAIN_ID = 8453n; // Base
6
+ const TEST_HASH = '0x1234567890123456789012345678901234567890123456789012345678901234';
7
+ const TEST_ADDRESS = '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb';
8
+ describe('onChainHash', () => {
9
+ it('should generate consistent hashes', () => {
10
+ const hash1 = onChainHash({ chainId: TEST_CHAIN_ID, hash: TEST_HASH });
11
+ const hash2 = onChainHash({ chainId: TEST_CHAIN_ID, hash: TEST_HASH });
12
+ assert.equal(hash1, hash2);
13
+ });
14
+ it('should generate 32-byte hashes', () => {
15
+ const hash = onChainHash({ chainId: TEST_CHAIN_ID, hash: TEST_HASH });
16
+ assert.equal(hash.length, 66); // '0x' + 64 hex chars
17
+ assert.ok(hash.startsWith('0x'));
18
+ });
19
+ it('should generate different hashes for different chain IDs', () => {
20
+ const hash1 = onChainHash({ chainId: 8453n, hash: TEST_HASH });
21
+ const hash2 = onChainHash({ chainId: 1n, hash: TEST_HASH });
22
+ assert.notEqual(hash1, hash2);
23
+ });
24
+ it('should generate different hashes for different hashes', () => {
25
+ const hash1 = '0x1111111111111111111111111111111111111111111111111111111111111111';
26
+ const hash2 = '0x2222222222222222222222222222222222222222222222222222222222222222';
27
+ const result1 = onChainHash({ chainId: TEST_CHAIN_ID, hash: hash1 });
28
+ const result2 = onChainHash({ chainId: TEST_CHAIN_ID, hash: hash2 });
29
+ assert.notEqual(result1, result2);
30
+ });
31
+ it('should handle numeric chain IDs', () => {
32
+ const hashBigInt = onChainHash({ chainId: 8453n, hash: TEST_HASH });
33
+ const hashNumber = onChainHash({ chainId: 8453, hash: TEST_HASH });
34
+ assert.equal(hashBigInt, hashNumber);
35
+ });
36
+ });
37
+ describe('onChainAddress', () => {
38
+ it('should generate consistent hashes', () => {
39
+ const hash1 = onChainAddress({ chainId: TEST_CHAIN_ID, address: TEST_ADDRESS });
40
+ const hash2 = onChainAddress({ chainId: TEST_CHAIN_ID, address: TEST_ADDRESS });
41
+ assert.equal(hash1, hash2);
42
+ });
43
+ it('should generate 32-byte hashes', () => {
44
+ const hash = onChainAddress({ chainId: TEST_CHAIN_ID, address: TEST_ADDRESS });
45
+ assert.equal(hash.length, 66);
46
+ assert.ok(hash.startsWith('0x'));
47
+ });
48
+ it('should generate different hashes for different addresses', () => {
49
+ const addr1 = '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb';
50
+ const addr2 = '0x1234567890123456789012345678901234567890';
51
+ const hash1 = onChainAddress({ chainId: TEST_CHAIN_ID, address: addr1 });
52
+ const hash2 = onChainAddress({ chainId: TEST_CHAIN_ID, address: addr2 });
53
+ assert.notEqual(hash1, hash2);
54
+ });
55
+ it('should be equivalent to onChainHash', () => {
56
+ const addressHash = onChainAddress({ chainId: TEST_CHAIN_ID, address: TEST_ADDRESS });
57
+ const chainHash = onChainHash({ chainId: TEST_CHAIN_ID, hash: TEST_ADDRESS });
58
+ assert.equal(addressHash, chainHash);
59
+ });
60
+ });
61
+ describe('ids object', () => {
62
+ it('should have all required ID generators', () => {
63
+ assert.ok(ids.block);
64
+ assert.ok(ids.transaction);
65
+ assert.ok(ids.providerHash);
66
+ assert.ok(ids.verifier);
67
+ });
68
+ it('should reference onChainHash functions', () => {
69
+ assert.equal(ids.block, onChainHash);
70
+ assert.equal(ids.transaction, onChainHash);
71
+ assert.equal(ids.providerHash, onChainHash);
72
+ });
73
+ it('should reference onChainAddress for verifier', () => {
74
+ assert.equal(ids.verifier, onChainAddress);
75
+ });
76
+ });
77
+ describe('orderId.deposit', () => {
78
+ it('should generate consistent deposit IDs', () => {
79
+ const id1 = orderId.deposit.serialize({
80
+ chainId: 8453n,
81
+ escrow: TEST_ADDRESS,
82
+ depositId: 123n,
83
+ });
84
+ const id2 = orderId.deposit.serialize({
85
+ chainId: 8453n,
86
+ escrow: TEST_ADDRESS,
87
+ depositId: 123n,
88
+ });
89
+ assert.equal(id1, id2);
90
+ });
91
+ it('should generate different IDs for different depositIds', () => {
92
+ const id1 = orderId.deposit.serialize({
93
+ chainId: 8453n,
94
+ escrow: TEST_ADDRESS,
95
+ depositId: 123n,
96
+ });
97
+ const id2 = orderId.deposit.serialize({
98
+ chainId: 8453n,
99
+ escrow: TEST_ADDRESS,
100
+ depositId: 124n,
101
+ });
102
+ assert.notEqual(id1, id2);
103
+ });
104
+ it('should generate different IDs for different escrow addresses', () => {
105
+ const escrow1 = '0x1111111111111111111111111111111111111111';
106
+ const escrow2 = '0x2222222222222222222222222222222222222222';
107
+ const id1 = orderId.deposit.serialize({
108
+ chainId: 8453n,
109
+ escrow: escrow1,
110
+ depositId: 123n,
111
+ });
112
+ const id2 = orderId.deposit.serialize({
113
+ chainId: 8453n,
114
+ escrow: escrow2,
115
+ depositId: 123n,
116
+ });
117
+ assert.notEqual(id1, id2);
118
+ });
119
+ it('should generate different IDs for different chains', () => {
120
+ const id1 = orderId.deposit.serialize({
121
+ chainId: 8453n,
122
+ escrow: TEST_ADDRESS,
123
+ depositId: 123n,
124
+ });
125
+ const id2 = orderId.deposit.serialize({
126
+ chainId: 1n,
127
+ escrow: TEST_ADDRESS,
128
+ depositId: 123n,
129
+ });
130
+ assert.notEqual(id1, id2);
131
+ });
132
+ it('should handle numeric chain IDs', () => {
133
+ const idBigInt = orderId.deposit.serialize({
134
+ chainId: 8453n,
135
+ escrow: TEST_ADDRESS,
136
+ depositId: 123n,
137
+ });
138
+ const idNumber = orderId.deposit.serialize({
139
+ chainId: 8453,
140
+ escrow: TEST_ADDRESS,
141
+ depositId: 123,
142
+ });
143
+ assert.equal(idBigInt, idNumber);
144
+ });
145
+ });
146
+ describe('orderId.block', () => {
147
+ it('should generate consistent block IDs', () => {
148
+ const id1 = orderId.block.serialize({
149
+ timestamp: 1000n,
150
+ chainId: 8453n,
151
+ });
152
+ const id2 = orderId.block.serialize({
153
+ timestamp: 1000n,
154
+ chainId: 8453n,
155
+ });
156
+ assert.equal(id1, id2);
157
+ });
158
+ it('should generate different IDs for different timestamps', () => {
159
+ const id1 = orderId.block.serialize({
160
+ timestamp: 1000n,
161
+ chainId: 8453n,
162
+ });
163
+ const id2 = orderId.block.serialize({
164
+ timestamp: 2000n,
165
+ chainId: 8453n,
166
+ });
167
+ assert.notEqual(id1, id2);
168
+ });
169
+ it('should sort chronologically', () => {
170
+ const id1 = orderId.block.serialize({ timestamp: 1000n, chainId: 8453n });
171
+ const id2 = orderId.block.serialize({ timestamp: 2000n, chainId: 8453n });
172
+ // IDs should sort in chronological order (timestamp is the prefix)
173
+ assert.ok(id1 < id2);
174
+ });
175
+ });
176
+ describe('orderId.transaction', () => {
177
+ it('should generate consistent transaction IDs', () => {
178
+ const id1 = orderId.transaction.serialize({
179
+ timestamp: 1000n,
180
+ chainId: 8453n,
181
+ transactionIndex: 5n,
182
+ });
183
+ const id2 = orderId.transaction.serialize({
184
+ timestamp: 1000n,
185
+ chainId: 8453n,
186
+ transactionIndex: 5n,
187
+ });
188
+ assert.equal(id1, id2);
189
+ });
190
+ it('should include transaction index in ordering', () => {
191
+ const id1 = orderId.transaction.serialize({
192
+ timestamp: 1000n,
193
+ chainId: 8453n,
194
+ transactionIndex: 1n,
195
+ });
196
+ const id2 = orderId.transaction.serialize({
197
+ timestamp: 1000n,
198
+ chainId: 8453n,
199
+ transactionIndex: 2n,
200
+ });
201
+ assert.ok(id1 < id2);
202
+ });
203
+ });
204
+ describe('orderId.log', () => {
205
+ it('should generate consistent log IDs', () => {
206
+ const id1 = orderId.log.serialize({
207
+ timestamp: 1000n,
208
+ chainId: 8453n,
209
+ transactionIndex: 5n,
210
+ logIndex: 3n,
211
+ });
212
+ const id2 = orderId.log.serialize({
213
+ timestamp: 1000n,
214
+ chainId: 8453n,
215
+ transactionIndex: 5n,
216
+ logIndex: 3n,
217
+ });
218
+ assert.equal(id1, id2);
219
+ });
220
+ it('should include log index in ordering', () => {
221
+ const id1 = orderId.log.serialize({
222
+ timestamp: 1000n,
223
+ chainId: 8453n,
224
+ transactionIndex: 1n,
225
+ logIndex: 1n,
226
+ });
227
+ const id2 = orderId.log.serialize({
228
+ timestamp: 1000n,
229
+ chainId: 8453n,
230
+ transactionIndex: 1n,
231
+ logIndex: 2n,
232
+ });
233
+ assert.ok(id1 < id2);
234
+ });
235
+ });
236
+ describe('orderId.order', () => {
237
+ it('should generate consistent order IDs', () => {
238
+ const id1 = orderId.order.serialize({
239
+ timestamp: 1000n,
240
+ chainId: 8453n,
241
+ transactionIndex: 5n,
242
+ logIndex: 3n,
243
+ name: 'deposit_received',
244
+ });
245
+ const id2 = orderId.order.serialize({
246
+ timestamp: 1000n,
247
+ chainId: 8453n,
248
+ transactionIndex: 5n,
249
+ logIndex: 3n,
250
+ name: 'deposit_received',
251
+ });
252
+ assert.equal(id1, id2);
253
+ });
254
+ it('should generate different IDs for different event names', () => {
255
+ const id1 = orderId.order.serialize({
256
+ timestamp: 1000n,
257
+ chainId: 8453n,
258
+ transactionIndex: 5n,
259
+ logIndex: 3n,
260
+ name: 'deposit_received',
261
+ });
262
+ const id2 = orderId.order.serialize({
263
+ timestamp: 1000n,
264
+ chainId: 8453n,
265
+ transactionIndex: 5n,
266
+ logIndex: 3n,
267
+ name: 'intent_signaled',
268
+ });
269
+ assert.notEqual(id1, id2);
270
+ });
271
+ });
272
+ describe('orderId.stat', () => {
273
+ const TEST_TOKEN = '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913';
274
+ const TEST_CURRENCY = '0x1234567890123456789012345678901234567890123456789012345678901234';
275
+ const TEST_VERIFIER = '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd';
276
+ it('should generate consistent stat IDs', () => {
277
+ const id1 = orderId.stat.serialize({
278
+ timestamp: 1000n,
279
+ type: 'hour',
280
+ event: 'deposit',
281
+ currency: TEST_CURRENCY,
282
+ token: TEST_TOKEN,
283
+ verifier: TEST_VERIFIER,
284
+ chainId: 8453n,
285
+ version: 'v2',
286
+ });
287
+ const id2 = orderId.stat.serialize({
288
+ timestamp: 1000n,
289
+ type: 'hour',
290
+ event: 'deposit',
291
+ currency: TEST_CURRENCY,
292
+ token: TEST_TOKEN,
293
+ verifier: TEST_VERIFIER,
294
+ chainId: 8453n,
295
+ version: 'v2',
296
+ });
297
+ assert.equal(id1, id2);
298
+ });
299
+ it('should bucket timestamps by hour', () => {
300
+ const id1 = orderId.stat.serialize({
301
+ timestamp: 3600n, // 1 hour in seconds
302
+ type: 'hour',
303
+ event: 'deposit',
304
+ currency: TEST_CURRENCY,
305
+ token: TEST_TOKEN,
306
+ verifier: TEST_VERIFIER,
307
+ chainId: 8453n,
308
+ version: 'v2',
309
+ });
310
+ const id2 = orderId.stat.serialize({
311
+ timestamp: 3700n, // Same hour
312
+ type: 'hour',
313
+ event: 'deposit',
314
+ currency: TEST_CURRENCY,
315
+ token: TEST_TOKEN,
316
+ verifier: TEST_VERIFIER,
317
+ chainId: 8453n,
318
+ version: 'v2',
319
+ });
320
+ assert.equal(id1, id2);
321
+ });
322
+ it('should generate different IDs for different hours', () => {
323
+ const id1 = orderId.stat.serialize({
324
+ timestamp: 3600n,
325
+ type: 'hour',
326
+ event: 'deposit',
327
+ currency: TEST_CURRENCY,
328
+ token: TEST_TOKEN,
329
+ verifier: TEST_VERIFIER,
330
+ chainId: 8453n,
331
+ version: 'v2',
332
+ });
333
+ const id2 = orderId.stat.serialize({
334
+ timestamp: 7200n, // Different hour
335
+ type: 'hour',
336
+ event: 'deposit',
337
+ currency: TEST_CURRENCY,
338
+ token: TEST_TOKEN,
339
+ verifier: TEST_VERIFIER,
340
+ chainId: 8453n,
341
+ version: 'v2',
342
+ });
343
+ assert.notEqual(id1, id2);
344
+ });
345
+ it('should generate different IDs for different types', () => {
346
+ const id1 = orderId.stat.serialize({
347
+ timestamp: 1000n,
348
+ type: 'hour',
349
+ event: 'deposit',
350
+ currency: TEST_CURRENCY,
351
+ token: TEST_TOKEN,
352
+ verifier: TEST_VERIFIER,
353
+ chainId: 8453n,
354
+ version: 'v2',
355
+ });
356
+ const id2 = orderId.stat.serialize({
357
+ timestamp: 1000n,
358
+ type: 'day',
359
+ event: 'deposit',
360
+ currency: TEST_CURRENCY,
361
+ token: TEST_TOKEN,
362
+ verifier: TEST_VERIFIER,
363
+ chainId: 8453n,
364
+ version: 'v2',
365
+ });
366
+ assert.notEqual(id1, id2);
367
+ });
368
+ it('should handle null currency', () => {
369
+ const id = orderId.stat.serialize({
370
+ timestamp: 1000n,
371
+ type: 'hour',
372
+ event: 'deposit',
373
+ currency: null,
374
+ token: TEST_TOKEN,
375
+ verifier: TEST_VERIFIER,
376
+ chainId: 8453n,
377
+ version: 'v2',
378
+ });
379
+ assert.ok(id);
380
+ assert.equal(id.length, 66);
381
+ });
382
+ it('should generate different IDs for different events', () => {
383
+ const id1 = orderId.stat.serialize({
384
+ timestamp: 1000n,
385
+ type: 'hour',
386
+ event: 'deposit',
387
+ currency: TEST_CURRENCY,
388
+ token: TEST_TOKEN,
389
+ verifier: TEST_VERIFIER,
390
+ chainId: 8453n,
391
+ version: 'v2',
392
+ });
393
+ const id2 = orderId.stat.serialize({
394
+ timestamp: 1000n,
395
+ type: 'hour',
396
+ event: 'withdrawal',
397
+ currency: TEST_CURRENCY,
398
+ token: TEST_TOKEN,
399
+ verifier: TEST_VERIFIER,
400
+ chainId: 8453n,
401
+ version: 'v2',
402
+ });
403
+ assert.notEqual(id1, id2);
404
+ });
405
+ it('should bucket timestamps by month', () => {
406
+ const id1 = orderId.stat.serialize({
407
+ timestamp: 2592000n, // 30 days in seconds
408
+ type: 'month',
409
+ event: 'deposit',
410
+ currency: TEST_CURRENCY,
411
+ token: TEST_TOKEN,
412
+ verifier: TEST_VERIFIER,
413
+ chainId: 8453n,
414
+ version: 'v2',
415
+ });
416
+ const id2 = orderId.stat.serialize({
417
+ timestamp: 2592100n, // Same month
418
+ type: 'month',
419
+ event: 'deposit',
420
+ currency: TEST_CURRENCY,
421
+ token: TEST_TOKEN,
422
+ verifier: TEST_VERIFIER,
423
+ chainId: 8453n,
424
+ version: 'v2',
425
+ });
426
+ assert.equal(id1, id2);
427
+ });
428
+ it('should bucket timestamps by day', () => {
429
+ const id1 = orderId.stat.serialize({
430
+ timestamp: 86400n, // 1 day in seconds
431
+ type: 'day',
432
+ event: 'deposit',
433
+ currency: TEST_CURRENCY,
434
+ token: TEST_TOKEN,
435
+ verifier: TEST_VERIFIER,
436
+ chainId: 8453n,
437
+ version: 'v2',
438
+ });
439
+ const id2 = orderId.stat.serialize({
440
+ timestamp: 86500n, // Same day
441
+ type: 'day',
442
+ event: 'deposit',
443
+ currency: TEST_CURRENCY,
444
+ token: TEST_TOKEN,
445
+ verifier: TEST_VERIFIER,
446
+ chainId: 8453n,
447
+ version: 'v2',
448
+ });
449
+ assert.equal(id1, id2);
450
+ });
451
+ });
452
+ describe('chunkHex', () => {
453
+ it('should chunk hex string correctly', () => {
454
+ const input = '0x1234567890abcdef';
455
+ const result = chunkHex(input, [4, 4]);
456
+ assert.deepEqual(result, ['0x12345678', '0x90abcdef']);
457
+ });
458
+ it('should handle different byte sizes', () => {
459
+ const input = '0x112233445566778899aabbcc';
460
+ const result = chunkHex(input, [2, 4, 6]);
461
+ assert.deepEqual(result, ['0x1122', '0x33445566', '0x778899aabbcc']);
462
+ });
463
+ it('should throw for invalid hex input', () => {
464
+ assert.throws(() => {
465
+ chunkHex('not-hex', [4]);
466
+ }, /Invalid hex input/);
467
+ });
468
+ it('should throw for wrong length hex input', () => {
469
+ const input = '0x1234'; // 2 bytes
470
+ assert.throws(() => {
471
+ chunkHex(input, [4, 4]); // expects 8 bytes
472
+ }, /Hex length/);
473
+ });
474
+ it('should handle single chunk', () => {
475
+ const input = '0x12345678';
476
+ const result = chunkHex(input, [4]);
477
+ assert.deepEqual(result, ['0x12345678']);
478
+ });
479
+ });
480
+ describe('orderId.deposit.parse', () => {
481
+ const TEST_ESCROW = '0x1111111111111111111111111111111111111111';
482
+ it('should roundtrip serialize and parse', () => {
483
+ const original = { chainId: 8453, escrow: TEST_ESCROW, depositId: 123n };
484
+ const serialized = orderId.deposit.serialize(original);
485
+ const parsed = orderId.deposit.parse(serialized);
486
+ assert.equal(parsed.chainId, 8453);
487
+ assert.equal(parsed.escrow.toLowerCase(), TEST_ESCROW.toLowerCase());
488
+ assert.equal(parsed.depositId, 123n);
489
+ });
490
+ it('should parse correct components', () => {
491
+ const serialized = orderId.deposit.serialize({
492
+ chainId: 369, // Pulsechain
493
+ escrow: TEST_ESCROW,
494
+ depositId: 999n,
495
+ });
496
+ const parsed = orderId.deposit.parse(serialized);
497
+ assert.equal(parsed.chainId, 369);
498
+ assert.equal(parsed.depositId, 999n);
499
+ });
500
+ it('should throw for unknown chainId', () => {
501
+ // Manually construct an ID with invalid chainId (1 = Ethereum mainnet, not in chainIds)
502
+ const badId = '0x1111111111111111111111111111111111111111000000000000007b00000001';
503
+ assert.throws(() => {
504
+ orderId.deposit.parse(badId);
505
+ }, /Unknown chainId/);
506
+ });
507
+ });
508
+ describe('orderId.block.parse', () => {
509
+ it('should roundtrip serialize and parse', () => {
510
+ const original = { timestamp: 1704067200n, chainId: 8453n };
511
+ const serialized = orderId.block.serialize(original);
512
+ const parsed = orderId.block.parse(serialized);
513
+ assert.equal(parsed.timestamp, 1704067200n);
514
+ assert.equal(parsed.chainId, 8453);
515
+ });
516
+ it('should throw for unknown chainId', () => {
517
+ // Manually construct with invalid chainId
518
+ const badId = '0x00000000659d268000000001';
519
+ assert.throws(() => {
520
+ orderId.block.parse(badId);
521
+ }, /Unknown chainId/);
522
+ });
523
+ });
524
+ describe('orderId.transaction.parse', () => {
525
+ it('should roundtrip serialize and parse', () => {
526
+ const original = { timestamp: 1704067200n, chainId: 8453n, transactionIndex: 5n };
527
+ const serialized = orderId.transaction.serialize(original);
528
+ const parsed = orderId.transaction.parse(serialized);
529
+ assert.equal(parsed.timestamp, 1704067200n);
530
+ assert.equal(parsed.transactionIndex, 5);
531
+ assert.equal(parsed.chainId, 8453);
532
+ });
533
+ it('should throw for unknown chainId', () => {
534
+ const badId = '0x00000000659d26800000000500000001';
535
+ assert.throws(() => {
536
+ orderId.transaction.parse(badId);
537
+ }, /Unknown chainId/);
538
+ });
539
+ });
540
+ describe('orderId.log.parse', () => {
541
+ it('should roundtrip serialize and parse', () => {
542
+ const original = { timestamp: 1704067200n, chainId: 8453n, transactionIndex: 5n, logIndex: 3n };
543
+ const serialized = orderId.log.serialize(original);
544
+ const parsed = orderId.log.parse(serialized);
545
+ assert.equal(parsed.timestamp, 1704067200n);
546
+ assert.equal(parsed.transactionIndex, 5);
547
+ assert.equal(parsed.logIndex, 3);
548
+ assert.equal(parsed.chainId, 8453);
549
+ });
550
+ it('should throw for unknown chainId', () => {
551
+ const badId = '0x00000000659d2680000000050000000300000001';
552
+ assert.throws(() => {
553
+ orderId.log.parse(badId);
554
+ }, /Unknown chainId/);
555
+ });
556
+ });
557
+ describe('orderId.order.parse', () => {
558
+ it('should roundtrip serialize and parse', () => {
559
+ const original = {
560
+ timestamp: 1704067200n,
561
+ chainId: 8453n,
562
+ transactionIndex: 5n,
563
+ logIndex: 3n,
564
+ name: 'deposit_received'
565
+ };
566
+ const serialized = orderId.order.serialize(original);
567
+ const parsed = orderId.order.parse(serialized);
568
+ assert.equal(parsed.timestamp, 1704067200n);
569
+ assert.equal(parsed.transactionIndex, 5);
570
+ assert.equal(parsed.logIndex, 3);
571
+ assert.equal(parsed.chainId, 8453);
572
+ assert.equal(parsed.name, 'deposit_received');
573
+ });
574
+ it('should return eventTypeId even if name not found', () => {
575
+ const serialized = orderId.order.serialize({
576
+ timestamp: 1000n,
577
+ chainId: 8453n,
578
+ transactionIndex: 1n,
579
+ logIndex: 1n,
580
+ name: 'deposit_received',
581
+ });
582
+ const parsed = orderId.order.parse(serialized);
583
+ assert.ok(typeof parsed.eventTypeId === 'number');
584
+ });
585
+ it('should return undefined name for unknown eventTypeId', () => {
586
+ // Manually construct an order ID with eventTypeId = 255 (not in eventOrderId map)
587
+ // Format: timestamp(8) + txIndex(4) + eventType(1) + logIndex(4) + chainId(4) = 21 bytes
588
+ const badEventTypeId = '0x00000000000003e8000000010000000100002105'; // eventType=0xff (255)
589
+ // This should parse but name should be undefined
590
+ try {
591
+ const parsed = orderId.order.parse(badEventTypeId);
592
+ // If parsing succeeds, name should be undefined for unknown eventTypeId
593
+ assert.equal(parsed.name, undefined);
594
+ assert.equal(parsed.eventTypeId, 0); // or whatever the parsed value is
595
+ }
596
+ catch {
597
+ // May throw for chainId validation - that's also acceptable
598
+ }
599
+ });
600
+ it('should throw for unknown chainId', () => {
601
+ const badId = '0x00000000659d2680000000050100000003000000001';
602
+ assert.throws(() => {
603
+ orderId.order.parse(badId);
604
+ }, /Unknown chainId|Hex length/);
605
+ });
606
+ });
607
+ });
@@ -0,0 +1,45 @@
1
+ /**
2
+ * @fileoverview Core utilities package for the Provex protocol.
3
+ *
4
+ * This package provides foundational types and utilities used across:
5
+ * - The indexer (blockchain event processing)
6
+ * - The UI (React application)
7
+ * - The analytics dashboard
8
+ * - Operational scripts
9
+ *
10
+ * @remarks
11
+ * **Key Exports:**
12
+ * - `contracts`: Contract addresses for all supported chains and versions
13
+ * - `payment`: Payment provider configurations and utilities
14
+ * - `currencies`: Fiat currency definitions and formatting
15
+ * - `tokens`: ERC20 token definitions (USDC)
16
+ * - `deposit`: Deposit status types and enums
17
+ * - `intent`: Intent data structures
18
+ * - `peer-types`: Attestation and proof types for V3 flow
19
+ * - `ids`: ID generation for indexer entities
20
+ * - `units`: Bigint formatting and parsing utilities
21
+ * - `fees`: Protocol fee calculations
22
+ * - `reputation`: Taker tier system
23
+ *
24
+ * @packageDocumentation
25
+ */
26
+ export * from './currencies.js';
27
+ export * from './units.js';
28
+ export * from './contracts.js';
29
+ export * from './participants.js';
30
+ export * from './chain.js';
31
+ export * from './intent.js';
32
+ export * from './conversionRates.js';
33
+ export * from './payment.js';
34
+ export * from './sort.js';
35
+ export * from './time.js';
36
+ export * from './tokens.js';
37
+ export * from './stats.js';
38
+ export * from './stateKey.js';
39
+ export * from './deposit.js';
40
+ export * from './depositKeys.js';
41
+ export * from './ids.js';
42
+ export * from './events.js';
43
+ export * from './peer-types.js';
44
+ export * from './fees.js';
45
+ export * from './reputation.js';
package/dist/index.js ADDED
@@ -0,0 +1,45 @@
1
+ /**
2
+ * @fileoverview Core utilities package for the Provex protocol.
3
+ *
4
+ * This package provides foundational types and utilities used across:
5
+ * - The indexer (blockchain event processing)
6
+ * - The UI (React application)
7
+ * - The analytics dashboard
8
+ * - Operational scripts
9
+ *
10
+ * @remarks
11
+ * **Key Exports:**
12
+ * - `contracts`: Contract addresses for all supported chains and versions
13
+ * - `payment`: Payment provider configurations and utilities
14
+ * - `currencies`: Fiat currency definitions and formatting
15
+ * - `tokens`: ERC20 token definitions (USDC)
16
+ * - `deposit`: Deposit status types and enums
17
+ * - `intent`: Intent data structures
18
+ * - `peer-types`: Attestation and proof types for V3 flow
19
+ * - `ids`: ID generation for indexer entities
20
+ * - `units`: Bigint formatting and parsing utilities
21
+ * - `fees`: Protocol fee calculations
22
+ * - `reputation`: Taker tier system
23
+ *
24
+ * @packageDocumentation
25
+ */
26
+ export * from './currencies.js';
27
+ export * from './units.js';
28
+ export * from './contracts.js';
29
+ export * from './participants.js';
30
+ export * from './chain.js';
31
+ export * from './intent.js';
32
+ export * from './conversionRates.js';
33
+ export * from './payment.js';
34
+ export * from './sort.js';
35
+ export * from './time.js';
36
+ export * from './tokens.js';
37
+ export * from './stats.js';
38
+ export * from './stateKey.js';
39
+ export * from './deposit.js';
40
+ export * from './depositKeys.js';
41
+ export * from './ids.js';
42
+ export * from './events.js';
43
+ export * from './peer-types.js';
44
+ export * from './fees.js';
45
+ export * from './reputation.js';