Repository Public
1// SP Wallet Core2// File: Sources/SPWalletCore/Domains/Ops/DashboardModels.swift3// Description: Models of the read-only aggregates behind the Dashboard and Activity screens (services/api/src/dashboard/4// status.ts, summary.ts, activity.ts). The answers are made of SECTIONS that fail on their own: a section is the5// source's answer with its own `asOf`, or the reason it could not be read — a failed section is shown as6// unavailable, never as zero. Paper, testnet and real money are three separate books: nothing here adds a figure7// of one to a figure of another, and there is no total across them.8//9// Author: Simon-Pierre Boucher10// Contact: contact@spboucher.ai11import Foundation1213/** `{ ok: true, data, asOf }` or `{ ok: false, error: { code, message } }` (`Section<T>` of dashboard/summary.ts). */14public enum DashboardSection<T: Codable & Sendable & Hashable>: Codable, Sendable, Hashable {15 case ok(T, asOf: Date)16 /** `code`: `timeout`, `source_failed`, or the code of the source's own error. `message` is for logs, not for the screen. */17 case failed(code: String, message: String)1819 private enum Keys: String, CodingKey { case ok, data, asOf, error }20 private struct Failure: Codable { let code: String; let message: String }2122 public init(from decoder: Decoder) throws {23 let c = try decoder.container(keyedBy: Keys.self)24 if try c.decode(Bool.self, forKey: .ok) { self = .ok(try c.decode(T.self, forKey: .data), asOf: try c.decode(Date.self, forKey: .asOf)) }25 else { let e = try c.decode(Failure.self, forKey: .error); self = .failed(code: e.code, message: e.message) }26 }27 public func encode(to encoder: Encoder) throws {28 var c = encoder.container(keyedBy: Keys.self)29 switch self {30 case .ok(let d, let asOf): try c.encode(true, forKey: .ok); try c.encode(d, forKey: .data); try c.encode(asOf, forKey: .asOf)31 case .failed(let code, let message): try c.encode(false, forKey: .ok); try c.encode(Failure(code: code, message: message), forKey: .error)32 }33 }34 public var data: T? { if case .ok(let d, _) = self { d } else { nil } }35 public var asOf: Date? { if case .ok(_, let a) = self { a } else { nil } }36 public var errorCode: String? { if case .failed(let c, _) = self { c } else { nil } }37}3839// MARK: status (the risk indicator of every screen)4041public struct DashboardStatus: Codable, Sendable, Hashable {42 public struct Switch: Codable, Sendable, Hashable { public let on: Bool; public let at: Date? }43 public struct Switches: Codable, Sendable, Hashable { public let killSwitch: Switch; public let readOnly: Switch }44 public struct LiveLimits: Codable, Sendable, Hashable {45 /** Number of live limits the owner has not set yet. */46 public let missing: Int47 public let ownerOrdersRefused: Bool48 public let automationOrdersRefused: Bool49 }50 public let ts: Date51 public let switches: DashboardSection<Switches>52 public let liveLimits: DashboardSection<LiveLimits>53}5455// MARK: summary5657/** One book of the risk service (`exposure(book)`). Every figure is the server's canonical decimal string. */58public struct DashboardBook: Codable, Sendable, Hashable {59 public struct AssetExposure: Codable, Sendable, Hashable { public let asset: String; public let long: String; public let short: String; public let net: String }60 public struct VenueExposure: Codable, Sendable, Hashable { public let venue: String; public let gross: String }61 public struct Account: Codable, Sendable, Hashable {62 public let venue: String63 public let accountId: String64 public let equity: String65 public let collateral: String66 public let freeCollateral: String67 public let marginUsed: String68 public let shareBps: String?69 }70 public struct Position: Codable, Sendable, Hashable {71 public let venue: String72 public let accountId: String73 public let asset: String74 public let instrument: String75 /** `long` or `short`. */76 public let side: String77 public let size: String78 public let markPrice: String79 public let notional: String80 public let leverage: String81 public let liquidationPrice: String?82 public let liquidationDistanceBps: String?83 public let unrealizedPnl: String84 }85 public struct Pnl: Codable, Sendable, Hashable {86 public let realizedToday: String87 public let unrealizedToday: String88 public let realizedInception: String89 public let unrealizedInception: String90 }91 /** `paper`, `live-mainnet`, `live-testnet`. */92 public let book: String93 public let ts: Date94 public let equity: String95 public let grossExposure: String96 public let leverage: String?97 public let byAsset: [AssetExposure]98 public let byVenue: [VenueExposure]99 public let collateral: [Account]100 public let positions: [Position]101 /** Accounts or venues the risk service could not read: the book is then incomplete and says so. */102 public let errors: [String]103 public let pnl: DashboardBook.Pnl?104105 public var isEmpty: Bool { collateral.isEmpty && positions.isEmpty }106 public var isComplete: Bool { errors.isEmpty }107}108109public struct DashboardRisk: Codable, Sendable, Hashable {110 public struct Switch: Codable, Sendable, Hashable { public let on: Bool; public let at: Date?; public let reason: String? }111 public struct Switches: Codable, Sendable, Hashable { public let killSwitch: Switch; public let readOnly: Switch }112 public struct Live: Codable, Sendable, Hashable {113 public let missing: [String]114 public let missingForAutomation: [String]115 public let ownerOrdersRefused: Bool116 public let automationOrdersRefused: Bool117 }118 public let switches: Switches119 public let live: Live120}121122public struct DashboardAgents: Codable, Sendable, Hashable {123 public struct Counts: Codable, Sendable, Hashable { public let active: Int; public let paused: Int; public let stopped: Int }124 public let count: Int125 public let total: Counts126 /** `research`, `paper`, `live`. */127 public let byMode: [String: Counts]128}129130public struct DashboardAgentsPnl: Codable, Sendable, Hashable {131 public struct Mode: Codable, Sendable, Hashable {132 /** nil when there is no figure (`reason` says why) — never zero. */133 public let abs: Double?134 /** Agents of the mode whose figure is in `abs`. */135 public let agents: Int136 public let partial: Bool137 /** `research_no_pnl` or `no_snapshot` when `abs` is nil. */138 public let reason: String?139 }140 public let tz: String141 /** `flows_included`. */142 public let note: String143 /** One figure per mode (`research`, `paper`, `live`), never across modes. */144 public let today: [String: Mode]145}146147public struct DashboardHealth: Codable, Sendable, Hashable {148 public struct Failing: Codable, Sendable, Hashable, Identifiable { public let id: String; public let group: String; public let name: String; public let lastSuccessAt: Date? }149 public struct Connectors: Codable, Sendable, Hashable { public let total: Int; public let ok: Int; public let notMeasured: Int; public let failing: [Failing] }150 public struct Loops: Codable, Sendable, Hashable { public let total: Int; public let late: [String] }151 public let signer: SignerReachability152 public let connectors: Connectors153 public let loops: Loops154}155156public struct DashboardSummary: Codable, Sendable, Hashable {157 public struct Books: Codable, Sendable, Hashable {158 public let paper: DashboardSection<DashboardBook>159 /** Real money (venue accounts on mainnet). */160 public let live: DashboardSection<DashboardBook>161 /** Test funds. */162 public let liveTestnet: DashboardSection<DashboardBook>163 }164 public let ts: Date165 public let books: Books166 public let risk: DashboardSection<DashboardRisk>167 public let agents: DashboardSection<DashboardAgents>168 public let agentsPnl: DashboardSection<DashboardAgentsPnl>169 public let health: DashboardSection<DashboardHealth>170 public let audit: DashboardSection<AuditChainStatus>171}172173// MARK: activity timeline174175/** One item of the unified history (`ActivityItem` of dashboard/activity.ts). It carries CODES and parameters, never a sentence. */176public struct TimelineItem: Codable, Sendable, Hashable, Identifiable {177 public struct Principal: Codable, Sendable, Hashable {178 /** `owner`, `agent`, `api_key`, `system`. */179 public let kind: String180 public let id: String181 }182 public struct Quantity: Codable, Sendable, Hashable {183 /** The server's exact decimal text. */184 public let value: String185 public let symbol: String?186 }187 public struct Reference: Codable, Sendable, Hashable {188 /** `proposal`, `deposit`, `order`, `audit`, `decision`, `plan`. */189 public let type: String190 public let id: String191 }192 public let id: String193 /** `transfer`, `receive`, `swap`, `bridge`, `plan`, `order`, `agent`, `mandate`, `risk`, `key`, `security`. */194 public let kind: String195 /** `wallet`, `plans`, `orders`, `decisions`, `audit`. */196 public let source: String197 public let at: Date198 /** Which money the item is about: `live`, `paper`, `research`, or nil when the source does not say. Shown on every row. */199 public let mode: String?200 public let venue: String?201 public let network: String?202 public let principal: Principal?203 /** `wallet.send`, `order`, `plan`, `risk.denied`, or the audit event type. */204 public let code: String205 public let params: [String: JSONValue]206 public let status: String?207 public let instrument: String?208 public let side: String?209 public let amount: Quantity?210 public let received: Quantity?211 public let ref: Reference212 public let txid: String?213 public let explorer: String?214 /** Wallet items: the row exactly as GET /activity answers it. */215 public let wallet: JSONValue?216217 /** The wallet row as the base model, when this item has one and it decodes. */218 public var walletItem: ActivityItem? { wallet.flatMap { try? $0.decode(ActivityItem.self) } }219 public var isSimulated: Bool { mode == "paper" || mode == "research" }220}221222public struct TimelinePage: Codable, Sendable, Hashable {223 public struct SourceError: Codable, Sendable, Hashable { public let source: String; public let code: String; public let message: String }224 public struct Audit: Codable, Sendable, Hashable { public let intact: Bool; public let brokenAt: Int?; public let total: Int; public let checkedAt: Date? }225 public struct Bounds: Codable, Sendable, Hashable { public let perSource: Int; public let plans: Int }226 public let items: [TimelineItem]227 /** Opaque; nil on the last page. */228 public let nextCursor: String?229 /** Sources that could not be read: the timeline is then INCOMPLETE and the screen must say so. */230 public let errors: [SourceError]231 public let audit: Audit?232 /** The timeline reaches back `perSource` rows per source and no further. */233 public let bounds: Bounds?234 public let kinds: [String]?235 public let ts: Date?236237 public var isComplete: Bool { errors.isEmpty }238}239240public struct TimelineQuery: Sendable, Equatable {241 public var kind: String?242 /** `live`, `paper` or `research`. */243 public var mode: String?244 /** A venue id or a network id. */245 public var venue: String?246 /** `owner`, `agent`, `api_key` or `system`. */247 public var principal: String?248 public var from: Date?249 public var to: Date?250 public init(kind: String? = nil, mode: String? = nil, venue: String? = nil, principal: String? = nil, from: Date? = nil, to: Date? = nil) {251 self.kind = kind; self.mode = mode; self.venue = venue; self.principal = principal; self.from = from; self.to = to252 }253 var items: [String: String?] {254 ["kind": kind, "mode": mode, "venue": venue, "principal": principal, "from": from.map(ISO8601.string), "to": to.map(ISO8601.string)]255 }256}257