/** * Business Rules Math Module * * Pure calculation functions for Diamond Hands Protocol business logic. * These functions contain no side effects - they only perform calculations. * * Used by vault-snapshot and other modules for consistent math across the system. */ /** * Calculate term start timestamp from expiry timestamp and term length * * Contract stores expiryAt but not termStartTimestamp. * We derive it: termStart = expiry - (termLength * 30 days) * * Uses constants to avoid magic numbers and ensure accuracy. * * @param expiryTimestamp - Unix timestamp when term expires (0 if no term) * @param termLengthMonths - Term length in months * @returns Unix timestamp when term started (0 if no term) */ export declare function calculateTermStartFromExpiry(expiryTimestamp: number, termLengthMonths: number): number; /** * Calculate term status from timestamps * * Determines: * - How long into the term we are * - Whether term is expired * - Days until expiry or days into grace period * * @param termStartTimestamp - Unix timestamp when term started (0 if not started) * @param selectedTermMonths - Term length in months * @returns Complete term status metrics */ export declare function calculateTermStatus(termStartTimestamp: number, selectedTermMonths: number): { termDurationDays: number; termLengthDays: number; isExpired: boolean; daysUntilExpiry: number; daysIntoGracePeriod: number; }; /** * Calculate current liquidation threshold with grace period escalation * * Active loan: uses on-chain value from LoanOperationsManager.liquidationThreshold() * Expired: * Day 0: 110% (11000 bps) * Day 30: maxExpiredThresholdBps (read from LiquidationManager on-chain) * Quadratic escalation between * * Uses integer-only math for deterministic results across all platforms. * * @param isExpired - Whether loan is expired * @param daysIntoGracePeriod - Days past expiry (0 if not expired) * @param activeLoanThresholdBps - On-chain active loan threshold from LoanOperationsManager * @param maxExpiredThresholdBps - On-chain max escalated threshold from LiquidationManager * @returns Liquidation threshold in basis points */ export declare function calculateLiquidationThreshold(isExpired: boolean, daysIntoGracePeriod: number, activeLoanThresholdBps: number, maxExpiredThresholdBps?: number): number; /** * Calculate collateral metrics from BTC balance and debt * * Converts BTC balance to USD value and calculates collateral ratio. * Uses BigInt for all calculations to avoid JavaScript number precision issues. * * @param availableBTCSats - Available BTC balance in satoshis * @param btcPriceUsd - BTC price with 8 decimals (e.g., 10000000000000 = $100,000) * @param ucdDebt - UCD debt in wei (18 decimals) * @returns Collateral value in USD and ratio in basis points */ export declare function calculateCollateralMetrics(availableBTCSats: bigint, btcPriceUsd: bigint, ucdDebt: bigint): { collateralValueUsd: bigint; collateralRatioBps: number; }; /** * Check if adding additional debt would maintain required collateral ratio * * Used by minting authorization to validate new mints. * * @param collateralValueUsd - Current collateral value in USD (whole dollars) * @param currentDebt - Current UCD debt in wei (18 decimals) * @param additionalDebt - Additional UCD debt to add in wei (18 decimals) * @param requiredRatioBps - Required collateral ratio in basis points (e.g., 11000 for 110%) * @returns True if sufficient collateral exists after adding debt */ export declare function hasSufficientCollateralForDebt(collateralValueUsd: bigint, currentDebt: bigint, additionalDebt: bigint, requiredRatioBps: number): boolean; /** * Calculate fee from amount using basis points * * Bulletproof fee calculation with validation. * Multiply before divide to maintain precision. * * @param amount - Principal amount (in wei or sats) * @param feeBps - Fee in basis points (e.g., 50 = 0.5%) * @returns Fee amount in same units as input * * @example * calculateFeeBps(1000000000000000000n, 50) // 0.5% of 1 ETH * // Returns: 5000000000000000n (0.005 ETH) */ export declare function calculateFeeBps(amount: bigint, feeBps: number): bigint; /** * Calculate USD value of BTC amount * * Converts satoshis to USD using price with 8 decimals. * Result maintains 8 decimal precision. * * @param btcSats - Bitcoin amount in satoshis * @param btcPriceUsd8Decimals - BTC price with 8 decimals (e.g., 9800000000000n = $98,000) * @returns USD value with 8 decimals * * @example * calculateBtcValueUsd(100000000n, 9800000000000n) // 1 BTC at $98,000 * // Returns: 9800000000000n ($98,000 with 8 decimals) */ export declare function calculateBtcValueUsd(btcSats: bigint, btcPriceUsd8Decimals: bigint): bigint; /** * Calculate collateral ratio in basis points (with 8-decimal collateral and 18-decimal debt) * * Used for UCD mint validation where collateral is in USD (8 decimals) * and debt is in UCD wei (18 decimals). * * @param collateralValueUsd8Decimals - Collateral value in USD with 8 decimals * @param debtUsd18Decimals - Debt amount in USD with 18 decimals (UCD wei) * @returns Collateral ratio in basis points (e.g., 20000 = 200%) * * @example * calculateCollateralRatioBps(9800000000000n, 4000000000000000000000n) * // $98,000 collateral / $4,000 debt = 24.5 = 2450% = 24500 bps * // Returns: 24500 * * Note: Returns MAX_SAFE_INTEGER for extreme over-collateralization cases */ export declare function calculateCollateralRatioBps(collateralValueUsd8Decimals: bigint, debtUsd18Decimals: bigint): number; /** * Check if collateral ratio meets minimum requirement * * @param collateralValueUsd8Decimals - Collateral value in USD with 8 decimals * @param debtUsd18Decimals - Debt amount in USD with 18 decimals * @param minimumRatioBps - Minimum required ratio in basis points (e.g., 20000 = 200%) * @returns True if collateral is sufficient * * @example * isCollateralSufficient(20000000000000n, 10000000000000000000000n, 20000) * // $200,000 collateral / $10,000 debt = 2000% >= 200% * // Returns: true */ export declare function isCollateralSufficient(collateralValueUsd8Decimals: bigint, debtUsd18Decimals: bigint, minimumRatioBps: number): boolean; /** * Convert basis points to percentage (for display) * * @param basisPoints - Value in basis points * @returns Percentage as number (e.g., 20000 bps => 200) * * @example * bpsToPercent(20000) // Returns: 200 * bpsToPercent(50) // Returns: 0.5 */ export declare function bpsToPercent(basisPoints: number): number; /** * Refuse an AGENT-authorized withdrawal that would take the position below the borrower's * delegation floor. * * WHY THIS IS A NAMED FUNCTION. It is the enforcement point whose absence makes * `agent-module-executor` refuse `mode="execute"` for `op="withdraw"` outright. That guard's * removal condition is *"amount-vs-floor enforcement is actually wired somewhere in the chain and * covered by a test that proves a floor-violating amount is refused"* — so the invariant needs a * name and a test, not an inline `if` inside a 900-line action. * * SCOPE. `effectiveFloorBps` is the borrower's own limit on how far an AGENT may take the * position, clamped by the governance minimum, and it is populated ONLY when the Safe-agent arm * authorized the call (`checkSafeAgentWithdrawAuthorization` sets `path.outcome`). A * borrower-signed or EIP-1271 withdrawal leaves it 0 and is governed by the protocol's * liquidation threshold alone — hence the `<= 0` early return, which is the non-agent path and * not a bypass. * * ZERO DEBT IS OUT OF SCOPE, deliberately. The caller applies this only where `ucdDebt > 0`, * because the floor is a RATIO and the post-withdrawal ratio is infinite for any amount once debt * is zero — there is nothing for it to bound, the protocol has no claim on the collateral, and * gating the scope on debt would leave a repaid multi-sig unable to retrieve its own BTC inside * the 60s `strictCurrentQuantum` window. What bounds the zero-debt case is the quorum-gated * destination allowlist. Never describe this floor to a user as a cap on withdrawal size. * * @param effectiveFloorBps Post-withdrawal collateral-ratio floor from the delegation record. * 0 (or negative) means "no agent floor applies" — see SCOPE above. * @param postWithdrawalCollateralRatioBps The ratio the position would hold AFTER this withdrawal. * @throws When an agent floor applies and the withdrawal would breach it. */ export declare function assertAgentWithdrawFloor(effectiveFloorBps: number, postWithdrawalCollateralRatioBps: number): void; //# sourceMappingURL=business-rules-math.module.d.ts.map