@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.js CHANGED
@@ -90,7 +90,8 @@ var init_deployment_addresses = __esm({
90
90
  BitcoinWithdrawalAddressRegistry: "0x6376D392097F0A81e51BD83960A97EA20eaf1dbe",
91
91
  CarryAgentRegistry: "0x5595CB538D44E6Cf179D20364fD631D0477e69f7",
92
92
  AgentDelegationRegistry: "0x6AE7fc6bE0925126b9D25b1B7Ce5AD5d127ec025",
93
- DHAgentDelegate: "0xa5A14489ae883960df6E1463A41a47aBE9Ecc5AF"
93
+ DHAgentDelegate: "0xa5A14489ae883960df6E1463A41a47aBE9Ecc5AF",
94
+ AgentModuleFactory: "0x1Fd3aCa2C030Cf77ce28Af9694D3Bb3034ACa2E5"
94
95
  },
95
96
  upgraded: {
96
97
  PositionManager: {
@@ -385,7 +386,8 @@ var init_deployment_addresses = __esm({
385
386
  BitcoinWithdrawalAddressRegistry: "0x6376D392097F0A81e51BD83960A97EA20eaf1dbe",
386
387
  CarryAgentRegistry: "0x5595CB538D44E6Cf179D20364fD631D0477e69f7",
387
388
  AgentDelegationRegistry: "0x6AE7fc6bE0925126b9D25b1B7Ce5AD5d127ec025",
388
- DHAgentDelegate: "0xa5A14489ae883960df6E1463A41a47aBE9Ecc5AF"
389
+ DHAgentDelegate: "0xa5A14489ae883960df6E1463A41a47aBE9Ecc5AF",
390
+ AgentModuleFactory: "0x1Fd3aCa2C030Cf77ce28Af9694D3Bb3034ACa2E5"
389
391
  };
390
392
  MAINNET_DEPLOYMENT = {
391
393
  network: "mainnet",
@@ -622,7 +624,7 @@ var init_deployment_addresses = __esm({
622
624
  LOCALHOST_DEPLOYMENT = {
623
625
  network: "localhost",
624
626
  chainId: 1337,
625
- timestamp: "2026-07-20T19:45:58.514Z",
627
+ timestamp: "2026-07-23T17:13:42.777Z",
626
628
  deployer: "",
627
629
  contracts: {
628
630
  MessageHashBuilder: "0xa82fF9aFd8f496c3d6ac40E2a0F282E47488CFc9",
@@ -642,6 +644,30 @@ var init_deployment_addresses = __esm({
642
644
  PositionManager: "0x851356ae760d987E095750cCeb3bC6014560891C",
643
645
  OperationAuthorizationRegistry: "0xc96304e3c037f81dA488ed9dEa1D8F2a48278a75",
644
646
  PKPValidation: "0xD0141E899a65C95a556fE2B27e5982A6DE7fDD7A"
647
+ },
648
+ latestEnv: {
649
+ UCD_TOKEN: "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512",
650
+ UCD_CONTROLLER: "0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9",
651
+ POSITION_MANAGER: "0x851356ae760d987E095750cCeb3bC6014560891C",
652
+ PKP_VALIDATION_REGISTRY: "0xD0141E899a65C95a556fE2B27e5982A6DE7fDD7A",
653
+ PKP_VALIDATION_CID_V1: "0x12203af2054b0ab6e18d898ddfc7985593439fb82d22336133387c1169f98d3702ab",
654
+ PKP_VALIDATION_CID_V3: "0x12203af2054b0ab6e18d898ddfc7985593439fb82d22336133387c1169f98d3702ab",
655
+ PKP_ETH_ADDRESS: "0x406f6a9855de2f5577b1525f0600461f02859bd0",
656
+ OPERATION_AUTHORIZATION_REGISTRY: "0xc96304e3c037f81dA488ed9dEa1D8F2a48278a75",
657
+ UCD_MINT_VALIDATOR_ADDRESS: "0x406f6a9855de2f5577b1525f0600461f02859bd0",
658
+ UCD_MINT_VALIDATOR_VERSION: 1,
659
+ BTC_WITHDRAWAL_VALIDATOR_ADDRESS: "0x406f6a9855de2f5577b1525f0600461f02859bd0",
660
+ BTC_WITHDRAWAL_VALIDATOR_VERSION: 3,
661
+ UPDATE_BALANCE_VALIDATOR_ADDRESS: "0x406f6a9855de2f5577b1525f0600461f02859bd0",
662
+ UPDATE_BALANCE_VALIDATOR_VERSION: 1,
663
+ PROCESS_PAYMENT_VALIDATOR_ADDRESS: "0x406f6a9855de2f5577b1525f0600461f02859bd0",
664
+ PROCESS_PAYMENT_VALIDATOR_VERSION: 1,
665
+ EXTEND_POSITION_VALIDATOR_ADDRESS: "0x406f6a9855de2f5577b1525f0600461f02859bd0",
666
+ EXTEND_POSITION_VALIDATOR_VERSION: 1,
667
+ POSITION_MANAGER_CORE_MODULE: "0xB7f8BC63BbcaD18155201308C8f3540b07f84F5e",
668
+ LOAN_OPERATIONS_MANAGER_MODULE: "0x68B1D87F95878fE05B998F19b66F4baba5De1aed",
669
+ TERM_MANAGER_MODULE: "0x0DCd1Bf9A1b36cE34237eEaFef220932846BCD82",
670
+ COMMUNITY_MANAGER_MODULE: ""
645
671
  }
646
672
  };
647
673
  LOCALHOST_CONTRACTS = {
@@ -1353,30 +1379,30 @@ var require_pkg_src = __commonJS({
1353
1379
  }
1354
1380
  },
1355
1381
  btcTransactionSigner: {
1356
- cid: "QmPncBUbxN9giWntJcvBMFW9GDAgA3DwgAkUAHxSsY67ic",
1382
+ cid: "QmakWzTce8NH1Jkgv1xwg83Bg9WmF2UG1Mq76pDYDSwwUp",
1357
1383
  authorizedCidHex: cidToHex(
1358
- "QmPncBUbxN9giWntJcvBMFW9GDAgA3DwgAkUAHxSsY67ic"
1384
+ "QmakWzTce8NH1Jkgv1xwg83Bg9WmF2UG1Mq76pDYDSwwUp"
1359
1385
  ),
1360
1386
  name: "Btc Transaction Signer",
1361
1387
  description: "Production Btc Transaction Signer",
1362
1388
  version: "1.1.1",
1363
1389
  deployed: true,
1364
- deployedAt: 1785226367707,
1365
- size: 66624,
1366
- hash: "9e0d9909519d5133bab03a9491566870b08d8b42c80e2d768bd24f02946cc781"
1390
+ deployedAt: 1786113907707,
1391
+ size: 67999,
1392
+ hash: "83f3644c68819215e05ba8db3225571354a009458fc1437f606df6cb9142d3f9"
1367
1393
  },
1368
1394
  ucdMintValidator: {
1369
- cid: "QmPRLCfseSWCAS4PpvQWt9aEd6rXqWYG4JyGTn26KYk6xc",
1395
+ cid: "Qmd8jgKRceLvf5aoTtwdXG2JqMoBK5cRJwrvEtQkgvbFbx",
1370
1396
  authorizedCidHex: cidToHex(
1371
- "QmPRLCfseSWCAS4PpvQWt9aEd6rXqWYG4JyGTn26KYk6xc"
1397
+ "Qmd8jgKRceLvf5aoTtwdXG2JqMoBK5cRJwrvEtQkgvbFbx"
1372
1398
  ),
1373
1399
  name: "Ucd Mint Validator",
1374
1400
  description: "Production Ucd Mint Validator",
1375
1401
  version: "1.2.1",
1376
1402
  deployed: true,
1377
- deployedAt: 1785226381631,
1378
- size: 81658,
1379
- hash: "d8ea21622885c3d26fdb64e2ba9d2f9460753a5cd7934b2928858899537443eb",
1403
+ deployedAt: 1786113908489,
1404
+ size: 82739,
1405
+ hash: "2bdd9aebdaad58a43a4731d5b7284f2531b7a9caaf8caf5719d891c4ed33cb2e",
1380
1406
  validatorWalletAddress: "0x82DB9F515957aA380B596C6eA2Bd30CEF181f236",
1381
1407
  pkp: {
1382
1408
  publicKey: "0x041ab17cd91fc5c0b761eea6092d032807561b621b82c488826776e04a9158d61ba64d809a4729f0501289d980732cb06d8dfd06999dd9c8efd2f495dad78b31bb",
@@ -1384,17 +1410,17 @@ var require_pkg_src = __commonJS({
1384
1410
  }
1385
1411
  },
1386
1412
  processPaymentValidator: {
1387
- cid: "QmNUzmHQHsaKThGkfZiD3xZrN64qZfn4P6J4MqWgvkeEsX",
1413
+ cid: "QmNXVdysXDhCaQ1uNE2Vgjbuzy6yUEnnVgHzmDfNKXMU6z",
1388
1414
  authorizedCidHex: cidToHex(
1389
- "QmNUzmHQHsaKThGkfZiD3xZrN64qZfn4P6J4MqWgvkeEsX"
1415
+ "QmNXVdysXDhCaQ1uNE2Vgjbuzy6yUEnnVgHzmDfNKXMU6z"
1390
1416
  ),
1391
1417
  name: "Process Payment Validator",
1392
1418
  description: "Production Process Payment Validator",
1393
1419
  version: "1.2.1",
1394
1420
  deployed: true,
1395
- deployedAt: 1785226385509,
1396
- size: 77655,
1397
- hash: "3c2af3c3e62e8e606d35f81fd7e6f4943f8516a9bef74f4150c8c1767cbd316f",
1421
+ deployedAt: 1786113909138,
1422
+ size: 78736,
1423
+ hash: "17191ecc9c009e0fa5ab52c876330beb2a9a75d30cccb92e7c0c04f169df088b",
1398
1424
  validatorWalletAddress: "0x82DB9F515957aA380B596C6eA2Bd30CEF181f236",
1399
1425
  pkp: {
1400
1426
  publicKey: "0x04cfccb9b6c7addc79469c0e9fafebef4c70801302c62cdb26b610723a9b8527d23f98a40158c7340d83fd0e62d02b5ec74c12f7a86d98e8c6973b4811110f19ad",
@@ -1402,17 +1428,17 @@ var require_pkg_src = __commonJS({
1402
1428
  }
1403
1429
  },
1404
1430
  extendPositionValidator: {
1405
- cid: "QmY2t41w1maaoDmdRPx6wTfciHPQAydSpLSKwKVFSS2DPM",
1431
+ cid: "QmYwBsyrhGfHWyRKx549G4tKHxsBLAuRUHcecigeUCkFav",
1406
1432
  authorizedCidHex: cidToHex(
1407
- "QmY2t41w1maaoDmdRPx6wTfciHPQAydSpLSKwKVFSS2DPM"
1433
+ "QmYwBsyrhGfHWyRKx549G4tKHxsBLAuRUHcecigeUCkFav"
1408
1434
  ),
1409
1435
  name: "Extend Position Validator",
1410
1436
  description: "Production Extend Position Validator",
1411
1437
  version: "1.2.1",
1412
1438
  deployed: true,
1413
- deployedAt: 1785226398291,
1414
- size: 75434,
1415
- hash: "ba55532fbeaf6e65d180b869fd69ee06393d574daa0ace524a9e831d846539a5",
1439
+ deployedAt: 1786113909872,
1440
+ size: 76515,
1441
+ hash: "1d4e085335af80f194a663a577876902c83015177e95906a0b461fa93bd5da08",
1416
1442
  validatorWalletAddress: "0x82DB9F515957aA380B596C6eA2Bd30CEF181f236",
1417
1443
  pkp: {
1418
1444
  publicKey: "0x04e52338f6a0c3362800f1a94c4c8f08be5030dde0fa217388c53d1c1a4790f4443c9f2886b2d2d8f4b75a9a156e9b028011b1a1cf7648da0e556aa980ec692836",
@@ -1420,9 +1446,9 @@ var require_pkg_src = __commonJS({
1420
1446
  }
1421
1447
  },
1422
1448
  btcWithdrawal: {
1423
- cid: "QmcBg8vPXsCmiKDk28bJVTqDpTrwCzSYAGu5V6ABVVPuK5",
1449
+ cid: "QmZuR3qSBajutXfRd91ffpyFrRMgmaMh7fDEHRiQQF8Ljp",
1424
1450
  authorizedCidHex: cidToHex(
1425
- "QmcBg8vPXsCmiKDk28bJVTqDpTrwCzSYAGu5V6ABVVPuK5"
1451
+ "QmZuR3qSBajutXfRd91ffpyFrRMgmaMh7fDEHRiQQF8Ljp"
1426
1452
  ),
1427
1453
  name: "Btc Withdrawal",
1428
1454
  description: "Production Btc Withdrawal",
@@ -1432,9 +1458,9 @@ var require_pkg_src = __commonJS({
1432
1458
  // than the one it replaced, which a rotation ceremony would treat as a rollback.
1433
1459
  version: "1.2.4",
1434
1460
  deployed: true,
1435
- deployedAt: 1785226412397,
1436
- size: 92754,
1437
- hash: "d3a8ec62e0b1e6d0566a154a884bf7ee4e25ef6dc2dd84fde98211d25c1dede5",
1461
+ deployedAt: 1786113910531,
1462
+ size: 95313,
1463
+ hash: "ce12f11e3b5469953bc919586e4f3b8b5d594224b58b799dd9a3b60e3749559c",
1438
1464
  validatorWalletAddress: "0x82DB9F515957aA380B596C6eA2Bd30CEF181f236",
1439
1465
  pkp: {
1440
1466
  publicKey: "0x043616787c5432415c24378c4ef48de2bcd6bb7b575b837e3cff09171802662da7105d79586c7659677a0ecbaddac4cce06cb2a11f69a16fa0c4d7002ac7d51a4d",
@@ -1545,6 +1571,21 @@ var require_pkg_src = __commonJS({
1545
1571
  size: 1046,
1546
1572
  hash: "03ee5be239c91c735d2c95db89a0d1b77e602cfc18af8eb64b3281fddcd90a72"
1547
1573
  },
1574
+ agentModuleExecutor: {
1575
+ // Bound to the isolated Sepolia agent group 7 ("DH-Agent-Sepolia"), which
1576
+ // holds this action and no PKPs until an agent is minted into it. NOT yet
1577
+ // bound on mainnet — the Base `DH-Agent-Mainnet` group is created at rollout.
1578
+ cid: "QmVgjGXUuxoGf8XyddW8a7btaazT9yEotpKoBgb65oPvcF",
1579
+ authorizedCidHex: cidToHex(
1580
+ "QmVgjGXUuxoGf8XyddW8a7btaazT9yEotpKoBgb65oPvcF"
1581
+ ),
1582
+ name: "Agent Module Executor",
1583
+ description: "Capability gate for multi-sig Safe agent delegation: binds the agent PKP to the AgentDelegationRegistry record before signing (authorize | execute modes)",
1584
+ version: "1.0.0",
1585
+ deployed: true,
1586
+ size: 11785,
1587
+ hash: "fa294e164273c2158f9ef11a0ccfd735c343c0e3759fcb4b37e0f8a5d1ab0d29"
1588
+ },
1548
1589
  staleSpendRecoveryValidator: {
1549
1590
  cid: "QmXQWT8V4wjuvH8BbC7xrkYxsVu5AKfvfWcS7jnYfrexYm",
1550
1591
  authorizedCidHex: cidToHex(
@@ -1564,17 +1605,17 @@ var require_pkg_src = __commonJS({
1564
1605
  }
1565
1606
  },
1566
1607
  btcUtxoInvalidator: {
1567
- cid: "QmZE422cZsEjYN8pE6AmpkgFoZCK4UAfdDuzoVRRsMJbwm",
1608
+ cid: "Qmf4Ze8yhnQhHbnX5RPgJrC7Jw1aA3gbpfpd52kFg7fd1m",
1568
1609
  authorizedCidHex: cidToHex(
1569
- "QmZE422cZsEjYN8pE6AmpkgFoZCK4UAfdDuzoVRRsMJbwm"
1610
+ "Qmf4Ze8yhnQhHbnX5RPgJrC7Jw1aA3gbpfpd52kFg7fd1m"
1570
1611
  ),
1571
1612
  name: "Btc Utxo Invalidator",
1572
1613
  description: "Production Btc Utxo Invalidator",
1573
1614
  version: "1.0.0",
1574
1615
  deployed: true,
1575
- deployedAt: 1785226423791,
1576
- size: 61472,
1577
- hash: "75eca528d8ac30ae8f21caaae206ccab162a83a048d622868e60f645f0c475e2"
1616
+ deployedAt: 1786113911948,
1617
+ size: 62553,
1618
+ hash: "1dcea2b49e545c423195be22e072dc5e3039ed1c95a8490b23505956029f5b33"
1578
1619
  }
1579
1620
  };
1580
1621
  function getDeploymentsForNetwork(network) {
@@ -2320,7 +2361,7 @@ ${errorReport}`);
2320
2361
  }
2321
2362
  init_debug_logger();
2322
2363
  init_session_signature_cache();
2323
- var import_ethers18 = require("ethers");
2364
+ var import_ethers19 = require("ethers");
2324
2365
  var EXPIRED_LOAN_MIN_LIQUIDATION_THRESHOLD_BPS = 11e3;
2325
2366
  var GRACE_PERIOD_DAYS = 30;
2326
2367
  var SATOSHIS_PER_BITCOIN = 100000000n;
@@ -5153,12 +5194,12 @@ ${auth.clientId}`;
5153
5194
  }
5154
5195
  async function executeVaultSnapshot(params) {
5155
5196
  global.ethers = {
5156
- ...import_ethers18.ethers,
5197
+ ...import_ethers19.ethers,
5157
5198
  providers: {
5158
- StaticJsonRpcProvider: import_ethers18.ethers.JsonRpcProvider
5199
+ StaticJsonRpcProvider: import_ethers19.ethers.JsonRpcProvider
5159
5200
  },
5160
- Contract: import_ethers18.ethers.Contract,
5161
- utils: import_ethers18.ethers
5201
+ Contract: import_ethers19.ethers.Contract,
5202
+ utils: import_ethers19.ethers
5162
5203
  // v6 moved utils to top level
5163
5204
  };
5164
5205
  global.Lit = {
@@ -5432,7 +5473,8 @@ function getSepoliaConfig() {
5432
5473
  bitcoinWithdrawalAddressRegistry: SEPOLIA_CONTRACTS.BitcoinWithdrawalAddressRegistry || "",
5433
5474
  carryAgentRegistry: SEPOLIA_CONTRACTS.CarryAgentRegistry || "",
5434
5475
  agentDelegationRegistry: SEPOLIA_CONTRACTS.AgentDelegationRegistry || "",
5435
- dhAgentDelegate: SEPOLIA_CONTRACTS.DHAgentDelegate || ""
5476
+ dhAgentDelegate: SEPOLIA_CONTRACTS.DHAgentDelegate || "",
5477
+ agentModuleFactory: SEPOLIA_CONTRACTS.AgentModuleFactory || ""
5436
5478
  },
5437
5479
  subgraphs: {
5438
5480
  // Lazy getter for the same module-init snapshot reason as serviceEndpoint.
@@ -5475,7 +5517,9 @@ function getMainnetConfig() {
5475
5517
  bitcoinWithdrawalAddressRegistry: MAINNET_CONTRACTS.BitcoinWithdrawalAddressRegistry || "",
5476
5518
  carryAgentRegistry: MAINNET_CONTRACTS.CarryAgentRegistry || "",
5477
5519
  agentDelegationRegistry: MAINNET_CONTRACTS.AgentDelegationRegistry || "",
5478
- dhAgentDelegate: MAINNET_CONTRACTS.DHAgentDelegate || ""
5520
+ dhAgentDelegate: MAINNET_CONTRACTS.DHAgentDelegate || "",
5521
+ // Empty on mainnet until the factory is deployed there — the ceremony is Sepolia-only.
5522
+ agentModuleFactory: MAINNET_CONTRACTS.AgentModuleFactory || ""
5479
5523
  },
5480
5524
  subgraphs: {
5481
5525
  diamondHandsUrl: "https://gateway-arbitrum.network.thegraph.com/api/subgraphs/id/CCTPsdYqco2jChDLLBQTbdJWwoukVoMt1cXeR9ti6r9A"
@@ -5682,6 +5726,7 @@ __export(src_exports, {
5682
5726
  getDelegateStatus: () => getDelegateStatus,
5683
5727
  getDeploymentByNetwork: () => getDeploymentByNetwork,
5684
5728
  getPositionDelegate: () => getPositionDelegate,
5729
+ getSafeAgentDelegation: () => getSafeAgentDelegation,
5685
5730
  isClosePositionAuth: () => isClosePositionAuth,
5686
5731
  isCreateLoanAuth: () => isCreateLoanAuth,
5687
5732
  isExtendTermAuth: () => isExtendTermAuth,
@@ -6223,7 +6268,10 @@ var import_ethers2 = require("ethers");
6223
6268
  // src/utils/quantum-timing.ts
6224
6269
  var QUANTUM_WINDOW_SECONDS = 60;
6225
6270
  var DEAD_ZONE_SECONDS = 8;
6226
- var INCLUSION_LATENCY_BUDGET = 16;
6271
+ var SLOT_SECONDS = 12;
6272
+ var INCLUSION_SLOT_TOLERANCE = 3;
6273
+ var INCLUSION_LATENCY_BUDGET = SLOT_SECONDS * INCLUSION_SLOT_TOLERANCE;
6274
+ var MAX_GATE_POLLS = 12;
6227
6275
  var POST_BOUNDARY_SKEW_MARGIN = 3;
6228
6276
  function calculateNextQuantumTimestamp() {
6229
6277
  const now = Math.floor(Date.now() / 1e3);
@@ -6256,8 +6304,17 @@ function validateQuantumTiming(signedTimestamp, _bufferSeconds = 30) {
6256
6304
  async function awaitSafeSubmissionWindow(quantumTimestamp, opts = {}) {
6257
6305
  const now = opts.now ?? (() => Math.floor(Date.now() / 1e3));
6258
6306
  const sleep = opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
6259
- const nowSec = now();
6260
6307
  const signatureQuantum = Math.floor(quantumTimestamp / QUANTUM_WINDOW_SECONDS) * QUANTUM_WINDOW_SECONDS;
6308
+ if (opts.chainNow) {
6309
+ const chainResult = await awaitSafeSubmissionWindowOnChain(
6310
+ signatureQuantum,
6311
+ opts.chainNow,
6312
+ sleep
6313
+ );
6314
+ if (chainResult)
6315
+ return chainResult;
6316
+ }
6317
+ const nowSec = now();
6261
6318
  const currentQuantum = Math.floor(nowSec / QUANTUM_WINDOW_SECONDS) * QUANTUM_WINDOW_SECONDS;
6262
6319
  const secsToBoundary = QUANTUM_WINDOW_SECONDS - nowSec % QUANTUM_WINDOW_SECONDS;
6263
6320
  const isNextQuantumSig = signatureQuantum === currentQuantum + QUANTUM_WINDOW_SECONDS;
@@ -6274,6 +6331,44 @@ async function awaitSafeSubmissionWindow(quantumTimestamp, opts = {}) {
6274
6331
  }
6275
6332
  return { waited: false, waitedSeconds: 0 };
6276
6333
  }
6334
+ async function awaitSafeSubmissionWindowOnChain(signatureQuantum, chainNow, sleep) {
6335
+ let waitedSeconds = 0;
6336
+ for (let poll = 0; poll < MAX_GATE_POLLS; poll++) {
6337
+ let head;
6338
+ try {
6339
+ head = await chainNow();
6340
+ } catch {
6341
+ head = null;
6342
+ }
6343
+ if (head === null || !Number.isFinite(head)) {
6344
+ return waitedSeconds > 0 ? { waited: true, waitedSeconds } : null;
6345
+ }
6346
+ const chainQuantum = Math.floor(head / QUANTUM_WINDOW_SECONDS) * QUANTUM_WINDOW_SECONDS;
6347
+ if (signatureQuantum <= chainQuantum) {
6348
+ return { waited: waitedSeconds > 0, waitedSeconds };
6349
+ }
6350
+ const earliestMine = head + SLOT_SECONDS;
6351
+ const latestMine = head + SLOT_SECONDS * INCLUSION_SLOT_TOLERANCE;
6352
+ const safeBeforeDeadZone = latestMine < signatureQuantum - DEAD_ZONE_SECONDS;
6353
+ const safeAfterBoundary = earliestMine >= signatureQuantum;
6354
+ if (safeBeforeDeadZone || safeAfterBoundary) {
6355
+ return { waited: waitedSeconds > 0, waitedSeconds };
6356
+ }
6357
+ const sleepSeconds = Math.max(
6358
+ 1,
6359
+ Math.min(SLOT_SECONDS, signatureQuantum - head)
6360
+ );
6361
+ log.debug(
6362
+ `[Quantum Timing] \u23F3 Deferring send ${sleepSeconds}s to clear the quantum dead zone (sig quantum ${signatureQuantum}, chain head ${head}, insured mine window [${earliestMine}, ${latestMine}])`
6363
+ );
6364
+ await sleep(sleepSeconds * 1e3);
6365
+ waitedSeconds += sleepSeconds;
6366
+ }
6367
+ log.debug(
6368
+ `[Quantum Timing] \u26A0\uFE0F Dead-zone gate exhausted ${MAX_GATE_POLLS} chain polls (sig quantum ${signatureQuantum}) \u2014 proceeding`
6369
+ );
6370
+ return { waited: waitedSeconds > 0, waitedSeconds };
6371
+ }
6277
6372
 
6278
6373
  // src/utils/mint-authorization.utils.ts
6279
6374
  var PKP_NFT_ABI = [
@@ -7230,6 +7325,20 @@ function decodeQuantumRevert(e) {
7230
7325
  }
7231
7326
  return { data, selector, name };
7232
7327
  }
7328
+ var RETRYABLE_QUANTUM_ERROR_NAMES = [
7329
+ "DeadZoneViolation",
7330
+ "QuantumOutsideWindow",
7331
+ "QuantumAlreadyUsed",
7332
+ "QuantumExpired"
7333
+ ];
7334
+ function isRetryableQuantumFailure(message) {
7335
+ if (!message)
7336
+ return false;
7337
+ if (message.includes("Transaction timeout") || message.includes("Transaction confirmation failed") || message.includes("Transaction receipt not found")) {
7338
+ return false;
7339
+ }
7340
+ return RETRYABLE_QUANTUM_ERROR_NAMES.some((name) => message.includes(name)) || message.includes("Transaction reverted");
7341
+ }
7233
7342
 
7234
7343
  // src/utils/quantum-submission.utils.ts
7235
7344
  var MAX_DEADZONE_RESIMULATIONS = 3;
@@ -7251,9 +7360,14 @@ async function assertSafeQuantumSubmission(opts) {
7251
7360
  const now = opts.now ?? (() => Math.floor(Date.now() / 1e3));
7252
7361
  const onDebug = opts.onDebug ?? (() => {
7253
7362
  });
7363
+ const chainNow = opts.chainNow ?? (typeof provider.getBlock === "function" ? async () => {
7364
+ const block = await provider.getBlock("latest");
7365
+ return typeof block?.timestamp === "number" ? block.timestamp : null;
7366
+ } : void 0);
7254
7367
  const gateOpts = {
7255
7368
  now,
7256
- ...opts.sleep ? { sleep: opts.sleep } : {}
7369
+ ...opts.sleep ? { sleep: opts.sleep } : {},
7370
+ ...chainNow ? { chainNow } : {}
7257
7371
  };
7258
7372
  await awaitSafeSubmissionWindow(quantumTimestamp, gateOpts);
7259
7373
  const SAME_BLOCK_INTERACTION_SELECTOR = "0x3b53d1db";
@@ -7338,7 +7452,7 @@ var defaultHttpGetJson = async (url) => {
7338
7452
  }
7339
7453
  return { status: res.status, body };
7340
7454
  };
7341
- async function reconcileAuthorizedSpend(spend, esploraBaseUrl, httpGetJson = defaultHttpGetJson) {
7455
+ async function reconcileAuthorizedSpend(spend, esploraBaseUrl, httpGetJson = defaultHttpGetJson, opts) {
7342
7456
  const base = esploraBaseUrl.replace(/\/+$/, "");
7343
7457
  const fundingRes = await httpGetJson(`${base}/tx/${spend.txid}`);
7344
7458
  if (fundingRes.status === 404) {
@@ -7370,20 +7484,28 @@ async function reconcileAuthorizedSpend(spend, esploraBaseUrl, httpGetJson = def
7370
7484
  };
7371
7485
  }
7372
7486
  const onChainOutputValue = output.value;
7373
- if (onChainOutputValue !== spend.satoshis) {
7374
- return {
7375
- spend,
7376
- status: "CORRUPT",
7377
- onChainOutputValue,
7378
- reason: `Authorization declares ${spend.satoshis} sats for ${spend.txid}:${spend.vout} but the on-chain output is ${onChainOutputValue} sats`
7379
- };
7487
+ if (opts?.vaultAddress) {
7488
+ const outputAddress = (output.scriptpubkey_address ?? "").toLowerCase();
7489
+ if (outputAddress !== opts.vaultAddress.toLowerCase()) {
7490
+ return {
7491
+ spend,
7492
+ status: "CORRUPT",
7493
+ onChainOutputValue,
7494
+ reason: `Authorized outpoint ${spend.txid}:${spend.vout} pays ${output.scriptpubkey_address ?? "an unknown script"}, not the vault ${opts.vaultAddress}`
7495
+ };
7496
+ }
7497
+ } else {
7498
+ log.warn(
7499
+ "Withdrawal reconciliation: no vaultAddress supplied \u2014 skipping the vault-ownership check",
7500
+ { txid: spend.txid, vout: spend.vout }
7501
+ );
7380
7502
  }
7381
- if (spend.targetAmount > onChainOutputValue) {
7503
+ if (spend.targetAmount >= spend.satoshis) {
7382
7504
  return {
7383
7505
  spend,
7384
7506
  status: "CORRUPT",
7385
7507
  onChainOutputValue,
7386
- reason: `Authorized targetAmount ${spend.targetAmount} sats exceeds the ${onChainOutputValue}-sat output it is authorized against`
7508
+ reason: `Authorized targetAmount ${spend.targetAmount} sats is not below the ${spend.satoshis}-sat aggregate input value \u2014 the contract could never have authorized this`
7387
7509
  };
7388
7510
  }
7389
7511
  const outspendRes = await httpGetJson(
@@ -7873,13 +7995,13 @@ function isValidBase58CheckAddress(address) {
7873
7995
  return BASE58_VERSIONS_MAINNET.has(version) || BASE58_VERSIONS_TEST.has(version);
7874
7996
  }
7875
7997
  function isValidBech32Address(address) {
7876
- const lower = address.toLowerCase();
7877
- if (lower !== address && address !== address.toUpperCase()) {
7998
+ const lower2 = address.toLowerCase();
7999
+ if (lower2 !== address && address !== address.toUpperCase()) {
7878
8000
  return false;
7879
8001
  }
7880
8002
  const tryDecode = (codec) => {
7881
8003
  try {
7882
- const decoded = codec.decode(lower);
8004
+ const decoded = codec.decode(lower2);
7883
8005
  if (!BECH32_HRPS.has(decoded.prefix))
7884
8006
  return false;
7885
8007
  if (decoded.words.length < 1)
@@ -11934,7 +12056,7 @@ var GraphClient = class {
11934
12056
  });
11935
12057
  clearTimeout(timeout);
11936
12058
  if (!res.ok) {
11937
- if (res.status === 429 || res.status >= 500) {
12059
+ if (res.status >= 500) {
11938
12060
  await this.backoff(attempt);
11939
12061
  continue;
11940
12062
  }
@@ -11943,12 +12065,7 @@ var GraphClient = class {
11943
12065
  }
11944
12066
  const data = await res.json();
11945
12067
  if (data.errors?.length) {
11946
- const errMsg = JSON.stringify(data.errors);
11947
- if (errMsg.includes("rate limit") && attempt < this.maxRetries) {
11948
- await this.backoff(attempt);
11949
- continue;
11950
- }
11951
- throw new Error(`GraphQL errors: ${errMsg}`);
12068
+ throw new Error(`GraphQL errors: ${JSON.stringify(data.errors)}`);
11952
12069
  }
11953
12070
  return data.data ?? null;
11954
12071
  } catch (err) {
@@ -16856,6 +16973,50 @@ var _abi2 = [
16856
16973
  name: "AutoRepayEnabled",
16857
16974
  type: "event"
16858
16975
  },
16976
+ {
16977
+ anonymous: false,
16978
+ inputs: [
16979
+ {
16980
+ indexed: true,
16981
+ internalType: "bytes32",
16982
+ name: "positionId",
16983
+ type: "bytes32"
16984
+ },
16985
+ {
16986
+ indexed: true,
16987
+ internalType: "address",
16988
+ name: "borrower",
16989
+ type: "address"
16990
+ }
16991
+ ],
16992
+ name: "AutoWithdrawDisabled",
16993
+ type: "event"
16994
+ },
16995
+ {
16996
+ anonymous: false,
16997
+ inputs: [
16998
+ {
16999
+ indexed: true,
17000
+ internalType: "bytes32",
17001
+ name: "positionId",
17002
+ type: "bytes32"
17003
+ },
17004
+ {
17005
+ indexed: true,
17006
+ internalType: "address",
17007
+ name: "borrower",
17008
+ type: "address"
17009
+ },
17010
+ {
17011
+ indexed: false,
17012
+ internalType: "uint32",
17013
+ name: "minWithdrawRatioBps",
17014
+ type: "uint32"
17015
+ }
17016
+ ],
17017
+ name: "AutoWithdrawEnabled",
17018
+ type: "event"
17019
+ },
16859
17020
  {
16860
17021
  anonymous: false,
16861
17022
  inputs: [
@@ -17174,6 +17335,19 @@ var _abi2 = [
17174
17335
  stateMutability: "view",
17175
17336
  type: "function"
17176
17337
  },
17338
+ {
17339
+ inputs: [],
17340
+ name: "SCOPE_WITHDRAW",
17341
+ outputs: [
17342
+ {
17343
+ internalType: "uint32",
17344
+ name: "",
17345
+ type: "uint32"
17346
+ }
17347
+ ],
17348
+ stateMutability: "view",
17349
+ type: "function"
17350
+ },
17177
17351
  {
17178
17352
  inputs: [],
17179
17353
  name: "UPGRADE_INTERFACE_VERSION",
@@ -17364,6 +17538,40 @@ var _abi2 = [
17364
17538
  stateMutability: "view",
17365
17539
  type: "function"
17366
17540
  },
17541
+ {
17542
+ inputs: [
17543
+ {
17544
+ internalType: "bytes32",
17545
+ name: "positionId",
17546
+ type: "bytes32"
17547
+ },
17548
+ {
17549
+ internalType: "address",
17550
+ name: "caller",
17551
+ type: "address"
17552
+ }
17553
+ ],
17554
+ name: "canWithdraw",
17555
+ outputs: [
17556
+ {
17557
+ internalType: "bool",
17558
+ name: "ok",
17559
+ type: "bool"
17560
+ },
17561
+ {
17562
+ internalType: "enum IAgentDelegationRegistry.DenyReason",
17563
+ name: "reason",
17564
+ type: "uint8"
17565
+ },
17566
+ {
17567
+ internalType: "uint32",
17568
+ name: "effectiveFloorBps",
17569
+ type: "uint32"
17570
+ }
17571
+ ],
17572
+ stateMutability: "view",
17573
+ type: "function"
17574
+ },
17367
17575
  {
17368
17576
  inputs: [],
17369
17577
  name: "core",
@@ -17416,6 +17624,19 @@ var _abi2 = [
17416
17624
  stateMutability: "nonpayable",
17417
17625
  type: "function"
17418
17626
  },
17627
+ {
17628
+ inputs: [
17629
+ {
17630
+ internalType: "bytes32",
17631
+ name: "positionId",
17632
+ type: "bytes32"
17633
+ }
17634
+ ],
17635
+ name: "disableAutoWithdraw",
17636
+ outputs: [],
17637
+ stateMutability: "nonpayable",
17638
+ type: "function"
17639
+ },
17419
17640
  {
17420
17641
  inputs: [
17421
17642
  {
@@ -17460,6 +17681,24 @@ var _abi2 = [
17460
17681
  stateMutability: "nonpayable",
17461
17682
  type: "function"
17462
17683
  },
17684
+ {
17685
+ inputs: [
17686
+ {
17687
+ internalType: "bytes32",
17688
+ name: "positionId",
17689
+ type: "bytes32"
17690
+ },
17691
+ {
17692
+ internalType: "uint32",
17693
+ name: "minWithdrawRatioBps",
17694
+ type: "uint32"
17695
+ }
17696
+ ],
17697
+ name: "enableAutoWithdraw",
17698
+ outputs: [],
17699
+ stateMutability: "nonpayable",
17700
+ type: "function"
17701
+ },
17463
17702
  {
17464
17703
  inputs: [
17465
17704
  {
@@ -17486,6 +17725,11 @@ var _abi2 = [
17486
17725
  internalType: "uint32",
17487
17726
  name: "minCollateralRatioBps",
17488
17727
  type: "uint32"
17728
+ },
17729
+ {
17730
+ internalType: "uint32",
17731
+ name: "minWithdrawRatioBps",
17732
+ type: "uint32"
17489
17733
  }
17490
17734
  ],
17491
17735
  internalType: "struct IAgentDelegationRegistry.LoanGrant",
@@ -17970,7 +18214,7 @@ var _abi2 = [
17970
18214
  type: "function"
17971
18215
  }
17972
18216
  ];
17973
- var _bytecode2 = "0x60a080604052346100ea57306080527ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a005460ff8160401c166100d9576002600160401b03196001600160401b03821601610073575b604051612d3290816100f0823960805181818161101d01526112020152f35b6001600160401b0319166001600160401b039081177ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a005581527fc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d290602090a13880610054565b63f92ee8a960e01b60005260046000fd5b600080fdfe608080604052600436101561001357600080fd5b60003560e01c90816301ffc9a714611dec575080630d33f3c214611dc85780630f3f4a9414611d7d5780631459457a146119d757806318c38b89146118c65780631d444541146117f5578063248a9ca3146117cf5780632ec22d4c1461170e5780632f0647ce146116935780632f2ff15d1461166257806336568abe1461161c578063392f1087146115cf5780633f4ba83a146114d75780633f5aadc9146114685780634f1ef28614611186578063523403631461109d5780635273b6f61461107457806352d1902d1461100a578063554c4f4b14610f7a578063561b7f8a14610e4e5780635bc26aea14610dc15780635c975abb14610d915780636289d18c14610d105780636711cc0b14610cf45780636e852a7814610bf35780637e35bf6e14610bbf5780638343f67414610b925780638456cb5914610a9c5780638f33527f14610a625780639010d07c14610a0e57806391d14854146109b45780639b9d30c11461098b578063a19af3f6146108c9578063a217fddf146108ad578063a3246ad3146107eb578063a8780864146107cf578063ac3c0e30146106c2578063ad3cb1cc1461067b578063b3fad638146105ef578063c34e9c26146105d3578063ca15c8731461059a578063d547741f14610562578063dd71d99a14610520578063e0922fa114610473578063efa9b4ae14610404578063f2f4eb26146103db578063f68e9553146103b2578063fd437d39146102735763ffa1ad741461023a57600080fd5b3461026e57600036600319011261026e5761026a610256611fb9565b604051918291602083526020830190611f94565b0390f35b600080fd5b3461026e57606036600319011261026e5761028c611e6f565b610294611e59565b9061029d611f5b565b906102a661247b565b6001600160a01b0316338190036103a25780600052600160205260406000209283549360ff8560e01c16600381101561038c5760010361037b57816001602092610312877fc5d5c5bbf10444998e0ea6d4725e79027b574e1c14ec929279e8a9a95c6c6e919688612929565b818060a01b038316828060a01b03198254161781556103318782612111565b0180546001600160401b031916426001600160401b03908116919091179091556001600160a01b0391821660008181526002855260409081902087905551969091168652951693a4005b632437e77160e11b60005260046000fd5b634e487b7160e01b600052602160045260246000fd5b62fca64560e31b60005260046000fd5b3461026e57600036600319011261026e576020604051600080516020612cc68339815191528152f35b3461026e57600036600319011261026e576003546040516001600160a01b039091168152602090f35b3461026e57602036600319011261026e5760043561042061247b565b61044261042c826124c6565b9190600163ffffffff825460a01c161790612037565b6001600160a01b0316907f812690e0a9dff5bf3add998111b8af5c8299608d859025bfe35662060ab4267d600080a3005b3461026e57602036600319011261026e576004356001600160401b0381169081810361026e576000546001600160a01b038116330361050f57600160a01b600160e01b0319811660a092831b600160a01b600160e01b031617600055604080519190921c6001600160401b0316815260208101929092527f9c195448332abd01f716db873b329752ea02f19d33f7e311fb0daf082c4edd0391a1005b6349db3bd360e01b60005260046000fd5b3461026e57602036600319011261026e576001600160a01b03610541611e6f565b166000526002602052602060018060a01b0360406000205416604051908152f35b3461026e57604036600319011261026e57610598600435610581611e59565b9061059361058e82612016565b6123a5565b612437565b005b3461026e57602036600319011261026e57600435600052600080516020612bc68339815191526020526020604060002054604051908152f35b3461026e57600036600319011261026e57602060405160028152f35b3461026e57602036600319011261026e5760006040805161060f81611f02565b828152826020820152015260043560005260046020526060604060002063ffffffff6040519161063e83611f02565b548160018060a01b038216938481528160406020830192828660a01c168452019360c01c1683526040519485525116602084015251166040820152f35b3461026e57600036600319011261026e5761026a604080519061069e8183611f1d565b60058252640352e302e360dc1b602083015251918291602083526020830190611f94565b3461026e57602036600319011261026e576106db611e6f565b600060806040516106eb81611ed1565b828152826020820152826040820152826060820152015260018060a01b03166000526001602052604060002060405161072381611ed1565b81546001600160a01b038116825260a081901c6001600160401b03166020830190815292604083019160e01c60ff1690600382101561038c57908252600101546001600160401b0380821660608501908152604092831c821660808601908152925194516001600160a01b031685529451166020840152905192600384101561038c57604083019390935291516001600160401b0390811660608301529151909116608082015260a090f35b3461026e57600036600319011261026e57602060405160018152f35b3461026e57602036600319011261026e57600435600052600080516020612bc683398151915260205260406000206040518060208354918281520190819360005260206000209060005b818110610897575050508161084b910382611f1d565b6040519182916020830190602084525180915260408301919060005b818110610875575050500390f35b82516001600160a01b0316845285945060209384019390920191600101610867565b8254845260209093019260019283019201610835565b3461026e57600036600319011261026e57602060405160008152f35b3461026e57604036600319011261026e576004356108e5611ebe565b906108ee61247b565b63ffffffff600554169163ffffffff811692808410908115610982575b506109715760207f1da95b87b4c773f68c1447ffc5644e41a8a8887d55065beb64852014cf7140be9161095e610940856124c6565b9290610959600463ffffffff835460a01c161782612037565b611fdc565b6040519485526001600160a01b031693a3005b634346c13160e01b60005260046000fd5b9050158461090b565b3461026e57600036600319011261026e576020604051600080516020612c068339815191528152f35b3461026e57604036600319011261026e576109cd611e59565b600435600052600080516020612ca683398151915260205260406000209060018060a01b0316600052602052602060ff604060002054166040519015158152f35b3461026e57604036600319011261026e57600435600052600080516020612bc68339815191526020526020610a4960243560406000206129f2565b905460405160039290921b1c6001600160a01b03168152f35b3461026e57602036600319011261026e576001600160a01b03610a83611e6f565b1660005260026020526020604060002054604051908152f35b3461026e57600036600319011261026e573360009081527f0ac8cc3d209806126662940717264973110c39045b305f6ba49613325a615e09602052604090205460ff1615610b6b57610aec61247b565b600160ff19600080516020612ce6833981519152541617600080516020612ce6833981519152557f62e78cea01bee320cd4e420270b5ea74000d11b0c9f74754ebdbfc544b05a2586020604051338152a16040514281527f80170b5fcdd2bf1e0660ef4b8851f86685f64d41b1a19de1471947ece8725aac60203392a2005b63e2517d3f60e01b60005233600452600080516020612c0683398151915260245260446000fd5b3461026e57600036600319011261026e5760005460405160a09190911c6001600160401b03168152602090f35b3461026e57604036600319011261026e57610be3610bdb611e59565b6004356120de565b9061026a60405192839283611e8f565b3461026e57604036600319011261026e57610c0c611e6f565b336000908152600080516020612c46833981519152602052604090205460ff161580610ce1575b6103a2576001600160a01b03166000818152600160205260409020805460e01c60ff16600381101561038c5760010361037b57805460ff60e01b198116600160e11b1782556001919091018054600160401b600160801b03191642604090811b600160401b600160801b0316919091179091555160243581526001600160a01b0390911691907fc0499cc07ed9d734deec6857802d3f0fa8d5afaf79a7008beb8ba7bef62a271b90602090a3005b50336001600160a01b0382161415610c33565b3461026e57600036600319011261026e57602060405160048152f35b3461026e57602036600319011261026e57610d29611eab565b6000546001600160a01b0316330361050f5763ffffffff8116908115610d8057600080516020612c8683398151915291610d7b63ffffffff926005549284198416176005556040519384931683611fff565b0390a1005b6338f3377360e21b60005260046000fd5b3461026e57600036600319011261026e57602060ff600080516020612ce683398151915254166040519015158152f35b3461026e57602036600319011261026e57610dda611e6f565b6000546001600160a01b038116903382900361050f576001600160a01b038316928315610e3d573b15610e2c576001600160a01b03191682176000908155600080516020612c668339815191529080a3005b636f7c43f160e01b60005260046000fd5b63e6c4247b60e01b60005260046000fd5b3461026e57606036600319011261026e57610e67611e6f565b610e6f611e59565b610e77611f5b565b91610e8061247b565b6001600160a01b031690338290036103a257816000526001602052604060002060ff815460e01c16600381101561038c57600114610f695781602091610ee8867ff223dc085722c94c84dcf67427e225799be616382a2a484d378f8921c69a20339587612929565b80546001600160a01b0319166001600160a01b038316178155610f0b8682612111565b6001810180546001600160801b031916426001600160401b0390811691909117909155815460ff60e01b1916600160e01b179091556001600160a01b03919091166000818152600284526040908190208690555195909116855293a3005b639d2c8fdf60e01b60005260046000fd5b3461026e57602036600319011261026e576001600160a01b03610f9b611e6f565b166000526001602052604060002060ff600080516020612ce683398151915254161580610fee575b60209181610fd7575b506040519015158152f35b5460a01c6001600160401b03164210905082610fcc565b5060ff815460e01c1690600382101561038c5790600114610fc3565b3461026e57600036600319011261026e577f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03163003611063576020604051600080516020612c268339815191528152f35b63703e46dd60e11b60005260046000fd5b3461026e57600036600319011261026e576000546040516001600160a01b039091168152602090f35b3461026e57602036600319011261026e57600435604060008281526004602052209081549160018060a01b038316928315908115611177575b5061116657336000908152600080516020612c46833981519152602052604090205460ff16158061115c575b61114b578061111e63fffffffe611124935460a01c1682612037565b82612381565b7fa5ffbdba01b15df97334e34ad816b9986b6057a5ddc97aba93b7e8ccd21759ed600080a3005b630a606ec360e11b60005260046000fd5b5082331415611102565b6360eb6c2360e01b60005260046000fd5b6001915060a01c1615846110d6565b604036600319011261026e5761119a611e6f565b602435906001600160401b03821161026e573660238301121561026e578160040135906111c682611f40565b916111d46040519384611f1d565b8083526020830193366024838301011161026e57816000926024602093018737840101526001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016308114908115611445575b50611063576000546001600160a01b0316330361050f576001600160a01b038116928315610e3d57813b156114345730841461142557600080516020612c26833981519152548461127c611fb9565b7f6ea8b34d59b2818d3f37a08dddaa06f5886b9c20c907b36f03ca6528e106e86f6112b260405192604084526040840190611f94565b4260208401526001600160a01b038516929081900390a36040516352d1902d60e01b8152602081600481895afa600091816113f1575b506113025785634c9c8ce360e01b60005260045260246000fd5b9081600080516020612c268339815191528793036113dd5750833b156113c8576001600160a01b0319168117600080516020612c26833981519152557fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b600080a28251156113ad5760008091610598945190845af43d156113a5573d9161138883611f40565b926113966040519485611f1d565b83523d6000602085013e612b64565b606091612b64565b505050346113b757005b63b398979f60e01b60005260046000fd5b50634c9c8ce360e01b60005260045260246000fd5b632a87526960e21b60005260045260246000fd5b9091506020813d60201161141d575b8161140d60209383611f1d565b8101031261026e575190876112e8565b3d9150611400565b614e8d60eb1b60005260046000fd5b632e07c4bd60e11b60005260046000fd5b600080516020612c26833981519152546001600160a01b0316141590508461122d565b3461026e57602036600319011261026e5760043561148461247b565b6114a6611490826124c6565b9190600263ffffffff825460a01c161790612037565b6001600160a01b0316907fc2713e691344add4705edc62a0ee5aede95ba66ad68f97316efef9cb50644edd600080a3005b3461026e57600036600319011261026e57336000908152600080516020612c46833981519152602052604090205460ff1615806115ba575b6115a957600080516020612ce68339815191525460ff8116156115985760ff1916600080516020612ce6833981519152557f5db9ee0a495bf2e6ff9c91a7834c1ba4fdd244a5e8aa4e537bd38aeae4b073aa6020604051338152a16040514281527f107553d8191d85b405879cf752997865edd48d94e20bda4dd27223c94b31a7cc60203392a2005b638dfc202b60e01b60005260046000fd5b63659a07b360e11b60005260046000fd5b506000546001600160a01b031633141561150f565b3461026e57604036600319011261026e57606063ffffffff6115fa6115f2611e59565b60043561205a565b90604093929351931515845261160f81611e85565b6020840152166040820152f35b3461026e57604036600319011261026e57611635611e59565b336001600160a01b038216036116515761059890600435612437565b63334bd91960e11b60005260046000fd5b3461026e57604036600319011261026e57610598600435611681611e59565b9061168e61058e82612016565b6123ef565b3461026e57602036600319011261026e576004356116dd6116b38261230f565b91906116c963fffffffb825460a01c1682612037565b805463ffffffff60c01b1916815583612381565b6001600160a01b0316907f09cb4ec44cdef8489412844b9682f87f7900758fa2bd7e039113e9125e538e0f600080a3005b3461026e57602036600319011261026e57600435604060008281526004602052209081549160018060a01b0383169283159081156117c0575b5061116657336000908152600080516020612c46833981519152602052604090205460ff1615806117b6575b61114b578061111e63fffffffd61178f935460a01c1682612037565b7fb390ab029eab314bd18190ebe73d7e5c1c336beaa5c83ac22578378dc9ccc405600080a3005b5082331415611773565b6002915060a01c161584611747565b3461026e57602036600319011261026e5760206117ed600435612016565b604051908152f35b3461026e57604036600319011261026e57600435611811611ebe565b63ffffffff600554169063ffffffff8116918083109081156118bd575b506109715761183c8361230f565b909263ffffffff845460c01c16809260018060a01b031633141591826118b2575b50506118a15761188d827e8a218591d4362499c38c38eb1c12e7a7cc97a9068818d1fe6046a42ba4a5ba94611fdc565b61189c60405192839283611fff565b0390a2005b634e80a40760e11b60005260046000fd5b11159050818661185d565b9050158461182e565b3461026e57602036600319011261026e576118df611eab565b600080516020612d068339815191525460ff8160401c169081156119bf575b506119ae57600080516020612d06833981519152805460026001600160401b0319821681179092556000546001600160481b031990911690911791906001600160a01b0316330361050f5763ffffffff168015610d8057604081600080516020612c868339815191529263ffffffff196005541617600555815190600082526020820152a1600080516020612d0683398151915255600080516020612be6833981519152602060405160028152a1005b63f92ee8a960e01b60005260046000fd5b60026001600160401b039190911610159050826118fe565b3461026e5760a036600319011261026e576119f0611e6f565b6119f8611e59565b906044356001600160a01b03811680820361026e576064356001600160a01b0381169182820361026e576084356001600160a01b038116949085900361026e57600080516020612d0683398151915254604081901c60ff161596906001600160401b031680159081611d75575b6001149081611d6b575b159081611d62575b506119ae57600080516020612d0683398151915280546001600160401b031916600117905586611d3d575b611aaa6127b2565b611ab26127b2565b611aba6127b2565b611ac26127b2565b6001600160a01b038116908115610e3d578315610e3d578515610e3d576001600160a01b038916988915610e3d573b15610e2c57858414611d2c5762aa36a746148015611d21575b8015611d16575b1580611d04575b610e2c57611b279060006127e0565b9081611cbd575b5015611c0257611b4c90600080516020612cc68339815191526127e0565b9081611c68575b5015611c0257611b7190600080516020612c068339815191526127e0565b9081611c13575b5015611c0257600080546001600160a01b031916841781556040519390600080516020612c668339815191528180a38015610e3d57803b15610e2c57600380546001600160a01b031916919091179055611bce57005b600080516020612d06833981519152805460ff60401b1916905560018152600080516020612be683398151915290602090a1005b63cf5a8e3360e01b60005260046000fd5b600080516020612c06833981519152600052600080516020612bc6833981519152602052611c61907ffff5b43478fa6840382eb63777e0da6941d320b940e3f70f0ea4780feff95827612a20565b5084611b78565b600080516020612cc6833981519152600052600080516020612bc6833981519152602052611cb6907f32d31393d541a265984ec379cddd8b1fcbe5f2b0eb8ad81dddfead454f135eaa612a20565b5086611b53565b60008052600080516020612bc6833981519152602052611cfd907f615f0f9e84155bea8cc509fe18befeb1baf65611e38a6ba60964480fb29dfd44612a20565b5088611b2e565b50823b1580611b185750843b15611b18565b50617a694614611b11565b506105394614611b0a565b638135316560e01b60005260046000fd5b600080516020612d06833981519152805460ff60401b1916600160401b179055611aa2565b90501589611a77565b303b159150611a6f565b889150611a65565b3461026e57604036600319011261026e5761026a611db1611d9c611e59565b6004356000526004602052604060002061213a565b611dba81611e85565b604051918183921583611e8f565b3461026e57600036600319011261026e57602063ffffffff60055416604051908152f35b3461026e57602036600319011261026e576004359063ffffffff60e01b821680920361026e57602091635a05180f60e01b8114908115611e2e575b5015158152f35b637965db0b60e01b811491508115611e48575b5083611e27565b6301ffc9a760e01b14905083611e41565b602435906001600160a01b038216820361026e57565b600435906001600160a01b038216820361026e57565b6008111561038c57565b901515815260408101929190602090611ea783611e85565b0152565b6004359063ffffffff8216820361026e57565b6024359063ffffffff8216820361026e57565b60a081019081106001600160401b03821117611eec57604052565b634e487b7160e01b600052604160045260246000fd5b606081019081106001600160401b03821117611eec57604052565b601f909101601f19168101906001600160401b03821190821017611eec57604052565b6001600160401b038111611eec57601f01601f191660200190565b604435906001600160401b038216820361026e57565b60005b838110611f845750506000910152565b8181015183820152602001611f74565b90602091611fad81518092818552858086019101611f71565b601f01601f1916010190565b60405190611fc8604083611f1d565b60058252640322e302e360dc1b6020830152565b805463ffffffff60c01b191660c09290921b63ffffffff60c01b16919091179055565b63ffffffff91821681529116602082015260400190565b600052600080516020612ca683398151915260205260016040600020015490565b805463ffffffff60a01b191660a09290921b63ffffffff60a01b16919091179055565b9190916000526004602052612074604060002092836121f7565b9161207e83611e85565b826120d45763ffffffff9192505460c01c1663ffffffff60055416811580156120cc575b6120bf578082106120b857505b60019160009190565b90506120af565b5050600090600390600090565b5080156120a2565b5060009190600090565b906120f59160005260046020526040600020612283565b6120fe81611e85565b8061210c5750600190600090565b600091565b8054600160a01b600160e01b03191660a09290921b600160a01b600160e01b0316919091179055565b60ff600080516020612ce683398151915254166121f057546001600160a01b0381169081156121e85760a01c600216156121e15760005260016020526040600020549060ff8260e01c16600381101561038c5780156121d9576002146121d2574260a083901c6001600160401b031611156121cb576001600160a01b039081169116036121c657600090565b600790565b5050600690565b5050600590565b505050600490565b5050600390565b505050600290565b5050600190565b60ff600080516020612ce683398151915254166121f057546001600160a01b0381169081156121e85760a01c600416156121e15760005260016020526040600020549060ff8260e01c16600381101561038c5780156121d9576002146121d2574260a083901c6001600160401b031611156121cb576001600160a01b039081169116036121c657600090565b60ff600080516020612ce683398151915254166121f057546001600160a01b0381169081156121e85760a01c600116156121e15760005260016020526040600020549060ff8260e01c16600381101561038c5780156121d9576002146121d2574260a083901c6001600160401b031611156121cb576001600160a01b039081169116036121c657600090565b6000908152600460205260409020805490916001600160a01b038216918215908115612372575b5061116657336000908152600080516020612c46833981519152602052604090205460ff161580612368575b61114b57565b5081331415612362565b6004915060a01c161538612336565b905460a01c63ffffffff16156123945750565b600052600460205260006040812055565b6000818152600080516020612ca68339815191526020908152604080832033845290915290205460ff16156123d75750565b63e2517d3f60e01b6000523360045260245260446000fd5b6123f982826127e0565b918261240457505090565b6000918252600080516020612bc68339815191526020526040909120612433916001600160a01b031690612a20565b5090565b6124418282612889565b918261244c57505090565b6000918252600080516020612bc68339815191526020526040909120612433916001600160a01b031690612a91565b60ff600080516020612ce6833981519152541661249457565b63d93c066560e01b60005260046000fd5b519064ffffffffff8216820361026e57565b519061ffff8216820361026e57565b80156127a157600354604051638d34875560e01b8152600481018390529190600090839060249082906001600160a01b03165afa918215612795576000926125ea575b5060808201516001600160a01b0316913383900361114b57610120015161252f81611e85565b61253881611e85565b600381149081156125d5575b81156125c1575b506125b05781600052600160205260ff60406000205460e01c16600381101561038c5760010361259f57600090815260046020526040902080546001600160a01b0319166001600160a01b03831617815591565b632e68d1d360e21b60005260046000fd5b63e9f6db8560e01b60005260046000fd5b600791506125ce81611e85565b143861254b565b90506125e081611e85565b6005811490612544565b90913d8082843e6125fb8184611f1d565b820191602081840312612775578051906001600160401b03821161277d5701906101a08284031261277257604051926101a084016001600160401b038111858210176127815760409081528351855260208085015190860152838101519085015260608301516001600160401b03811161277d57830181601f8201121561277d5780519061268882611f40565b926126966040519485611f1d565b8284526020838301011161277957906126b59160208085019101611f71565b606084015260808201516001600160a01b03811681036127755760808401526126e060a083016124a5565b60a08401526126f160c083016124a5565b60c084015261270260e083016124b7565b60e084015261271461010083016124a5565b6101008401526101208201519060088210156127725750612765916101809161012085015261274661014082016124a5565b61014085015261275961016082016124b7565b610160850152016124a5565b6101808201529038612509565b80fd5b5080fd5b8380fd5b8280fd5b634e487b7160e01b83526041600452602483fd5b6040513d6000823e3d90fd5b637bb0b62160e01b60005260046000fd5b60ff600080516020612d068339815191525460401c16156127cf57565b631afcd79f60e31b60005260046000fd5b6000818152600080516020612ca6833981519152602090815260408083206001600160a01b038616845290915290205460ff16612882576000818152600080516020612ca6833981519152602090815260408083206001600160a01b0395909516808452949091528120805460ff19166001179055339291907f2f8788117e7eff1d82e926ec794901d17c78024a50270940304540a733656f0d9080a4600190565b5050600090565b6000818152600080516020612ca6833981519152602090815260408083206001600160a01b038616845290915290205460ff1615612882576000818152600080516020612ca6833981519152602090815260408083206001600160a01b0395909516808452949091528120805460ff19169055339291907ff6391f5c32d9c69d2a47ea670b442974b53935d1edc7fd64eb21e047a839171b9080a4600190565b1580156129e1575b610e3d576001600160a01b03166000908152600260205260409020546129d0576001600160401b0316428111156129bf5760005460a01c6001600160401b03168015159182612994575b505061298357565b63e774e7b560e01b60005260046000fd5b9091504281039081116129a95711388061297b565b634e487b7160e01b600052601160045260246000fd5b63efa7aefd60e01b60005260046000fd5b633b53f64f60e21b60005260046000fd5b506001600160a01b03811615612931565b8054821015612a0a5760005260206000200190600090565b634e487b7160e01b600052603260045260246000fd5b6001810190826000528160205260406000205415600014612a89578054600160401b811015611eec57612a74612a5d8260018794018555846129f2565b819391549060031b91821b91600019901b19161790565b90555491600052602052604060002055600190565b505050600090565b9060018201918160005282602052604060002054801515600014612b5b5760001981018181116129a95782546000198101919082116129a957818103612b24575b50505080548015612b0e576000190190612aec82826129f2565b8154906000199060031b1b191690555560005260205260006040812055600190565b634e487b7160e01b600052603160045260246000fd5b612b44612b34612a5d93866129f2565b90549060031b1c928392866129f2565b905560005283602052604060002055388080612ad2565b50505050600090565b90612b8a5750805115612b7957602081519101fd5b63d6bda27560e01b60005260046000fd5b81511580612bbc575b612b9b575090565b639996b31560e01b60009081526001600160a01b0391909116600452602490fd5b50803b15612b9356fec1f6fe24621ce81ec5827caf0253cadb74709b061630e6b55e82371705932000c7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d275027301df982f5215b4963a854cf9c73219df80cb6acd8f098189c9480c7bbc360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc07966f2ab141347034724e9b2a472c2f77f5cd745aeba27cacba797b8fcb708617dc5b1290f7495d7f8cc68476e6941aaf510598fa925d0d2720edc8805a846cd37bc77615afefc793aa17d75d4249a9b517ba91b0e54a49c9c4a44b3d7e228302dd7bc7dec4dceedda775e58dd541e08a116c6c53815c0bd028192f7b626800edcc084d3dcd65a1f7f23c65c46722faca6953d28e43150a467cf43e5c309238cd5ed15c6e187e77e9aee88184c21f4f2182ab5827cb3b7e07fbedcd63f03300f0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a00a164736f6c6343000822000a";
18217
+ var _bytecode2 = "0x60a080604052346100ea57306080527ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a005460ff8160401c166100d9576002600160401b03196001600160401b03821601610073575b60405161304c90816100f0823960805181818161114401526113290152f35b6001600160401b0319166001600160401b039081177ff0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a005581527fc7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d290602090a13880610054565b63f92ee8a960e01b60005260046000fd5b600080fdfe608080604052600436101561001357600080fd5b60003560e01c90816301ffc9a714611ff7575080630d33f3c214611fd35780630f3f4a9414611f885780631459457a14611be257806318c38b8914611ad15780631d44454114611a005780632106764c146119d9578063248a9ca3146119b35780632ec22d4c146118f25780632f0647ce146118775780632f2ff15d14611846578063324207441461177257806336568abe1461172c578063392f1087146116f65780633f4ba83a146115fe5780633f5aadc91461158f5780634f1ef286146112ad57806352340363146111c45780635273b6f61461119b57806352d1902d14611131578063554c4f4b146110a1578063561b7f8a14610f755780635bc26aea14610ee85780635c975abb14610eb85780636289d18c14610e375780636711cc0b14610e1b5780636e852a7814610d1a5780637e35bf6e14610ce65780638343f67414610cb95780638456cb5914610bc35780638f33527f14610b895780639010d07c14610b3557806391d1485414610adb5780639b9d30c114610ab2578063a19af3f614610a01578063a217fddf146109e5578063a3246ad314610923578063a878086414610907578063ac3c0e30146107fa578063ad3cb1cc146107b3578063b3fad6381461070e578063c34e9c26146106f2578063c86deb31146106d6578063ca15c8731461069d578063d547741f14610665578063dd71d99a14610623578063e0922fa114610576578063efa9b4ae14610507578063f2f4eb26146104de578063f68e9553146104b5578063f94f67ab146103de578063fd437d391461029f5763ffa1ad741461026657600080fd5b3461029a57600036600319011261029a576102966102826121ed565b6040519182916020835260208301906121c8565b0390f35b600080fd5b3461029a57606036600319011261029a576102b861207a565b6102c0612064565b906102c961218f565b906102d2612795565b6001600160a01b0316338190036103ce5780600052600160205260406000209283549360ff8560e01c1660038110156103b8576001036103a75781600160209261033e877fc5d5c5bbf10444998e0ea6d4725e79027b574e1c14ec929279e8a9a95c6c6e919688612c43565b818060a01b038316828060a01b031982541617815561035d878261239f565b0180546001600160401b031916426001600160401b03908116919091179091556001600160a01b0391821660008181526002855260409081902087905551969091168652951693a4005b632437e77160e11b60005260046000fd5b634e487b7160e01b600052602160045260246000fd5b62fca64560e31b60005260046000fd5b3461029a57604036600319011261029a576004356103fa6120c9565b90610403612795565b63ffffffff600554169163ffffffff8116928084109081156104ac575b5061049b5760207f6b7727cc9c3df4a29acf0afe01594c1b7e845d20f40f4a14e4c292164a9ed8e191610452846127e0565b9161046a600863ffffffff845460a01c1617836122e3565b81546001600160e01b031660e09190911b6001600160e01b0319161790556040519485526001600160a01b031693a3005b634346c13160e01b60005260046000fd5b90501584610420565b3461029a57600036600319011261029a576020604051600080516020612fe08339815191528152f35b3461029a57600036600319011261029a576003546040516001600160a01b039091168152602090f35b3461029a57602036600319011261029a57600435610523612795565b61054561052f826127e0565b9190600163ffffffff825460a01c1617906122e3565b6001600160a01b0316907f812690e0a9dff5bf3add998111b8af5c8299608d859025bfe35662060ab4267d600080a3005b3461029a57602036600319011261029a576004356001600160401b0381169081810361029a576000546001600160a01b038116330361061257600160a01b600160e01b0319811660a092831b600160a01b600160e01b031617600055604080519190921c6001600160401b0316815260208101929092527f9c195448332abd01f716db873b329752ea02f19d33f7e311fb0daf082c4edd0391a1005b6349db3bd360e01b60005260046000fd5b3461029a57602036600319011261029a576001600160a01b0361064461207a565b166000526002602052602060018060a01b0360406000205416604051908152f35b3461029a57604036600319011261029a5761069b600435610684612064565b90610696610691826122c2565b6126bf565b612751565b005b3461029a57602036600319011261029a57600435600052600080516020612ee08339815191526020526020604060002054604051908152f35b3461029a57600036600319011261029a57602060405160088152f35b3461029a57600036600319011261029a57602060405160028152f35b3461029a57602036600319011261029a576000606060405161072f81612136565b828152826020820152826040820152015260043560005260046020526080604060002063ffffffff6040519161076483612136565b548160018060a01b038216938481528160208201818560a01c16815260606040840193838760c01c168552019460e01c8552604051968752511660208601525116604084015251166060820152f35b3461029a57600036600319011261029a5761029660408051906107d68183612151565b60058252640352e302e360dc1b6020830152519182916020835260208301906121c8565b3461029a57602036600319011261029a5761081361207a565b6000608060405161082381612105565b828152826020820152826040820152826060820152015260018060a01b03166000526001602052604060002060405161085b81612105565b81546001600160a01b038116825260a081901c6001600160401b03166020830190815292604083019160e01c60ff169060038210156103b857908252600101546001600160401b0380821660608501908152604092831c821660808601908152925194516001600160a01b03168552945116602084015290519260038410156103b857604083019390935291516001600160401b0390811660608301529151909116608082015260a090f35b3461029a57600036600319011261029a57602060405160018152f35b3461029a57602036600319011261029a57600435600052600080516020612ee083398151915260205260406000206040518060208354918281520190819360005260206000209060005b8181106109cf5750505081610983910382612151565b6040519182916020830190602084525180915260408301919060005b8181106109ad575050500390f35b82516001600160a01b031684528594506020938401939092019160010161099f565b825484526020909301926001928301920161096d565b3461029a57600036600319011261029a57602060405160008152f35b3461029a57604036600319011261029a57600435610a1d6120c9565b90610a26612795565b63ffffffff600554169163ffffffff811692808410908115610aa9575b5061049b5760207f1da95b87b4c773f68c1447ffc5644e41a8a8887d55065beb64852014cf7140be91610a96610a78856127e0565b9290610a91600463ffffffff835460a01c1617826122e3565b612210565b6040519485526001600160a01b031693a3005b90501584610a43565b3461029a57600036600319011261029a576020604051600080516020612f208339815191528152f35b3461029a57604036600319011261029a57610af4612064565b600435600052600080516020612fc083398151915260205260406000209060018060a01b0316600052602052602060ff604060002054166040519015158152f35b3461029a57604036600319011261029a57600435600052600080516020612ee08339815191526020526020610b706024356040600020612d0c565b905460405160039290921b1c6001600160a01b03168152f35b3461029a57602036600319011261029a576001600160a01b03610baa61207a565b1660005260026020526020604060002054604051908152f35b3461029a57600036600319011261029a573360009081527f0ac8cc3d209806126662940717264973110c39045b305f6ba49613325a615e09602052604090205460ff1615610c9257610c13612795565b600160ff19600080516020613000833981519152541617600080516020613000833981519152557f62e78cea01bee320cd4e420270b5ea74000d11b0c9f74754ebdbfc544b05a2586020604051338152a16040514281527f80170b5fcdd2bf1e0660ef4b8851f86685f64d41b1a19de1471947ece8725aac60203392a2005b63e2517d3f60e01b60005233600452600080516020612f2083398151915260245260446000fd5b3461029a57600036600319011261029a5760005460405160a09190911c6001600160401b03168152602090f35b3461029a57604036600319011261029a57610d0a610d02612064565b60043561236c565b906102966040519283928361209a565b3461029a57604036600319011261029a57610d3361207a565b336000908152600080516020612f60833981519152602052604090205460ff161580610e08575b6103ce576001600160a01b03166000818152600160205260409020805460e01c60ff1660038110156103b8576001036103a757805460ff60e01b198116600160e11b1782556001919091018054600160401b600160801b03191642604090811b600160401b600160801b0316919091179091555160243581526001600160a01b0390911691907fc0499cc07ed9d734deec6857802d3f0fa8d5afaf79a7008beb8ba7bef62a271b90602090a3005b50336001600160a01b0382161415610d5a565b3461029a57600036600319011261029a57602060405160048152f35b3461029a57602036600319011261029a57610e506120b6565b6000546001600160a01b031633036106125763ffffffff8116908115610ea757600080516020612fa083398151915291610ea263ffffffff926005549284198416176005556040519384931683612233565b0390a1005b6338f3377360e21b60005260046000fd5b3461029a57600036600319011261029a57602060ff60008051602061300083398151915254166040519015158152f35b3461029a57602036600319011261029a57610f0161207a565b6000546001600160a01b0381169033829003610612576001600160a01b038316928315610f64573b15610f53576001600160a01b03191682176000908155600080516020612f808339815191529080a3005b636f7c43f160e01b60005260046000fd5b63e6c4247b60e01b60005260046000fd5b3461029a57606036600319011261029a57610f8e61207a565b610f96612064565b610f9e61218f565b91610fa7612795565b6001600160a01b031690338290036103ce57816000526001602052604060002060ff815460e01c1660038110156103b857600114611090578160209161100f867ff223dc085722c94c84dcf67427e225799be616382a2a484d378f8921c69a20339587612c43565b80546001600160a01b0319166001600160a01b038316178155611032868261239f565b6001810180546001600160801b031916426001600160401b0390811691909117909155815460ff60e01b1916600160e01b179091556001600160a01b03919091166000818152600284526040908190208690555195909116855293a3005b639d2c8fdf60e01b60005260046000fd5b3461029a57602036600319011261029a576001600160a01b036110c261207a565b166000526001602052604060002060ff60008051602061300083398151915254161580611115575b602091816110fe575b506040519015158152f35b5460a01c6001600160401b031642109050826110f3565b5060ff815460e01c169060038210156103b857906001146110ea565b3461029a57600036600319011261029a577f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316300361118a576020604051600080516020612f408339815191528152f35b63703e46dd60e11b60005260046000fd5b3461029a57600036600319011261029a576000546040516001600160a01b039091168152602090f35b3461029a57602036600319011261029a57600435604060008281526004602052209081549160018060a01b03831692831590811561129e575b5061128d57336000908152600080516020612f60833981519152602052604090205460ff161580611283575b611272578061124563fffffffe61124b935460a01c16826122e3565b8261269b565b7fa5ffbdba01b15df97334e34ad816b9986b6057a5ddc97aba93b7e8ccd21759ed600080a3005b630a606ec360e11b60005260046000fd5b5082331415611229565b6360eb6c2360e01b60005260046000fd5b6001915060a01c1615846111fd565b604036600319011261029a576112c161207a565b602435906001600160401b03821161029a573660238301121561029a578160040135906112ed82612174565b916112fb6040519384612151565b8083526020830193366024838301011161029a57816000926024602093018737840101526001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001630811490811561156c575b5061118a576000546001600160a01b03163303610612576001600160a01b038116928315610f6457813b1561155b5730841461154c57600080516020612f4083398151915254846113a36121ed565b7f6ea8b34d59b2818d3f37a08dddaa06f5886b9c20c907b36f03ca6528e106e86f6113d9604051926040845260408401906121c8565b4260208401526001600160a01b038516929081900390a36040516352d1902d60e01b8152602081600481895afa60009181611518575b506114295785634c9c8ce360e01b60005260045260246000fd5b9081600080516020612f408339815191528793036115045750833b156114ef576001600160a01b0319168117600080516020612f40833981519152557fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b600080a28251156114d4576000809161069b945190845af43d156114cc573d916114af83612174565b926114bd6040519485612151565b83523d6000602085013e612e7e565b606091612e7e565b505050346114de57005b63b398979f60e01b60005260046000fd5b50634c9c8ce360e01b60005260045260246000fd5b632a87526960e21b60005260045260246000fd5b9091506020813d602011611544575b8161153460209383612151565b8101031261029a5751908761140f565b3d9150611527565b614e8d60eb1b60005260046000fd5b632e07c4bd60e11b60005260046000fd5b600080516020612f40833981519152546001600160a01b03161415905084611354565b3461029a57602036600319011261029a576004356115ab612795565b6115cd6115b7826127e0565b9190600263ffffffff825460a01c1617906122e3565b6001600160a01b0316907fc2713e691344add4705edc62a0ee5aede95ba66ad68f97316efef9cb50644edd600080a3005b3461029a57600036600319011261029a57336000908152600080516020612f60833981519152602052604090205460ff1615806116e1575b6116d0576000805160206130008339815191525460ff8116156116bf5760ff1916600080516020613000833981519152557f5db9ee0a495bf2e6ff9c91a7834c1ba4fdd244a5e8aa4e537bd38aeae4b073aa6020604051338152a16040514281527f107553d8191d85b405879cf752997865edd48d94e20bda4dd27223c94b31a7cc60203392a2005b638dfc202b60e01b60005260046000fd5b63659a07b360e11b60005260046000fd5b506000546001600160a01b0316331415611636565b3461029a57604036600319011261029a5761029661171d611715612064565b600435612306565b604093919351938493846120dc565b3461029a57604036600319011261029a57611745612064565b336001600160a01b038216036117615761069b90600435612751565b63334bd91960e11b60005260046000fd5b3461029a57602036600319011261029a57600435604060008281526004602052209081549160018060a01b038316928315908115611837575b5061128d57336000908152600080516020612f60833981519152602052604090205460ff16158061182d575b61127257806117f363fffffff7611806935460a01c16826122e3565b80546001600160e01b031681558261269b565b7f054490ec6650e07db9b97a06ea31d44cebd44b390c9975a380055e1b4d3cac21600080a3005b50823314156117d7565b6008915060a01c1615846117ab565b3461029a57604036600319011261029a5761069b600435611865612064565b90611872610691826122c2565b612709565b3461029a57602036600319011261029a576004356118c161189782612629565b91906118ad63fffffffb825460a01c16826122e3565b805463ffffffff60c01b191681558361269b565b6001600160a01b0316907f09cb4ec44cdef8489412844b9682f87f7900758fa2bd7e039113e9125e538e0f600080a3005b3461029a57602036600319011261029a57600435604060008281526004602052209081549160018060a01b0383169283159081156119a4575b5061128d57336000908152600080516020612f60833981519152602052604090205460ff16158061199a575b611272578061124563fffffffd611973935460a01c16826122e3565b7fb390ab029eab314bd18190ebe73d7e5c1c336beaa5c83ac22578378dc9ccc405600080a3005b5082331415611957565b6002915060a01c16158461192b565b3461029a57602036600319011261029a5760206119d16004356122c2565b604051908152f35b3461029a57604036600319011261029a5761029661171d6119f8612064565b60043561224a565b3461029a57604036600319011261029a57600435611a1c6120c9565b63ffffffff600554169063ffffffff811691808310908115611ac8575b5061049b57611a4783612629565b909263ffffffff845460c01c16809260018060a01b03163314159182611abd575b5050611aac57611a98827e8a218591d4362499c38c38eb1c12e7a7cc97a9068818d1fe6046a42ba4a5ba94612210565b611aa760405192839283612233565b0390a2005b634e80a40760e11b60005260046000fd5b111590508186611a68565b90501584611a39565b3461029a57602036600319011261029a57611aea6120b6565b6000805160206130208339815191525460ff8160401c16908115611bca575b50611bb957600080516020613020833981519152805460026001600160401b0319821681179092556000546001600160481b031990911690911791906001600160a01b031633036106125763ffffffff168015610ea757604081600080516020612fa08339815191529263ffffffff196005541617600555815190600082526020820152a160008051602061302083398151915255600080516020612f00833981519152602060405160028152a1005b63f92ee8a960e01b60005260046000fd5b60026001600160401b03919091161015905082611b09565b3461029a5760a036600319011261029a57611bfb61207a565b611c03612064565b906044356001600160a01b03811680820361029a576064356001600160a01b0381169182820361029a576084356001600160a01b038116949085900361029a5760008051602061302083398151915254604081901c60ff161596906001600160401b031680159081611f80575b6001149081611f76575b159081611f6d575b50611bb95760008051602061302083398151915280546001600160401b031916600117905586611f48575b611cb5612acc565b611cbd612acc565b611cc5612acc565b611ccd612acc565b6001600160a01b038116908115610f64578315610f64578515610f64576001600160a01b038916988915610f64573b15610f5357858414611f375762aa36a746148015611f2c575b8015611f21575b1580611f0f575b610f5357611d32906000612afa565b9081611ec8575b5015611e0d57611d5790600080516020612fe0833981519152612afa565b9081611e73575b5015611e0d57611d7c90600080516020612f20833981519152612afa565b9081611e1e575b5015611e0d57600080546001600160a01b031916841781556040519390600080516020612f808339815191528180a38015610f6457803b15610f5357600380546001600160a01b031916919091179055611dd957005b600080516020613020833981519152805460ff60401b1916905560018152600080516020612f0083398151915290602090a1005b63cf5a8e3360e01b60005260046000fd5b600080516020612f20833981519152600052600080516020612ee0833981519152602052611e6c907ffff5b43478fa6840382eb63777e0da6941d320b940e3f70f0ea4780feff95827612d3a565b5084611d83565b600080516020612fe0833981519152600052600080516020612ee0833981519152602052611ec1907f32d31393d541a265984ec379cddd8b1fcbe5f2b0eb8ad81dddfead454f135eaa612d3a565b5086611d5e565b60008052600080516020612ee0833981519152602052611f08907f615f0f9e84155bea8cc509fe18befeb1baf65611e38a6ba60964480fb29dfd44612d3a565b5088611d39565b50823b1580611d235750843b15611d23565b50617a694614611d1c565b506105394614611d15565b638135316560e01b60005260046000fd5b600080516020613020833981519152805460ff60401b1916600160401b179055611cad565b90501589611c82565b303b159150611c7a565b889150611c70565b3461029a57604036600319011261029a57610296611fbc611fa7612064565b600435600052600460205260406000206123c8565b611fc581612090565b60405191818392158361209a565b3461029a57600036600319011261029a57602063ffffffff60055416604051908152f35b3461029a57602036600319011261029a576004359063ffffffff60e01b821680920361029a57602091635a05180f60e01b8114908115612039575b5015158152f35b637965db0b60e01b811491508115612053575b5083612032565b6301ffc9a760e01b1490508361204c565b602435906001600160a01b038216820361029a57565b600435906001600160a01b038216820361029a57565b600811156103b857565b9015158152604081019291906020906120b283612090565b0152565b6004359063ffffffff8216820361029a57565b6024359063ffffffff8216820361029a57565b91604091949363ffffffff916060850196151585526120fa81612090565b602085015216910152565b60a081019081106001600160401b0382111761212057604052565b634e487b7160e01b600052604160045260246000fd5b608081019081106001600160401b0382111761212057604052565b601f909101601f19168101906001600160401b0382119082101761212057604052565b6001600160401b03811161212057601f01601f191660200190565b604435906001600160401b038216820361029a57565b60005b8381106121b85750506000910152565b81810151838201526020016121a8565b906020916121e1815180928185528580860191016121a5565b601f01601f1916010190565b604051906121fc604083612151565b60058252640322e312e360dc1b6020830152565b805463ffffffff60c01b191660c09290921b63ffffffff60c01b16919091179055565b63ffffffff91821681529116602082015260400190565b600052600460205261226160406000209182612485565b61226a81612090565b806122b857505460e01c63ffffffff60055416811580156122b0575b6122a35780821061229c57505b60019160009190565b9050612293565b5050600090600390600090565b508015612286565b6000929091508290565b600052600080516020612fc083398151915260205260016040600020015490565b805463ffffffff60a01b191660a09290921b63ffffffff60a01b16919091179055565b919091600052600460205261232060406000209283612511565b9161232a83612090565b826123625763ffffffff9192505460c01c1663ffffffff60055416811580156122b0576122a35780821061229c575060019160009190565b5060009190600090565b90612383916000526004602052604060002061259d565b61238c81612090565b8061239a5750600190600090565b600091565b8054600160a01b600160e01b03191660a09290921b600160a01b600160e01b0316919091179055565b60ff600080516020613000833981519152541661247e57546001600160a01b0381169081156124765760a01c6002161561246f5760005260016020526040600020549060ff8260e01c1660038110156103b857801561246757600214612460574260a083901c6001600160401b03161115612459576001600160a01b0390811691160361245457600090565b600790565b5050600690565b5050600590565b505050600490565b5050600390565b505050600290565b5050600190565b60ff600080516020613000833981519152541661247e57546001600160a01b0381169081156124765760a01c6008161561246f5760005260016020526040600020549060ff8260e01c1660038110156103b857801561246757600214612460574260a083901c6001600160401b03161115612459576001600160a01b0390811691160361245457600090565b60ff600080516020613000833981519152541661247e57546001600160a01b0381169081156124765760a01c6004161561246f5760005260016020526040600020549060ff8260e01c1660038110156103b857801561246757600214612460574260a083901c6001600160401b03161115612459576001600160a01b0390811691160361245457600090565b60ff600080516020613000833981519152541661247e57546001600160a01b0381169081156124765760a01c6001161561246f5760005260016020526040600020549060ff8260e01c1660038110156103b857801561246757600214612460574260a083901c6001600160401b03161115612459576001600160a01b0390811691160361245457600090565b6000908152600460205260409020805490916001600160a01b03821691821590811561268c575b5061128d57336000908152600080516020612f60833981519152602052604090205460ff161580612682575b61127257565b508133141561267c565b6004915060a01c161538612650565b905460a01c63ffffffff16156126ae5750565b600052600460205260006040812055565b6000818152600080516020612fc08339815191526020908152604080832033845290915290205460ff16156126f15750565b63e2517d3f60e01b6000523360045260245260446000fd5b6127138282612afa565b918261271e57505090565b6000918252600080516020612ee0833981519152602052604090912061274d916001600160a01b031690612d3a565b5090565b61275b8282612ba3565b918261276657505090565b6000918252600080516020612ee0833981519152602052604090912061274d916001600160a01b031690612dab565b60ff60008051602061300083398151915254166127ae57565b63d93c066560e01b60005260046000fd5b519064ffffffffff8216820361029a57565b519061ffff8216820361029a57565b8015612abb57600354604051638d34875560e01b8152600481018390529190600090839060249082906001600160a01b03165afa918215612aaf57600092612904575b5060808201516001600160a01b0316913383900361127257610120015161284981612090565b61285281612090565b600381149081156128ef575b81156128db575b506128ca5781600052600160205260ff60406000205460e01c1660038110156103b8576001036128b957600090815260046020526040902080546001600160a01b0319166001600160a01b03831617815591565b632e68d1d360e21b60005260046000fd5b63e9f6db8560e01b60005260046000fd5b600791506128e881612090565b1438612865565b90506128fa81612090565b600581149061285e565b90913d8082843e6129158184612151565b820191602081840312612a8f578051906001600160401b038211612a975701906101a082840312612a8c57604051926101a084016001600160401b03811185821017612a9b5760409081528351855260208085015190860152838101519085015260608301516001600160401b038111612a9757830181601f82011215612a97578051906129a282612174565b926129b06040519485612151565b82845260208383010111612a9357906129cf91602080850191016121a5565b606084015260808201516001600160a01b0381168103612a8f5760808401526129fa60a083016127bf565b60a0840152612a0b60c083016127bf565b60c0840152612a1c60e083016127d1565b60e0840152612a2e61010083016127bf565b610100840152610120820151906008821015612a8c5750612a7f9161018091610120850152612a6061014082016127bf565b610140850152612a7361016082016127d1565b610160850152016127bf565b6101808201529038612823565b80fd5b5080fd5b8380fd5b8280fd5b634e487b7160e01b83526041600452602483fd5b6040513d6000823e3d90fd5b637bb0b62160e01b60005260046000fd5b60ff6000805160206130208339815191525460401c1615612ae957565b631afcd79f60e31b60005260046000fd5b6000818152600080516020612fc0833981519152602090815260408083206001600160a01b038616845290915290205460ff16612b9c576000818152600080516020612fc0833981519152602090815260408083206001600160a01b0395909516808452949091528120805460ff19166001179055339291907f2f8788117e7eff1d82e926ec794901d17c78024a50270940304540a733656f0d9080a4600190565b5050600090565b6000818152600080516020612fc0833981519152602090815260408083206001600160a01b038616845290915290205460ff1615612b9c576000818152600080516020612fc0833981519152602090815260408083206001600160a01b0395909516808452949091528120805460ff19169055339291907ff6391f5c32d9c69d2a47ea670b442974b53935d1edc7fd64eb21e047a839171b9080a4600190565b158015612cfb575b610f64576001600160a01b0316600090815260026020526040902054612cea576001600160401b031642811115612cd95760005460a01c6001600160401b03168015159182612cae575b5050612c9d57565b63e774e7b560e01b60005260046000fd5b909150428103908111612cc357113880612c95565b634e487b7160e01b600052601160045260246000fd5b63efa7aefd60e01b60005260046000fd5b633b53f64f60e21b60005260046000fd5b506001600160a01b03811615612c4b565b8054821015612d245760005260206000200190600090565b634e487b7160e01b600052603260045260246000fd5b6001810190826000528160205260406000205415600014612da3578054600160401b81101561212057612d8e612d77826001879401855584612d0c565b819391549060031b91821b91600019901b19161790565b90555491600052602052604060002055600190565b505050600090565b9060018201918160005282602052604060002054801515600014612e75576000198101818111612cc3578254600019810191908211612cc357818103612e3e575b50505080548015612e28576000190190612e068282612d0c565b8154906000199060031b1b191690555560005260205260006040812055600190565b634e487b7160e01b600052603160045260246000fd5b612e5e612e4e612d779386612d0c565b90549060031b1c92839286612d0c565b905560005283602052604060002055388080612dec565b50505050600090565b90612ea45750805115612e9357602081519101fd5b63d6bda27560e01b60005260046000fd5b81511580612ed6575b612eb5575090565b639996b31560e01b60009081526001600160a01b0391909116600452602490fd5b50803b15612ead56fec1f6fe24621ce81ec5827caf0253cadb74709b061630e6b55e82371705932000c7f505b2f371ae2175ee4913f4499e1f2633a7b5936321eed1cdaeb6115181d275027301df982f5215b4963a854cf9c73219df80cb6acd8f098189c9480c7bbc360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc07966f2ab141347034724e9b2a472c2f77f5cd745aeba27cacba797b8fcb708617dc5b1290f7495d7f8cc68476e6941aaf510598fa925d0d2720edc8805a846cd37bc77615afefc793aa17d75d4249a9b517ba91b0e54a49c9c4a44b3d7e228302dd7bc7dec4dceedda775e58dd541e08a116c6c53815c0bd028192f7b626800edcc084d3dcd65a1f7f23c65c46722faca6953d28e43150a467cf43e5c309238cd5ed15c6e187e77e9aee88184c21f4f2182ab5827cb3b7e07fbedcd63f03300f0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a00a164736f6c6343000822000a";
17974
18218
  var isSuperArgs2 = (xs) => xs.length > 1;
17975
18219
  var AgentDelegationRegistry__factory = class extends import_ethers15.ContractFactory {
17976
18220
  constructor(...args) {
@@ -18615,6 +18859,158 @@ var DiamondHandsSDK = class _DiamondHandsSDK {
18615
18859
  return;
18616
18860
  await this.serverSession.adoptLoginPayload(payload);
18617
18861
  }
18862
+ /**
18863
+ * Build the Safe-delegation ceremony for a multi-sig Safe (Phase 1 of
18864
+ * multi-sig support): asks lit-ops-server to mint (or recover) the Safe's
18865
+ * agent PKP and returns the exact call batch the Safe's owners must execute
18866
+ * (AgentModuleFactory.deploy → safe.enableModule → registerAgent), derived
18867
+ * from live chain state so a half-done ceremony resumes.
18868
+ *
18869
+ * Service-mode only — the server holds the Chipotle account that mints the
18870
+ * PKP and it gates the route to current Safe owners. `knownAgentAddress` is
18871
+ * the caller-persisted agent from an earlier prepare whose ceremony has not
18872
+ * landed yet; echoing it back prevents a duplicate mint after a server
18873
+ * restart.
18874
+ */
18875
+ async prepareSafeAgentModule(request) {
18876
+ if (this.config.mode !== "service") {
18877
+ throw new Error(
18878
+ "prepareSafeAgentModule requires service mode \u2014 the agent PKP is minted by lit-ops-server's Chipotle account and cannot be minted standalone"
18879
+ );
18880
+ }
18881
+ const PREPARE_CLIENT_TIMEOUT_MS = 3e4;
18882
+ const abort = new AbortController();
18883
+ const abortTimer = setTimeout(() => abort.abort(), PREPARE_CLIENT_TIMEOUT_MS);
18884
+ let resp;
18885
+ try {
18886
+ resp = await fetch(
18887
+ `${this.config.serviceEndpoint}/api/lit/safe/agent-module/prepare`,
18888
+ {
18889
+ method: "POST",
18890
+ headers: {
18891
+ "Content-Type": "application/json",
18892
+ ...await this.getAuthHeader()
18893
+ },
18894
+ signal: abort.signal,
18895
+ body: JSON.stringify({
18896
+ safeAddress: request.safeAddress,
18897
+ ...request.knownAgentAddress ? { knownAgentAddress: request.knownAgentAddress } : {}
18898
+ })
18899
+ }
18900
+ );
18901
+ } catch (fetchErr) {
18902
+ throw abort.signal.aborted ? new Error(
18903
+ `prepareSafeAgentModule timed out after ${PREPARE_CLIENT_TIMEOUT_MS}ms \u2014 the delegation server did not answer`
18904
+ ) : fetchErr;
18905
+ } finally {
18906
+ clearTimeout(abortTimer);
18907
+ }
18908
+ const data = await resp.json().catch(() => null);
18909
+ if (!resp.ok || !data?.success) {
18910
+ throw new Error(
18911
+ `prepareSafeAgentModule failed (${resp.status}): ${data?.error ?? "no error detail from server"}`
18912
+ );
18913
+ }
18914
+ return {
18915
+ status: data.status,
18916
+ agentAddress: data.agentAddress,
18917
+ moduleAddress: data.moduleAddress,
18918
+ validUntil: data.validUntil,
18919
+ calls: data.calls
18920
+ };
18921
+ }
18922
+ /**
18923
+ * Shared transport for `/agent-module/{authorize,execute}`.
18924
+ *
18925
+ * Both routes run the agent PKP's key inside a Lit Action, gated on the
18926
+ * caller's session being a CURRENT Safe owner (M-8c). Service-mode only, for
18927
+ * the same reason as {@link prepareSafeAgentModule}: the key lives in the
18928
+ * server's Chipotle account and has no standalone equivalent.
18929
+ */
18930
+ async postAgentModuleRoute(mode, body) {
18931
+ if (this.config.mode !== "service") {
18932
+ throw new Error(
18933
+ `agentModule${mode === "authorize" ? "Authorize" : "Execute"} requires service mode \u2014 the agent PKP is held by lit-ops-server's Chipotle account`
18934
+ );
18935
+ }
18936
+ const AGENT_MODULE_CLIENT_TIMEOUT_MS = 45e3;
18937
+ const abort = new AbortController();
18938
+ const abortTimer = setTimeout(
18939
+ () => abort.abort(),
18940
+ AGENT_MODULE_CLIENT_TIMEOUT_MS
18941
+ );
18942
+ let resp;
18943
+ try {
18944
+ resp = await fetch(
18945
+ `${this.config.serviceEndpoint}/api/lit/agent-module/${mode}`,
18946
+ {
18947
+ method: "POST",
18948
+ headers: {
18949
+ "Content-Type": "application/json",
18950
+ ...await this.getAuthHeader()
18951
+ },
18952
+ signal: abort.signal,
18953
+ body: JSON.stringify(body)
18954
+ }
18955
+ );
18956
+ } catch (fetchErr) {
18957
+ throw abort.signal.aborted ? new Error(
18958
+ `agent-module ${mode} timed out after ${AGENT_MODULE_CLIENT_TIMEOUT_MS}ms \u2014 the delegation server did not answer`
18959
+ ) : fetchErr;
18960
+ } finally {
18961
+ clearTimeout(abortTimer);
18962
+ }
18963
+ const data = await resp.json().catch(() => null);
18964
+ if (!resp.ok || !data?.success) {
18965
+ throw new Error(
18966
+ `agent-module ${mode} failed (${resp.status}): ${data?.error ?? "no error detail from server"}`
18967
+ );
18968
+ }
18969
+ return data;
18970
+ }
18971
+ /**
18972
+ * The agent signs the borrower authorization envelope for `op` on `positionId`.
18973
+ *
18974
+ * The returned `{ timestamp, signature }` feeds the SDK's PRECOMPUTED
18975
+ * authorization path (`generateExtendAuthorization` and siblings) — the same
18976
+ * path Safe/EIP-1271 borrowers already use. It is NOT a generic message
18977
+ * signer: the Lit Action REBUILDS the envelope from these fields rather than
18978
+ * signing anything the caller supplies, which is what stops a delegated agent
18979
+ * key being turned into an arbitrary oracle.
18980
+ *
18981
+ * `timestamp` must be quantum-aligned by the caller
18982
+ * (`calculateNextQuantumTimestamp()`) and must be the SAME value later passed
18983
+ * to the protocol call, or the validator recovers a different signer.
18984
+ */
18985
+ async agentModuleAuthorize(request) {
18986
+ return this.postAgentModuleRoute("authorize", {
18987
+ safeAddress: request.safeAddress,
18988
+ positionId: request.positionId,
18989
+ op: request.op,
18990
+ timestamp: request.timestamp,
18991
+ ...request.amount !== void 0 ? { amount: request.amount } : {},
18992
+ ...request.selectedTerm !== void 0 ? { selectedTerm: request.selectedTerm } : {}
18993
+ });
18994
+ }
18995
+ /**
18996
+ * The agent signs `module.execute(target, innerData)` as a raw EIP-1559
18997
+ * transaction. Returns the SIGNED transaction — the caller broadcasts it, so
18998
+ * the tx hash and receipt come from the caller's own provider.
18999
+ *
19000
+ * `tx` fields are caller-supplied because the Lit Action runs on many nodes
19001
+ * and reconciles results: anything fetched in-action (nonce, gas) would
19002
+ * differ per node and fail consensus.
19003
+ */
19004
+ async agentModuleExecute(request) {
19005
+ return this.postAgentModuleRoute("execute", {
19006
+ safeAddress: request.safeAddress,
19007
+ positionId: request.positionId,
19008
+ op: request.op,
19009
+ target: request.target,
19010
+ innerData: request.innerData,
19011
+ tx: request.tx
19012
+ });
19013
+ }
18618
19014
  /**
18619
19015
  * Audit H-9: invalidate the LoanQuery cache so subsequent reads return
18620
19016
  * the post-write state. We clear the entire loan-query cache (not just
@@ -19644,7 +20040,12 @@ var DiamondHandsSDK = class _DiamondHandsSDK {
19644
20040
  ...this.config.contractAddresses?.btcSpendAuthorizer ? { BTCSpendAuthorizer: this.config.contractAddresses.btcSpendAuthorizer } : {},
19645
20041
  ...this.config.contractAddresses?.bitcoinProviderRegistry ? { BitcoinProviderRegistry: this.config.contractAddresses.bitcoinProviderRegistry } : {},
19646
20042
  // Required for Safe-as-borrower auth verification (third arm).
19647
- ...this.config.contractAddresses?.positionDelegateRegistry ? { PositionDelegateRegistry: this.config.contractAddresses.positionDelegateRegistry } : {}
20043
+ ...this.config.contractAddresses?.positionDelegateRegistry ? { PositionDelegateRegistry: this.config.contractAddresses.positionDelegateRegistry } : {},
20044
+ // Required for the Safe-agent scoped mint grant (canMint) — without
20045
+ // this the LIT Action's agent-grant arm is skipped entirely and a
20046
+ // delegated agent signer falls through to the borrower/EIP-1271
20047
+ // arms, which reject it.
20048
+ ...this.config.contractAddresses?.agentDelegationRegistry ? { AgentDelegationRegistry: this.config.contractAddresses.agentDelegationRegistry } : {}
19648
20049
  };
19649
20050
  if (this.config.debug) {
19650
20051
  log.info(" Contract addresses passed to LIT Action:", {});
@@ -21301,7 +21702,12 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
21301
21702
  // Required for the LIT Action verifier's third auth arm in
21302
21703
  // Safe-as-borrower flows. See diamond-hands-sdk.ts:5786 for the same
21303
21704
  // pattern in the withdraw flow.
21304
- PositionDelegateRegistry: this.config.contractAddresses?.positionDelegateRegistry || ""
21705
+ PositionDelegateRegistry: this.config.contractAddresses?.positionDelegateRegistry || "",
21706
+ // Required for the Safe-agent scoped renew grant (canRenew) — without
21707
+ // this the LIT Action's agent-grant arm is skipped entirely and a
21708
+ // delegated agent signer falls through to the borrower/EIP-1271 arms,
21709
+ // which reject it.
21710
+ AgentDelegationRegistry: this.config.contractAddresses?.agentDelegationRegistry || ""
21305
21711
  };
21306
21712
  if (!contracts.PositionManager) {
21307
21713
  throw new Error("PositionManager address is required");
@@ -21971,10 +22377,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
21971
22377
  isRetryablePaymentQuantumFailure(result) {
21972
22378
  if (result.success || !result.error)
21973
22379
  return false;
21974
- const e = result.error;
21975
- if (e.includes("Transaction timeout"))
21976
- return false;
21977
- return e.includes("DeadZoneViolation") || e.includes("QuantumOutsideWindow") || e.includes("QuantumAlreadyUsed") || e.includes("QuantumExpired") || e === "Transaction reverted";
22380
+ return isRetryableQuantumFailure(result.error);
21978
22381
  }
21979
22382
  /**
21980
22383
  * One payment attempt: user auth → Lit Action authorization → dead-zone gate →
@@ -22038,7 +22441,12 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
22038
22441
  BTCSpendAuthorizer: this.config.contractAddresses?.btcSpendAuthorizer || "",
22039
22442
  BitcoinProviderRegistry: this.config.contractAddresses?.bitcoinProviderRegistry || "",
22040
22443
  // Required for Safe-as-borrower auth verification (third arm).
22041
- PositionDelegateRegistry: this.config.contractAddresses?.positionDelegateRegistry || ""
22444
+ PositionDelegateRegistry: this.config.contractAddresses?.positionDelegateRegistry || "",
22445
+ // Required for the Safe-agent scoped repay grant (canRepay) —
22446
+ // without this the LIT Action's agent-grant arm is skipped entirely
22447
+ // and a delegated agent signer falls through to the
22448
+ // borrower/EIP-1271 arms, which reject it.
22449
+ AgentDelegationRegistry: this.config.contractAddresses?.agentDelegationRegistry || ""
22042
22450
  };
22043
22451
  if (this.config.debug) {
22044
22452
  log.info(" Contract addresses:", {});
@@ -22608,6 +23016,37 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
22608
23016
  }
22609
23017
  return this.withdrawalAddressModule.getEntries(target);
22610
23018
  }
23019
+ /**
23020
+ * Recover the revert reason of an already-mined, already-reverted transaction.
23021
+ *
23022
+ * A status-0 receipt carries no revert data, and ethers does not re-fetch it, so
23023
+ * the reason has to be recovered by replaying the exact calldata as an `eth_call`
23024
+ * pinned to the block that mined it — the only block whose `block.timestamp`
23025
+ * reproduces a timing-dependent revert such as `DeadZoneViolation()`.
23026
+ *
23027
+ * Caveat: the replay runs against end-of-block state rather than the state at the
23028
+ * transaction's own index, so for a state-dependent revert the recovered name is
23029
+ * diagnostic, not proof. Timing reverts (the case this exists for) are unaffected —
23030
+ * they depend only on the block timestamp.
23031
+ *
23032
+ * @returns the decoded error name, its raw selector if unknown, or `null` when the
23033
+ * replay yields no revert data at all (never throws — a failed diagnosis
23034
+ * must not mask the underlying failure).
23035
+ */
23036
+ async decodeMinedRevert(params) {
23037
+ try {
23038
+ await this.getProviderOrThrow().call({
23039
+ to: params.to,
23040
+ from: params.from,
23041
+ data: params.data,
23042
+ blockTag: params.blockNumber
23043
+ });
23044
+ return null;
23045
+ } catch (replayError) {
23046
+ const decoded = decodeQuantumRevert(replayError);
23047
+ return decoded.name ?? decoded.selector ?? null;
23048
+ }
23049
+ }
22611
23050
  /**
22612
23051
  * Withdraw Bitcoin from a position
22613
23052
  *
@@ -23181,9 +23620,23 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
23181
23620
  txHash: tx.hash
23182
23621
  });
23183
23622
  }
23623
+ const withdrawProvider = this.getProviderOrThrow();
23624
+ const withdrawTimeoutMs = this.config.transactionTimeoutMs || 3e5;
23184
23625
  let receipt;
23185
23626
  try {
23186
- receipt = await tx.wait();
23627
+ receipt = await Promise.race([
23628
+ withdrawProvider.waitForTransaction(tx.hash, 1),
23629
+ new Promise(
23630
+ (_, reject) => setTimeout(
23631
+ () => reject(
23632
+ new Error(
23633
+ `Transaction timeout after ${withdrawTimeoutMs}ms. Hash: ${tx.hash}`
23634
+ )
23635
+ ),
23636
+ withdrawTimeoutMs
23637
+ )
23638
+ )
23639
+ ]);
23187
23640
  } catch (waitError) {
23188
23641
  const waitErrorMsg = waitError instanceof Error ? waitError.message : String(waitError);
23189
23642
  if (this.config.debug) {
@@ -23200,7 +23653,26 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
23200
23653
  if (!receipt) {
23201
23654
  return {
23202
23655
  success: false,
23203
- error: `Transaction confirmation returned no receipt. Hash: ${tx.hash}`
23656
+ error: `Transaction receipt not found for hash: ${tx.hash}`
23657
+ };
23658
+ }
23659
+ if (receipt.status === 0) {
23660
+ const revertName = await this.decodeMinedRevert({
23661
+ to: positionManagerAddress,
23662
+ from: await withdrawSigner.getAddress(),
23663
+ data: withdrawCalldata,
23664
+ blockNumber: receipt.blockNumber
23665
+ });
23666
+ if (this.config.debug) {
23667
+ log.error("\u274C withdrawBTC reverted on-chain", {
23668
+ txHash: receipt.hash,
23669
+ blockNumber: receipt.blockNumber,
23670
+ revert: revertName ?? "undecodable"
23671
+ });
23672
+ }
23673
+ return {
23674
+ success: false,
23675
+ error: `Transaction reverted: ${revertName ?? "unknown reason"} (tx: ${receipt.hash})`
23204
23676
  };
23205
23677
  }
23206
23678
  if (this.config.debug) {
@@ -23306,6 +23778,11 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
23306
23778
  * The service path's post-broadcast CRIT-2 re-verification exists to catch
23307
23779
  * a LYING SERVER; here the broadcast happens locally from the signatures we
23308
23780
  * just obtained, so the esplora-returned txid is already first-hand.
23781
+ *
23782
+ * Multi-UTXO consolidation in standalone mode requires the caller to supply
23783
+ * `request.utxos` explicitly — `fetchConfirmedVaultUtxos` is service-mode
23784
+ * only, so there is no automatic gather here; without the set, a multi-UTXO
23785
+ * vault fails in the signer with its "Insufficient UTXO value" error.
23309
23786
  */
23310
23787
  async executeBTCWithdrawalStandalonePhase2(request, envelope, txid, vout, chainId) {
23311
23788
  const positionDetails = await this.getPositionDetailsView(
@@ -23512,6 +23989,35 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
23512
23989
  const network = await this.getProviderOrThrow().getNetwork();
23513
23990
  const chainId = Number(network.chainId);
23514
23991
  const signer = this.getSignerOrThrow();
23992
+ let executionUtxos = request.utxos;
23993
+ if (!executionUtxos || executionUtxos.length === 0) {
23994
+ const gathered = await this.fetchConfirmedVaultUtxos(request.positionId);
23995
+ if (gathered.length > 0) {
23996
+ const normTxid = (t) => String(t).toLowerCase().replace(/^0x/, "");
23997
+ const byOutpoint = /* @__PURE__ */ new Map();
23998
+ for (const u of gathered) {
23999
+ byOutpoint.set(`${normTxid(u.txid)}:${u.vout}`, {
24000
+ txid: normTxid(u.txid),
24001
+ vout: Number(u.vout),
24002
+ value: Number(u.value)
24003
+ });
24004
+ }
24005
+ if (!byOutpoint.has(`${normTxid(txid)}:${vout}`)) {
24006
+ return {
24007
+ success: false,
24008
+ 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`
24009
+ };
24010
+ }
24011
+ if (byOutpoint.size > 1) {
24012
+ executionUtxos = Array.from(byOutpoint.values());
24013
+ if (this.config.debug) {
24014
+ log.info("\u{1F9E9} Multi-UTXO withdrawal: gathered full input set", {
24015
+ inputs: executionUtxos.length
24016
+ });
24017
+ }
24018
+ }
24019
+ }
24020
+ }
23515
24021
  const envelope = await buildBtcExecuteEnvelope({
23516
24022
  positionId: request.positionId,
23517
24023
  txid,
@@ -23522,7 +24028,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
23522
24028
  networkFee: request.networkFee,
23523
24029
  chainId,
23524
24030
  signer,
23525
- ...request.utxos ? { utxos: request.utxos } : {}
24031
+ ...executionUtxos ? { utxos: executionUtxos } : {}
23526
24032
  });
23527
24033
  if (this.config.mode !== "service") {
23528
24034
  return await this.executeBTCWithdrawalStandalonePhase2(
@@ -24294,13 +24800,11 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
24294
24800
  if (authResult.success) {
24295
24801
  break;
24296
24802
  }
24297
- const isQuantumError = authResult.error?.includes(
24298
- "QuantumOutsideWindow"
24299
- );
24803
+ const isQuantumError = isRetryableQuantumFailure(authResult.error);
24300
24804
  if (isQuantumError && wAttempt < MAX_WITHDRAWAL_QUANTUM_RETRIES) {
24301
24805
  if (this.config.debug) {
24302
24806
  log.warn(
24303
- `\u26A0\uFE0F Phase 1 QuantumOutsideWindow (attempt ${wAttempt}/${MAX_WITHDRAWAL_QUANTUM_RETRIES}). LIT action took too long. Retrying with fresh quantum...`
24807
+ `\u26A0\uFE0F Phase 1 quantum-timing failure (attempt ${wAttempt}/${MAX_WITHDRAWAL_QUANTUM_RETRIES}): ${authResult.error}. Retrying with a fresh quantum...`
24304
24808
  );
24305
24809
  }
24306
24810
  await new Promise((resolve) => setTimeout(resolve, 2e3));
@@ -24737,6 +25241,49 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
24737
25241
  }
24738
25242
  return addr;
24739
25243
  }
25244
+ /**
25245
+ * `LoanGrant` fields that EVERY deployed registry version returns.
25246
+ *
25247
+ * Deliberately omits `minWithdrawRatioBps` — see {@link readLoanGrant}.
25248
+ */
25249
+ static LOAN_GRANT_PREFIX_IFACE = new import_ethers17.Interface([
25250
+ "function getLoanGrant(bytes32 positionId) view returns (address borrower, uint32 scopeBits, uint32 minCollateralRatioBps)"
25251
+ ]);
25252
+ /**
25253
+ * Read a position's grant, decoding only the struct prefix that every deployed registry
25254
+ * version shares.
25255
+ *
25256
+ * `LoanGrant` gained a fourth member (`minWithdrawRatioBps`) in registry v2.1.0, and the
25257
+ * typechain bindings are generated from that source — but Sepolia (`0x6AE7fc6b…`) and mainnet
25258
+ * (`0x54853b7E…`) both run v2.0.0, whose `getLoanGrant` returns three words. Decoding that
25259
+ * with the generated four-field decoder fails `BAD_DATA` on every live chain, which is what
25260
+ * took the Agents tab down. The first three members are identical in both layouts, and ethers
25261
+ * ignores the trailing static word on a v2.1.0 registry, so this decoder is correct against
25262
+ * both. Nothing in this SDK reads `minWithdrawRatioBps`: WITHDRAW is not a scope it grants.
25263
+ */
25264
+ async readLoanGrant(registryAddress, pid) {
25265
+ const iface = _DiamondHandsSDK.LOAN_GRANT_PREFIX_IFACE;
25266
+ const data = await this.getProviderOrThrow().call({
25267
+ to: registryAddress,
25268
+ data: iface.encodeFunctionData("getLoanGrant", [pid])
25269
+ });
25270
+ if ((0, import_ethers17.getBytes)(data).length < 96) {
25271
+ throw new SDKError({
25272
+ message: `AgentDelegationRegistry at ${registryAddress} returned ${(0, import_ethers17.getBytes)(data).length} bytes for getLoanGrant(bytes32) \u2014 expected at least 96. Wrong address, or a registry version this SDK does not support.`,
25273
+ category: "CONTRACT" /* CONTRACT */,
25274
+ severity: "HIGH" /* HIGH */
25275
+ });
25276
+ }
25277
+ const [borrower, scopeBits, minCollateralRatioBps] = iface.decodeFunctionResult(
25278
+ "getLoanGrant",
25279
+ data
25280
+ );
25281
+ return {
25282
+ borrower,
25283
+ scopeBits: Number(scopeBits),
25284
+ minCollateralRatioBps: Number(minCollateralRatioBps)
25285
+ };
25286
+ }
24740
25287
  /**
24741
25288
  * Read a position's auto-renew delegation state (used to drive the Enable/Disable toggle).
24742
25289
  * Pure view — no signer required.
@@ -24745,8 +25292,8 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
24745
25292
  const addr = this.agentDelegationRegistryOrThrow();
24746
25293
  const registry = AgentDelegationRegistry__factory.connect(addr, this.getProviderOrThrow());
24747
25294
  const pid = this.toBytes32(positionId);
24748
- const grant = await registry.getLoanGrant(pid);
24749
- const enabled = grant.borrower !== "0x0000000000000000000000000000000000000000" && (Number(grant.scopeBits) & 1) !== 0;
25295
+ const grant = await this.readLoanGrant(addr, pid);
25296
+ const enabled = grant.borrower !== "0x0000000000000000000000000000000000000000" && (grant.scopeBits & 1) !== 0;
24750
25297
  const agentActive = user ? await registry.isAgentActive(user) : null;
24751
25298
  const canRenew = user ? await registry.canRenew(pid, user).then((r) => ({ ok: r.ok, reason: Number(r.reason) })) : null;
24752
25299
  return {
@@ -24757,16 +25304,58 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
24757
25304
  };
24758
25305
  }
24759
25306
  /**
24760
- * Enable auto-renew delegation for a position. Orchestrates the first-time setup: if the
24761
- * borrower has no active agent, mints a fresh per-user agent PKP (via lit-ops-server) and
24762
- * registers it, then records the position's renew grant. Borrower-signed.
25307
+ * Read every delegation scope a position has granted, plus the mint floor and the protocol
25308
+ * minimum the UI must clamp its editor to. Pure view no signer required.
25309
+ *
25310
+ * Superset of `getAutoRenewStatus`, which predates the multi-scope UI and is kept for
25311
+ * callers that only care about renew.
24763
25312
  */
24764
- async enableAutoRenew(positionId, options) {
25313
+ async getDelegationStatus(positionId, user) {
25314
+ const addr = this.agentDelegationRegistryOrThrow();
25315
+ const registry = AgentDelegationRegistry__factory.connect(addr, this.getProviderOrThrow());
25316
+ const pid = this.toBytes32(positionId);
25317
+ const [grant, protocolMin] = await Promise.all([
25318
+ this.readLoanGrant(addr, pid),
25319
+ registry.agentMintFloorMinBps().then(Number)
25320
+ ]);
25321
+ const hasGrant = grant.borrower !== "0x0000000000000000000000000000000000000000";
25322
+ const scopes = grant.scopeBits;
25323
+ const mintEnabled = hasGrant && (scopes & 4) !== 0;
25324
+ const floor = grant.minCollateralRatioBps;
25325
+ const agent = user ? await registry.agentOf(user) : null;
25326
+ return {
25327
+ renewEnabled: hasGrant && (scopes & 1) !== 0,
25328
+ repayEnabled: hasGrant && (scopes & 2) !== 0,
25329
+ mintEnabled,
25330
+ minCollateralRatioBps: mintEnabled ? floor : 0,
25331
+ effectiveMintFloorBps: mintEnabled ? Math.max(floor, protocolMin) : 0,
25332
+ protocolMinFloorBps: protocolMin,
25333
+ borrower: grant.borrower,
25334
+ agentActive: user ? await registry.isAgentActive(user) : null,
25335
+ agentValidUntil: agent ? Number(agent.validUntil) : null
25336
+ };
25337
+ }
25338
+ /** Governance floor-of-floors for agent mints (bps). The UI's minimum for the floor editor. */
25339
+ async getAgentMintFloorMinBps() {
25340
+ const addr = this.agentDelegationRegistryOrThrow();
25341
+ const registry = AgentDelegationRegistry__factory.connect(addr, this.getProviderOrThrow());
25342
+ return Number(await registry.agentMintFloorMinBps());
25343
+ }
25344
+ /**
25345
+ * Shared enable-path preamble for EVERY scope: resolve the registry, ensure the borrower has
25346
+ * an active agent (minting + registering a per-user PKP on first use), and make that agent the
25347
+ * position's Gate-1 delegate. Returns the signer-connected registry and the agent address.
25348
+ *
25349
+ * Every `enableAuto*` runs this: the registry reverts `NoActiveAgent` without an agent, and
25350
+ * `lit-ops-server.isPositionDelegate` 403s the agent on the execute routes without Gate 1 —
25351
+ * so a scope enabled without this preamble would look on but never execute.
25352
+ */
25353
+ async prepareAgentDelegation(pid, options) {
24765
25354
  const addr = this.agentDelegationRegistryOrThrow();
24766
25355
  const pdrAddr = this.config.contractAddresses?.positionDelegateRegistry;
24767
25356
  if (!pdrAddr) {
24768
25357
  throw new SDKError({
24769
- message: "PositionDelegateRegistry address not configured \u2014 required to authorize the renewal agent (contractAddresses.positionDelegateRegistry)",
25358
+ message: "PositionDelegateRegistry address not configured \u2014 required to authorize the agent (contractAddresses.positionDelegateRegistry)",
24770
25359
  category: "CONFIGURATION" /* CONFIGURATION */,
24771
25360
  severity: "HIGH" /* HIGH */
24772
25361
  });
@@ -24774,7 +25363,6 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
24774
25363
  const signer = this.getSignerOrThrow();
24775
25364
  const user = await signer.getAddress();
24776
25365
  const registry = AgentDelegationRegistry__factory.connect(addr, signer);
24777
- const pid = this.toBytes32(positionId);
24778
25366
  let agentAddress;
24779
25367
  const hasActiveAgent = await registry.isAgentActive(user);
24780
25368
  if (hasActiveAgent) {
@@ -24796,6 +25384,54 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
24796
25384
  const delTx = await setPositionDelegate(pid, agentAddress, signer, pdrAddr);
24797
25385
  await delTx.wait();
24798
25386
  }
25387
+ return { registry, agentAddress };
25388
+ }
25389
+ /**
25390
+ * Shared disable-path epilogue: drop the position's Gate-1 delegate, but ONLY once no scope
25391
+ * remains. Scopes compose, so clearing Gate 1 on every disable would silently break the
25392
+ * surviving ones server-side (the agent would 403 on the execute routes while its remaining
25393
+ * grant still reads as enabled on-chain).
25394
+ *
25395
+ * Also refuses to clobber a delegate registered for anything other than this user's agent.
25396
+ */
25397
+ async clearGate1IfNoScopesRemain(pid) {
25398
+ const addr = this.agentDelegationRegistryOrThrow();
25399
+ const pdrAddr = this.config.contractAddresses?.positionDelegateRegistry;
25400
+ if (!pdrAddr) {
25401
+ throw new SDKError({
25402
+ message: "PositionDelegateRegistry address not configured \u2014 required to clear the agent's Gate-1 authorization (contractAddresses.positionDelegateRegistry)",
25403
+ category: "CONFIGURATION" /* CONFIGURATION */,
25404
+ severity: "HIGH" /* HIGH */
25405
+ });
25406
+ }
25407
+ const signer = this.getSignerOrThrow();
25408
+ const user = await signer.getAddress();
25409
+ const registry = AgentDelegationRegistry__factory.connect(addr, signer);
25410
+ const grant = await this.readLoanGrant(addr, pid);
25411
+ if (grant.scopeBits !== 0)
25412
+ return;
25413
+ const [currentDelegate, userAgent] = await Promise.all([
25414
+ getPositionDelegate(pid, signer, pdrAddr),
25415
+ registry.agentOf(user).then((a) => a.agent)
25416
+ ]);
25417
+ if (currentDelegate !== import_ethers17.ZeroAddress && currentDelegate.toLowerCase() === userAgent.toLowerCase()) {
25418
+ const clearTx = await setPositionDelegate(
25419
+ pid,
25420
+ "0x0000000000000000000000000000000000000000",
25421
+ signer,
25422
+ pdrAddr
25423
+ );
25424
+ await clearTx.wait();
25425
+ }
25426
+ }
25427
+ /**
25428
+ * Enable auto-renew delegation for a position. Orchestrates the first-time setup: if the
25429
+ * borrower has no active agent, mints a fresh per-user agent PKP (via lit-ops-server) and
25430
+ * registers it, then records the position's renew grant. Borrower-signed.
25431
+ */
25432
+ async enableAutoRenew(positionId, options) {
25433
+ const pid = this.toBytes32(positionId);
25434
+ const { registry, agentAddress } = await this.prepareAgentDelegation(pid, options);
24799
25435
  const tx = await registry.enableAutoRenew(pid);
24800
25436
  const receipt = await tx.wait();
24801
25437
  return { hash: tx.hash, blockNumber: receipt?.blockNumber ?? 0, agentAddress };
@@ -24804,35 +25440,102 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
24804
25440
  async disableAutoRenew(positionId) {
24805
25441
  const addr = this.agentDelegationRegistryOrThrow();
24806
25442
  const signer = this.getSignerOrThrow();
24807
- const user = await signer.getAddress();
24808
25443
  const registry = AgentDelegationRegistry__factory.connect(addr, signer);
24809
25444
  const pid = this.toBytes32(positionId);
24810
- const pdrAddr = this.config.contractAddresses?.positionDelegateRegistry;
24811
- if (!pdrAddr) {
25445
+ const tx = await registry.disableAutoRenew(pid);
25446
+ const receipt = await tx.wait();
25447
+ await this.clearGate1IfNoScopesRemain(pid);
25448
+ return { hash: tx.hash, blockNumber: receipt?.blockNumber ?? 0 };
25449
+ }
25450
+ /**
25451
+ * Enable auto-repay delegation: the agent may repay this position's debt on the borrower's
25452
+ * behalf. Deliberately uncapped in the registry — repay only ever burns the borrower's own
25453
+ * debt from the borrower's own funds, and the ERC-20 allowance to PositionManager (never
25454
+ * granted to the agent) is the natural bound. Borrower-signed.
25455
+ */
25456
+ async enableAutoRepay(positionId, options) {
25457
+ const pid = this.toBytes32(positionId);
25458
+ const { registry, agentAddress } = await this.prepareAgentDelegation(pid, options);
25459
+ const tx = await registry.enableAutoRepay(pid);
25460
+ const receipt = await tx.wait();
25461
+ return { hash: tx.hash, blockNumber: receipt?.blockNumber ?? 0, agentAddress };
25462
+ }
25463
+ /** Disable auto-repay delegation for a position. Borrower-signed. */
25464
+ async disableAutoRepay(positionId) {
25465
+ const addr = this.agentDelegationRegistryOrThrow();
25466
+ const signer = this.getSignerOrThrow();
25467
+ const registry = AgentDelegationRegistry__factory.connect(addr, signer);
25468
+ const pid = this.toBytes32(positionId);
25469
+ const tx = await registry.disableAutoRepay(pid);
25470
+ const receipt = await tx.wait();
25471
+ await this.clearGate1IfNoScopesRemain(pid);
25472
+ return { hash: tx.hash, blockNumber: receipt?.blockNumber ?? 0 };
25473
+ }
25474
+ /**
25475
+ * Enable auto-mint delegation with a post-mint collateral-ratio floor (bps) the mint validator
25476
+ * enforces. `minCollateralRatioBps` must be at or above the governance minimum
25477
+ * (`getAgentMintFloorMinBps()`, 15000 = 150%) — checked here so the caller gets a readable
25478
+ * error instead of a `FloorBelowProtocolMin` revert after signing. Borrower-signed.
25479
+ */
25480
+ async enableAutoMint(positionId, minCollateralRatioBps, options) {
25481
+ const pid = this.toBytes32(positionId);
25482
+ await this.assertMintFloorAtOrAboveProtocolMin(minCollateralRatioBps);
25483
+ const { registry, agentAddress } = await this.prepareAgentDelegation(pid, options);
25484
+ const tx = await registry.enableAutoMint(pid, minCollateralRatioBps);
25485
+ const receipt = await tx.wait();
25486
+ return { hash: tx.hash, blockNumber: receipt?.blockNumber ?? 0, agentAddress };
25487
+ }
25488
+ /** Disable auto-mint delegation for a position (also zeroes its mint floor). Borrower-signed. */
25489
+ async disableAutoMint(positionId) {
25490
+ const addr = this.agentDelegationRegistryOrThrow();
25491
+ const signer = this.getSignerOrThrow();
25492
+ const registry = AgentDelegationRegistry__factory.connect(addr, signer);
25493
+ const pid = this.toBytes32(positionId);
25494
+ const tx = await registry.disableAutoMint(pid);
25495
+ const receipt = await tx.wait();
25496
+ await this.clearGate1IfNoScopesRemain(pid);
25497
+ return { hash: tx.hash, blockNumber: receipt?.blockNumber ?? 0 };
25498
+ }
25499
+ /**
25500
+ * Change an existing mint grant's collateral floor in place — no disable/re-enable dance.
25501
+ * MINT must already be granted; the new floor must clear the governance minimum. Note the
25502
+ * borrower may move it in either direction (above the minimum); only `REGISTRAR_ROLE` is
25503
+ * restricted to raising. Borrower-signed.
25504
+ */
25505
+ async setMintCollateralFloor(positionId, newFloorBps) {
25506
+ const addr = this.agentDelegationRegistryOrThrow();
25507
+ await this.assertMintFloorAtOrAboveProtocolMin(newFloorBps);
25508
+ const signer = this.getSignerOrThrow();
25509
+ const registry = AgentDelegationRegistry__factory.connect(addr, signer);
25510
+ const pid = this.toBytes32(positionId);
25511
+ const tx = await registry.setMintCollateralFloor(pid, newFloorBps);
25512
+ const receipt = await tx.wait();
25513
+ return { hash: tx.hash, blockNumber: receipt?.blockNumber ?? 0 };
25514
+ }
25515
+ /** Pre-flight the registry's `FloorBelowProtocolMin` guard with a message a user can act on. */
25516
+ async assertMintFloorAtOrAboveProtocolMin(floorBps) {
25517
+ if (!Number.isInteger(floorBps) || floorBps <= 0) {
24812
25518
  throw new SDKError({
24813
- message: "PositionDelegateRegistry address not configured \u2014 required to clear the renewal agent's Gate-1 authorization (contractAddresses.positionDelegateRegistry)",
25519
+ message: `Invalid collateral floor: ${floorBps} bps \u2014 must be a positive integer`,
25520
+ category: "VALIDATION" /* VALIDATION */,
25521
+ severity: "MEDIUM" /* MEDIUM */
25522
+ });
25523
+ }
25524
+ const protocolMin = await this.getAgentMintFloorMinBps();
25525
+ if (protocolMin === 0) {
25526
+ throw new SDKError({
25527
+ message: "Agent minting is not configured on this network (agentMintFloorMinBps is 0) \u2014 the registry would reject every mint grant",
24814
25528
  category: "CONFIGURATION" /* CONFIGURATION */,
24815
25529
  severity: "HIGH" /* HIGH */
24816
25530
  });
24817
25531
  }
24818
- {
24819
- const [currentDelegate, userAgent] = await Promise.all([
24820
- getPositionDelegate(pid, signer, pdrAddr).catch(() => null),
24821
- registry.agentOf(user).then((a) => a.agent).catch(() => null)
24822
- ]);
24823
- if (currentDelegate && userAgent && currentDelegate.toLowerCase() === userAgent.toLowerCase()) {
24824
- const clearTx = await setPositionDelegate(
24825
- pid,
24826
- "0x0000000000000000000000000000000000000000",
24827
- signer,
24828
- pdrAddr
24829
- );
24830
- await clearTx.wait();
24831
- }
25532
+ if (floorBps < protocolMin) {
25533
+ throw new SDKError({
25534
+ message: `Collateral floor ${floorBps / 100}% is below the protocol minimum of ${protocolMin / 100}%`,
25535
+ category: "VALIDATION" /* VALIDATION */,
25536
+ severity: "MEDIUM" /* MEDIUM */
25537
+ });
24832
25538
  }
24833
- const tx = await registry.disableAutoRenew(pid);
24834
- const receipt = await tx.wait();
24835
- return { hash: tx.hash, blockNumber: receipt?.blockNumber ?? 0 };
24836
25539
  }
24837
25540
  // ============================================================================
24838
25541
  // HIGH-LEVEL API - Bitcoin Operations
@@ -26032,6 +26735,146 @@ var EventHelpers = {
26032
26735
  });
26033
26736
  }
26034
26737
  };
26738
+
26739
+ // src/utils/safe-agent-delegation.utils.ts
26740
+ var import_ethers18 = require("ethers");
26741
+ init_deployment_addresses();
26742
+ var SAFE_ABI = [
26743
+ "function getModulesPaginated(address start, uint256 pageSize) view returns (address[] modules, address next)"
26744
+ ];
26745
+ var MODULE_ABI = [
26746
+ "function safe() view returns (address)",
26747
+ "function agent() view returns (address)",
26748
+ "function positionManager() view returns (address)"
26749
+ ];
26750
+ var FACTORY_ABI = ["function isFromFactory(address) view returns (bool)"];
26751
+ var REGISTRY_ABI = [
26752
+ "function agentOf(address user) view returns (address agent, uint64 validUntil, uint8 status, uint64 registeredAt, uint64 revokedAt)"
26753
+ ];
26754
+ var MODULE_LIST_SENTINEL = "0x0000000000000000000000000000000000000001";
26755
+ var MODULE_PAGE_SIZE = 50;
26756
+ var lower = (v) => v.trim().toLowerCase();
26757
+ async function classifyModule(provider, moduleAddress, safeAddress, positionManager, factoryAddress) {
26758
+ const module2 = new import_ethers18.Contract(moduleAddress, MODULE_ABI, provider);
26759
+ let boundSafe;
26760
+ try {
26761
+ boundSafe = await module2.safe();
26762
+ } catch {
26763
+ return null;
26764
+ }
26765
+ if (lower(boundSafe) !== lower(safeAddress))
26766
+ return false;
26767
+ let modulePm;
26768
+ try {
26769
+ modulePm = await module2.positionManager();
26770
+ } catch {
26771
+ return false;
26772
+ }
26773
+ if (lower(modulePm) !== lower(positionManager))
26774
+ return false;
26775
+ if (!factoryAddress)
26776
+ return null;
26777
+ try {
26778
+ const factory = new import_ethers18.Contract(factoryAddress, FACTORY_ABI, provider);
26779
+ return await factory.isFromFactory(moduleAddress) === true;
26780
+ } catch {
26781
+ return null;
26782
+ }
26783
+ }
26784
+ async function getSafeAgentDelegation(params) {
26785
+ const { safeAddress, chainId, provider } = params;
26786
+ const normalized = lower(safeAddress);
26787
+ const unknown = {
26788
+ safeAddress: normalized,
26789
+ chainId,
26790
+ status: "unknown",
26791
+ moduleAddress: null,
26792
+ agentAddress: null,
26793
+ validUntil: null,
26794
+ truncated: false
26795
+ };
26796
+ const contracts = Object.values(ALL_DEPLOYMENTS).find(
26797
+ (deployment) => deployment.chainId === chainId
26798
+ )?.contracts;
26799
+ const positionManager = contracts?.PositionManager;
26800
+ if (!positionManager)
26801
+ return unknown;
26802
+ const factoryAddress = contracts?.AgentModuleFactory;
26803
+ const registryAddress = contracts?.AgentDelegationRegistry;
26804
+ let modules;
26805
+ let next;
26806
+ try {
26807
+ const safe = new import_ethers18.Contract(safeAddress, SAFE_ABI, provider);
26808
+ const page = await safe.getModulesPaginated(
26809
+ MODULE_LIST_SENTINEL,
26810
+ MODULE_PAGE_SIZE
26811
+ );
26812
+ modules = [...page[0]];
26813
+ next = page[1];
26814
+ } catch {
26815
+ return unknown;
26816
+ }
26817
+ const truncated = BigInt(next) > BigInt(MODULE_LIST_SENTINEL);
26818
+ let indeterminate = false;
26819
+ for (const moduleAddress of modules) {
26820
+ const verdict = await classifyModule(
26821
+ provider,
26822
+ moduleAddress,
26823
+ normalized,
26824
+ positionManager,
26825
+ factoryAddress
26826
+ );
26827
+ if (verdict === null) {
26828
+ indeterminate = true;
26829
+ continue;
26830
+ }
26831
+ if (!verdict)
26832
+ continue;
26833
+ let agentAddress = null;
26834
+ try {
26835
+ agentAddress = await new import_ethers18.Contract(
26836
+ moduleAddress,
26837
+ MODULE_ABI,
26838
+ provider
26839
+ ).agent();
26840
+ } catch {
26841
+ }
26842
+ if (!registryAddress) {
26843
+ indeterminate = true;
26844
+ continue;
26845
+ }
26846
+ let validUntil;
26847
+ let status;
26848
+ try {
26849
+ const record = await new import_ethers18.Contract(
26850
+ registryAddress,
26851
+ REGISTRY_ABI,
26852
+ provider
26853
+ ).agentOf(safeAddress);
26854
+ status = Number(record[2]);
26855
+ validUntil = Number(record[1]);
26856
+ } catch {
26857
+ indeterminate = true;
26858
+ continue;
26859
+ }
26860
+ const now = Math.floor(Date.now() / 1e3);
26861
+ if (status !== 1 || validUntil <= now) {
26862
+ return { ...unknown, status: "not-delegated", truncated };
26863
+ }
26864
+ return {
26865
+ safeAddress: normalized,
26866
+ chainId,
26867
+ status: "delegated",
26868
+ moduleAddress,
26869
+ agentAddress: agentAddress ? lower(agentAddress) : null,
26870
+ validUntil,
26871
+ truncated
26872
+ };
26873
+ }
26874
+ if (truncated || indeterminate)
26875
+ return { ...unknown, truncated };
26876
+ return { ...unknown, status: "not-delegated" };
26877
+ }
26035
26878
  // Annotate the CommonJS export names for ESM import in node:
26036
26879
  0 && (module.exports = {
26037
26880
  ALL_CONTRACTS,
@@ -26093,6 +26936,7 @@ var EventHelpers = {
26093
26936
  getDelegateStatus,
26094
26937
  getDeploymentByNetwork,
26095
26938
  getPositionDelegate,
26939
+ getSafeAgentDelegation,
26096
26940
  isClosePositionAuth,
26097
26941
  isCreateLoanAuth,
26098
26942
  isExtendTermAuth,