@babylonlabs-io/ts-sdk 0.19.0 → 0.20.1
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/README.md +56 -12
- package/dist/bitcoin-CNnPFU6Y.cjs +2 -0
- package/dist/bitcoin-CNnPFU6Y.cjs.map +1 -0
- package/dist/{constants-Q7v2O7Ps.js → bitcoin-nOOgeRyl.js} +64 -76
- package/dist/bitcoin-nOOgeRyl.js.map +1 -0
- package/dist/buildAndBroadcastRefund-DWEQvj9T.cjs +2 -0
- package/dist/buildAndBroadcastRefund-DWEQvj9T.cjs.map +1 -0
- package/dist/buildAndBroadcastRefund-DnTQkCgG.js +1331 -0
- package/dist/buildAndBroadcastRefund-DnTQkCgG.js.map +1 -0
- package/dist/challengeAssert-D1lpvuMv.js +283 -0
- package/dist/challengeAssert-D1lpvuMv.js.map +1 -0
- package/dist/challengeAssert-nYlgeAI8.cjs +2 -0
- package/dist/challengeAssert-nYlgeAI8.cjs.map +1 -0
- package/dist/errors-D9EtjJoD.cjs +2 -0
- package/dist/errors-D9EtjJoD.cjs.map +1 -0
- package/dist/{errors-aMocmFr-.js → errors-D_PI__IT.js} +98 -97
- package/dist/errors-D_PI__IT.js.map +1 -0
- package/dist/fundPeginTransaction-BLYXxLBv.js +70 -0
- package/dist/fundPeginTransaction-BLYXxLBv.js.map +1 -0
- package/dist/fundPeginTransaction-DxNOeyNI.cjs +2 -0
- package/dist/fundPeginTransaction-DxNOeyNI.cjs.map +1 -0
- package/dist/index-BpXpESWu.cjs +2 -0
- package/dist/index-BpXpESWu.cjs.map +1 -0
- package/dist/{index-BvFLf3vm.js → index-CFwoGJNQ.js} +146 -518
- package/dist/index-CFwoGJNQ.js.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +133 -109
- package/dist/payout-DzGsAnhf.js +261 -0
- package/dist/payout-DzGsAnhf.js.map +1 -0
- package/dist/payout-RFIh7GLN.cjs +2 -0
- package/dist/payout-RFIh7GLN.cjs.map +1 -0
- package/dist/psbtInputFields-05ZrwRzf.cjs +2 -0
- package/dist/psbtInputFields-05ZrwRzf.cjs.map +1 -0
- package/dist/psbtInputFields-r1ss6WLU.js +237 -0
- package/dist/psbtInputFields-r1ss6WLU.js.map +1 -0
- package/dist/signing-DeWVBl7m.js +64 -0
- package/dist/signing-DeWVBl7m.js.map +1 -0
- package/dist/signing-cl-lowxV.cjs +2 -0
- package/dist/signing-cl-lowxV.cjs.map +1 -0
- package/dist/tbv/core/clients/index.cjs +1 -1
- package/dist/tbv/core/clients/index.js +2 -2
- package/dist/tbv/core/index.cjs +1 -1
- package/dist/tbv/core/index.d.ts +3 -1
- package/dist/tbv/core/index.d.ts.map +1 -1
- package/dist/tbv/core/index.js +130 -106
- package/dist/tbv/core/managers/PeginManager.d.ts +1 -1
- package/dist/tbv/core/primitives/index.cjs +1 -1
- package/dist/tbv/core/primitives/index.d.ts +3 -2
- package/dist/tbv/core/primitives/index.d.ts.map +1 -1
- package/dist/tbv/core/primitives/index.js +24 -23
- package/dist/tbv/core/primitives/psbt/__tests__/helpers.d.ts +1 -1
- package/dist/tbv/core/primitives/psbt/index.d.ts +1 -1
- package/dist/tbv/core/primitives/psbt/payout.d.ts.map +1 -1
- package/dist/tbv/core/primitives/psbt/pegin.d.ts +2 -3
- package/dist/tbv/core/primitives/psbt/pegin.d.ts.map +1 -1
- package/dist/tbv/core/primitives/utils/bitcoin.d.ts +4 -0
- package/dist/tbv/core/primitives/utils/bitcoin.d.ts.map +1 -1
- package/dist/tbv/core/primitives/utils/index.d.ts +1 -1
- package/dist/tbv/core/primitives/utils/index.d.ts.map +1 -1
- package/dist/tbv/core/services/activation/__tests__/activateVault.test.d.ts +2 -0
- package/dist/tbv/core/services/activation/__tests__/activateVault.test.d.ts.map +1 -0
- package/dist/tbv/core/services/activation/activateVault.d.ts +77 -0
- package/dist/tbv/core/services/activation/activateVault.d.ts.map +1 -0
- package/dist/tbv/core/services/activation/index.d.ts +7 -0
- package/dist/tbv/core/services/activation/index.d.ts.map +1 -0
- package/dist/tbv/core/services/deposit/__tests__/peginState.test.d.ts +2 -0
- package/dist/tbv/core/services/deposit/__tests__/peginState.test.d.ts.map +1 -0
- package/dist/tbv/core/services/deposit/__tests__/validation.test.d.ts +5 -0
- package/dist/tbv/core/services/deposit/__tests__/validation.test.d.ts.map +1 -0
- package/dist/tbv/core/services/deposit/index.d.ts +4 -0
- package/dist/tbv/core/services/deposit/index.d.ts.map +1 -1
- package/dist/tbv/core/services/deposit/peginState.d.ts +93 -0
- package/dist/tbv/core/services/deposit/peginState.d.ts.map +1 -0
- package/dist/tbv/core/services/deposit/validation.d.ts +114 -0
- package/dist/tbv/core/services/deposit/validation.d.ts.map +1 -0
- package/dist/tbv/core/services/index.cjs +1 -1
- package/dist/tbv/core/services/index.d.ts +3 -0
- package/dist/tbv/core/services/index.d.ts.map +1 -1
- package/dist/tbv/core/services/index.js +25 -7
- package/dist/tbv/core/services/index.js.map +1 -1
- package/dist/tbv/core/services/pegout/__tests__/state.test.d.ts +2 -0
- package/dist/tbv/core/services/pegout/__tests__/state.test.d.ts.map +1 -0
- package/dist/tbv/core/services/pegout/index.d.ts +2 -0
- package/dist/tbv/core/services/pegout/index.d.ts.map +1 -0
- package/dist/tbv/core/services/pegout/state.d.ts +30 -0
- package/dist/tbv/core/services/pegout/state.d.ts.map +1 -0
- package/dist/tbv/core/services/refund/__tests__/buildAndBroadcastRefund.test.d.ts +2 -0
- package/dist/tbv/core/services/refund/__tests__/buildAndBroadcastRefund.test.d.ts.map +1 -0
- package/dist/tbv/core/services/refund/buildAndBroadcastRefund.d.ts +103 -0
- package/dist/tbv/core/services/refund/buildAndBroadcastRefund.d.ts.map +1 -0
- package/dist/tbv/core/services/refund/errors.d.ts +13 -0
- package/dist/tbv/core/services/refund/errors.d.ts.map +1 -0
- package/dist/tbv/core/services/refund/index.d.ts +8 -0
- package/dist/tbv/core/services/refund/index.d.ts.map +1 -0
- package/dist/tbv/core/utils/index.cjs +1 -1
- package/dist/tbv/core/utils/index.js +30 -24
- package/dist/tbv/core/utils/transaction/fundPeginTransaction.d.ts +1 -1
- package/dist/tbv/core/utils/utxo/__tests__/availability.test.d.ts +3 -0
- package/dist/tbv/core/utils/utxo/__tests__/availability.test.d.ts.map +1 -0
- package/dist/tbv/core/utils/utxo/__tests__/reservation.test.d.ts +3 -0
- package/dist/tbv/core/utils/utxo/__tests__/reservation.test.d.ts.map +1 -0
- package/dist/tbv/core/utils/utxo/availability.d.ts +62 -0
- package/dist/tbv/core/utils/utxo/availability.d.ts.map +1 -0
- package/dist/tbv/core/utils/utxo/index.d.ts +2 -0
- package/dist/tbv/core/utils/utxo/index.d.ts.map +1 -1
- package/dist/tbv/core/utils/utxo/reservation.d.ts +91 -0
- package/dist/tbv/core/utils/utxo/reservation.d.ts.map +1 -0
- package/dist/tbv/index.cjs +1 -1
- package/dist/tbv/index.js +130 -106
- package/dist/tbv/integrations/aave/index.cjs +1 -1
- package/dist/tbv/integrations/aave/index.cjs.map +1 -1
- package/dist/tbv/integrations/aave/index.d.ts +3 -3
- package/dist/tbv/integrations/aave/index.d.ts.map +1 -1
- package/dist/tbv/integrations/aave/index.js +145 -169
- package/dist/tbv/integrations/aave/index.js.map +1 -1
- package/dist/tbv/integrations/aave/utils/healthFactor.d.ts +0 -30
- package/dist/tbv/integrations/aave/utils/healthFactor.d.ts.map +1 -1
- package/dist/tbv/integrations/aave/utils/index.d.ts +2 -2
- package/dist/tbv/integrations/aave/utils/index.d.ts.map +1 -1
- package/dist/types-B-p4dhEH.cjs +2 -0
- package/dist/types-B-p4dhEH.cjs.map +1 -0
- package/dist/types-DWjaqVfP.js +608 -0
- package/dist/types-DWjaqVfP.js.map +1 -0
- package/package.json +5 -1
- package/dist/challengeAssert-D_k_ADgP.cjs +0 -2
- package/dist/challengeAssert-D_k_ADgP.cjs.map +0 -1
- package/dist/challengeAssert-k5_LWUtO.js +0 -362
- package/dist/challengeAssert-k5_LWUtO.js.map +0 -1
- package/dist/constants-EiyZkXce.cjs +0 -2
- package/dist/constants-EiyZkXce.cjs.map +0 -1
- package/dist/constants-Q7v2O7Ps.js.map +0 -1
- package/dist/errors-DVNYib5y.cjs +0 -2
- package/dist/errors-DVNYib5y.cjs.map +0 -1
- package/dist/errors-aMocmFr-.js.map +0 -1
- package/dist/fundPeginTransaction-DpwnDslW.js +0 -50
- package/dist/fundPeginTransaction-DpwnDslW.js.map +0 -1
- package/dist/fundPeginTransaction-EbrZzlrh.cjs +0 -2
- package/dist/fundPeginTransaction-EbrZzlrh.cjs.map +0 -1
- package/dist/index-BX-V3C9t.js +0 -995
- package/dist/index-BX-V3C9t.js.map +0 -1
- package/dist/index-BgnxXV5G.cjs +0 -2
- package/dist/index-BgnxXV5G.cjs.map +0 -1
- package/dist/index-BvFLf3vm.js.map +0 -1
- package/dist/index-DpKhuCta.cjs +0 -2
- package/dist/index-DpKhuCta.cjs.map +0 -1
- package/dist/payout-Ce9vSs9e.js +0 -164
- package/dist/payout-Ce9vSs9e.js.map +0 -1
- package/dist/payout-CfsDnjKI.cjs +0 -2
- package/dist/payout-CfsDnjKI.cjs.map +0 -1
- package/dist/psbtInputFields-C0nKn_GD.cjs +0 -2
- package/dist/psbtInputFields-C0nKn_GD.cjs.map +0 -1
- package/dist/psbtInputFields-DO0ELwiv.js +0 -116
- package/dist/psbtInputFields-DO0ELwiv.js.map +0 -1
- package/dist/signing-Deg5lCoC.cjs +0 -2
- package/dist/signing-Deg5lCoC.cjs.map +0 -1
- package/dist/signing-Drwr3bXB.js +0 -16
- package/dist/signing-Drwr3bXB.js.map +0 -1
- package/dist/types-D1rYwwCu.js +0 -235
- package/dist/types-D1rYwwCu.js.map +0 -1
- package/dist/types-DEWiqXYp.cjs +0 -2
- package/dist/types-DEWiqXYp.cjs.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../../src/tbv/integrations/aave/constants.ts","../../../../src/tbv/integrations/aave/clients/query.ts","../../../../src/tbv/integrations/aave/utils/debtUtils.ts","../../../../src/tbv/integrations/aave/clients/spoke.ts","../../../../src/tbv/integrations/aave/clients/transaction.ts","../../../../src/tbv/integrations/aave/utils/aaveConversions.ts","../../../../src/tbv/integrations/aave/utils/borrowRatio.ts","../../../../src/tbv/integrations/aave/utils/healthFactor.ts","../../../../src/tbv/integrations/aave/utils/vaultSelection.ts","../../../../src/tbv/integrations/aave/utils/cascadeSimulation.ts","../../../../src/tbv/integrations/aave/utils/optimalOrder.ts","../../../../src/tbv/integrations/aave/utils/vaultSplit.ts","../../../../src/tbv/integrations/aave/utils/seizureSimulation.ts"],"sourcesContent":["/**\n * Aave Protocol Constants\n *\n * Constants for interacting with Aave v4 protocol.\n * Reference: https://github.com/aave/aave-v4 ISpoke.sol\n */\n\n/**\n * Aave contract function names\n * Centralized constants for contract interactions\n */\nexport const AAVE_FUNCTION_NAMES = {\n /** Withdraw selected vaults from position (partial withdrawal) */\n WITHDRAW_COLLATERALS: \"withdrawCollaterals\",\n /** Borrow from Core Spoke position */\n BORROW: \"borrowFromCorePosition\",\n /** Repay debt to Core Spoke position */\n REPAY: \"repayToCorePosition\",\n /** Reorder vault prefix ordering for liquidation priority */\n REORDER_VAULTS: \"reorderVaults\",\n} as const;\n\n/**\n * BTC token decimals (satoshis)\n * 1 BTC = 100,000,000 satoshis\n */\nexport const BTC_DECIMALS = 8;\n\n/**\n * USDC token decimals\n * Used for debt calculations\n */\nexport const USDC_DECIMALS = 6;\n\n/**\n * Divisor to convert basis points (BPS) to percentage\n *\n * In Aave v4, risk parameters like collateralRisk are stored in BPS\n * where 10000 BPS = 100%.\n *\n * Example: 8000 BPS / 100 = 80%\n *\n * Reference: ISpoke.sol - \"collateralRisk The risk associated with a\n * collateral asset, expressed in BPS\"\n */\nexport const BPS_TO_PERCENT_DIVISOR = 100;\n\n/**\n * Full basis points scale (10000 BPS = 100%)\n *\n * Use this when converting BPS directly to decimal:\n * Example: 8000 BPS / 10000 = 0.80\n */\nexport const BPS_SCALE = 10000;\n\n/**\n * Aave base currency decimals\n * Account data values (collateral, debt) use 1e26 = $1 USD\n *\n * Reference: ISpoke.sol UserAccountData\n */\nexport const AAVE_BASE_CURRENCY_DECIMALS = 26;\n\n/**\n * Aave RAY-scaled base currency decimals\n * Debt values (totalDebtValueRay) use 1e35 = $1 USD\n *\n * Reference: IAaveSpoke.sol UserAccountData.totalDebtValueRay\n */\nexport const AAVE_BASE_CURRENCY_RAY_DECIMALS = 35;\n\n/**\n * WAD decimals (1e18 = 1.0)\n * Used for health factor and collateral factor values\n *\n * Reference: ISpoke.sol - \"healthFactor expressed in WAD. 1e18 represents a health factor of 1.00\"\n */\nexport const WAD_DECIMALS = 18;\n\n/**\n * Health factor warning threshold\n * Positions below this are considered at risk of liquidation\n */\nexport const HEALTH_FACTOR_WARNING_THRESHOLD = 1.5;\n\n/**\n * Minimum health factor allowed for borrowing\n * Prevents users from borrowing if resulting health factor would be below this.\n */\nexport const MIN_HEALTH_FACTOR_FOR_BORROW = 1.2;\n\n/**\n * Buffer for full repayment to account for interest accrual\n * between fetching debt and transaction execution.\n * 0.01% buffer (1 basis point) - the contract only takes what's owed.\n */\nexport const FULL_REPAY_BUFFER_DIVISOR = 10000n; // 1/10000 = 0.01% buffer\n\n","/**\n * Aave Integration Adapter - Read operations (queries)\n *\n * Only includes functions that provide data NOT available from the indexer.\n * Most position/vault data should be fetched from the GraphQL indexer instead.\n */\n\nimport { type Address, type Hex, type PublicClient, zeroAddress } from \"viem\";\n\nimport type { AaveMarketPosition, PositionSizeParams } from \"../types.js\";\nimport AaveIntegrationAdapterABI from \"./abis/AaveIntegrationAdapter.abi.json\";\n\n/**\n * Get a position by user address.\n *\n * The adapter resolves the user's proxy contract and collateralized vault IDs.\n *\n * NOTE: Prefer using the indexer (fetchAavePositionWithCollaterals) for position data.\n * This function is only needed when you need data not available in the indexer,\n * or when you need to verify on-chain state.\n *\n * @param publicClient - Viem public client for reading contracts\n * @param contractAddress - AaveIntegrationAdapter contract address\n * @param user - User's Ethereum address\n * @returns Market position data or null if position doesn't exist\n */\nexport async function getPosition(\n publicClient: PublicClient,\n contractAddress: Address,\n user: Address,\n): Promise<AaveMarketPosition | null> {\n const result = await publicClient.readContract({\n address: contractAddress,\n abi: AaveIntegrationAdapterABI,\n functionName: \"getPosition\",\n args: [user],\n });\n\n type PositionResult = {\n proxyContract: Address;\n vaultIds: Hex[];\n };\n\n const position = result as PositionResult;\n\n // Check if position exists (proxyContract should not be zero address)\n if (position.proxyContract === zeroAddress) {\n return null;\n }\n\n return {\n proxyContract: position.proxyContract,\n vaultIds: position.vaultIds,\n };\n}\n\n/**\n * Get total collateral for a user's position.\n *\n * @param publicClient - Viem public client for reading contracts\n * @param contractAddress - AaveIntegrationAdapter contract address\n * @param user - User's Ethereum address\n * @returns Total collateral amount in satoshis\n */\nexport async function getPositionCollateral(\n publicClient: PublicClient,\n contractAddress: Address,\n user: Address,\n): Promise<bigint> {\n const result = await publicClient.readContract({\n address: contractAddress,\n abi: AaveIntegrationAdapterABI,\n functionName: \"getPositionCollateral\",\n args: [user],\n });\n\n return result as bigint;\n}\n\n/**\n * Get position size parameters from the adapter contract.\n *\n * Returns the maximum BTC position size and maximum vaults per position\n * as configured on-chain.\n *\n * @param publicClient - Viem public client for reading contracts\n * @param contractAddress - AaveIntegrationAdapter contract address\n * @returns Position size parameters (maxPositionBTC, maxVaultsPerPosition)\n */\nexport async function getPositionSizeParams(\n publicClient: PublicClient,\n contractAddress: Address,\n): Promise<PositionSizeParams> {\n const result = await publicClient.readContract({\n address: contractAddress,\n abi: AaveIntegrationAdapterABI,\n functionName: \"getPositionSizeParams\",\n });\n\n const [maxPositionBTC, maxVaultsPerPosition] = result as [bigint, bigint];\n\n return {\n maxPositionBTC,\n maxVaultsPerPosition,\n };\n}\n","/**\n * Aave Debt Utilities\n *\n * Shared utility functions for debt calculations.\n */\n\nimport type { AaveSpokeUserPosition } from \"../types.js\";\n\n/**\n * Check if a position has any debt based on Spoke position data.\n *\n * A position is considered to have debt if any of:\n * - drawnShares > 0 (borrowed principal)\n * - premiumShares > 0 (accrued interest shares)\n *\n * @param position - User position data from Spoke\n * @returns true if the position has any debt\n */\nexport function hasDebtFromPosition(position: AaveSpokeUserPosition): boolean {\n return position.drawnShares > 0n || position.premiumShares > 0n;\n}\n","/**\n * Aave Spoke Client - Read operations\n *\n * Provides read operations for interacting with Aave v4 Spoke contracts.\n * Used to fetch live user position data (debt, collateral) from the Core Spoke.\n *\n * Note: Reserve data should be fetched from the indexer via fetchReserves.ts\n * since it doesn't need to be live and benefits from caching.\n */\n\nimport type { Address, PublicClient } from \"viem\";\n\nimport type {\n AaveSpokeUserAccountData,\n AaveSpokeUserPosition,\n} from \"../types.js\";\nimport { hasDebtFromPosition } from \"../utils/debtUtils.js\";\nimport AaveSpokeABI from \"./abis/AaveSpoke.abi.json\";\n\n/** Account data result type from contract */\ntype AccountDataResult = {\n riskPremium: bigint;\n avgCollateralFactor: bigint;\n healthFactor: bigint;\n totalCollateralValue: bigint;\n totalDebtValueRay: bigint;\n activeCollateralCount: bigint;\n borrowCount: bigint;\n};\n\n/** Position result type from contract */\ntype PositionResult = {\n drawnShares: bigint;\n premiumShares: bigint;\n premiumOffsetRay: bigint;\n suppliedShares: bigint;\n dynamicConfigKey: number;\n};\n\n/**\n * Maps contract result to AaveSpokeUserPosition\n */\nfunction mapPositionResult(result: PositionResult): AaveSpokeUserPosition {\n return {\n drawnShares: result.drawnShares,\n premiumShares: result.premiumShares,\n premiumOffsetRay: result.premiumOffsetRay,\n suppliedShares: result.suppliedShares,\n dynamicConfigKey: result.dynamicConfigKey,\n };\n}\n\n/**\n * Get aggregated user account health data from AAVE spoke.\n *\n * **Live data** - Fetches real-time account health including health factor, total collateral,\n * and total debt across all reserves. Values are calculated on-chain using AAVE oracles\n * and are the authoritative source for liquidation decisions.\n *\n * @param publicClient - Viem public client for reading contracts (from `createPublicClient()`)\n * @param spokeAddress - AAVE Spoke contract address (BTC Vault Core Spoke for vBTC collateral)\n * @param userAddress - User's proxy contract address (NOT user's wallet address)\n * @returns User account data with health metrics, collateral, and debt values\n *\n * @example\n * ```typescript\n * import { getUserAccountData } from \"@babylonlabs-io/ts-sdk/tbv/integrations/aave\";\n * import { createPublicClient, http } from \"viem\";\n * import { sepolia } from \"viem/chains\";\n *\n * const publicClient = createPublicClient({\n * chain: sepolia,\n * transport: http()\n * });\n *\n * const accountData = await getUserAccountData(\n * publicClient,\n * \"0x123...\", // AAVE Spoke address\n * \"0x456...\" // User's AAVE proxy address (from getPosition)\n * );\n *\n * console.log(\"Health Factor:\", accountData.healthFactor);\n * console.log(\"Collateral (USD):\", accountData.totalCollateralValue);\n * console.log(\"Debt (USD):\", accountData.totalDebtValueRay);\n * ```\n *\n * @remarks\n * **Return values:**\n * - `healthFactor` - WAD format (1e18 = 1.0). Below 1.0 = liquidatable\n * - `totalCollateralValue` - USD value in base currency (1e26 = $1)\n * - `totalDebtValueRay` - USD value in RAY-scaled base currency (1e35 = $1)\n * - `avgCollateralFactor` - Weighted average collateral factor in WAD (1e18 = 100%)\n * - `riskPremium` - Additional risk premium\n *\n * **Use cases:**\n * - Check liquidation risk before borrowing\n * - Calculate safe borrow amount\n * - Monitor position health\n * - Display UI health indicators\n */\nexport async function getUserAccountData(\n publicClient: PublicClient,\n spokeAddress: Address,\n userAddress: Address,\n): Promise<AaveSpokeUserAccountData> {\n const result = await publicClient.readContract({\n address: spokeAddress,\n abi: AaveSpokeABI,\n functionName: \"getUserAccountData\",\n args: [userAddress],\n });\n\n const data = result as AccountDataResult;\n return {\n riskPremium: data.riskPremium,\n avgCollateralFactor: data.avgCollateralFactor,\n healthFactor: data.healthFactor,\n totalCollateralValue: data.totalCollateralValue,\n totalDebtValueRay: data.totalDebtValueRay,\n activeCollateralCount: data.activeCollateralCount,\n borrowCount: data.borrowCount,\n };\n}\n\n/**\n * Get user position from the Spoke\n *\n * This fetches live data from the contract because debt accrues interest\n * and needs to be current for accurate health factor calculations.\n *\n * @param publicClient - Viem public client for reading contracts\n * @param spokeAddress - Aave Spoke contract address\n * @param reserveId - Reserve ID\n * @param userAddress - User's proxy contract address\n * @returns User position data\n */\nexport async function getUserPosition(\n publicClient: PublicClient,\n spokeAddress: Address,\n reserveId: bigint,\n userAddress: Address,\n): Promise<AaveSpokeUserPosition> {\n const result = await publicClient.readContract({\n address: spokeAddress,\n abi: AaveSpokeABI,\n functionName: \"getUserPosition\",\n args: [reserveId, userAddress],\n });\n\n return mapPositionResult(result as PositionResult);\n}\n\n/**\n * Check if a user has any debt in a reserve\n *\n * @param publicClient - Viem public client for reading contracts\n * @param spokeAddress - Aave Spoke contract address\n * @param reserveId - Reserve ID\n * @param userAddress - User's proxy contract address\n * @returns true if user has debt\n */\nexport async function hasDebt(\n publicClient: PublicClient,\n spokeAddress: Address,\n reserveId: bigint,\n userAddress: Address,\n): Promise<boolean> {\n const position = await getUserPosition(\n publicClient,\n spokeAddress,\n reserveId,\n userAddress,\n );\n return hasDebtFromPosition(position);\n}\n\n/**\n * Check if a user has supplied collateral in a reserve\n *\n * @param publicClient - Viem public client for reading contracts\n * @param spokeAddress - Aave Spoke contract address\n * @param reserveId - Reserve ID\n * @param userAddress - User's proxy contract address\n * @returns true if user has supplied collateral\n */\nexport async function hasCollateral(\n publicClient: PublicClient,\n spokeAddress: Address,\n reserveId: bigint,\n userAddress: Address,\n): Promise<boolean> {\n const position = await getUserPosition(\n publicClient,\n spokeAddress,\n reserveId,\n userAddress,\n );\n return position.suppliedShares > 0n;\n}\n\n/**\n * Get user's exact total debt in a reserve (token units, not shares).\n *\n * Returns the precise amount owed including accrued interest. Essential for full repayment.\n * Debt accrues interest every block, so this must be fetched live from the contract.\n *\n * @param publicClient - Viem public client for reading contracts\n * @param spokeAddress - AAVE Spoke contract address\n * @param reserveId - Reserve ID for the debt asset (e.g., `2n` for USDC)\n * @param userAddress - User's proxy contract address\n * @returns Total debt amount in token units (e.g., for USDC: `100000000n` = 100 USDC)\n *\n * @example\n * ```typescript\n * import { getUserTotalDebt, FULL_REPAY_BUFFER_DIVISOR } from \"@babylonlabs-io/ts-sdk/tbv/integrations/aave\";\n * import { formatUnits } from \"viem\";\n *\n * const totalDebt = await getUserTotalDebt(\n * publicClient,\n * AAVE_SPOKE_ADDRESS,\n * 2n, // USDC reserve\n * proxyAddress\n * );\n *\n * // For full repayment, add buffer to account for interest accrual\n * const repayAmount = totalDebt + (totalDebt / FULL_REPAY_BUFFER_DIVISOR);\n *\n * console.log(\"Debt:\", formatUnits(totalDebt, 6), \"USDC\");\n * ```\n *\n * @remarks\n * **Important for full repayment:**\n * - Add `FULL_REPAY_BUFFER_DIVISOR` buffer to account for interest between fetch and tx execution\n * - Contract only takes what's owed; excess stays in wallet\n * - For partial repayment, use any amount less than total debt\n */\nexport async function getUserTotalDebt(\n publicClient: PublicClient,\n spokeAddress: Address,\n reserveId: bigint,\n userAddress: Address,\n): Promise<bigint> {\n const result = await publicClient.readContract({\n address: spokeAddress,\n abi: AaveSpokeABI,\n functionName: \"getUserTotalDebt\",\n args: [reserveId, userAddress],\n });\n\n return result as bigint;\n}\n\n/** Result type from the `getReserve` contract call.\n *\n * Matches the on-chain `Reserve` struct defined in `ITBVAaveSpoke.sol`:\n * struct Reserve {\n * address underlying;\n * address hub;\n * uint16 assetId;\n * uint8 decimals;\n * uint24 collateralRisk;\n * ReserveFlags flags; // uint8 bitmap\n * uint32 dynamicConfigKey;\n * }\n *\n * Note: this is the `Reserve` struct, NOT `ReserveConfig` — the contract\n * exposes both as separate functions and they return different shapes.\n */\ntype ReserveResult = {\n underlying: Address;\n hub: Address;\n assetId: number;\n decimals: number;\n collateralRisk: number;\n flags: number;\n dynamicConfigKey: number;\n};\n\n/**\n * Get reserve data from the Core Spoke contract via the `getReserve` selector.\n *\n * Returns static reserve properties including the `dynamicConfigKey` needed\n * for `getDynamicReserveConfig` calls. Use this as a fallback when reserve\n * data is not available from the GraphQL indexer.\n *\n * Do NOT confuse with the contract's separate `getReserveConfig` function,\n * which returns `{collateralRisk, paused, frozen, borrowable, receiveSharesEnabled}`.\n *\n * @param publicClient - Viem public client for reading contracts\n * @param spokeAddress - Core Spoke contract address\n * @param reserveId - Reserve ID\n * @returns Reserve data including `dynamicConfigKey`\n */\nexport async function getReserve(\n publicClient: PublicClient,\n spokeAddress: Address,\n reserveId: bigint,\n): Promise<ReserveResult> {\n const result = await publicClient.readContract({\n address: spokeAddress,\n abi: AaveSpokeABI,\n functionName: \"getReserve\",\n args: [reserveId],\n });\n return result as ReserveResult;\n}\n\n/** Result type from getLiquidationConfig contract call */\ntype LiquidationConfigResult = {\n targetHealthFactor: bigint;\n healthFactorForMaxBonus: bigint;\n liquidationBonusFactor: bigint;\n};\n\n/** Result type from getDynamicReserveConfig contract call */\ntype DynamicReserveConfigResult = {\n collateralFactor: bigint;\n maxLiquidationBonus: bigint;\n liquidationFee: bigint;\n};\n\n/**\n * Get the target health factor (THF) from the Core Spoke contract.\n *\n * Per-spoke governance parameter. After a liquidation, the protocol targets\n * restoring the position to this health factor.\n *\n * @param publicClient - Viem public client for reading contracts\n * @param spokeAddress - Core Spoke contract address\n * @returns Target health factor in WAD (1e18 = 1.0). Example: 1.10 = 1_100_000_000_000_000_000n\n */\nexport async function getTargetHealthFactor(\n publicClient: PublicClient,\n spokeAddress: Address,\n): Promise<bigint> {\n const result = await publicClient.readContract({\n address: spokeAddress,\n abi: AaveSpokeABI,\n functionName: \"getLiquidationConfig\",\n });\n const config = result as LiquidationConfigResult;\n return config.targetHealthFactor;\n}\n\n/**\n * Get the dynamic reserve config from the Core Spoke contract.\n *\n * Returns collateral factor, max liquidation bonus, and liquidation fee\n * for a specific reserve and dynamic config key.\n *\n * @param publicClient - Viem public client for reading contracts\n * @param spokeAddress - Core Spoke contract address\n * @param reserveId - Reserve ID (e.g., vBTC reserve ID from indexer config)\n * @param dynamicConfigKey - Dynamic config key (from reserve data)\n * @returns Dynamic reserve config with collateralFactor (BPS), maxLiquidationBonus (BPS), liquidationFee (BPS)\n */\nexport async function getDynamicReserveConfig(\n publicClient: PublicClient,\n spokeAddress: Address,\n reserveId: bigint,\n dynamicConfigKey: number,\n): Promise<DynamicReserveConfigResult> {\n const result = await publicClient.readContract({\n address: spokeAddress,\n abi: AaveSpokeABI,\n functionName: \"getDynamicReserveConfig\",\n args: [reserveId, dynamicConfigKey],\n });\n return result as DynamicReserveConfigResult;\n}\n","/**\n * Aave Integration Adapter - Transaction builders\n *\n * Provides transaction builders for the AaveIntegrationAdapter contract.\n * Only includes Core Spoke operations for regular users (no Arbitrageur operations).\n *\n * These functions return unsigned transaction parameters that can be executed\n * by the vault service using its wallet client and transaction factory.\n */\n\nimport { type Address, type Hex, encodeFunctionData } from \"viem\";\n\nimport { AAVE_FUNCTION_NAMES } from \"../config.js\";\nimport type { TransactionParams } from \"../types.js\";\nimport AaveIntegrationAdapterABI from \"./abis/AaveIntegrationAdapter.abi.json\";\n\n/**\n * Build transaction to reorder vaults for liquidation priority.\n *\n * The permuted array must contain exactly the same vault IDs as the\n * current position, in the desired new order. Vaults are seized in\n * prefix order (index 0 first) during liquidation.\n *\n * @param contractAddress - AaveIntegrationAdapter contract address\n * @param permutedVaultIds - Vault IDs in desired new order (must be a permutation of current vaults)\n * @returns Unsigned transaction parameters\n */\nexport function buildReorderVaultsTx(\n contractAddress: Address,\n permutedVaultIds: Hex[],\n): TransactionParams {\n const data = encodeFunctionData({\n abi: AaveIntegrationAdapterABI,\n functionName: AAVE_FUNCTION_NAMES.REORDER_VAULTS,\n args: [permutedVaultIds],\n });\n\n return {\n to: contractAddress,\n data,\n };\n}\n\n/**\n * Build transaction to withdraw selected vaults from AAVE position.\n *\n * Withdraws specific vaults (partial withdrawal) and redeems them back to the depositor.\n * **Requires zero debt** - position must have no outstanding borrows.\n *\n * @param contractAddress - AaveIntegrationAdapter contract address\n * @param vaultIds - Array of vault IDs (bytes32) to withdraw\n * @returns Unsigned transaction parameters for execution with viem wallet\n */\nexport function buildWithdrawCollateralsTx(\n contractAddress: Address,\n vaultIds: Hex[],\n): TransactionParams {\n const data = encodeFunctionData({\n abi: AaveIntegrationAdapterABI,\n functionName: AAVE_FUNCTION_NAMES.WITHDRAW_COLLATERALS,\n args: [vaultIds],\n });\n\n return {\n to: contractAddress,\n data,\n };\n}\n\n/**\n * Build transaction to borrow assets against vBTC collateral.\n *\n * Borrows stablecoins (e.g., USDC) against your BTC collateral position.\n * Health factor must remain above 1.0 after borrowing, otherwise transaction will revert.\n *\n * @param contractAddress - AaveIntegrationAdapter contract address\n * @param debtReserveId - AAVE reserve ID for the debt asset (e.g., `2n` for USDC reserve)\n * @param amount - Amount to borrow in token units with decimals (e.g., for USDC with 6 decimals: `100000000n` = 100 USDC). Use `parseUnits()` from viem.\n * @param receiver - Address to receive borrowed tokens (usually user's address)\n * @returns Unsigned transaction parameters for execution with viem wallet\n *\n * @example\n * ```typescript\n * import { buildBorrowTx } from \"@babylonlabs-io/ts-sdk/tbv/integrations/aave\";\n * import { parseUnits } from \"viem\";\n *\n * // Borrow 100 USDC (6 decimals)\n * const borrowAmount = parseUnits(\"100\", 6);\n *\n * const txParams = buildBorrowTx(\n * \"0x123...\", // Adapter address\n * 2n, // USDC reserve ID\n * borrowAmount,\n * \"0x456...\" // Receiver address\n * );\n *\n * const hash = await walletClient.sendTransaction({\n * to: txParams.to,\n * data: txParams.data,\n * chain: sepolia,\n * });\n * ```\n *\n * @remarks\n * **What happens on-chain:**\n * 1. Checks health factor won't drop below liquidation threshold (1.0)\n * 2. Mints debt tokens to user's proxy contract\n * 3. Transfers borrowed asset to receiver address\n * 4. Updates position debt\n * 5. Emits `Borrowed` event\n *\n * **Possible errors:**\n * - Borrow would make health factor < 1.0\n * - Insufficient collateral\n * - Reserve doesn't exist\n * - Position doesn't exist\n *\n * **Important:** Calculate safe borrow amount using `calculateHealthFactor()` to avoid liquidation.\n */\nexport function buildBorrowTx(\n contractAddress: Address,\n debtReserveId: bigint,\n amount: bigint,\n receiver: Address,\n): TransactionParams {\n const data = encodeFunctionData({\n abi: AaveIntegrationAdapterABI,\n functionName: AAVE_FUNCTION_NAMES.BORROW,\n args: [debtReserveId, amount, receiver],\n });\n\n return {\n to: contractAddress,\n data,\n };\n}\n\n/**\n * Build transaction to repay debt on AAVE position.\n *\n * **Requires token approval** - user must approve adapter to spend debt token first.\n * Repays borrowed assets (partial or full repayment supported).\n *\n * @param contractAddress - AaveIntegrationAdapter contract address\n * @param borrower - Borrower's address (for self-repay, use connected wallet address)\n * @param debtReserveId - AAVE reserve ID for the debt asset\n * @param amount - Amount to repay in token units. Can repay partial or full debt. For full repay, use `getUserTotalDebt()` to get exact amount.\n * @returns Unsigned transaction parameters for execution with viem wallet\n *\n * @example\n * ```typescript\n * import { buildRepayTx } from \"@babylonlabs-io/ts-sdk/tbv/integrations/aave\";\n *\n * // Build repay transaction (self-repay)\n * const txParams = buildRepayTx(\n * AAVE_ADAPTER,\n * borrowerAddress, // Connected wallet address for self-repay\n * USDC_RESERVE_ID,\n * repayAmount\n * );\n *\n * const hash = await walletClient.sendTransaction({\n * to: txParams.to,\n * data: txParams.data,\n * chain: sepolia,\n * });\n * ```\n *\n * @remarks\n * **What happens on-chain:**\n * 1. Transfers tokens from user to adapter (requires approval)\n * 2. Burns debt tokens from user's proxy\n * 3. Updates position debt\n * 4. Emits `Repaid` event\n *\n * **Possible errors:**\n * - Insufficient token approval\n * - User doesn't have enough tokens\n * - Repay amount exceeds debt\n * - Position doesn't exist\n */\nexport function buildRepayTx(\n contractAddress: Address,\n borrower: Address,\n debtReserveId: bigint,\n amount: bigint,\n): TransactionParams {\n const data = encodeFunctionData({\n abi: AaveIntegrationAdapterABI,\n functionName: AAVE_FUNCTION_NAMES.REPAY,\n args: [borrower, debtReserveId, amount],\n });\n\n return {\n to: contractAddress,\n data,\n };\n}\n\n","/**\n * Aave Value Conversion Utilities\n *\n * Converts Aave on-chain values to human-readable numbers.\n */\n\nimport {\n AAVE_BASE_CURRENCY_DECIMALS,\n AAVE_BASE_CURRENCY_RAY_DECIMALS,\n WAD_DECIMALS,\n} from \"../constants.js\";\n\n/**\n * Convert Aave base currency value to USD\n *\n * Aave uses 1e26 = $1 USD for collateral values.\n *\n * @param value - Value in Aave base currency (1e26 = $1)\n * @returns Value in USD\n */\nexport function aaveValueToUsd(value: bigint): number {\n return Number(value) / 10 ** AAVE_BASE_CURRENCY_DECIMALS;\n}\n\n/**\n * Convert Aave RAY-scaled base currency value to USD\n *\n * Debt values use higher precision: 1e35 = $1 USD.\n *\n * @param value - Value in RAY-scaled base currency (1e35 = $1)\n * @returns Value in USD\n */\nexport function aaveRayValueToUsd(value: bigint): number {\n return Number(value) / 10 ** AAVE_BASE_CURRENCY_RAY_DECIMALS;\n}\n\n/**\n * Convert Aave WAD value to number\n *\n * WAD is used for health factor and collateral factor (1e18 = 1.0).\n *\n * @param value - Value in WAD (1e18 = 1.0)\n * @returns Decimal number\n */\nexport function wadToNumber(value: bigint): number {\n return Number(value) / 10 ** WAD_DECIMALS;\n}\n","/**\n * Borrow Ratio Utilities\n *\n * Borrow ratio = debt / collateral as a percentage.\n * Shows how much of the collateral is being used for borrowing.\n */\n\n/**\n * Calculate borrow ratio (debt / collateral) as percentage string\n *\n * @param debtUsd - Total debt in USD\n * @param collateralValueUsd - Total collateral value in USD\n * @returns Formatted percentage string (e.g., \"15.7%\")\n */\nexport function calculateBorrowRatio(\n debtUsd: number,\n collateralValueUsd: number,\n): string {\n if (collateralValueUsd <= 0) return \"0%\";\n const ratio = (debtUsd / collateralValueUsd) * 100;\n return `${ratio.toFixed(1)}%`;\n}\n","/**\n * Health Factor Utilities for Aave\n *\n * Health factor is calculated by Aave on-chain using oracle prices.\n * A health factor below 1.0 means the position can be liquidated.\n *\n * Status thresholds:\n * - no_debt: No active debt (null health factor)\n * - danger: < 1.0 (can be liquidated)\n * - warning: < HEALTH_FACTOR_WARNING_THRESHOLD (at risk)\n * - safe: >= HEALTH_FACTOR_WARNING_THRESHOLD (healthy)\n *\n * Color mapping:\n * - Green (#00E676): safe\n * - Amber (#FFC400): warning\n * - Red (#FF1744): danger\n * - Gray (#5A5A5A): no_debt\n */\n\nimport { BPS_SCALE, HEALTH_FACTOR_WARNING_THRESHOLD } from \"../constants.js\";\n\nexport const HEALTH_FACTOR_COLORS = {\n GREEN: \"#00E676\",\n AMBER: \"#FFC400\",\n RED: \"#FF1744\",\n GRAY: \"#5A5A5A\",\n} as const;\n\nexport type HealthFactorColor =\n (typeof HEALTH_FACTOR_COLORS)[keyof typeof HEALTH_FACTOR_COLORS];\n\n/**\n * Health factor status based on our liquidation threshold\n */\nexport type HealthFactorStatus = \"safe\" | \"warning\" | \"danger\" | \"no_debt\";\n\n/**\n * Determine health factor status for UI display\n *\n * @param healthFactor - The health factor as a number (null if no debt)\n * @param hasDebt - Whether the position has active debt\n * @returns The status classification\n */\nexport function getHealthFactorStatus(\n healthFactor: number | null,\n hasDebt: boolean,\n): HealthFactorStatus {\n if (!hasDebt) return \"no_debt\";\n if (healthFactor === null) return \"safe\";\n if (healthFactor < 1.0) return \"danger\";\n if (healthFactor < HEALTH_FACTOR_WARNING_THRESHOLD) return \"warning\";\n return \"safe\";\n}\n\n/**\n * Gets the appropriate color for a health factor status.\n *\n * @param status - The health factor status\n * @returns The color code for the status\n */\nexport function getHealthFactorColor(\n status: HealthFactorStatus,\n): HealthFactorColor {\n switch (status) {\n case \"safe\":\n return HEALTH_FACTOR_COLORS.GREEN;\n case \"warning\":\n return HEALTH_FACTOR_COLORS.AMBER;\n case \"danger\":\n return HEALTH_FACTOR_COLORS.RED;\n case \"no_debt\":\n return HEALTH_FACTOR_COLORS.GRAY;\n }\n}\n\n/**\n * Format health factor number for display\n *\n * @param healthFactor - Health factor number (null if no debt)\n * @returns Formatted string for display\n */\nexport function formatHealthFactor(healthFactor: number | null): string {\n if (healthFactor === null) {\n return \"-\"; // No debt\n }\n return healthFactor.toFixed(2);\n}\n\n/**\n * Checks if a health factor value represents a healthy position.\n *\n * @param healthFactor - The health factor as a number\n * @returns true if the health factor is >= 1.0 (healthy), false otherwise\n */\nexport function isHealthFactorHealthy(healthFactor: number | null): boolean {\n if (healthFactor === null) {\n return true; // No debt = healthy\n }\n return healthFactor >= 1.0;\n}\n\n/**\n * Get health factor status from a numeric value.\n * Used for UI components that work with Infinity for no-debt scenarios.\n *\n * @param value - Health factor value (Infinity when no debt)\n * @returns The status classification\n */\nexport function getHealthFactorStatusFromValue(\n value: number,\n): HealthFactorStatus {\n const hasDebt = isFinite(value);\n const healthFactor = isFinite(value) ? value : null;\n return getHealthFactorStatus(healthFactor, hasDebt);\n}\n\n/**\n * Calculate health factor for an AAVE position.\n *\n * **Formula:** `HF = (Collateral × Liquidation Threshold) / Total Debt`\n *\n * Health factor determines liquidation risk:\n * - `>= 1.5` - Safe (green)\n * - `1.0 - 1.5` - Warning (amber)\n * - `< 1.0` - Danger, position can be liquidated (red)\n *\n * @param collateralValueUsd - Total collateral value in USD (as number, not bigint)\n * @param totalDebtUsd - Total debt value in USD (as number, not bigint)\n * @param liquidationThresholdBps - Liquidation threshold in basis points (e.g., `8000` = 80%)\n * @returns Health factor value (e.g., `1.5`), or `Infinity` if no debt\n *\n * @example\n * ```typescript\n * import { calculateHealthFactor, HEALTH_FACTOR_WARNING_THRESHOLD } from \"@babylonlabs-io/ts-sdk/tbv/integrations/aave\";\n *\n * // User has $10,000 BTC collateral, $5,000 debt, 80% LT\n * const hf = calculateHealthFactor(10000, 5000, 8000);\n * // Result: 1.6 (safe to borrow more)\n *\n * if (hf < 1.0) {\n * console.error(\"Position can be liquidated!\");\n * } else if (hf < HEALTH_FACTOR_WARNING_THRESHOLD) {\n * console.warn(\"Position at risk, consider repaying\");\n * } else {\n * console.log(\"Position is safe\");\n * }\n * ```\n *\n * @remarks\n * **Before borrowing:**\n * Use this to calculate resulting health factor and ensure it stays above safe threshold.\n *\n * **Unit conversions:**\n * - Convert AAVE base currency (1e26) to USD by dividing by 1e26\n * - Use `aaveValueToUsd()` helper for automatic conversion\n */\nexport function calculateHealthFactor(\n collateralValueUsd: number,\n totalDebtUsd: number,\n liquidationThresholdBps: number,\n): number {\n if (totalDebtUsd <= 0) return Infinity;\n return (\n (collateralValueUsd * (liquidationThresholdBps / BPS_SCALE)) / totalDebtUsd\n );\n}\n","/**\n * Vault Selection Utilities for Aave\n *\n * Provides functions for selecting vaults to match a target collateral amount.\n * Uses a greedy algorithm that prioritizes larger vaults first.\n */\n\nexport interface SelectableVault {\n id: string;\n amount: number;\n}\n\nexport interface VaultSelectionResult {\n /** IDs of selected vaults */\n vaultIds: string[];\n /** Actual total amount from selected vaults */\n actualAmount: number;\n}\n\n/**\n * Select vaults to match the target amount using a greedy algorithm.\n * Sorts vaults by amount descending and picks until target is met.\n *\n * @param vaults - Available vaults to select from\n * @param targetAmount - Target amount to reach\n * @returns Selected vault IDs and actual amount\n */\nexport function selectVaultsForAmount(\n vaults: SelectableVault[],\n targetAmount: number,\n): VaultSelectionResult {\n if (targetAmount <= 0) {\n return { vaultIds: [], actualAmount: 0 };\n }\n\n const sortedVaults = [...vaults].sort((a, b) => b.amount - a.amount);\n\n const selectedIds: string[] = [];\n let selectedAmount = 0;\n\n for (const vault of sortedVaults) {\n if (selectedAmount >= targetAmount) break;\n selectedIds.push(vault.id);\n selectedAmount += vault.amount;\n }\n\n return { vaultIds: selectedIds, actualAmount: selectedAmount };\n}\n\n/**\n * Calculate total amount from a list of vaults\n *\n * @param vaults - Vaults to sum\n * @returns Total amount in BTC\n */\nexport function calculateTotalVaultAmount(vaults: SelectableVault[]): number {\n return vaults.reduce((sum, vault) => sum + vault.amount, 0);\n}\n","/**\n * Cascade Liquidation Simulation\n *\n * Simulates multi-group liquidation cascades for Aave positions backed by\n * indivisible BTC vaults (UTXOs). Each liquidation event seizes a prefix of\n * vaults until the target seizure is covered, then debt is reduced and the\n * cascade continues with remaining vaults.\n *\n * Used by the optimizer to score different vault orderings by how much\n * collateral survives the cascade.\n */\n\n/**\n * Minimal vault shape for cascade simulation.\n * UI layers extend this with display fields (e.g. `name`).\n */\nexport interface CascadeVault {\n id: string;\n btc: number;\n}\n\n/** 1% tolerance for prefix walk coverage — avoids cliff flip at boundary */\nexport const SEIZURE_TOL = 0.01;\n\n/** Circuit breaker for group cascade loop */\nexport const MAX_GROUPS = 20;\n\n/** Minimum debt threshold to continue cascade (avoids infinite loop on dust) */\nexport const MIN_DEBT_THRESHOLD = 0.01;\n\n/**\n * Prefix walk: consume vaults front-to-back until target seizure is covered.\n * Returns the vaults in the first liquidation group.\n */\nexport function getGroup1FromOrder<T extends CascadeVault>(\n order: T[],\n seizedFraction: number,\n seizureTol: number,\n): T[] {\n if (order.length === 0) return [];\n const totalBtc = order.reduce((s, v) => s + v.btc, 0);\n const coverThreshold = totalBtc * seizedFraction * (1 - seizureTol);\n let prefixSum = 0;\n let i = 0;\n while (i < order.length && prefixSum < coverThreshold) {\n prefixSum += order[i].btc;\n i++;\n }\n return order.slice(0, i);\n}\n\n/**\n * Simulate one liquidation group, returning updated debt and remaining BTC.\n * Handles both safe (non-last) and full (last) liquidation paths.\n */\nfunction simulateOneGroup(\n seizedBtc: number,\n totalBtc: number,\n debt: number,\n isLastGroup: boolean,\n seizedFraction: number,\n CF: number,\n THF: number,\n maxLB: number,\n expectedHF: number,\n): { debtAfter: number; btcAfter: number } {\n const liqPenalty = maxLB * CF;\n const pLiq = debt / (totalBtc * CF);\n const targetSeizure = totalBtc * seizedFraction;\n const overSeizureBtc = Math.max(0, seizedBtc - targetSeizure);\n const denominator = THF - liqPenalty;\n const debtToRepay =\n denominator === 0 ? debt : debt * ((THF - expectedHF) / denominator);\n\n let debtAfter: number;\n if (isLastGroup) {\n debtAfter = 0;\n } else {\n const overSeizureVal = (overSeizureBtc * pLiq) / maxLB;\n const fairnessDebtRepay = Math.min(overSeizureVal, debt - debtToRepay);\n debtAfter = Math.max(0, debt - debtToRepay - fairnessDebtRepay);\n }\n return { debtAfter, btcAfter: Math.max(0, totalBtc - seizedBtc) };\n}\n\n/**\n * Simulate full liquidation cascade with debt model.\n *\n * PRIMARY score: sumBtcAfterEvents — sum of BTC remaining after every event.\n * Captures how much collateral survives at each stage.\n * TIEBREAKER: btcAfterG1 — BTC remaining after the first (most likely) event.\n */\nexport function simulateCascade<T extends CascadeVault>(\n order: T[],\n totalDebt: number,\n seizedFraction: number,\n seizureTol: number,\n CF: number,\n THF: number,\n maxLB: number,\n expectedHF: number,\n): { sumBtcAfterEvents: number; btcAfterG1: number } {\n let remaining = [...order];\n let debt = totalDebt;\n const initialTotalBtc = order.reduce((s, v) => s + v.btc, 0);\n let btcAfterG1 = -1;\n let sumBtcAfterEvents = 0;\n let groupCount = 0;\n\n while (\n remaining.length > 0 &&\n debt > MIN_DEBT_THRESHOLD &&\n groupCount < MAX_GROUPS\n ) {\n const totalBtc = remaining.reduce((s, v) => s + v.btc, 0);\n const coverThreshold = totalBtc * seizedFraction * (1 - seizureTol);\n let prefixSum = 0;\n let i = 0;\n while (i < remaining.length && prefixSum < coverThreshold) {\n prefixSum += remaining[i].btc;\n i++;\n }\n const isLastGroup = i >= remaining.length;\n const { debtAfter } = simulateOneGroup(\n prefixSum,\n totalBtc,\n debt,\n isLastGroup,\n seizedFraction,\n CF,\n THF,\n maxLB,\n expectedHF,\n );\n remaining = remaining.slice(i);\n debt = debtAfter;\n const btcNow = totalBtc - prefixSum;\n sumBtcAfterEvents += btcNow;\n if (btcAfterG1 < 0) btcAfterG1 = btcNow;\n groupCount++;\n }\n\n return {\n sumBtcAfterEvents,\n btcAfterG1:\n btcAfterG1 < 0 ? initialTotalBtc : btcAfterG1,\n };\n}\n","/**\n * Optimal Vault Ordering for Liquidation Protection\n *\n * Finds the vault ordering that maximizes collateral surviving a multi-group\n * liquidation cascade. Uses exhaustive bitmask enumeration (2^n for n ≤ 20)\n * to find the minimum-BTC subset covering the target seizure for Group 1,\n * then greedy for subsequent groups.\n *\n * The iterative refinement loop re-runs the optimizer on its own output,\n * converging in ≤3 iterations in practice.\n */\n\nimport { simulateCascade, type CascadeVault } from \"./cascadeSimulation.js\";\n\n/** Convergence threshold for iterative refinement */\nconst EPS = 0.0001;\n\n/** Max refinement iterations for optimizer */\nconst MAX_ITER = 5;\n\n/** Max vaults for exhaustive bitmask search */\nconst MAX_VAULTS_FOR_EXHAUSTIVE = 20;\n\n/**\n * Greedy sub-order: arrange remaining vaults by finding minimum BTC subset\n * covering target for each group, using exhaustive search when feasible.\n */\nfunction greedyOrder<T extends CascadeVault>(\n vaults: T[],\n seizedFraction: number,\n seizureTol: number,\n): T[] {\n const result: T[] = [];\n let remaining = [...vaults];\n\n while (remaining.length > 0) {\n const totalBtc = remaining.reduce((s, v) => s + v.btc, 0);\n const coverThreshold = totalBtc * seizedFraction * (1 - seizureTol);\n let optSubset: T[] = [...remaining];\n let optSum = totalBtc;\n const m = remaining.length;\n\n if (m <= MAX_VAULTS_FOR_EXHAUSTIVE) {\n for (let mask = 1; mask < 1 << m; mask++) {\n let sum = 0;\n const subset: T[] = [];\n for (let bit = 0; bit < m; bit++) {\n if (mask & (1 << bit)) {\n sum += remaining[bit].btc;\n subset.push(remaining[bit]);\n }\n }\n if (sum >= coverThreshold && sum < optSum) {\n optSum = sum;\n optSubset = subset;\n }\n }\n }\n\n const ids = new Set(optSubset.map((v) => v.id));\n result.push(...[...optSubset].sort((a, b) => b.btc - a.btc));\n remaining = remaining.filter((v) => !ids.has(v.id));\n }\n\n return result;\n}\n\n/**\n * Single-pass optimizer: enumerate all possible Group 1 subsets,\n * use greedy for remaining groups, pick the order that maximizes\n * collateral surviving the cascade.\n */\nfunction computeOptimalOrderSinglePass<T extends CascadeVault>(\n vaults: T[],\n totalDebt: number,\n seizedFraction: number,\n seizureTol: number,\n CF: number,\n THF: number,\n maxLB: number,\n expectedHF: number,\n): { order: T[]; sumBtcAfterEvents: number; btcAfterG1: number } {\n if (vaults.length === 0)\n return { order: [], sumBtcAfterEvents: 0, btcAfterG1: 0 };\n if (vaults.length > MAX_VAULTS_FOR_EXHAUSTIVE) {\n throw new Error(\n `Too many vaults for exhaustive search: ${vaults.length} exceeds maximum of ${MAX_VAULTS_FOR_EXHAUSTIVE}`,\n );\n }\n if (vaults.length === 1) {\n const sim = simulateCascade(\n vaults,\n totalDebt,\n seizedFraction,\n seizureTol,\n CF,\n THF,\n maxLB,\n expectedHF,\n );\n return { order: [...vaults], ...sim };\n }\n\n const n = vaults.length;\n const totalBtc = vaults.reduce((s, v) => s + v.btc, 0);\n const coverThreshold = totalBtc * seizedFraction * (1 - seizureTol);\n\n let bestOrder = [...vaults];\n let bestSum = -Infinity;\n let bestAfterG1 = -Infinity;\n\n for (let mask = 1; mask < 1 << n; mask++) {\n let g1Btc = 0;\n const g1: T[] = [];\n const rest: T[] = [];\n for (let bit = 0; bit < n; bit++) {\n if (mask & (1 << bit)) {\n g1Btc += vaults[bit].btc;\n g1.push(vaults[bit]);\n } else {\n rest.push(vaults[bit]);\n }\n }\n if (g1Btc < coverThreshold) continue;\n\n g1.sort((a, b) => b.btc - a.btc);\n const candidate = [...g1, ...greedyOrder(rest, seizedFraction, seizureTol)];\n const { sumBtcAfterEvents, btcAfterG1 } = simulateCascade(\n candidate,\n totalDebt,\n seizedFraction,\n seizureTol,\n CF,\n THF,\n maxLB,\n expectedHF,\n );\n\n if (\n sumBtcAfterEvents > bestSum + EPS ||\n (Math.abs(sumBtcAfterEvents - bestSum) <= EPS &&\n btcAfterG1 > bestAfterG1 + EPS)\n ) {\n bestSum = sumBtcAfterEvents;\n bestAfterG1 = btcAfterG1;\n bestOrder = candidate;\n }\n }\n\n return {\n order: bestOrder,\n sumBtcAfterEvents: bestSum,\n btcAfterG1: bestAfterG1,\n };\n}\n\n/**\n * Main optimizer: iterative refinement until stable.\n * Re-running with the improved order lets the next pass find better\n * G1 subsets. Converges in ≤3 iterations in practice.\n */\nexport function computeOptimalOrder<T extends CascadeVault>(\n vaults: T[],\n totalDebt: number,\n seizedFraction: number,\n seizureTol: number,\n CF: number,\n THF: number,\n maxLB: number,\n expectedHF: number,\n): { order: T[]; sumBtcAfterEvents: number; btcAfterG1: number } {\n let result = computeOptimalOrderSinglePass(\n vaults,\n totalDebt,\n seizedFraction,\n seizureTol,\n CF,\n THF,\n maxLB,\n expectedHF,\n );\n\n for (let i = 0; i < MAX_ITER; i++) {\n const refined = computeOptimalOrderSinglePass(\n result.order,\n totalDebt,\n seizedFraction,\n seizureTol,\n CF,\n THF,\n maxLB,\n expectedHF,\n );\n const primaryImproves =\n refined.sumBtcAfterEvents > result.sumBtcAfterEvents + EPS;\n const primaryTied =\n Math.abs(refined.sumBtcAfterEvents - result.sumBtcAfterEvents) <= EPS;\n const tiebreakerImproves =\n primaryTied && refined.btcAfterG1 > result.btcAfterG1 + EPS;\n if (!primaryImproves && !tiebreakerImproves) {\n break;\n }\n result = refined;\n }\n\n return result;\n}\n","/**\n * Vault Split Utilities for Aave Liquidation Protection\n *\n * BTC vaults are indivisible UTXOs. During liquidation, the protocol seizes\n * whole vaults as a prefix of the borrower's ordered vault list until the\n * target seizure amount is covered. Splitting deposits into 2 optimally-sized\n * vaults (sacrificial + protected) minimizes over-seizure loss.\n *\n * The sacrificial vault (index 0) is sized to cover the expected target seizure\n * plus a safety margin. The protected vault (index 1) holds the remainder and\n * survives liquidation.\n *\n * Seizure formula (from Aave v4 Section 4.2):\n * ```\n * liq_penalty = LB × CF\n * debt_to_repay = total_debt × (THF - current_HF) / (THF - liq_penalty)\n * target_seizure = debt_to_repay × LB\n * ```\n */\n\nconst MAX_SAFE_BIGINT = BigInt(Number.MAX_SAFE_INTEGER);\n\nexport function assertSafePrecision(value: bigint, name: string): void {\n if (value > MAX_SAFE_BIGINT) {\n throw new RangeError(\n `${name} (${value}) exceeds Number.MAX_SAFE_INTEGER; precision would be lost`,\n );\n }\n}\n\n/**\n * Parameters for computing the optimal vault split.\n */\nexport interface OptimalSplitParams {\n /** Total deposit amount in satoshis */\n totalBtc: bigint;\n /** Collateral factor (e.g. 0.75 for 75%) */\n CF: number;\n /** Liquidation bonus (e.g. 1.05 for 5% bonus) */\n LB: number;\n /** Target health factor (e.g. 1.10) */\n THF: number;\n /** Expected health factor at liquidation (e.g. 0.95) */\n expectedHF: number;\n /** Safety margin multiplier for the sacrificial vault (e.g. 1.05 for 5% buffer) */\n safetyMargin: number;\n}\n\n/**\n * Result of the optimal vault split computation.\n */\nexport interface OptimalSplitResult {\n /** Sacrificial vault amount in satoshis (index 0, seized first) */\n sacrificialVault: bigint;\n /** Protected vault amount in satoshis (index 1, survives liquidation) */\n protectedVault: bigint;\n /** Fraction of collateral that would be seized (0–1) */\n seizedFraction: number;\n /** Raw target seizure amount in satoshis (before safety margin) */\n targetSeizureBtc: bigint;\n}\n\n/**\n * Parameters for computing the minimum deposit required for a split.\n */\nexport interface MinDepositForSplitParams {\n /** Minimum peg-in amount in satoshis */\n minPegin: bigint;\n /** Seized fraction (0–1), from computeOptimalSplit or computeSeizedFraction */\n seizedFraction: number;\n /** Safety margin multiplier (e.g. 1.05) */\n safetyMargin: number;\n}\n\n/**\n * Parameters for checking if a vault rebalance is needed.\n */\nexport interface RebalanceCheckParams {\n /** Ordered vault amounts in satoshis (index 0 is sacrificial) */\n vaultAmounts: bigint[];\n /** Collateral factor (e.g. 0.75) */\n CF: number;\n /** Liquidation bonus (e.g. 1.05) */\n LB: number;\n /** Target health factor (e.g. 1.10) */\n THF: number;\n /** Expected health factor at liquidation (e.g. 0.95) */\n expectedHF: number;\n /** Safety margin multiplier (e.g. 1.05) */\n safetyMargin: number;\n}\n\n/**\n * Result of a vault rebalance check.\n */\nexport interface RebalanceCheckResult {\n /** Whether the sacrificial vault needs to be increased */\n needsRebalance: boolean;\n /** How much more the sacrificial vault needs in satoshis (0n if no rebalance needed) */\n deficit: bigint;\n /** Current sacrificial vault coverage in satoshis */\n currentCoverage: bigint;\n /** Required sacrificial vault coverage in satoshis */\n targetCoverage: bigint;\n}\n\n/**\n * Compute the fraction of collateral that would be seized during liquidation,\n * returning both the raw (unclamped) and clamped values.\n *\n * The raw value is useful for detecting unusual protocol parameter combinations\n * (values outside [0, 1] indicate something unexpected).\n *\n * Formula:\n * ```\n * liq_penalty = LB × CF\n * seized_fraction = CF × (THF - expectedHF) / (THF - liq_penalty) × LB / expectedHF\n * ```\n *\n * @param CF - Collateral factor (e.g. 0.75)\n * @param LB - Liquidation bonus (e.g. 1.05)\n * @param THF - Target health factor (e.g. 1.10)\n * @param expectedHF - Expected health factor at liquidation (e.g. 0.95)\n * @returns Both the raw seized fraction and the clamped [0, 1] value\n */\nexport function computeSeizedFractionDetailed(\n CF: number,\n LB: number,\n THF: number,\n expectedHF: number,\n): { seizedFraction: number; seizedFractionRaw: number } {\n // HF ≤ 0 means position is fully underwater — full seizure\n if (expectedHF <= 0) {\n return { seizedFraction: 1, seizedFractionRaw: Infinity };\n }\n\n const liqPenalty = LB * CF;\n\n // If THF <= liq_penalty, full liquidation is inevitable\n if (THF <= liqPenalty) {\n return { seizedFraction: 1, seizedFractionRaw: Infinity };\n }\n\n // Floating-point errors here are ~1e-15, negligible relative to the 5%\n // safety margin applied by callers (computeOptimalSplit, checkRebalanceNeeded).\n const seizedFractionRaw =\n ((CF * (THF - expectedHF)) / (THF - liqPenalty)) * (LB / expectedHF);\n\n return {\n seizedFraction: Math.max(0, Math.min(1, seizedFractionRaw)),\n seizedFractionRaw,\n };\n}\n\n/**\n * Compute the fraction of collateral that would be seized during liquidation.\n *\n * @param CF - Collateral factor (e.g. 0.75)\n * @param LB - Liquidation bonus (e.g. 1.05)\n * @param THF - Target health factor (e.g. 1.10)\n * @param expectedHF - Expected health factor at liquidation (e.g. 0.95)\n * @returns Seized fraction clamped to [0, 1]\n */\nexport function computeSeizedFraction(\n CF: number,\n LB: number,\n THF: number,\n expectedHF: number,\n): number {\n return computeSeizedFractionDetailed(CF, LB, THF, expectedHF).seizedFraction;\n}\n\n/**\n * Compute the optimal split between a sacrificial vault and a protected vault.\n *\n * The sacrificial vault (index 0) is sized to cover the target seizure amount\n * plus a safety margin. The protected vault (index 1) holds the remainder.\n *\n * @param params - Split parameters including total BTC, risk params, and safety margin\n * @returns Split result with vault sizes, seized fraction, and target seizure\n *\n * @example\n * ```typescript\n * import { computeOptimalSplit } from \"@babylonlabs-io/ts-sdk/tbv/integrations/aave\";\n *\n * const result = computeOptimalSplit({\n * totalBtc: 1_000_000_000n, // 10 BTC in sats\n * CF: 0.75,\n * LB: 1.05,\n * THF: 1.10,\n * expectedHF: 0.95,\n * safetyMargin: 1.05,\n * });\n * // result.sacrificialVault ≈ 418_000_000n (4.18 BTC)\n * // result.protectedVault ≈ 582_000_000n (5.82 BTC)\n * ```\n */\nexport function computeOptimalSplit(\n params: OptimalSplitParams,\n): OptimalSplitResult {\n const { totalBtc, CF, LB, THF, expectedHF, safetyMargin } = params;\n\n if (totalBtc <= 0n) {\n return {\n sacrificialVault: 0n,\n protectedVault: 0n,\n seizedFraction: 0,\n targetSeizureBtc: 0n,\n };\n }\n\n assertSafePrecision(totalBtc, \"totalBtc\");\n\n const seizedFraction = computeSeizedFraction(CF, LB, THF, expectedHF);\n\n const totalBtcNum = Number(totalBtc);\n const targetSeizureBtc = BigInt(Math.ceil(totalBtcNum * seizedFraction));\n\n const sacrificialRaw = BigInt(\n Math.ceil(totalBtcNum * seizedFraction * safetyMargin),\n );\n const sacrificialVault =\n sacrificialRaw > totalBtc ? totalBtc : sacrificialRaw;\n const protectedVault = totalBtc - sacrificialVault;\n\n return {\n sacrificialVault,\n protectedVault,\n seizedFraction,\n targetSeizureBtc,\n };\n}\n\n/**\n * Compute the minimum total deposit required for a 2-vault split.\n *\n * Both vaults must be at least `minPegin` satoshis. This function returns\n * the minimum total deposit where both the sacrificial and protected vaults\n * would meet the minimum peg-in requirement.\n *\n * @param params - Parameters including minimum peg-in, seized fraction, and safety margin\n * @returns Minimum total deposit in satoshis. Returns 0n in two cases:\n * - `seizedFraction * safetyMargin >= 1`: split impossible (sacrificial vault would consume entire deposit)\n * - `seizedFraction <= 0`: split not useful (no seizure expected at this health factor)\n *\n * @example\n * ```typescript\n * import { computeMinDepositForSplit } from \"@babylonlabs-io/ts-sdk/tbv/integrations/aave\";\n *\n * const minDeposit = computeMinDepositForSplit({\n * minPegin: 50_000n, // 0.0005 BTC\n * seizedFraction: 0.398,\n * safetyMargin: 1.05,\n * });\n * ```\n */\nexport function computeMinDepositForSplit(\n params: MinDepositForSplitParams,\n): bigint {\n const { minPegin, seizedFraction, safetyMargin } = params;\n\n assertSafePrecision(minPegin, \"minPegin\");\n\n const sacrificialShare = seizedFraction * safetyMargin;\n\n // If sacrificial vault would consume the entire deposit, split is not possible\n if (sacrificialShare >= 1) {\n return 0n;\n }\n\n // If seized fraction is effectively zero, split is not useful\n if (sacrificialShare <= 0) {\n return 0n;\n }\n\n // Minimum total so the protected vault (smaller share) >= minPegin\n const protectedShare = 1 - sacrificialShare;\n const minFromProtected = Math.ceil(Number(minPegin) / protectedShare);\n\n // Minimum total so the sacrificial vault >= minPegin\n const minFromSacrificial = Math.ceil(Number(minPegin) / sacrificialShare);\n\n return BigInt(Math.max(minFromProtected, minFromSacrificial));\n}\n\n/**\n * Check if the sacrificial vault (index 0) needs to be increased to cover\n * the current target seizure amount.\n *\n * **Scope:** This function only checks whether the sacrificial vault's sizing\n * is adequate. It does NOT detect whether a split exists — a single vault that\n * exceeds the target coverage returns `needsRebalance: false`. Callers should\n * check `vaultAmounts.length < 2` separately to detect unsplit positions.\n *\n * Used on position page load to detect when parameter changes (THF, CF, LB)\n * have made the current split insufficient.\n *\n * @param params - Current vault amounts and risk parameters\n * @returns Whether rebalance is needed, with deficit details\n *\n * @example\n * ```typescript\n * import { checkRebalanceNeeded } from \"@babylonlabs-io/ts-sdk/tbv/integrations/aave\";\n *\n * const result = checkRebalanceNeeded({\n * vaultAmounts: [300_000_000n, 700_000_000n], // 3 BTC sacrificial, 7 BTC protected\n * CF: 0.75,\n * LB: 1.05,\n * THF: 1.10,\n * expectedHF: 0.95,\n * safetyMargin: 1.05,\n * });\n *\n * if (result.needsRebalance) {\n * console.log(`Sacrificial vault needs ${result.deficit} more sats`);\n * }\n * ```\n */\nexport function checkRebalanceNeeded(\n params: RebalanceCheckParams,\n): RebalanceCheckResult {\n const { vaultAmounts, CF, LB, THF, expectedHF, safetyMargin } = params;\n\n if (vaultAmounts.length === 0) {\n return {\n needsRebalance: false,\n deficit: 0n,\n currentCoverage: 0n,\n targetCoverage: 0n,\n };\n }\n\n const totalBtc = vaultAmounts.reduce((sum, v) => sum + v, 0n);\n assertSafePrecision(totalBtc, \"totalBtc\");\n\n const seizedFraction = computeSeizedFraction(CF, LB, THF, expectedHF);\n\n const targetCoverage = BigInt(\n Math.ceil(Number(totalBtc) * seizedFraction * safetyMargin),\n );\n const currentCoverage = vaultAmounts[0];\n\n const deficit =\n targetCoverage > currentCoverage ? targetCoverage - currentCoverage : 0n;\n\n return {\n needsRebalance: deficit > 0n,\n deficit,\n currentCoverage,\n targetCoverage,\n };\n}\n","/**\n * Seizure Simulation Utilities\n *\n * Simulates which vaults would be seized during Aave liquidation.\n * Vaults are seized in prefix order (index 0, 1, 2, ...) until the\n * target seizure amount is covered.\n *\n * Reference: Aave v4 Section 4.2 — liquidation seizes a prefix of the\n * borrower's ordered vault list.\n */\n\nimport { assertSafePrecision, computeSeizedFraction } from \"./vaultSplit.js\";\n\n/**\n * A vault with its on-chain ID and BTC amount, in liquidation-priority order.\n */\nexport interface OrderedVault {\n /** On-chain vault ID (bytes32 hex string) */\n id: string;\n /** Vault amount in satoshis */\n amountSats: bigint;\n}\n\n/**\n * Parameters for simulating prefix seizure.\n */\nexport interface PrefixSeizureParams {\n /** Vaults in their current on-chain order (index 0 is seized first) */\n orderedVaults: OrderedVault[];\n /** Target seizure amount in satoshis */\n targetSeizureSats: bigint;\n}\n\n/**\n * Result of a prefix seizure simulation.\n */\nexport interface PrefixSeizureResult {\n /** Vaults that would be seized (the prefix) */\n seizedVaults: OrderedVault[];\n /** Vaults that survive liquidation */\n protectedVaults: OrderedVault[];\n /** Over-seizure amount in satoshis (total seized - target) */\n overSeizureSats: bigint;\n /** Index where seizure stops (exclusive: vaults[0..cutoffIndex] are seized) */\n cutoffIndex: number;\n /** Total amount seized in satoshis */\n totalSeizedSats: bigint;\n}\n\n/**\n * Parameters for computing target seizure in satoshis.\n */\nexport interface TargetSeizureParams {\n /** Total collateral in satoshis */\n totalCollateralSats: bigint;\n /** Collateral factor (e.g. 0.75) */\n CF: number;\n /** Liquidation bonus (e.g. 1.05) */\n LB: number;\n /** Target health factor (e.g. 1.10) */\n THF: number;\n /** Expected health factor at liquidation (e.g. 0.95) */\n expectedHF: number;\n}\n\n/**\n * Compute the target seizure amount in satoshis.\n *\n * Uses `computeSeizedFraction` to determine what fraction of total collateral\n * would be seized, then converts to an absolute satoshi amount.\n *\n * @param params - Total collateral and risk parameters\n * @returns Target seizure amount in satoshis (rounded up)\n *\n * @example\n * ```typescript\n * const targetSats = computeTargetSeizureSats({\n * totalCollateralSats: 1_000_000_000n, // 10 BTC\n * CF: 0.75,\n * LB: 1.05,\n * THF: 1.10,\n * expectedHF: 0.95,\n * });\n * // targetSats ≈ 398_000_000n (3.98 BTC)\n * ```\n */\nexport function computeTargetSeizureSats(\n params: TargetSeizureParams,\n): bigint {\n const { totalCollateralSats, CF, LB, THF, expectedHF } = params;\n\n if (totalCollateralSats <= 0n) {\n return 0n;\n }\n\n assertSafePrecision(totalCollateralSats, \"totalCollateralSats\");\n\n const seizedFraction = computeSeizedFraction(CF, LB, THF, expectedHF);\n\n return BigInt(Math.ceil(Number(totalCollateralSats) * seizedFraction));\n}\n\n/**\n * Simulate prefix seizure for a given set of ordered vaults.\n *\n * Walks the ordered vault list, accumulating amounts until the target\n * seizure is covered. Returns which vaults are seized vs protected,\n * the over-seizure amount, and the cutoff index.\n *\n * @param params - Ordered vaults and target seizure amount\n * @returns Seizure simulation result\n * @throws Error if orderedVaults is empty\n * @throws Error if targetSeizureSats is <= 0\n *\n * @example\n * ```typescript\n * const result = simulatePrefixSeizure({\n * orderedVaults: [\n * { id: \"0xabc...\", amountSats: 200_000_000n },\n * { id: \"0xdef...\", amountSats: 300_000_000n },\n * { id: \"0x123...\", amountSats: 500_000_000n },\n * ],\n * targetSeizureSats: 400_000_000n,\n * });\n * // result.seizedVaults = first 2 vaults (200M + 300M = 500M >= 400M)\n * // result.overSeizureSats = 100_000_000n\n * // result.cutoffIndex = 2\n * ```\n */\nexport function simulatePrefixSeizure(\n params: PrefixSeizureParams,\n): PrefixSeizureResult {\n const { orderedVaults, targetSeizureSats } = params;\n\n if (orderedVaults.length === 0) {\n throw new Error(\"orderedVaults must not be empty\");\n }\n\n if (targetSeizureSats <= 0n) {\n throw new Error(\n \"targetSeizureSats must be positive; use computeTargetSeizureSats to derive it\",\n );\n }\n\n let accumulated = 0n;\n let cutoffIndex = orderedVaults.length; // default: all vaults seized\n\n for (let i = 0; i < orderedVaults.length; i++) {\n accumulated += orderedVaults[i].amountSats;\n\n if (accumulated >= targetSeizureSats) {\n cutoffIndex = i + 1;\n break;\n }\n }\n\n const seizedVaults = orderedVaults.slice(0, cutoffIndex);\n const protectedVaults = orderedVaults.slice(cutoffIndex);\n const totalSeizedSats = accumulated;\n const overSeizureSats =\n totalSeizedSats > targetSeizureSats\n ? totalSeizedSats - targetSeizureSats\n : 0n;\n\n return {\n seizedVaults,\n protectedVaults,\n overSeizureSats,\n cutoffIndex,\n totalSeizedSats,\n };\n}\n"],"names":["AAVE_FUNCTION_NAMES","BTC_DECIMALS","USDC_DECIMALS","BPS_TO_PERCENT_DIVISOR","BPS_SCALE","AAVE_BASE_CURRENCY_DECIMALS","AAVE_BASE_CURRENCY_RAY_DECIMALS","WAD_DECIMALS","HEALTH_FACTOR_WARNING_THRESHOLD","MIN_HEALTH_FACTOR_FOR_BORROW","FULL_REPAY_BUFFER_DIVISOR","getPosition","publicClient","contractAddress","user","position","AaveIntegrationAdapterABI","zeroAddress","getPositionCollateral","getPositionSizeParams","result","maxPositionBTC","maxVaultsPerPosition","hasDebtFromPosition","mapPositionResult","getUserAccountData","spokeAddress","userAddress","data","AaveSpokeABI","getUserPosition","reserveId","hasDebt","hasCollateral","getUserTotalDebt","getReserve","getTargetHealthFactor","getDynamicReserveConfig","dynamicConfigKey","buildReorderVaultsTx","permutedVaultIds","encodeFunctionData","buildWithdrawCollateralsTx","vaultIds","buildBorrowTx","debtReserveId","amount","receiver","buildRepayTx","borrower","aaveValueToUsd","value","aaveRayValueToUsd","wadToNumber","calculateBorrowRatio","debtUsd","collateralValueUsd","HEALTH_FACTOR_COLORS","getHealthFactorStatus","healthFactor","getHealthFactorColor","status","formatHealthFactor","isHealthFactorHealthy","getHealthFactorStatusFromValue","calculateHealthFactor","totalDebtUsd","liquidationThresholdBps","selectVaultsForAmount","vaults","targetAmount","sortedVaults","a","b","selectedIds","selectedAmount","vault","calculateTotalVaultAmount","sum","SEIZURE_TOL","MAX_GROUPS","MIN_DEBT_THRESHOLD","getGroup1FromOrder","order","seizedFraction","seizureTol","coverThreshold","s","v","prefixSum","i","simulateOneGroup","seizedBtc","totalBtc","debt","isLastGroup","CF","THF","maxLB","expectedHF","liqPenalty","pLiq","targetSeizure","overSeizureBtc","denominator","debtToRepay","debtAfter","overSeizureVal","fairnessDebtRepay","simulateCascade","totalDebt","remaining","initialTotalBtc","btcAfterG1","sumBtcAfterEvents","groupCount","btcNow","EPS","MAX_ITER","MAX_VAULTS_FOR_EXHAUSTIVE","greedyOrder","optSubset","optSum","m","mask","subset","bit","ids","computeOptimalOrderSinglePass","sim","n","bestOrder","bestSum","bestAfterG1","g1Btc","g1","rest","candidate","computeOptimalOrder","refined","primaryImproves","tiebreakerImproves","MAX_SAFE_BIGINT","assertSafePrecision","name","computeSeizedFractionDetailed","LB","seizedFractionRaw","computeSeizedFraction","computeOptimalSplit","params","safetyMargin","totalBtcNum","targetSeizureBtc","sacrificialRaw","sacrificialVault","protectedVault","computeMinDepositForSplit","minPegin","sacrificialShare","protectedShare","minFromProtected","minFromSacrificial","checkRebalanceNeeded","vaultAmounts","targetCoverage","currentCoverage","deficit","computeTargetSeizureSats","totalCollateralSats","simulatePrefixSeizure","orderedVaults","targetSeizureSats","accumulated","cut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AAI,IAAAkD,GAAI,KAAAjD,GAAK,YAAAE,MAAemD;AAEzD,MAAImB,KAAuB;AACzB,WAAO;AAGT,EAAA1B,EAAoB0B,GAAqB,qBAAqB;AAE9D,QAAMrF,IAAiBgE,EAAsBpD,GAAIkD,GAAIjD,GAAKE,CAAU;AAEpE,SAAO,OAAO,KAAK,KAAK,OAAOsE,CAAmB,IAAIrF,CAAc,CAAC;AACvE;AA6BO,SAASsF,GACdpB,GACqB;AACrB,QAAM,EAAE,eAAAqB,GAAe,mBAAAC,EAAA,IAAsBtB;AAE7C,MAAIqB,EAAc,WAAW;AAC3B,UAAM,IAAI,MAAM,iCAAiC;AAGnD,MAAIC,KAAqB;AACvB,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAIJ,MAAIC,IAAc,IACdC,IAAcH,EAAc;AAEhC,WAASjF,IAAI,GAAGA,IAAIiF,EAAc,QAAQjF;AAGxC,QAFAmF,KAAeF,EAAcjF,CAAC,EAAE,YAE5BmF,KAAeD,GAAmB;AACpC,MAAAE,IAAcpF,IAAI;AAClB;AAAA,IACF;AAGF,QAAMqF,IAAeJ,EAAc,MAAM,GAAGG,CAAW,GACjDE,IAAkBL,EAAc,MAAMG,CAAW,GACjDG,IAAkBJ,GAClBK,IACJD,IAAkBL,IACdK,IAAkBL,IAClB;AAEN,SAAO;AAAA,IACL,cAAAG;AAAA,IACA,iBAAAC;AAAA,IACA,iBAAAE;AAAA,IACA,aAAAJ;AAAA,IACA,iBAAAG;AAAA,EAAA;AAEJ;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../../src/tbv/integrations/aave/constants.ts","../../../../src/tbv/integrations/aave/clients/query.ts","../../../../src/tbv/integrations/aave/utils/debtUtils.ts","../../../../src/tbv/integrations/aave/clients/spoke.ts","../../../../src/tbv/integrations/aave/clients/transaction.ts","../../../../src/tbv/integrations/aave/utils/aaveConversions.ts","../../../../src/tbv/integrations/aave/utils/borrowRatio.ts","../../../../src/tbv/integrations/aave/utils/healthFactor.ts","../../../../src/tbv/integrations/aave/utils/vaultSelection.ts","../../../../src/tbv/integrations/aave/utils/cascadeSimulation.ts","../../../../src/tbv/integrations/aave/utils/optimalOrder.ts","../../../../src/tbv/integrations/aave/utils/vaultSplit.ts","../../../../src/tbv/integrations/aave/utils/seizureSimulation.ts"],"sourcesContent":["/**\n * Aave Protocol Constants\n *\n * Constants for interacting with Aave v4 protocol.\n * Reference: https://github.com/aave/aave-v4 ISpoke.sol\n */\n\n/**\n * Aave contract function names\n * Centralized constants for contract interactions\n */\nexport const AAVE_FUNCTION_NAMES = {\n /** Withdraw selected vaults from position (partial withdrawal) */\n WITHDRAW_COLLATERALS: \"withdrawCollaterals\",\n /** Borrow from Core Spoke position */\n BORROW: \"borrowFromCorePosition\",\n /** Repay debt to Core Spoke position */\n REPAY: \"repayToCorePosition\",\n /** Reorder vault prefix ordering for liquidation priority */\n REORDER_VAULTS: \"reorderVaults\",\n} as const;\n\n/**\n * BTC token decimals (satoshis)\n * 1 BTC = 100,000,000 satoshis\n */\nexport const BTC_DECIMALS = 8;\n\n/**\n * USDC token decimals\n * Used for debt calculations\n */\nexport const USDC_DECIMALS = 6;\n\n/**\n * Divisor to convert basis points (BPS) to percentage\n *\n * In Aave v4, risk parameters like collateralRisk are stored in BPS\n * where 10000 BPS = 100%.\n *\n * Example: 8000 BPS / 100 = 80%\n *\n * Reference: ISpoke.sol - \"collateralRisk The risk associated with a\n * collateral asset, expressed in BPS\"\n */\nexport const BPS_TO_PERCENT_DIVISOR = 100;\n\n/**\n * Full basis points scale (10000 BPS = 100%)\n *\n * Use this when converting BPS directly to decimal:\n * Example: 8000 BPS / 10000 = 0.80\n */\nexport const BPS_SCALE = 10000;\n\n/**\n * Aave base currency decimals\n * Account data values (collateral, debt) use 1e26 = $1 USD\n *\n * Reference: ISpoke.sol UserAccountData\n */\nexport const AAVE_BASE_CURRENCY_DECIMALS = 26;\n\n/**\n * Aave RAY-scaled base currency decimals\n * Debt values (totalDebtValueRay) use 1e35 = $1 USD\n *\n * Reference: IAaveSpoke.sol UserAccountData.totalDebtValueRay\n */\nexport const AAVE_BASE_CURRENCY_RAY_DECIMALS = 35;\n\n/**\n * WAD decimals (1e18 = 1.0)\n * Used for health factor and collateral factor values\n *\n * Reference: ISpoke.sol - \"healthFactor expressed in WAD. 1e18 represents a health factor of 1.00\"\n */\nexport const WAD_DECIMALS = 18;\n\n/**\n * Health factor warning threshold\n * Positions below this are considered at risk of liquidation\n */\nexport const HEALTH_FACTOR_WARNING_THRESHOLD = 1.5;\n\n/**\n * Minimum health factor allowed for borrowing\n * Prevents users from borrowing if resulting health factor would be below this.\n */\nexport const MIN_HEALTH_FACTOR_FOR_BORROW = 1.2;\n\n/**\n * Buffer for full repayment to account for interest accrual\n * between fetching debt and transaction execution.\n * 0.01% buffer (1 basis point) - the contract only takes what's owed.\n */\nexport const FULL_REPAY_BUFFER_DIVISOR = 10000n; // 1/10000 = 0.01% buffer\n\n","/**\n * Aave Integration Adapter - Read operations (queries)\n *\n * Only includes functions that provide data NOT available from the indexer.\n * Most position/vault data should be fetched from the GraphQL indexer instead.\n */\n\nimport { type Address, type Hex, type PublicClient, zeroAddress } from \"viem\";\n\nimport type { AaveMarketPosition, PositionSizeParams } from \"../types.js\";\nimport AaveIntegrationAdapterABI from \"./abis/AaveIntegrationAdapter.abi.json\";\n\n/**\n * Get a position by user address.\n *\n * The adapter resolves the user's proxy contract and collateralized vault IDs.\n *\n * NOTE: Prefer using the indexer (fetchAavePositionWithCollaterals) for position data.\n * This function is only needed when you need data not available in the indexer,\n * or when you need to verify on-chain state.\n *\n * @param publicClient - Viem public client for reading contracts\n * @param contractAddress - AaveIntegrationAdapter contract address\n * @param user - User's Ethereum address\n * @returns Market position data or null if position doesn't exist\n */\nexport async function getPosition(\n publicClient: PublicClient,\n contractAddress: Address,\n user: Address,\n): Promise<AaveMarketPosition | null> {\n const result = await publicClient.readContract({\n address: contractAddress,\n abi: AaveIntegrationAdapterABI,\n functionName: \"getPosition\",\n args: [user],\n });\n\n type PositionResult = {\n proxyContract: Address;\n vaultIds: Hex[];\n };\n\n const position = result as PositionResult;\n\n // Check if position exists (proxyContract should not be zero address)\n if (position.proxyContract === zeroAddress) {\n return null;\n }\n\n return {\n proxyContract: position.proxyContract,\n vaultIds: position.vaultIds,\n };\n}\n\n/**\n * Get total collateral for a user's position.\n *\n * @param publicClient - Viem public client for reading contracts\n * @param contractAddress - AaveIntegrationAdapter contract address\n * @param user - User's Ethereum address\n * @returns Total collateral amount in satoshis\n */\nexport async function getPositionCollateral(\n publicClient: PublicClient,\n contractAddress: Address,\n user: Address,\n): Promise<bigint> {\n const result = await publicClient.readContract({\n address: contractAddress,\n abi: AaveIntegrationAdapterABI,\n functionName: \"getPositionCollateral\",\n args: [user],\n });\n\n return result as bigint;\n}\n\n/**\n * Get position size parameters from the adapter contract.\n *\n * Returns the maximum BTC position size and maximum vaults per position\n * as configured on-chain.\n *\n * @param publicClient - Viem public client for reading contracts\n * @param contractAddress - AaveIntegrationAdapter contract address\n * @returns Position size parameters (maxPositionBTC, maxVaultsPerPosition)\n */\nexport async function getPositionSizeParams(\n publicClient: PublicClient,\n contractAddress: Address,\n): Promise<PositionSizeParams> {\n const result = await publicClient.readContract({\n address: contractAddress,\n abi: AaveIntegrationAdapterABI,\n functionName: \"getPositionSizeParams\",\n });\n\n const [maxPositionBTC, maxVaultsPerPosition] = result as [bigint, bigint];\n\n return {\n maxPositionBTC,\n maxVaultsPerPosition,\n };\n}\n","/**\n * Aave Debt Utilities\n *\n * Shared utility functions for debt calculations.\n */\n\nimport type { AaveSpokeUserPosition } from \"../types.js\";\n\n/**\n * Check if a position has any debt based on Spoke position data.\n *\n * A position is considered to have debt if any of:\n * - drawnShares > 0 (borrowed principal)\n * - premiumShares > 0 (accrued interest shares)\n *\n * @param position - User position data from Spoke\n * @returns true if the position has any debt\n */\nexport function hasDebtFromPosition(position: AaveSpokeUserPosition): boolean {\n return position.drawnShares > 0n || position.premiumShares > 0n;\n}\n","/**\n * Aave Spoke Client - Read operations\n *\n * Provides read operations for interacting with Aave v4 Spoke contracts.\n * Used to fetch live user position data (debt, collateral) from the Core Spoke.\n *\n * Note: Reserve data should be fetched from the indexer via fetchReserves.ts\n * since it doesn't need to be live and benefits from caching.\n */\n\nimport type { Address, PublicClient } from \"viem\";\n\nimport type {\n AaveSpokeUserAccountData,\n AaveSpokeUserPosition,\n} from \"../types.js\";\nimport { hasDebtFromPosition } from \"../utils/debtUtils.js\";\nimport AaveSpokeABI from \"./abis/AaveSpoke.abi.json\";\n\n/** Account data result type from contract */\ntype AccountDataResult = {\n riskPremium: bigint;\n avgCollateralFactor: bigint;\n healthFactor: bigint;\n totalCollateralValue: bigint;\n totalDebtValueRay: bigint;\n activeCollateralCount: bigint;\n borrowCount: bigint;\n};\n\n/** Position result type from contract */\ntype PositionResult = {\n drawnShares: bigint;\n premiumShares: bigint;\n premiumOffsetRay: bigint;\n suppliedShares: bigint;\n dynamicConfigKey: number;\n};\n\n/**\n * Maps contract result to AaveSpokeUserPosition\n */\nfunction mapPositionResult(result: PositionResult): AaveSpokeUserPosition {\n return {\n drawnShares: result.drawnShares,\n premiumShares: result.premiumShares,\n premiumOffsetRay: result.premiumOffsetRay,\n suppliedShares: result.suppliedShares,\n dynamicConfigKey: result.dynamicConfigKey,\n };\n}\n\n/**\n * Get aggregated user account health data from AAVE spoke.\n *\n * **Live data** - Fetches real-time account health including health factor, total collateral,\n * and total debt across all reserves. Values are calculated on-chain using AAVE oracles\n * and are the authoritative source for liquidation decisions.\n *\n * @param publicClient - Viem public client for reading contracts (from `createPublicClient()`)\n * @param spokeAddress - AAVE Spoke contract address (BTC Vault Core Spoke for vBTC collateral)\n * @param userAddress - User's proxy contract address (NOT user's wallet address)\n * @returns User account data with health metrics, collateral, and debt values\n *\n * @example\n * ```typescript\n * import { getUserAccountData } from \"@babylonlabs-io/ts-sdk/tbv/integrations/aave\";\n * import { createPublicClient, http } from \"viem\";\n * import { sepolia } from \"viem/chains\";\n *\n * const publicClient = createPublicClient({\n * chain: sepolia,\n * transport: http()\n * });\n *\n * const accountData = await getUserAccountData(\n * publicClient,\n * \"0x123...\", // AAVE Spoke address\n * \"0x456...\" // User's AAVE proxy address (from getPosition)\n * );\n *\n * console.log(\"Health Factor:\", accountData.healthFactor);\n * console.log(\"Collateral (USD):\", accountData.totalCollateralValue);\n * console.log(\"Debt (USD):\", accountData.totalDebtValueRay);\n * ```\n *\n * @remarks\n * **Return values:**\n * - `healthFactor` - WAD format (1e18 = 1.0). Below 1.0 = liquidatable\n * - `totalCollateralValue` - USD value in base currency (1e26 = $1)\n * - `totalDebtValueRay` - USD value in RAY-scaled base currency (1e35 = $1)\n * - `avgCollateralFactor` - Weighted average collateral factor in WAD (1e18 = 100%)\n * - `riskPremium` - Additional risk premium\n *\n * **Use cases:**\n * - Check liquidation risk before borrowing\n * - Calculate safe borrow amount\n * - Monitor position health\n * - Display UI health indicators\n */\nexport async function getUserAccountData(\n publicClient: PublicClient,\n spokeAddress: Address,\n userAddress: Address,\n): Promise<AaveSpokeUserAccountData> {\n const result = await publicClient.readContract({\n address: spokeAddress,\n abi: AaveSpokeABI,\n functionName: \"getUserAccountData\",\n args: [userAddress],\n });\n\n const data = result as AccountDataResult;\n return {\n riskPremium: data.riskPremium,\n avgCollateralFactor: data.avgCollateralFactor,\n healthFactor: data.healthFactor,\n totalCollateralValue: data.totalCollateralValue,\n totalDebtValueRay: data.totalDebtValueRay,\n activeCollateralCount: data.activeCollateralCount,\n borrowCount: data.borrowCount,\n };\n}\n\n/**\n * Get user position from the Spoke\n *\n * This fetches live data from the contract because debt accrues interest\n * and needs to be current for accurate health factor calculations.\n *\n * @param publicClient - Viem public client for reading contracts\n * @param spokeAddress - Aave Spoke contract address\n * @param reserveId - Reserve ID\n * @param userAddress - User's proxy contract address\n * @returns User position data\n */\nexport async function getUserPosition(\n publicClient: PublicClient,\n spokeAddress: Address,\n reserveId: bigint,\n userAddress: Address,\n): Promise<AaveSpokeUserPosition> {\n const result = await publicClient.readContract({\n address: spokeAddress,\n abi: AaveSpokeABI,\n functionName: \"getUserPosition\",\n args: [reserveId, userAddress],\n });\n\n return mapPositionResult(result as PositionResult);\n}\n\n/**\n * Check if a user has any debt in a reserve\n *\n * @param publicClient - Viem public client for reading contracts\n * @param spokeAddress - Aave Spoke contract address\n * @param reserveId - Reserve ID\n * @param userAddress - User's proxy contract address\n * @returns true if user has debt\n */\nexport async function hasDebt(\n publicClient: PublicClient,\n spokeAddress: Address,\n reserveId: bigint,\n userAddress: Address,\n): Promise<boolean> {\n const position = await getUserPosition(\n publicClient,\n spokeAddress,\n reserveId,\n userAddress,\n );\n return hasDebtFromPosition(position);\n}\n\n/**\n * Check if a user has supplied collateral in a reserve\n *\n * @param publicClient - Viem public client for reading contracts\n * @param spokeAddress - Aave Spoke contract address\n * @param reserveId - Reserve ID\n * @param userAddress - User's proxy contract address\n * @returns true if user has supplied collateral\n */\nexport async function hasCollateral(\n publicClient: PublicClient,\n spokeAddress: Address,\n reserveId: bigint,\n userAddress: Address,\n): Promise<boolean> {\n const position = await getUserPosition(\n publicClient,\n spokeAddress,\n reserveId,\n userAddress,\n );\n return position.suppliedShares > 0n;\n}\n\n/**\n * Get user's exact total debt in a reserve (token units, not shares).\n *\n * Returns the precise amount owed including accrued interest. Essential for full repayment.\n * Debt accrues interest every block, so this must be fetched live from the contract.\n *\n * @param publicClient - Viem public client for reading contracts\n * @param spokeAddress - AAVE Spoke contract address\n * @param reserveId - Reserve ID for the debt asset (e.g., `2n` for USDC)\n * @param userAddress - User's proxy contract address\n * @returns Total debt amount in token units (e.g., for USDC: `100000000n` = 100 USDC)\n *\n * @example\n * ```typescript\n * import { getUserTotalDebt, FULL_REPAY_BUFFER_DIVISOR } from \"@babylonlabs-io/ts-sdk/tbv/integrations/aave\";\n * import { formatUnits } from \"viem\";\n *\n * const totalDebt = await getUserTotalDebt(\n * publicClient,\n * AAVE_SPOKE_ADDRESS,\n * 2n, // USDC reserve\n * proxyAddress\n * );\n *\n * // For full repayment, add buffer to account for interest accrual\n * const repayAmount = totalDebt + (totalDebt / FULL_REPAY_BUFFER_DIVISOR);\n *\n * console.log(\"Debt:\", formatUnits(totalDebt, 6), \"USDC\");\n * ```\n *\n * @remarks\n * **Important for full repayment:**\n * - Add `FULL_REPAY_BUFFER_DIVISOR` buffer to account for interest between fetch and tx execution\n * - Contract only takes what's owed; excess stays in wallet\n * - For partial repayment, use any amount less than total debt\n */\nexport async function getUserTotalDebt(\n publicClient: PublicClient,\n spokeAddress: Address,\n reserveId: bigint,\n userAddress: Address,\n): Promise<bigint> {\n const result = await publicClient.readContract({\n address: spokeAddress,\n abi: AaveSpokeABI,\n functionName: \"getUserTotalDebt\",\n args: [reserveId, userAddress],\n });\n\n return result as bigint;\n}\n\n/** Result type from the `getReserve` contract call.\n *\n * Matches the on-chain `Reserve` struct defined in `ITBVAaveSpoke.sol`:\n * struct Reserve {\n * address underlying;\n * address hub;\n * uint16 assetId;\n * uint8 decimals;\n * uint24 collateralRisk;\n * ReserveFlags flags; // uint8 bitmap\n * uint32 dynamicConfigKey;\n * }\n *\n * Note: this is the `Reserve` struct, NOT `ReserveConfig` — the contract\n * exposes both as separate functions and they return different shapes.\n */\ntype ReserveResult = {\n underlying: Address;\n hub: Address;\n assetId: number;\n decimals: number;\n collateralRisk: number;\n flags: number;\n dynamicConfigKey: number;\n};\n\n/**\n * Get reserve data from the Core Spoke contract via the `getReserve` selector.\n *\n * Returns static reserve properties including the `dynamicConfigKey` needed\n * for `getDynamicReserveConfig` calls. Use this as a fallback when reserve\n * data is not available from the GraphQL indexer.\n *\n * Do NOT confuse with the contract's separate `getReserveConfig` function,\n * which returns `{collateralRisk, paused, frozen, borrowable, receiveSharesEnabled}`.\n *\n * @param publicClient - Viem public client for reading contracts\n * @param spokeAddress - Core Spoke contract address\n * @param reserveId - Reserve ID\n * @returns Reserve data including `dynamicConfigKey`\n */\nexport async function getReserve(\n publicClient: PublicClient,\n spokeAddress: Address,\n reserveId: bigint,\n): Promise<ReserveResult> {\n const result = await publicClient.readContract({\n address: spokeAddress,\n abi: AaveSpokeABI,\n functionName: \"getReserve\",\n args: [reserveId],\n });\n return result as ReserveResult;\n}\n\n/** Result type from getLiquidationConfig contract call */\ntype LiquidationConfigResult = {\n targetHealthFactor: bigint;\n healthFactorForMaxBonus: bigint;\n liquidationBonusFactor: bigint;\n};\n\n/** Result type from getDynamicReserveConfig contract call */\ntype DynamicReserveConfigResult = {\n collateralFactor: bigint;\n maxLiquidationBonus: bigint;\n liquidationFee: bigint;\n};\n\n/**\n * Get the target health factor (THF) from the Core Spoke contract.\n *\n * Per-spoke governance parameter. After a liquidation, the protocol targets\n * restoring the position to this health factor.\n *\n * @param publicClient - Viem public client for reading contracts\n * @param spokeAddress - Core Spoke contract address\n * @returns Target health factor in WAD (1e18 = 1.0). Example: 1.10 = 1_100_000_000_000_000_000n\n */\nexport async function getTargetHealthFactor(\n publicClient: PublicClient,\n spokeAddress: Address,\n): Promise<bigint> {\n const result = await publicClient.readContract({\n address: spokeAddress,\n abi: AaveSpokeABI,\n functionName: \"getLiquidationConfig\",\n });\n const config = result as LiquidationConfigResult;\n return config.targetHealthFactor;\n}\n\n/**\n * Get the dynamic reserve config from the Core Spoke contract.\n *\n * Returns collateral factor, max liquidation bonus, and liquidation fee\n * for a specific reserve and dynamic config key.\n *\n * @param publicClient - Viem public client for reading contracts\n * @param spokeAddress - Core Spoke contract address\n * @param reserveId - Reserve ID (e.g., vBTC reserve ID from indexer config)\n * @param dynamicConfigKey - Dynamic config key (from reserve data)\n * @returns Dynamic reserve config with collateralFactor (BPS), maxLiquidationBonus (BPS), liquidationFee (BPS)\n */\nexport async function getDynamicReserveConfig(\n publicClient: PublicClient,\n spokeAddress: Address,\n reserveId: bigint,\n dynamicConfigKey: number,\n): Promise<DynamicReserveConfigResult> {\n const result = await publicClient.readContract({\n address: spokeAddress,\n abi: AaveSpokeABI,\n functionName: \"getDynamicReserveConfig\",\n args: [reserveId, dynamicConfigKey],\n });\n return result as DynamicReserveConfigResult;\n}\n","/**\n * Aave Integration Adapter - Transaction builders\n *\n * Provides transaction builders for the AaveIntegrationAdapter contract.\n * Only includes Core Spoke operations for regular users (no Arbitrageur operations).\n *\n * These functions return unsigned transaction parameters that can be executed\n * by the vault service using its wallet client and transaction factory.\n */\n\nimport { type Address, type Hex, encodeFunctionData } from \"viem\";\n\nimport { AAVE_FUNCTION_NAMES } from \"../config.js\";\nimport type { TransactionParams } from \"../types.js\";\nimport AaveIntegrationAdapterABI from \"./abis/AaveIntegrationAdapter.abi.json\";\n\n/**\n * Build transaction to reorder vaults for liquidation priority.\n *\n * The permuted array must contain exactly the same vault IDs as the\n * current position, in the desired new order. Vaults are seized in\n * prefix order (index 0 first) during liquidation.\n *\n * @param contractAddress - AaveIntegrationAdapter contract address\n * @param permutedVaultIds - Vault IDs in desired new order (must be a permutation of current vaults)\n * @returns Unsigned transaction parameters\n */\nexport function buildReorderVaultsTx(\n contractAddress: Address,\n permutedVaultIds: Hex[],\n): TransactionParams {\n const data = encodeFunctionData({\n abi: AaveIntegrationAdapterABI,\n functionName: AAVE_FUNCTION_NAMES.REORDER_VAULTS,\n args: [permutedVaultIds],\n });\n\n return {\n to: contractAddress,\n data,\n };\n}\n\n/**\n * Build transaction to withdraw selected vaults from AAVE position.\n *\n * Withdraws specific vaults (partial withdrawal) and redeems them back to the depositor.\n * **Requires zero debt** - position must have no outstanding borrows.\n *\n * @param contractAddress - AaveIntegrationAdapter contract address\n * @param vaultIds - Array of vault IDs (bytes32) to withdraw\n * @returns Unsigned transaction parameters for execution with viem wallet\n */\nexport function buildWithdrawCollateralsTx(\n contractAddress: Address,\n vaultIds: Hex[],\n): TransactionParams {\n const data = encodeFunctionData({\n abi: AaveIntegrationAdapterABI,\n functionName: AAVE_FUNCTION_NAMES.WITHDRAW_COLLATERALS,\n args: [vaultIds],\n });\n\n return {\n to: contractAddress,\n data,\n };\n}\n\n/**\n * Build transaction to borrow assets against vBTC collateral.\n *\n * Borrows stablecoins (e.g., USDC) against your BTC collateral position.\n * Health factor must remain above 1.0 after borrowing, otherwise transaction will revert.\n *\n * @param contractAddress - AaveIntegrationAdapter contract address\n * @param debtReserveId - AAVE reserve ID for the debt asset (e.g., `2n` for USDC reserve)\n * @param amount - Amount to borrow in token units with decimals (e.g., for USDC with 6 decimals: `100000000n` = 100 USDC). Use `parseUnits()` from viem.\n * @param receiver - Address to receive borrowed tokens (usually user's address)\n * @returns Unsigned transaction parameters for execution with viem wallet\n *\n * @example\n * ```typescript\n * import { buildBorrowTx } from \"@babylonlabs-io/ts-sdk/tbv/integrations/aave\";\n * import { parseUnits } from \"viem\";\n *\n * // Borrow 100 USDC (6 decimals)\n * const borrowAmount = parseUnits(\"100\", 6);\n *\n * const txParams = buildBorrowTx(\n * \"0x123...\", // Adapter address\n * 2n, // USDC reserve ID\n * borrowAmount,\n * \"0x456...\" // Receiver address\n * );\n *\n * const hash = await walletClient.sendTransaction({\n * to: txParams.to,\n * data: txParams.data,\n * chain: sepolia,\n * });\n * ```\n *\n * @remarks\n * **What happens on-chain:**\n * 1. Checks health factor won't drop below liquidation threshold (1.0)\n * 2. Mints debt tokens to user's proxy contract\n * 3. Transfers borrowed asset to receiver address\n * 4. Updates position debt\n * 5. Emits `Borrowed` event\n *\n * **Possible errors:**\n * - Borrow would make health factor < 1.0\n * - Insufficient collateral\n * - Reserve doesn't exist\n * - Position doesn't exist\n *\n * **Important:** Calculate safe borrow amount using `calculateHealthFactor()` to avoid liquidation.\n */\nexport function buildBorrowTx(\n contractAddress: Address,\n debtReserveId: bigint,\n amount: bigint,\n receiver: Address,\n): TransactionParams {\n const data = encodeFunctionData({\n abi: AaveIntegrationAdapterABI,\n functionName: AAVE_FUNCTION_NAMES.BORROW,\n args: [debtReserveId, amount, receiver],\n });\n\n return {\n to: contractAddress,\n data,\n };\n}\n\n/**\n * Build transaction to repay debt on AAVE position.\n *\n * **Requires token approval** - user must approve adapter to spend debt token first.\n * Repays borrowed assets (partial or full repayment supported).\n *\n * @param contractAddress - AaveIntegrationAdapter contract address\n * @param borrower - Borrower's address (for self-repay, use connected wallet address)\n * @param debtReserveId - AAVE reserve ID for the debt asset\n * @param amount - Amount to repay in token units. Can repay partial or full debt. For full repay, use `getUserTotalDebt()` to get exact amount.\n * @returns Unsigned transaction parameters for execution with viem wallet\n *\n * @example\n * ```typescript\n * import { buildRepayTx } from \"@babylonlabs-io/ts-sdk/tbv/integrations/aave\";\n *\n * // Build repay transaction (self-repay)\n * const txParams = buildRepayTx(\n * AAVE_ADAPTER,\n * borrowerAddress, // Connected wallet address for self-repay\n * USDC_RESERVE_ID,\n * repayAmount\n * );\n *\n * const hash = await walletClient.sendTransaction({\n * to: txParams.to,\n * data: txParams.data,\n * chain: sepolia,\n * });\n * ```\n *\n * @remarks\n * **What happens on-chain:**\n * 1. Transfers tokens from user to adapter (requires approval)\n * 2. Burns debt tokens from user's proxy\n * 3. Updates position debt\n * 4. Emits `Repaid` event\n *\n * **Possible errors:**\n * - Insufficient token approval\n * - User doesn't have enough tokens\n * - Repay amount exceeds debt\n * - Position doesn't exist\n */\nexport function buildRepayTx(\n contractAddress: Address,\n borrower: Address,\n debtReserveId: bigint,\n amount: bigint,\n): TransactionParams {\n const data = encodeFunctionData({\n abi: AaveIntegrationAdapterABI,\n functionName: AAVE_FUNCTION_NAMES.REPAY,\n args: [borrower, debtReserveId, amount],\n });\n\n return {\n to: contractAddress,\n data,\n };\n}\n\n","/**\n * Aave Value Conversion Utilities\n *\n * Converts Aave on-chain values to human-readable numbers.\n */\n\nimport {\n AAVE_BASE_CURRENCY_DECIMALS,\n AAVE_BASE_CURRENCY_RAY_DECIMALS,\n WAD_DECIMALS,\n} from \"../constants.js\";\n\n/**\n * Convert Aave base currency value to USD\n *\n * Aave uses 1e26 = $1 USD for collateral values.\n *\n * @param value - Value in Aave base currency (1e26 = $1)\n * @returns Value in USD\n */\nexport function aaveValueToUsd(value: bigint): number {\n return Number(value) / 10 ** AAVE_BASE_CURRENCY_DECIMALS;\n}\n\n/**\n * Convert Aave RAY-scaled base currency value to USD\n *\n * Debt values use higher precision: 1e35 = $1 USD.\n *\n * @param value - Value in RAY-scaled base currency (1e35 = $1)\n * @returns Value in USD\n */\nexport function aaveRayValueToUsd(value: bigint): number {\n return Number(value) / 10 ** AAVE_BASE_CURRENCY_RAY_DECIMALS;\n}\n\n/**\n * Convert Aave WAD value to number\n *\n * WAD is used for health factor and collateral factor (1e18 = 1.0).\n *\n * @param value - Value in WAD (1e18 = 1.0)\n * @returns Decimal number\n */\nexport function wadToNumber(value: bigint): number {\n return Number(value) / 10 ** WAD_DECIMALS;\n}\n","/**\n * Borrow Ratio Utilities\n *\n * Borrow ratio = debt / collateral as a percentage.\n * Shows how much of the collateral is being used for borrowing.\n */\n\n/**\n * Calculate borrow ratio (debt / collateral) as percentage string\n *\n * @param debtUsd - Total debt in USD\n * @param collateralValueUsd - Total collateral value in USD\n * @returns Formatted percentage string (e.g., \"15.7%\")\n */\nexport function calculateBorrowRatio(\n debtUsd: number,\n collateralValueUsd: number,\n): string {\n if (collateralValueUsd <= 0) return \"0%\";\n const ratio = (debtUsd / collateralValueUsd) * 100;\n return `${ratio.toFixed(1)}%`;\n}\n","/**\n * Health Factor Utilities for Aave\n *\n * Health factor is calculated by Aave on-chain using oracle prices.\n * A health factor below 1.0 means the position can be liquidated.\n *\n * Status thresholds:\n * - no_debt: No active debt (null health factor)\n * - danger: < 1.0 (can be liquidated)\n * - warning: < HEALTH_FACTOR_WARNING_THRESHOLD (at risk)\n * - safe: >= HEALTH_FACTOR_WARNING_THRESHOLD (healthy)\n */\n\nimport { BPS_SCALE, HEALTH_FACTOR_WARNING_THRESHOLD } from \"../constants.js\";\n\nexport type HealthFactorStatus = \"safe\" | \"warning\" | \"danger\" | \"no_debt\";\n\n/**\n * Determine health factor status for UI display\n *\n * @param healthFactor - The health factor as a number (null if no debt)\n * @param hasDebt - Whether the position has active debt\n * @returns The status classification\n */\nexport function getHealthFactorStatus(\n healthFactor: number | null,\n hasDebt: boolean,\n): HealthFactorStatus {\n if (!hasDebt) return \"no_debt\";\n if (healthFactor === null) return \"safe\";\n if (healthFactor < 1.0) return \"danger\";\n if (healthFactor < HEALTH_FACTOR_WARNING_THRESHOLD) return \"warning\";\n return \"safe\";\n}\n\n/**\n * Checks if a health factor value represents a healthy position.\n *\n * @param healthFactor - The health factor as a number\n * @returns true if the health factor is >= 1.0 (healthy), false otherwise\n */\nexport function isHealthFactorHealthy(healthFactor: number | null): boolean {\n if (healthFactor === null) {\n return true; // No debt = healthy\n }\n return healthFactor >= 1.0;\n}\n\n/**\n * Get health factor status from a numeric value.\n * Used for UI components that work with Infinity for no-debt scenarios.\n *\n * @param value - Health factor value (Infinity when no debt)\n * @returns The status classification\n */\nexport function getHealthFactorStatusFromValue(\n value: number,\n): HealthFactorStatus {\n const hasDebt = isFinite(value);\n const healthFactor = isFinite(value) ? value : null;\n return getHealthFactorStatus(healthFactor, hasDebt);\n}\n\n/**\n * Calculate health factor for an AAVE position.\n *\n * **Formula:** `HF = (Collateral × Liquidation Threshold) / Total Debt`\n *\n * Health factor determines liquidation risk:\n * - `>= 1.5` - Safe (green)\n * - `1.0 - 1.5` - Warning (amber)\n * - `< 1.0` - Danger, position can be liquidated (red)\n *\n * @param collateralValueUsd - Total collateral value in USD (as number, not bigint)\n * @param totalDebtUsd - Total debt value in USD (as number, not bigint)\n * @param liquidationThresholdBps - Liquidation threshold in basis points (e.g., `8000` = 80%)\n * @returns Health factor value (e.g., `1.5`), or `Infinity` if no debt\n *\n * @example\n * ```typescript\n * import { calculateHealthFactor, HEALTH_FACTOR_WARNING_THRESHOLD } from \"@babylonlabs-io/ts-sdk/tbv/integrations/aave\";\n *\n * // User has $10,000 BTC collateral, $5,000 debt, 80% LT\n * const hf = calculateHealthFactor(10000, 5000, 8000);\n * // Result: 1.6 (safe to borrow more)\n *\n * if (hf < 1.0) {\n * console.error(\"Position can be liquidated!\");\n * } else if (hf < HEALTH_FACTOR_WARNING_THRESHOLD) {\n * console.warn(\"Position at risk, consider repaying\");\n * } else {\n * console.log(\"Position is safe\");\n * }\n * ```\n *\n * @remarks\n * **Before borrowing:**\n * Use this to calculate resulting health factor and ensure it stays above safe threshold.\n *\n * **Unit conversions:**\n * - Convert AAVE base currency (1e26) to USD by dividing by 1e26\n * - Use `aaveValueToUsd()` helper for automatic conversion\n */\nexport function calculateHealthFactor(\n collateralValueUsd: number,\n totalDebtUsd: number,\n liquidationThresholdBps: number,\n): number {\n if (totalDebtUsd <= 0) return Infinity;\n return (\n (collateralValueUsd * (liquidationThresholdBps / BPS_SCALE)) / totalDebtUsd\n );\n}\n","/**\n * Vault Selection Utilities for Aave\n *\n * Provides functions for selecting vaults to match a target collateral amount.\n * Uses a greedy algorithm that prioritizes larger vaults first.\n */\n\nexport interface SelectableVault {\n id: string;\n amount: number;\n}\n\nexport interface VaultSelectionResult {\n /** IDs of selected vaults */\n vaultIds: string[];\n /** Actual total amount from selected vaults */\n actualAmount: number;\n}\n\n/**\n * Select vaults to match the target amount using a greedy algorithm.\n * Sorts vaults by amount descending and picks until target is met.\n *\n * @param vaults - Available vaults to select from\n * @param targetAmount - Target amount to reach\n * @returns Selected vault IDs and actual amount\n */\nexport function selectVaultsForAmount(\n vaults: SelectableVault[],\n targetAmount: number,\n): VaultSelectionResult {\n if (targetAmount <= 0) {\n return { vaultIds: [], actualAmount: 0 };\n }\n\n const sortedVaults = [...vaults].sort((a, b) => b.amount - a.amount);\n\n const selectedIds: string[] = [];\n let selectedAmount = 0;\n\n for (const vault of sortedVaults) {\n if (selectedAmount >= targetAmount) break;\n selectedIds.push(vault.id);\n selectedAmount += vault.amount;\n }\n\n return { vaultIds: selectedIds, actualAmount: selectedAmount };\n}\n\n/**\n * Calculate total amount from a list of vaults\n *\n * @param vaults - Vaults to sum\n * @returns Total amount in BTC\n */\nexport function calculateTotalVaultAmount(vaults: SelectableVault[]): number {\n return vaults.reduce((sum, vault) => sum + vault.amount, 0);\n}\n","/**\n * Cascade Liquidation Simulation\n *\n * Simulates multi-group liquidation cascades for Aave positions backed by\n * indivisible BTC vaults (UTXOs). Each liquidation event seizes a prefix of\n * vaults until the target seizure is covered, then debt is reduced and the\n * cascade continues with remaining vaults.\n *\n * Used by the optimizer to score different vault orderings by how much\n * collateral survives the cascade.\n */\n\n/**\n * Minimal vault shape for cascade simulation.\n * UI layers extend this with display fields (e.g. `name`).\n */\nexport interface CascadeVault {\n id: string;\n btc: number;\n}\n\n/** 1% tolerance for prefix walk coverage — avoids cliff flip at boundary */\nexport const SEIZURE_TOL = 0.01;\n\n/** Circuit breaker for group cascade loop */\nexport const MAX_GROUPS = 20;\n\n/** Minimum debt threshold to continue cascade (avoids infinite loop on dust) */\nexport const MIN_DEBT_THRESHOLD = 0.01;\n\n/**\n * Prefix walk: consume vaults front-to-back until target seizure is covered.\n * Returns the vaults in the first liquidation group.\n */\nexport function getGroup1FromOrder<T extends CascadeVault>(\n order: T[],\n seizedFraction: number,\n seizureTol: number,\n): T[] {\n if (order.length === 0) return [];\n const totalBtc = order.reduce((s, v) => s + v.btc, 0);\n const coverThreshold = totalBtc * seizedFraction * (1 - seizureTol);\n let prefixSum = 0;\n let i = 0;\n while (i < order.length && prefixSum < coverThreshold) {\n prefixSum += order[i].btc;\n i++;\n }\n return order.slice(0, i);\n}\n\n/**\n * Simulate one liquidation group, returning updated debt and remaining BTC.\n * Handles both safe (non-last) and full (last) liquidation paths.\n */\nfunction simulateOneGroup(\n seizedBtc: number,\n totalBtc: number,\n debt: number,\n isLastGroup: boolean,\n seizedFraction: number,\n CF: number,\n THF: number,\n maxLB: number,\n expectedHF: number,\n): { debtAfter: number; btcAfter: number } {\n const liqPenalty = maxLB * CF;\n const pLiq = debt / (totalBtc * CF);\n const targetSeizure = totalBtc * seizedFraction;\n const overSeizureBtc = Math.max(0, seizedBtc - targetSeizure);\n const denominator = THF - liqPenalty;\n const debtToRepay =\n denominator === 0 ? debt : debt * ((THF - expectedHF) / denominator);\n\n let debtAfter: number;\n if (isLastGroup) {\n debtAfter = 0;\n } else {\n const overSeizureVal = (overSeizureBtc * pLiq) / maxLB;\n const fairnessDebtRepay = Math.min(overSeizureVal, debt - debtToRepay);\n debtAfter = Math.max(0, debt - debtToRepay - fairnessDebtRepay);\n }\n return { debtAfter, btcAfter: Math.max(0, totalBtc - seizedBtc) };\n}\n\n/**\n * Simulate full liquidation cascade with debt model.\n *\n * PRIMARY score: sumBtcAfterEvents — sum of BTC remaining after every event.\n * Captures how much collateral survives at each stage.\n * TIEBREAKER: btcAfterG1 — BTC remaining after the first (most likely) event.\n */\nexport function simulateCascade<T extends CascadeVault>(\n order: T[],\n totalDebt: number,\n seizedFraction: number,\n seizureTol: number,\n CF: number,\n THF: number,\n maxLB: number,\n expectedHF: number,\n): { sumBtcAfterEvents: number; btcAfterG1: number } {\n let remaining = [...order];\n let debt = totalDebt;\n const initialTotalBtc = order.reduce((s, v) => s + v.btc, 0);\n let btcAfterG1 = -1;\n let sumBtcAfterEvents = 0;\n let groupCount = 0;\n\n while (\n remaining.length > 0 &&\n debt > MIN_DEBT_THRESHOLD &&\n groupCount < MAX_GROUPS\n ) {\n const totalBtc = remaining.reduce((s, v) => s + v.btc, 0);\n const coverThreshold = totalBtc * seizedFraction * (1 - seizureTol);\n let prefixSum = 0;\n let i = 0;\n while (i < remaining.length && prefixSum < coverThreshold) {\n prefixSum += remaining[i].btc;\n i++;\n }\n const isLastGroup = i >= remaining.length;\n const { debtAfter } = simulateOneGroup(\n prefixSum,\n totalBtc,\n debt,\n isLastGroup,\n seizedFraction,\n CF,\n THF,\n maxLB,\n expectedHF,\n );\n remaining = remaining.slice(i);\n debt = debtAfter;\n const btcNow = totalBtc - prefixSum;\n sumBtcAfterEvents += btcNow;\n if (btcAfterG1 < 0) btcAfterG1 = btcNow;\n groupCount++;\n }\n\n return {\n sumBtcAfterEvents,\n btcAfterG1:\n btcAfterG1 < 0 ? initialTotalBtc : btcAfterG1,\n };\n}\n","/**\n * Optimal Vault Ordering for Liquidation Protection\n *\n * Finds the vault ordering that maximizes collateral surviving a multi-group\n * liquidation cascade. Uses exhaustive bitmask enumeration (2^n for n ≤ 20)\n * to find the minimum-BTC subset covering the target seizure for Group 1,\n * then greedy for subsequent groups.\n *\n * The iterative refinement loop re-runs the optimizer on its own output,\n * converging in ≤3 iterations in practice.\n */\n\nimport { simulateCascade, type CascadeVault } from \"./cascadeSimulation.js\";\n\n/** Convergence threshold for iterative refinement */\nconst EPS = 0.0001;\n\n/** Max refinement iterations for optimizer */\nconst MAX_ITER = 5;\n\n/** Max vaults for exhaustive bitmask search */\nconst MAX_VAULTS_FOR_EXHAUSTIVE = 20;\n\n/**\n * Greedy sub-order: arrange remaining vaults by finding minimum BTC subset\n * covering target for each group, using exhaustive search when feasible.\n */\nfunction greedyOrder<T extends CascadeVault>(\n vaults: T[],\n seizedFraction: number,\n seizureTol: number,\n): T[] {\n const result: T[] = [];\n let remaining = [...vaults];\n\n while (remaining.length > 0) {\n const totalBtc = remaining.reduce((s, v) => s + v.btc, 0);\n const coverThreshold = totalBtc * seizedFraction * (1 - seizureTol);\n let optSubset: T[] = [...remaining];\n let optSum = totalBtc;\n const m = remaining.length;\n\n if (m <= MAX_VAULTS_FOR_EXHAUSTIVE) {\n for (let mask = 1; mask < 1 << m; mask++) {\n let sum = 0;\n const subset: T[] = [];\n for (let bit = 0; bit < m; bit++) {\n if (mask & (1 << bit)) {\n sum += remaining[bit].btc;\n subset.push(remaining[bit]);\n }\n }\n if (sum >= coverThreshold && sum < optSum) {\n optSum = sum;\n optSubset = subset;\n }\n }\n }\n\n const ids = new Set(optSubset.map((v) => v.id));\n result.push(...[...optSubset].sort((a, b) => b.btc - a.btc));\n remaining = remaining.filter((v) => !ids.has(v.id));\n }\n\n return result;\n}\n\n/**\n * Single-pass optimizer: enumerate all possible Group 1 subsets,\n * use greedy for remaining groups, pick the order that maximizes\n * collateral surviving the cascade.\n */\nfunction computeOptimalOrderSinglePass<T extends CascadeVault>(\n vaults: T[],\n totalDebt: number,\n seizedFraction: number,\n seizureTol: number,\n CF: number,\n THF: number,\n maxLB: number,\n expectedHF: number,\n): { order: T[]; sumBtcAfterEvents: number; btcAfterG1: number } {\n if (vaults.length === 0)\n return { order: [], sumBtcAfterEvents: 0, btcAfterG1: 0 };\n if (vaults.length > MAX_VAULTS_FOR_EXHAUSTIVE) {\n throw new Error(\n `Too many vaults for exhaustive search: ${vaults.length} exceeds maximum of ${MAX_VAULTS_FOR_EXHAUSTIVE}`,\n );\n }\n if (vaults.length === 1) {\n const sim = simulateCascade(\n vaults,\n totalDebt,\n seizedFraction,\n seizureTol,\n CF,\n THF,\n maxLB,\n expectedHF,\n );\n return { order: [...vaults], ...sim };\n }\n\n const n = vaults.length;\n const totalBtc = vaults.reduce((s, v) => s + v.btc, 0);\n const coverThreshold = totalBtc * seizedFraction * (1 - seizureTol);\n\n let bestOrder = [...vaults];\n let bestSum = -Infinity;\n let bestAfterG1 = -Infinity;\n\n for (let mask = 1; mask < 1 << n; mask++) {\n let g1Btc = 0;\n const g1: T[] = [];\n const rest: T[] = [];\n for (let bit = 0; bit < n; bit++) {\n if (mask & (1 << bit)) {\n g1Btc += vaults[bit].btc;\n g1.push(vaults[bit]);\n } else {\n rest.push(vaults[bit]);\n }\n }\n if (g1Btc < coverThreshold) continue;\n\n g1.sort((a, b) => b.btc - a.btc);\n const candidate = [...g1, ...greedyOrder(rest, seizedFraction, seizureTol)];\n const { sumBtcAfterEvents, btcAfterG1 } = simulateCascade(\n candidate,\n totalDebt,\n seizedFraction,\n seizureTol,\n CF,\n THF,\n maxLB,\n expectedHF,\n );\n\n if (\n sumBtcAfterEvents > bestSum + EPS ||\n (Math.abs(sumBtcAfterEvents - bestSum) <= EPS &&\n btcAfterG1 > bestAfterG1 + EPS)\n ) {\n bestSum = sumBtcAfterEvents;\n bestAfterG1 = btcAfterG1;\n bestOrder = candidate;\n }\n }\n\n return {\n order: bestOrder,\n sumBtcAfterEvents: bestSum,\n btcAfterG1: bestAfterG1,\n };\n}\n\n/**\n * Main optimizer: iterative refinement until stable.\n * Re-running with the improved order lets the next pass find better\n * G1 subsets. Converges in ≤3 iterations in practice.\n */\nexport function computeOptimalOrder<T extends CascadeVault>(\n vaults: T[],\n totalDebt: number,\n seizedFraction: number,\n seizureTol: number,\n CF: number,\n THF: number,\n maxLB: number,\n expectedHF: number,\n): { order: T[]; sumBtcAfterEvents: number; btcAfterG1: number } {\n let result = computeOptimalOrderSinglePass(\n vaults,\n totalDebt,\n seizedFraction,\n seizureTol,\n CF,\n THF,\n maxLB,\n expectedHF,\n );\n\n for (let i = 0; i < MAX_ITER; i++) {\n const refined = computeOptimalOrderSinglePass(\n result.order,\n totalDebt,\n seizedFraction,\n seizureTol,\n CF,\n THF,\n maxLB,\n expectedHF,\n );\n const primaryImproves =\n refined.sumBtcAfterEvents > result.sumBtcAfterEvents + EPS;\n const primaryTied =\n Math.abs(refined.sumBtcAfterEvents - result.sumBtcAfterEvents) <= EPS;\n const tiebreakerImproves =\n primaryTied && refined.btcAfterG1 > result.btcAfterG1 + EPS;\n if (!primaryImproves && !tiebreakerImproves) {\n break;\n }\n result = refined;\n }\n\n return result;\n}\n","/**\n * Vault Split Utilities for Aave Liquidation Protection\n *\n * BTC vaults are indivisible UTXOs. During liquidation, the protocol seizes\n * whole vaults as a prefix of the borrower's ordered vault list until the\n * target seizure amount is covered. Splitting deposits into 2 optimally-sized\n * vaults (sacrificial + protected) minimizes over-seizure loss.\n *\n * The sacrificial vault (index 0) is sized to cover the expected target seizure\n * plus a safety margin. The protected vault (index 1) holds the remainder and\n * survives liquidation.\n *\n * Seizure formula (from Aave v4 Section 4.2):\n * ```\n * liq_penalty = LB × CF\n * debt_to_repay = total_debt × (THF - current_HF) / (THF - liq_penalty)\n * target_seizure = debt_to_repay × LB\n * ```\n */\n\nconst MAX_SAFE_BIGINT = BigInt(Number.MAX_SAFE_INTEGER);\n\nexport function assertSafePrecision(value: bigint, name: string): void {\n if (value > MAX_SAFE_BIGINT) {\n throw new RangeError(\n `${name} (${value}) exceeds Number.MAX_SAFE_INTEGER; precision would be lost`,\n );\n }\n}\n\n/**\n * Parameters for computing the optimal vault split.\n */\nexport interface OptimalSplitParams {\n /** Total deposit amount in satoshis */\n totalBtc: bigint;\n /** Collateral factor (e.g. 0.75 for 75%) */\n CF: number;\n /** Liquidation bonus (e.g. 1.05 for 5% bonus) */\n LB: number;\n /** Target health factor (e.g. 1.10) */\n THF: number;\n /** Expected health factor at liquidation (e.g. 0.95) */\n expectedHF: number;\n /** Safety margin multiplier for the sacrificial vault (e.g. 1.05 for 5% buffer) */\n safetyMargin: number;\n}\n\n/**\n * Result of the optimal vault split computation.\n */\nexport interface OptimalSplitResult {\n /** Sacrificial vault amount in satoshis (index 0, seized first) */\n sacrificialVault: bigint;\n /** Protected vault amount in satoshis (index 1, survives liquidation) */\n protectedVault: bigint;\n /** Fraction of collateral that would be seized (0–1) */\n seizedFraction: number;\n /** Raw target seizure amount in satoshis (before safety margin) */\n targetSeizureBtc: bigint;\n}\n\n/**\n * Parameters for computing the minimum deposit required for a split.\n */\nexport interface MinDepositForSplitParams {\n /** Minimum peg-in amount in satoshis */\n minPegin: bigint;\n /** Seized fraction (0–1), from computeOptimalSplit or computeSeizedFraction */\n seizedFraction: number;\n /** Safety margin multiplier (e.g. 1.05) */\n safetyMargin: number;\n}\n\n/**\n * Parameters for checking if a vault rebalance is needed.\n */\nexport interface RebalanceCheckParams {\n /** Ordered vault amounts in satoshis (index 0 is sacrificial) */\n vaultAmounts: bigint[];\n /** Collateral factor (e.g. 0.75) */\n CF: number;\n /** Liquidation bonus (e.g. 1.05) */\n LB: number;\n /** Target health factor (e.g. 1.10) */\n THF: number;\n /** Expected health factor at liquidation (e.g. 0.95) */\n expectedHF: number;\n /** Safety margin multiplier (e.g. 1.05) */\n safetyMargin: number;\n}\n\n/**\n * Result of a vault rebalance check.\n */\nexport interface RebalanceCheckResult {\n /** Whether the sacrificial vault needs to be increased */\n needsRebalance: boolean;\n /** How much more the sacrificial vault needs in satoshis (0n if no rebalance needed) */\n deficit: bigint;\n /** Current sacrificial vault coverage in satoshis */\n currentCoverage: bigint;\n /** Required sacrificial vault coverage in satoshis */\n targetCoverage: bigint;\n}\n\n/**\n * Compute the fraction of collateral that would be seized during liquidation,\n * returning both the raw (unclamped) and clamped values.\n *\n * The raw value is useful for detecting unusual protocol parameter combinations\n * (values outside [0, 1] indicate something unexpected).\n *\n * Formula:\n * ```\n * liq_penalty = LB × CF\n * seized_fraction = CF × (THF - expectedHF) / (THF - liq_penalty) × LB / expectedHF\n * ```\n *\n * @param CF - Collateral factor (e.g. 0.75)\n * @param LB - Liquidation bonus (e.g. 1.05)\n * @param THF - Target health factor (e.g. 1.10)\n * @param expectedHF - Expected health factor at liquidation (e.g. 0.95)\n * @returns Both the raw seized fraction and the clamped [0, 1] value\n */\nexport function computeSeizedFractionDetailed(\n CF: number,\n LB: number,\n THF: number,\n expectedHF: number,\n): { seizedFraction: number; seizedFractionRaw: number } {\n // HF ≤ 0 means position is fully underwater — full seizure\n if (expectedHF <= 0) {\n return { seizedFraction: 1, seizedFractionRaw: Infinity };\n }\n\n const liqPenalty = LB * CF;\n\n // If THF <= liq_penalty, full liquidation is inevitable\n if (THF <= liqPenalty) {\n return { seizedFraction: 1, seizedFractionRaw: Infinity };\n }\n\n // Floating-point errors here are ~1e-15, negligible relative to the 5%\n // safety margin applied by callers (computeOptimalSplit, checkRebalanceNeeded).\n const seizedFractionRaw =\n ((CF * (THF - expectedHF)) / (THF - liqPenalty)) * (LB / expectedHF);\n\n return {\n seizedFraction: Math.max(0, Math.min(1, seizedFractionRaw)),\n seizedFractionRaw,\n };\n}\n\n/**\n * Compute the fraction of collateral that would be seized during liquidation.\n *\n * @param CF - Collateral factor (e.g. 0.75)\n * @param LB - Liquidation bonus (e.g. 1.05)\n * @param THF - Target health factor (e.g. 1.10)\n * @param expectedHF - Expected health factor at liquidation (e.g. 0.95)\n * @returns Seized fraction clamped to [0, 1]\n */\nexport function computeSeizedFraction(\n CF: number,\n LB: number,\n THF: number,\n expectedHF: number,\n): number {\n return computeSeizedFractionDetailed(CF, LB, THF, expectedHF).seizedFraction;\n}\n\n/**\n * Compute the optimal split between a sacrificial vault and a protected vault.\n *\n * The sacrificial vault (index 0) is sized to cover the target seizure amount\n * plus a safety margin. The protected vault (index 1) holds the remainder.\n *\n * @param params - Split parameters including total BTC, risk params, and safety margin\n * @returns Split result with vault sizes, seized fraction, and target seizure\n *\n * @example\n * ```typescript\n * import { computeOptimalSplit } from \"@babylonlabs-io/ts-sdk/tbv/integrations/aave\";\n *\n * const result = computeOptimalSplit({\n * totalBtc: 1_000_000_000n, // 10 BTC in sats\n * CF: 0.75,\n * LB: 1.05,\n * THF: 1.10,\n * expectedHF: 0.95,\n * safetyMargin: 1.05,\n * });\n * // result.sacrificialVault ≈ 418_000_000n (4.18 BTC)\n * // result.protectedVault ≈ 582_000_000n (5.82 BTC)\n * ```\n */\nexport function computeOptimalSplit(\n params: OptimalSplitParams,\n): OptimalSplitResult {\n const { totalBtc, CF, LB, THF, expectedHF, safetyMargin } = params;\n\n if (totalBtc <= 0n) {\n return {\n sacrificialVault: 0n,\n protectedVault: 0n,\n seizedFraction: 0,\n targetSeizureBtc: 0n,\n };\n }\n\n assertSafePrecision(totalBtc, \"totalBtc\");\n\n const seizedFraction = computeSeizedFraction(CF, LB, THF, expectedHF);\n\n const totalBtcNum = Number(totalBtc);\n const targetSeizureBtc = BigInt(Math.ceil(totalBtcNum * seizedFraction));\n\n const sacrificialRaw = BigInt(\n Math.ceil(totalBtcNum * seizedFraction * safetyMargin),\n );\n const sacrificialVault =\n sacrificialRaw > totalBtc ? totalBtc : sacrificialRaw;\n const protectedVault = totalBtc - sacrificialVault;\n\n return {\n sacrificialVault,\n protectedVault,\n seizedFraction,\n targetSeizureBtc,\n };\n}\n\n/**\n * Compute the minimum total deposit required for a 2-vault split.\n *\n * Both vaults must be at least `minPegin` satoshis. This function returns\n * the minimum total deposit where both the sacrificial and protected vaults\n * would meet the minimum peg-in requirement.\n *\n * @param params - Parameters including minimum peg-in, seized fraction, and safety margin\n * @returns Minimum total deposit in satoshis. Returns 0n in two cases:\n * - `seizedFraction * safetyMargin >= 1`: split impossible (sacrificial vault would consume entire deposit)\n * - `seizedFraction <= 0`: split not useful (no seizure expected at this health factor)\n *\n * @example\n * ```typescript\n * import { computeMinDepositForSplit } from \"@babylonlabs-io/ts-sdk/tbv/integrations/aave\";\n *\n * const minDeposit = computeMinDepositForSplit({\n * minPegin: 50_000n, // 0.0005 BTC\n * seizedFraction: 0.398,\n * safetyMargin: 1.05,\n * });\n * ```\n */\nexport function computeMinDepositForSplit(\n params: MinDepositForSplitParams,\n): bigint {\n const { minPegin, seizedFraction, safetyMargin } = params;\n\n assertSafePrecision(minPegin, \"minPegin\");\n\n const sacrificialShare = seizedFraction * safetyMargin;\n\n // If sacrificial vault would consume the entire deposit, split is not possible\n if (sacrificialShare >= 1) {\n return 0n;\n }\n\n // If seized fraction is effectively zero, split is not useful\n if (sacrificialShare <= 0) {\n return 0n;\n }\n\n // Minimum total so the protected vault (smaller share) >= minPegin\n const protectedShare = 1 - sacrificialShare;\n const minFromProtected = Math.ceil(Number(minPegin) / protectedShare);\n\n // Minimum total so the sacrificial vault >= minPegin\n const minFromSacrificial = Math.ceil(Number(minPegin) / sacrificialShare);\n\n return BigInt(Math.max(minFromProtected, minFromSacrificial));\n}\n\n/**\n * Check if the sacrificial vault (index 0) needs to be increased to cover\n * the current target seizure amount.\n *\n * **Scope:** This function only checks whether the sacrificial vault's sizing\n * is adequate. It does NOT detect whether a split exists — a single vault that\n * exceeds the target coverage returns `needsRebalance: false`. Callers should\n * check `vaultAmounts.length < 2` separately to detect unsplit positions.\n *\n * Used on position page load to detect when parameter changes (THF, CF, LB)\n * have made the current split insufficient.\n *\n * @param params - Current vault amounts and risk parameters\n * @returns Whether rebalance is needed, with deficit details\n *\n * @example\n * ```typescript\n * import { checkRebalanceNeeded } from \"@babylonlabs-io/ts-sdk/tbv/integrations/aave\";\n *\n * const result = checkRebalanceNeeded({\n * vaultAmounts: [300_000_000n, 700_000_000n], // 3 BTC sacrificial, 7 BTC protected\n * CF: 0.75,\n * LB: 1.05,\n * THF: 1.10,\n * expectedHF: 0.95,\n * safetyMargin: 1.05,\n * });\n *\n * if (result.needsRebalance) {\n * console.log(`Sacrificial vault needs ${result.deficit} more sats`);\n * }\n * ```\n */\nexport function checkRebalanceNeeded(\n params: RebalanceCheckParams,\n): RebalanceCheckResult {\n const { vaultAmounts, CF, LB, THF, expectedHF, safetyMargin } = params;\n\n if (vaultAmounts.length === 0) {\n return {\n needsRebalance: false,\n deficit: 0n,\n currentCoverage: 0n,\n targetCoverage: 0n,\n };\n }\n\n const totalBtc = vaultAmounts.reduce((sum, v) => sum + v, 0n);\n assertSafePrecision(totalBtc, \"totalBtc\");\n\n const seizedFraction = computeSeizedFraction(CF, LB, THF, expectedHF);\n\n const targetCoverage = BigInt(\n Math.ceil(Number(totalBtc) * seizedFraction * safetyMargin),\n );\n const currentCoverage = vaultAmounts[0];\n\n const deficit =\n targetCoverage > currentCoverage ? targetCoverage - currentCoverage : 0n;\n\n return {\n needsRebalance: deficit > 0n,\n deficit,\n currentCoverage,\n targetCoverage,\n };\n}\n","/**\n * Seizure Simulation Utilities\n *\n * Simulates which vaults would be seized during Aave liquidation.\n * Vaults are seized in prefix order (index 0, 1, 2, ...) until the\n * target seizure amount is covered.\n *\n * Reference: Aave v4 Section 4.2 — liquidation seizes a prefix of the\n * borrower's ordered vault list.\n */\n\nimport { assertSafePrecision, computeSeizedFraction } from \"./vaultSplit.js\";\n\n/**\n * A vault with its on-chain ID and BTC amount, in liquidation-priority order.\n */\nexport interface OrderedVault {\n /** On-chain vault ID (bytes32 hex string) */\n id: string;\n /** Vault amount in satoshis */\n amountSats: bigint;\n}\n\n/**\n * Parameters for simulating prefix seizure.\n */\nexport interface PrefixSeizureParams {\n /** Vaults in their current on-chain order (index 0 is seized first) */\n orderedVaults: OrderedVault[];\n /** Target seizure amount in satoshis */\n targetSeizureSats: bigint;\n}\n\n/**\n * Result of a prefix seizure simulation.\n */\nexport interface PrefixSeizureResult {\n /** Vaults that would be seized (the prefix) */\n seizedVaults: OrderedVault[];\n /** Vaults that survive liquidation */\n protectedVaults: OrderedVault[];\n /** Over-seizure amount in satoshis (total seized - target) */\n overSeizureSats: bigint;\n /** Index where seizure stops (exclusive: vaults[0..cutoffIndex] are seized) */\n cutoffIndex: number;\n /** Total amount seized in satoshis */\n totalSeizedSats: bigint;\n}\n\n/**\n * Parameters for computing target seizure in satoshis.\n */\nexport interface TargetSeizureParams {\n /** Total collateral in satoshis */\n totalCollateralSats: bigint;\n /** Collateral factor (e.g. 0.75) */\n CF: number;\n /** Liquidation bonus (e.g. 1.05) */\n LB: number;\n /** Target health factor (e.g. 1.10) */\n THF: number;\n /** Expected health factor at liquidation (e.g. 0.95) */\n expectedHF: number;\n}\n\n/**\n * Compute the target seizure amount in satoshis.\n *\n * Uses `computeSeizedFraction` to determine what fraction of total collateral\n * would be seized, then converts to an absolute satoshi amount.\n *\n * @param params - Total collateral and risk parameters\n * @returns Target seizure amount in satoshis (rounded up)\n *\n * @example\n * ```typescript\n * const targetSats = computeTargetSeizureSats({\n * totalCollateralSats: 1_000_000_000n, // 10 BTC\n * CF: 0.75,\n * LB: 1.05,\n * THF: 1.10,\n * expectedHF: 0.95,\n * });\n * // targetSats ≈ 398_000_000n (3.98 BTC)\n * ```\n */\nexport function computeTargetSeizureSats(\n params: TargetSeizureParams,\n): bigint {\n const { totalCollateralSats, CF, LB, THF, expectedHF } = params;\n\n if (totalCollateralSats <= 0n) {\n return 0n;\n }\n\n assertSafePrecision(totalCollateralSats, \"totalCollateralSats\");\n\n const seizedFraction = computeSeizedFraction(CF, LB, THF, expectedHF);\n\n return BigInt(Math.ceil(Number(totalCollateralSats) * seizedFraction));\n}\n\n/**\n * Simulate prefix seizure for a given set of ordered vaults.\n *\n * Walks the ordered vault list, accumulating amounts until the target\n * seizure is covered. Returns which vaults are seized vs protected,\n * the over-seizure amount, and the cutoff index.\n *\n * @param params - Ordered vaults and target seizure amount\n * @returns Seizure simulation result\n * @throws Error if orderedVaults is empty\n * @throws Error if targetSeizureSats is <= 0\n *\n * @example\n * ```typescript\n * const result = simulatePrefixSeizure({\n * orderedVaults: [\n * { id: \"0xabc...\", amountSats: 200_000_000n },\n * { id: \"0xdef...\", amountSats: 300_000_000n },\n * { id: \"0x123...\", amountSats: 500_000_000n },\n * ],\n * targetSeizureSats: 400_000_000n,\n * });\n * // result.seizedVaults = first 2 vaults (200M + 300M = 500M >= 400M)\n * // result.overSeizureSats = 100_000_000n\n * // result.cutoffIndex = 2\n * ```\n */\nexport function simulatePrefixSeizure(\n params: PrefixSeizureParams,\n): PrefixSeizureResult {\n const { orderedVaults, targetSeizureSats } = params;\n\n if (orderedVaults.length === 0) {\n throw new Error(\"orderedVaults must not be empty\");\n }\n\n if (targetSeizureSats <= 0n) {\n throw new Error(\n \"targetSeizureSats must be positive; use computeTargetSeizureSats to derive it\",\n );\n }\n\n let accumulated = 0n;\n let cutoffIndex = orderedVaults.length; // default: all vaults seized\n\n for (let i = 0; i < orderedVaults.length; i++) {\n accumulated += orderedVaults[i].amountSats;\n\n if (accumulated >= targetSeizureSats) {\n cutoffIndex = i + 1;\n break;\n }\n }\n\n const seizedVaults = orderedVaults.slice(0, cutoffIndex);\n const protectedVaults = orderedVaults.slice(cutoffIndex);\n const totalSeizedSats = accumulated;\n const overSeizureSats =\n totalSeizedSats > targetSeizureSats\n ? totalSeizedSats - targetSeizureSats\n : 0n;\n\n return {\n seizedVaults,\n protectedVaults,\n overSeizureSats,\n cutoffIndex,\n totalSeizedSats,\n };\n}\n"],"names":["AAVE_FUNCTION_NAMES","BTC_DECIMALS","USDC_DECIMALS","BPS_TO_PERCENT_DIVISOR","BPS_SCALE","AAVE_BASE_CURRENCY_DECIMALS","AAVE_BASE_CURRENCY_RAY_DECIMALS","WAD_DECIMALS","HEALTH_FACTOR_WARNING_THRESHOLD","MIN_HEALTH_FACTOR_FOR_BORROW","FULL_REPAY_BUFFER_DIVISOR","getPosition","publicClient","contractAddress","user","position","AaveIntegrationAdapterABI","zeroAddress","getPositionCollateral","getPositionSizeParams","result","maxPositionBTC","maxVaultsPerPosition","hasDebtFromPosition","mapPositionResult","getUserAccountData","spokeAddress","userAddress","data","AaveSpokeABI","getUserPosition","reserveId","hasDebt","hasCollateral","getUserTotalDebt","getReserve","getTargetHealthFactor","getDynamicReserveConfig","dynamicConfigKey","buildReorderVaultsTx","permutedVaultIds","encodeFunctionData","buildWithdrawCollateralsTx","vaultIds","buildBorrowTx","debtReserveId","amount","receiver","buildRepayTx","borrower","aaveValueToUsd","value","aaveRayValueToUsd","wadToNumber","calculateBorrowRatio","debtUsd","collateralValueUsd","getHealthFactorStatus","healthFactor","isHealthFactorHealthy","getHealthFactorStatusFromValue","calculateHealthFactor","totalDebtUsd","liquidationThresholdBps","selectVaultsForAmount","vaults","targetAmount","sortedVaults","a","b","selectedIds","selectedAmount","vault","calculateTotalVaultAmount","sum","SEIZURE_TOL","MAX_GROUPS","MIN_DEBT_THRESHOLD","getGroup1FromOrder","order","seizedFraction","seizureTol","coverThreshold","s","v","prefixSum","i","simulateOneGroup","seizedBtc","totalBtc","debt","isLastGroup","CF","THF","maxLB","expectedHF","liqPenalty","pLiq","targetSeizure","overSeizureBtc","denominator","debtToRepay","debtAfter","overSeizureVal","fairnessDebtRepay","simulateCascade","totalDebt","remaining","initialTotalBtc","btcAfterG1","sumBtcAfterEvents","groupCount","btcNow","EPS","MAX_ITER","MAX_VAULTS_FOR_EXHAUSTIVE","greedyOrder","optSubset","optSum","m","mask","subset","bit","ids","computeOptimalOrderSinglePass","sim","n","bestOrder","bestSum","bestAfterG1","g1Btc","g1","rest","candidate","computeOptimalOrder","refined","primaryImproves","tiebreakerImproves","MAX_SAFE_BIGINT","assertSafePrecision","name","computeSeizedFractionDetailed","LB","seizedFractionRaw","computeSeizedFraction","computeOptimalSplit","params","safetyMargin","totalBtcNum","targetSeizureBtc","sacrificialRaw","sacrificialVault","protectedVault","computeMinDepositForSplit","minPegin","sacrificialShare","protectedShare","minFromProtected","minFromSacrificial","checkRebalanceNeeded","vaultAmounts","targetCoverage","currentCoverage","deficit","computeTargetSeizureSats","totalCollateralSats","simulatePrefixSeizure","orderedVaults","targetSeizureSats","accumulated","cutoffIndex","seizedVaults","protectedVaults","totalSeizedSats","overSeizureSats"],"mappings":";AAWO,MAAMA,IAAsB;AAAA;AAAA,EAEjC,sBAAsB;AAAA;AAAA,EAEtB,QAAQ;AAAA;AAAA,EAER,OAAO;AAAA;AAAA,EAEP,gBAAgB;AAClB,GAMaC,IAAe,GAMfC,IAAgB,GAahBC,IAAyB,KAQzBC,IAAY,KAQZC,IAA8B,IAQ9BC,IAAkC,IAQlCC,IAAe,IAMfC,IAAkC,KAMlCC,KAA+B,KAO/BC,KAA4B;ACtEzC,eAAsBC,GACpBC,GACAC,GACAC,GACoC;AAapC,QAAMC,IAZS,MAAMH,EAAa,aAAa;AAAA,IAC7C,SAASC;AAAA,IACT,KAAKG;AAAA,IACL,cAAc;AAAA,IACd,MAAM,CAACF,CAAI;AAAA,EAAA,CACZ;AAUD,SAAIC,EAAS,kBAAkBE,IACtB,OAGF;AAAA,IACL,eAAeF,EAAS;AAAA,IACxB,UAAUA,EAAS;AAAA,EAAA;AAEvB;AAUA,eAAsBG,GACpBN,GACAC,GACAC,GACiB;AAQjB,SAPe,MAAMF,EAAa,aAAa;AAAA,IAC7C,SAASC;AAAA,IACT,KAAKG;AAAA,IACL,cAAc;AAAA,IACd,MAAM,CAACF,CAAI;AAAA,EAAA,CACZ;AAGH;AAYA,eAAsBK,GACpBP,GACAC,GAC6B;AAC7B,QAAMO,IAAS,MAAMR,EAAa,aAAa;AAAA,IAC7C,SAASC;AAAA,IACT,KAAKG;AAAA,IACL,cAAc;AAAA,EAAA,CACf,GAEK,CAACK,GAAgBC,CAAoB,IAAIF;AAE/C,SAAO;AAAA,IACL,gB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E,CAAU;AAEpE,SAAO,OAAO,KAAK,KAAK,OAAOsE,CAAmB,IAAIrF,CAAc,CAAC;AACvE;AA6BO,SAASsF,GACdpB,GACqB;AACrB,QAAM,EAAE,eAAAqB,GAAe,mBAAAC,EAAA,IAAsBtB;AAE7C,MAAIqB,EAAc,WAAW;AAC3B,UAAM,IAAI,MAAM,iCAAiC;AAGnD,MAAIC,KAAqB;AACvB,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAIJ,MAAIC,IAAc,IACdC,IAAcH,EAAc;AAEhC,WAASjF,IAAI,GAAGA,IAAIiF,EAAc,QAAQjF;AAGxC,QAFAmF,KAAeF,EAAcjF,CAAC,EAAE,YAE5BmF,KAAeD,GAAmB;AACpC,MAAAE,IAAcpF,IAAI;AAClB;AAAA,IACF;AAGF,QAAMqF,IAAeJ,EAAc,MAAM,GAAGG,CAAW,GACjDE,IAAkBL,EAAc,MAAMG,CAAW,GACjDG,IAAkBJ,GAClBK,IACJD,IAAkBL,IACdK,IAAkBL,IAClB;AAEN,SAAO;AAAA,IACL,cAAAG;AAAA,IACA,iBAAAC;AAAA,IACA,iBAAAE;AAAA,IACA,aAAAJ;AAAA,IACA,iBAAAG;AAAA,EAAA;AAEJ;"}
|
|
@@ -9,22 +9,6 @@
|
|
|
9
9
|
* - danger: < 1.0 (can be liquidated)
|
|
10
10
|
* - warning: < HEALTH_FACTOR_WARNING_THRESHOLD (at risk)
|
|
11
11
|
* - safe: >= HEALTH_FACTOR_WARNING_THRESHOLD (healthy)
|
|
12
|
-
*
|
|
13
|
-
* Color mapping:
|
|
14
|
-
* - Green (#00E676): safe
|
|
15
|
-
* - Amber (#FFC400): warning
|
|
16
|
-
* - Red (#FF1744): danger
|
|
17
|
-
* - Gray (#5A5A5A): no_debt
|
|
18
|
-
*/
|
|
19
|
-
export declare const HEALTH_FACTOR_COLORS: {
|
|
20
|
-
readonly GREEN: "#00E676";
|
|
21
|
-
readonly AMBER: "#FFC400";
|
|
22
|
-
readonly RED: "#FF1744";
|
|
23
|
-
readonly GRAY: "#5A5A5A";
|
|
24
|
-
};
|
|
25
|
-
export type HealthFactorColor = (typeof HEALTH_FACTOR_COLORS)[keyof typeof HEALTH_FACTOR_COLORS];
|
|
26
|
-
/**
|
|
27
|
-
* Health factor status based on our liquidation threshold
|
|
28
12
|
*/
|
|
29
13
|
export type HealthFactorStatus = "safe" | "warning" | "danger" | "no_debt";
|
|
30
14
|
/**
|
|
@@ -35,20 +19,6 @@ export type HealthFactorStatus = "safe" | "warning" | "danger" | "no_debt";
|
|
|
35
19
|
* @returns The status classification
|
|
36
20
|
*/
|
|
37
21
|
export declare function getHealthFactorStatus(healthFactor: number | null, hasDebt: boolean): HealthFactorStatus;
|
|
38
|
-
/**
|
|
39
|
-
* Gets the appropriate color for a health factor status.
|
|
40
|
-
*
|
|
41
|
-
* @param status - The health factor status
|
|
42
|
-
* @returns The color code for the status
|
|
43
|
-
*/
|
|
44
|
-
export declare function getHealthFactorColor(status: HealthFactorStatus): HealthFactorColor;
|
|
45
|
-
/**
|
|
46
|
-
* Format health factor number for display
|
|
47
|
-
*
|
|
48
|
-
* @param healthFactor - Health factor number (null if no debt)
|
|
49
|
-
* @returns Formatted string for display
|
|
50
|
-
*/
|
|
51
|
-
export declare function formatHealthFactor(healthFactor: number | null): string;
|
|
52
22
|
/**
|
|
53
23
|
* Checks if a health factor value represents a healthy position.
|
|
54
24
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"healthFactor.d.ts","sourceRoot":"","sources":["../../../../../src/tbv/integrations/aave/utils/healthFactor.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"healthFactor.d.ts","sourceRoot":"","sources":["../../../../../src/tbv/integrations/aave/utils/healthFactor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAIH,MAAM,MAAM,kBAAkB,GAAG,MAAM,GAAG,SAAS,GAAG,QAAQ,GAAG,SAAS,CAAC;AAE3E;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CACnC,YAAY,EAAE,MAAM,GAAG,IAAI,EAC3B,OAAO,EAAE,OAAO,GACf,kBAAkB,CAMpB;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAK1E;AAED;;;;;;GAMG;AACH,wBAAgB,8BAA8B,CAC5C,KAAK,EAAE,MAAM,GACZ,kBAAkB,CAIpB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,wBAAgB,qBAAqB,CACnC,kBAAkB,EAAE,MAAM,EAC1B,YAAY,EAAE,MAAM,EACpB,uBAAuB,EAAE,MAAM,GAC9B,MAAM,CAKR"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export { aaveRayValueToUsd, aaveValueToUsd, wadToNumber, } from './aaveConversions.js';
|
|
2
2
|
export { calculateBorrowRatio } from './borrowRatio.js';
|
|
3
3
|
export { hasDebtFromPosition } from './debtUtils.js';
|
|
4
|
-
export {
|
|
5
|
-
export type {
|
|
4
|
+
export { calculateHealthFactor, getHealthFactorStatus, getHealthFactorStatusFromValue, isHealthFactorHealthy, } from './healthFactor.js';
|
|
5
|
+
export type { HealthFactorStatus } from './healthFactor.js';
|
|
6
6
|
export { calculateTotalVaultAmount, selectVaultsForAmount, } from './vaultSelection.js';
|
|
7
7
|
export type { SelectableVault, VaultSelectionResult, } from './vaultSelection.js';
|
|
8
8
|
export { getGroup1FromOrder, MAX_GROUPS, MIN_DEBT_THRESHOLD, SEIZURE_TOL, simulateCascade, } from './cascadeSimulation.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/tbv/integrations/aave/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,cAAc,EACd,WAAW,GACZ,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EACL,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/tbv/integrations/aave/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,cAAc,EACd,WAAW,GACZ,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EACL,qBAAqB,EACrB,qBAAqB,EACrB,8BAA8B,EAC9B,qBAAqB,GACtB,MAAM,mBAAmB,CAAC;AAC3B,YAAY,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EACL,yBAAyB,EACzB,qBAAqB,GACtB,MAAM,qBAAqB,CAAC;AAC7B,YAAY,EACV,eAAe,EACf,oBAAoB,GACrB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,kBAAkB,EAClB,UAAU,EACV,kBAAkB,EAClB,WAAW,EACX,eAAe,GAChB,MAAM,wBAAwB,CAAC;AAChC,YAAY,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,EACL,wBAAwB,EACxB,qBAAqB,GACtB,MAAM,wBAAwB,CAAC;AAChC,YAAY,EACV,YAAY,EACZ,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,GACpB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,oBAAoB,EACpB,yBAAyB,EACzB,mBAAmB,EACnB,qBAAqB,EACrB,6BAA6B,GAC9B,MAAM,iBAAiB,CAAC;AACzB,YAAY,EACV,wBAAwB,EACxB,kBAAkB,EAClB,kBAAkB,EAClB,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var _=Object.defineProperty;var h=(e,t,n)=>t in e?_(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var u=(e,t,n)=>h(e,typeof t!="symbol"?t+"":t,n);const N=[{type:"function",name:"submitPeginRequest",inputs:[{name:"depositor",type:"address",internalType:"address"},{name:"depositorBtcPubKey",type:"bytes32",internalType:"bytes32"},{name:"btcPopSignature",type:"bytes",internalType:"bytes"},{name:"unsignedPrePeginTx",type:"bytes",internalType:"bytes"},{name:"depositorSignedPeginTx",type:"bytes",internalType:"bytes"},{name:"vaultProvider",type:"address",internalType:"address"},{name:"hashlock",type:"bytes32",internalType:"bytes32"},{name:"htlcVout",type:"uint8",internalType:"uint8"},{name:"depositorPayoutBtcAddress",type:"bytes",internalType:"bytes"},{name:"depositorWotsPkHash",type:"bytes32",internalType:"bytes32"}],outputs:[{name:"",type:"bytes32",internalType:"bytes32"}],stateMutability:"payable"},{type:"function",name:"submitPeginRequest",inputs:[{name:"depositor",type:"address",internalType:"address"},{name:"depositorBtcPubKey",type:"bytes32",internalType:"bytes32"},{name:"btcPopSignature",type:"bytes",internalType:"bytes"},{name:"unsignedPrePeginTx",type:"bytes",internalType:"bytes"},{name:"depositorSignedPeginTx",type:"bytes",internalType:"bytes"},{name:"vaultProvider",type:"address",internalType:"address"},{name:"hashlock",type:"bytes32",internalType:"bytes32"},{name:"htlcVout",type:"uint8",internalType:"uint8"},{name:"referralCode",type:"uint32",internalType:"uint32"},{name:"depositorPayoutBtcAddress",type:"bytes",internalType:"bytes"},{name:"depositorWotsPkHash",type:"bytes32",internalType:"bytes32"}],outputs:[{name:"",type:"bytes32",internalType:"bytes32"}],stateMutability:"payable"},{type:"function",name:"submitPeginRequestBatch",inputs:[{name:"depositor",type:"address",internalType:"address"},{name:"vaultProvider",type:"address",internalType:"address"},{name:"requests",type:"tuple[]",internalType:"struct IBTCVaultRegistry.BatchPeginRequest[]",components:[{name:"depositorBtcPubKey",type:"bytes32",internalType:"bytes32"},{name:"btcPopSignature",type:"bytes",internalType:"bytes"},{name:"unsignedPrePeginTx",type:"bytes",internalType:"bytes"},{name:"depositorSignedPeginTx",type:"bytes",internalType:"bytes"},{name:"hashlock",type:"bytes32",internalType:"bytes32"},{name:"htlcVout",type:"uint8",internalType:"uint8"},{name:"referralCode",type:"uint32",internalType:"uint32"},{name:"depositorPayoutBtcAddress",type:"bytes",internalType:"bytes"},{name:"depositorWotsPkHash",type:"bytes32",internalType:"bytes32"}]}],outputs:[{name:"vaultIds",type:"bytes32[]",internalType:"bytes32[]"}],stateMutability:"payable"},{type:"function",name:"activateVaultWithSecret",inputs:[{name:"vaultId",type:"bytes32",internalType:"bytes32"},{name:"s",type:"bytes32",internalType:"bytes32"},{name:"activationMetadata",type:"bytes",internalType:"bytes"}],outputs:[],stateMutability:"nonpayable"},{type:"function",name:"getPegInFee",inputs:[{name:"vaultProvider",type:"address",internalType:"address"}],outputs:[{name:"totalFee",type:"uint256",internalType:"uint256"}],stateMutability:"view"},{type:"function",name:"getBtcVaultBasicInfo",inputs:[{name:"vaultId",type:"bytes32",internalType:"bytes32"}],outputs:[{name:"depositor",type:"address",internalType:"address"},{name:"depositorBtcPubKey",type:"bytes32",internalType:"bytes32"},{name:"amount",type:"uint256",internalType:"uint256"},{name:"vaultProvider",type:"address",internalType:"address"},{name:"status",type:"uint8",internalType:"enum IBTCVaultRegistry.BTCVaultStatus"},{name:"applicationEntryPoint",type:"address",internalType:"address"},{name:"createdAt",type:"uint256",internalType:"uint256"}],stateMutability:"view"},{type:"error",name:"InvalidPeginFee",inputs:[{name:"provided",type:"uint256",internalType:"uint256"},{name:"required",type:"uint256",internalType:"uint256"}]},{type:"error",name:"InvalidSecret",inputs:[]},{type:"error",name:"ActivationDeadlineExpired",inputs:[]},{type:"error",name:"InvalidHashlock",inputs:[]},{type:"error",name:"DuplicateHashlock",inputs:[]},{type:"error",name:"CapExceeded",inputs:[]},{type:"error",name:"InvalidOutputIndex",inputs:[]},{type:"error",name:"PeginSignaturesIncomplete",inputs:[]},{type:"function",name:"getBtcVaultProtocolInfo",inputs:[{name:"vaultId",type:"bytes32",internalType:"bytes32"}],outputs:[{name:"depositorSignedPeginTx",type:"bytes",internalType:"bytes"},{name:"universalChallengersVersion",type:"uint32",internalType:"uint32"},{name:"appVaultKeepersVersion",type:"uint32",internalType:"uint32"},{name:"offchainParamsVersion",type:"uint32",internalType:"uint32"},{name:"verifiedAt",type:"uint256",internalType:"uint256"},{name:"depositorWotsPkHash",type:"bytes32",internalType:"bytes32"},{name:"hashlock",type:"bytes32",internalType:"bytes32"},{name:"htlcVout",type:"uint8",internalType:"uint8"},{name:"depositorPopSignature",type:"bytes",internalType:"bytes"},{name:"prePeginTxHash",type:"bytes32",internalType:"bytes32"},{name:"vaultProviderCommissionBps",type:"uint16",internalType:"uint16"}],stateMutability:"view"},{type:"function",name:"protocolParams",inputs:[],outputs:[{name:"",type:"address",internalType:"contract IProtocolParams"}],stateMutability:"view"},{type:"function",name:"applicationRegistry",inputs:[],outputs:[{name:"",type:"address",internalType:"contract IApplicationRegistry"}],stateMutability:"view"}];class l extends Error{constructor(t,n){super(n),this.code=t,this.name="JsonRpcError"}}const T={TIMEOUT:-32e3,NETWORK:-32001,PROXY_TIMEOUT:-32002,PROXY_UNAVAILABLE:-32003,INVALID_RESPONSE:-32700},g="2.0",O=3,v=1e3,w=new Set([408,429,500,502,503,504]),f=new Set(["vaultProvider_getPeginStatus","vaultProvider_getPegoutStatus","vaultProvider_requestDepositorPresignTransactions"]);function D(e){return f.has(e)}class L{constructor(t){u(this,"baseUrl");u(this,"timeout");u(this,"headers");u(this,"requestId",0);u(this,"retries");u(this,"retryDelay");u(this,"retryableFor");this.baseUrl=t.baseUrl.replace(/\/$/,""),this.timeout=t.timeout,this.headers={"Content-Type":"application/json",...t.headers},this.retries=t.retries??O,this.retryDelay=t.retryDelay??v,this.retryableFor=t.retryableFor??D}async call(t,n,r){const a=await this.fetchWithRetry(t,n,r);let s;try{s=await a.json()}catch{throw new l(T.INVALID_RESPONSE,"Invalid JSON-RPC response: body is not valid JSON")}if(s===null||typeof s!="object"||Array.isArray(s))throw new l(T.INVALID_RESPONSE,`Invalid JSON-RPC response: expected an object, got ${typeof s}`);const o=s;if("error"in o&&o.error!=null){const d=o.error;throw new l(d.code??T.INVALID_RESPONSE,d.message??"Unknown RPC error")}if(!("result"in o))throw new l(T.INVALID_RESPONSE,'Invalid JSON-RPC response: missing "result" field');return o.result}async callRaw(t,n,r){return this.fetchWithRetry(t,n,r)}async fetchWithRetry(t,n,r){const a=++this.requestId,s=this.retryableFor(t)?this.retries:0,d=JSON.stringify({jsonrpc:g,method:t,params:[n],id:a});let c=null;for(let y=0;y<=s;y++){const b=new AbortController,m=setTimeout(()=>b.abort(),this.timeout),p=r?V(r,b.signal):null,S=p?p.signal:b.signal;try{const i=await fetch(this.baseUrl,{method:"POST",headers:this.headers,body:d,signal:S});if(clearTimeout(m),p==null||p.cleanup(),!i.ok){if(y<s&&w.has(i.status)){const A=this.retryDelay*Math.pow(2,y);await this.sleep(A,r);continue}throw new Error(`HTTP error: ${i.status} ${i.statusText}`)}return i}catch(i){if(clearTimeout(m),p==null||p.cleanup(),c=i instanceof Error?i:new Error(String(i)),r!=null&&r.aborted)throw new Error("Request aborted");if(i instanceof Error&&i.name==="AbortError"){if(y<s){const P=this.retryDelay*Math.pow(2,y);await this.sleep(P,r);continue}throw new l(T.TIMEOUT,`Request timeout after ${this.timeout}ms (${s+1} attempts)`)}if(i instanceof TypeError){if(y<s){const P=this.retryDelay*Math.pow(2,y);await this.sleep(P,r);continue}throw new l(T.NETWORK,`Network error: ${i.message} (${s+1} attempts)`)}throw i}}throw c||new Error("Unknown error after retries")}sleep(t,n){return new Promise((r,a)=>{if(n!=null&&n.aborted){a(new Error("Request aborted"));return}const s=setTimeout(()=>{n==null||n.removeEventListener("abort",o),r()},t),o=()=>{clearTimeout(s),a(new Error("Request aborted"))};n==null||n.addEventListener("abort",o,{once:!0})})}getBaseUrl(){return this.baseUrl}}function V(e,t){if(e.aborted)return{signal:e,cleanup:()=>{}};if(t.aborted)return{signal:t,cleanup:()=>{}};const n=new AbortController,r=()=>{t.removeEventListener("abort",a),n.abort()},a=()=>{e.removeEventListener("abort",r),n.abort()};e.addEventListener("abort",r,{once:!0}),t.addEventListener("abort",a,{once:!0});const s=()=>{e.removeEventListener("abort",r),t.removeEventListener("abort",a)};return{signal:n.signal,cleanup:s}}var E=(e=>(e.PENDING_INGESTION="PendingIngestion",e.PENDING_DEPOSITOR_WOTS_PK="PendingDepositorWotsPK",e.PENDING_BABE_SETUP="PendingBabeSetup",e.PENDING_CHALLENGER_PRESIGNING="PendingChallengerPresigning",e.PENDING_PEGIN_SIGS_AVAILABILITY="PendingPeginSigsAvailability",e.PENDING_PRE_PEGIN_CONFIRMATIONS="PendingPrePegInConfirmations",e.PENDING_DEPOSITOR_SIGNATURES="PendingDepositorSignatures",e.PENDING_ACKS="PendingACKs",e.PENDING_ACTIVATION="PendingActivation",e.ACTIVATED="Activated",e.EXPIRED="Expired",e.CLAIM_POSTED="ClaimPosted",e.PEGGED_OUT="PeggedOut",e))(E||{});const B=["PendingIngestion","PendingBabeSetup","PendingChallengerPresigning","PendingPeginSigsAvailability","PendingPrePegInConfirmations"],U=["PendingACKs","PendingActivation","Activated"],R=new Set(["PendingBabeSetup","PendingChallengerPresigning","PendingPeginSigsAvailability","PendingPrePegInConfirmations",...U]),C=new Set(["Expired","ClaimPosted","PeggedOut"]),G=new Set([...R,"PendingDepositorSignatures"]);var I=(e=>(e[e.DATABASE_ERROR=-32005]="DATABASE_ERROR",e[e.PRESIGN_ERROR=-32006]="PRESIGN_ERROR",e[e.JSON_SERIALIZATION_ERROR=-32007]="JSON_SERIALIZATION_ERROR",e[e.TX_GRAPH_ERROR=-32008]="TX_GRAPH_ERROR",e[e.INVALID_GRAPH=-32009]="INVALID_GRAPH",e[e.VALIDATION_ERROR=-32010]="VALIDATION_ERROR",e[e.NOT_FOUND=-32011]="NOT_FOUND",e[e.INTERNAL_ERROR=-32603]="INTERNAL_ERROR",e))(I||{});exports.BTCVaultRegistryABI=N;exports.DaemonStatus=E;exports.JSON_RPC_ERROR_CODES=T;exports.JsonRpcClient=L;exports.JsonRpcError=l;exports.POST_WOTS_STATUSES=G;exports.PRE_DEPOSITOR_SIGNATURES_STATES=B;exports.RpcErrorCode=I;exports.VP_TERMINAL_STATUSES=C;exports.VP_TRANSIENT_STATUSES=R;
|
|
2
|
+
//# sourceMappingURL=types-B-p4dhEH.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types-B-p4dhEH.cjs","sources":["../src/tbv/core/contracts/abis/BTCVaultRegistry.abi.ts","../src/tbv/core/clients/vault-provider/json-rpc-client.ts","../src/tbv/core/clients/vault-provider/types.ts"],"sourcesContent":["/**\n * BTCVaultRegistry Contract ABI\n *\n * Minimal ABI containing only the functions needed by the SDK.\n * Full ABI is available in the vault service package.\n *\n * @module contracts/abis/BTCVaultRegistry\n */\n\n/**\n * Minimal ABI for BTCVaultRegistry contract.\n * Contains submitPeginRequest, submitPeginRequestBatch, activateVaultWithSecret, getPegInFee, and getBtcVaultBasicInfo.\n */\nexport const BTCVaultRegistryABI = [\n {\n type: \"function\",\n name: \"submitPeginRequest\",\n inputs: [\n {\n name: \"depositor\",\n type: \"address\",\n internalType: \"address\",\n },\n {\n name: \"depositorBtcPubKey\",\n type: \"bytes32\",\n internalType: \"bytes32\",\n },\n {\n name: \"btcPopSignature\",\n type: \"bytes\",\n internalType: \"bytes\",\n },\n {\n name: \"unsignedPrePeginTx\",\n type: \"bytes\",\n internalType: \"bytes\",\n },\n {\n name: \"depositorSignedPeginTx\",\n type: \"bytes\",\n internalType: \"bytes\",\n },\n {\n name: \"vaultProvider\",\n type: \"address\",\n internalType: \"address\",\n },\n {\n name: \"hashlock\",\n type: \"bytes32\",\n internalType: \"bytes32\",\n },\n {\n name: \"htlcVout\",\n type: \"uint8\",\n internalType: \"uint8\",\n },\n {\n name: \"depositorPayoutBtcAddress\",\n type: \"bytes\",\n internalType: \"bytes\",\n },\n {\n name: \"depositorWotsPkHash\",\n type: \"bytes32\",\n internalType: \"bytes32\",\n },\n ],\n outputs: [\n {\n name: \"\",\n type: \"bytes32\",\n internalType: \"bytes32\",\n },\n ],\n stateMutability: \"payable\",\n },\n {\n type: \"function\",\n name: \"submitPeginRequest\",\n inputs: [\n {\n name: \"depositor\",\n type: \"address\",\n internalType: \"address\",\n },\n {\n name: \"depositorBtcPubKey\",\n type: \"bytes32\",\n internalType: \"bytes32\",\n },\n {\n name: \"btcPopSignature\",\n type: \"bytes\",\n internalType: \"bytes\",\n },\n {\n name: \"unsignedPrePeginTx\",\n type: \"bytes\",\n internalType: \"bytes\",\n },\n {\n name: \"depositorSignedPeginTx\",\n type: \"bytes\",\n internalType: \"bytes\",\n },\n {\n name: \"vaultProvider\",\n type: \"address\",\n internalType: \"address\",\n },\n {\n name: \"hashlock\",\n type: \"bytes32\",\n internalType: \"bytes32\",\n },\n {\n name: \"htlcVout\",\n type: \"uint8\",\n internalType: \"uint8\",\n },\n {\n name: \"referralCode\",\n type: \"uint32\",\n internalType: \"uint32\",\n },\n {\n name: \"depositorPayoutBtcAddress\",\n type: \"bytes\",\n internalType: \"bytes\",\n },\n {\n name: \"depositorWotsPkHash\",\n type: \"bytes32\",\n internalType: \"bytes32\",\n },\n ],\n outputs: [\n {\n name: \"\",\n type: \"bytes32\",\n internalType: \"bytes32\",\n },\n ],\n stateMutability: \"payable\",\n },\n {\n type: \"function\",\n name: \"submitPeginRequestBatch\",\n inputs: [\n { name: \"depositor\", type: \"address\", internalType: \"address\" },\n { name: \"vaultProvider\", type: \"address\", internalType: \"address\" },\n {\n name: \"requests\",\n type: \"tuple[]\",\n internalType: \"struct IBTCVaultRegistry.BatchPeginRequest[]\",\n components: [\n { name: \"depositorBtcPubKey\", type: \"bytes32\", internalType: \"bytes32\" },\n { name: \"btcPopSignature\", type: \"bytes\", internalType: \"bytes\" },\n { name: \"unsignedPrePeginTx\", type: \"bytes\", internalType: \"bytes\" },\n { name: \"depositorSignedPeginTx\", type: \"bytes\", internalType: \"bytes\" },\n { name: \"hashlock\", type: \"bytes32\", internalType: \"bytes32\" },\n { name: \"htlcVout\", type: \"uint8\", internalType: \"uint8\" },\n { name: \"referralCode\", type: \"uint32\", internalType: \"uint32\" },\n { name: \"depositorPayoutBtcAddress\", type: \"bytes\", internalType: \"bytes\" },\n { name: \"depositorWotsPkHash\", type: \"bytes32\", internalType: \"bytes32\" },\n ],\n },\n ],\n outputs: [\n { name: \"vaultIds\", type: \"bytes32[]\", internalType: \"bytes32[]\" },\n ],\n stateMutability: \"payable\",\n },\n {\n type: \"function\",\n name: \"activateVaultWithSecret\",\n inputs: [\n {\n name: \"vaultId\",\n type: \"bytes32\",\n internalType: \"bytes32\",\n },\n {\n name: \"s\",\n type: \"bytes32\",\n internalType: \"bytes32\",\n },\n {\n name: \"activationMetadata\",\n type: \"bytes\",\n internalType: \"bytes\",\n },\n ],\n outputs: [],\n stateMutability: \"nonpayable\",\n },\n {\n type: \"function\",\n name: \"getPegInFee\",\n inputs: [\n {\n name: \"vaultProvider\",\n type: \"address\",\n internalType: \"address\",\n },\n ],\n outputs: [\n {\n name: \"totalFee\",\n type: \"uint256\",\n internalType: \"uint256\",\n },\n ],\n stateMutability: \"view\",\n },\n {\n type: \"function\",\n name: \"getBtcVaultBasicInfo\",\n inputs: [\n {\n name: \"vaultId\",\n type: \"bytes32\",\n internalType: \"bytes32\",\n },\n ],\n outputs: [\n { name: \"depositor\", type: \"address\", internalType: \"address\" },\n { name: \"depositorBtcPubKey\", type: \"bytes32\", internalType: \"bytes32\" },\n { name: \"amount\", type: \"uint256\", internalType: \"uint256\" },\n { name: \"vaultProvider\", type: \"address\", internalType: \"address\" },\n { name: \"status\", type: \"uint8\", internalType: \"enum IBTCVaultRegistry.BTCVaultStatus\" },\n { name: \"applicationEntryPoint\", type: \"address\", internalType: \"address\" },\n { name: \"createdAt\", type: \"uint256\", internalType: \"uint256\" },\n ],\n stateMutability: \"view\",\n },\n {\n type: \"error\",\n name: \"InvalidPeginFee\",\n inputs: [\n {\n name: \"provided\",\n type: \"uint256\",\n internalType: \"uint256\",\n },\n {\n name: \"required\",\n type: \"uint256\",\n internalType: \"uint256\",\n },\n ],\n },\n {\n type: \"error\",\n name: \"InvalidSecret\",\n inputs: [],\n },\n {\n type: \"error\",\n name: \"ActivationDeadlineExpired\",\n inputs: [],\n },\n {\n type: \"error\",\n name: \"InvalidHashlock\",\n inputs: [],\n },\n {\n type: \"error\",\n name: \"DuplicateHashlock\",\n inputs: [],\n },\n {\n type: \"error\",\n name: \"CapExceeded\",\n inputs: [],\n },\n {\n type: \"error\",\n name: \"InvalidOutputIndex\",\n inputs: [],\n },\n {\n type: \"error\",\n name: \"PeginSignaturesIncomplete\",\n inputs: [],\n },\n {\n type: \"function\",\n name: \"getBtcVaultProtocolInfo\",\n inputs: [\n {\n name: \"vaultId\",\n type: \"bytes32\",\n internalType: \"bytes32\",\n },\n ],\n outputs: [\n {\n name: \"depositorSignedPeginTx\",\n type: \"bytes\",\n internalType: \"bytes\",\n },\n {\n name: \"universalChallengersVersion\",\n type: \"uint32\",\n internalType: \"uint32\",\n },\n {\n name: \"appVaultKeepersVersion\",\n type: \"uint32\",\n internalType: \"uint32\",\n },\n {\n name: \"offchainParamsVersion\",\n type: \"uint32\",\n internalType: \"uint32\",\n },\n {\n name: \"verifiedAt\",\n type: \"uint256\",\n internalType: \"uint256\",\n },\n {\n name: \"depositorWotsPkHash\",\n type: \"bytes32\",\n internalType: \"bytes32\",\n },\n {\n name: \"hashlock\",\n type: \"bytes32\",\n internalType: \"bytes32\",\n },\n {\n name: \"htlcVout\",\n type: \"uint8\",\n internalType: \"uint8\",\n },\n {\n name: \"depositorPopSignature\",\n type: \"bytes\",\n internalType: \"bytes\",\n },\n {\n name: \"prePeginTxHash\",\n type: \"bytes32\",\n internalType: \"bytes32\",\n },\n {\n name: \"vaultProviderCommissionBps\",\n type: \"uint16\",\n internalType: \"uint16\",\n },\n ],\n stateMutability: \"view\",\n },\n {\n type: \"function\",\n name: \"protocolParams\",\n inputs: [],\n outputs: [\n {\n name: \"\",\n type: \"address\",\n internalType: \"contract IProtocolParams\",\n },\n ],\n stateMutability: \"view\",\n },\n {\n type: \"function\",\n name: \"applicationRegistry\",\n inputs: [],\n outputs: [\n {\n name: \"\",\n type: \"address\",\n internalType: \"contract IApplicationRegistry\",\n },\n ],\n stateMutability: \"view\",\n },\n] as const;\n","/**\n * Generic JSON-RPC 2.0 HTTP Client\n *\n * Framework-agnostic client using `fetch()` — works in browsers and Node.js 18+.\n * Includes configurable retry policy and AbortSignal passthrough.\n */\n\nexport interface JsonRpcRequest<T = unknown> {\n jsonrpc: \"2.0\";\n method: string;\n params: T;\n id: number | string;\n}\n\nexport interface JsonRpcSuccessResponse<T = unknown> {\n jsonrpc: \"2.0\";\n result: T;\n id: number | string;\n}\n\nexport interface JsonRpcErrorResponse {\n jsonrpc: \"2.0\";\n error: {\n code: number;\n message: string;\n data?: unknown;\n };\n id: number | string;\n}\n\nexport type JsonRpcResponse<T = unknown> =\n | JsonRpcSuccessResponse<T>\n | JsonRpcErrorResponse;\n\nexport interface JsonRpcClientConfig {\n /** Base URL of the RPC service */\n baseUrl: string;\n /** Timeout in milliseconds per request attempt */\n timeout: number;\n /** Optional custom headers */\n headers?: Record<string, string>;\n /** Number of retry attempts for transient errors (default: 3) */\n retries?: number;\n /** Initial retry delay in milliseconds (default: 1000) */\n retryDelay?: number;\n /**\n * Predicate to determine if a method is safe to retry.\n * Default: only retry `vaultProvider_getPeginStatus` and `vaultProvider_getPegoutStatus`.\n * Write/mutating methods are NOT retried by default.\n */\n retryableFor?: (method: string) => boolean;\n}\n\nexport class JsonRpcError extends Error {\n constructor(\n public code: number,\n message: string,\n ) {\n super(message);\n this.name = \"JsonRpcError\";\n }\n}\n\nexport const JSON_RPC_ERROR_CODES = {\n TIMEOUT: -32000,\n NETWORK: -32001,\n /** VP proxy: request timed out at proxy level */\n PROXY_TIMEOUT: -32002,\n /** VP proxy: VP unreachable / DNS failure / response too large */\n PROXY_UNAVAILABLE: -32003,\n /** SDK client: response missing \"result\" field (malformed JSON-RPC) */\n INVALID_RESPONSE: -32700,\n} as const;\n\n/** JSON-RPC protocol version */\nconst JSON_RPC_VERSION = \"2.0\" as const;\n\n/** Default number of retry attempts for transient errors */\nconst DEFAULT_RETRY_ATTEMPTS = 3;\n\n/** Default initial retry delay in milliseconds */\nconst DEFAULT_RETRY_DELAY_MS = 1000;\n\n/** HTTP status codes that indicate transient server errors and are safe to retry */\nconst RETRYABLE_HTTP_STATUS_CODES: ReadonlySet<number> = new Set([\n 408, // Request Timeout\n 429, // Too Many Requests\n 500, // Internal Server Error\n 502, // Bad Gateway\n 503, // Service Unavailable\n 504, // Gateway Timeout\n]);\n\n/** Default retry predicate: only retry read-only / idempotent methods */\nconst DEFAULT_RETRYABLE_METHODS: ReadonlySet<string> = new Set([\n \"vaultProvider_getPeginStatus\",\n \"vaultProvider_getPegoutStatus\",\n \"vaultProvider_requestDepositorPresignTransactions\",\n]);\n\nfunction defaultRetryableFor(method: string): boolean {\n return DEFAULT_RETRYABLE_METHODS.has(method);\n}\n\n/**\n * Generic JSON-RPC 2.0 HTTP client with safe retry policy.\n */\nexport class JsonRpcClient {\n private baseUrl: string;\n private timeout: number;\n private headers: Record<string, string>;\n private requestId = 0;\n private retries: number;\n private retryDelay: number;\n private retryableFor: (method: string) => boolean;\n\n constructor(config: JsonRpcClientConfig) {\n this.baseUrl = config.baseUrl.replace(/\\/$/, \"\");\n this.timeout = config.timeout;\n this.headers = {\n \"Content-Type\": \"application/json\",\n ...config.headers,\n };\n this.retries = config.retries ?? DEFAULT_RETRY_ATTEMPTS;\n this.retryDelay = config.retryDelay ?? DEFAULT_RETRY_DELAY_MS;\n this.retryableFor = config.retryableFor ?? defaultRetryableFor;\n }\n\n /**\n * Make a JSON-RPC request with optional retry for safe methods.\n *\n * @param method - The RPC method name\n * @param params - The method parameters\n * @param signal - Optional AbortSignal for caller-controlled cancellation\n * @returns The result from the RPC method\n * @throws JsonRpcError if the RPC call fails\n */\n async call<TParams, TResult>(\n method: string,\n params: TParams,\n signal?: AbortSignal,\n ): Promise<TResult> {\n const response = await this.fetchWithRetry(method, params, signal);\n\n let jsonResponse: unknown;\n try {\n jsonResponse = await response.json();\n } catch {\n throw new JsonRpcError(\n JSON_RPC_ERROR_CODES.INVALID_RESPONSE,\n \"Invalid JSON-RPC response: body is not valid JSON\",\n );\n }\n\n if (\n jsonResponse === null ||\n typeof jsonResponse !== \"object\" ||\n Array.isArray(jsonResponse)\n ) {\n throw new JsonRpcError(\n JSON_RPC_ERROR_CODES.INVALID_RESPONSE,\n `Invalid JSON-RPC response: expected an object, got ${typeof jsonResponse}`,\n );\n }\n\n const rpcResponse = jsonResponse as Record<string, unknown>;\n\n if (\"error\" in rpcResponse && rpcResponse.error != null) {\n const err = rpcResponse.error as { code?: number; message?: string };\n throw new JsonRpcError(\n err.code ?? JSON_RPC_ERROR_CODES.INVALID_RESPONSE,\n err.message ?? \"Unknown RPC error\",\n );\n }\n\n if (!(\"result\" in rpcResponse)) {\n throw new JsonRpcError(\n JSON_RPC_ERROR_CODES.INVALID_RESPONSE,\n `Invalid JSON-RPC response: missing \"result\" field`,\n );\n }\n\n return rpcResponse.result as TResult;\n }\n\n /**\n * Make a JSON-RPC request returning the raw Response (unparsed body).\n */\n async callRaw<TParams>(\n method: string,\n params: TParams,\n signal?: AbortSignal,\n ): Promise<Response> {\n return this.fetchWithRetry(method, params, signal);\n }\n\n private async fetchWithRetry<TParams>(\n method: string,\n params: TParams,\n callerSignal?: AbortSignal,\n ): Promise<Response> {\n const requestId = ++this.requestId;\n const maxRetries = this.retryableFor(method) ? this.retries : 0;\n\n // jsonrpsee (Rust backend) expects params as an array (positional parameters)\n const request: JsonRpcRequest<TParams[]> = {\n jsonrpc: JSON_RPC_VERSION,\n method,\n params: [params],\n id: requestId,\n };\n\n const body = JSON.stringify(request);\n let lastError: Error | null = null;\n\n for (let attempt = 0; attempt <= maxRetries; attempt++) {\n // Merge caller signal with per-request timeout signal\n const timeoutController = new AbortController();\n const timeoutId = setTimeout(\n () => timeoutController.abort(),\n this.timeout,\n );\n\n const merged = callerSignal\n ? mergeAbortSignals(callerSignal, timeoutController.signal)\n : null;\n const signal = merged ? merged.signal : timeoutController.signal;\n\n try {\n const response = await fetch(this.baseUrl, {\n method: \"POST\",\n headers: this.headers,\n body,\n signal,\n });\n\n clearTimeout(timeoutId);\n merged?.cleanup();\n\n if (!response.ok) {\n const shouldRetry =\n attempt < maxRetries &&\n RETRYABLE_HTTP_STATUS_CODES.has(response.status);\n\n if (shouldRetry) {\n const delay = this.retryDelay * Math.pow(2, attempt);\n await this.sleep(delay, callerSignal);\n continue;\n }\n\n throw new Error(\n `HTTP error: ${response.status} ${response.statusText}`,\n );\n }\n\n return response;\n } catch (error) {\n clearTimeout(timeoutId);\n merged?.cleanup();\n lastError = error instanceof Error ? error : new Error(String(error));\n\n // Check if caller aborted (not our timeout)\n if (callerSignal?.aborted) {\n throw new Error(\"Request aborted\");\n }\n\n if (error instanceof Error && error.name === \"AbortError\") {\n if (attempt < maxRetries) {\n const delay = this.retryDelay * Math.pow(2, attempt);\n await this.sleep(delay, callerSignal);\n continue;\n }\n throw new JsonRpcError(\n JSON_RPC_ERROR_CODES.TIMEOUT,\n `Request timeout after ${this.timeout}ms (${maxRetries + 1} attempts)`,\n );\n }\n\n // Handle network errors (CORS, connection refused, etc.)\n if (error instanceof TypeError) {\n if (attempt < maxRetries) {\n const delay = this.retryDelay * Math.pow(2, attempt);\n await this.sleep(delay, callerSignal);\n continue;\n }\n throw new JsonRpcError(\n JSON_RPC_ERROR_CODES.NETWORK,\n `Network error: ${error.message} (${maxRetries + 1} attempts)`,\n );\n }\n\n // Don't retry JSON-RPC errors (business logic errors)\n throw error;\n }\n }\n\n throw lastError || new Error(\"Unknown error after retries\");\n }\n\n private sleep(ms: number, signal?: AbortSignal): Promise<void> {\n return new Promise((resolve, reject) => {\n if (signal?.aborted) {\n reject(new Error(\"Request aborted\"));\n return;\n }\n const timeoutId = setTimeout(() => {\n signal?.removeEventListener(\"abort\", onAbort);\n resolve();\n }, ms);\n const onAbort = () => {\n clearTimeout(timeoutId);\n reject(new Error(\"Request aborted\"));\n };\n signal?.addEventListener(\"abort\", onAbort, { once: true });\n });\n }\n\n getBaseUrl(): string {\n return this.baseUrl;\n }\n}\n\ninterface MergedSignal {\n signal: AbortSignal;\n /** Remove listeners from the source signals. Call after the request completes. */\n cleanup: () => void;\n}\n\n/**\n * Merge two AbortSignals — the returned signal aborts if either input aborts.\n * Returns a cleanup function to remove listeners when the request completes\n * normally, preventing listener accumulation in long-lived polling flows.\n */\nfunction mergeAbortSignals(\n a: AbortSignal,\n b: AbortSignal,\n): MergedSignal {\n if (a.aborted) return { signal: a, cleanup: () => {} };\n if (b.aborted) return { signal: b, cleanup: () => {} };\n\n const controller = new AbortController();\n const onAbortA = () => {\n b.removeEventListener(\"abort\", onAbortB);\n controller.abort();\n };\n const onAbortB = () => {\n a.removeEventListener(\"abort\", onAbortA);\n controller.abort();\n };\n a.addEventListener(\"abort\", onAbortA, { once: true });\n b.addEventListener(\"abort\", onAbortB, { once: true });\n\n const cleanup = () => {\n a.removeEventListener(\"abort\", onAbortA);\n b.removeEventListener(\"abort\", onAbortB);\n };\n\n return { signal: controller.signal, cleanup };\n}\n","/**\n * Type definitions for Vault Provider JSON-RPC API.\n *\n * These types match the `vaultProvider_*` RPC namespace defined by the\n * btc-vault daemon. They are the SDK's canonical copy of the VP protocol\n * contract, independent of any frontend framework.\n *\n * @see https://github.com/babylonlabs-io/btc-vault/blob/main/docs/pegin.md\n */\n\n// ============================================================================\n// Daemon Status\n// ============================================================================\n\n/**\n * Backend daemon status (vault provider database).\n * Source: btc-vault crates/vaultd/src/workers/claimer/mod.rs PegInStatus enum\n *\n * State flow (happy path):\n * PendingIngestion -> PendingDepositorWotsPK -> PendingBabeSetup -> PendingChallengerPresigning\n * -> PendingPeginSigsAvailability -> PendingPrePegInConfirmations\n * -> PendingDepositorSignatures -> PendingACKs -> PendingActivation -> Activated\n *\n * Terminal / branching states:\n * - Expired: vault timed out before activation\n * - ClaimPosted: claim transaction posted on-chain\n * - PeggedOut: BTC has been returned to the depositor\n */\nexport enum DaemonStatus {\n PENDING_INGESTION = \"PendingIngestion\",\n PENDING_DEPOSITOR_WOTS_PK = \"PendingDepositorWotsPK\",\n PENDING_BABE_SETUP = \"PendingBabeSetup\",\n PENDING_CHALLENGER_PRESIGNING = \"PendingChallengerPresigning\",\n PENDING_PEGIN_SIGS_AVAILABILITY = \"PendingPeginSigsAvailability\",\n PENDING_PRE_PEGIN_CONFIRMATIONS = \"PendingPrePegInConfirmations\",\n PENDING_DEPOSITOR_SIGNATURES = \"PendingDepositorSignatures\",\n PENDING_ACKS = \"PendingACKs\",\n PENDING_ACTIVATION = \"PendingActivation\",\n ACTIVATED = \"Activated\",\n EXPIRED = \"Expired\",\n CLAIM_POSTED = \"ClaimPosted\",\n PEGGED_OUT = \"PeggedOut\",\n}\n\n// ============================================================================\n// Status Groups\n// ============================================================================\n\n/**\n * States where the VP is still processing (no depositor action needed).\n * Excludes PENDING_DEPOSITOR_WOTS_PK (requires depositor action).\n */\nexport const PRE_DEPOSITOR_SIGNATURES_STATES: readonly DaemonStatus[] = [\n DaemonStatus.PENDING_INGESTION,\n DaemonStatus.PENDING_BABE_SETUP,\n DaemonStatus.PENDING_CHALLENGER_PRESIGNING,\n DaemonStatus.PENDING_PEGIN_SIGS_AVAILABILITY,\n DaemonStatus.PENDING_PRE_PEGIN_CONFIRMATIONS,\n];\n\n/** States after PendingDepositorSignatures where the depositor has no action. */\nconst POST_PAYOUT_SIGNATURE_STATUSES: readonly DaemonStatus[] = [\n DaemonStatus.PENDING_ACKS,\n DaemonStatus.PENDING_ACTIVATION,\n DaemonStatus.ACTIVATED,\n];\n\n/**\n * Statuses where no depositor action is needed (VP processing or already past\n * depositor interaction). Excludes PENDING_INGESTION and PENDING_DEPOSITOR_WOTS_PK.\n */\nexport const VP_TRANSIENT_STATUSES: ReadonlySet<DaemonStatus> = new Set([\n DaemonStatus.PENDING_BABE_SETUP,\n DaemonStatus.PENDING_CHALLENGER_PRESIGNING,\n DaemonStatus.PENDING_PEGIN_SIGS_AVAILABILITY,\n DaemonStatus.PENDING_PRE_PEGIN_CONFIRMATIONS,\n ...POST_PAYOUT_SIGNATURE_STATUSES,\n]);\n\n/**\n * Terminal VP statuses where no further progress is possible.\n * If the VP reaches one of these states while polling, polling should\n * stop immediately with an error rather than waiting for timeout.\n */\nexport const VP_TERMINAL_STATUSES: ReadonlySet<DaemonStatus> = new Set([\n DaemonStatus.EXPIRED,\n DaemonStatus.CLAIM_POSTED,\n DaemonStatus.PEGGED_OUT,\n]);\n\n/**\n * Statuses that come after WOTS key submission.\n * If the VP is already in one of these states, the WOTS key was already\n * submitted and we can skip.\n */\nexport const POST_WOTS_STATUSES: ReadonlySet<DaemonStatus> = new Set([\n ...VP_TRANSIENT_STATUSES,\n DaemonStatus.PENDING_DEPOSITOR_SIGNATURES,\n]);\n\n// ============================================================================\n// WOTS Types (needed by SubmitDepositorWotsKeyParams)\n// ============================================================================\n\n/**\n * WOTS configuration for a single block.\n * Matches Rust `babe::wots::Config` serde format.\n */\nexport interface WotsConfig {\n /** Digit bit-width (e.g. 4 → base-16 digits). */\n d: number;\n /** Number of message digits in this block. */\n n: number;\n /** Radix used for the checksum computation. */\n checksum_radix: number;\n}\n\n/**\n * A single block of WOTS public keys.\n * Chain values are arrays of byte values (matching Rust `[u8; 20]`).\n */\nexport interface WotsBlockPublicKey {\n config: WotsConfig;\n message_terminals: number[][];\n checksum_major_terminal: number[];\n checksum_minor_terminal: number[];\n}\n\n// ============================================================================\n// Request Parameter Types\n// ============================================================================\n\n/** Params for requesting the payout/claim/assert transactions to pre-sign. */\nexport interface RequestDepositorPresignTransactionsParams {\n pegin_txid: string;\n depositor_pk: string;\n}\n\n/** Params for submitting the depositor's WOTS public key to the VP. */\nexport interface SubmitDepositorWotsKeyParams {\n pegin_txid: string;\n depositor_pk: string;\n wots_public_keys: WotsBlockPublicKey[];\n}\n\n/** Per-challenger signatures for the depositor-as-claimer flow. */\nexport interface DepositorPreSigsPerChallenger {\n nopayout_signature: string;\n}\n\n/** Depositor-as-claimer pre-signatures (payout + per-challenger). */\nexport interface DepositorAsClaimerPresignatures {\n payout_signatures: ClaimerSignatures;\n per_challenger: Record<string, DepositorPreSigsPerChallenger>;\n}\n\n/** Params for submitting depositor pre-signatures including claimer presignatures. */\nexport interface SubmitDepositorPresignaturesParams {\n pegin_txid: string;\n depositor_pk: string;\n signatures: Record<string, ClaimerSignatures>;\n depositor_claimer_presignatures: DepositorAsClaimerPresignatures;\n}\n\n/** Payout signatures per claimer. */\nexport interface ClaimerSignatures {\n payout_signature: string;\n}\n\n/** Params for requesting BaBe DecryptorArtifacts from the VP. */\nexport interface RequestDepositorClaimerArtifactsParams {\n pegin_txid: string;\n depositor_pk: string;\n}\n\n/** Params for querying pegin status. Either pegin_txid or vault_id must be provided. */\nexport type GetPeginStatusParams =\n | { pegin_txid: string; vault_id?: never }\n | { vault_id: string; pegin_txid?: never };\n\n// ============================================================================\n// Response Types\n// ============================================================================\n\n/** A raw Bitcoin transaction with its hex encoding. */\nexport interface TransactionData {\n tx_hex: string;\n}\n\n/** Set of transactions the depositor must pre-sign for a single claimer. */\nexport interface ClaimerTransactions {\n claimer_pubkey: string;\n claim_tx: TransactionData;\n assert_tx: TransactionData;\n payout_tx: TransactionData;\n payout_psbt: string;\n}\n\n/** Per-segment connector data for ChallengeAssert inputs. */\nexport interface ChallengeAssertConnectorData {\n wots_pks_json: string;\n gc_wots_keys_json: string;\n}\n\n/** Challenger-specific transactions and signing data for the depositor graph. */\nexport interface PresignDataPerChallenger {\n challenger_pubkey: string;\n challenge_assert_x_tx: TransactionData;\n challenge_assert_y_tx: TransactionData;\n nopayout_tx: TransactionData;\n nopayout_psbt: string;\n challenge_assert_connectors: ChallengeAssertConnectorData[];\n output_label_hashes: string[];\n}\n\n/** Depositor-as-claimer TxGraph transactions. */\nexport interface DepositorGraphTransactions {\n claim_tx: TransactionData;\n assert_tx: TransactionData;\n payout_tx: TransactionData;\n payout_psbt: string;\n challenger_presign_data: PresignDataPerChallenger[];\n offchain_params_version: number;\n}\n\n/** Response from `requestDepositorPresignTransactions`. */\nexport interface RequestDepositorPresignTransactionsResponse {\n txs: ClaimerTransactions[];\n depositor_graph: DepositorGraphTransactions;\n}\n\n/** BaBe garbled-circuit session data for a single challenger. */\nexport interface BaBeSessionData {\n decryptor_artifacts_hex: string;\n}\n\n/** Response from `requestDepositorClaimerArtifacts`. */\nexport interface RequestDepositorClaimerArtifactsResponse {\n tx_graph_json: string;\n verifying_key_hex: string;\n babe_sessions: Record<string, BaBeSessionData>;\n}\n\n/** Progress tracker for a multi-challenger operation. */\nexport interface ChallengerProgress {\n total_challengers: number;\n completed_challengers: number;\n completed_challenger_pubkeys: string[];\n pending_challenger_pubkeys: string[];\n}\n\nexport type GcDataProgress = ChallengerProgress;\nexport type AckCollectionProgress = ChallengerProgress;\n\n/** Extended presigning progress with all 3 concurrent phases. */\nexport interface PresigningProgress extends ChallengerProgress {\n depositor_graph_created?: boolean;\n vk_challenger_presigning_completed?: number;\n vk_challenger_presigning_total?: number;\n}\n\n/** Detailed progress breakdown for an in-progress pegin. */\nexport interface PeginProgressDetails {\n gc_data?: GcDataProgress;\n presigning?: PresigningProgress;\n ack_collection?: AckCollectionProgress;\n claimer_graphs?: ClaimerGraphStatus[];\n}\n\n/** Per-claimer graph status (challenger perspective). */\nexport interface ClaimerGraphStatus {\n claimer_pubkey: string;\n presigned: boolean;\n}\n\n/** Response from `getPeginStatus`. */\nexport interface GetPeginStatusResponse {\n pegin_txid: string;\n status: string;\n progress: PeginProgressDetails;\n health_info: string;\n last_error?: string;\n}\n\n// ============================================================================\n// Pegout Types\n// ============================================================================\n\n/** Params for querying pegout status from the VP daemon. */\nexport interface GetPegoutStatusParams {\n pegin_txid: string;\n}\n\n/** Claimer-side pegout progress. */\nexport interface ClaimerPegoutStatus {\n status: string;\n failed: boolean;\n claim_txid?: string;\n claimer_pubkey?: string;\n challenger_pubkey?: string;\n created_at?: string;\n updated_at?: string;\n}\n\n/** Challenger-side pegout progress. */\nexport interface ChallengerPegoutStatus {\n status: string;\n claim_txid?: string;\n claimer_pubkey?: string;\n assert_txid?: string;\n challenge_assert_txid?: string;\n nopayout_txid?: string;\n created_at?: string;\n updated_at?: string;\n}\n\n/** Response from `getPegoutStatus`. */\nexport interface GetPegoutStatusResponse {\n pegin_txid: string;\n found: boolean;\n claimer?: ClaimerPegoutStatus;\n challenger?: ChallengerPegoutStatus;\n}\n\n// ============================================================================\n// Error Codes\n// ============================================================================\n\n/** JSON-RPC error codes returned by the vault provider. */\nexport enum RpcErrorCode {\n DATABASE_ERROR = -32005,\n PRESIGN_ERROR = -32006,\n JSON_SERIALIZATION_ERROR = -32007,\n TX_GRAPH_ERROR = -32008,\n INVALID_GRAPH = -32009,\n VALIDATION_ERROR = -32010,\n NOT_FOUND = -32011,\n INTERNAL_ERROR = -32603,\n}\n"],"names":["BTCVaultRegistryABI","JsonRpcError","code","message","JSON_RPC_ERROR_CODES","JSON_RPC_VERSION","DEFAULT_RETRY_ATTEMPTS","DEFAULT_RETRY_DELAY_MS","RETRYABLE_HTTP_STATUS_CODES","DEFAULT_RETRYABLE_METHODS","defaultRetryableFor","method","JsonRpcClient","config","__publicField","params","signal","response","jsonResponse","rpcResponse","err","callerSignal","requestId","maxRetries","body","lastError","attempt","timeoutController","timeoutId","merged","mergeAbortSignals","delay","error","ms","resolve","reject","onAbort","a","b","controller","onAbortA","onAbortB","cleanup","DaemonStatus","PRE_DEPOSITOR_SIGNATURES_STATES","POST_PAYOUT_SIGNATURE_STATUSES","VP_TRANSIENT_STATUSES","VP_TERMINAL_STATUSES","POST_WOTS_STAT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