diff --git a/DashWallet/Sources/UI/Payments/InternalTransfer/InternalTransferViewModel.swift b/DashWallet/Sources/UI/Payments/InternalTransfer/InternalTransferViewModel.swift index 11599ae08b..b718753e57 100644 --- a/DashWallet/Sources/UI/Payments/InternalTransfer/InternalTransferViewModel.swift +++ b/DashWallet/Sources/UI/Payments/InternalTransfer/InternalTransferViewModel.swift @@ -1575,6 +1575,21 @@ final class InternalTransferViewModel: ObservableObject { } } + /// `coreTransferFundsMessage` for the typed amount: the caller gives its + /// route's ceiling and the cost the route adds on top of the L1 reserve; + /// the L1 reserve is the cached one and the pooled figures are read live. + private func coreRouteFundsMessage(ceilingDuffs: UInt64, routeCostDuffs: UInt64) -> String? { + let state = SwiftDashSDKWalletState.shared + return Self.coreTransferFundsMessage( + requestedDuffs: dashDuffsUnsigned, + ceilingDuffs: ceilingDuffs, + sendableDuffs: state.sendableDuffs, + l1ReserveDuffs: coreL1FeeReserveDuffs, + routeCostDuffs: routeCostDuffs, + pooledKnown: state.pooledSpendableDuffs != nil, + pool: SwiftDashSDKTransactionSender.sendPoolConfirmationSnapshot) + } + /// Inline, user-facing explanation for an amount rejected before Confirm. /// Zero stays quiet while the user has not entered an amount; a /// fee-estimation failure fails closed with a generic retry. @@ -1618,18 +1633,11 @@ final class InternalTransferViewModel: ObservableObject { guard let feeDuffs = CoreToShieldedAmountPolicy.currentPoolFeeDuffs else { return Self.feeEstimateUnavailableMessage } - let ceilingDuffs = SwiftDashSDKWalletState.feeAwareMax( - spendable: coreSpendableDuffs, - reserve: feeDuffs) - let state = SwiftDashSDKWalletState.shared - return Self.coreTransferFundsMessage( - requestedDuffs: dashDuffsUnsigned, - ceilingDuffs: ceilingDuffs, - sendableDuffs: state.sendableDuffs, - l1ReserveDuffs: coreL1FeeReserveDuffs, - routeCostDuffs: feeDuffs, - pooledKnown: state.pooledSpendableDuffs != nil, - pool: SwiftDashSDKTransactionSender.sendPoolConfirmationSnapshot) + return coreRouteFundsMessage( + ceilingDuffs: SwiftDashSDKWalletState.feeAwareMax( + spendable: coreSpendableDuffs, + reserve: feeDuffs), + routeCostDuffs: feeDuffs) case .coreToPlatform: // The static address-funding reserve rides on top of the amount, @@ -1640,18 +1648,11 @@ final class InternalTransferViewModel: ObservableObject { } let routeReserveDuffs = CoreToPlatformAmountPolicy.reserveDuffs( reserveCredits: reserveCredits) - let ceilingDuffs = CoreToPlatformAmountPolicy.maxAmountDuffs( - spendableDuffs: coreSpendableDuffs, - reserveCredits: reserveCredits) - let state = SwiftDashSDKWalletState.shared - return Self.coreTransferFundsMessage( - requestedDuffs: dashDuffsUnsigned, - ceilingDuffs: ceilingDuffs, - sendableDuffs: state.sendableDuffs, - l1ReserveDuffs: coreL1FeeReserveDuffs, - routeCostDuffs: routeReserveDuffs, - pooledKnown: state.pooledSpendableDuffs != nil, - pool: SwiftDashSDKTransactionSender.sendPoolConfirmationSnapshot) + return coreRouteFundsMessage( + ceilingDuffs: CoreToPlatformAmountPolicy.maxAmountDuffs( + spendableDuffs: coreSpendableDuffs, + reserveCredits: reserveCredits), + routeCostDuffs: routeReserveDuffs) case .platformToShielded: if awaitingPlatformShieldResync { @@ -1781,10 +1782,12 @@ final class InternalTransferViewModel: ObservableObject { } switch source { case .core: - return TransferSpendAmountPolicy.insufficientBalanceMessage( - balanceName: source.balanceName, - requestedDuffs: dashDuffsUnsigned, - spendableDuffs: coreSpendableDuffs) + // The SDK builds the top-up's asset lock with the same pooled + // funding as Core → Platform; the lock holds nothing back beyond + // the ordinary L1 reserve, so the ceiling is the cached envelope. + return coreRouteFundsMessage( + ceilingDuffs: coreSpendableDuffs, + routeCostDuffs: 0) case .platform: guard let ceiling = platformIdentityFundableDuffs else { return Self.feeEstimateUnavailableMessage diff --git a/DashWalletTests/PooledSendableBalanceTests.swift b/DashWalletTests/PooledSendableBalanceTests.swift index d1ad4067e9..70f736f836 100644 --- a/DashWalletTests/PooledSendableBalanceTests.swift +++ b/DashWalletTests/PooledSendableBalanceTests.swift @@ -367,6 +367,86 @@ final class PooledSendableBalanceTests: XCTestCase { XCTAssertEqual(message, "Insufficient balance") } + // The Core → Identity top-up adds no route cost: only the L1 reserve + // rides on top of the amount, and the ceiling is the cached envelope. + + func testNoRouteCostReturnsStillConfirmingWhenTheL1ReserveFits() { + let message = coreRouteMessage( + requestedDuffs: 110_000_000, + routeCostDuffs: 0, + pool: pool(confirmed: 100_000_000, unconfirmed: 20_000_000)) + + XCTAssertEqual(message, SendAmountError.fundsStillConfirming(20_000_000).errorDescription) + } + + func testNoRouteCostReturnsInsufficientWhenTheL1ReserveUsesTheLastRoom() { + let message = coreRouteMessage( + requestedDuffs: 119_500_000, + routeCostDuffs: 0, + pool: pool(confirmed: 100_000_000, unconfirmed: 20_000_000)) + + XCTAssertEqual(message, "Insufficient balance") + } + + func testNoRouteCostHandlesConfirmedFundsBelowTheL1Reserve() { + let message = coreRouteMessage( + requestedDuffs: 90_000_000, + cachedSpendableDuffs: 0, + sendableDuffs: 50_000, + routeCostDuffs: 0, + pool: pool(confirmed: 50_000, unconfirmed: 100_000_000)) + + XCTAssertEqual(message, SendAmountError.fundsStillConfirming(100_000_000).errorDescription) + } + + func testNoRouteCostHandlesTheFirstReceiveIntoAnEmptyWallet() { + // 1.5M of room after confirmation: the 1M L1 reserve fits, any route + // cost above 0.5M would not. + let message = coreRouteMessage( + requestedDuffs: 98_500_000, + cachedSpendableDuffs: 0, + sendableDuffs: 0, + routeCostDuffs: 0, + pool: pool(confirmed: 0, unconfirmed: 100_000_000)) + + XCTAssertEqual(message, SendAmountError.fundsStillConfirming(100_000_000).errorDescription) + } + + func testNoRouteCostReturnsInsufficientWhenTheAmountCannotFitAfterConfirmation() { + let message = coreRouteMessage( + requestedDuffs: 121_000_000, + routeCostDuffs: 0, + pool: pool(confirmed: 100_000_000, unconfirmed: 20_000_000)) + + XCTAssertEqual(message, "Insufficient balance") + } + + func testNoRouteCostDoesNotWaitWhileThePooledFigureIsUnknown() { + // A pool that would answer "still confirming" if it were read. + var poolReads = 0 + let snapshot = pool(confirmed: 100_000_000, unconfirmed: 20_000_000) + let message = InternalTransferViewModel.coreTransferFundsMessage( + requestedDuffs: 110_000_000, + ceilingDuffs: 99_000_000, + sendableDuffs: 100_000_000, + l1ReserveDuffs: 1_000_000, + routeCostDuffs: 0, + pooledKnown: false, + pool: { poolReads += 1; return snapshot }) + + XCTAssertEqual(message, "Insufficient balance") + XCTAssertEqual(poolReads, 0) + } + + func testNoRouteCostReturnsStillConfirmingWhenTheL1ReserveTakesExactlyTheRoomLeft() { + let message = coreRouteMessage( + requestedDuffs: 119_000_000, + routeCostDuffs: 0, + pool: pool(confirmed: 100_000_000, unconfirmed: 20_000_000)) + + XCTAssertEqual(message, SendAmountError.fundsStillConfirming(20_000_000).errorDescription) + } + func testAGapWithNothingConfirmingIsNeverCalledAWait() { // The pool holds more than the gate counts, but none of it is // confirming — a pooled figure not re-read yet, or an account the