@gvnrdao/dh-sdk 0.0.320 → 0.0.324

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.mjs CHANGED
@@ -96,7 +96,8 @@ var init_deployment_addresses = __esm({
96
96
  BitcoinWithdrawalAddressRegistry: "0x6376D392097F0A81e51BD83960A97EA20eaf1dbe",
97
97
  CarryAgentRegistry: "0x5595CB538D44E6Cf179D20364fD631D0477e69f7",
98
98
  AgentDelegationRegistry: "0x6AE7fc6bE0925126b9D25b1B7Ce5AD5d127ec025",
99
- DHAgentDelegate: "0xa5A14489ae883960df6E1463A41a47aBE9Ecc5AF"
99
+ DHAgentDelegate: "0xa5A14489ae883960df6E1463A41a47aBE9Ecc5AF",
100
+ AgentModuleFactory: "0x1Fd3aCa2C030Cf77ce28Af9694D3Bb3034ACa2E5"
100
101
  },
101
102
  upgraded: {
102
103
  PositionManager: {
@@ -391,7 +392,8 @@ var init_deployment_addresses = __esm({
391
392
  BitcoinWithdrawalAddressRegistry: "0x6376D392097F0A81e51BD83960A97EA20eaf1dbe",
392
393
  CarryAgentRegistry: "0x5595CB538D44E6Cf179D20364fD631D0477e69f7",
393
394
  AgentDelegationRegistry: "0x6AE7fc6bE0925126b9D25b1B7Ce5AD5d127ec025",
394
- DHAgentDelegate: "0xa5A14489ae883960df6E1463A41a47aBE9Ecc5AF"
395
+ DHAgentDelegate: "0xa5A14489ae883960df6E1463A41a47aBE9Ecc5AF",
396
+ AgentModuleFactory: "0x1Fd3aCa2C030Cf77ce28Af9694D3Bb3034ACa2E5"
395
397
  };
396
398
  MAINNET_DEPLOYMENT = {
397
399
  network: "mainnet",
@@ -628,7 +630,7 @@ var init_deployment_addresses = __esm({
628
630
  LOCALHOST_DEPLOYMENT = {
629
631
  network: "localhost",
630
632
  chainId: 1337,
631
- timestamp: "2026-07-20T19:45:58.514Z",
633
+ timestamp: "2026-07-23T17:13:42.777Z",
632
634
  deployer: "",
633
635
  contracts: {
634
636
  MessageHashBuilder: "0xa82fF9aFd8f496c3d6ac40E2a0F282E47488CFc9",
@@ -648,6 +650,30 @@ var init_deployment_addresses = __esm({
648
650
  PositionManager: "0x851356ae760d987E095750cCeb3bC6014560891C",
649
651
  OperationAuthorizationRegistry: "0xc96304e3c037f81dA488ed9dEa1D8F2a48278a75",
650
652
  PKPValidation: "0xD0141E899a65C95a556fE2B27e5982A6DE7fDD7A"
653
+ },
654
+ latestEnv: {
655
+ UCD_TOKEN: "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512",
656
+ UCD_CONTROLLER: "0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9",
657
+ POSITION_MANAGER: "0x851356ae760d987E095750cCeb3bC6014560891C",
658
+ PKP_VALIDATION_REGISTRY: "0xD0141E899a65C95a556fE2B27e5982A6DE7fDD7A",
659
+ PKP_VALIDATION_CID_V1: "0x12203af2054b0ab6e18d898ddfc7985593439fb82d22336133387c1169f98d3702ab",
660
+ PKP_VALIDATION_CID_V3: "0x12203af2054b0ab6e18d898ddfc7985593439fb82d22336133387c1169f98d3702ab",
661
+ PKP_ETH_ADDRESS: "0x406f6a9855de2f5577b1525f0600461f02859bd0",
662
+ OPERATION_AUTHORIZATION_REGISTRY: "0xc96304e3c037f81dA488ed9dEa1D8F2a48278a75",
663
+ UCD_MINT_VALIDATOR_ADDRESS: "0x406f6a9855de2f5577b1525f0600461f02859bd0",
664
+ UCD_MINT_VALIDATOR_VERSION: 1,
665
+ BTC_WITHDRAWAL_VALIDATOR_ADDRESS: "0x406f6a9855de2f5577b1525f0600461f02859bd0",
666
+ BTC_WITHDRAWAL_VALIDATOR_VERSION: 3,
667
+ UPDATE_BALANCE_VALIDATOR_ADDRESS: "0x406f6a9855de2f5577b1525f0600461f02859bd0",
668
+ UPDATE_BALANCE_VALIDATOR_VERSION: 1,
669
+ PROCESS_PAYMENT_VALIDATOR_ADDRESS: "0x406f6a9855de2f5577b1525f0600461f02859bd0",
670
+ PROCESS_PAYMENT_VALIDATOR_VERSION: 1,
671
+ EXTEND_POSITION_VALIDATOR_ADDRESS: "0x406f6a9855de2f5577b1525f0600461f02859bd0",
672
+ EXTEND_POSITION_VALIDATOR_VERSION: 1,
673
+ POSITION_MANAGER_CORE_MODULE: "0xB7f8BC63BbcaD18155201308C8f3540b07f84F5e",
674
+ LOAN_OPERATIONS_MANAGER_MODULE: "0x68B1D87F95878fE05B998F19b66F4baba5De1aed",
675
+ TERM_MANAGER_MODULE: "0x0DCd1Bf9A1b36cE34237eEaFef220932846BCD82",
676
+ COMMUNITY_MANAGER_MODULE: ""
651
677
  }
652
678
  };
653
679
  LOCALHOST_CONTRACTS = {
@@ -1359,30 +1385,30 @@ var require_pkg_src = __commonJS({
1359
1385
  }
1360
1386
  },
1361
1387
  btcTransactionSigner: {
1362
- cid: "QmPncBUbxN9giWntJcvBMFW9GDAgA3DwgAkUAHxSsY67ic",
1388
+ cid: "QmakWzTce8NH1Jkgv1xwg83Bg9WmF2UG1Mq76pDYDSwwUp",
1363
1389
  authorizedCidHex: cidToHex(
1364
- "QmPncBUbxN9giWntJcvBMFW9GDAgA3DwgAkUAHxSsY67ic"
1390
+ "QmakWzTce8NH1Jkgv1xwg83Bg9WmF2UG1Mq76pDYDSwwUp"
1365
1391
  ),
1366
1392
  name: "Btc Transaction Signer",
1367
1393
  description: "Production Btc Transaction Signer",
1368
1394
  version: "1.1.1",
1369
1395
  deployed: true,
1370
- deployedAt: 1785226367707,
1371
- size: 66624,
1372
- hash: "9e0d9909519d5133bab03a9491566870b08d8b42c80e2d768bd24f02946cc781"
1396
+ deployedAt: 1786113907707,
1397
+ size: 67999,
1398
+ hash: "83f3644c68819215e05ba8db3225571354a009458fc1437f606df6cb9142d3f9"
1373
1399
  },
1374
1400
  ucdMintValidator: {
1375
- cid: "QmPRLCfseSWCAS4PpvQWt9aEd6rXqWYG4JyGTn26KYk6xc",
1401
+ cid: "Qmd8jgKRceLvf5aoTtwdXG2JqMoBK5cRJwrvEtQkgvbFbx",
1376
1402
  authorizedCidHex: cidToHex(
1377
- "QmPRLCfseSWCAS4PpvQWt9aEd6rXqWYG4JyGTn26KYk6xc"
1403
+ "Qmd8jgKRceLvf5aoTtwdXG2JqMoBK5cRJwrvEtQkgvbFbx"
1378
1404
  ),
1379
1405
  name: "Ucd Mint Validator",
1380
1406
  description: "Production Ucd Mint Validator",
1381
1407
  version: "1.2.1",
1382
1408
  deployed: true,
1383
- deployedAt: 1785226381631,
1384
- size: 81658,
1385
- hash: "d8ea21622885c3d26fdb64e2ba9d2f9460753a5cd7934b2928858899537443eb",
1409
+ deployedAt: 1786113908489,
1410
+ size: 82739,
1411
+ hash: "2bdd9aebdaad58a43a4731d5b7284f2531b7a9caaf8caf5719d891c4ed33cb2e",
1386
1412
  validatorWalletAddress: "0x82DB9F515957aA380B596C6eA2Bd30CEF181f236",
1387
1413
  pkp: {
1388
1414
  publicKey: "0x041ab17cd91fc5c0b761eea6092d032807561b621b82c488826776e04a9158d61ba64d809a4729f0501289d980732cb06d8dfd06999dd9c8efd2f495dad78b31bb",
@@ -1390,17 +1416,17 @@ var require_pkg_src = __commonJS({
1390
1416
  }
1391
1417
  },
1392
1418
  processPaymentValidator: {
1393
- cid: "QmNUzmHQHsaKThGkfZiD3xZrN64qZfn4P6J4MqWgvkeEsX",
1419
+ cid: "QmNXVdysXDhCaQ1uNE2Vgjbuzy6yUEnnVgHzmDfNKXMU6z",
1394
1420
  authorizedCidHex: cidToHex(
1395
- "QmNUzmHQHsaKThGkfZiD3xZrN64qZfn4P6J4MqWgvkeEsX"
1421
+ "QmNXVdysXDhCaQ1uNE2Vgjbuzy6yUEnnVgHzmDfNKXMU6z"
1396
1422
  ),
1397
1423
  name: "Process Payment Validator",
1398
1424
  description: "Production Process Payment Validator",
1399
1425
  version: "1.2.1",
1400
1426
  deployed: true,
1401
- deployedAt: 1785226385509,
1402
- size: 77655,
1403
- hash: "3c2af3c3e62e8e606d35f81fd7e6f4943f8516a9bef74f4150c8c1767cbd316f",
1427
+ deployedAt: 1786113909138,
1428
+ size: 78736,
1429
+ hash: "17191ecc9c009e0fa5ab52c876330beb2a9a75d30cccb92e7c0c04f169df088b",
1404
1430
  validatorWalletAddress: "0x82DB9F515957aA380B596C6eA2Bd30CEF181f236",
1405
1431
  pkp: {
1406
1432
  publicKey: "0x04cfccb9b6c7addc79469c0e9fafebef4c70801302c62cdb26b610723a9b8527d23f98a40158c7340d83fd0e62d02b5ec74c12f7a86d98e8c6973b4811110f19ad",
@@ -1408,17 +1434,17 @@ var require_pkg_src = __commonJS({
1408
1434
  }
1409
1435
  },
1410
1436
  extendPositionValidator: {
1411
- cid: "QmY2t41w1maaoDmdRPx6wTfciHPQAydSpLSKwKVFSS2DPM",
1437
+ cid: "QmYwBsyrhGfHWyRKx549G4tKHxsBLAuRUHcecigeUCkFav",
1412
1438
  authorizedCidHex: cidToHex(
1413
- "QmY2t41w1maaoDmdRPx6wTfciHPQAydSpLSKwKVFSS2DPM"
1439
+ "QmYwBsyrhGfHWyRKx549G4tKHxsBLAuRUHcecigeUCkFav"
1414
1440
  ),
1415
1441
  name: "Extend Position Validator",
1416
1442
  description: "Production Extend Position Validator",
1417
1443
  version: "1.2.1",
1418
1444
  deployed: true,
1419
- deployedAt: 1785226398291,
1420
- size: 75434,
1421
- hash: "ba55532fbeaf6e65d180b869fd69ee06393d574daa0ace524a9e831d846539a5",
1445
+ deployedAt: 1786113909872,
1446
+ size: 76515,
1447
+ hash: "1d4e085335af80f194a663a577876902c83015177e95906a0b461fa93bd5da08",
1422
1448
  validatorWalletAddress: "0x82DB9F515957aA380B596C6eA2Bd30CEF181f236",
1423
1449
  pkp: {
1424
1450
  publicKey: "0x04e52338f6a0c3362800f1a94c4c8f08be5030dde0fa217388c53d1c1a4790f4443c9f2886b2d2d8f4b75a9a156e9b028011b1a1cf7648da0e556aa980ec692836",
@@ -1426,9 +1452,9 @@ var require_pkg_src = __commonJS({
1426
1452
  }
1427
1453
  },
1428
1454
  btcWithdrawal: {
1429
- cid: "QmcBg8vPXsCmiKDk28bJVTqDpTrwCzSYAGu5V6ABVVPuK5",
1455
+ cid: "QmZuR3qSBajutXfRd91ffpyFrRMgmaMh7fDEHRiQQF8Ljp",
1430
1456
  authorizedCidHex: cidToHex(
1431
- "QmcBg8vPXsCmiKDk28bJVTqDpTrwCzSYAGu5V6ABVVPuK5"
1457
+ "QmZuR3qSBajutXfRd91ffpyFrRMgmaMh7fDEHRiQQF8Ljp"
1432
1458
  ),
1433
1459
  name: "Btc Withdrawal",
1434
1460
  description: "Production Btc Withdrawal",
@@ -1438,9 +1464,9 @@ var require_pkg_src = __commonJS({
1438
1464
  // than the one it replaced, which a rotation ceremony would treat as a rollback.
1439
1465
  version: "1.2.4",
1440
1466
  deployed: true,
1441
- deployedAt: 1785226412397,
1442
- size: 92754,
1443
- hash: "d3a8ec62e0b1e6d0566a154a884bf7ee4e25ef6dc2dd84fde98211d25c1dede5",
1467
+ deployedAt: 1786113910531,
1468
+ size: 95313,
1469
+ hash: "ce12f11e3b5469953bc919586e4f3b8b5d594224b58b799dd9a3b60e3749559c",
1444
1470
  validatorWalletAddress: "0x82DB9F515957aA380B596C6eA2Bd30CEF181f236",
1445
1471
  pkp: {
1446
1472
  publicKey: "0x043616787c5432415c24378c4ef48de2bcd6bb7b575b837e3cff09171802662da7105d79586c7659677a0ecbaddac4cce06cb2a11f69a16fa0c4d7002ac7d51a4d",
@@ -1551,6 +1577,21 @@ var require_pkg_src = __commonJS({
1551
1577
  size: 1046,
1552
1578
  hash: "03ee5be239c91c735d2c95db89a0d1b77e602cfc18af8eb64b3281fddcd90a72"
1553
1579
  },
1580
+ agentModuleExecutor: {
1581
+ // Bound to the isolated Sepolia agent group 7 ("DH-Agent-Sepolia"), which
1582
+ // holds this action and no PKPs until an agent is minted into it. NOT yet
1583
+ // bound on mainnet — the Base `DH-Agent-Mainnet` group is created at rollout.
1584
+ cid: "QmVgjGXUuxoGf8XyddW8a7btaazT9yEotpKoBgb65oPvcF",
1585
+ authorizedCidHex: cidToHex(
1586
+ "QmVgjGXUuxoGf8XyddW8a7btaazT9yEotpKoBgb65oPvcF"
1587
+ ),
1588
+ name: "Agent Module Executor",
1589
+ description: "Capability gate for multi-sig Safe agent delegation: binds the agent PKP to the AgentDelegationRegistry record before signing (authorize | execute modes)",
1590
+ version: "1.0.0",
1591
+ deployed: true,
1592
+ size: 11785,
1593
+ hash: "fa294e164273c2158f9ef11a0ccfd735c343c0e3759fcb4b37e0f8a5d1ab0d29"
1594
+ },
1554
1595
  staleSpendRecoveryValidator: {
1555
1596
  cid: "QmXQWT8V4wjuvH8BbC7xrkYxsVu5AKfvfWcS7jnYfrexYm",
1556
1597
  authorizedCidHex: cidToHex(
@@ -1570,17 +1611,17 @@ var require_pkg_src = __commonJS({
1570
1611
  }
1571
1612
  },
1572
1613
  btcUtxoInvalidator: {
1573
- cid: "QmZE422cZsEjYN8pE6AmpkgFoZCK4UAfdDuzoVRRsMJbwm",
1614
+ cid: "Qmf4Ze8yhnQhHbnX5RPgJrC7Jw1aA3gbpfpd52kFg7fd1m",
1574
1615
  authorizedCidHex: cidToHex(
1575
- "QmZE422cZsEjYN8pE6AmpkgFoZCK4UAfdDuzoVRRsMJbwm"
1616
+ "Qmf4Ze8yhnQhHbnX5RPgJrC7Jw1aA3gbpfpd52kFg7fd1m"
1576
1617
  ),
1577
1618
  name: "Btc Utxo Invalidator",
1578
1619
  description: "Production Btc Utxo Invalidator",
1579
1620
  version: "1.0.0",
1580
1621
  deployed: true,
1581
- deployedAt: 1785226423791,
1582
- size: 61472,
1583
- hash: "75eca528d8ac30ae8f21caaae206ccab162a83a048d622868e60f645f0c475e2"
1622
+ deployedAt: 1786113911948,
1623
+ size: 62553,
1624
+ hash: "1dcea2b49e545c423195be22e072dc5e3039ed1c95a8490b23505956029f5b33"
1584
1625
  }
1585
1626
  };
1586
1627
  function getDeploymentsForNetwork(network) {
@@ -2326,7 +2367,7 @@ ${errorReport}`);
2326
2367
  }
2327
2368
  init_debug_logger();
2328
2369
  init_session_signature_cache();
2329
- var import_ethers18 = __require("ethers");
2370
+ var import_ethers19 = __require("ethers");
2330
2371
  var EXPIRED_LOAN_MIN_LIQUIDATION_THRESHOLD_BPS = 11e3;
2331
2372
  var GRACE_PERIOD_DAYS = 30;
2332
2373
  var SATOSHIS_PER_BITCOIN = 100000000n;
@@ -5159,12 +5200,12 @@ ${auth.clientId}`;
5159
5200
  }
5160
5201
  async function executeVaultSnapshot(params) {
5161
5202
  global.ethers = {
5162
- ...import_ethers18.ethers,
5203
+ ...import_ethers19.ethers,
5163
5204
  providers: {
5164
- StaticJsonRpcProvider: import_ethers18.ethers.JsonRpcProvider
5205
+ StaticJsonRpcProvider: import_ethers19.ethers.JsonRpcProvider
5165
5206
  },
5166
- Contract: import_ethers18.ethers.Contract,
5167
- utils: import_ethers18.ethers
5207
+ Contract: import_ethers19.ethers.Contract,
5208
+ utils: import_ethers19.ethers
5168
5209
  // v6 moved utils to top level
5169
5210
  };
5170
5211
  global.Lit = {
@@ -5438,7 +5479,8 @@ function getSepoliaConfig() {
5438
5479
  bitcoinWithdrawalAddressRegistry: SEPOLIA_CONTRACTS.BitcoinWithdrawalAddressRegistry || "",
5439
5480
  carryAgentRegistry: SEPOLIA_CONTRACTS.CarryAgentRegistry || "",
5440
5481
  agentDelegationRegistry: SEPOLIA_CONTRACTS.AgentDelegationRegistry || "",
5441
- dhAgentDelegate: SEPOLIA_CONTRACTS.DHAgentDelegate || ""
5482
+ dhAgentDelegate: SEPOLIA_CONTRACTS.DHAgentDelegate || "",
5483
+ agentModuleFactory: SEPOLIA_CONTRACTS.AgentModuleFactory || ""
5442
5484
  },
5443
5485
  subgraphs: {
5444
5486
  // Lazy getter for the same module-init snapshot reason as serviceEndpoint.
@@ -5481,7 +5523,9 @@ function getMainnetConfig() {
5481
5523
  bitcoinWithdrawalAddressRegistry: MAINNET_CONTRACTS.BitcoinWithdrawalAddressRegistry || "",
5482
5524
  carryAgentRegistry: MAINNET_CONTRACTS.CarryAgentRegistry || "",
5483
5525
  agentDelegationRegistry: MAINNET_CONTRACTS.AgentDelegationRegistry || "",
5484
- dhAgentDelegate: MAINNET_CONTRACTS.DHAgentDelegate || ""
5526
+ dhAgentDelegate: MAINNET_CONTRACTS.DHAgentDelegate || "",
5527
+ // Empty on mainnet until the factory is deployed there — the ceremony is Sepolia-only.
5528
+ agentModuleFactory: MAINNET_CONTRACTS.AgentModuleFactory || ""
5485
5529
  },
5486
5530
  subgraphs: {
5487
5531
  diamondHandsUrl: "https://gateway-arbitrum.network.thegraph.com/api/subgraphs/id/CCTPsdYqco2jChDLLBQTbdJWwoukVoMt1cXeR9ti6r9A"
@@ -6135,7 +6179,10 @@ import { keccak256, toUtf8Bytes, solidityPackedKeccak256, getBytes, Contract } f
6135
6179
  // src/utils/quantum-timing.ts
6136
6180
  var QUANTUM_WINDOW_SECONDS = 60;
6137
6181
  var DEAD_ZONE_SECONDS = 8;
6138
- var INCLUSION_LATENCY_BUDGET = 16;
6182
+ var SLOT_SECONDS = 12;
6183
+ var INCLUSION_SLOT_TOLERANCE = 3;
6184
+ var INCLUSION_LATENCY_BUDGET = SLOT_SECONDS * INCLUSION_SLOT_TOLERANCE;
6185
+ var MAX_GATE_POLLS = 12;
6139
6186
  var POST_BOUNDARY_SKEW_MARGIN = 3;
6140
6187
  function calculateNextQuantumTimestamp() {
6141
6188
  const now = Math.floor(Date.now() / 1e3);
@@ -6168,8 +6215,17 @@ function validateQuantumTiming(signedTimestamp, _bufferSeconds = 30) {
6168
6215
  async function awaitSafeSubmissionWindow(quantumTimestamp, opts = {}) {
6169
6216
  const now = opts.now ?? (() => Math.floor(Date.now() / 1e3));
6170
6217
  const sleep = opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
6171
- const nowSec = now();
6172
6218
  const signatureQuantum = Math.floor(quantumTimestamp / QUANTUM_WINDOW_SECONDS) * QUANTUM_WINDOW_SECONDS;
6219
+ if (opts.chainNow) {
6220
+ const chainResult = await awaitSafeSubmissionWindowOnChain(
6221
+ signatureQuantum,
6222
+ opts.chainNow,
6223
+ sleep
6224
+ );
6225
+ if (chainResult)
6226
+ return chainResult;
6227
+ }
6228
+ const nowSec = now();
6173
6229
  const currentQuantum = Math.floor(nowSec / QUANTUM_WINDOW_SECONDS) * QUANTUM_WINDOW_SECONDS;
6174
6230
  const secsToBoundary = QUANTUM_WINDOW_SECONDS - nowSec % QUANTUM_WINDOW_SECONDS;
6175
6231
  const isNextQuantumSig = signatureQuantum === currentQuantum + QUANTUM_WINDOW_SECONDS;
@@ -6186,6 +6242,44 @@ async function awaitSafeSubmissionWindow(quantumTimestamp, opts = {}) {
6186
6242
  }
6187
6243
  return { waited: false, waitedSeconds: 0 };
6188
6244
  }
6245
+ async function awaitSafeSubmissionWindowOnChain(signatureQuantum, chainNow, sleep) {
6246
+ let waitedSeconds = 0;
6247
+ for (let poll = 0; poll < MAX_GATE_POLLS; poll++) {
6248
+ let head;
6249
+ try {
6250
+ head = await chainNow();
6251
+ } catch {
6252
+ head = null;
6253
+ }
6254
+ if (head === null || !Number.isFinite(head)) {
6255
+ return waitedSeconds > 0 ? { waited: true, waitedSeconds } : null;
6256
+ }
6257
+ const chainQuantum = Math.floor(head / QUANTUM_WINDOW_SECONDS) * QUANTUM_WINDOW_SECONDS;
6258
+ if (signatureQuantum <= chainQuantum) {
6259
+ return { waited: waitedSeconds > 0, waitedSeconds };
6260
+ }
6261
+ const earliestMine = head + SLOT_SECONDS;
6262
+ const latestMine = head + SLOT_SECONDS * INCLUSION_SLOT_TOLERANCE;
6263
+ const safeBeforeDeadZone = latestMine < signatureQuantum - DEAD_ZONE_SECONDS;
6264
+ const safeAfterBoundary = earliestMine >= signatureQuantum;
6265
+ if (safeBeforeDeadZone || safeAfterBoundary) {
6266
+ return { waited: waitedSeconds > 0, waitedSeconds };
6267
+ }
6268
+ const sleepSeconds = Math.max(
6269
+ 1,
6270
+ Math.min(SLOT_SECONDS, signatureQuantum - head)
6271
+ );
6272
+ log.debug(
6273
+ `[Quantum Timing] \u23F3 Deferring send ${sleepSeconds}s to clear the quantum dead zone (sig quantum ${signatureQuantum}, chain head ${head}, insured mine window [${earliestMine}, ${latestMine}])`
6274
+ );
6275
+ await sleep(sleepSeconds * 1e3);
6276
+ waitedSeconds += sleepSeconds;
6277
+ }
6278
+ log.debug(
6279
+ `[Quantum Timing] \u26A0\uFE0F Dead-zone gate exhausted ${MAX_GATE_POLLS} chain polls (sig quantum ${signatureQuantum}) \u2014 proceeding`
6280
+ );
6281
+ return { waited: waitedSeconds > 0, waitedSeconds };
6282
+ }
6189
6283
 
6190
6284
  // src/utils/mint-authorization.utils.ts
6191
6285
  var PKP_NFT_ABI = [
@@ -7142,6 +7236,20 @@ function decodeQuantumRevert(e) {
7142
7236
  }
7143
7237
  return { data, selector, name };
7144
7238
  }
7239
+ var RETRYABLE_QUANTUM_ERROR_NAMES = [
7240
+ "DeadZoneViolation",
7241
+ "QuantumOutsideWindow",
7242
+ "QuantumAlreadyUsed",
7243
+ "QuantumExpired"
7244
+ ];
7245
+ function isRetryableQuantumFailure(message) {
7246
+ if (!message)
7247
+ return false;
7248
+ if (message.includes("Transaction timeout") || message.includes("Transaction confirmation failed") || message.includes("Transaction receipt not found")) {
7249
+ return false;
7250
+ }
7251
+ return RETRYABLE_QUANTUM_ERROR_NAMES.some((name) => message.includes(name)) || message.includes("Transaction reverted");
7252
+ }
7145
7253
 
7146
7254
  // src/utils/quantum-submission.utils.ts
7147
7255
  var MAX_DEADZONE_RESIMULATIONS = 3;
@@ -7163,9 +7271,14 @@ async function assertSafeQuantumSubmission(opts) {
7163
7271
  const now = opts.now ?? (() => Math.floor(Date.now() / 1e3));
7164
7272
  const onDebug = opts.onDebug ?? (() => {
7165
7273
  });
7274
+ const chainNow = opts.chainNow ?? (typeof provider.getBlock === "function" ? async () => {
7275
+ const block = await provider.getBlock("latest");
7276
+ return typeof block?.timestamp === "number" ? block.timestamp : null;
7277
+ } : void 0);
7166
7278
  const gateOpts = {
7167
7279
  now,
7168
- ...opts.sleep ? { sleep: opts.sleep } : {}
7280
+ ...opts.sleep ? { sleep: opts.sleep } : {},
7281
+ ...chainNow ? { chainNow } : {}
7169
7282
  };
7170
7283
  await awaitSafeSubmissionWindow(quantumTimestamp, gateOpts);
7171
7284
  const SAME_BLOCK_INTERACTION_SELECTOR = "0x3b53d1db";
@@ -7250,7 +7363,7 @@ var defaultHttpGetJson = async (url) => {
7250
7363
  }
7251
7364
  return { status: res.status, body };
7252
7365
  };
7253
- async function reconcileAuthorizedSpend(spend, esploraBaseUrl, httpGetJson = defaultHttpGetJson) {
7366
+ async function reconcileAuthorizedSpend(spend, esploraBaseUrl, httpGetJson = defaultHttpGetJson, opts) {
7254
7367
  const base = esploraBaseUrl.replace(/\/+$/, "");
7255
7368
  const fundingRes = await httpGetJson(`${base}/tx/${spend.txid}`);
7256
7369
  if (fundingRes.status === 404) {
@@ -7282,20 +7395,28 @@ async function reconcileAuthorizedSpend(spend, esploraBaseUrl, httpGetJson = def
7282
7395
  };
7283
7396
  }
7284
7397
  const onChainOutputValue = output.value;
7285
- if (onChainOutputValue !== spend.satoshis) {
7286
- return {
7287
- spend,
7288
- status: "CORRUPT",
7289
- onChainOutputValue,
7290
- reason: `Authorization declares ${spend.satoshis} sats for ${spend.txid}:${spend.vout} but the on-chain output is ${onChainOutputValue} sats`
7291
- };
7398
+ if (opts?.vaultAddress) {
7399
+ const outputAddress = (output.scriptpubkey_address ?? "").toLowerCase();
7400
+ if (outputAddress !== opts.vaultAddress.toLowerCase()) {
7401
+ return {
7402
+ spend,
7403
+ status: "CORRUPT",
7404
+ onChainOutputValue,
7405
+ reason: `Authorized outpoint ${spend.txid}:${spend.vout} pays ${output.scriptpubkey_address ?? "an unknown script"}, not the vault ${opts.vaultAddress}`
7406
+ };
7407
+ }
7408
+ } else {
7409
+ log.warn(
7410
+ "Withdrawal reconciliation: no vaultAddress supplied \u2014 skipping the vault-ownership check",
7411
+ { txid: spend.txid, vout: spend.vout }
7412
+ );
7292
7413
  }
7293
- if (spend.targetAmount > onChainOutputValue) {
7414
+ if (spend.targetAmount >= spend.satoshis) {
7294
7415
  return {
7295
7416
  spend,
7296
7417
  status: "CORRUPT",
7297
7418
  onChainOutputValue,
7298
- reason: `Authorized targetAmount ${spend.targetAmount} sats exceeds the ${onChainOutputValue}-sat output it is authorized against`
7419
+ reason: `Authorized targetAmount ${spend.targetAmount} sats is not below the ${spend.satoshis}-sat aggregate input value \u2014 the contract could never have authorized this`
7299
7420
  };
7300
7421
  }
7301
7422
  const outspendRes = await httpGetJson(
@@ -7791,13 +7912,13 @@ function isValidBase58CheckAddress(address) {
7791
7912
  return BASE58_VERSIONS_MAINNET.has(version) || BASE58_VERSIONS_TEST.has(version);
7792
7913
  }
7793
7914
  function isValidBech32Address(address) {
7794
- const lower = address.toLowerCase();
7795
- if (lower !== address && address !== address.toUpperCase()) {
7915
+ const lower2 = address.toLowerCase();
7916
+ if (lower2 !== address && address !== address.toUpperCase()) {
7796
7917
  return false;
7797
7918
  }
7798
7919
  const tryDecode = (codec) => {
7799
7920
  try {
7800
- const decoded = codec.decode(lower);
7921
+ const decoded = codec.decode(lower2);
7801
7922
  if (!BECH32_HRPS.has(decoded.prefix))
7802
7923
  return false;
7803
7924
  if (decoded.words.length < 1)
@@ -11852,7 +11973,7 @@ var GraphClient = class {
11852
11973
  });
11853
11974
  clearTimeout(timeout);
11854
11975
  if (!res.ok) {
11855
- if (res.status === 429 || res.status >= 500) {
11976
+ if (res.status >= 500) {
11856
11977
  await this.backoff(attempt);
11857
11978
  continue;
11858
11979
  }
@@ -11861,12 +11982,7 @@ var GraphClient = class {
11861
11982
  }
11862
11983
  const data = await res.json();
11863
11984
  if (data.errors?.length) {
11864
- const errMsg = JSON.stringify(data.errors);
11865
- if (errMsg.includes("rate limit") && attempt < this.maxRetries) {
11866
- await this.backoff(attempt);
11867
- continue;
11868
- }
11869
- throw new Error(`GraphQL errors: ${errMsg}`);
11985
+ throw new Error(`GraphQL errors: ${JSON.stringify(data.errors)}`);
11870
11986
  }
11871
11987
  return data.data ?? null;
11872
11988
  } catch (err) {
@@ -16782,6 +16898,50 @@ var _abi2 = [
16782
16898
  name: "AutoRepayEnabled",
16783
16899
  type: "event"
16784
16900
  },
16901
+ {
16902
+ anonymous: false,
16903
+ inputs: [
16904
+ {
16905
+ indexed: true,
16906
+ internalType: "bytes32",
16907
+ name: "positionId",
16908
+ type: "bytes32"
16909
+ },
16910
+ {
16911
+ indexed: true,
16912
+ internalType: "address",
16913
+ name: "borrower",
16914
+ type: "address"
16915
+ }
16916
+ ],
16917
+ name: "AutoWithdrawDisabled",
16918
+ type: "event"
16919
+ },
16920
+ {
16921
+ anonymous: false,
16922
+ inputs: [
16923
+ {
16924
+ indexed: true,
16925
+ internalType: "bytes32",
16926
+ name: "positionId",
16927
+ type: "bytes32"
16928
+ },
16929
+ {
16930
+ indexed: true,
16931
+ internalType: "address",
16932
+ name: "borrower",
16933
+ type: "address"
16934
+ },
16935
+ {
16936
+ indexed: false,
16937
+ internalType: "uint32",
16938
+ name: "minWithdrawRatioBps",
16939
+ type: "uint32"
16940
+ }
16941
+ ],
16942
+ name: "AutoWithdrawEnabled",
16943
+ type: "event"
16944
+ },
16785
16945
  {
16786
16946
  anonymous: false,
16787
16947
  inputs: [
@@ -17100,6 +17260,19 @@ var _abi2 = [
17100
17260
  stateMutability: "view",
17101
17261
  type: "function"
17102
17262
  },
17263
+ {
17264
+ inputs: [],
17265
+ name: "SCOPE_WITHDRAW",
17266
+ outputs: [
17267
+ {
17268
+ internalType: "uint32",
17269
+ name: "",
17270
+ type: "uint32"
17271
+ }
17272
+ ],
17273
+ stateMutability: "view",
17274
+ type: "function"
17275
+ },
17103
17276
  {
17104
17277
  inputs: [],
17105
17278
  name: "UPGRADE_INTERFACE_VERSION",
@@ -17290,6 +17463,40 @@ var _abi2 = [
17290
17463
  stateMutability: "view",
17291
17464
  type: "function"
17292
17465
  },
17466
+ {
17467
+ inputs: [
17468
+ {
17469
+ internalType: "bytes32",
17470
+ name: "positionId",
17471
+ type: "bytes32"
17472
+ },
17473
+ {
17474
+ internalType: "address",
17475
+ name: "caller",
17476
+ type: "address"
17477
+ }
17478
+ ],
17479
+ name: "canWithdraw",
17480
+ outputs: [
17481
+ {
17482
+ internalType: "bool",
17483
+ name: "ok",
17484
+ type: "bool"
17485
+ },
17486
+ {
17487
+ internalType: "enum IAgentDelegationRegistry.DenyReason",
17488
+ name: "reason",
17489
+ type: "uint8"
17490
+ },
17491
+ {
17492
+ internalType: "uint32",
17493
+ name: "effectiveFloorBps",
17494
+ type: "uint32"
17495
+ }
17496
+ ],
17497
+ stateMutability: "view",
17498
+ type: "function"
17499
+ },
17293
17500
  {
17294
17501
  inputs: [],
17295
17502
  name: "core",
@@ -17342,6 +17549,19 @@ var _abi2 = [
17342
17549
  stateMutability: "nonpayable",
17343
17550
  type: "function"
17344
17551
  },
17552
+ {
17553
+ inputs: [
17554
+ {
17555
+ internalType: "bytes32",
17556
+ name: "positionId",
17557
+ type: "bytes32"
17558
+ }
17559
+ ],
17560
+ name: "disableAutoWithdraw",
17561
+ outputs: [],
17562
+ stateMutability: "nonpayable",
17563
+ type: "function"
17564
+ },
17345
17565
  {
17346
17566
  inputs: [
17347
17567
  {
@@ -17386,6 +17606,24 @@ var _abi2 = [
17386
17606
  stateMutability: "nonpayable",
17387
17607
  type: "function"
17388
17608
  },
17609
+ {
17610
+ inputs: [
17611
+ {
17612
+ internalType: "bytes32",
17613
+ name: "positionId",
17614
+ type: "bytes32"
17615
+ },
17616
+ {
17617
+ internalType: "uint32",
17618
+ name: "minWithdrawRatioBps",
17619
+ type: "uint32"
17620
+ }
17621
+ ],
17622
+ name: "enableAutoWithdraw",
17623
+ outputs: [],
17624
+ stateMutability: "nonpayable",
17625
+ type: "function"
17626
+ },
17389
17627
  {
17390
17628
  inputs: [
17391
17629
  {
@@ -17412,6 +17650,11 @@ var _abi2 = [
17412
17650
  internalType: "uint32",
17413
17651
  name: "minCollateralRatioBps",
17414
17652
  type: "uint32"
17653
+ },
17654
+ {
17655
+ internalType: "uint32",
17656
+ name: "minWithdrawRatioBps",
17657
+ type: "uint32"
17415
17658
  }
17416
17659
  ],
17417
17660
  internalType: "struct IAgentDelegationRegistry.LoanGrant",
@@ -17896,7 +18139,7 @@ var _abi2 = [
17896
18139
  type: "function"
17897
18140
  }
17898
18141
  ];
17899
- var _bytecode2 = "0x60a080604052346100ea57306080527ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a005460ff8160401c166100d9576002600160401b03196001600160401b03821601610073575b604051612d3290816100f0823960805181818161101d01526112020152f35b6001600160401b0319166001600160401b039081177ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a005581527fc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d290602090a13880610054565b63f92ee8a960e01b60005260046000fd5b600080fdfe608080604052600436101561001357600080fd5b60003560e01c90816301ffc9a714611dec575080630d33f3c214611dc85780630f3f4a9414611d7d5780631459457a146119d757806318c38b89146118c65780631d444541146117f5578063248a9ca3146117cf5780632ec22d4c1461170e5780632f0647ce146116935780632f2ff15d1461166257806336568abe1461161c578063392f1087146115cf5780633f4ba83a146114d75780633f5aadc9146114685780634f1ef28614611186578063523403631461109d5780635273b6f61461107457806352d1902d1461100a578063554c4f4b14610f7a578063561b7f8a14610e4e5780635bc26aea14610dc15780635c975abb14610d915780636289d18c14610d105780636711cc0b14610cf45780636e852a7814610bf35780637e35bf6e14610bbf5780638343f67414610b925780638456cb5914610a9c5780638f33527f14610a625780639010d07c14610a0e57806391d14854146109b45780639b9d30c11461098b578063a19af3f6146108c9578063a217fddf146108ad578063a3246ad3146107eb578063a8780864146107cf578063ac3c0e30146106c2578063ad3cb1cc1461067b578063b3fad638146105ef578063c34e9c26146105d3578063ca15c8731461059a578063d547741f14610562578063dd71d99a14610520578063e0922fa114610473578063efa9b4ae14610404578063f2f4eb26146103db578063f68e9553146103b2578063fd437d39146102735763ffa1ad741461023a57600080fd5b3461026e57600036600319011261026e5761026a610256611fb9565b604051918291602083526020830190611f94565b0390f35b600080fd5b3461026e57606036600319011261026e5761028c611e6f565b610294611e59565b9061029d611f5b565b906102a661247b565b6001600160a01b0316338190036103a25780600052600160205260406000209283549360ff8560e01c16600381101561038c5760010361037b57816001602092610312877fc5d5c5bbf10444998e0ea6d4725e79027b574e1c14ec929279e8a9a95c6c6e919688612929565b818060a01b038316828060a01b03198254161781556103318782612111565b0180546001600160401b031916426001600160401b03908116919091179091556001600160a01b0391821660008181526002855260409081902087905551969091168652951693a4005b632437e77160e11b60005260046000fd5b634e487b7160e01b600052602160045260246000fd5b62fca64560e31b60005260046000fd5b3461026e57600036600319011261026e576020604051600080516020612cc68339815191528152f35b3461026e57600036600319011261026e576003546040516001600160a01b039091168152602090f35b3461026e57602036600319011261026e5760043561042061247b565b61044261042c826124c6565b9190600163ffffffff825460a01c161790612037565b6001600160a01b0316907f812690e0a9dff5bf3add998111b8af5c8299608d859025bfe35662060ab4267d600080a3005b3461026e57602036600319011261026e576004356001600160401b0381169081810361026e576000546001600160a01b038116330361050f57600160a01b600160e01b0319811660a092831b600160a01b600160e01b031617600055604080519190921c6001600160401b0316815260208101929092527f9c195448332abd01f716db873b329752ea02f19d33f7e311fb0daf082c4edd0391a1005b6349db3bd360e01b60005260046000fd5b3461026e57602036600319011261026e576001600160a01b03610541611e6f565b166000526002602052602060018060a01b0360406000205416604051908152f35b3461026e57604036600319011261026e57610598600435610581611e59565b9061059361058e82612016565b6123a5565b612437565b005b3461026e57602036600319011261026e57600435600052600080516020612bc68339815191526020526020604060002054604051908152f35b3461026e57600036600319011261026e57602060405160028152f35b3461026e57602036600319011261026e5760006040805161060f81611f02565b828152826020820152015260043560005260046020526060604060002063ffffffff6040519161063e83611f02565b548160018060a01b038216938481528160406020830192828660a01c168452019360c01c1683526040519485525116602084015251166040820152f35b3461026e57600036600319011261026e5761026a604080519061069e8183611f1d565b60058252640352e302e360dc1b602083015251918291602083526020830190611f94565b3461026e57602036600319011261026e576106db611e6f565b600060806040516106eb81611ed1565b828152826020820152826040820152826060820152015260018060a01b03166000526001602052604060002060405161072381611ed1565b81546001600160a01b038116825260a081901c6001600160401b03166020830190815292604083019160e01c60ff1690600382101561038c57908252600101546001600160401b0380821660608501908152604092831c821660808601908152925194516001600160a01b031685529451166020840152905192600384101561038c57604083019390935291516001600160401b0390811660608301529151909116608082015260a090f35b3461026e57600036600319011261026e57602060405160018152f35b3461026e57602036600319011261026e57600435600052600080516020612bc683398151915260205260406000206040518060208354918281520190819360005260206000209060005b818110610897575050508161084b910382611f1d565b6040519182916020830190602084525180915260408301919060005b818110610875575050500390f35b82516001600160a01b0316845285945060209384019390920191600101610867565b8254845260209093019260019283019201610835565b3461026e57600036600319011261026e57602060405160008152f35b3461026e57604036600319011261026e576004356108e5611ebe565b906108ee61247b565b63ffffffff600554169163ffffffff811692808410908115610982575b506109715760207f1da95b87b4c773f68c1447ffc5644e41a8a8887d55065beb64852014cf7140be9161095e610940856124c6565b9290610959600463ffffffff835460a01c161782612037565b611fdc565b6040519485526001600160a01b031693a3005b634346c13160e01b60005260046000fd5b9050158461090b565b3461026e57600036600319011261026e576020604051600080516020612c068339815191528152f35b3461026e57604036600319011261026e576109cd611e59565b600435600052600080516020612ca683398151915260205260406000209060018060a01b0316600052602052602060ff604060002054166040519015158152f35b3461026e57604036600319011261026e57600435600052600080516020612bc68339815191526020526020610a4960243560406000206129f2565b905460405160039290921b1c6001600160a01b03168152f35b3461026e57602036600319011261026e576001600160a01b03610a83611e6f565b1660005260026020526020604060002054604051908152f35b3461026e57600036600319011261026e573360009081527f0ac8cc3d209806126662940717264973110c39045b305f6ba49613325a615e09602052604090205460ff1615610b6b57610aec61247b565b600160ff19600080516020612ce6833981519152541617600080516020612ce6833981519152557f62e78cea01bee320cd4e420270b5ea74000d11b0c9f74754ebdbfc544b05a2586020604051338152a16040514281527f80170b5fcdd2bf1e0660ef4b8851f86685f64d41b1a19de1471947ece8725aac60203392a2005b63e2517d3f60e01b60005233600452600080516020612c0683398151915260245260446000fd5b3461026e57600036600319011261026e5760005460405160a09190911c6001600160401b03168152602090f35b3461026e57604036600319011261026e57610be3610bdb611e59565b6004356120de565b9061026a60405192839283611e8f565b3461026e57604036600319011261026e57610c0c611e6f565b336000908152600080516020612c46833981519152602052604090205460ff161580610ce1575b6103a2576001600160a01b03166000818152600160205260409020805460e01c60ff16600381101561038c5760010361037b57805460ff60e01b198116600160e11b1782556001919091018054600160401b600160801b03191642604090811b600160401b600160801b0316919091179091555160243581526001600160a01b0390911691907fc0499cc07ed9d734deec6857802d3f0fa8d5afaf79a7008beb8ba7bef62a271b90602090a3005b50336001600160a01b0382161415610c33565b3461026e57600036600319011261026e57602060405160048152f35b3461026e57602036600319011261026e57610d29611eab565b6000546001600160a01b0316330361050f5763ffffffff8116908115610d8057600080516020612c8683398151915291610d7b63ffffffff926005549284198416176005556040519384931683611fff565b0390a1005b6338f3377360e21b60005260046000fd5b3461026e57600036600319011261026e57602060ff600080516020612ce683398151915254166040519015158152f35b3461026e57602036600319011261026e57610dda611e6f565b6000546001600160a01b038116903382900361050f576001600160a01b038316928315610e3d573b15610e2c576001600160a01b03191682176000908155600080516020612c668339815191529080a3005b636f7c43f160e01b60005260046000fd5b63e6c4247b60e01b60005260046000fd5b3461026e57606036600319011261026e57610e67611e6f565b610e6f611e59565b610e77611f5b565b91610e8061247b565b6001600160a01b031690338290036103a257816000526001602052604060002060ff815460e01c16600381101561038c57600114610f695781602091610ee8867ff223dc085722c94c84dcf67427e225799be616382a2a484d378f8921c69a20339587612929565b80546001600160a01b0319166001600160a01b038316178155610f0b8682612111565b6001810180546001600160801b031916426001600160401b0390811691909117909155815460ff60e01b1916600160e01b179091556001600160a01b03919091166000818152600284526040908190208690555195909116855293a3005b639d2c8fdf60e01b60005260046000fd5b3461026e57602036600319011261026e576001600160a01b03610f9b611e6f565b166000526001602052604060002060ff600080516020612ce683398151915254161580610fee575b60209181610fd7575b506040519015158152f35b5460a01c6001600160401b03164210905082610fcc565b5060ff815460e01c1690600382101561038c5790600114610fc3565b3461026e57600036600319011261026e577f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03163003611063576020604051600080516020612c268339815191528152f35b63703e46dd60e11b60005260046000fd5b3461026e57600036600319011261026e576000546040516001600160a01b039091168152602090f35b3461026e57602036600319011261026e57600435604060008281526004602052209081549160018060a01b038316928315908115611177575b5061116657336000908152600080516020612c46833981519152602052604090205460ff16158061115c575b61114b578061111e63fffffffe611124935460a01c1682612037565b82612381565b7fa5ffbdba01b15df97334e34ad816b9986b6057a5ddc97aba93b7e8ccd21759ed600080a3005b630a606ec360e11b60005260046000fd5b5082331415611102565b6360eb6c2360e01b60005260046000fd5b6001915060a01c1615846110d6565b604036600319011261026e5761119a611e6f565b602435906001600160401b03821161026e573660238301121561026e578160040135906111c682611f40565b916111d46040519384611f1d565b8083526020830193366024838301011161026e57816000926024602093018737840101526001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016308114908115611445575b50611063576000546001600160a01b0316330361050f576001600160a01b038116928315610e3d57813b156114345730841461142557600080516020612c26833981519152548461127c611fb9565b7f6ea8b34d59b2818d3f37a08dddaa06f5886b9c20c907b36f03ca6528e106e86f6112b260405192604084526040840190611f94565b4260208401526001600160a01b038516929081900390a36040516352d1902d60e01b8152602081600481895afa600091816113f1575b506113025785634c9c8ce360e01b60005260045260246000fd5b9081600080516020612c268339815191528793036113dd5750833b156113c8576001600160a01b0319168117600080516020612c26833981519152557fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b600080a28251156113ad5760008091610598945190845af43d156113a5573d9161138883611f40565b926113966040519485611f1d565b83523d6000602085013e612b64565b606091612b64565b505050346113b757005b63b398979f60e01b60005260046000fd5b50634c9c8ce360e01b60005260045260246000fd5b632a87526960e21b60005260045260246000fd5b9091506020813d60201161141d575b8161140d60209383611f1d565b8101031261026e575190876112e8565b3d9150611400565b614e8d60eb1b60005260046000fd5b632e07c4bd60e11b60005260046000fd5b600080516020612c26833981519152546001600160a01b0316141590508461122d565b3461026e57602036600319011261026e5760043561148461247b565b6114a6611490826124c6565b9190600263ffffffff825460a01c161790612037565b6001600160a01b0316907fc2713e691344add4705edc62a0ee5aede95ba66ad68f97316efef9cb50644edd600080a3005b3461026e57600036600319011261026e57336000908152600080516020612c46833981519152602052604090205460ff1615806115ba575b6115a957600080516020612ce68339815191525460ff8116156115985760ff1916600080516020612ce6833981519152557f5db9ee0a495bf2e6ff9c91a7834c1ba4fdd244a5e8aa4e537bd38aeae4b073aa6020604051338152a16040514281527f107553d8191d85b405879cf752997865edd48d94e20bda4dd27223c94b31a7cc60203392a2005b638dfc202b60e01b60005260046000fd5b63659a07b360e11b60005260046000fd5b506000546001600160a01b031633141561150f565b3461026e57604036600319011261026e57606063ffffffff6115fa6115f2611e59565b60043561205a565b90604093929351931515845261160f81611e85565b6020840152166040820152f35b3461026e57604036600319011261026e57611635611e59565b336001600160a01b038216036116515761059890600435612437565b63334bd91960e11b60005260046000fd5b3461026e57604036600319011261026e57610598600435611681611e59565b9061168e61058e82612016565b6123ef565b3461026e57602036600319011261026e576004356116dd6116b38261230f565b91906116c963fffffffb825460a01c1682612037565b805463ffffffff60c01b1916815583612381565b6001600160a01b0316907f09cb4ec44cdef8489412844b9682f87f7900758fa2bd7e039113e9125e538e0f600080a3005b3461026e57602036600319011261026e57600435604060008281526004602052209081549160018060a01b0383169283159081156117c0575b5061116657336000908152600080516020612c46833981519152602052604090205460ff1615806117b6575b61114b578061111e63fffffffd61178f935460a01c1682612037565b7fb390ab029eab314bd18190ebe73d7e5c1c336beaa5c83ac22578378dc9ccc405600080a3005b5082331415611773565b6002915060a01c161584611747565b3461026e57602036600319011261026e5760206117ed600435612016565b604051908152f35b3461026e57604036600319011261026e57600435611811611ebe565b63ffffffff600554169063ffffffff8116918083109081156118bd575b506109715761183c8361230f565b909263ffffffff845460c01c16809260018060a01b031633141591826118b2575b50506118a15761188d827e8a218591d4362499c38c38eb1c12e7a7cc97a9068818d1fe6046a42ba4a5ba94611fdc565b61189c60405192839283611fff565b0390a2005b634e80a40760e11b60005260046000fd5b11159050818661185d565b9050158461182e565b3461026e57602036600319011261026e576118df611eab565b600080516020612d068339815191525460ff8160401c169081156119bf575b506119ae57600080516020612d06833981519152805460026001600160401b0319821681179092556000546001600160481b031990911690911791906001600160a01b0316330361050f5763ffffffff168015610d8057604081600080516020612c868339815191529263ffffffff196005541617600555815190600082526020820152a1600080516020612d0683398151915255600080516020612be6833981519152602060405160028152a1005b63f92ee8a960e01b60005260046000fd5b60026001600160401b039190911610159050826118fe565b3461026e5760a036600319011261026e576119f0611e6f565b6119f8611e59565b906044356001600160a01b03811680820361026e576064356001600160a01b0381169182820361026e576084356001600160a01b038116949085900361026e57600080516020612d0683398151915254604081901c60ff161596906001600160401b031680159081611d75575b6001149081611d6b575b159081611d62575b506119ae57600080516020612d0683398151915280546001600160401b031916600117905586611d3d575b611aaa6127b2565b611ab26127b2565b611aba6127b2565b611ac26127b2565b6001600160a01b038116908115610e3d578315610e3d578515610e3d576001600160a01b038916988915610e3d573b15610e2c57858414611d2c5762aa36a746148015611d21575b8015611d16575b1580611d04575b610e2c57611b279060006127e0565b9081611cbd575b5015611c0257611b4c90600080516020612cc68339815191526127e0565b9081611c68575b5015611c0257611b7190600080516020612c068339815191526127e0565b9081611c13575b5015611c0257600080546001600160a01b031916841781556040519390600080516020612c668339815191528180a38015610e3d57803b15610e2c57600380546001600160a01b031916919091179055611bce57005b600080516020612d06833981519152805460ff60401b1916905560018152600080516020612be683398151915290602090a1005b63cf5a8e3360e01b60005260046000fd5b600080516020612c06833981519152600052600080516020612bc6833981519152602052611c61907ffff5b43478fa6840382eb63777e0da6941d320b940e3f70f0ea4780feff95827612a20565b5084611b78565b600080516020612cc6833981519152600052600080516020612bc6833981519152602052611cb6907f32d31393d541a265984ec379cddd8b1fcbe5f2b0eb8ad81dddfead454f135eaa612a20565b5086611b53565b60008052600080516020612bc6833981519152602052611cfd907f615f0f9e84155bea8cc509fe18befeb1baf65611e38a6ba60964480fb29dfd44612a20565b5088611b2e565b50823b1580611b185750843b15611b18565b50617a694614611b11565b506105394614611b0a565b638135316560e01b60005260046000fd5b600080516020612d06833981519152805460ff60401b1916600160401b179055611aa2565b90501589611a77565b303b159150611a6f565b889150611a65565b3461026e57604036600319011261026e5761026a611db1611d9c611e59565b6004356000526004602052604060002061213a565b611dba81611e85565b604051918183921583611e8f565b3461026e57600036600319011261026e57602063ffffffff60055416604051908152f35b3461026e57602036600319011261026e576004359063ffffffff60e01b821680920361026e57602091635a05180f60e01b8114908115611e2e575b5015158152f35b637965db0b60e01b811491508115611e48575b5083611e27565b6301ffc9a760e01b14905083611e41565b602435906001600160a01b038216820361026e57565b600435906001600160a01b038216820361026e57565b6008111561038c57565b901515815260408101929190602090611ea783611e85565b0152565b6004359063ffffffff8216820361026e57565b6024359063ffffffff8216820361026e57565b60a081019081106001600160401b03821117611eec57604052565b634e487b7160e01b600052604160045260246000fd5b606081019081106001600160401b03821117611eec57604052565b601f909101601f19168101906001600160401b03821190821017611eec57604052565b6001600160401b038111611eec57601f01601f191660200190565b604435906001600160401b038216820361026e57565b60005b838110611f845750506000910152565b8181015183820152602001611f74565b90602091611fad81518092818552858086019101611f71565b601f01601f1916010190565b60405190611fc8604083611f1d565b60058252640322e302e360dc1b6020830152565b805463ffffffff60c01b191660c09290921b63ffffffff60c01b16919091179055565b63ffffffff91821681529116602082015260400190565b600052600080516020612ca683398151915260205260016040600020015490565b805463ffffffff60a01b191660a09290921b63ffffffff60a01b16919091179055565b9190916000526004602052612074604060002092836121f7565b9161207e83611e85565b826120d45763ffffffff9192505460c01c1663ffffffff60055416811580156120cc575b6120bf578082106120b857505b60019160009190565b90506120af565b5050600090600390600090565b5080156120a2565b5060009190600090565b906120f59160005260046020526040600020612283565b6120fe81611e85565b8061210c5750600190600090565b600091565b8054600160a01b600160e01b03191660a09290921b600160a01b600160e01b0316919091179055565b60ff600080516020612ce683398151915254166121f057546001600160a01b0381169081156121e85760a01c600216156121e15760005260016020526040600020549060ff8260e01c16600381101561038c5780156121d9576002146121d2574260a083901c6001600160401b031611156121cb576001600160a01b039081169116036121c657600090565b600790565b5050600690565b5050600590565b505050600490565b5050600390565b505050600290565b5050600190565b60ff600080516020612ce683398151915254166121f057546001600160a01b0381169081156121e85760a01c600416156121e15760005260016020526040600020549060ff8260e01c16600381101561038c5780156121d9576002146121d2574260a083901c6001600160401b031611156121cb576001600160a01b039081169116036121c657600090565b60ff600080516020612ce683398151915254166121f057546001600160a01b0381169081156121e85760a01c600116156121e15760005260016020526040600020549060ff8260e01c16600381101561038c5780156121d9576002146121d2574260a083901c6001600160401b031611156121cb576001600160a01b039081169116036121c657600090565b6000908152600460205260409020805490916001600160a01b038216918215908115612372575b5061116657336000908152600080516020612c46833981519152602052604090205460ff161580612368575b61114b57565b5081331415612362565b6004915060a01c161538612336565b905460a01c63ffffffff16156123945750565b600052600460205260006040812055565b6000818152600080516020612ca68339815191526020908152604080832033845290915290205460ff16156123d75750565b63e2517d3f60e01b6000523360045260245260446000fd5b6123f982826127e0565b918261240457505090565b6000918252600080516020612bc68339815191526020526040909120612433916001600160a01b031690612a20565b5090565b6124418282612889565b918261244c57505090565b6000918252600080516020612bc68339815191526020526040909120612433916001600160a01b031690612a91565b60ff600080516020612ce6833981519152541661249457565b63d93c066560e01b60005260046000fd5b519064ffffffffff8216820361026e57565b519061ffff8216820361026e57565b80156127a157600354604051638d34875560e01b8152600481018390529190600090839060249082906001600160a01b03165afa918215612795576000926125ea575b5060808201516001600160a01b0316913383900361114b57610120015161252f81611e85565b61253881611e85565b600381149081156125d5575b81156125c1575b506125b05781600052600160205260ff60406000205460e01c16600381101561038c5760010361259f57600090815260046020526040902080546001600160a01b0319166001600160a01b03831617815591565b632e68d1d360e21b60005260046000fd5b63e9f6db8560e01b60005260046000fd5b600791506125ce81611e85565b143861254b565b90506125e081611e85565b6005811490612544565b90913d8082843e6125fb8184611f1d565b820191602081840312612775578051906001600160401b03821161277d5701906101a08284031261277257604051926101a084016001600160401b038111858210176127815760409081528351855260208085015190860152838101519085015260608301516001600160401b03811161277d57830181601f8201121561277d5780519061268882611f40565b926126966040519485611f1d565b8284526020838301011161277957906126b59160208085019101611f71565b606084015260808201516001600160a01b03811681036127755760808401526126e060a083016124a5565b60a08401526126f160c083016124a5565b60c084015261270260e083016124b7565b60e084015261271461010083016124a5565b6101008401526101208201519060088210156127725750612765916101809161012085015261274661014082016124a5565b61014085015261275961016082016124b7565b610160850152016124a5565b6101808201529038612509565b80fd5b5080fd5b8380fd5b8280fd5b634e487b7160e01b83526041600452602483fd5b6040513d6000823e3d90fd5b637bb0b62160e01b60005260046000fd5b60ff600080516020612d068339815191525460401c16156127cf57565b631afcd79f60e31b60005260046000fd5b6000818152600080516020612ca6833981519152602090815260408083206001600160a01b038616845290915290205460ff16612882576000818152600080516020612ca6833981519152602090815260408083206001600160a01b0395909516808452949091528120805460ff19166001179055339291907f2f8788117e7eff1d82e926ec794901d17c78024a50270940304540a733656f0d9080a4600190565b5050600090565b6000818152600080516020612ca6833981519152602090815260408083206001600160a01b038616845290915290205460ff1615612882576000818152600080516020612ca6833981519152602090815260408083206001600160a01b0395909516808452949091528120805460ff19169055339291907ff6391f5c32d9c69d2a47ea670b442974b53935d1edc7fd64eb21e047a839171b9080a4600190565b1580156129e1575b610e3d576001600160a01b03166000908152600260205260409020546129d0576001600160401b0316428111156129bf5760005460a01c6001600160401b03168015159182612994575b505061298357565b63e774e7b560e01b60005260046000fd5b9091504281039081116129a95711388061297b565b634e487b7160e01b600052601160045260246000fd5b63efa7aefd60e01b60005260046000fd5b633b53f64f60e21b60005260046000fd5b506001600160a01b03811615612931565b8054821015612a0a5760005260206000200190600090565b634e487b7160e01b600052603260045260246000fd5b6001810190826000528160205260406000205415600014612a89578054600160401b811015611eec57612a74612a5d8260018794018555846129f2565b819391549060031b91821b91600019901b19161790565b90555491600052602052604060002055600190565b505050600090565b9060018201918160005282602052604060002054801515600014612b5b5760001981018181116129a95782546000198101919082116129a957818103612b24575b50505080548015612b0e576000190190612aec82826129f2565b8154906000199060031b1b191690555560005260205260006040812055600190565b634e487b7160e01b600052603160045260246000fd5b612b44612b34612a5d93866129f2565b90549060031b1c928392866129f2565b905560005283602052604060002055388080612ad2565b50505050600090565b90612b8a5750805115612b7957602081519101fd5b63d6bda27560e01b60005260046000fd5b81511580612bbc575b612b9b575090565b639996b31560e01b60009081526001600160a01b0391909116600452602490fd5b50803b15612b9356fec1f6fe24621ce81ec5827caf0253cadb74709b061630e6b55e82371705932000c7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d275027301df982f5215b4963a854cf9c73219df80cb6acd8f098189c9480c7bbc360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc07966f2ab141347034724e9b2a472c2f77f5cd745aeba27cacba797b8fcb708617dc5b1290f7495d7f8cc68476e6941aaf510598fa925d0d2720edc8805a846cd37bc77615afefc793aa17d75d4249a9b517ba91b0e54a49c9c4a44b3d7e228302dd7bc7dec4dceedda775e58dd541e08a116c6c53815c0bd028192f7b626800edcc084d3dcd65a1f7f23c65c46722faca6953d28e43150a467cf43e5c309238cd5ed15c6e187e77e9aee88184c21f4f2182ab5827cb3b7e07fbedcd63f03300f0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a00a164736f6c6343000822000a";
18142
+ var _bytecode2 = "0x60a080604052346100ea57306080527ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a005460ff8160401c166100d9576002600160401b03196001600160401b03821601610073575b60405161304c90816100f0823960805181818161114401526113290152f35b6001600160401b0319166001600160401b039081177ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a005581527fc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d290602090a13880610054565b63f92ee8a960e01b60005260046000fd5b600080fdfe608080604052600436101561001357600080fd5b60003560e01c90816301ffc9a714611ff7575080630d33f3c214611fd35780630f3f4a9414611f885780631459457a14611be257806318c38b8914611ad15780631d44454114611a005780632106764c146119d9578063248a9ca3146119b35780632ec22d4c146118f25780632f0647ce146118775780632f2ff15d14611846578063324207441461177257806336568abe1461172c578063392f1087146116f65780633f4ba83a146115fe5780633f5aadc91461158f5780634f1ef286146112ad57806352340363146111c45780635273b6f61461119b57806352d1902d14611131578063554c4f4b146110a1578063561b7f8a14610f755780635bc26aea14610ee85780635c975abb14610eb85780636289d18c14610e375780636711cc0b14610e1b5780636e852a7814610d1a5780637e35bf6e14610ce65780638343f67414610cb95780638456cb5914610bc35780638f33527f14610b895780639010d07c14610b3557806391d1485414610adb5780639b9d30c114610ab2578063a19af3f614610a01578063a217fddf146109e5578063a3246ad314610923578063a878086414610907578063ac3c0e30146107fa578063ad3cb1cc146107b3578063b3fad6381461070e578063c34e9c26146106f2578063c86deb31146106d6578063ca15c8731461069d578063d547741f14610665578063dd71d99a14610623578063e0922fa114610576578063efa9b4ae14610507578063f2f4eb26146104de578063f68e9553146104b5578063f94f67ab146103de578063fd437d391461029f5763ffa1ad741461026657600080fd5b3461029a57600036600319011261029a576102966102826121ed565b6040519182916020835260208301906121c8565b0390f35b600080fd5b3461029a57606036600319011261029a576102b861207a565b6102c0612064565b906102c961218f565b906102d2612795565b6001600160a01b0316338190036103ce5780600052600160205260406000209283549360ff8560e01c1660038110156103b8576001036103a75781600160209261033e877fc5d5c5bbf10444998e0ea6d4725e79027b574e1c14ec929279e8a9a95c6c6e919688612c43565b818060a01b038316828060a01b031982541617815561035d878261239f565b0180546001600160401b031916426001600160401b03908116919091179091556001600160a01b0391821660008181526002855260409081902087905551969091168652951693a4005b632437e77160e11b60005260046000fd5b634e487b7160e01b600052602160045260246000fd5b62fca64560e31b60005260046000fd5b3461029a57604036600319011261029a576004356103fa6120c9565b90610403612795565b63ffffffff600554169163ffffffff8116928084109081156104ac575b5061049b5760207f6b7727cc9c3df4a29acf0afe01594c1b7e845d20f40f4a14e4c292164a9ed8e191610452846127e0565b9161046a600863ffffffff845460a01c1617836122e3565b81546001600160e01b031660e09190911b6001600160e01b0319161790556040519485526001600160a01b031693a3005b634346c13160e01b60005260046000fd5b90501584610420565b3461029a57600036600319011261029a576020604051600080516020612fe08339815191528152f35b3461029a57600036600319011261029a576003546040516001600160a01b039091168152602090f35b3461029a57602036600319011261029a57600435610523612795565b61054561052f826127e0565b9190600163ffffffff825460a01c1617906122e3565b6001600160a01b0316907f812690e0a9dff5bf3add998111b8af5c8299608d859025bfe35662060ab4267d600080a3005b3461029a57602036600319011261029a576004356001600160401b0381169081810361029a576000546001600160a01b038116330361061257600160a01b600160e01b0319811660a092831b600160a01b600160e01b031617600055604080519190921c6001600160401b0316815260208101929092527f9c195448332abd01f716db873b329752ea02f19d33f7e311fb0daf082c4edd0391a1005b6349db3bd360e01b60005260046000fd5b3461029a57602036600319011261029a576001600160a01b0361064461207a565b166000526002602052602060018060a01b0360406000205416604051908152f35b3461029a57604036600319011261029a5761069b600435610684612064565b90610696610691826122c2565b6126bf565b612751565b005b3461029a57602036600319011261029a57600435600052600080516020612ee08339815191526020526020604060002054604051908152f35b3461029a57600036600319011261029a57602060405160088152f35b3461029a57600036600319011261029a57602060405160028152f35b3461029a57602036600319011261029a576000606060405161072f81612136565b828152826020820152826040820152015260043560005260046020526080604060002063ffffffff6040519161076483612136565b548160018060a01b038216938481528160208201818560a01c16815260606040840193838760c01c168552019460e01c8552604051968752511660208601525116604084015251166060820152f35b3461029a57600036600319011261029a5761029660408051906107d68183612151565b60058252640352e302e360dc1b6020830152519182916020835260208301906121c8565b3461029a57602036600319011261029a5761081361207a565b6000608060405161082381612105565b828152826020820152826040820152826060820152015260018060a01b03166000526001602052604060002060405161085b81612105565b81546001600160a01b038116825260a081901c6001600160401b03166020830190815292604083019160e01c60ff169060038210156103b857908252600101546001600160401b0380821660608501908152604092831c821660808601908152925194516001600160a01b03168552945116602084015290519260038410156103b857604083019390935291516001600160401b0390811660608301529151909116608082015260a090f35b3461029a57600036600319011261029a57602060405160018152f35b3461029a57602036600319011261029a57600435600052600080516020612ee083398151915260205260406000206040518060208354918281520190819360005260206000209060005b8181106109cf5750505081610983910382612151565b6040519182916020830190602084525180915260408301919060005b8181106109ad575050500390f35b82516001600160a01b031684528594506020938401939092019160010161099f565b825484526020909301926001928301920161096d565b3461029a57600036600319011261029a57602060405160008152f35b3461029a57604036600319011261029a57600435610a1d6120c9565b90610a26612795565b63ffffffff600554169163ffffffff811692808410908115610aa9575b5061049b5760207f1da95b87b4c773f68c1447ffc5644e41a8a8887d55065beb64852014cf7140be91610a96610a78856127e0565b9290610a91600463ffffffff835460a01c1617826122e3565b612210565b6040519485526001600160a01b031693a3005b90501584610a43565b3461029a57600036600319011261029a576020604051600080516020612f208339815191528152f35b3461029a57604036600319011261029a57610af4612064565b600435600052600080516020612fc083398151915260205260406000209060018060a01b0316600052602052602060ff604060002054166040519015158152f35b3461029a57604036600319011261029a57600435600052600080516020612ee08339815191526020526020610b706024356040600020612d0c565b905460405160039290921b1c6001600160a01b03168152f35b3461029a57602036600319011261029a576001600160a01b03610baa61207a565b1660005260026020526020604060002054604051908152f35b3461029a57600036600319011261029a573360009081527f0ac8cc3d209806126662940717264973110c39045b305f6ba49613325a615e09602052604090205460ff1615610c9257610c13612795565b600160ff19600080516020613000833981519152541617600080516020613000833981519152557f62e78cea01bee320cd4e420270b5ea74000d11b0c9f74754ebdbfc544b05a2586020604051338152a16040514281527f80170b5fcdd2bf1e0660ef4b8851f86685f64d41b1a19de1471947ece8725aac60203392a2005b63e2517d3f60e01b60005233600452600080516020612f2083398151915260245260446000fd5b3461029a57600036600319011261029a5760005460405160a09190911c6001600160401b03168152602090f35b3461029a57604036600319011261029a57610d0a610d02612064565b60043561236c565b906102966040519283928361209a565b3461029a57604036600319011261029a57610d3361207a565b336000908152600080516020612f60833981519152602052604090205460ff161580610e08575b6103ce576001600160a01b03166000818152600160205260409020805460e01c60ff1660038110156103b8576001036103a757805460ff60e01b198116600160e11b1782556001919091018054600160401b600160801b03191642604090811b600160401b600160801b0316919091179091555160243581526001600160a01b0390911691907fc0499cc07ed9d734deec6857802d3f0fa8d5afaf79a7008beb8ba7bef62a271b90602090a3005b50336001600160a01b0382161415610d5a565b3461029a57600036600319011261029a57602060405160048152f35b3461029a57602036600319011261029a57610e506120b6565b6000546001600160a01b031633036106125763ffffffff8116908115610ea757600080516020612fa083398151915291610ea263ffffffff926005549284198416176005556040519384931683612233565b0390a1005b6338f3377360e21b60005260046000fd5b3461029a57600036600319011261029a57602060ff60008051602061300083398151915254166040519015158152f35b3461029a57602036600319011261029a57610f0161207a565b6000546001600160a01b0381169033829003610612576001600160a01b038316928315610f64573b15610f53576001600160a01b03191682176000908155600080516020612f808339815191529080a3005b636f7c43f160e01b60005260046000fd5b63e6c4247b60e01b60005260046000fd5b3461029a57606036600319011261029a57610f8e61207a565b610f96612064565b610f9e61218f565b91610fa7612795565b6001600160a01b031690338290036103ce57816000526001602052604060002060ff815460e01c1660038110156103b857600114611090578160209161100f867ff223dc085722c94c84dcf67427e225799be616382a2a484d378f8921c69a20339587612c43565b80546001600160a01b0319166001600160a01b038316178155611032868261239f565b6001810180546001600160801b031916426001600160401b0390811691909117909155815460ff60e01b1916600160e01b179091556001600160a01b03919091166000818152600284526040908190208690555195909116855293a3005b639d2c8fdf60e01b60005260046000fd5b3461029a57602036600319011261029a576001600160a01b036110c261207a565b166000526001602052604060002060ff60008051602061300083398151915254161580611115575b602091816110fe575b506040519015158152f35b5460a01c6001600160401b031642109050826110f3565b5060ff815460e01c169060038210156103b857906001146110ea565b3461029a57600036600319011261029a577f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316300361118a576020604051600080516020612f408339815191528152f35b63703e46dd60e11b60005260046000fd5b3461029a57600036600319011261029a576000546040516001600160a01b039091168152602090f35b3461029a57602036600319011261029a57600435604060008281526004602052209081549160018060a01b03831692831590811561129e575b5061128d57336000908152600080516020612f60833981519152602052604090205460ff161580611283575b611272578061124563fffffffe61124b935460a01c16826122e3565b8261269b565b7fa5ffbdba01b15df97334e34ad816b9986b6057a5ddc97aba93b7e8ccd21759ed600080a3005b630a606ec360e11b60005260046000fd5b5082331415611229565b6360eb6c2360e01b60005260046000fd5b6001915060a01c1615846111fd565b604036600319011261029a576112c161207a565b602435906001600160401b03821161029a573660238301121561029a578160040135906112ed82612174565b916112fb6040519384612151565b8083526020830193366024838301011161029a57816000926024602093018737840101526001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001630811490811561156c575b5061118a576000546001600160a01b03163303610612576001600160a01b038116928315610f6457813b1561155b5730841461154c57600080516020612f4083398151915254846113a36121ed565b7f6ea8b34d59b2818d3f37a08dddaa06f5886b9c20c907b36f03ca6528e106e86f6113d9604051926040845260408401906121c8565b4260208401526001600160a01b038516929081900390a36040516352d1902d60e01b8152602081600481895afa60009181611518575b506114295785634c9c8ce360e01b60005260045260246000fd5b9081600080516020612f408339815191528793036115045750833b156114ef576001600160a01b0319168117600080516020612f40833981519152557fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b600080a28251156114d4576000809161069b945190845af43d156114cc573d916114af83612174565b926114bd6040519485612151565b83523d6000602085013e612e7e565b606091612e7e565b505050346114de57005b63b398979f60e01b60005260046000fd5b50634c9c8ce360e01b60005260045260246000fd5b632a87526960e21b60005260045260246000fd5b9091506020813d602011611544575b8161153460209383612151565b8101031261029a5751908761140f565b3d9150611527565b614e8d60eb1b60005260046000fd5b632e07c4bd60e11b60005260046000fd5b600080516020612f40833981519152546001600160a01b03161415905084611354565b3461029a57602036600319011261029a576004356115ab612795565b6115cd6115b7826127e0565b9190600263ffffffff825460a01c1617906122e3565b6001600160a01b0316907fc2713e691344add4705edc62a0ee5aede95ba66ad68f97316efef9cb50644edd600080a3005b3461029a57600036600319011261029a57336000908152600080516020612f60833981519152602052604090205460ff1615806116e1575b6116d0576000805160206130008339815191525460ff8116156116bf5760ff1916600080516020613000833981519152557f5db9ee0a495bf2e6ff9c91a7834c1ba4fdd244a5e8aa4e537bd38aeae4b073aa6020604051338152a16040514281527f107553d8191d85b405879cf752997865edd48d94e20bda4dd27223c94b31a7cc60203392a2005b638dfc202b60e01b60005260046000fd5b63659a07b360e11b60005260046000fd5b506000546001600160a01b0316331415611636565b3461029a57604036600319011261029a5761029661171d611715612064565b600435612306565b604093919351938493846120dc565b3461029a57604036600319011261029a57611745612064565b336001600160a01b038216036117615761069b90600435612751565b63334bd91960e11b60005260046000fd5b3461029a57602036600319011261029a57600435604060008281526004602052209081549160018060a01b038316928315908115611837575b5061128d57336000908152600080516020612f60833981519152602052604090205460ff16158061182d575b61127257806117f363fffffff7611806935460a01c16826122e3565b80546001600160e01b031681558261269b565b7f054490ec6650e07db9b97a06ea31d44cebd44b390c9975a380055e1b4d3cac21600080a3005b50823314156117d7565b6008915060a01c1615846117ab565b3461029a57604036600319011261029a5761069b600435611865612064565b90611872610691826122c2565b612709565b3461029a57602036600319011261029a576004356118c161189782612629565b91906118ad63fffffffb825460a01c16826122e3565b805463ffffffff60c01b191681558361269b565b6001600160a01b0316907f09cb4ec44cdef8489412844b9682f87f7900758fa2bd7e039113e9125e538e0f600080a3005b3461029a57602036600319011261029a57600435604060008281526004602052209081549160018060a01b0383169283159081156119a4575b5061128d57336000908152600080516020612f60833981519152602052604090205460ff16158061199a575b611272578061124563fffffffd611973935460a01c16826122e3565b7fb390ab029eab314bd18190ebe73d7e5c1c336beaa5c83ac22578378dc9ccc405600080a3005b5082331415611957565b6002915060a01c16158461192b565b3461029a57602036600319011261029a5760206119d16004356122c2565b604051908152f35b3461029a57604036600319011261029a5761029661171d6119f8612064565b60043561224a565b3461029a57604036600319011261029a57600435611a1c6120c9565b63ffffffff600554169063ffffffff811691808310908115611ac8575b5061049b57611a4783612629565b909263ffffffff845460c01c16809260018060a01b03163314159182611abd575b5050611aac57611a98827e8a218591d4362499c38c38eb1c12e7a7cc97a9068818d1fe6046a42ba4a5ba94612210565b611aa760405192839283612233565b0390a2005b634e80a40760e11b60005260046000fd5b111590508186611a68565b90501584611a39565b3461029a57602036600319011261029a57611aea6120b6565b6000805160206130208339815191525460ff8160401c16908115611bca575b50611bb957600080516020613020833981519152805460026001600160401b0319821681179092556000546001600160481b031990911690911791906001600160a01b031633036106125763ffffffff168015610ea757604081600080516020612fa08339815191529263ffffffff196005541617600555815190600082526020820152a160008051602061302083398151915255600080516020612f00833981519152602060405160028152a1005b63f92ee8a960e01b60005260046000fd5b60026001600160401b03919091161015905082611b09565b3461029a5760a036600319011261029a57611bfb61207a565b611c03612064565b906044356001600160a01b03811680820361029a576064356001600160a01b0381169182820361029a576084356001600160a01b038116949085900361029a5760008051602061302083398151915254604081901c60ff161596906001600160401b031680159081611f80575b6001149081611f76575b159081611f6d575b50611bb95760008051602061302083398151915280546001600160401b031916600117905586611f48575b611cb5612acc565b611cbd612acc565b611cc5612acc565b611ccd612acc565b6001600160a01b038116908115610f64578315610f64578515610f64576001600160a01b038916988915610f64573b15610f5357858414611f375762aa36a746148015611f2c575b8015611f21575b1580611f0f575b610f5357611d32906000612afa565b9081611ec8575b5015611e0d57611d5790600080516020612fe0833981519152612afa565b9081611e73575b5015611e0d57611d7c90600080516020612f20833981519152612afa565b9081611e1e575b5015611e0d57600080546001600160a01b031916841781556040519390600080516020612f808339815191528180a38015610f6457803b15610f5357600380546001600160a01b031916919091179055611dd957005b600080516020613020833981519152805460ff60401b1916905560018152600080516020612f0083398151915290602090a1005b63cf5a8e3360e01b60005260046000fd5b600080516020612f20833981519152600052600080516020612ee0833981519152602052611e6c907ffff5b43478fa6840382eb63777e0da6941d320b940e3f70f0ea4780feff95827612d3a565b5084611d83565b600080516020612fe0833981519152600052600080516020612ee0833981519152602052611ec1907f32d31393d541a265984ec379cddd8b1fcbe5f2b0eb8ad81dddfead454f135eaa612d3a565b5086611d5e565b60008052600080516020612ee0833981519152602052611f08907f615f0f9e84155bea8cc509fe18befeb1baf65611e38a6ba60964480fb29dfd44612d3a565b5088611d39565b50823b1580611d235750843b15611d23565b50617a694614611d1c565b506105394614611d15565b638135316560e01b60005260046000fd5b600080516020613020833981519152805460ff60401b1916600160401b179055611cad565b90501589611c82565b303b159150611c7a565b889150611c70565b3461029a57604036600319011261029a57610296611fbc611fa7612064565b600435600052600460205260406000206123c8565b611fc581612090565b60405191818392158361209a565b3461029a57600036600319011261029a57602063ffffffff60055416604051908152f35b3461029a57602036600319011261029a576004359063ffffffff60e01b821680920361029a57602091635a05180f60e01b8114908115612039575b5015158152f35b637965db0b60e01b811491508115612053575b5083612032565b6301ffc9a760e01b1490508361204c565b602435906001600160a01b038216820361029a57565b600435906001600160a01b038216820361029a57565b600811156103b857565b9015158152604081019291906020906120b283612090565b0152565b6004359063ffffffff8216820361029a57565b6024359063ffffffff8216820361029a57565b91604091949363ffffffff916060850196151585526120fa81612090565b602085015216910152565b60a081019081106001600160401b0382111761212057604052565b634e487b7160e01b600052604160045260246000fd5b608081019081106001600160401b0382111761212057604052565b601f909101601f19168101906001600160401b0382119082101761212057604052565b6001600160401b03811161212057601f01601f191660200190565b604435906001600160401b038216820361029a57565b60005b8381106121b85750506000910152565b81810151838201526020016121a8565b906020916121e1815180928185528580860191016121a5565b601f01601f1916010190565b604051906121fc604083612151565b60058252640322e312e360dc1b6020830152565b805463ffffffff60c01b191660c09290921b63ffffffff60c01b16919091179055565b63ffffffff91821681529116602082015260400190565b600052600460205261226160406000209182612485565b61226a81612090565b806122b857505460e01c63ffffffff60055416811580156122b0575b6122a35780821061229c57505b60019160009190565b9050612293565b5050600090600390600090565b508015612286565b6000929091508290565b600052600080516020612fc083398151915260205260016040600020015490565b805463ffffffff60a01b191660a09290921b63ffffffff60a01b16919091179055565b919091600052600460205261232060406000209283612511565b9161232a83612090565b826123625763ffffffff9192505460c01c1663ffffffff60055416811580156122b0576122a35780821061229c575060019160009190565b5060009190600090565b90612383916000526004602052604060002061259d565b61238c81612090565b8061239a5750600190600090565b600091565b8054600160a01b600160e01b03191660a09290921b600160a01b600160e01b0316919091179055565b60ff600080516020613000833981519152541661247e57546001600160a01b0381169081156124765760a01c6002161561246f5760005260016020526040600020549060ff8260e01c1660038110156103b857801561246757600214612460574260a083901c6001600160401b03161115612459576001600160a01b0390811691160361245457600090565b600790565b5050600690565b5050600590565b505050600490565b5050600390565b505050600290565b5050600190565b60ff600080516020613000833981519152541661247e57546001600160a01b0381169081156124765760a01c6008161561246f5760005260016020526040600020549060ff8260e01c1660038110156103b857801561246757600214612460574260a083901c6001600160401b03161115612459576001600160a01b0390811691160361245457600090565b60ff600080516020613000833981519152541661247e57546001600160a01b0381169081156124765760a01c6004161561246f5760005260016020526040600020549060ff8260e01c1660038110156103b857801561246757600214612460574260a083901c6001600160401b03161115612459576001600160a01b0390811691160361245457600090565b60ff600080516020613000833981519152541661247e57546001600160a01b0381169081156124765760a01c6001161561246f5760005260016020526040600020549060ff8260e01c1660038110156103b857801561246757600214612460574260a083901c6001600160401b03161115612459576001600160a01b0390811691160361245457600090565b6000908152600460205260409020805490916001600160a01b03821691821590811561268c575b5061128d57336000908152600080516020612f60833981519152602052604090205460ff161580612682575b61127257565b508133141561267c565b6004915060a01c161538612650565b905460a01c63ffffffff16156126ae5750565b600052600460205260006040812055565b6000818152600080516020612fc08339815191526020908152604080832033845290915290205460ff16156126f15750565b63e2517d3f60e01b6000523360045260245260446000fd5b6127138282612afa565b918261271e57505090565b6000918252600080516020612ee0833981519152602052604090912061274d916001600160a01b031690612d3a565b5090565b61275b8282612ba3565b918261276657505090565b6000918252600080516020612ee0833981519152602052604090912061274d916001600160a01b031690612dab565b60ff60008051602061300083398151915254166127ae57565b63d93c066560e01b60005260046000fd5b519064ffffffffff8216820361029a57565b519061ffff8216820361029a57565b8015612abb57600354604051638d34875560e01b8152600481018390529190600090839060249082906001600160a01b03165afa918215612aaf57600092612904575b5060808201516001600160a01b0316913383900361127257610120015161284981612090565b61285281612090565b600381149081156128ef575b81156128db575b506128ca5781600052600160205260ff60406000205460e01c1660038110156103b8576001036128b957600090815260046020526040902080546001600160a01b0319166001600160a01b03831617815591565b632e68d1d360e21b60005260046000fd5b63e9f6db8560e01b60005260046000fd5b600791506128e881612090565b1438612865565b90506128fa81612090565b600581149061285e565b90913d8082843e6129158184612151565b820191602081840312612a8f578051906001600160401b038211612a975701906101a082840312612a8c57604051926101a084016001600160401b03811185821017612a9b5760409081528351855260208085015190860152838101519085015260608301516001600160401b038111612a9757830181601f82011215612a97578051906129a282612174565b926129b06040519485612151565b82845260208383010111612a9357906129cf91602080850191016121a5565b606084015260808201516001600160a01b0381168103612a8f5760808401526129fa60a083016127bf565b60a0840152612a0b60c083016127bf565b60c0840152612a1c60e083016127d1565b60e0840152612a2e61010083016127bf565b610100840152610120820151906008821015612a8c5750612a7f9161018091610120850152612a6061014082016127bf565b610140850152612a7361016082016127d1565b610160850152016127bf565b6101808201529038612823565b80fd5b5080fd5b8380fd5b8280fd5b634e487b7160e01b83526041600452602483fd5b6040513d6000823e3d90fd5b637bb0b62160e01b60005260046000fd5b60ff6000805160206130208339815191525460401c1615612ae957565b631afcd79f60e31b60005260046000fd5b6000818152600080516020612fc0833981519152602090815260408083206001600160a01b038616845290915290205460ff16612b9c576000818152600080516020612fc0833981519152602090815260408083206001600160a01b0395909516808452949091528120805460ff19166001179055339291907f2f8788117e7eff1d82e926ec794901d17c78024a50270940304540a733656f0d9080a4600190565b5050600090565b6000818152600080516020612fc0833981519152602090815260408083206001600160a01b038616845290915290205460ff1615612b9c576000818152600080516020612fc0833981519152602090815260408083206001600160a01b0395909516808452949091528120805460ff19169055339291907ff6391f5c32d9c69d2a47ea670b442974b53935d1edc7fd64eb21e047a839171b9080a4600190565b158015612cfb575b610f64576001600160a01b0316600090815260026020526040902054612cea576001600160401b031642811115612cd95760005460a01c6001600160401b03168015159182612cae575b5050612c9d57565b63e774e7b560e01b60005260046000fd5b909150428103908111612cc357113880612c95565b634e487b7160e01b600052601160045260246000fd5b63efa7aefd60e01b60005260046000fd5b633b53f64f60e21b60005260046000fd5b506001600160a01b03811615612c4b565b8054821015612d245760005260206000200190600090565b634e487b7160e01b600052603260045260246000fd5b6001810190826000528160205260406000205415600014612da3578054600160401b81101561212057612d8e612d77826001879401855584612d0c565b819391549060031b91821b91600019901b19161790565b90555491600052602052604060002055600190565b505050600090565b9060018201918160005282602052604060002054801515600014612e75576000198101818111612cc3578254600019810191908211612cc357818103612e3e575b50505080548015612e28576000190190612e068282612d0c565b8154906000199060031b1b191690555560005260205260006040812055600190565b634e487b7160e01b600052603160045260246000fd5b612e5e612e4e612d779386612d0c565b90549060031b1c92839286612d0c565b905560005283602052604060002055388080612dec565b50505050600090565b90612ea45750805115612e9357602081519101fd5b63d6bda27560e01b60005260046000fd5b81511580612ed6575b612eb5575090565b639996b31560e01b60009081526001600160a01b0391909116600452602490fd5b50803b15612ead56fec1f6fe24621ce81ec5827caf0253cadb74709b061630e6b55e82371705932000c7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d275027301df982f5215b4963a854cf9c73219df80cb6acd8f098189c9480c7bbc360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc07966f2ab141347034724e9b2a472c2f77f5cd745aeba27cacba797b8fcb708617dc5b1290f7495d7f8cc68476e6941aaf510598fa925d0d2720edc8805a846cd37bc77615afefc793aa17d75d4249a9b517ba91b0e54a49c9c4a44b3d7e228302dd7bc7dec4dceedda775e58dd541e08a116c6c53815c0bd028192f7b626800edcc084d3dcd65a1f7f23c65c46722faca6953d28e43150a467cf43e5c309238cd5ed15c6e187e77e9aee88184c21f4f2182ab5827cb3b7e07fbedcd63f03300f0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a00a164736f6c6343000822000a";
17900
18143
  var isSuperArgs2 = (xs) => xs.length > 1;
17901
18144
  var AgentDelegationRegistry__factory = class extends ContractFactory2 {
17902
18145
  constructor(...args) {
@@ -18541,6 +18784,158 @@ var DiamondHandsSDK = class _DiamondHandsSDK {
18541
18784
  return;
18542
18785
  await this.serverSession.adoptLoginPayload(payload);
18543
18786
  }
18787
+ /**
18788
+ * Build the Safe-delegation ceremony for a multi-sig Safe (Phase 1 of
18789
+ * multi-sig support): asks lit-ops-server to mint (or recover) the Safe's
18790
+ * agent PKP and returns the exact call batch the Safe's owners must execute
18791
+ * (AgentModuleFactory.deploy → safe.enableModule → registerAgent), derived
18792
+ * from live chain state so a half-done ceremony resumes.
18793
+ *
18794
+ * Service-mode only — the server holds the Chipotle account that mints the
18795
+ * PKP and it gates the route to current Safe owners. `knownAgentAddress` is
18796
+ * the caller-persisted agent from an earlier prepare whose ceremony has not
18797
+ * landed yet; echoing it back prevents a duplicate mint after a server
18798
+ * restart.
18799
+ */
18800
+ async prepareSafeAgentModule(request) {
18801
+ if (this.config.mode !== "service") {
18802
+ throw new Error(
18803
+ "prepareSafeAgentModule requires service mode \u2014 the agent PKP is minted by lit-ops-server's Chipotle account and cannot be minted standalone"
18804
+ );
18805
+ }
18806
+ const PREPARE_CLIENT_TIMEOUT_MS = 3e4;
18807
+ const abort = new AbortController();
18808
+ const abortTimer = setTimeout(() => abort.abort(), PREPARE_CLIENT_TIMEOUT_MS);
18809
+ let resp;
18810
+ try {
18811
+ resp = await fetch(
18812
+ `${this.config.serviceEndpoint}/api/lit/safe/agent-module/prepare`,
18813
+ {
18814
+ method: "POST",
18815
+ headers: {
18816
+ "Content-Type": "application/json",
18817
+ ...await this.getAuthHeader()
18818
+ },
18819
+ signal: abort.signal,
18820
+ body: JSON.stringify({
18821
+ safeAddress: request.safeAddress,
18822
+ ...request.knownAgentAddress ? { knownAgentAddress: request.knownAgentAddress } : {}
18823
+ })
18824
+ }
18825
+ );
18826
+ } catch (fetchErr) {
18827
+ throw abort.signal.aborted ? new Error(
18828
+ `prepareSafeAgentModule timed out after ${PREPARE_CLIENT_TIMEOUT_MS}ms \u2014 the delegation server did not answer`
18829
+ ) : fetchErr;
18830
+ } finally {
18831
+ clearTimeout(abortTimer);
18832
+ }
18833
+ const data = await resp.json().catch(() => null);
18834
+ if (!resp.ok || !data?.success) {
18835
+ throw new Error(
18836
+ `prepareSafeAgentModule failed (${resp.status}): ${data?.error ?? "no error detail from server"}`
18837
+ );
18838
+ }
18839
+ return {
18840
+ status: data.status,
18841
+ agentAddress: data.agentAddress,
18842
+ moduleAddress: data.moduleAddress,
18843
+ validUntil: data.validUntil,
18844
+ calls: data.calls
18845
+ };
18846
+ }
18847
+ /**
18848
+ * Shared transport for `/agent-module/{authorize,execute}`.
18849
+ *
18850
+ * Both routes run the agent PKP's key inside a Lit Action, gated on the
18851
+ * caller's session being a CURRENT Safe owner (M-8c). Service-mode only, for
18852
+ * the same reason as {@link prepareSafeAgentModule}: the key lives in the
18853
+ * server's Chipotle account and has no standalone equivalent.
18854
+ */
18855
+ async postAgentModuleRoute(mode, body) {
18856
+ if (this.config.mode !== "service") {
18857
+ throw new Error(
18858
+ `agentModule${mode === "authorize" ? "Authorize" : "Execute"} requires service mode \u2014 the agent PKP is held by lit-ops-server's Chipotle account`
18859
+ );
18860
+ }
18861
+ const AGENT_MODULE_CLIENT_TIMEOUT_MS = 45e3;
18862
+ const abort = new AbortController();
18863
+ const abortTimer = setTimeout(
18864
+ () => abort.abort(),
18865
+ AGENT_MODULE_CLIENT_TIMEOUT_MS
18866
+ );
18867
+ let resp;
18868
+ try {
18869
+ resp = await fetch(
18870
+ `${this.config.serviceEndpoint}/api/lit/agent-module/${mode}`,
18871
+ {
18872
+ method: "POST",
18873
+ headers: {
18874
+ "Content-Type": "application/json",
18875
+ ...await this.getAuthHeader()
18876
+ },
18877
+ signal: abort.signal,
18878
+ body: JSON.stringify(body)
18879
+ }
18880
+ );
18881
+ } catch (fetchErr) {
18882
+ throw abort.signal.aborted ? new Error(
18883
+ `agent-module ${mode} timed out after ${AGENT_MODULE_CLIENT_TIMEOUT_MS}ms \u2014 the delegation server did not answer`
18884
+ ) : fetchErr;
18885
+ } finally {
18886
+ clearTimeout(abortTimer);
18887
+ }
18888
+ const data = await resp.json().catch(() => null);
18889
+ if (!resp.ok || !data?.success) {
18890
+ throw new Error(
18891
+ `agent-module ${mode} failed (${resp.status}): ${data?.error ?? "no error detail from server"}`
18892
+ );
18893
+ }
18894
+ return data;
18895
+ }
18896
+ /**
18897
+ * The agent signs the borrower authorization envelope for `op` on `positionId`.
18898
+ *
18899
+ * The returned `{ timestamp, signature }` feeds the SDK's PRECOMPUTED
18900
+ * authorization path (`generateExtendAuthorization` and siblings) — the same
18901
+ * path Safe/EIP-1271 borrowers already use. It is NOT a generic message
18902
+ * signer: the Lit Action REBUILDS the envelope from these fields rather than
18903
+ * signing anything the caller supplies, which is what stops a delegated agent
18904
+ * key being turned into an arbitrary oracle.
18905
+ *
18906
+ * `timestamp` must be quantum-aligned by the caller
18907
+ * (`calculateNextQuantumTimestamp()`) and must be the SAME value later passed
18908
+ * to the protocol call, or the validator recovers a different signer.
18909
+ */
18910
+ async agentModuleAuthorize(request) {
18911
+ return this.postAgentModuleRoute("authorize", {
18912
+ safeAddress: request.safeAddress,
18913
+ positionId: request.positionId,
18914
+ op: request.op,
18915
+ timestamp: request.timestamp,
18916
+ ...request.amount !== void 0 ? { amount: request.amount } : {},
18917
+ ...request.selectedTerm !== void 0 ? { selectedTerm: request.selectedTerm } : {}
18918
+ });
18919
+ }
18920
+ /**
18921
+ * The agent signs `module.execute(target, innerData)` as a raw EIP-1559
18922
+ * transaction. Returns the SIGNED transaction — the caller broadcasts it, so
18923
+ * the tx hash and receipt come from the caller's own provider.
18924
+ *
18925
+ * `tx` fields are caller-supplied because the Lit Action runs on many nodes
18926
+ * and reconciles results: anything fetched in-action (nonce, gas) would
18927
+ * differ per node and fail consensus.
18928
+ */
18929
+ async agentModuleExecute(request) {
18930
+ return this.postAgentModuleRoute("execute", {
18931
+ safeAddress: request.safeAddress,
18932
+ positionId: request.positionId,
18933
+ op: request.op,
18934
+ target: request.target,
18935
+ innerData: request.innerData,
18936
+ tx: request.tx
18937
+ });
18938
+ }
18544
18939
  /**
18545
18940
  * Audit H-9: invalidate the LoanQuery cache so subsequent reads return
18546
18941
  * the post-write state. We clear the entire loan-query cache (not just
@@ -19570,7 +19965,12 @@ var DiamondHandsSDK = class _DiamondHandsSDK {
19570
19965
  ...this.config.contractAddresses?.btcSpendAuthorizer ? { BTCSpendAuthorizer: this.config.contractAddresses.btcSpendAuthorizer } : {},
19571
19966
  ...this.config.contractAddresses?.bitcoinProviderRegistry ? { BitcoinProviderRegistry: this.config.contractAddresses.bitcoinProviderRegistry } : {},
19572
19967
  // Required for Safe-as-borrower auth verification (third arm).
19573
- ...this.config.contractAddresses?.positionDelegateRegistry ? { PositionDelegateRegistry: this.config.contractAddresses.positionDelegateRegistry } : {}
19968
+ ...this.config.contractAddresses?.positionDelegateRegistry ? { PositionDelegateRegistry: this.config.contractAddresses.positionDelegateRegistry } : {},
19969
+ // Required for the Safe-agent scoped mint grant (canMint) — without
19970
+ // this the LIT Action's agent-grant arm is skipped entirely and a
19971
+ // delegated agent signer falls through to the borrower/EIP-1271
19972
+ // arms, which reject it.
19973
+ ...this.config.contractAddresses?.agentDelegationRegistry ? { AgentDelegationRegistry: this.config.contractAddresses.agentDelegationRegistry } : {}
19574
19974
  };
19575
19975
  if (this.config.debug) {
19576
19976
  log.info(" Contract addresses passed to LIT Action:", {});
@@ -21227,7 +21627,12 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
21227
21627
  // Required for the LIT Action verifier's third auth arm in
21228
21628
  // Safe-as-borrower flows. See diamond-hands-sdk.ts:5786 for the same
21229
21629
  // pattern in the withdraw flow.
21230
- PositionDelegateRegistry: this.config.contractAddresses?.positionDelegateRegistry || ""
21630
+ PositionDelegateRegistry: this.config.contractAddresses?.positionDelegateRegistry || "",
21631
+ // Required for the Safe-agent scoped renew grant (canRenew) — without
21632
+ // this the LIT Action's agent-grant arm is skipped entirely and a
21633
+ // delegated agent signer falls through to the borrower/EIP-1271 arms,
21634
+ // which reject it.
21635
+ AgentDelegationRegistry: this.config.contractAddresses?.agentDelegationRegistry || ""
21231
21636
  };
21232
21637
  if (!contracts.PositionManager) {
21233
21638
  throw new Error("PositionManager address is required");
@@ -21897,10 +22302,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
21897
22302
  isRetryablePaymentQuantumFailure(result) {
21898
22303
  if (result.success || !result.error)
21899
22304
  return false;
21900
- const e = result.error;
21901
- if (e.includes("Transaction timeout"))
21902
- return false;
21903
- return e.includes("DeadZoneViolation") || e.includes("QuantumOutsideWindow") || e.includes("QuantumAlreadyUsed") || e.includes("QuantumExpired") || e === "Transaction reverted";
22305
+ return isRetryableQuantumFailure(result.error);
21904
22306
  }
21905
22307
  /**
21906
22308
  * One payment attempt: user auth → Lit Action authorization → dead-zone gate →
@@ -21964,7 +22366,12 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
21964
22366
  BTCSpendAuthorizer: this.config.contractAddresses?.btcSpendAuthorizer || "",
21965
22367
  BitcoinProviderRegistry: this.config.contractAddresses?.bitcoinProviderRegistry || "",
21966
22368
  // Required for Safe-as-borrower auth verification (third arm).
21967
- PositionDelegateRegistry: this.config.contractAddresses?.positionDelegateRegistry || ""
22369
+ PositionDelegateRegistry: this.config.contractAddresses?.positionDelegateRegistry || "",
22370
+ // Required for the Safe-agent scoped repay grant (canRepay) —
22371
+ // without this the LIT Action's agent-grant arm is skipped entirely
22372
+ // and a delegated agent signer falls through to the
22373
+ // borrower/EIP-1271 arms, which reject it.
22374
+ AgentDelegationRegistry: this.config.contractAddresses?.agentDelegationRegistry || ""
21968
22375
  };
21969
22376
  if (this.config.debug) {
21970
22377
  log.info(" Contract addresses:", {});
@@ -22534,6 +22941,37 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
22534
22941
  }
22535
22942
  return this.withdrawalAddressModule.getEntries(target);
22536
22943
  }
22944
+ /**
22945
+ * Recover the revert reason of an already-mined, already-reverted transaction.
22946
+ *
22947
+ * A status-0 receipt carries no revert data, and ethers does not re-fetch it, so
22948
+ * the reason has to be recovered by replaying the exact calldata as an `eth_call`
22949
+ * pinned to the block that mined it — the only block whose `block.timestamp`
22950
+ * reproduces a timing-dependent revert such as `DeadZoneViolation()`.
22951
+ *
22952
+ * Caveat: the replay runs against end-of-block state rather than the state at the
22953
+ * transaction's own index, so for a state-dependent revert the recovered name is
22954
+ * diagnostic, not proof. Timing reverts (the case this exists for) are unaffected —
22955
+ * they depend only on the block timestamp.
22956
+ *
22957
+ * @returns the decoded error name, its raw selector if unknown, or `null` when the
22958
+ * replay yields no revert data at all (never throws — a failed diagnosis
22959
+ * must not mask the underlying failure).
22960
+ */
22961
+ async decodeMinedRevert(params) {
22962
+ try {
22963
+ await this.getProviderOrThrow().call({
22964
+ to: params.to,
22965
+ from: params.from,
22966
+ data: params.data,
22967
+ blockTag: params.blockNumber
22968
+ });
22969
+ return null;
22970
+ } catch (replayError) {
22971
+ const decoded = decodeQuantumRevert(replayError);
22972
+ return decoded.name ?? decoded.selector ?? null;
22973
+ }
22974
+ }
22537
22975
  /**
22538
22976
  * Withdraw Bitcoin from a position
22539
22977
  *
@@ -23107,9 +23545,23 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
23107
23545
  txHash: tx.hash
23108
23546
  });
23109
23547
  }
23548
+ const withdrawProvider = this.getProviderOrThrow();
23549
+ const withdrawTimeoutMs = this.config.transactionTimeoutMs || 3e5;
23110
23550
  let receipt;
23111
23551
  try {
23112
- receipt = await tx.wait();
23552
+ receipt = await Promise.race([
23553
+ withdrawProvider.waitForTransaction(tx.hash, 1),
23554
+ new Promise(
23555
+ (_, reject) => setTimeout(
23556
+ () => reject(
23557
+ new Error(
23558
+ `Transaction timeout after ${withdrawTimeoutMs}ms. Hash: ${tx.hash}`
23559
+ )
23560
+ ),
23561
+ withdrawTimeoutMs
23562
+ )
23563
+ )
23564
+ ]);
23113
23565
  } catch (waitError) {
23114
23566
  const waitErrorMsg = waitError instanceof Error ? waitError.message : String(waitError);
23115
23567
  if (this.config.debug) {
@@ -23126,7 +23578,26 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
23126
23578
  if (!receipt) {
23127
23579
  return {
23128
23580
  success: false,
23129
- error: `Transaction confirmation returned no receipt. Hash: ${tx.hash}`
23581
+ error: `Transaction receipt not found for hash: ${tx.hash}`
23582
+ };
23583
+ }
23584
+ if (receipt.status === 0) {
23585
+ const revertName = await this.decodeMinedRevert({
23586
+ to: positionManagerAddress,
23587
+ from: await withdrawSigner.getAddress(),
23588
+ data: withdrawCalldata,
23589
+ blockNumber: receipt.blockNumber
23590
+ });
23591
+ if (this.config.debug) {
23592
+ log.error("\u274C withdrawBTC reverted on-chain", {
23593
+ txHash: receipt.hash,
23594
+ blockNumber: receipt.blockNumber,
23595
+ revert: revertName ?? "undecodable"
23596
+ });
23597
+ }
23598
+ return {
23599
+ success: false,
23600
+ error: `Transaction reverted: ${revertName ?? "unknown reason"} (tx: ${receipt.hash})`
23130
23601
  };
23131
23602
  }
23132
23603
  if (this.config.debug) {
@@ -23232,6 +23703,11 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
23232
23703
  * The service path's post-broadcast CRIT-2 re-verification exists to catch
23233
23704
  * a LYING SERVER; here the broadcast happens locally from the signatures we
23234
23705
  * just obtained, so the esplora-returned txid is already first-hand.
23706
+ *
23707
+ * Multi-UTXO consolidation in standalone mode requires the caller to supply
23708
+ * `request.utxos` explicitly — `fetchConfirmedVaultUtxos` is service-mode
23709
+ * only, so there is no automatic gather here; without the set, a multi-UTXO
23710
+ * vault fails in the signer with its "Insufficient UTXO value" error.
23235
23711
  */
23236
23712
  async executeBTCWithdrawalStandalonePhase2(request, envelope, txid, vout, chainId) {
23237
23713
  const positionDetails = await this.getPositionDetailsView(
@@ -23438,6 +23914,35 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
23438
23914
  const network = await this.getProviderOrThrow().getNetwork();
23439
23915
  const chainId = Number(network.chainId);
23440
23916
  const signer = this.getSignerOrThrow();
23917
+ let executionUtxos = request.utxos;
23918
+ if (!executionUtxos || executionUtxos.length === 0) {
23919
+ const gathered = await this.fetchConfirmedVaultUtxos(request.positionId);
23920
+ if (gathered.length > 0) {
23921
+ const normTxid = (t) => String(t).toLowerCase().replace(/^0x/, "");
23922
+ const byOutpoint = /* @__PURE__ */ new Map();
23923
+ for (const u of gathered) {
23924
+ byOutpoint.set(`${normTxid(u.txid)}:${u.vout}`, {
23925
+ txid: normTxid(u.txid),
23926
+ vout: Number(u.vout),
23927
+ value: Number(u.value)
23928
+ });
23929
+ }
23930
+ if (!byOutpoint.has(`${normTxid(txid)}:${vout}`)) {
23931
+ return {
23932
+ success: false,
23933
+ error: `Authorized outpoint ${txid}:${vout} is missing from the vault's confirmed UTXO set \u2014 refusing to execute against an incoherent view of the vault`
23934
+ };
23935
+ }
23936
+ if (byOutpoint.size > 1) {
23937
+ executionUtxos = Array.from(byOutpoint.values());
23938
+ if (this.config.debug) {
23939
+ log.info("\u{1F9E9} Multi-UTXO withdrawal: gathered full input set", {
23940
+ inputs: executionUtxos.length
23941
+ });
23942
+ }
23943
+ }
23944
+ }
23945
+ }
23441
23946
  const envelope = await buildBtcExecuteEnvelope({
23442
23947
  positionId: request.positionId,
23443
23948
  txid,
@@ -23448,7 +23953,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
23448
23953
  networkFee: request.networkFee,
23449
23954
  chainId,
23450
23955
  signer,
23451
- ...request.utxos ? { utxos: request.utxos } : {}
23956
+ ...executionUtxos ? { utxos: executionUtxos } : {}
23452
23957
  });
23453
23958
  if (this.config.mode !== "service") {
23454
23959
  return await this.executeBTCWithdrawalStandalonePhase2(
@@ -24220,13 +24725,11 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
24220
24725
  if (authResult.success) {
24221
24726
  break;
24222
24727
  }
24223
- const isQuantumError = authResult.error?.includes(
24224
- "QuantumOutsideWindow"
24225
- );
24728
+ const isQuantumError = isRetryableQuantumFailure(authResult.error);
24226
24729
  if (isQuantumError && wAttempt < MAX_WITHDRAWAL_QUANTUM_RETRIES) {
24227
24730
  if (this.config.debug) {
24228
24731
  log.warn(
24229
- `\u26A0\uFE0F Phase 1 QuantumOutsideWindow (attempt ${wAttempt}/${MAX_WITHDRAWAL_QUANTUM_RETRIES}). LIT action took too long. Retrying with fresh quantum...`
24732
+ `\u26A0\uFE0F Phase 1 quantum-timing failure (attempt ${wAttempt}/${MAX_WITHDRAWAL_QUANTUM_RETRIES}): ${authResult.error}. Retrying with a fresh quantum...`
24230
24733
  );
24231
24734
  }
24232
24735
  await new Promise((resolve) => setTimeout(resolve, 2e3));
@@ -24663,6 +25166,49 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
24663
25166
  }
24664
25167
  return addr;
24665
25168
  }
25169
+ /**
25170
+ * `LoanGrant` fields that EVERY deployed registry version returns.
25171
+ *
25172
+ * Deliberately omits `minWithdrawRatioBps` — see {@link readLoanGrant}.
25173
+ */
25174
+ static LOAN_GRANT_PREFIX_IFACE = new Interface5([
25175
+ "function getLoanGrant(bytes32 positionId) view returns (address borrower, uint32 scopeBits, uint32 minCollateralRatioBps)"
25176
+ ]);
25177
+ /**
25178
+ * Read a position's grant, decoding only the struct prefix that every deployed registry
25179
+ * version shares.
25180
+ *
25181
+ * `LoanGrant` gained a fourth member (`minWithdrawRatioBps`) in registry v2.1.0, and the
25182
+ * typechain bindings are generated from that source — but Sepolia (`0x6AE7fc6b…`) and mainnet
25183
+ * (`0x54853b7E…`) both run v2.0.0, whose `getLoanGrant` returns three words. Decoding that
25184
+ * with the generated four-field decoder fails `BAD_DATA` on every live chain, which is what
25185
+ * took the Agents tab down. The first three members are identical in both layouts, and ethers
25186
+ * ignores the trailing static word on a v2.1.0 registry, so this decoder is correct against
25187
+ * both. Nothing in this SDK reads `minWithdrawRatioBps`: WITHDRAW is not a scope it grants.
25188
+ */
25189
+ async readLoanGrant(registryAddress, pid) {
25190
+ const iface = _DiamondHandsSDK.LOAN_GRANT_PREFIX_IFACE;
25191
+ const data = await this.getProviderOrThrow().call({
25192
+ to: registryAddress,
25193
+ data: iface.encodeFunctionData("getLoanGrant", [pid])
25194
+ });
25195
+ if (getBytes5(data).length < 96) {
25196
+ throw new SDKError({
25197
+ message: `AgentDelegationRegistry at ${registryAddress} returned ${getBytes5(data).length} bytes for getLoanGrant(bytes32) \u2014 expected at least 96. Wrong address, or a registry version this SDK does not support.`,
25198
+ category: "CONTRACT" /* CONTRACT */,
25199
+ severity: "HIGH" /* HIGH */
25200
+ });
25201
+ }
25202
+ const [borrower, scopeBits, minCollateralRatioBps] = iface.decodeFunctionResult(
25203
+ "getLoanGrant",
25204
+ data
25205
+ );
25206
+ return {
25207
+ borrower,
25208
+ scopeBits: Number(scopeBits),
25209
+ minCollateralRatioBps: Number(minCollateralRatioBps)
25210
+ };
25211
+ }
24666
25212
  /**
24667
25213
  * Read a position's auto-renew delegation state (used to drive the Enable/Disable toggle).
24668
25214
  * Pure view — no signer required.
@@ -24671,8 +25217,8 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
24671
25217
  const addr = this.agentDelegationRegistryOrThrow();
24672
25218
  const registry = AgentDelegationRegistry__factory.connect(addr, this.getProviderOrThrow());
24673
25219
  const pid = this.toBytes32(positionId);
24674
- const grant = await registry.getLoanGrant(pid);
24675
- const enabled = grant.borrower !== "0x0000000000000000000000000000000000000000" && (Number(grant.scopeBits) & 1) !== 0;
25220
+ const grant = await this.readLoanGrant(addr, pid);
25221
+ const enabled = grant.borrower !== "0x0000000000000000000000000000000000000000" && (grant.scopeBits & 1) !== 0;
24676
25222
  const agentActive = user ? await registry.isAgentActive(user) : null;
24677
25223
  const canRenew = user ? await registry.canRenew(pid, user).then((r) => ({ ok: r.ok, reason: Number(r.reason) })) : null;
24678
25224
  return {
@@ -24683,16 +25229,58 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
24683
25229
  };
24684
25230
  }
24685
25231
  /**
24686
- * Enable auto-renew delegation for a position. Orchestrates the first-time setup: if the
24687
- * borrower has no active agent, mints a fresh per-user agent PKP (via lit-ops-server) and
24688
- * registers it, then records the position's renew grant. Borrower-signed.
25232
+ * Read every delegation scope a position has granted, plus the mint floor and the protocol
25233
+ * minimum the UI must clamp its editor to. Pure view no signer required.
25234
+ *
25235
+ * Superset of `getAutoRenewStatus`, which predates the multi-scope UI and is kept for
25236
+ * callers that only care about renew.
24689
25237
  */
24690
- async enableAutoRenew(positionId, options) {
25238
+ async getDelegationStatus(positionId, user) {
25239
+ const addr = this.agentDelegationRegistryOrThrow();
25240
+ const registry = AgentDelegationRegistry__factory.connect(addr, this.getProviderOrThrow());
25241
+ const pid = this.toBytes32(positionId);
25242
+ const [grant, protocolMin] = await Promise.all([
25243
+ this.readLoanGrant(addr, pid),
25244
+ registry.agentMintFloorMinBps().then(Number)
25245
+ ]);
25246
+ const hasGrant = grant.borrower !== "0x0000000000000000000000000000000000000000";
25247
+ const scopes = grant.scopeBits;
25248
+ const mintEnabled = hasGrant && (scopes & 4) !== 0;
25249
+ const floor = grant.minCollateralRatioBps;
25250
+ const agent = user ? await registry.agentOf(user) : null;
25251
+ return {
25252
+ renewEnabled: hasGrant && (scopes & 1) !== 0,
25253
+ repayEnabled: hasGrant && (scopes & 2) !== 0,
25254
+ mintEnabled,
25255
+ minCollateralRatioBps: mintEnabled ? floor : 0,
25256
+ effectiveMintFloorBps: mintEnabled ? Math.max(floor, protocolMin) : 0,
25257
+ protocolMinFloorBps: protocolMin,
25258
+ borrower: grant.borrower,
25259
+ agentActive: user ? await registry.isAgentActive(user) : null,
25260
+ agentValidUntil: agent ? Number(agent.validUntil) : null
25261
+ };
25262
+ }
25263
+ /** Governance floor-of-floors for agent mints (bps). The UI's minimum for the floor editor. */
25264
+ async getAgentMintFloorMinBps() {
25265
+ const addr = this.agentDelegationRegistryOrThrow();
25266
+ const registry = AgentDelegationRegistry__factory.connect(addr, this.getProviderOrThrow());
25267
+ return Number(await registry.agentMintFloorMinBps());
25268
+ }
25269
+ /**
25270
+ * Shared enable-path preamble for EVERY scope: resolve the registry, ensure the borrower has
25271
+ * an active agent (minting + registering a per-user PKP on first use), and make that agent the
25272
+ * position's Gate-1 delegate. Returns the signer-connected registry and the agent address.
25273
+ *
25274
+ * Every `enableAuto*` runs this: the registry reverts `NoActiveAgent` without an agent, and
25275
+ * `lit-ops-server.isPositionDelegate` 403s the agent on the execute routes without Gate 1 —
25276
+ * so a scope enabled without this preamble would look on but never execute.
25277
+ */
25278
+ async prepareAgentDelegation(pid, options) {
24691
25279
  const addr = this.agentDelegationRegistryOrThrow();
24692
25280
  const pdrAddr = this.config.contractAddresses?.positionDelegateRegistry;
24693
25281
  if (!pdrAddr) {
24694
25282
  throw new SDKError({
24695
- message: "PositionDelegateRegistry address not configured \u2014 required to authorize the renewal agent (contractAddresses.positionDelegateRegistry)",
25283
+ message: "PositionDelegateRegistry address not configured \u2014 required to authorize the agent (contractAddresses.positionDelegateRegistry)",
24696
25284
  category: "CONFIGURATION" /* CONFIGURATION */,
24697
25285
  severity: "HIGH" /* HIGH */
24698
25286
  });
@@ -24700,7 +25288,6 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
24700
25288
  const signer = this.getSignerOrThrow();
24701
25289
  const user = await signer.getAddress();
24702
25290
  const registry = AgentDelegationRegistry__factory.connect(addr, signer);
24703
- const pid = this.toBytes32(positionId);
24704
25291
  let agentAddress;
24705
25292
  const hasActiveAgent = await registry.isAgentActive(user);
24706
25293
  if (hasActiveAgent) {
@@ -24722,6 +25309,54 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
24722
25309
  const delTx = await setPositionDelegate(pid, agentAddress, signer, pdrAddr);
24723
25310
  await delTx.wait();
24724
25311
  }
25312
+ return { registry, agentAddress };
25313
+ }
25314
+ /**
25315
+ * Shared disable-path epilogue: drop the position's Gate-1 delegate, but ONLY once no scope
25316
+ * remains. Scopes compose, so clearing Gate 1 on every disable would silently break the
25317
+ * surviving ones server-side (the agent would 403 on the execute routes while its remaining
25318
+ * grant still reads as enabled on-chain).
25319
+ *
25320
+ * Also refuses to clobber a delegate registered for anything other than this user's agent.
25321
+ */
25322
+ async clearGate1IfNoScopesRemain(pid) {
25323
+ const addr = this.agentDelegationRegistryOrThrow();
25324
+ const pdrAddr = this.config.contractAddresses?.positionDelegateRegistry;
25325
+ if (!pdrAddr) {
25326
+ throw new SDKError({
25327
+ message: "PositionDelegateRegistry address not configured \u2014 required to clear the agent's Gate-1 authorization (contractAddresses.positionDelegateRegistry)",
25328
+ category: "CONFIGURATION" /* CONFIGURATION */,
25329
+ severity: "HIGH" /* HIGH */
25330
+ });
25331
+ }
25332
+ const signer = this.getSignerOrThrow();
25333
+ const user = await signer.getAddress();
25334
+ const registry = AgentDelegationRegistry__factory.connect(addr, signer);
25335
+ const grant = await this.readLoanGrant(addr, pid);
25336
+ if (grant.scopeBits !== 0)
25337
+ return;
25338
+ const [currentDelegate, userAgent] = await Promise.all([
25339
+ getPositionDelegate(pid, signer, pdrAddr),
25340
+ registry.agentOf(user).then((a) => a.agent)
25341
+ ]);
25342
+ if (currentDelegate !== ZeroAddress3 && currentDelegate.toLowerCase() === userAgent.toLowerCase()) {
25343
+ const clearTx = await setPositionDelegate(
25344
+ pid,
25345
+ "0x0000000000000000000000000000000000000000",
25346
+ signer,
25347
+ pdrAddr
25348
+ );
25349
+ await clearTx.wait();
25350
+ }
25351
+ }
25352
+ /**
25353
+ * Enable auto-renew delegation for a position. Orchestrates the first-time setup: if the
25354
+ * borrower has no active agent, mints a fresh per-user agent PKP (via lit-ops-server) and
25355
+ * registers it, then records the position's renew grant. Borrower-signed.
25356
+ */
25357
+ async enableAutoRenew(positionId, options) {
25358
+ const pid = this.toBytes32(positionId);
25359
+ const { registry, agentAddress } = await this.prepareAgentDelegation(pid, options);
24725
25360
  const tx = await registry.enableAutoRenew(pid);
24726
25361
  const receipt = await tx.wait();
24727
25362
  return { hash: tx.hash, blockNumber: receipt?.blockNumber ?? 0, agentAddress };
@@ -24730,35 +25365,102 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
24730
25365
  async disableAutoRenew(positionId) {
24731
25366
  const addr = this.agentDelegationRegistryOrThrow();
24732
25367
  const signer = this.getSignerOrThrow();
24733
- const user = await signer.getAddress();
24734
25368
  const registry = AgentDelegationRegistry__factory.connect(addr, signer);
24735
25369
  const pid = this.toBytes32(positionId);
24736
- const pdrAddr = this.config.contractAddresses?.positionDelegateRegistry;
24737
- if (!pdrAddr) {
25370
+ const tx = await registry.disableAutoRenew(pid);
25371
+ const receipt = await tx.wait();
25372
+ await this.clearGate1IfNoScopesRemain(pid);
25373
+ return { hash: tx.hash, blockNumber: receipt?.blockNumber ?? 0 };
25374
+ }
25375
+ /**
25376
+ * Enable auto-repay delegation: the agent may repay this position's debt on the borrower's
25377
+ * behalf. Deliberately uncapped in the registry — repay only ever burns the borrower's own
25378
+ * debt from the borrower's own funds, and the ERC-20 allowance to PositionManager (never
25379
+ * granted to the agent) is the natural bound. Borrower-signed.
25380
+ */
25381
+ async enableAutoRepay(positionId, options) {
25382
+ const pid = this.toBytes32(positionId);
25383
+ const { registry, agentAddress } = await this.prepareAgentDelegation(pid, options);
25384
+ const tx = await registry.enableAutoRepay(pid);
25385
+ const receipt = await tx.wait();
25386
+ return { hash: tx.hash, blockNumber: receipt?.blockNumber ?? 0, agentAddress };
25387
+ }
25388
+ /** Disable auto-repay delegation for a position. Borrower-signed. */
25389
+ async disableAutoRepay(positionId) {
25390
+ const addr = this.agentDelegationRegistryOrThrow();
25391
+ const signer = this.getSignerOrThrow();
25392
+ const registry = AgentDelegationRegistry__factory.connect(addr, signer);
25393
+ const pid = this.toBytes32(positionId);
25394
+ const tx = await registry.disableAutoRepay(pid);
25395
+ const receipt = await tx.wait();
25396
+ await this.clearGate1IfNoScopesRemain(pid);
25397
+ return { hash: tx.hash, blockNumber: receipt?.blockNumber ?? 0 };
25398
+ }
25399
+ /**
25400
+ * Enable auto-mint delegation with a post-mint collateral-ratio floor (bps) the mint validator
25401
+ * enforces. `minCollateralRatioBps` must be at or above the governance minimum
25402
+ * (`getAgentMintFloorMinBps()`, 15000 = 150%) — checked here so the caller gets a readable
25403
+ * error instead of a `FloorBelowProtocolMin` revert after signing. Borrower-signed.
25404
+ */
25405
+ async enableAutoMint(positionId, minCollateralRatioBps, options) {
25406
+ const pid = this.toBytes32(positionId);
25407
+ await this.assertMintFloorAtOrAboveProtocolMin(minCollateralRatioBps);
25408
+ const { registry, agentAddress } = await this.prepareAgentDelegation(pid, options);
25409
+ const tx = await registry.enableAutoMint(pid, minCollateralRatioBps);
25410
+ const receipt = await tx.wait();
25411
+ return { hash: tx.hash, blockNumber: receipt?.blockNumber ?? 0, agentAddress };
25412
+ }
25413
+ /** Disable auto-mint delegation for a position (also zeroes its mint floor). Borrower-signed. */
25414
+ async disableAutoMint(positionId) {
25415
+ const addr = this.agentDelegationRegistryOrThrow();
25416
+ const signer = this.getSignerOrThrow();
25417
+ const registry = AgentDelegationRegistry__factory.connect(addr, signer);
25418
+ const pid = this.toBytes32(positionId);
25419
+ const tx = await registry.disableAutoMint(pid);
25420
+ const receipt = await tx.wait();
25421
+ await this.clearGate1IfNoScopesRemain(pid);
25422
+ return { hash: tx.hash, blockNumber: receipt?.blockNumber ?? 0 };
25423
+ }
25424
+ /**
25425
+ * Change an existing mint grant's collateral floor in place — no disable/re-enable dance.
25426
+ * MINT must already be granted; the new floor must clear the governance minimum. Note the
25427
+ * borrower may move it in either direction (above the minimum); only `REGISTRAR_ROLE` is
25428
+ * restricted to raising. Borrower-signed.
25429
+ */
25430
+ async setMintCollateralFloor(positionId, newFloorBps) {
25431
+ const addr = this.agentDelegationRegistryOrThrow();
25432
+ await this.assertMintFloorAtOrAboveProtocolMin(newFloorBps);
25433
+ const signer = this.getSignerOrThrow();
25434
+ const registry = AgentDelegationRegistry__factory.connect(addr, signer);
25435
+ const pid = this.toBytes32(positionId);
25436
+ const tx = await registry.setMintCollateralFloor(pid, newFloorBps);
25437
+ const receipt = await tx.wait();
25438
+ return { hash: tx.hash, blockNumber: receipt?.blockNumber ?? 0 };
25439
+ }
25440
+ /** Pre-flight the registry's `FloorBelowProtocolMin` guard with a message a user can act on. */
25441
+ async assertMintFloorAtOrAboveProtocolMin(floorBps) {
25442
+ if (!Number.isInteger(floorBps) || floorBps <= 0) {
24738
25443
  throw new SDKError({
24739
- message: "PositionDelegateRegistry address not configured \u2014 required to clear the renewal agent's Gate-1 authorization (contractAddresses.positionDelegateRegistry)",
25444
+ message: `Invalid collateral floor: ${floorBps} bps \u2014 must be a positive integer`,
25445
+ category: "VALIDATION" /* VALIDATION */,
25446
+ severity: "MEDIUM" /* MEDIUM */
25447
+ });
25448
+ }
25449
+ const protocolMin = await this.getAgentMintFloorMinBps();
25450
+ if (protocolMin === 0) {
25451
+ throw new SDKError({
25452
+ message: "Agent minting is not configured on this network (agentMintFloorMinBps is 0) \u2014 the registry would reject every mint grant",
24740
25453
  category: "CONFIGURATION" /* CONFIGURATION */,
24741
25454
  severity: "HIGH" /* HIGH */
24742
25455
  });
24743
25456
  }
24744
- {
24745
- const [currentDelegate, userAgent] = await Promise.all([
24746
- getPositionDelegate(pid, signer, pdrAddr).catch(() => null),
24747
- registry.agentOf(user).then((a) => a.agent).catch(() => null)
24748
- ]);
24749
- if (currentDelegate && userAgent && currentDelegate.toLowerCase() === userAgent.toLowerCase()) {
24750
- const clearTx = await setPositionDelegate(
24751
- pid,
24752
- "0x0000000000000000000000000000000000000000",
24753
- signer,
24754
- pdrAddr
24755
- );
24756
- await clearTx.wait();
24757
- }
25457
+ if (floorBps < protocolMin) {
25458
+ throw new SDKError({
25459
+ message: `Collateral floor ${floorBps / 100}% is below the protocol minimum of ${protocolMin / 100}%`,
25460
+ category: "VALIDATION" /* VALIDATION */,
25461
+ severity: "MEDIUM" /* MEDIUM */
25462
+ });
24758
25463
  }
24759
- const tx = await registry.disableAutoRenew(pid);
24760
- const receipt = await tx.wait();
24761
- return { hash: tx.hash, blockNumber: receipt?.blockNumber ?? 0 };
24762
25464
  }
24763
25465
  // ============================================================================
24764
25466
  // HIGH-LEVEL API - Bitcoin Operations
@@ -25958,6 +26660,146 @@ var EventHelpers = {
25958
26660
  });
25959
26661
  }
25960
26662
  };
26663
+
26664
+ // src/utils/safe-agent-delegation.utils.ts
26665
+ init_deployment_addresses();
26666
+ import { Contract as Contract10, Interface as Interface6 } from "ethers";
26667
+ var SAFE_ABI = [
26668
+ "function getModulesPaginated(address start, uint256 pageSize) view returns (address[] modules, address next)"
26669
+ ];
26670
+ var MODULE_ABI = [
26671
+ "function safe() view returns (address)",
26672
+ "function agent() view returns (address)",
26673
+ "function positionManager() view returns (address)"
26674
+ ];
26675
+ var FACTORY_ABI = ["function isFromFactory(address) view returns (bool)"];
26676
+ var REGISTRY_ABI = [
26677
+ "function agentOf(address user) view returns (address agent, uint64 validUntil, uint8 status, uint64 registeredAt, uint64 revokedAt)"
26678
+ ];
26679
+ var MODULE_LIST_SENTINEL = "0x0000000000000000000000000000000000000001";
26680
+ var MODULE_PAGE_SIZE = 50;
26681
+ var lower = (v) => v.trim().toLowerCase();
26682
+ async function classifyModule(provider, moduleAddress, safeAddress, positionManager, factoryAddress) {
26683
+ const module = new Contract10(moduleAddress, MODULE_ABI, provider);
26684
+ let boundSafe;
26685
+ try {
26686
+ boundSafe = await module.safe();
26687
+ } catch {
26688
+ return null;
26689
+ }
26690
+ if (lower(boundSafe) !== lower(safeAddress))
26691
+ return false;
26692
+ let modulePm;
26693
+ try {
26694
+ modulePm = await module.positionManager();
26695
+ } catch {
26696
+ return false;
26697
+ }
26698
+ if (lower(modulePm) !== lower(positionManager))
26699
+ return false;
26700
+ if (!factoryAddress)
26701
+ return null;
26702
+ try {
26703
+ const factory = new Contract10(factoryAddress, FACTORY_ABI, provider);
26704
+ return await factory.isFromFactory(moduleAddress) === true;
26705
+ } catch {
26706
+ return null;
26707
+ }
26708
+ }
26709
+ async function getSafeAgentDelegation(params) {
26710
+ const { safeAddress, chainId, provider } = params;
26711
+ const normalized = lower(safeAddress);
26712
+ const unknown = {
26713
+ safeAddress: normalized,
26714
+ chainId,
26715
+ status: "unknown",
26716
+ moduleAddress: null,
26717
+ agentAddress: null,
26718
+ validUntil: null,
26719
+ truncated: false
26720
+ };
26721
+ const contracts = Object.values(ALL_DEPLOYMENTS).find(
26722
+ (deployment) => deployment.chainId === chainId
26723
+ )?.contracts;
26724
+ const positionManager = contracts?.PositionManager;
26725
+ if (!positionManager)
26726
+ return unknown;
26727
+ const factoryAddress = contracts?.AgentModuleFactory;
26728
+ const registryAddress = contracts?.AgentDelegationRegistry;
26729
+ let modules;
26730
+ let next;
26731
+ try {
26732
+ const safe = new Contract10(safeAddress, SAFE_ABI, provider);
26733
+ const page = await safe.getModulesPaginated(
26734
+ MODULE_LIST_SENTINEL,
26735
+ MODULE_PAGE_SIZE
26736
+ );
26737
+ modules = [...page[0]];
26738
+ next = page[1];
26739
+ } catch {
26740
+ return unknown;
26741
+ }
26742
+ const truncated = BigInt(next) > BigInt(MODULE_LIST_SENTINEL);
26743
+ let indeterminate = false;
26744
+ for (const moduleAddress of modules) {
26745
+ const verdict = await classifyModule(
26746
+ provider,
26747
+ moduleAddress,
26748
+ normalized,
26749
+ positionManager,
26750
+ factoryAddress
26751
+ );
26752
+ if (verdict === null) {
26753
+ indeterminate = true;
26754
+ continue;
26755
+ }
26756
+ if (!verdict)
26757
+ continue;
26758
+ let agentAddress = null;
26759
+ try {
26760
+ agentAddress = await new Contract10(
26761
+ moduleAddress,
26762
+ MODULE_ABI,
26763
+ provider
26764
+ ).agent();
26765
+ } catch {
26766
+ }
26767
+ if (!registryAddress) {
26768
+ indeterminate = true;
26769
+ continue;
26770
+ }
26771
+ let validUntil;
26772
+ let status;
26773
+ try {
26774
+ const record = await new Contract10(
26775
+ registryAddress,
26776
+ REGISTRY_ABI,
26777
+ provider
26778
+ ).agentOf(safeAddress);
26779
+ status = Number(record[2]);
26780
+ validUntil = Number(record[1]);
26781
+ } catch {
26782
+ indeterminate = true;
26783
+ continue;
26784
+ }
26785
+ const now = Math.floor(Date.now() / 1e3);
26786
+ if (status !== 1 || validUntil <= now) {
26787
+ return { ...unknown, status: "not-delegated", truncated };
26788
+ }
26789
+ return {
26790
+ safeAddress: normalized,
26791
+ chainId,
26792
+ status: "delegated",
26793
+ moduleAddress,
26794
+ agentAddress: agentAddress ? lower(agentAddress) : null,
26795
+ validUntil,
26796
+ truncated
26797
+ };
26798
+ }
26799
+ if (truncated || indeterminate)
26800
+ return { ...unknown, truncated };
26801
+ return { ...unknown, status: "not-delegated" };
26802
+ }
25961
26803
  export {
25962
26804
  ALL_CONTRACTS,
25963
26805
  ALL_DEPLOYMENTS,
@@ -26019,6 +26861,7 @@ export {
26019
26861
  getDelegateStatus,
26020
26862
  getDeploymentByNetwork,
26021
26863
  getPositionDelegate,
26864
+ getSafeAgentDelegation,
26022
26865
  isClosePositionAuth,
26023
26866
  isCreateLoanAuth,
26024
26867
  isExtendTermAuth,