@aztec/ethereum 0.0.0-test.0

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 (118) hide show
  1. package/README.md +3 -0
  2. package/dest/chain.d.ts +25 -0
  3. package/dest/chain.d.ts.map +1 -0
  4. package/dest/chain.js +53 -0
  5. package/dest/client.d.ts +16 -0
  6. package/dest/client.d.ts.map +1 -0
  7. package/dest/client.js +31 -0
  8. package/dest/config.d.ts +39 -0
  9. package/dest/config.d.ts.map +1 -0
  10. package/dest/config.js +70 -0
  11. package/dest/constants.d.ts +4 -0
  12. package/dest/constants.d.ts.map +1 -0
  13. package/dest/constants.js +2 -0
  14. package/dest/contracts/empire_base.d.ts +13 -0
  15. package/dest/contracts/empire_base.d.ts.map +1 -0
  16. package/dest/contracts/empire_base.js +11 -0
  17. package/dest/contracts/fee_juice.d.ts +15 -0
  18. package/dest/contracts/fee_juice.d.ts.map +1 -0
  19. package/dest/contracts/fee_juice.js +52 -0
  20. package/dest/contracts/forwarder.d.ts +24 -0
  21. package/dest/contracts/forwarder.d.ts.map +1 -0
  22. package/dest/contracts/forwarder.js +101 -0
  23. package/dest/contracts/governance.d.ts +79 -0
  24. package/dest/contracts/governance.d.ts.map +1 -0
  25. package/dest/contracts/governance.js +247 -0
  26. package/dest/contracts/governance_proposer.d.ts +28 -0
  27. package/dest/contracts/governance_proposer.d.ts.map +1 -0
  28. package/dest/contracts/governance_proposer.js +82 -0
  29. package/dest/contracts/index.d.ts +9 -0
  30. package/dest/contracts/index.d.ts.map +1 -0
  31. package/dest/contracts/index.js +8 -0
  32. package/dest/contracts/registry.d.ts +24 -0
  33. package/dest/contracts/registry.d.ts.map +1 -0
  34. package/dest/contracts/registry.js +85 -0
  35. package/dest/contracts/rollup.d.ts +92 -0
  36. package/dest/contracts/rollup.d.ts.map +1 -0
  37. package/dest/contracts/rollup.js +234 -0
  38. package/dest/contracts/slashing_proposer.d.ts +21 -0
  39. package/dest/contracts/slashing_proposer.d.ts.map +1 -0
  40. package/dest/contracts/slashing_proposer.js +47 -0
  41. package/dest/deploy_l1_contracts.d.ts +21210 -0
  42. package/dest/deploy_l1_contracts.d.ts.map +1 -0
  43. package/dest/deploy_l1_contracts.js +687 -0
  44. package/dest/eth_cheat_codes.d.ts +147 -0
  45. package/dest/eth_cheat_codes.d.ts.map +1 -0
  46. package/dest/eth_cheat_codes.js +303 -0
  47. package/dest/index.d.ts +14 -0
  48. package/dest/index.d.ts.map +1 -0
  49. package/dest/index.js +13 -0
  50. package/dest/l1_contract_addresses.d.ts +57 -0
  51. package/dest/l1_contract_addresses.d.ts.map +1 -0
  52. package/dest/l1_contract_addresses.js +97 -0
  53. package/dest/l1_reader.d.ts +16 -0
  54. package/dest/l1_reader.d.ts.map +1 -0
  55. package/dest/l1_reader.js +27 -0
  56. package/dest/l1_tx_utils.d.ts +192 -0
  57. package/dest/l1_tx_utils.d.ts.map +1 -0
  58. package/dest/l1_tx_utils.js +641 -0
  59. package/dest/l1_tx_utils_with_blobs.d.ts +12 -0
  60. package/dest/l1_tx_utils_with_blobs.d.ts.map +1 -0
  61. package/dest/l1_tx_utils_with_blobs.js +64 -0
  62. package/dest/queries.d.ts +12 -0
  63. package/dest/queries.d.ts.map +1 -0
  64. package/dest/queries.js +35 -0
  65. package/dest/test/delayed_tx_utils.d.ts +8 -0
  66. package/dest/test/delayed_tx_utils.d.ts.map +1 -0
  67. package/dest/test/delayed_tx_utils.js +21 -0
  68. package/dest/test/eth_cheat_codes_with_state.d.ts +18 -0
  69. package/dest/test/eth_cheat_codes_with_state.d.ts.map +1 -0
  70. package/dest/test/eth_cheat_codes_with_state.js +34 -0
  71. package/dest/test/index.d.ts +6 -0
  72. package/dest/test/index.d.ts.map +1 -0
  73. package/dest/test/index.js +5 -0
  74. package/dest/test/start_anvil.d.ts +12 -0
  75. package/dest/test/start_anvil.d.ts.map +1 -0
  76. package/dest/test/start_anvil.js +46 -0
  77. package/dest/test/tx_delayer.d.ts +25 -0
  78. package/dest/test/tx_delayer.d.ts.map +1 -0
  79. package/dest/test/tx_delayer.js +116 -0
  80. package/dest/test/upgrade_utils.d.ts +11 -0
  81. package/dest/test/upgrade_utils.d.ts.map +1 -0
  82. package/dest/test/upgrade_utils.js +104 -0
  83. package/dest/types.d.ts +14 -0
  84. package/dest/types.d.ts.map +1 -0
  85. package/dest/types.js +1 -0
  86. package/dest/utils.d.ts +24 -0
  87. package/dest/utils.d.ts.map +1 -0
  88. package/dest/utils.js +209 -0
  89. package/package.json +98 -0
  90. package/src/chain.ts +71 -0
  91. package/src/client.ts +58 -0
  92. package/src/config.ts +103 -0
  93. package/src/constants.ts +4 -0
  94. package/src/contracts/empire_base.ts +19 -0
  95. package/src/contracts/fee_juice.ts +43 -0
  96. package/src/contracts/forwarder.ts +132 -0
  97. package/src/contracts/governance.ts +285 -0
  98. package/src/contracts/governance_proposer.ts +82 -0
  99. package/src/contracts/index.ts +8 -0
  100. package/src/contracts/registry.ts +106 -0
  101. package/src/contracts/rollup.ts +274 -0
  102. package/src/contracts/slashing_proposer.ts +51 -0
  103. package/src/deploy_l1_contracts.ts +948 -0
  104. package/src/eth_cheat_codes.ts +314 -0
  105. package/src/index.ts +13 -0
  106. package/src/l1_contract_addresses.ts +109 -0
  107. package/src/l1_reader.ts +42 -0
  108. package/src/l1_tx_utils.ts +847 -0
  109. package/src/l1_tx_utils_with_blobs.ts +86 -0
  110. package/src/queries.ts +58 -0
  111. package/src/test/delayed_tx_utils.ts +24 -0
  112. package/src/test/eth_cheat_codes_with_state.ts +38 -0
  113. package/src/test/index.ts +5 -0
  114. package/src/test/start_anvil.ts +52 -0
  115. package/src/test/tx_delayer.ts +163 -0
  116. package/src/test/upgrade_utils.ts +100 -0
  117. package/src/types.ts +33 -0
  118. package/src/utils.ts +276 -0
package/src/utils.ts ADDED
@@ -0,0 +1,276 @@
1
+ import type { Fr } from '@aztec/foundation/fields';
2
+ import type { Logger } from '@aztec/foundation/log';
3
+ import { ErrorsAbi } from '@aztec/l1-artifacts';
4
+
5
+ import {
6
+ type Abi,
7
+ BaseError,
8
+ type ContractEventName,
9
+ ContractFunctionRevertedError,
10
+ type DecodeEventLogReturnType,
11
+ type Hex,
12
+ type Log,
13
+ decodeErrorResult,
14
+ decodeEventLog,
15
+ } from 'viem';
16
+
17
+ export interface L2Claim {
18
+ claimSecret: Fr;
19
+ claimAmount: Fr;
20
+ messageHash: Hex;
21
+ messageLeafIndex: bigint;
22
+ }
23
+
24
+ export class FormattedViemError extends Error {
25
+ metaMessages?: any[];
26
+
27
+ constructor(message: string, metaMessages?: any[]) {
28
+ super(message);
29
+ this.name = 'FormattedViemError';
30
+ this.metaMessages = metaMessages;
31
+ }
32
+ }
33
+
34
+ export function extractEvent<
35
+ const TAbi extends Abi | readonly unknown[],
36
+ TEventName extends ContractEventName<TAbi>,
37
+ TEventType = DecodeEventLogReturnType<TAbi, TEventName, Hex[], undefined, true>,
38
+ >(
39
+ logs: Log[],
40
+ address: Hex,
41
+ abi: TAbi,
42
+ eventName: TEventName,
43
+ filter?: (log: TEventType) => boolean,
44
+ logger?: Logger,
45
+ ): TEventType {
46
+ const event = tryExtractEvent(logs, address, abi, eventName, filter, logger);
47
+ if (!event) {
48
+ throw new Error(`Failed to find matching event ${eventName} for contract ${address}`);
49
+ }
50
+ return event;
51
+ }
52
+
53
+ function tryExtractEvent<
54
+ const TAbi extends Abi | readonly unknown[],
55
+ TEventName extends ContractEventName<TAbi>,
56
+ TEventType = DecodeEventLogReturnType<TAbi, TEventName, Hex[], undefined, true>,
57
+ >(
58
+ logs: Log[],
59
+ address: Hex,
60
+ abi: TAbi,
61
+ eventName: TEventName,
62
+ filter?: (log: TEventType) => boolean,
63
+ logger?: Logger,
64
+ ): TEventType | undefined {
65
+ for (const log of logs) {
66
+ if (log.address.toLowerCase() === address.toLowerCase()) {
67
+ try {
68
+ const decodedEvent = decodeEventLog({ abi, ...log });
69
+ if (decodedEvent.eventName === eventName) {
70
+ const matchingEvent = decodedEvent as TEventType;
71
+ if (!filter || filter(matchingEvent)) {
72
+ return matchingEvent;
73
+ }
74
+ }
75
+ } catch (err) {
76
+ logger?.warn(`Failed to decode event log for contract ${address}: ${err}`);
77
+ }
78
+ }
79
+ }
80
+ }
81
+
82
+ export function prettyLogViemErrorMsg(err: any) {
83
+ if (err instanceof BaseError) {
84
+ const revertError = err.walk(err => err instanceof ContractFunctionRevertedError);
85
+ if (revertError instanceof ContractFunctionRevertedError) {
86
+ const errorName = revertError.data?.errorName ?? '';
87
+ const args =
88
+ revertError.metaMessages && revertError.metaMessages?.length > 1 ? revertError.metaMessages[1].trimStart() : '';
89
+ return `${errorName}${args}`;
90
+ }
91
+ }
92
+ return err?.message ?? err;
93
+ }
94
+
95
+ function getNestedErrorData(error: unknown): string | undefined {
96
+ // If nothing, bail
97
+ if (!error) {
98
+ return undefined;
99
+ }
100
+
101
+ // If it's an object with a `data` property, return it
102
+ // (Remember to check TS type-safely or cast as needed)
103
+ if (typeof error === 'object' && error !== null && 'data' in error) {
104
+ const possibleData = (error as any).data;
105
+ if (typeof possibleData === 'string' && possibleData.startsWith('0x')) {
106
+ return possibleData;
107
+ }
108
+ }
109
+
110
+ // If it has a `cause`, recurse
111
+ if (typeof error === 'object' && error !== null && 'cause' in error) {
112
+ return getNestedErrorData((error as any).cause);
113
+ }
114
+
115
+ // Not found
116
+ return undefined;
117
+ }
118
+
119
+ /**
120
+ * Formats a Viem error into a FormattedViemError instance.
121
+ * @param error - The error to format.
122
+ * @param abi - The ABI to use for decoding.
123
+ * @returns A FormattedViemError instance.
124
+ */
125
+ export function formatViemError(error: any, abi: Abi = ErrorsAbi): FormattedViemError {
126
+ // If error is already a FormattedViemError, return it as is
127
+ if (error instanceof FormattedViemError) {
128
+ return error;
129
+ }
130
+
131
+ // First try to decode as a custom error using the ABI
132
+ try {
133
+ const data = getNestedErrorData(error);
134
+ if (data) {
135
+ // Try to decode the error data using the ABI
136
+ const decoded = decodeErrorResult({
137
+ abi,
138
+ data: data as Hex,
139
+ });
140
+ if (decoded) {
141
+ return new FormattedViemError(`${decoded.errorName}(${decoded.args?.join(', ') ?? ''})`, error?.metaMessages);
142
+ }
143
+ }
144
+
145
+ // If it's a BaseError, try to get the custom error through ContractFunctionRevertedError
146
+ if (error instanceof BaseError) {
147
+ const revertError = error.walk(err => err instanceof ContractFunctionRevertedError);
148
+
149
+ if (revertError instanceof ContractFunctionRevertedError) {
150
+ let errorName = revertError.data?.errorName;
151
+ if (!errorName) {
152
+ errorName = revertError.signature ?? '';
153
+ }
154
+ const args =
155
+ revertError.metaMessages && revertError.metaMessages?.length > 1
156
+ ? revertError.metaMessages[1].trimStart()
157
+ : '';
158
+ return new FormattedViemError(`${errorName}${args}`, error?.metaMessages);
159
+ }
160
+ }
161
+ } catch (decodeErr) {
162
+ // If decoding fails, we fall back to the original formatting
163
+ }
164
+
165
+ // If it's a regular Error instance, return it with its message
166
+ if (error instanceof Error) {
167
+ return error;
168
+ }
169
+
170
+ // Original formatting logic for non-custom errors
171
+ const truncateHex = (hex: string, length = 100) => {
172
+ if (!hex || typeof hex !== 'string') {
173
+ return hex;
174
+ }
175
+ if (!hex.startsWith('0x')) {
176
+ return hex;
177
+ }
178
+ if (hex.length <= length * 2) {
179
+ return hex;
180
+ }
181
+ return `${hex.slice(0, length)}...${hex.slice(-length)}`;
182
+ };
183
+
184
+ const formatRequestBody = (body: string) => {
185
+ try {
186
+ const parsed = JSON.parse(body);
187
+
188
+ // Recursively process all parameters that might contain hex strings
189
+ const processParams = (obj: any): any => {
190
+ if (Array.isArray(obj)) {
191
+ return obj.map(item => processParams(item));
192
+ }
193
+ if (typeof obj === 'object' && obj !== null) {
194
+ const result: any = {};
195
+ for (const [key, value] of Object.entries(obj)) {
196
+ result[key] = processParams(value);
197
+ }
198
+ return result;
199
+ }
200
+ if (typeof obj === 'string') {
201
+ if (obj.startsWith('0x')) {
202
+ return truncateHex(obj);
203
+ }
204
+ }
205
+ return obj;
206
+ };
207
+
208
+ // Process the entire request body
209
+ const processed = processParams(parsed);
210
+ return JSON.stringify(processed, null, 2);
211
+ } catch {
212
+ return body;
213
+ }
214
+ };
215
+
216
+ const truncateHexStringsInText = (text: string): string => {
217
+ const hexRegex = /\b0x[a-fA-F0-9]{10,}/g;
218
+ return text.replace(hexRegex, hex => truncateHex(hex));
219
+ };
220
+
221
+ const extractAndFormatRequestBody = (message: string): string => {
222
+ // First handle Request body JSON
223
+ const requestBodyRegex = /Request body: ({[\s\S]*?})\n/g;
224
+ let result = message.replace(requestBodyRegex, (match, body) => {
225
+ return `Request body: ${formatRequestBody(body)}\n`;
226
+ });
227
+
228
+ // Then handle Arguments section
229
+ const argsRegex = /((?:Request |Estimate Gas )?Arguments:[\s\S]*?(?=\n\n|$))/g;
230
+ result = result.replace(argsRegex, section => {
231
+ const lines = section.split('\n');
232
+ const processedLines = lines.map(line => {
233
+ // Check if line contains a colon followed by content
234
+ const colonIndex = line.indexOf(':');
235
+ if (colonIndex !== -1) {
236
+ const [prefix, content] = [line.slice(0, colonIndex + 1), line.slice(colonIndex + 1).trim()];
237
+ // If content contains a hex string, truncate it
238
+ if (content.includes('0x')) {
239
+ const hexMatches = content.match(/0x[a-fA-F0-9]+/g) || [];
240
+ let processedContent = content;
241
+ hexMatches.forEach(hex => {
242
+ processedContent = processedContent.replace(hex, truncateHex(hex));
243
+ });
244
+ return `${prefix} ${processedContent}`;
245
+ }
246
+ }
247
+ return line;
248
+ });
249
+ return processedLines.join('\n');
250
+ });
251
+
252
+ // Finally, catch any remaining hex strings in the message
253
+ result = truncateHexStringsInText(result);
254
+
255
+ return result;
256
+ };
257
+
258
+ const formattedRes = extractAndFormatRequestBody(error?.message || String(error));
259
+
260
+ return new FormattedViemError(formattedRes.replace(/\\n/g, '\n'), error?.metaMessages);
261
+ }
262
+
263
+ export function tryGetCustomErrorName(err: any) {
264
+ try {
265
+ // See https://viem.sh/docs/contract/simulateContract#handling-custom-errors
266
+ if (err.name === 'ViemError' || err.name === 'ContractFunctionExecutionError') {
267
+ const baseError = err as BaseError;
268
+ const revertError = baseError.walk(err => (err as Error).name === 'ContractFunctionRevertedError');
269
+ if (revertError) {
270
+ return (revertError as ContractFunctionRevertedError).data?.errorName;
271
+ }
272
+ }
273
+ } catch (_e) {
274
+ return undefined;
275
+ }
276
+ }