-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ece615e
commit a7a915d
Showing
7 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...mmunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/Wallet/Models/Wallet.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// Wallet.swift | ||
// SOLARdVPNCommunityCoreiOS | ||
// | ||
// Created by Viktoriia Kostyleva on 04.10.2022. | ||
// | ||
|
||
import Foundation | ||
|
||
struct Wallet: Codable { | ||
let address: String | ||
let balance: Int | ||
let currency: String | ||
|
||
init(address: String, balance: Int, currency: String) { | ||
self.address = address | ||
self.balance = balance | ||
self.currency = currency | ||
} | ||
} | ||
|
||
// MARK: - Codable implementation | ||
|
||
extension Wallet { | ||
enum CodingKeys: String, CodingKey { | ||
case address | ||
case balance | ||
case currency | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...oreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/Wallet/WalletRouteCollection.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// WalletRouteCollection.swift | ||
// SOLARdVPNCommunityCoreiOS | ||
// | ||
// Created by Viktoriia Kostyleva on 04.10.2022. | ||
// | ||
|
||
import Foundation | ||
import Vapor | ||
|
||
struct WalletRouteCollection: RouteCollection { | ||
let context: HasSecurityService & HasWalletStorage & HasWalletService | ||
|
||
func boot(routes: RoutesBuilder) throws { | ||
routes.get("wallet", use: getWallet) | ||
} | ||
} | ||
|
||
extension WalletRouteCollection { | ||
func getWallet(_ req: Request) async throws -> String { | ||
try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<String, Error>) in | ||
fetchBalance() { result in | ||
switch result { | ||
case let .failure(error): | ||
continuation.resume(throwing: Abort(.init(statusCode: 500), reason: error.localizedDescription)) | ||
|
||
case let .success(balance): | ||
let address = context.walletStorage.walletAddress | ||
let wallet = Wallet(address: address, balance: balance, currency: ClientConstants.denom) | ||
|
||
Encoder.encode(model: wallet, continuation: continuation) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// MARK: - Private | ||
|
||
extension WalletRouteCollection { | ||
private func fetchBalance( | ||
completion: @escaping (Result<Int, Error>) -> Void | ||
) { | ||
context.walletService.fetchBalance { result in | ||
switch result { | ||
case let .failure(error): | ||
completion(.failure(error)) | ||
|
||
case let .success(balances): | ||
guard let balance = balances.first(where: { $0.denom == ClientConstants.denom }) else { | ||
completion(.success(0)) | ||
return | ||
} | ||
|
||
guard let amount = Int(balance.amount) else { | ||
// TODO: Call completion with error | ||
|
||
return | ||
} | ||
|
||
completion(.success(amount)) | ||
} | ||
} | ||
} | ||
} |