@owney/sdk 0.7.32-beta.2 → 0.7.32-beta.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.
@@ -175,6 +175,7 @@ function batchArgs(b) {
175
175
 
176
176
  // src/lib/token-batch-family.ts
177
177
  var memory = /* @__PURE__ */ new Map();
178
+ var unfinished = /* @__PURE__ */ new Map();
178
179
  var settled = /* @__PURE__ */ new Map();
179
180
  var hashPattern = /^0x[\da-f]{64}$/i;
180
181
  var uint = (value) => typeof value === "string" && /^\d{1,78}$/.test(value) && BigInt(value) < 1n << 256n;
@@ -395,6 +396,9 @@ function merge(base, incoming) {
395
396
  async function loadTokenBatchFamily(scope, expectedHash, allowMissing = false) {
396
397
  try {
397
398
  const key = tokenBatchKey(scope), records = /* @__PURE__ */ new Map();
399
+ for (const sidecar of [...unfinished.keys()])
400
+ if (sidecar.startsWith(`${key}:family:`) && !tokenBatchRead(sidecar))
401
+ unfinished.delete(sidecar);
398
402
  for (const k of keys().filter((k2) => k2.startsWith(`${key}:family:`))) {
399
403
  const raw = tokenBatchRead(k);
400
404
  if (!raw) continue;
@@ -422,6 +426,19 @@ async function loadTokenBatchFamily(scope, expectedHash, allowMissing = false) {
422
426
  );
423
427
  }
424
428
  }
429
+ const marked = /* @__PURE__ */ new Set();
430
+ for (const [id, family] of [...records]) {
431
+ const sidecar = `${key}:family:${id}`;
432
+ const terminal = unfinished.get(sidecar);
433
+ if (!terminal) continue;
434
+ if (family.terminal) {
435
+ unfinished.delete(sidecar);
436
+ continue;
437
+ }
438
+ finishTokenBatchFamily(scope, family, terminal);
439
+ marked.add(id);
440
+ records.set(id, { ...family, terminal });
441
+ }
425
442
  if (expectedHash) {
426
443
  let found = [...records.values()].filter(
427
444
  (f) => f.attempts.some((a) => same(a.hash, expectedHash))
@@ -440,7 +457,7 @@ async function loadTokenBatchFamily(scope, expectedHash, allowMissing = false) {
440
457
  return found[0];
441
458
  }
442
459
  const active = [...records.values()].filter(
443
- (f) => !f.terminal || f.authorizationId === projectedId
460
+ (f) => !f.terminal || f.authorizationId === projectedId && !marked.has(f.authorizationId)
444
461
  );
445
462
  if (active.length > 1) throw pending();
446
463
  return active[0];
@@ -449,6 +466,59 @@ async function loadTokenBatchFamily(scope, expectedHash, allowMissing = false) {
449
466
  throw pending();
450
467
  }
451
468
  }
469
+ async function dropExpiredHashlessTokenBatch(scope, pub) {
470
+ try {
471
+ const key = tokenBatchKey(scope);
472
+ const family = await loadTokenBatchFamily(scope);
473
+ const raw = family ? void 0 : tokenBatchRead(key);
474
+ const body = family ? family.terminal ? void 0 : family.body : raw ? JSON.parse(raw) : void 0;
475
+ if (!body || body.serializedTransaction || family?.attempts.some((a) => a.phase === "deposit") || !uint(body.deadline))
476
+ return false;
477
+ const block = await pub.getBlock();
478
+ if (typeof block?.timestamp !== "bigint" || block.timestamp <= BigInt(body.deadline))
479
+ return false;
480
+ if (family)
481
+ finishTokenBatchFamily(scope, family, {
482
+ kind: "expired",
483
+ blockNumber: typeof block.number === "bigint" ? block.number.toString() : "0"
484
+ });
485
+ else tokenBatchClear(key, raw);
486
+ return true;
487
+ } catch {
488
+ return false;
489
+ }
490
+ }
491
+ async function discardHashlessTokenBatch(scope, deadline, matches) {
492
+ try {
493
+ const key = tokenBatchKey(scope);
494
+ const family = await loadTokenBatchFamily(scope);
495
+ const raw = family ? void 0 : tokenBatchRead(key);
496
+ const body = family ? family.terminal ? void 0 : family.body : raw ? JSON.parse(raw) : void 0;
497
+ if (!body || body.serializedTransaction || family?.attempts.some((a) => a.phase === "deposit") || body.deadline !== deadline || body.chainId !== scope.chainId || !same(body.from, scope.owner) || !same(body.token, scope.token) || !matches(body))
498
+ return false;
499
+ if (family)
500
+ finishTokenBatchFamily(scope, family, { kind: "failed", blockNumber: "0" });
501
+ else tokenBatchClear(key, raw);
502
+ return true;
503
+ } catch {
504
+ return false;
505
+ }
506
+ }
507
+ async function savedTokenBatchBody(scope, pub) {
508
+ try {
509
+ if (pub) await dropExpiredHashlessTokenBatch(scope, pub);
510
+ const family = await loadTokenBatchFamily(scope);
511
+ if (family) return family.terminal ? void 0 : family.body;
512
+ const raw = tokenBatchRead(tokenBatchKey(scope));
513
+ if (!raw) return void 0;
514
+ const body = JSON.parse(raw);
515
+ if (body?.chainId !== scope.chainId || typeof body.from !== "string" || typeof body.token !== "string" || !same(body.from, scope.owner) || !same(body.token, scope.token) || !uint(body.deadline) || !Array.isArray(body.transfers) || !body.transfers.length)
516
+ return void 0;
517
+ return body;
518
+ } catch {
519
+ return void 0;
520
+ }
521
+ }
452
522
  async function saveTokenBatchAttempt(scope, body, attempt, assertCurrent = () => {
453
523
  }) {
454
524
  try {
@@ -507,10 +577,15 @@ function finishTokenBatchFamily(scope, family, terminal) {
507
577
  return;
508
578
  const evidence = terminalOf(terminal, current.attempts);
509
579
  if (!evidence) return;
510
- tokenBatchWrite(
511
- sidecar,
512
- JSON.stringify({ ...current, terminal: evidence })
513
- );
580
+ try {
581
+ tokenBatchWrite(
582
+ sidecar,
583
+ JSON.stringify({ ...current, terminal: evidence })
584
+ );
585
+ unfinished.delete(sidecar);
586
+ } catch {
587
+ unfinished.set(sidecar, evidence);
588
+ }
514
589
  const projected = tokenBatchRead(key);
515
590
  if (projected && tokenBatchAuthorizationId(JSON.parse(projected), family.relayer) === family.authorizationId)
516
591
  tokenBatchClear(key, projected);
@@ -539,6 +614,9 @@ export {
539
614
  tokenBatchCall,
540
615
  validateTokenBatchAttempt,
541
616
  loadTokenBatchFamily,
617
+ dropExpiredHashlessTokenBatch,
618
+ discardHashlessTokenBatch,
619
+ savedTokenBatchBody,
542
620
  saveTokenBatchAttempt,
543
621
  finishTokenBatchFamily
544
622
  };