Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Features/Futures/FuturesView.swift3// Description: Entry of the derivatives terminal. A permanent header shows the trading mode (simulation, testnet, real — colour and label, never mixed), the state of the live quote stream and the server's switches; the tabs below are the markets list, the instrument page with its order ticket, the account, the venue setup (authorization, leverage, collateral), the risk engine, GMX v2 and the read-only market explorer. Viewing any of them triggers nothing: every operation starts from an explicit button.4import SwiftUI5import SPWalletCore67struct FuturesView: View {8 let model: AppModel9 @State private var store: FuturesStore?10 @State private var ops: FutLiveOps?11 @State private var ticket: FutTicketFlow?12 @State private var tab: Tab = .markets1314 enum Tab: String, CaseIterable, Identifiable {15 case markets, trade, account, venue, risk, gmx, explorer16 var id: String { rawValue }17 var title: String {18 switch self {19 case .markets: "Marchés"; case .trade: "Instrument et ordre"; case .account: "Compte"; case .venue: "Plateforme et collatéral"20 case .risk: "Risque"; case .gmx: "GMX v2"; case .explorer: "Explorateur"21 }22 }23 }2425 var body: some View {26 Group {27 if let store, let ops, let ticket {28 VStack(spacing: 0) {29 FutHeader(model: model, store: store, tab: $tab)30 Divider()31 content(store: store, ops: ops, ticket: ticket)32 }33 .background(Color(nsColor: .windowBackgroundColor))34 .sheet(item: Binding(get: { ops.sheet }, set: { if $0 == nil, ops.sheet != nil { Task { await ops.dismiss() } } })) { s in35 FutApprovalSheet(model: model, store: store, ops: ops, sheet: s)36 }37 .futPolling(every: 60, id: store.venue) { await store.refreshTickers() }38 .futPolling(every: 20, id: "\(store.mode.rawValue)|\(store.paperId?.rawValue ?? "")") { await store.refreshAccount() }39 .futPolling(every: 90, id: "locks") { await store.loadLocks() }40 .onAppear { store.startStream() }41 .onDisappear { store.stopStream() }42 } else {43 ProgressView("Chargement des marchés dérivés…").frame(maxWidth: .infinity, maxHeight: .infinity)44 }45 }46 .task {47 guard store == nil else { return }48 let s = FuturesStore(model: model)49 let o = FutLiveOps(model: model, store: s)50 store = s; ops = o; ticket = FutTicketFlow(model: model, store: s, ops: o)51 await s.loadAll()52 }53 }5455 @ViewBuilder private func content(store: FuturesStore, ops: FutLiveOps, ticket: FutTicketFlow) -> some View {56 switch tab {57 case .markets:58 FutMarketsView(model: model, store: store) { id in store.instrumentId = id; tab = .trade }59 case .trade:60 FutScroll { FutInstrumentView(model: model, store: store, ops: ops, ticket: ticket) { tab = $0 } }61 case .account:62 FutScroll { FutAccountView(model: model, store: store, ops: ops) { id in store.instrumentId = id; tab = .trade } }63 case .venue:64 FutScroll { FutVenueView(model: model, store: store, ops: ops) }65 case .risk:66 FutScroll { FutRiskView(model: model, store: store) }67 case .gmx:68 FutScroll { FutGMXView(model: model, store: store) }69 case .explorer:70 FutScroll { FutExplorerView(model: model, store: store) }71 }72 }73}7475/// The standard scrolling body of a tab (the markets tab is a full-height table instead).76struct FutScroll<Content: View>: View {77 @ViewBuilder var content: Content78 var body: some View {79 ScrollView {80 VStack(alignment: .leading, spacing: 16) { content }81 .padding(20).frame(maxWidth: 1320, alignment: .leading).frame(maxWidth: .infinity, alignment: .leading)82 }83 }84}8586private struct FutHeader: View {87 let model: AppModel88 @Bindable var store: FuturesStore89 @Binding var tab: FuturesView.Tab9091 var body: some View {92 VStack(alignment: .leading, spacing: 10) {93 HStack(alignment: .firstTextBaseline, spacing: 14) {94 Text("Dérivés").font(.largeTitle.weight(.semibold))95 FutModeBadge(mode: store.mode)96 Spacer()97 HStack(spacing: 6) {98 Circle().fill(store.streamState == .live ? Theme.positive : Theme.warning).frame(width: 8, height: 8)99 Text(store.streamText).font(.caption).foregroundStyle(.secondary).lineLimit(1)100 }.accessibilityElement(children: .combine)101 }102 HStack(spacing: 14) {103 Picker("Mode", selection: $store.mode) {104 ForEach(FutMode.allCases) { Text($0.title).tag($0) }105 }.pickerStyle(.segmented).labelsHidden().frame(maxWidth: 520)106 Text(store.mode.explanation).font(.caption).foregroundStyle(.secondary).lineLimit(2).fixedSize(horizontal: false, vertical: true)107 }108 Picker("Section", selection: $tab) {109 ForEach(FuturesView.Tab.allCases) { Text($0.title).tag($0) }110 }.pickerStyle(.segmented).labelsHidden()111 if !model.canOperate { Banner(.warning, "Session ou portefeuille indisponible : consultation seulement, toutes les opérations sont désactivées.") }112 if let k = store.killSwitch, k.on { Banner(.danger, "Coupe-circuit global engagé\(k.reason.map { " — \($0)" } ?? "") : aucun nouvel ordre n’ajoute de risque. Annulations et ordres strictement réducteurs restent possibles.") }113 if let r = store.readOnlySwitch, r.on { Banner(.warning, "Mode lecture seule engagé\(r.reason.map { " — \($0)" } ?? "") : seules les annulations passent.") }114 if model.portfolio?.value.halted == true { Banner(.danger, "Arrêt d’urgence du signataire actif : plus rien ne peut être signé. Il se lève sur la machine du signataire uniquement.") }115 if let e = store.streamError { Banner(.warning, e) }116 }117 .padding(.horizontal, 20).padding(.top, 16).padding(.bottom, 12)118 .overlay(alignment: .top) { Rectangle().fill(store.mode.color).frame(height: 4).accessibilityHidden(true) }119 }120}121122/// The server locks that matter to live derivatives, with the server's reasons. Read from the capability list, the risk engine and the live configuration.123struct FutLocksCard: View {124 let store: FuturesStore125 static let capabilityIds = ["deriv_live_mainnet", "deriv_gmx_live", "trading_api_execution", "deriv_mandate_mainnet"]126127 var body: some View {128 Card("Verrous du serveur") {129 if let caps = store.capabilities.value {130 ForEach(Self.capabilityIds, id: \.self) { id in131 if let c = caps.first(where: { $0.id == id }) {132 FutLockRow(name: c.id, on: c.on, reason: c.why + (c.on || c.missing.isEmpty ? "" : " — réglage(s) du serveur : \(c.missing.joined(separator: ", "))"))133 } else if let l = store.executionLock(id) {134 FutLockRow(name: l.id, on: l.on, reason: l.why)135 } else {136 FutLockRow(name: id, on: nil, reason: "Le serveur ne publie pas ce verrou.")137 }138 }139 } else {140 Banner(.warning, "Capacités du serveur illisibles\(store.capabilities.error.map { " : \($0)" } ?? "") — les contrôles réels restent fermés tant que le serveur ne répond pas.")141 ForEach(store.riskState.value?.locks ?? []) { l in FutLockRow(name: l.id, on: l.on, reason: l.why) }142 }143 if let c = store.config.value, let m = c.mainnet {144 Divider()145 FutLockRow(name: "Hyperliquid mainnet (configuration des comptes réels)", on: m.enabled, reason: m.reason ?? (m.enabled ? nil : m.code))146 if let mm = c.mandateLive.mainnet { FutLockRow(name: "Mandats autonomes sur mainnet", on: c.mandateLive.available && mm.enabled, reason: mm.reason ?? c.mandateLive.reason) }147 } else if let e = store.config.error {148 Text("Configuration des comptes réels illisible : \(e)").font(.callout).foregroundStyle(Theme.negative)149 }150 Text("Ces verrous sont des réglages du serveur : l’app les lit et ne peut pas les modifier. Annulations et clôtures réductrices ne passent jamais par eux.").font(.caption).foregroundStyle(.secondary)151 }152 }153}154155/// The jurisdiction notice of the venue, as the server publishes it, with the owner's own acknowledgement for this session.156struct FutJurisdictionCard: View {157 @Bindable var store: FuturesStore158 var body: some View {159 Card("Avis de juridiction — Hyperliquid", trailing: AnyView(FutModeBadge(mode: store.mode))) {160 if let j = store.jurisdiction.value {161 let ack = store.locale == "fr" ? j.fr : j.en162 Text(ack.text).font(.callout).textSelection(.enabled).fixedSize(horizontal: false, vertical: true)163 HStack(spacing: 10) {164 if let v = ack.version { Text("Version \(v)").font(.caption).foregroundStyle(.secondary) }165 if let s = ack.source { Text("Source : \(s)").font(.caption).foregroundStyle(.secondary).lineLimit(1) }166 if !ack.textMatchesHash { ProvenanceTag(p: .unavailable, text: "Empreinte du texte incohérente") }167 }168 Toggle("J’ai lu cet avis. Information générale, pas un avis juridique.", isOn: $store.jurisdictionRead).toggleStyle(.checkbox)169 if !store.jurisdictionRead { Text("Les opérations réelles (ordre, levier, dépôt, retrait, autorisation) restent désactivées tant que cette case n’est pas cochée. Annuler, clôturer et arrêter restent toujours possibles.").font(.caption).foregroundStyle(.secondary) }170 } else {171 Banner(.danger, "Avis de juridiction indisponible\(store.jurisdiction.error.map { " : \($0)" } ?? "") — aucune opération réelle n’est proposée sans lui.")172 }173 }174 }175}176