@gas-killer/analyzer-wasm 0.2.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.
@@ -0,0 +1,102 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ export interface AnalyzeTraceResult {
5
+ encoded_updates: string;
6
+ gas_estimate: number;
7
+ is_heuristic: boolean;
8
+ state_update_count: number;
9
+ skipped_opcodes: string[];
10
+ reentered: boolean;
11
+ }
12
+
13
+ export interface EncodeTraceResult {
14
+ encoded_updates: string;
15
+ state_update_count: number;
16
+ skipped_opcodes: string[];
17
+ }
18
+
19
+ export interface EstimateGasResult {
20
+ gas_estimate: number;
21
+ is_heuristic: boolean;
22
+ state_update_count: number;
23
+ skipped_opcodes: string[];
24
+ reentered: boolean;
25
+ }
26
+
27
+
28
+
29
+ /**
30
+ * Analyze a Geth trace: parse state updates, ABI-encode them, and estimate gas.
31
+ *
32
+ * `trace_json` is the JSON body of a debug_traceTransaction/debug_traceCall response
33
+ * (the `result` field, not the full JSON-RPC envelope).
34
+ *
35
+ * `estimator_address` is the hex address where the gas estimator contract will be
36
+ * deployed in the empty CacheDB, e.g. `"0x1234..."`.
37
+ *
38
+ * `caller_address` is the hex address of the original transaction sender, used as
39
+ * `tx.origin` during gas simulation.
40
+ *
41
+ * `origin_address` is the traced transaction's target contract; when supplied,
42
+ * callbacks re-entering it are detected and reported via `reentered`.
43
+ *
44
+ * Returns a JS object with: `encoded_updates`, `gas_estimate`, `is_heuristic`,
45
+ * `state_update_count`, `skipped_opcodes`, `reentered`.
46
+ */
47
+ export function analyze_trace(trace_json: string, estimator_address: string, caller_address: string, estimate_state_changes_block_number?: bigint | null, origin_address?: string | null): any;
48
+
49
+ /**
50
+ * Encode state updates only (no gas estimation).
51
+ */
52
+ export function encode_trace(trace_json: string): any;
53
+
54
+ /**
55
+ * Heuristic-only gas estimation (no revm, faster, less accurate).
56
+ *
57
+ * `origin_address` enables re-entry detection — see [`analyze_trace`].
58
+ */
59
+ export function estimate_gas_heuristic(trace_json: string, origin_address?: string | null): any;
60
+
61
+ /**
62
+ * Initialize panic hook for better error messages in browser console.
63
+ */
64
+ export function init(): void;
65
+
66
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
67
+
68
+ export interface InitOutput {
69
+ readonly memory: WebAssembly.Memory;
70
+ readonly analyze_trace: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: bigint, i: number, j: number) => [number, number, number];
71
+ readonly encode_trace: (a: number, b: number) => [number, number, number];
72
+ readonly estimate_gas_heuristic: (a: number, b: number, c: number, d: number) => [number, number, number];
73
+ readonly init: () => void;
74
+ readonly __wbindgen_malloc: (a: number, b: number) => number;
75
+ readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
76
+ readonly __wbindgen_free: (a: number, b: number, c: number) => void;
77
+ readonly __wbindgen_externrefs: WebAssembly.Table;
78
+ readonly __externref_table_dealloc: (a: number) => void;
79
+ readonly __wbindgen_start: () => void;
80
+ }
81
+
82
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
83
+
84
+ /**
85
+ * Instantiates the given `module`, which can either be bytes or
86
+ * a precompiled `WebAssembly.Module`.
87
+ *
88
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
89
+ *
90
+ * @returns {InitOutput}
91
+ */
92
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
93
+
94
+ /**
95
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
96
+ * for everything else, calls `WebAssembly.instantiate` directly.
97
+ *
98
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
99
+ *
100
+ * @returns {Promise<InitOutput>}
101
+ */
102
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
@@ -0,0 +1,353 @@
1
+ let wasm;
2
+
3
+ let cachedDataViewMemory0 = null;
4
+ function getDataViewMemory0() {
5
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
6
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
7
+ }
8
+ return cachedDataViewMemory0;
9
+ }
10
+
11
+ function getStringFromWasm0(ptr, len) {
12
+ ptr = ptr >>> 0;
13
+ return decodeText(ptr, len);
14
+ }
15
+
16
+ let cachedUint8ArrayMemory0 = null;
17
+ function getUint8ArrayMemory0() {
18
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
19
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
20
+ }
21
+ return cachedUint8ArrayMemory0;
22
+ }
23
+
24
+ function isLikeNone(x) {
25
+ return x === undefined || x === null;
26
+ }
27
+
28
+ function passStringToWasm0(arg, malloc, realloc) {
29
+ if (realloc === undefined) {
30
+ const buf = cachedTextEncoder.encode(arg);
31
+ const ptr = malloc(buf.length, 1) >>> 0;
32
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
33
+ WASM_VECTOR_LEN = buf.length;
34
+ return ptr;
35
+ }
36
+
37
+ let len = arg.length;
38
+ let ptr = malloc(len, 1) >>> 0;
39
+
40
+ const mem = getUint8ArrayMemory0();
41
+
42
+ let offset = 0;
43
+
44
+ for (; offset < len; offset++) {
45
+ const code = arg.charCodeAt(offset);
46
+ if (code > 0x7F) break;
47
+ mem[ptr + offset] = code;
48
+ }
49
+ if (offset !== len) {
50
+ if (offset !== 0) {
51
+ arg = arg.slice(offset);
52
+ }
53
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
54
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
55
+ const ret = cachedTextEncoder.encodeInto(arg, view);
56
+
57
+ offset += ret.written;
58
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
59
+ }
60
+
61
+ WASM_VECTOR_LEN = offset;
62
+ return ptr;
63
+ }
64
+
65
+ function takeFromExternrefTable0(idx) {
66
+ const value = wasm.__wbindgen_externrefs.get(idx);
67
+ wasm.__externref_table_dealloc(idx);
68
+ return value;
69
+ }
70
+
71
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
72
+ cachedTextDecoder.decode();
73
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
74
+ let numBytesDecoded = 0;
75
+ function decodeText(ptr, len) {
76
+ numBytesDecoded += len;
77
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
78
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
79
+ cachedTextDecoder.decode();
80
+ numBytesDecoded = len;
81
+ }
82
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
83
+ }
84
+
85
+ const cachedTextEncoder = new TextEncoder();
86
+
87
+ if (!('encodeInto' in cachedTextEncoder)) {
88
+ cachedTextEncoder.encodeInto = function (arg, view) {
89
+ const buf = cachedTextEncoder.encode(arg);
90
+ view.set(buf);
91
+ return {
92
+ read: arg.length,
93
+ written: buf.length
94
+ };
95
+ }
96
+ }
97
+
98
+ let WASM_VECTOR_LEN = 0;
99
+
100
+ /**
101
+ * Analyze a Geth trace: parse state updates, ABI-encode them, and estimate gas.
102
+ *
103
+ * `trace_json` is the JSON body of a debug_traceTransaction/debug_traceCall response
104
+ * (the `result` field, not the full JSON-RPC envelope).
105
+ *
106
+ * `estimator_address` is the hex address where the gas estimator contract will be
107
+ * deployed in the empty CacheDB, e.g. `"0x1234..."`.
108
+ *
109
+ * `caller_address` is the hex address of the original transaction sender, used as
110
+ * `tx.origin` during gas simulation.
111
+ *
112
+ * `origin_address` is the traced transaction's target contract; when supplied,
113
+ * callbacks re-entering it are detected and reported via `reentered`.
114
+ *
115
+ * Returns a JS object with: `encoded_updates`, `gas_estimate`, `is_heuristic`,
116
+ * `state_update_count`, `skipped_opcodes`, `reentered`.
117
+ * @param {string} trace_json
118
+ * @param {string} estimator_address
119
+ * @param {string} caller_address
120
+ * @param {bigint | null} [estimate_state_changes_block_number]
121
+ * @param {string | null} [origin_address]
122
+ * @returns {any}
123
+ */
124
+ export function analyze_trace(trace_json, estimator_address, caller_address, estimate_state_changes_block_number, origin_address) {
125
+ const ptr0 = passStringToWasm0(trace_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
126
+ const len0 = WASM_VECTOR_LEN;
127
+ const ptr1 = passStringToWasm0(estimator_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
128
+ const len1 = WASM_VECTOR_LEN;
129
+ const ptr2 = passStringToWasm0(caller_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
130
+ const len2 = WASM_VECTOR_LEN;
131
+ var ptr3 = isLikeNone(origin_address) ? 0 : passStringToWasm0(origin_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
132
+ var len3 = WASM_VECTOR_LEN;
133
+ const ret = wasm.analyze_trace(ptr0, len0, ptr1, len1, ptr2, len2, !isLikeNone(estimate_state_changes_block_number), isLikeNone(estimate_state_changes_block_number) ? BigInt(0) : estimate_state_changes_block_number, ptr3, len3);
134
+ if (ret[2]) {
135
+ throw takeFromExternrefTable0(ret[1]);
136
+ }
137
+ return takeFromExternrefTable0(ret[0]);
138
+ }
139
+
140
+ /**
141
+ * Encode state updates only (no gas estimation).
142
+ * @param {string} trace_json
143
+ * @returns {any}
144
+ */
145
+ export function encode_trace(trace_json) {
146
+ const ptr0 = passStringToWasm0(trace_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
147
+ const len0 = WASM_VECTOR_LEN;
148
+ const ret = wasm.encode_trace(ptr0, len0);
149
+ if (ret[2]) {
150
+ throw takeFromExternrefTable0(ret[1]);
151
+ }
152
+ return takeFromExternrefTable0(ret[0]);
153
+ }
154
+
155
+ /**
156
+ * Heuristic-only gas estimation (no revm, faster, less accurate).
157
+ *
158
+ * `origin_address` enables re-entry detection — see [`analyze_trace`].
159
+ * @param {string} trace_json
160
+ * @param {string | null} [origin_address]
161
+ * @returns {any}
162
+ */
163
+ export function estimate_gas_heuristic(trace_json, origin_address) {
164
+ const ptr0 = passStringToWasm0(trace_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
165
+ const len0 = WASM_VECTOR_LEN;
166
+ var ptr1 = isLikeNone(origin_address) ? 0 : passStringToWasm0(origin_address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
167
+ var len1 = WASM_VECTOR_LEN;
168
+ const ret = wasm.estimate_gas_heuristic(ptr0, len0, ptr1, len1);
169
+ if (ret[2]) {
170
+ throw takeFromExternrefTable0(ret[1]);
171
+ }
172
+ return takeFromExternrefTable0(ret[0]);
173
+ }
174
+
175
+ /**
176
+ * Initialize panic hook for better error messages in browser console.
177
+ */
178
+ export function init() {
179
+ wasm.init();
180
+ }
181
+
182
+ const EXPECTED_RESPONSE_TYPES = new Set(['basic', 'cors', 'default']);
183
+
184
+ async function __wbg_load(module, imports) {
185
+ if (typeof Response === 'function' && module instanceof Response) {
186
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
187
+ try {
188
+ return await WebAssembly.instantiateStreaming(module, imports);
189
+ } catch (e) {
190
+ const validResponse = module.ok && EXPECTED_RESPONSE_TYPES.has(module.type);
191
+
192
+ if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
193
+ console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
194
+
195
+ } else {
196
+ throw e;
197
+ }
198
+ }
199
+ }
200
+
201
+ const bytes = await module.arrayBuffer();
202
+ return await WebAssembly.instantiate(bytes, imports);
203
+ } else {
204
+ const instance = await WebAssembly.instantiate(module, imports);
205
+
206
+ if (instance instanceof WebAssembly.Instance) {
207
+ return { instance, module };
208
+ } else {
209
+ return instance;
210
+ }
211
+ }
212
+ }
213
+
214
+ function __wbg_get_imports() {
215
+ const imports = {};
216
+ imports.wbg = {};
217
+ imports.wbg.__wbg_Error_52673b7de5a0ca89 = function(arg0, arg1) {
218
+ const ret = Error(getStringFromWasm0(arg0, arg1));
219
+ return ret;
220
+ };
221
+ imports.wbg.__wbg_String_8f0eb39a4a4c2f66 = function(arg0, arg1) {
222
+ const ret = String(arg1);
223
+ const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
224
+ const len1 = WASM_VECTOR_LEN;
225
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
226
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
227
+ };
228
+ imports.wbg.__wbg___wbindgen_throw_dd24417ed36fc46e = function(arg0, arg1) {
229
+ throw new Error(getStringFromWasm0(arg0, arg1));
230
+ };
231
+ imports.wbg.__wbg_error_7534b8e9a36f1ab4 = function(arg0, arg1) {
232
+ let deferred0_0;
233
+ let deferred0_1;
234
+ try {
235
+ deferred0_0 = arg0;
236
+ deferred0_1 = arg1;
237
+ console.error(getStringFromWasm0(arg0, arg1));
238
+ } finally {
239
+ wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
240
+ }
241
+ };
242
+ imports.wbg.__wbg_new_1ba21ce319a06297 = function() {
243
+ const ret = new Object();
244
+ return ret;
245
+ };
246
+ imports.wbg.__wbg_new_25f239778d6112b9 = function() {
247
+ const ret = new Array();
248
+ return ret;
249
+ };
250
+ imports.wbg.__wbg_new_8a6f238a6ece86ea = function() {
251
+ const ret = new Error();
252
+ return ret;
253
+ };
254
+ imports.wbg.__wbg_set_3f1d0b984ed272ed = function(arg0, arg1, arg2) {
255
+ arg0[arg1] = arg2;
256
+ };
257
+ imports.wbg.__wbg_set_7df433eea03a5c14 = function(arg0, arg1, arg2) {
258
+ arg0[arg1 >>> 0] = arg2;
259
+ };
260
+ imports.wbg.__wbg_stack_0ed75d68575b0f3c = function(arg0, arg1) {
261
+ const ret = arg1.stack;
262
+ const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
263
+ const len1 = WASM_VECTOR_LEN;
264
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
265
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
266
+ };
267
+ imports.wbg.__wbindgen_cast_2241b6af4c4b2941 = function(arg0, arg1) {
268
+ // Cast intrinsic for `Ref(String) -> Externref`.
269
+ const ret = getStringFromWasm0(arg0, arg1);
270
+ return ret;
271
+ };
272
+ imports.wbg.__wbindgen_cast_4625c577ab2ec9ee = function(arg0) {
273
+ // Cast intrinsic for `U64 -> Externref`.
274
+ const ret = BigInt.asUintN(64, arg0);
275
+ return ret;
276
+ };
277
+ imports.wbg.__wbindgen_cast_d6cd19b81560fd6e = function(arg0) {
278
+ // Cast intrinsic for `F64 -> Externref`.
279
+ const ret = arg0;
280
+ return ret;
281
+ };
282
+ imports.wbg.__wbindgen_init_externref_table = function() {
283
+ const table = wasm.__wbindgen_externrefs;
284
+ const offset = table.grow(4);
285
+ table.set(0, undefined);
286
+ table.set(offset + 0, undefined);
287
+ table.set(offset + 1, null);
288
+ table.set(offset + 2, true);
289
+ table.set(offset + 3, false);
290
+ };
291
+
292
+ return imports;
293
+ }
294
+
295
+ function __wbg_finalize_init(instance, module) {
296
+ wasm = instance.exports;
297
+ __wbg_init.__wbindgen_wasm_module = module;
298
+ cachedDataViewMemory0 = null;
299
+ cachedUint8ArrayMemory0 = null;
300
+
301
+
302
+ wasm.__wbindgen_start();
303
+ return wasm;
304
+ }
305
+
306
+ function initSync(module) {
307
+ if (wasm !== undefined) return wasm;
308
+
309
+
310
+ if (typeof module !== 'undefined') {
311
+ if (Object.getPrototypeOf(module) === Object.prototype) {
312
+ ({module} = module)
313
+ } else {
314
+ console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
315
+ }
316
+ }
317
+
318
+ const imports = __wbg_get_imports();
319
+ if (!(module instanceof WebAssembly.Module)) {
320
+ module = new WebAssembly.Module(module);
321
+ }
322
+ const instance = new WebAssembly.Instance(module, imports);
323
+ return __wbg_finalize_init(instance, module);
324
+ }
325
+
326
+ async function __wbg_init(module_or_path) {
327
+ if (wasm !== undefined) return wasm;
328
+
329
+
330
+ if (typeof module_or_path !== 'undefined') {
331
+ if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
332
+ ({module_or_path} = module_or_path)
333
+ } else {
334
+ console.warn('using deprecated parameters for the initialization function; pass a single object instead')
335
+ }
336
+ }
337
+
338
+ if (typeof module_or_path === 'undefined') {
339
+ module_or_path = new URL('gas_killer_wasm_bg.wasm', import.meta.url);
340
+ }
341
+ const imports = __wbg_get_imports();
342
+
343
+ if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
344
+ module_or_path = fetch(module_or_path);
345
+ }
346
+
347
+ const { instance, module } = await __wbg_load(await module_or_path, imports);
348
+
349
+ return __wbg_finalize_init(instance, module);
350
+ }
351
+
352
+ export { initSync };
353
+ export default __wbg_init;
Binary file
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@gas-killer/analyzer-wasm",
3
+ "type": "module",
4
+ "version": "0.2.0",
5
+ "files": [
6
+ "gas_killer_wasm_bg.wasm",
7
+ "gas_killer_wasm.js",
8
+ "gas_killer_wasm.d.ts"
9
+ ],
10
+ "main": "gas_killer_wasm.js",
11
+ "types": "gas_killer_wasm.d.ts",
12
+ "sideEffects": [
13
+ "./snippets/*"
14
+ ],
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/gas-killer/gas-analyzer.git"
18
+ }
19
+ }