Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions DashWallet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1118,9 +1118,16 @@
AA00100B2CA0B10001A0B10B /* SwapKitAPIService.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA00100A2CA0B10001A0B10A /* SwapKitAPIService.swift */; };
AA00100C2CA0B10001A0B10C /* SwapKitAPIService.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA00100A2CA0B10001A0B10A /* SwapKitAPIService.swift */; };
AA00100E2CA0B10001A0B10E /* SwapKitQuoteDecodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA00100D2CA0B10001A0B10D /* SwapKitQuoteDecodingTests.swift */; };
C5A11E000000000000000005 /* SwapRouteAvailabilityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5A11E000000000000000004 /* SwapRouteAvailabilityTests.swift */; };
AA00100F2CA0B10001A0B10F /* swapkit_quote_response.json in Resources */ = {isa = PBXBuildFile; fileRef = AA0010102CA0B10001A0B110 /* swapkit_quote_response.json */; };
AA0010122CA0B10001A0B112 /* SwapKitSwapProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0010112CA0B10001A0B111 /* SwapKitSwapProvider.swift */; };
AA0010132CA0B10001A0B113 /* SwapKitSwapProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0010112CA0B10001A0B111 /* SwapKitSwapProvider.swift */; };
C5A11E000000000000000002 /* SwapRouteAvailability.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5A11E000000000000000001 /* SwapRouteAvailability.swift */; };
C5A11E000000000000000003 /* SwapRouteAvailability.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5A11E000000000000000001 /* SwapRouteAvailability.swift */; };
C5A11E0000000000000000F2 /* SwapKitFailureDetail.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5A11E0000000000000000F1 /* SwapKitFailureDetail.swift */; };
C5A11E0000000000000000F3 /* SwapKitFailureDetail.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5A11E0000000000000000F1 /* SwapKitFailureDetail.swift */; };
C5A11E0000000000000000F7 /* SelectCoinViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5A11E0000000000000000F6 /* SelectCoinViewModelTests.swift */; };
C5A11E0000000000000000F5 /* SwapKitFailureDetailTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5A11E0000000000000000F4 /* SwapKitFailureDetailTests.swift */; };
AA0020012FA0000000000014 /* SwapExecutionData.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0020012FA0000000000013 /* SwapExecutionData.swift */; };
AA0020012FA0000000000015 /* SwapExecutionData.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0020012FA0000000000013 /* SwapExecutionData.swift */; };
AA0020032FA0000000000003 /* SwapProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0020022FA0000000000002 /* SwapProvider.swift */; };
Expand Down Expand Up @@ -3264,8 +3271,13 @@
AA0010072CA0B10001A0B107 /* SwapKitModels.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwapKitModels.swift; sourceTree = "<group>"; };
AA00100A2CA0B10001A0B10A /* SwapKitAPIService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwapKitAPIService.swift; sourceTree = "<group>"; };
AA00100D2CA0B10001A0B10D /* SwapKitQuoteDecodingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwapKitQuoteDecodingTests.swift; sourceTree = "<group>"; };
C5A11E000000000000000004 /* SwapRouteAvailabilityTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwapRouteAvailabilityTests.swift; sourceTree = "<group>"; };
AA0010102CA0B10001A0B110 /* swapkit_quote_response.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = swapkit_quote_response.json; sourceTree = "<group>"; };
AA0010112CA0B10001A0B111 /* SwapKitSwapProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwapKitSwapProvider.swift; sourceTree = "<group>"; };
C5A11E000000000000000001 /* SwapRouteAvailability.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwapRouteAvailability.swift; sourceTree = "<group>"; };
C5A11E0000000000000000F1 /* SwapKitFailureDetail.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwapKitFailureDetail.swift; sourceTree = "<group>"; };
C5A11E0000000000000000F6 /* SelectCoinViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectCoinViewModelTests.swift; sourceTree = "<group>"; };
C5A11E0000000000000000F4 /* SwapKitFailureDetailTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwapKitFailureDetailTests.swift; sourceTree = "<group>"; };
AA0020012FA0000000000013 /* SwapExecutionData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwapExecutionData.swift; sourceTree = "<group>"; };
AA0020022FA0000000000002 /* SwapProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwapProvider.swift; sourceTree = "<group>"; };
AA0020052FA0000000000005 /* SwapBackend.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwapBackend.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -7393,10 +7405,13 @@
7A11E5E70000000000000011 /* ResetWalletInfoWipeForwardingTests.swift */,
CB9200012FE3000000000001 /* PassiveWalletStateUITailTests.swift */,
AA0003032CA0F58E00A1B402 /* SwapAddressValidatorTests.swift */,
C5A11E0000000000000000F6 /* SelectCoinViewModelTests.swift */,
C5A11E0000000000000000F4 /* SwapKitFailureDetailTests.swift */,
5A7E0C1D2E3F405162738496 /* VotingKeyFormatTests.swift */,
5A7E0C1D2E3F405162738498 /* VotingKeyImportTests.swift */,
AA00F1002FF0A10000A1B402 /* ExchangeAddressLookupContextTests.swift */,
AA00100D2CA0B10001A0B10D /* SwapKitQuoteDecodingTests.swift */,
C5A11E000000000000000004 /* SwapRouteAvailabilityTests.swift */,
5EAD222130A0000000A1B202 /* DashAmountFormatterTests.swift */,
DA57A1010000000000000001 /* PastedAmountNormalizationTests.swift */,
DA5A00000000000000000005 /* DashPayIdentityKeysTests.swift */,
Expand Down Expand Up @@ -7671,6 +7686,8 @@
AA0010072CA0B10001A0B107 /* SwapKitModels.swift */,
AA00100A2CA0B10001A0B10A /* SwapKitAPIService.swift */,
AA0010112CA0B10001A0B111 /* SwapKitSwapProvider.swift */,
C5A11E000000000000000001 /* SwapRouteAvailability.swift */,
C5A11E0000000000000000F1 /* SwapKitFailureDetail.swift */,
);
path = SwapKit;
sourceTree = "<group>";
Expand Down Expand Up @@ -10467,6 +10484,8 @@
AA0010082CA0B10001A0B108 /* SwapKitModels.swift in Sources */,
AA00100B2CA0B10001A0B10B /* SwapKitAPIService.swift in Sources */,
AA0010122CA0B10001A0B112 /* SwapKitSwapProvider.swift in Sources */,
C5A11E000000000000000002 /* SwapRouteAvailability.swift in Sources */,
C5A11E0000000000000000F2 /* SwapKitFailureDetail.swift in Sources */,
AA0020012FA0000000000014 /* SwapExecutionData.swift in Sources */,
AA00020E2BA0F58E00A1B305 /* SelectCoinViewModel.swift in Sources */,
AA0002142BA0F58E00A1B307 /* SelectCoinView.swift in Sources */,
Expand Down Expand Up @@ -10711,10 +10730,13 @@
7A11E5E70000000000000012 /* ResetWalletInfoWipeForwardingTests.swift in Sources */,
CB9200022FE3000000000002 /* PassiveWalletStateUITailTests.swift in Sources */,
AA0003042CA0F58E00A1B402 /* SwapAddressValidatorTests.swift in Sources */,
C5A11E0000000000000000F7 /* SelectCoinViewModelTests.swift in Sources */,
C5A11E0000000000000000F5 /* SwapKitFailureDetailTests.swift in Sources */,
5A7E0C1D2E3F405162738495 /* VotingKeyFormatTests.swift in Sources */,
5A7E0C1D2E3F405162738497 /* VotingKeyImportTests.swift in Sources */,
AA00F1012FF0A10000A1B402 /* ExchangeAddressLookupContextTests.swift in Sources */,
AA00100E2CA0B10001A0B10E /* SwapKitQuoteDecodingTests.swift in Sources */,
C5A11E000000000000000005 /* SwapRouteAvailabilityTests.swift in Sources */,
5EAD222230A0000000A1B202 /* DashAmountFormatterTests.swift in Sources */,
DA57A1010000000000000002 /* PastedAmountNormalizationTests.swift in Sources */,
DA5A00000000000000000004 /* DashPayIdentityKeysTests.swift in Sources */,
Expand Down Expand Up @@ -11534,6 +11556,8 @@
AA0010092CA0B10001A0B109 /* SwapKitModels.swift in Sources */,
AA00100C2CA0B10001A0B10C /* SwapKitAPIService.swift in Sources */,
AA0010132CA0B10001A0B113 /* SwapKitSwapProvider.swift in Sources */,
C5A11E000000000000000003 /* SwapRouteAvailability.swift in Sources */,
C5A11E0000000000000000F3 /* SwapKitFailureDetail.swift in Sources */,
AA0020012FA0000000000015 /* SwapExecutionData.swift in Sources */,
AA00020F2BA0F58E00A1B305 /* SelectCoinViewModel.swift in Sources */,
AA0002152BA0F58E00A1B307 /* SelectCoinView.swift in Sources */,
Expand Down
1 change: 0 additions & 1 deletion DashWallet/Sources/Models/Maya/MayaSwapProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import Foundation
/// Maps Maya-specific DTOs (`MayaSwapQuote`, `MayaSwapTransactionInfo`) to the neutral result types.
final class MayaSwapProvider: SwapProvider {
var displayName: String { "Maya" }
var onBuyRoutabilityChanged: (() -> Void)?

func fetchPools() async throws -> [SwapPool] {
try await MayaAPIService.shared.fetchPools()
Expand Down
34 changes: 29 additions & 5 deletions DashWallet/Sources/Models/Swap/SwapKitErrorCopy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ enum SwapKitErrorCopy {
)
}

// A provider's trading halt (`swapHalted` today) is not something the user can fix with a
// different amount or address: the coin cannot be swapped until the provider resumes.
if isHaltedCode(code) {
return unavailableNowMessage(coin: coin)
}

switch code {
// `memoTooLongForSourceChain` is SwapKit's own name for what the wallet also detects
// locally before broadcasting; both mean the OP_RETURN will not fit.
Expand All @@ -85,10 +91,7 @@ enum SwapKitErrorCopy {
comment: "Dash DEX / dex_error_no_route"
)
case "blacklistasset", "invalidasset":
return String(format: NSLocalizedString(
"%@ can't be swapped at the moment.",
comment: "Dash DEX / dex_error_blacklisted"
), coin.code)
return unavailableNowMessage(coin: coin)
case "invalidrequest", "validation_error":
return NSLocalizedString(
"We couldn't set up your swap. Please check the amount and address, then try again.",
Expand Down Expand Up @@ -163,7 +166,9 @@ enum SwapKitErrorCopy {
/// amount far below a route's floor as well as for a pair it cannot carry: measured
/// 2026-08-28, DASH → BTC returned it at 0.01 DASH, `sellAssetAmountTooSmall` (min 0.175) at
/// 0.05, and a route at 0.3. Callers must not read the top-level form as "this cannot be
/// swapped at all" — hence the neutral copy, and `routability(from:)` ignoring it.
/// swapped at all" — hence the neutral copy. The live-availability probes
/// (`LiveRouteState.classify`, `classifyPerProvider`) take it as conclusive because they are sized
/// well above every route floor; the address screen re-asks without the address before trusting it.
static func isNoRoute(_ rawError: String?) -> Bool {
code(of: rawError) == noRoutesFoundCode
}
Expand All @@ -176,6 +181,25 @@ enum SwapKitErrorCopy {
isBelowMinimumCode(code(of: rawError))
}

/// "<coin> can't be swapped at the moment." — for a coin no provider can carry right now, where
/// neither the amount nor the address is the user's to change.
static func unavailableNowMessage(coin: SwapCryptoCurrency) -> String {
String(format: NSLocalizedString(
"%@ can't be swapped at the moment.",
comment: "Dash DEX / dex_error_blacklisted"
), coin.code)
}

/// True when a provider reported a trading halt for the pair (`swapHalted`, as Maya does while a
/// chain's trading is paused). SwapKit does not enumerate the halt codes, so the family is matched.
static func isHalted(_ rawError: String?) -> Bool {
isHaltedCode(code(of: rawError))
}

private static func isHaltedCode(_ code: String) -> Bool {
code.contains("halted")
}

private static func isBelowMinimumCode(_ code: String) -> Bool {
code.hasSuffix("amounttoosmall") || code.hasSuffix("amounttoolow")
}
Expand Down
33 changes: 27 additions & 6 deletions DashWallet/Sources/Models/Swap/SwapProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// limitations under the License.
//

import Combine
import Foundation

// MARK: - Result types
Expand Down Expand Up @@ -136,12 +137,24 @@ extension SwapProvider {
/// Returns a map of asset identifier (uppercased) → routing network label
/// for display in the coin picker ("Maya", "NEAR", "Multiple networks").
/// Default: empty — non-SwapKit providers don't have multi-provider routing.
func networkLabels(for pools: [SwapPool]) async -> [String: String] { [:] }
func networkLabels(for pools: [SwapPool], direction: SwapDirection) async -> [String: String] { [:] }

/// Returns the set of asset identifiers (uppercased) that are halted due to
/// provider-specific halt logic (e.g. Maya chain halt for mayaOnly assets).
/// provider-specific halt logic (SwapKit: no provider can carry the coin right now).
/// Default: empty set (use chain-based halt from inbound addresses).
func haltedAssets(from inboundAddresses: [SwapInboundAddress], pools: [SwapPool]) async -> Set<String> { [] }
func haltedAssets(from inboundAddresses: [SwapInboundAddress], pools: [SwapPool], direction: SwapDirection) async -> Set<String> { [] }

/// Starts checking, in the background, whether the shown coins can be swapped right now.
/// Default: nothing to check — the provider's pools and inbound addresses are already live.
func refreshRouteAvailability(for pools: [SwapPool], direction: SwapDirection) {}

/// Stops the background check started by `refreshRouteAvailability`. Default: nothing runs.
func stopRouteAvailabilityRefresh(direction: SwapDirection) {}

/// Directions whose live route availability changed. Default: never.
var routeAvailabilityChanges: AnyPublisher<SwapDirection, Never> {
Empty().eraseToAnyPublisher()
}
}

// MARK: - Protocol
Expand All @@ -156,7 +169,9 @@ protocol SwapProvider {
var displayName: String { get }
var usesGenericFeeLabel: Bool { get }
var buildsSwapKitDeposit: Bool { get }
var onBuyRoutabilityChanged: (() -> Void)? { get set }
/// Directions whose live route availability changed, so the picker can re-label. MUST be a
/// protocol requirement for dynamic dispatch.
var routeAvailabilityChanges: AnyPublisher<SwapDirection, Never> { get }

func fetchPools() async throws -> [SwapPool]
func fetchInboundAddresses() async throws -> [SwapInboundAddress]
Expand All @@ -167,10 +182,16 @@ protocol SwapProvider {
func fetchPools(direction: SwapDirection) async throws -> [SwapPool]

/// Routing network labels per asset. MUST be a protocol requirement for dynamic dispatch.
func networkLabels(for pools: [SwapPool]) async -> [String: String]
func networkLabels(for pools: [SwapPool], direction: SwapDirection) async -> [String: String]

/// Provider-specific halted assets. MUST be a protocol requirement for dynamic dispatch.
func haltedAssets(from inboundAddresses: [SwapInboundAddress], pools: [SwapPool]) async -> Set<String>
func haltedAssets(from inboundAddresses: [SwapInboundAddress], pools: [SwapPool], direction: SwapDirection) async -> Set<String>

/// Live route checks for the shown coins. MUST be a protocol requirement for dynamic dispatch.
func refreshRouteAvailability(for pools: [SwapPool], direction: SwapDirection)

/// Stops the live route checks for `direction`. MUST be a protocol requirement for dynamic dispatch.
func stopRouteAvailabilityRefresh(direction: SwapDirection)

/// Returns `nil` if the destination address is valid, otherwise an error string.
func validateAddress(destination: String, toAsset: String) async -> String?
Expand Down
56 changes: 56 additions & 0 deletions DashWallet/Sources/Models/SwapKit/SwapKitFailureDetail.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// SwapKitFailureDetail.swift
// DashWallet
//
// Copyright © 2026 Dash Core Group. All rights reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation

/// One log line about a failed SwapKit request. `HTTPClientError.statusCode` renders as
/// "Status Code: 403, Data Length: 4545", which cannot tell SwapKit's own refusal (a JSON body) from
/// a page served in front of it (an HTML block page). This names the path (never the query or the
/// headers, so never the API key) and the body: an HTML page by its `<title>` (any case, with or
/// without attributes; an empty one falls back to the body), anything else by its first characters
/// on one line.
enum SwapKitFailureDetail {
static let excerptLength = 200

static func describe(_ error: Error) -> String {
guard let clientError = error as? HTTPClientError, case .statusCode(let response) = clientError else {
return error.localizedDescription
}
let path = response.request?.url?.path ?? "?"
return "HTTP \(response.statusCode) \(path) (\(response.data.count) bytes): \(excerpt(of: response.data))"
}

private static func excerpt(of data: Data) -> String {
guard let body = String(data: data, encoding: .utf8) else { return "<non-utf8 body>" }
let text = title(in: body).flatMap { $0.allSatisfy(\.isWhitespace) ? nil : $0 } ?? body
let line = text.split(whereSeparator: \.isWhitespace).joined(separator: " ")
return line.count > excerptLength ? line.prefix(excerptLength) + "…" : line
}

private static func title(in html: String) -> String? {
guard let open = html.range(of: "<title", options: .caseInsensitive),
let tagEnd = html.range(of: ">", range: open.upperBound..<html.endIndex),
let close = html.range(of: "</title>", options: .caseInsensitive,
range: tagEnd.upperBound..<html.endIndex)
else {
return nil
}
return String(html[tagEnd.upperBound..<close.lowerBound])
}
}
Loading
Loading