Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : App/Sources/Wallet/SendResultView.swift8// Module : Wallet9// Repository : https://git.spboucher.ai/qyvora10// Project Website : Not specified11// Created : 2026-12-0512// Last Modified : 2026-12-0513//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI42import QyvoraCore43import QyvoraWallet44import QyvoraUI4546/// Outcome of a broadcast, followed live with the service's `track`: seen, in a block, final, failed or expired —47/// in the chain's own terms. While the transaction can still be executed nothing here offers to send again.48struct SendResultView: View {49 let result: SendResult50 /// "0.01 ETH", when the flow knows what was sent.51 let amountLine: String?52 let onDone: () -> Void53 /// nil where a new payment makes no sense (a handed-over plan, an imported signed transaction).54 let onSendAnother: (() -> Void)?5556 @Environment(WalletStore.self) private var wallet57 @State private var status: TransactionStatus?58 @State private var trackError: String?59 @State private var checking = false60 @State private var checks = 06162 var chain: ChainDescriptor? { result.chain.descriptor }63 var family: ChainFamily { chain?.family ?? .evm }6465 /// True once the chain has settled the transaction one way or the other.66 var isSettled: Bool {67 switch status {68 case .confirmed?, .finalized?, .failed?, .expired?: return true69 default: return false70 }71 }7273 var body: some View {74 SendScaffold(title: "", subtitle: nil, onBack: nil, onClose: onDone) {75 ResultView(status: mark, title: title, message: message, identifierLabel: identifierLabel, identifier: result.id, network: chain?.qNetwork) {76 EmptyView()77 }78 .frame(maxWidth: .infinity)79 .privacySensitive()80 statusSection81 if let url = result.explorerURL { WalletExplorerLink(title: "View on a block explorer", url: url) }82 } footer: {83 if family == .utxo || trackError != nil, !isFinal {84 LoadingButton("Check the status", loadingTitle: family == .utxo ? "Synchronizing" : "Asking the network", systemImage: "arrow.triangle.2.circlepath",85 state: checking ? .loading : .idle) { Task { await check(synchronizing: true) } }86 .disabled(checking)87 }88 if isSettled, let onSendAnother {89 Button(status == .expired ? "Prepare a new payment" : "Send another payment", action: onSendAnother).buttonStyle(.qSecondary)90 }91 Button("Done", action: onDone).buttonStyle(.qPrimary)92 }93 .task(id: result.id) { await follow() }94 .qHaptic(.sent, trigger: result.id)95 .qHaptic(trigger: status) { new in96 switch new {97 case .failed?, .expired?: return .failed98 case .finalized?, .confirmed?: return .sent99 default: return nil100 }101 }102 .qAnimation(value: status)103 }104105 var isFinal: Bool {106 switch status {107 case .finalized?, .failed?, .expired?: return true108 default: return false109 }110 }111112 // MARK: Wording113114 var mark: ResultView<EmptyView>.Status {115 switch status {116 case .confirmed?, .finalized?: return .sent117 case .failed?, .expired?: return .failed118 case nil, .notFound?, .pending?: return .pending119 }120 }121122 var title: String {123 switch status {124 case nil, .notFound?: return "Broadcast"125 case .pending?: return "Waiting for a block"126 case .confirmed?: return "In a block"127 case .finalized?:128 switch family {129 case .solana: return "Finalized"130 case .tron: return "Irreversible"131 case .evm, .utxo: return "Final"132 }133 case .failed?: return "Failed on chain"134 case .expired?: return "Expired, not sent"135 }136 }137138 var message: String {139 let what = amountLine.map { "\($0) on \(chain?.displayName ?? "the network")" } ?? "The transaction"140 switch status {141 case nil:142 return "\(what) was handed to the network. Its identifier was computed on this device from the signed bytes."143 case .notFound?:144 return "\(what) was broadcast. The endpoint does not list it yet; that is common in the first seconds."145 case .pending?:146 return "\(what) is known to the network and waits for a block."147 case .confirmed(_, let confirmations, _)?:148 switch family {149 case .utxo: return "\(what) is in a block" + (confirmations.map { " (\($0) confirmation\($0 == 1 ? "" : "s"))" } ?? "") + ". Each further block makes it harder to undo."150 case .evm: return "\(what) is in a block" + (confirmations.map { ", \($0) deep" } ?? "") + ". It is not final yet."151 case .solana: return "\(what) is confirmed by the cluster and not finalized yet."152 case .tron: return "\(what) is in a block that is not irreversible yet."153 }154 case .finalized?:155 switch family {156 case .utxo: return "\(what) is buried deep enough that a reorganisation is not expected."157 case .evm: return "\(what) is deeper than the depth this wallet treats as final."158 case .solana: return "\(what) is finalized: it cannot be rolled back."159 case .tron: return "\(what) is in an irreversible block."160 }161 case .failed(_, let reason, _)?:162 return "The transaction was included and failed" + (reason.map { ": \($0)" } ?? ".") + " The fee was paid; nothing else moved."163 case .expired?:164 return "The transaction was never included and can no longer be. Nothing moved and no fee was paid. A new payment can be prepared."165 }166 }167168 var identifierLabel: String {169 switch family {170 case .solana: return "Signature"171 case .evm: return "Transaction hash"172 case .utxo, .tron: return "Transaction ID"173 }174 }175176 @ViewBuilder var statusSection: some View {177 ReviewSection("Status", footnote: footnote) {178 HStack(spacing: QSpace.m) {179 if !isFinal, trackError == nil, family != .utxo {180 ProgressView().controlSize(.small)181 } else {182 Image(systemName: isFinal ? "checkmark.circle" : "clock").foregroundStyle(QColor.textSecondary)183 }184 Text(statusLine).font(QFont.callout).foregroundStyle(QColor.textPrimary).fixedSize(horizontal: false, vertical: true)185 Spacer(minLength: 0)186 }187 .padding(.vertical, QSpace.m)188 .accessibilityElement(children: .combine)189 if let height = blockHeight { ReviewRow(chain?.heightNoun ?? "Block", value: height.formatted(.number.grouping(.never))) }190 if let fee = feePaid, let chain { ReviewRow("Fee paid", value: WalletChainFormat.native(fee, chain)) }191 if let last = result.lastValidBlockHeight, !isSettled { ReviewRow("Can be included until block height", value: last.formatted(.number.grouping(.never))) }192 if let expires = result.expiresAt, !isSettled { ReviewRow("Can be included until", value: expires.formatted(date: .omitted, time: .standard)) }193 }194 }195196 var statusLine: String {197 if let trackError { return "The status could not be read: \(trackError)" }198 switch status {199 case nil: return family == .utxo ? "Broadcast. Synchronize to read its confirmations." : "Asking the network where it stands"200 case .notFound?: return "Not listed by the endpoint yet"201 case .pending?: return "Seen, not in a block yet"202 case .confirmed?: return isFollowing ? "In a block; waiting for finality" : "In a block"203 case .finalized?: return "Settled"204 case .failed?: return "Included and failed"205 case .expired?: return "Expired"206 }207 }208209 var isFollowing: Bool { family != .utxo && !isFinal }210211 var footnote: String? {212 switch family {213 case .utxo: return "On \(chain?.nativeName ?? "this chain") the status is read from the wallet's own synchronization, not from a separate request."214 case .solana: return isSettled ? nil : "The same signed transaction is the only one that exists for this payment: it either lands before its blockhash expires or it never does."215 case .tron, .evm: return nil216 }217 }218219 var blockHeight: UInt64? {220 switch status {221 case .confirmed(let height, _, _)?: return height222 case .finalized(let height, _)?: return height223 case .failed(let height, _, _)?: return height224 default: return nil225 }226 }227228 var feePaid: UInt256? {229 switch status {230 case .confirmed(_, _, let fee)?: return fee231 case .finalized(_, let fee)?: return fee232 case .failed(_, _, let fee)?: return fee233 default: return nil234 }235 }236237 // MARK: Tracking238239 /// Account chains: asks the network every few seconds until the transaction is final, failed or expired, for240 /// about five minutes. UTXO chains answer from the wallet's snapshot: nothing is polled, the user synchronizes.241 func follow() async {242 guard family != .utxo else { return }243 for attempt in 0..<60 {244 if Task.isCancelled || isFinal { return }245 try? await Task.sleep(for: .seconds(attempt == 0 ? 2 : (isSettled ? 10 : 5)))246 if Task.isCancelled { return }247 await check(synchronizing: false)248 }249 }250251 func check(synchronizing: Bool) async {252 guard let service = wallet.service else { return }253 if synchronizing {254 guard !checking else { return }255 checking = true256 }257 defer { if synchronizing { checking = false } }258 if synchronizing, family == .utxo { await wallet.synchronize(chains: [result.chain]) }259 do {260 status = try await service.track(chain: result.chain, id: result.id)261 trackError = nil262 checks += 1263 if isFinal { await wallet.refresh() }264 } catch {265 // a failed status request says nothing about the transaction: the last known status stays on screen266 if synchronizing || status == nil { trackError = WalletStore.message(for: error) }267 }268 }269}270