Model+Balances.swift (2784B)
1 /* 2 * This file is part of GNU Taler, ©2022-25 Taler Systems S.A. 3 * See LICENSE.md 4 */ 5 /** 6 * @author Marc Stibane 7 */ 8 import Foundation 9 import taler_swift 10 11 // MARK: - 12 13 enum BalanceFlag: String, Codable { 14 case incomingAml = "incoming-aml" 15 case incomingConfirmation = "incoming-confirmation" 16 case incomingKyc = "incoming-kyc" 17 case outgoingKyc = "outgoing-kyc" 18 } 19 20 /// A currency balance 21 struct Balance: Identifiable, Decodable, Hashable, Sendable { 22 var id: String { 23 if let url = scopeInfo.url { 24 return url 25 } 26 return scopeInfo.currency 27 } 28 29 var scopeInfo: ScopeInfo 30 var available: Amount 31 var pendingIncoming: Amount 32 var pendingOutgoing: Amount 33 var flags: [BalanceFlag] 34 var shoppingUrls: [String]? 35 var disablePeerPayments: Bool? 36 var disableDirectDeposits: Bool? // TODO: en/disable actions based on this 37 38 public static func == (lhs: Balance, rhs: Balance) -> Bool { 39 lhs.scopeInfo == rhs.scopeInfo 40 && lhs.available == rhs.available 41 && lhs.pendingIncoming == rhs.pendingIncoming 42 && lhs.pendingOutgoing == rhs.pendingOutgoing 43 && lhs.flags == rhs.flags 44 && lhs.disablePeerPayments == rhs.disablePeerPayments 45 && lhs.disableDirectDeposits == rhs.disableDirectDeposits 46 } 47 48 public func hash(into hasher: inout Hasher) { 49 hasher.combine(scopeInfo) 50 hasher.combine(available) 51 hasher.combine(pendingIncoming) 52 hasher.combine(pendingOutgoing) 53 hasher.combine(flags) 54 hasher.combine(disablePeerPayments) 55 hasher.combine(disableDirectDeposits) 56 } 57 } 58 extension Balance { 59 static func firstwithDeposit(_ balances: [Balance]) -> Balance? { 60 balances.first { balance in 61 balance.disableDirectDeposits != true 62 } 63 } 64 static func firstWithP2P(_ balances: [Balance]) -> Balance? { 65 balances.first { balance in 66 balance.disablePeerPayments != true 67 } 68 } 69 } 70 // MARK: - 71 /// A request to get the balances held in the wallet. 72 fileprivate struct Balances: WalletBackendFormattedRequest { 73 func operation() -> String { "getBalances" } 74 func args() -> Args { Args() } 75 76 struct Args: Encodable {} // no arguments needed 77 78 struct Response: Decodable, Sendable { // list of balances 79 var balances: [Balance] 80 } 81 } 82 // MARK: - 83 extension WalletModel { 84 /// fetch Balances from Wallet-Core. No networking involved 85 nonisolated func getBalances(_ stack: CallStack, viewHandles: Bool = false) async throws -> [Balance] { 86 let request = Balances() 87 let response = try await sendRequest(request, viewHandles: viewHandles) 88 return response.balances 89 } 90 }