x402z-facilitator 0.0.10 → 0.0.11

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 CHANGED
@@ -263,7 +263,7 @@ var X402zEvmFacilitator = class {
263
263
  }
264
264
  }
265
265
  async flushBatch(tokenAddress, entries) {
266
- const requests = entries.map((entry) => {
266
+ const buildRequests = (batchEntries2) => batchEntries2.map(({ entry }) => {
267
267
  const confidentialPayload = entry.payload.payload;
268
268
  return {
269
269
  p: confidentialPayload.authorization,
@@ -272,7 +272,10 @@ var X402zEvmFacilitator = class {
272
272
  sig: confidentialPayload.signature
273
273
  };
274
274
  });
275
- const txRequest = {
275
+ const originalEntries = entries.map((entry, index) => ({ entry, index }));
276
+ let batchEntries = originalEntries;
277
+ let requests = buildRequests(batchEntries);
278
+ let txRequest = {
276
279
  address: this.batcherAddress,
277
280
  abi: batcherAbi,
278
281
  functionName: "batchConfidentialTransferWithAuthorization",
@@ -287,11 +290,57 @@ var X402zEvmFacilitator = class {
287
290
  size: requests.length
288
291
  });
289
292
  }
293
+ try {
294
+ const [successes, transferredHandles] = await this.config.signer.simulateContract(txRequest);
295
+ if (successes.length === batchEntries.length && transferredHandles.length === batchEntries.length) {
296
+ const filtered = [];
297
+ batchEntries.forEach((item, index) => {
298
+ if (successes[index]) {
299
+ filtered.push(item);
300
+ return;
301
+ }
302
+ const confidentialPayload = item.entry.payload.payload;
303
+ item.entry.resolve({
304
+ success: false,
305
+ errorReason: "preflight_failed",
306
+ payer: confidentialPayload.authorization.holder,
307
+ transaction: "",
308
+ network: item.entry.requirements.network,
309
+ batch: { index: item.index, success: false, transferredHandle: transferredHandles[index] }
310
+ });
311
+ });
312
+ if (filtered.length === 0) {
313
+ return;
314
+ }
315
+ batchEntries = filtered;
316
+ requests = buildRequests(batchEntries);
317
+ txRequest = {
318
+ ...txRequest,
319
+ args: [tokenAddress, requests]
320
+ };
321
+ }
322
+ } catch (error) {
323
+ if (process.env.X402Z_DEBUG === "1") {
324
+ console.error("[x402z-facilitator] settle tx error", error);
325
+ }
326
+ for (const entry of entries) {
327
+ const confidentialPayload = entry.payload.payload;
328
+ entry.resolve({
329
+ success: false,
330
+ errorReason: "preflight_failed",
331
+ payer: confidentialPayload.authorization.holder,
332
+ transaction: "",
333
+ network: entry.requirements.network
334
+ });
335
+ }
336
+ return;
337
+ }
338
+ const settleEntries = batchEntries.map((item) => item.entry);
290
339
  let txHash;
291
340
  try {
292
341
  txHash = await this.config.signer.writeContract(txRequest);
293
342
  } catch (error) {
294
- for (const entry of entries) {
343
+ for (const entry of settleEntries) {
295
344
  const confidentialPayload = entry.payload.payload;
296
345
  entry.resolve({
297
346
  success: false,
@@ -313,7 +362,7 @@ var X402zEvmFacilitator = class {
313
362
  console.debug("[x402z-facilitator] tx receipt", receipt);
314
363
  }
315
364
  if (receipt.status !== "success") {
316
- for (const entry of entries) {
365
+ for (const entry of settleEntries) {
317
366
  const confidentialPayload = entry.payload.payload;
318
367
  entry.resolve({
319
368
  success: false,
@@ -327,7 +376,7 @@ var X402zEvmFacilitator = class {
327
376
  }
328
377
  }
329
378
  if (!this.waitForReceipt) {
330
- entries.forEach((entry) => {
379
+ settleEntries.forEach((entry) => {
331
380
  const confidentialPayload = entry.payload.payload;
332
381
  entry.resolve({
333
382
  success: true,
@@ -370,31 +419,27 @@ var X402zEvmFacilitator = class {
370
419
  }
371
420
  }
372
421
  }
373
- entries.forEach((entry, index) => {
422
+ settleEntries.forEach((entry, index) => {
374
423
  const confidentialPayload = entry.payload.payload;
375
424
  const batchResult = batchResults.get(index);
376
425
  if (!batchResult || !batchResult.success) {
377
- entry.resolve(
378
- {
379
- success: false,
380
- errorReason: "settlement_failed",
381
- payer: confidentialPayload.authorization.holder,
382
- transaction: txHash,
383
- network: entry.requirements.network,
384
- ...batchResult ? { batch: { index, ...batchResult } } : {}
385
- }
386
- );
387
- return;
388
- }
389
- entry.resolve(
390
- {
391
- success: true,
426
+ entry.resolve({
427
+ success: false,
428
+ errorReason: "settlement_failed",
392
429
  payer: confidentialPayload.authorization.holder,
393
430
  transaction: txHash,
394
431
  network: entry.requirements.network,
395
- batch: { index, ...batchResult }
396
- }
397
- );
432
+ ...batchResult ? { batch: { index, ...batchResult } } : {}
433
+ });
434
+ return;
435
+ }
436
+ entry.resolve({
437
+ success: true,
438
+ payer: confidentialPayload.authorization.holder,
439
+ transaction: txHash,
440
+ network: entry.requirements.network,
441
+ batch: { index, ...batchResult }
442
+ });
398
443
  });
399
444
  }
400
445
  };
@@ -522,7 +567,7 @@ function createFacilitatorFromEnv() {
522
567
  },
523
568
  transport: (0, import_viem2.http)(rpcUrl)
524
569
  }).extend(import_viem2.publicActions);
525
- const signer = (0, import_evm.toFacilitatorEvmSigner)({
570
+ const baseSigner = (0, import_evm.toFacilitatorEvmSigner)({
526
571
  address: account.address,
527
572
  readContract: (args) => client.readContract({
528
573
  ...args,
@@ -563,6 +608,18 @@ function createFacilitatorFromEnv() {
563
608
  waitForTransactionReceipt: (args) => client.waitForTransactionReceipt(args),
564
609
  getCode: (args) => client.getCode(args)
565
610
  });
611
+ const signer = {
612
+ ...baseSigner,
613
+ simulateContract: async (args) => {
614
+ const result = await client.simulateContract({
615
+ ...args,
616
+ args: args.args || [],
617
+ account
618
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
619
+ });
620
+ return result.result;
621
+ }
622
+ };
566
623
  const facilitator = new import_facilitator.x402Facilitator();
567
624
  for (const network of networks) {
568
625
  facilitator.register(
package/dist/index.mjs CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  createFacilitatorService,
4
4
  startFacilitator,
5
5
  startFacilitatorService
6
- } from "./chunk-3XCMVK47.mjs";
6
+ } from "./chunk-3E55KK5J.mjs";
7
7
 
8
8
  // src/scheme/register.ts
9
9
  function registerX402zEvmFacilitatorScheme(facilitator, config) {
@@ -315,7 +315,7 @@ var X402zEvmFacilitator = class {
315
315
  }
316
316
  }
317
317
  async flushBatch(tokenAddress, entries) {
318
- const requests = entries.map((entry) => {
318
+ const buildRequests = (batchEntries2) => batchEntries2.map(({ entry }) => {
319
319
  const confidentialPayload = entry.payload.payload;
320
320
  return {
321
321
  p: confidentialPayload.authorization,
@@ -324,7 +324,10 @@ var X402zEvmFacilitator = class {
324
324
  sig: confidentialPayload.signature
325
325
  };
326
326
  });
327
- const txRequest = {
327
+ const originalEntries = entries.map((entry, index) => ({ entry, index }));
328
+ let batchEntries = originalEntries;
329
+ let requests = buildRequests(batchEntries);
330
+ let txRequest = {
328
331
  address: this.batcherAddress,
329
332
  abi: batcherAbi,
330
333
  functionName: "batchConfidentialTransferWithAuthorization",
@@ -339,11 +342,57 @@ var X402zEvmFacilitator = class {
339
342
  size: requests.length
340
343
  });
341
344
  }
345
+ try {
346
+ const [successes, transferredHandles] = await this.config.signer.simulateContract(txRequest);
347
+ if (successes.length === batchEntries.length && transferredHandles.length === batchEntries.length) {
348
+ const filtered = [];
349
+ batchEntries.forEach((item, index) => {
350
+ if (successes[index]) {
351
+ filtered.push(item);
352
+ return;
353
+ }
354
+ const confidentialPayload = item.entry.payload.payload;
355
+ item.entry.resolve({
356
+ success: false,
357
+ errorReason: "preflight_failed",
358
+ payer: confidentialPayload.authorization.holder,
359
+ transaction: "",
360
+ network: item.entry.requirements.network,
361
+ batch: { index: item.index, success: false, transferredHandle: transferredHandles[index] }
362
+ });
363
+ });
364
+ if (filtered.length === 0) {
365
+ return;
366
+ }
367
+ batchEntries = filtered;
368
+ requests = buildRequests(batchEntries);
369
+ txRequest = {
370
+ ...txRequest,
371
+ args: [tokenAddress, requests]
372
+ };
373
+ }
374
+ } catch (error) {
375
+ if (process.env.X402Z_DEBUG === "1") {
376
+ console.error("[x402z-facilitator] settle tx error", error);
377
+ }
378
+ for (const entry of entries) {
379
+ const confidentialPayload = entry.payload.payload;
380
+ entry.resolve({
381
+ success: false,
382
+ errorReason: "preflight_failed",
383
+ payer: confidentialPayload.authorization.holder,
384
+ transaction: "",
385
+ network: entry.requirements.network
386
+ });
387
+ }
388
+ return;
389
+ }
390
+ const settleEntries = batchEntries.map((item) => item.entry);
342
391
  let txHash;
343
392
  try {
344
393
  txHash = await this.config.signer.writeContract(txRequest);
345
394
  } catch (error) {
346
- for (const entry of entries) {
395
+ for (const entry of settleEntries) {
347
396
  const confidentialPayload = entry.payload.payload;
348
397
  entry.resolve({
349
398
  success: false,
@@ -365,7 +414,7 @@ var X402zEvmFacilitator = class {
365
414
  console.debug("[x402z-facilitator] tx receipt", receipt);
366
415
  }
367
416
  if (receipt.status !== "success") {
368
- for (const entry of entries) {
417
+ for (const entry of settleEntries) {
369
418
  const confidentialPayload = entry.payload.payload;
370
419
  entry.resolve({
371
420
  success: false,
@@ -379,7 +428,7 @@ var X402zEvmFacilitator = class {
379
428
  }
380
429
  }
381
430
  if (!this.waitForReceipt) {
382
- entries.forEach((entry) => {
431
+ settleEntries.forEach((entry) => {
383
432
  const confidentialPayload = entry.payload.payload;
384
433
  entry.resolve({
385
434
  success: true,
@@ -422,31 +471,27 @@ var X402zEvmFacilitator = class {
422
471
  }
423
472
  }
424
473
  }
425
- entries.forEach((entry, index) => {
474
+ settleEntries.forEach((entry, index) => {
426
475
  const confidentialPayload = entry.payload.payload;
427
476
  const batchResult = batchResults.get(index);
428
477
  if (!batchResult || !batchResult.success) {
429
- entry.resolve(
430
- {
431
- success: false,
432
- errorReason: "settlement_failed",
433
- payer: confidentialPayload.authorization.holder,
434
- transaction: txHash,
435
- network: entry.requirements.network,
436
- ...batchResult ? { batch: { index, ...batchResult } } : {}
437
- }
438
- );
439
- return;
440
- }
441
- entry.resolve(
442
- {
443
- success: true,
478
+ entry.resolve({
479
+ success: false,
480
+ errorReason: "settlement_failed",
444
481
  payer: confidentialPayload.authorization.holder,
445
482
  transaction: txHash,
446
483
  network: entry.requirements.network,
447
- batch: { index, ...batchResult }
448
- }
449
- );
484
+ ...batchResult ? { batch: { index, ...batchResult } } : {}
485
+ });
486
+ return;
487
+ }
488
+ entry.resolve({
489
+ success: true,
490
+ payer: confidentialPayload.authorization.holder,
491
+ transaction: txHash,
492
+ network: entry.requirements.network,
493
+ batch: { index, ...batchResult }
494
+ });
450
495
  });
451
496
  }
452
497
  };
@@ -503,7 +548,7 @@ function createFacilitatorFromEnv() {
503
548
  },
504
549
  transport: (0, import_viem2.http)(rpcUrl)
505
550
  }).extend(import_viem2.publicActions);
506
- const signer = (0, import_evm.toFacilitatorEvmSigner)({
551
+ const baseSigner = (0, import_evm.toFacilitatorEvmSigner)({
507
552
  address: account.address,
508
553
  readContract: (args) => client.readContract({
509
554
  ...args,
@@ -544,6 +589,18 @@ function createFacilitatorFromEnv() {
544
589
  waitForTransactionReceipt: (args) => client.waitForTransactionReceipt(args),
545
590
  getCode: (args) => client.getCode(args)
546
591
  });
592
+ const signer = {
593
+ ...baseSigner,
594
+ simulateContract: async (args) => {
595
+ const result = await client.simulateContract({
596
+ ...args,
597
+ args: args.args || [],
598
+ account
599
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
600
+ });
601
+ return result.result;
602
+ }
603
+ };
547
604
  const facilitator = new import_facilitator.x402Facilitator();
548
605
  for (const network of networks) {
549
606
  facilitator.register(
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  createFacilitatorFromEnv,
3
3
  startFacilitator
4
- } from "../chunk-3XCMVK47.mjs";
4
+ } from "../chunk-3E55KK5J.mjs";
5
5
  export {
6
6
  createFacilitatorFromEnv,
7
7
  startFacilitator
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "x402z-facilitator",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",
@@ -11,7 +11,7 @@
11
11
  "@x402/core": "^2.0.0",
12
12
  "@x402/evm": "^2.0.0",
13
13
  "viem": "^2.39.3",
14
- "x402z-shared": "0.0.10"
14
+ "x402z-shared": "0.0.19"
15
15
  },
16
16
  "devDependencies": {
17
17
  "jest": "^29.7.0",