-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathAccountService.swift
More file actions
99 lines (80 loc) · 3.48 KB
/
AccountService.swift
File metadata and controls
99 lines (80 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// Created by tkhp
// Copyright © 2023 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
// MARK: - AccountService
class AccountService {
private let accountRepository: AccountRepository
private weak var authInterop: CBAuthInterop?
init(authInterop: CBAuthInterop) {
self.authInterop = authInterop
accountRepository = AccountRepository(authInterop: authInterop)
}
public func refreshAccount(_ accountName: String) async throws {
let account = try await account(by: accountName)
try await refresh(account: account)
}
public func refresh(account: CBAccount) async throws {
try await account.refreshAccount()
CBAccountManager.shared.store(account: account)
}
public func account(by name: String) async throws -> CBAccount {
try await accountRepository.account(by: name)
}
public func allAccounts() async throws -> [CBAccount] {
try await accountRepository.all()
}
/// Returns all crypto accounts regardless of balance.
/// Used by Maya to find accounts for currencies with zero balance.
public func allAccountsIncludingEmpty() async throws -> [CBAccount] {
try await accountRepository.allIncludingEmpty()
}
public func retrieveAddress(for accountName: String) async throws -> String {
let account = try await account(by: accountName)
return try await account.retrieveAddress()
}
public func send(from accountName: String, amount: UInt64, verificationCode: String?, idem: UUID?) async throws -> CoinbaseTransaction {
let account = try await account(by: accountName)
let tx = try await account.send(amount: amount, verificationCode: verificationCode, idem: idem)
return tx
}
public func placeBuyOrder(for accountName: String, amount: UInt64) async throws -> CoinbasePlaceBuyOrder {
let account = try await account(by: accountName)
return try await account.placeCoinbaseBuyOrder(amount: amount)
}
public func deposit(to accountName: String, from paymentMethodId: String, amount: UInt64) async throws {
let account = try await account(by: accountName)
let result = try await account.deposit(from: paymentMethodId, amount: amount)
if result.status != "created" {
throw Coinbase.Error.general(.depositFailed)
}
}
func placeTradeOrder(from origin: CBAccount, to destination: CBAccount, amount: String) async throws -> CoinbaseSwapeTrade {
try await origin.convert(amount: amount, to: destination)
}
public func commitTradeOrder(origin: CBAccount, orderID: String) async throws -> CoinbaseSwapeTrade {
let order = try await origin.commitTradeOrder(orderID: orderID)
return order
}
func removeStoredAccount() {
CBAccountManager.shared.removeAccount()
}
}
extension AccountService {
var dashAccount: CBAccount? {
accountRepository.dashAccount
}
}