@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/dist/ids.d.ts ADDED
@@ -0,0 +1,175 @@
1
+ /**
2
+ * @fileoverview ID generation and parsing utilities for the indexer.
3
+ *
4
+ * The indexer uses composite IDs (orderIds) to uniquely identify and order
5
+ * events across multiple chains. Each ID type encodes different information:
6
+ *
7
+ * - **deposit**: [escrow (20)][localId (8)][chainId (4)] = 32 bytes
8
+ * - **block**: [timestamp (8)][chainId (4)] = 12 bytes
9
+ * - **transaction**: [timestamp (8)][txIndex (4)][chainId (4)] = 16 bytes
10
+ * - **log**: [timestamp (8)][txIndex (4)][logIndex (4)][chainId (4)] = 20 bytes
11
+ * - **order**: [timestamp (8)][txIndex (4)][eventType (1)][logIndex (4)][chainId (4)] = 21 bytes
12
+ * - **stat**: [timestamp (8)][hash (20)][chainId (4)] = 32 bytes
13
+ *
14
+ * @remarks
15
+ * The orderId structure ensures:
16
+ * - Global ordering across chains by timestamp
17
+ * - Deterministic ordering within same block
18
+ * - Parseability to extract original components
19
+ *
20
+ * @see {@link events} for event type definitions
21
+ * @see {@link depositKeys} for deposit compound keys
22
+ */
23
+ import { type Hex } from "viem";
24
+ import { type EventType } from "./events.js";
25
+ import { type TimeResolutionKey } from "./time.js";
26
+ import type { StatEvent } from "./stats.js";
27
+ import { type ChainId } from "./chain.js";
28
+ /** Input parameters for provider hash generation */
29
+ export type ProviderHashInputs = {
30
+ chainId: bigint | number;
31
+ hash: Hex;
32
+ };
33
+ /** Input parameters for on-chain hash generation */
34
+ export type OnChainHashInputs = {
35
+ chainId: bigint | number;
36
+ hash: Hex;
37
+ };
38
+ /** Input parameters for on-chain address hashing */
39
+ export type OnChainAddressInputs = {
40
+ chainId: bigint | number;
41
+ address: Hex;
42
+ };
43
+ /**
44
+ * Generate a chain-specific hash by combining chainId and an existing hash.
45
+ * Creates a unique identifier that includes chain context.
46
+ */
47
+ export declare const onChainHash: ({ chainId, hash }: OnChainHashInputs) => `0x${string}`;
48
+ export declare const onChainAddress: ({ chainId, address }: OnChainAddressInputs) => `0x${string}`;
49
+ /**
50
+ * ID generator functions for various entity types.
51
+ * Each function creates a unique, deterministic identifier.
52
+ */
53
+ export declare const ids: {
54
+ /** Block ID: keccak256(chainId || blockHash) */
55
+ block: ({ chainId, hash }: OnChainHashInputs) => `0x${string}`;
56
+ /** Transaction ID: keccak256(chainId || txHash) */
57
+ transaction: ({ chainId, hash }: OnChainHashInputs) => `0x${string}`;
58
+ /** Provider hash ID: keccak256(chainId || providerHash) */
59
+ providerHash: ({ chainId, hash }: OnChainHashInputs) => `0x${string}`;
60
+ /** Verifier ID: keccak256(chainId || verifierAddress) */
61
+ verifier: ({ chainId, address }: OnChainAddressInputs) => `0x${string}`;
62
+ };
63
+ export type OrderIdInputs = {
64
+ timestamp: bigint;
65
+ chainId: bigint;
66
+ };
67
+ export type OrderIdTransactionInputs = OrderIdInputs & {
68
+ transactionIndex: bigint;
69
+ };
70
+ export type OrderIdLogInputs = OrderIdTransactionInputs & {
71
+ logIndex: bigint;
72
+ };
73
+ export type OrderIdOrderInputs = OrderIdLogInputs & {
74
+ name: EventType;
75
+ };
76
+ export type DepositIdInputs = {
77
+ chainId: bigint | number;
78
+ escrow: Hex;
79
+ depositId: bigint | number;
80
+ };
81
+ export type LogStatsIdInputs = {
82
+ timestamp: bigint;
83
+ type: TimeResolutionKey;
84
+ event: StatEvent;
85
+ currency: Hex | null;
86
+ token: Hex;
87
+ verifier: Hex;
88
+ chainId: bigint | number;
89
+ version: 'v2' | 'v3';
90
+ };
91
+ /**
92
+ * Chunk a hex string into parts of specified byte sizes
93
+ * @param input - The hex string to chunk (with 0x prefix)
94
+ * @param byteSizes - Array of byte sizes for each chunk
95
+ * @returns Array of hex strings, one for each chunk
96
+ */
97
+ export declare function chunkHex<T extends readonly number[]>(input: Hex, byteSizes: T): {
98
+ [K in keyof T]: Hex;
99
+ };
100
+ /**
101
+ * OrderId serialization and parsing utilities.
102
+ *
103
+ * Each orderId type has a `serialize` method to create the ID from components,
104
+ * and a `parse` method to extract components from an existing ID.
105
+ *
106
+ * @example
107
+ * ```ts
108
+ * // Create a deposit orderId
109
+ * const id = orderId.deposit.serialize({ chainId: 8453n, escrow, depositId: 42n })
110
+ *
111
+ * // Parse it back
112
+ * const { chainId, escrow, depositId } = orderId.deposit.parse(id)
113
+ * ```
114
+ */
115
+ export declare const orderId: {
116
+ /**
117
+ * Deposit orderId: uniquely identifies a deposit across all chains and escrows.
118
+ * Format: [escrow (20)][localId (8)][chainId (4)] = 32 bytes
119
+ */
120
+ deposit: {
121
+ serialize: ({ chainId, escrow, depositId }: DepositIdInputs) => `0x${string}`;
122
+ parse: (id: Hex) => {
123
+ escrow: Hex;
124
+ depositId: bigint;
125
+ chainId: ChainId;
126
+ };
127
+ };
128
+ block: {
129
+ serialize: ({ timestamp, chainId }: OrderIdInputs) => `0x${string}`;
130
+ parse: (id: Hex) => {
131
+ timestamp: bigint;
132
+ chainId: ChainId;
133
+ };
134
+ };
135
+ transaction: {
136
+ serialize: ({ timestamp, chainId, transactionIndex }: OrderIdTransactionInputs) => `0x${string}`;
137
+ parse: (id: Hex) => {
138
+ timestamp: bigint;
139
+ transactionIndex: number;
140
+ chainId: ChainId;
141
+ };
142
+ };
143
+ log: {
144
+ serialize: ({ timestamp, chainId, transactionIndex, logIndex }: OrderIdLogInputs) => `0x${string}`;
145
+ parse: (id: Hex) => {
146
+ timestamp: bigint;
147
+ transactionIndex: number;
148
+ logIndex: number;
149
+ chainId: ChainId;
150
+ };
151
+ };
152
+ order: {
153
+ serialize: ({ timestamp, chainId, transactionIndex, logIndex, name }: OrderIdOrderInputs) => `0x${string}`;
154
+ parse: (id: Hex) => {
155
+ timestamp: bigint;
156
+ transactionIndex: number;
157
+ eventTypeId: number;
158
+ name?: EventType;
159
+ logIndex: number;
160
+ chainId: ChainId;
161
+ };
162
+ };
163
+ stat: {
164
+ serialize: ({ timestamp, type, event, currency, verifier, token, chainId, version }: LogStatsIdInputs) => `0x${string}`;
165
+ /**
166
+ * Parse a stat orderId to extract timestamp and chainId.
167
+ * Note: The hash component cannot be reversed to get type/event/currency/verifier/token.
168
+ * Format: [timestamp (8)][hash (20)][chainId (4)] = 32 bytes
169
+ */
170
+ parse: (id: Hex) => {
171
+ timestamp: bigint;
172
+ chainId: ChainId;
173
+ };
174
+ };
175
+ };
package/dist/ids.js ADDED
@@ -0,0 +1,231 @@
1
+ /**
2
+ * @fileoverview ID generation and parsing utilities for the indexer.
3
+ *
4
+ * The indexer uses composite IDs (orderIds) to uniquely identify and order
5
+ * events across multiple chains. Each ID type encodes different information:
6
+ *
7
+ * - **deposit**: [escrow (20)][localId (8)][chainId (4)] = 32 bytes
8
+ * - **block**: [timestamp (8)][chainId (4)] = 12 bytes
9
+ * - **transaction**: [timestamp (8)][txIndex (4)][chainId (4)] = 16 bytes
10
+ * - **log**: [timestamp (8)][txIndex (4)][logIndex (4)][chainId (4)] = 20 bytes
11
+ * - **order**: [timestamp (8)][txIndex (4)][eventType (1)][logIndex (4)][chainId (4)] = 21 bytes
12
+ * - **stat**: [timestamp (8)][hash (20)][chainId (4)] = 32 bytes
13
+ *
14
+ * @remarks
15
+ * The orderId structure ensures:
16
+ * - Global ordering across chains by timestamp
17
+ * - Deterministic ordering within same block
18
+ * - Parseability to extract original components
19
+ *
20
+ * @see {@link events} for event type definitions
21
+ * @see {@link depositKeys} for deposit compound keys
22
+ */
23
+ import { concatHex, keccak256, numberToHex, stringToHex, isHex } from "viem";
24
+ import { eventOrderId } from "./events.js";
25
+ import { interval } from "./time.js";
26
+ import { chainIds } from "./chain.js";
27
+ /**
28
+ * Generate a chain-specific hash by combining chainId and an existing hash.
29
+ * Creates a unique identifier that includes chain context.
30
+ */
31
+ export const onChainHash = ({ chainId, hash }) => (keccak256(concatHex([
32
+ numberToHex(chainId, { size: 8 }),
33
+ hash,
34
+ ])));
35
+ export const onChainAddress = ({ chainId, address }) => (onChainHash({ chainId, hash: address }));
36
+ /**
37
+ * ID generator functions for various entity types.
38
+ * Each function creates a unique, deterministic identifier.
39
+ */
40
+ export const ids = {
41
+ /** Block ID: keccak256(chainId || blockHash) */
42
+ block: onChainHash,
43
+ /** Transaction ID: keccak256(chainId || txHash) */
44
+ transaction: onChainHash,
45
+ /** Provider hash ID: keccak256(chainId || providerHash) */
46
+ providerHash: onChainHash,
47
+ /** Verifier ID: keccak256(chainId || verifierAddress) */
48
+ verifier: onChainAddress,
49
+ };
50
+ const hourFactor = BigInt(interval.hour) / BigInt(interval.second);
51
+ const dayFactor = BigInt(interval.day) / BigInt(interval.second);
52
+ const monthFactor = BigInt(interval.month) / BigInt(interval.second);
53
+ /**
54
+ * Chunk a hex string into parts of specified byte sizes
55
+ * @param input - The hex string to chunk (with 0x prefix)
56
+ * @param byteSizes - Array of byte sizes for each chunk
57
+ * @returns Array of hex strings, one for each chunk
58
+ */
59
+ export function chunkHex(input, byteSizes) {
60
+ const expectedBytes = byteSizes.reduce((a, b) => a + b, 0);
61
+ if (!isHex(input, { strict: true })) {
62
+ throw new Error(`Invalid hex input: ${input}`);
63
+ }
64
+ const hex = input.slice(2).toLowerCase();
65
+ const expectedHexLen = expectedBytes * 2;
66
+ if (hex.length !== expectedHexLen) {
67
+ throw new Error(`Hex length ${hex.length / 2} bytes does not match expected ${expectedHexLen / 2} bytes`);
68
+ }
69
+ const out = [];
70
+ let offset = 0;
71
+ for (const size of byteSizes) {
72
+ const chars = size * 2;
73
+ const part = "0x" + hex.slice(offset, offset + chars);
74
+ out.push(part);
75
+ offset += chars;
76
+ }
77
+ return out;
78
+ }
79
+ /**
80
+ * Type guard to check if a number is a valid ChainId
81
+ */
82
+ function isChainId(value) {
83
+ return chainIds.includes(value);
84
+ }
85
+ /**
86
+ * OrderId serialization and parsing utilities.
87
+ *
88
+ * Each orderId type has a `serialize` method to create the ID from components,
89
+ * and a `parse` method to extract components from an existing ID.
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * // Create a deposit orderId
94
+ * const id = orderId.deposit.serialize({ chainId: 8453n, escrow, depositId: 42n })
95
+ *
96
+ * // Parse it back
97
+ * const { chainId, escrow, depositId } = orderId.deposit.parse(id)
98
+ * ```
99
+ */
100
+ export const orderId = {
101
+ /**
102
+ * Deposit orderId: uniquely identifies a deposit across all chains and escrows.
103
+ * Format: [escrow (20)][localId (8)][chainId (4)] = 32 bytes
104
+ */
105
+ deposit: {
106
+ serialize: ({ chainId, escrow, depositId }) => concatHex([
107
+ escrow,
108
+ numberToHex(depositId, { size: 8 }),
109
+ numberToHex(chainId, { size: 4 }),
110
+ ]),
111
+ parse: (id) => {
112
+ const [escrow, depositIdHex, chainIdHex] = chunkHex(id, [20, 8, 4]);
113
+ const depositId = BigInt(depositIdHex);
114
+ const chainIdNum = Number(BigInt(chainIdHex));
115
+ if (!isChainId(chainIdNum)) {
116
+ throw new Error(`Unknown chainId parsed from deposit id: ${chainIdNum}`);
117
+ }
118
+ return { escrow, depositId, chainId: chainIdNum };
119
+ },
120
+ },
121
+ block: {
122
+ serialize: ({ timestamp, chainId }) => concatHex([
123
+ numberToHex(timestamp, { size: 8 }),
124
+ numberToHex(chainId, { size: 4 }),
125
+ ]),
126
+ parse: (id) => {
127
+ const [timestampHex, chainIdHex] = chunkHex(id, [8, 4]);
128
+ const timestamp = BigInt(timestampHex);
129
+ const chainIdNum = Number(BigInt(chainIdHex));
130
+ if (!isChainId(chainIdNum)) {
131
+ throw new Error(`Unknown chainId parsed from block id: ${chainIdNum}`);
132
+ }
133
+ return { timestamp, chainId: chainIdNum };
134
+ },
135
+ },
136
+ transaction: {
137
+ serialize: ({ timestamp, chainId, transactionIndex }) => concatHex([
138
+ numberToHex(timestamp, { size: 8 }),
139
+ numberToHex(transactionIndex, { size: 4 }),
140
+ numberToHex(chainId, { size: 4 }),
141
+ ]),
142
+ parse: (id) => {
143
+ const [timestampHex, txIndexHex, chainIdHex] = chunkHex(id, [8, 4, 4]);
144
+ const timestamp = BigInt(timestampHex);
145
+ const transactionIndex = Number(BigInt(txIndexHex));
146
+ const chainIdNum = Number(BigInt(chainIdHex));
147
+ if (!isChainId(chainIdNum)) {
148
+ throw new Error(`Unknown chainId parsed from transaction id: ${chainIdNum}`);
149
+ }
150
+ return { timestamp, transactionIndex, chainId: chainIdNum };
151
+ },
152
+ },
153
+ log: {
154
+ serialize: ({ timestamp, chainId, transactionIndex, logIndex }) => concatHex([
155
+ numberToHex(timestamp, { size: 8 }),
156
+ numberToHex(transactionIndex, { size: 4 }),
157
+ numberToHex(logIndex, { size: 4 }),
158
+ numberToHex(chainId, { size: 4 }),
159
+ ]),
160
+ parse: (id) => {
161
+ const [timestampHex, txIndexHex, logIndexHex, chainIdHex] = chunkHex(id, [8, 4, 4, 4]);
162
+ const timestamp = BigInt(timestampHex);
163
+ const transactionIndex = Number(BigInt(txIndexHex));
164
+ const logIndex = Number(BigInt(logIndexHex));
165
+ const chainIdNum = Number(BigInt(chainIdHex));
166
+ if (!isChainId(chainIdNum)) {
167
+ throw new Error(`Unknown chainId parsed from log id: ${chainIdNum}`);
168
+ }
169
+ return { timestamp, transactionIndex, logIndex, chainId: chainIdNum };
170
+ },
171
+ },
172
+ order: {
173
+ serialize: ({ timestamp, chainId, transactionIndex, logIndex, name }) => concatHex([
174
+ numberToHex(timestamp, { size: 8 }),
175
+ numberToHex(transactionIndex, { size: 4 }),
176
+ numberToHex(eventOrderId.get(name), { size: 1 }),
177
+ numberToHex(logIndex, { size: 4 }),
178
+ numberToHex(chainId, { size: 4 }),
179
+ ]),
180
+ parse: (id) => {
181
+ const [timestampHex, txIndexHex, typeHex, logIndexHex, chainIdHex] = chunkHex(id, [8, 4, 1, 4, 4]);
182
+ const timestamp = BigInt(timestampHex);
183
+ const transactionIndex = Number(BigInt(txIndexHex));
184
+ const eventTypeId = Number(BigInt(typeHex));
185
+ const logIndex = Number(BigInt(logIndexHex));
186
+ const chainIdNum = Number(BigInt(chainIdHex));
187
+ if (!isChainId(chainIdNum)) {
188
+ throw new Error(`Unknown chainId parsed from order id: ${chainIdNum}`);
189
+ }
190
+ const typeEntries = Array.from(eventOrderId.entries());
191
+ const name = typeEntries.find(([, v]) => v === eventTypeId)?.[0];
192
+ return { timestamp, transactionIndex, eventTypeId, name, logIndex, chainId: chainIdNum };
193
+ },
194
+ },
195
+ stat: {
196
+ serialize: ({ timestamp, type, event, currency, verifier, token, chainId, version }) => {
197
+ const hour = (timestamp / hourFactor) * hourFactor;
198
+ const day = (timestamp / dayFactor) * dayFactor;
199
+ const month = (timestamp / monthFactor) * monthFactor;
200
+ let time = timestamp;
201
+ if (type === "hour") {
202
+ time = hour;
203
+ }
204
+ if (type === "day") {
205
+ time = day;
206
+ }
207
+ if (type === "month") {
208
+ time = month;
209
+ }
210
+ return concatHex([
211
+ numberToHex(time, { size: 8 }),
212
+ `0x${keccak256(concatHex([stringToHex(type), stringToHex(event), token, currency ?? '0x', verifier, stringToHex(version)])).slice(26)}`,
213
+ numberToHex(chainId, { size: 4 }),
214
+ ]);
215
+ },
216
+ /**
217
+ * Parse a stat orderId to extract timestamp and chainId.
218
+ * Note: The hash component cannot be reversed to get type/event/currency/verifier/token.
219
+ * Format: [timestamp (8)][hash (20)][chainId (4)] = 32 bytes
220
+ */
221
+ parse: (id) => {
222
+ const [timestampHex, , chainIdHex] = chunkHex(id, [8, 20, 4]);
223
+ const timestamp = BigInt(timestampHex);
224
+ const chainIdNum = Number(BigInt(chainIdHex));
225
+ if (!isChainId(chainIdNum)) {
226
+ throw new Error(`Unknown chainId parsed from stat id: ${chainIdNum}`);
227
+ }
228
+ return { timestamp, chainId: chainIdNum };
229
+ },
230
+ },
231
+ };
@@ -0,0 +1 @@
1
+ export {};