@vleap/warps-adapter-evm 0.2.0-alpha.2

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.
package/dist/index.js ADDED
@@ -0,0 +1,793 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ EVM_CHAIN_CONFIGS: () => EVM_CHAIN_CONFIGS,
24
+ WarpEvmBuilder: () => WarpEvmBuilder,
25
+ WarpEvmConstants: () => WarpEvmConstants,
26
+ WarpEvmExecutor: () => WarpEvmExecutor,
27
+ WarpEvmExplorer: () => WarpEvmExplorer,
28
+ WarpEvmResults: () => WarpEvmResults,
29
+ WarpEvmSerializer: () => WarpEvmSerializer,
30
+ getEvmAdapter: () => getEvmAdapter,
31
+ getEvmApiUrl: () => getEvmApiUrl,
32
+ getEvmBlockTime: () => getEvmBlockTime,
33
+ getEvmChainConfig: () => getEvmChainConfig,
34
+ getEvmChainId: () => getEvmChainId,
35
+ getEvmExplorerUrl: () => getEvmExplorerUrl,
36
+ getEvmNativeToken: () => getEvmNativeToken,
37
+ getEvmRegistryAddress: () => getEvmRegistryAddress,
38
+ getSupportedEnvironments: () => getSupportedEnvironments,
39
+ getSupportedEvmChains: () => getSupportedEvmChains
40
+ });
41
+ module.exports = __toCommonJS(index_exports);
42
+
43
+ // src/config.ts
44
+ var EVM_CHAIN_CONFIGS = {
45
+ ethereum: {
46
+ mainnet: {
47
+ apiUrl: "https://eth-mainnet.g.alchemy.com/v2/demo",
48
+ explorerUrl: "https://etherscan.io",
49
+ chainId: "1",
50
+ registryAddress: "0x0000000000000000000000000000000000000000",
51
+ nativeToken: "ETH",
52
+ blockTime: 12
53
+ },
54
+ testnet: {
55
+ apiUrl: "https://eth-sepolia.g.alchemy.com/v2/demo",
56
+ explorerUrl: "https://sepolia.etherscan.io",
57
+ chainId: "11155111",
58
+ registryAddress: "0x0000000000000000000000000000000000000000",
59
+ nativeToken: "ETH",
60
+ blockTime: 12
61
+ },
62
+ devnet: {
63
+ apiUrl: "http://localhost:8545",
64
+ explorerUrl: "http://localhost:4000",
65
+ chainId: "1337",
66
+ registryAddress: "0x0000000000000000000000000000000000000000",
67
+ nativeToken: "ETH",
68
+ blockTime: 12
69
+ }
70
+ },
71
+ arbitrum: {
72
+ mainnet: {
73
+ apiUrl: "https://arb-mainnet.g.alchemy.com/v2/demo",
74
+ explorerUrl: "https://arbiscan.io",
75
+ chainId: "42161",
76
+ registryAddress: "0x0000000000000000000000000000000000000000",
77
+ nativeToken: "ETH",
78
+ blockTime: 1
79
+ },
80
+ testnet: {
81
+ apiUrl: "https://arb-sepolia.g.alchemy.com/v2/demo",
82
+ explorerUrl: "https://sepolia.arbiscan.io",
83
+ chainId: "421614",
84
+ registryAddress: "0x0000000000000000000000000000000000000000",
85
+ nativeToken: "ETH",
86
+ blockTime: 1
87
+ },
88
+ devnet: {
89
+ apiUrl: "http://localhost:8545",
90
+ explorerUrl: "http://localhost:4000",
91
+ chainId: "1337",
92
+ registryAddress: "0x0000000000000000000000000000000000000000",
93
+ nativeToken: "ETH",
94
+ blockTime: 1
95
+ }
96
+ },
97
+ base: {
98
+ mainnet: {
99
+ apiUrl: "https://mainnet.base.org",
100
+ explorerUrl: "https://basescan.org",
101
+ chainId: "8453",
102
+ registryAddress: "0x0000000000000000000000000000000000000000",
103
+ nativeToken: "ETH",
104
+ blockTime: 2
105
+ },
106
+ testnet: {
107
+ apiUrl: "https://sepolia.base.org",
108
+ explorerUrl: "https://sepolia.basescan.org",
109
+ chainId: "84532",
110
+ registryAddress: "0x0000000000000000000000000000000000000000",
111
+ nativeToken: "ETH",
112
+ blockTime: 2
113
+ },
114
+ devnet: {
115
+ apiUrl: "http://localhost:8545",
116
+ explorerUrl: "http://localhost:4000",
117
+ chainId: "1337",
118
+ registryAddress: "0x0000000000000000000000000000000000000000",
119
+ nativeToken: "ETH",
120
+ blockTime: 2
121
+ }
122
+ }
123
+ };
124
+ var DEFAULT_CHAIN = "ethereum";
125
+ var getEvmChainConfig = (chain = DEFAULT_CHAIN, env) => {
126
+ const chainConfigs = EVM_CHAIN_CONFIGS[chain];
127
+ if (!chainConfigs) {
128
+ throw new Error(`Unsupported EVM chain: ${chain}`);
129
+ }
130
+ const config = chainConfigs[env];
131
+ if (!config) {
132
+ throw new Error(`Unsupported environment ${env} for chain ${chain}`);
133
+ }
134
+ return config;
135
+ };
136
+ var getEvmApiUrl = (env, chain = DEFAULT_CHAIN) => {
137
+ return getEvmChainConfig(chain, env).apiUrl;
138
+ };
139
+ var getEvmExplorerUrl = (env, chain = DEFAULT_CHAIN) => {
140
+ return getEvmChainConfig(chain, env).explorerUrl;
141
+ };
142
+ var getEvmChainId = (env, chain = DEFAULT_CHAIN) => {
143
+ return getEvmChainConfig(chain, env).chainId;
144
+ };
145
+ var getEvmRegistryAddress = (env, chain = DEFAULT_CHAIN) => {
146
+ return getEvmChainConfig(chain, env).registryAddress;
147
+ };
148
+ var getEvmNativeToken = (env, chain = DEFAULT_CHAIN) => {
149
+ return getEvmChainConfig(chain, env).nativeToken;
150
+ };
151
+ var getEvmBlockTime = (env, chain = DEFAULT_CHAIN) => {
152
+ return getEvmChainConfig(chain, env).blockTime || 12;
153
+ };
154
+ var getSupportedEvmChains = () => {
155
+ return Object.keys(EVM_CHAIN_CONFIGS);
156
+ };
157
+ var getSupportedEnvironments = (chain) => {
158
+ const chainConfigs = EVM_CHAIN_CONFIGS[chain];
159
+ if (!chainConfigs) {
160
+ return [];
161
+ }
162
+ return Object.keys(chainConfigs);
163
+ };
164
+
165
+ // src/constants.ts
166
+ var WarpEvmConstants = {
167
+ ChainName: "evm",
168
+ ChainPrefix: "evm",
169
+ Ether: {
170
+ Identifier: "ETH",
171
+ DisplayName: "Ether",
172
+ Decimals: 18
173
+ },
174
+ GasLimit: {
175
+ Default: 21e3,
176
+ ContractCall: 1e5,
177
+ ContractDeploy: 5e5,
178
+ Transfer: 21e3,
179
+ Approve: 46e3,
180
+ Swap: 2e5
181
+ },
182
+ GasPrice: {
183
+ Default: "20000000000",
184
+ // 20 gwei
185
+ Low: "10000000000",
186
+ // 10 gwei
187
+ Medium: "20000000000",
188
+ // 20 gwei
189
+ High: "50000000000"
190
+ // 50 gwei
191
+ },
192
+ Network: {
193
+ Ethereum: {
194
+ ChainId: "1",
195
+ Name: "Ethereum",
196
+ BlockTime: 12
197
+ },
198
+ Arbitrum: {
199
+ ChainId: "42161",
200
+ Name: "Arbitrum",
201
+ BlockTime: 1
202
+ },
203
+ Base: {
204
+ ChainId: "8453",
205
+ Name: "Base",
206
+ BlockTime: 2
207
+ }
208
+ },
209
+ Validation: {
210
+ AddressLength: 42,
211
+ HexPrefix: "0x",
212
+ MinGasLimit: 21e3,
213
+ MaxGasLimit: 3e7
214
+ },
215
+ Timeouts: {
216
+ DefaultRpcTimeout: 3e4,
217
+ // 30 seconds
218
+ GasEstimationTimeout: 1e4,
219
+ // 10 seconds
220
+ QueryTimeout: 15e3
221
+ // 15 seconds
222
+ }
223
+ };
224
+
225
+ // src/WarpEvmBuilder.ts
226
+ var import_ethers = require("ethers");
227
+ var WarpEvmBuilder = class {
228
+ constructor(config) {
229
+ this.config = config;
230
+ this.warp = {};
231
+ this.actions = [];
232
+ }
233
+ async createFromRaw(encoded) {
234
+ try {
235
+ const decoded = JSON.parse(encoded);
236
+ return decoded;
237
+ } catch (error) {
238
+ throw new Error(`Failed to decode warp from raw data: ${error}`);
239
+ }
240
+ }
241
+ setTitle(title) {
242
+ this.warp.title = title;
243
+ return this;
244
+ }
245
+ setDescription(description) {
246
+ this.warp.description = description;
247
+ return this;
248
+ }
249
+ setPreview(preview) {
250
+ this.warp.preview = preview;
251
+ return this;
252
+ }
253
+ setActions(actions) {
254
+ this.actions = actions;
255
+ return this;
256
+ }
257
+ addAction(action) {
258
+ this.actions.push(action);
259
+ return this;
260
+ }
261
+ async build() {
262
+ if (!this.warp.title) {
263
+ throw new Error("Warp title is required");
264
+ }
265
+ return {
266
+ protocol: "warp",
267
+ name: this.warp.name || "evm-warp",
268
+ title: this.warp.title,
269
+ description: this.warp.description || null,
270
+ preview: this.warp.preview || null,
271
+ actions: this.actions,
272
+ meta: {
273
+ chain: "evm",
274
+ hash: import_ethers.ethers.keccak256(import_ethers.ethers.toUtf8Bytes(this.warp.title)),
275
+ creator: this.config.user?.wallets?.evm || "",
276
+ createdAt: (/* @__PURE__ */ new Date()).toISOString()
277
+ }
278
+ };
279
+ }
280
+ createInscriptionTransaction(warp) {
281
+ const warpData = JSON.stringify(warp);
282
+ const data = import_ethers.ethers.toUtf8Bytes(warpData);
283
+ return {
284
+ data: import_ethers.ethers.hexlify(data)
285
+ };
286
+ }
287
+ async createFromTransaction(tx, validate) {
288
+ if (!tx.data || tx.data === "0x") {
289
+ throw new Error("Transaction has no data");
290
+ }
291
+ try {
292
+ const data = import_ethers.ethers.toUtf8String(tx.data);
293
+ const warp = JSON.parse(data);
294
+ if (validate) {
295
+ if (!warp.protocol || warp.protocol !== "warp") {
296
+ throw new Error("Invalid warp protocol");
297
+ }
298
+ if (!warp.title) {
299
+ throw new Error("Warp title is required");
300
+ }
301
+ }
302
+ return warp;
303
+ } catch (error) {
304
+ throw new Error(`Failed to create warp from transaction: ${error}`);
305
+ }
306
+ }
307
+ async createFromTransactionHash(hash, cache) {
308
+ try {
309
+ const provider = new import_ethers.ethers.JsonRpcProvider(getEvmApiUrl(this.config.env));
310
+ const tx = await provider.getTransaction(hash);
311
+ if (!tx) {
312
+ return null;
313
+ }
314
+ return await this.createFromTransaction(tx);
315
+ } catch (error) {
316
+ return null;
317
+ }
318
+ }
319
+ };
320
+
321
+ // src/WarpEvmExecutor.ts
322
+ var import_warps3 = require("@vleap/warps");
323
+ var import_ethers3 = require("ethers");
324
+
325
+ // src/WarpEvmResults.ts
326
+ var import_warps2 = require("@vleap/warps");
327
+
328
+ // src/WarpEvmSerializer.ts
329
+ var import_warps = require("@vleap/warps");
330
+ var import_ethers2 = require("ethers");
331
+ var SplitParamsRegex = new RegExp(`${import_warps.WarpConstants.ArgParamsSeparator}(.*)`);
332
+ var WarpEvmSerializer = class {
333
+ constructor() {
334
+ this.coreSerializer = new import_warps.WarpSerializer();
335
+ }
336
+ typedToString(value) {
337
+ if (typeof value === "string") {
338
+ if (import_ethers2.ethers.isAddress(value)) {
339
+ return `address:${value}`;
340
+ }
341
+ if (import_ethers2.ethers.isHexString(value) && !import_ethers2.ethers.isAddress(value)) {
342
+ return `hex:${value}`;
343
+ }
344
+ return `string:${value}`;
345
+ }
346
+ if (typeof value === "number") {
347
+ if (Number.isInteger(value)) {
348
+ if (value >= 0 && value <= 255) return `uint8:${value}`;
349
+ if (value >= 0 && value <= 65535) return `uint16:${value}`;
350
+ if (value >= 0 && value <= 4294967295) return `uint32:${value}`;
351
+ return `uint64:${value}`;
352
+ }
353
+ return `string:${value}`;
354
+ }
355
+ if (typeof value === "bigint") {
356
+ return `biguint:${value.toString()}`;
357
+ }
358
+ if (typeof value === "boolean") {
359
+ return `boolean:${value}`;
360
+ }
361
+ if (Array.isArray(value)) {
362
+ if (value.length === 0) return `list:string:`;
363
+ const types = value.map((item) => this.typedToString(item).split(import_warps.WarpConstants.ArgParamsSeparator)[0]);
364
+ const type = types[0];
365
+ const values = value.map((item) => this.typedToString(item).split(import_warps.WarpConstants.ArgParamsSeparator)[1]);
366
+ return `list:${type}:${values.join(",")}`;
367
+ }
368
+ if (value === null || value === void 0) {
369
+ return `string:null`;
370
+ }
371
+ return `string:${String(value)}`;
372
+ }
373
+ typedToNative(value) {
374
+ const stringValue = this.typedToString(value);
375
+ const [type, ...valueParts] = stringValue.split(import_warps.WarpConstants.ArgParamsSeparator);
376
+ const nativeValue = valueParts.join(import_warps.WarpConstants.ArgParamsSeparator);
377
+ return [type, this.parseNativeValue(type, nativeValue)];
378
+ }
379
+ nativeToTyped(type, value) {
380
+ switch (type) {
381
+ case "string":
382
+ return String(value);
383
+ case "uint8":
384
+ case "uint16":
385
+ case "uint32":
386
+ case "uint64":
387
+ return BigInt(value);
388
+ case "biguint":
389
+ return BigInt(value);
390
+ case "boolean":
391
+ return Boolean(value);
392
+ case "address":
393
+ return String(value);
394
+ case "hex":
395
+ return String(value);
396
+ default:
397
+ if (type.startsWith("list:")) {
398
+ const [, itemType, itemsStr] = type.split(":");
399
+ if (!itemsStr) return [];
400
+ const items = itemsStr.split(",");
401
+ return items.map((item) => this.nativeToTyped(itemType, item));
402
+ }
403
+ return String(value);
404
+ }
405
+ }
406
+ nativeToType(type) {
407
+ switch (type) {
408
+ case "string":
409
+ return "string";
410
+ case "uint8":
411
+ case "uint16":
412
+ case "uint32":
413
+ case "uint64":
414
+ case "biguint":
415
+ return "bigint";
416
+ case "boolean":
417
+ return "boolean";
418
+ case "address":
419
+ return "string";
420
+ case "hex":
421
+ return "string";
422
+ default:
423
+ return "string";
424
+ }
425
+ }
426
+ stringToTyped(value) {
427
+ const parts = value.split(import_warps.WarpConstants.ArgParamsSeparator, 2);
428
+ if (parts.length < 2) {
429
+ return value;
430
+ }
431
+ const [type, stringValue] = parts;
432
+ switch (type) {
433
+ case "string":
434
+ return stringValue;
435
+ case "uint8":
436
+ case "uint16":
437
+ case "uint32":
438
+ case "uint64":
439
+ return BigInt(stringValue);
440
+ case "biguint":
441
+ return BigInt(stringValue);
442
+ case "boolean":
443
+ return stringValue === "true";
444
+ case "address":
445
+ return stringValue;
446
+ case "hex":
447
+ return stringValue;
448
+ default:
449
+ if (type.startsWith("list:")) {
450
+ const [, itemType, itemsStr] = type.split(":");
451
+ if (!itemsStr) return [];
452
+ const items = itemsStr.split(",");
453
+ return items.map((item) => this.stringToTyped(`${itemType}:${item}`));
454
+ }
455
+ return stringValue;
456
+ }
457
+ }
458
+ parseNativeValue(type, value) {
459
+ switch (type) {
460
+ case "string":
461
+ return value;
462
+ case "uint8":
463
+ case "uint16":
464
+ case "uint32":
465
+ case "uint64":
466
+ case "biguint":
467
+ return BigInt(value);
468
+ case "boolean":
469
+ return value === "true";
470
+ case "address":
471
+ return value;
472
+ case "hex":
473
+ return value;
474
+ default:
475
+ return value;
476
+ }
477
+ }
478
+ };
479
+
480
+ // src/WarpEvmResults.ts
481
+ var WarpEvmResults = class {
482
+ constructor(config) {
483
+ this.config = config;
484
+ this.serializer = new WarpEvmSerializer();
485
+ }
486
+ async getTransactionExecutionResults(warp, tx) {
487
+ const success = tx.status === 1;
488
+ const gasUsed = tx.gasUsed?.toString() || "0";
489
+ const gasPrice = tx.gasPrice?.toString() || "0";
490
+ const blockNumber = tx.blockNumber?.toString() || "0";
491
+ const transactionHash = tx.hash;
492
+ const logs = tx.logs.map((log) => ({
493
+ address: log.address,
494
+ topics: log.topics,
495
+ data: log.data,
496
+ blockNumber: log.blockNumber?.toString() || "0",
497
+ transactionHash: log.transactionHash,
498
+ index: log.index?.toString() || "0"
499
+ }));
500
+ return {
501
+ success,
502
+ warp,
503
+ action: 0,
504
+ user: this.config.user?.wallets?.evm || null,
505
+ txHash: transactionHash,
506
+ next: null,
507
+ values: [transactionHash, blockNumber, gasUsed, gasPrice, ...logs.length > 0 ? logs : []],
508
+ results: {},
509
+ messages: {}
510
+ };
511
+ }
512
+ async extractQueryResults(warp, typedValues, actionIndex, inputs) {
513
+ const values = typedValues.map((t) => this.serializer.typedToString(t));
514
+ const valuesRaw = typedValues.map((t) => this.serializer.typedToNative(t)[1]);
515
+ let results = {};
516
+ if (!warp.results) return { values, results };
517
+ const getNestedValue = (path) => {
518
+ const indices = path.split(".").slice(1).map((i) => parseInt(i) - 1);
519
+ if (indices.length === 0) return void 0;
520
+ let value = valuesRaw[indices[0]];
521
+ for (let i = 1; i < indices.length; i++) {
522
+ if (value === void 0 || value === null) return void 0;
523
+ value = value[indices[i]];
524
+ }
525
+ return value;
526
+ };
527
+ for (const [key, path] of Object.entries(warp.results)) {
528
+ if (path.startsWith(import_warps2.WarpConstants.Transform.Prefix)) continue;
529
+ const currentActionIndex = (0, import_warps2.parseResultsOutIndex)(path);
530
+ if (currentActionIndex !== null && currentActionIndex !== actionIndex) {
531
+ results[key] = null;
532
+ continue;
533
+ }
534
+ if (path.startsWith("out.") || path === "out" || path.startsWith("out[")) {
535
+ results[key] = getNestedValue(path) || null;
536
+ } else {
537
+ results[key] = path;
538
+ }
539
+ }
540
+ return { values, results: await (0, import_warps2.evaluateResultsCommon)(warp, results, actionIndex, inputs) };
541
+ }
542
+ };
543
+
544
+ // src/WarpEvmExecutor.ts
545
+ var WarpEvmExecutor = class {
546
+ constructor(config) {
547
+ this.config = config;
548
+ this.serializer = new WarpEvmSerializer();
549
+ this.provider = new import_ethers3.ethers.JsonRpcProvider(getEvmApiUrl(config.env));
550
+ this.results = new WarpEvmResults(config);
551
+ }
552
+ async createTransaction(executable) {
553
+ const action = (0, import_warps3.getWarpActionByIndex)(executable.warp, executable.action);
554
+ let tx = null;
555
+ if (action.type === "transfer") {
556
+ tx = await this.createTransferTransaction(executable);
557
+ } else if (action.type === "contract") {
558
+ tx = await this.createContractCallTransaction(executable);
559
+ } else if (action.type === "query") {
560
+ throw new Error("WarpEvmExecutor: Invalid action type for createTransaction; Use executeQuery instead");
561
+ } else if (action.type === "collect") {
562
+ throw new Error("WarpEvmExecutor: Invalid action type for createTransaction; Use executeCollect instead");
563
+ }
564
+ if (!tx) throw new Error(`WarpEvmExecutor: Invalid action type (${action.type})`);
565
+ return tx;
566
+ }
567
+ async createTransferTransaction(executable) {
568
+ const userWallet = this.config.user?.wallets?.[executable.chain.name];
569
+ if (!userWallet) throw new Error("WarpEvmExecutor: createTransfer - user address not set");
570
+ if (!import_ethers3.ethers.isAddress(executable.destination)) {
571
+ throw new Error(`WarpEvmExecutor: Invalid destination address: ${executable.destination}`);
572
+ }
573
+ if (executable.value < 0) {
574
+ throw new Error(`WarpEvmExecutor: Transfer value cannot be negative: ${executable.value}`);
575
+ }
576
+ const tx = {
577
+ to: executable.destination,
578
+ value: executable.value,
579
+ data: executable.data ? this.serializer.stringToTyped(executable.data) : "0x"
580
+ };
581
+ return this.estimateGasAndSetDefaults(tx, userWallet);
582
+ }
583
+ async createContractCallTransaction(executable) {
584
+ const userWallet = this.config.user?.wallets?.[executable.chain.name];
585
+ if (!userWallet) throw new Error("WarpEvmExecutor: createContractCall - user address not set");
586
+ const action = (0, import_warps3.getWarpActionByIndex)(executable.warp, executable.action);
587
+ if (!action || !("func" in action) || !action.func) {
588
+ throw new Error("WarpEvmExecutor: Contract action must have a function name");
589
+ }
590
+ if (!import_ethers3.ethers.isAddress(executable.destination)) {
591
+ throw new Error(`WarpEvmExecutor: Invalid contract address: ${executable.destination}`);
592
+ }
593
+ if (executable.value < 0) {
594
+ throw new Error(`WarpEvmExecutor: Contract call value cannot be negative: ${executable.value}`);
595
+ }
596
+ try {
597
+ const iface = new import_ethers3.ethers.Interface([`function ${action.func}`]);
598
+ const encodedData = iface.encodeFunctionData(action.func, executable.args);
599
+ const tx = {
600
+ to: executable.destination,
601
+ value: executable.value,
602
+ data: encodedData
603
+ };
604
+ return this.estimateGasAndSetDefaults(tx, userWallet);
605
+ } catch (error) {
606
+ throw new Error(`WarpEvmExecutor: Failed to encode function data for ${action.func}: ${error}`);
607
+ }
608
+ }
609
+ async executeQuery(executable) {
610
+ const action = (0, import_warps3.getWarpActionByIndex)(executable.warp, executable.action);
611
+ if (action.type !== "query") {
612
+ throw new Error(`WarpEvmExecutor: Invalid action type for executeQuery: ${action.type}`);
613
+ }
614
+ if (!action.func) {
615
+ throw new Error("WarpEvmExecutor: Query action must have a function name");
616
+ }
617
+ if (!import_ethers3.ethers.isAddress(executable.destination)) {
618
+ throw new Error(`WarpEvmExecutor: Invalid contract address for query: ${executable.destination}`);
619
+ }
620
+ try {
621
+ const iface = new import_ethers3.ethers.Interface([`function ${action.func}`]);
622
+ const encodedData = iface.encodeFunctionData(action.func, executable.args);
623
+ const result = await this.provider.call({
624
+ to: executable.destination,
625
+ data: encodedData
626
+ });
627
+ const decodedResult = iface.decodeFunctionResult(action.func, result);
628
+ const isSuccess = true;
629
+ const { values, results } = await this.results.extractQueryResults(
630
+ executable.warp,
631
+ decodedResult,
632
+ executable.action,
633
+ executable.resolvedInputs
634
+ );
635
+ const adapter = getEvmAdapter(this.config);
636
+ const next = (0, import_warps3.getNextInfo)(this.config, adapter, executable.warp, executable.action, results);
637
+ return {
638
+ success: isSuccess,
639
+ warp: executable.warp,
640
+ action: executable.action,
641
+ user: this.config.user?.wallets?.[executable.chain.name] || null,
642
+ txHash: null,
643
+ next,
644
+ values,
645
+ results,
646
+ messages: (0, import_warps3.applyResultsToMessages)(executable.warp, results)
647
+ };
648
+ } catch (error) {
649
+ return {
650
+ success: false,
651
+ warp: executable.warp,
652
+ action: executable.action,
653
+ user: this.config.user?.wallets?.[executable.chain.name] || null,
654
+ txHash: null,
655
+ next: null,
656
+ values: [],
657
+ results: {},
658
+ messages: {}
659
+ };
660
+ }
661
+ }
662
+ async preprocessInput(chain, input, type, value) {
663
+ const typedValue = this.serializer.stringToTyped(value);
664
+ switch (type) {
665
+ case "address":
666
+ if (!import_ethers3.ethers.isAddress(typedValue)) {
667
+ throw new Error(`Invalid address format: ${typedValue}`);
668
+ }
669
+ return import_ethers3.ethers.getAddress(typedValue);
670
+ case "hex":
671
+ if (!import_ethers3.ethers.isHexString(typedValue)) {
672
+ throw new Error(`Invalid hex format: ${typedValue}`);
673
+ }
674
+ return typedValue;
675
+ case "uint8":
676
+ case "uint16":
677
+ case "uint32":
678
+ case "uint64":
679
+ case "biguint":
680
+ const bigIntValue = BigInt(typedValue);
681
+ if (bigIntValue < 0) {
682
+ throw new Error(`Negative value not allowed for type ${type}: ${typedValue}`);
683
+ }
684
+ return bigIntValue.toString();
685
+ default:
686
+ return String(typedValue);
687
+ }
688
+ }
689
+ async estimateGasAndSetDefaults(tx, from) {
690
+ try {
691
+ const gasEstimate = await this.provider.estimateGas({
692
+ ...tx,
693
+ from
694
+ });
695
+ if (gasEstimate < BigInt(WarpEvmConstants.Validation.MinGasLimit)) {
696
+ throw new Error(`Gas estimate too low: ${gasEstimate}`);
697
+ }
698
+ if (gasEstimate > BigInt(WarpEvmConstants.Validation.MaxGasLimit)) {
699
+ throw new Error(`Gas estimate too high: ${gasEstimate}`);
700
+ }
701
+ const feeData = await this.provider.getFeeData();
702
+ if (feeData.maxFeePerGas && feeData.maxPriorityFeePerGas) {
703
+ return {
704
+ ...tx,
705
+ gasLimit: gasEstimate,
706
+ maxFeePerGas: feeData.maxFeePerGas,
707
+ maxPriorityFeePerGas: feeData.maxPriorityFeePerGas
708
+ };
709
+ } else if (feeData.gasPrice) {
710
+ return {
711
+ ...tx,
712
+ gasLimit: gasEstimate,
713
+ gasPrice: feeData.gasPrice
714
+ };
715
+ } else {
716
+ return {
717
+ ...tx,
718
+ gasLimit: gasEstimate,
719
+ gasPrice: import_ethers3.ethers.parseUnits(WarpEvmConstants.GasPrice.Default, "wei")
720
+ };
721
+ }
722
+ } catch (error) {
723
+ let defaultGasLimit = BigInt(WarpEvmConstants.GasLimit.Default);
724
+ if (tx.data && tx.data !== "0x") {
725
+ defaultGasLimit = BigInt(WarpEvmConstants.GasLimit.ContractCall);
726
+ } else {
727
+ defaultGasLimit = BigInt(WarpEvmConstants.GasLimit.Transfer);
728
+ }
729
+ return {
730
+ ...tx,
731
+ gasLimit: defaultGasLimit,
732
+ gasPrice: import_ethers3.ethers.parseUnits(WarpEvmConstants.GasPrice.Default, "wei")
733
+ };
734
+ }
735
+ }
736
+ async signMessage(message, privateKey) {
737
+ throw new Error("Not implemented");
738
+ }
739
+ };
740
+
741
+ // src/WarpEvmExplorer.ts
742
+ var WarpEvmExplorer = class {
743
+ constructor(chainInfo, chainName = "ethereum") {
744
+ this.chainInfo = chainInfo;
745
+ this.chainName = chainName;
746
+ }
747
+ getAccountUrl(address) {
748
+ const baseUrl = this.chainInfo.explorerUrl || getEvmExplorerUrl("mainnet", this.chainName);
749
+ return `${baseUrl}/address/${address}`;
750
+ }
751
+ getTransactionUrl(hash) {
752
+ const baseUrl = this.chainInfo.explorerUrl || getEvmExplorerUrl("mainnet", this.chainName);
753
+ return `${baseUrl}/tx/${hash}`;
754
+ }
755
+ };
756
+
757
+ // src/main.ts
758
+ var getEvmAdapter = (config, fallback) => {
759
+ if (!fallback) throw new Error("EVM adapter requires a fallback adapter");
760
+ return {
761
+ chain: WarpEvmConstants.ChainName,
762
+ prefix: WarpEvmConstants.ChainPrefix,
763
+ builder: () => new WarpEvmBuilder(config),
764
+ executor: new WarpEvmExecutor(config),
765
+ results: new WarpEvmResults(config),
766
+ serializer: new WarpEvmSerializer(),
767
+ registry: fallback.registry,
768
+ explorer: (chainInfo) => new WarpEvmExplorer(chainInfo),
769
+ abiBuilder: () => fallback.abiBuilder(),
770
+ brandBuilder: () => fallback.brandBuilder()
771
+ };
772
+ };
773
+ // Annotate the CommonJS export names for ESM import in node:
774
+ 0 && (module.exports = {
775
+ EVM_CHAIN_CONFIGS,
776
+ WarpEvmBuilder,
777
+ WarpEvmConstants,
778
+ WarpEvmExecutor,
779
+ WarpEvmExplorer,
780
+ WarpEvmResults,
781
+ WarpEvmSerializer,
782
+ getEvmAdapter,
783
+ getEvmApiUrl,
784
+ getEvmBlockTime,
785
+ getEvmChainConfig,
786
+ getEvmChainId,
787
+ getEvmExplorerUrl,
788
+ getEvmNativeToken,
789
+ getEvmRegistryAddress,
790
+ getSupportedEnvironments,
791
+ getSupportedEvmChains
792
+ });
793
+ //# sourceMappingURL=index.js.map