Skip to content

Commit

Permalink
#10: Add GET wallet request
Browse files Browse the repository at this point in the history
  • Loading branch information
VictoriaKostyleva committed Oct 5, 2022
1 parent ece615e commit a7a915d
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
923C372E28EB0BEE003CFC03 /* continents.json in Resources */ = {isa = PBXBuildFile; fileRef = 923C372D28EB0BED003CFC03 /* continents.json */; };
923C373128EB0CB2003CFC03 /* CountryExtra.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923C373028EB0CB2003CFC03 /* CountryExtra.swift */; };
923C373328EB150A003CFC03 /* GetContinentResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923C373228EB150A003CFC03 /* GetContinentResponse.swift */; };
923C373728EC791E003CFC03 /* WalletRouteCollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923C373628EC791E003CFC03 /* WalletRouteCollection.swift */; };
923C373A28EC79E8003CFC03 /* Wallet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923C373928EC79E8003CFC03 /* Wallet.swift */; };
92D6B3FD28E19E20004CF9DF /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92D6B3FC28E19E20004CF9DF /* AppDelegate.swift */; };
92D6B3FF28E19E20004CF9DF /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92D6B3FE28E19E20004CF9DF /* SceneDelegate.swift */; };
92D6B40128E19E20004CF9DF /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92D6B40028E19E20004CF9DF /* ViewController.swift */; };
Expand Down Expand Up @@ -245,6 +247,8 @@
923C372D28EB0BED003CFC03 /* continents.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = continents.json; sourceTree = "<group>"; };
923C373028EB0CB2003CFC03 /* CountryExtra.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CountryExtra.swift; sourceTree = "<group>"; };
923C373228EB150A003CFC03 /* GetContinentResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetContinentResponse.swift; sourceTree = "<group>"; };
923C373628EC791E003CFC03 /* WalletRouteCollection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletRouteCollection.swift; sourceTree = "<group>"; };
923C373928EC79E8003CFC03 /* Wallet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Wallet.swift; sourceTree = "<group>"; };
92D6B3F928E19E20004CF9DF /* SOLARdVPNCommunityCoreiOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SOLARdVPNCommunityCoreiOS.app; sourceTree = BUILT_PRODUCTS_DIR; };
92D6B3FC28E19E20004CF9DF /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
92D6B3FE28E19E20004CF9DF /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -587,6 +591,7 @@
923C372328E72FDA003CFC03 /* RouteCollections */ = {
isa = PBXGroup;
children = (
923C373528EC7904003CFC03 /* Wallet */,
923C372428E72FF0003CFC03 /* Nodes */,
225A83A428EC297500F66619 /* Tunnel */,
);
Expand Down Expand Up @@ -620,6 +625,23 @@
path = Models;
sourceTree = "<group>";
};
923C373528EC7904003CFC03 /* Wallet */ = {
isa = PBXGroup;
children = (
923C373828EC79D9003CFC03 /* Models */,
923C373628EC791E003CFC03 /* WalletRouteCollection.swift */,
);
path = Wallet;
sourceTree = "<group>";
};
923C373828EC79D9003CFC03 /* Models */ = {
isa = PBXGroup;
children = (
923C373928EC79E8003CFC03 /* Wallet.swift */,
);
path = Models;
sourceTree = "<group>";
};
92D6B3F028E19E20004CF9DF = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -990,6 +1012,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
923C373A28EC79E8003CFC03 /* Wallet.swift in Sources */,
92D6B40128E19E20004CF9DF /* ViewController.swift in Sources */,
22C3EEED28E48D9A007DB01B /* SentinelNode+Ext.swift in Sources */,
923C372628EAE93E003CFC03 /* Encoder.swift in Sources */,
Expand Down Expand Up @@ -1030,6 +1053,7 @@
22C3EED428E48B52007DB01B /* TunnelsService.swift in Sources */,
225A839D28EAE54400F66619 /* SecurityService.swift in Sources */,
22C3EEE628E48D9A007DB01B /* TunnelConfiguration+Ext.swift in Sources */,
923C373728EC791E003CFC03 /* WalletRouteCollection.swift in Sources */,
22C3EEE828E48D9A007DB01B /* Serializer.swift in Sources */,
22C3EEE728E48D9A007DB01B /* NEVPNStatus+Ext.swift in Sources */,
92D6B41428E1E133004CF9DF /* DVPNServer.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ extension CommonContext: HasTunnelManager {}
protocol HasSessionsService { var sessionsService: SessionsServiceType { get } }
extension CommonContext: HasSessionsService {}

protocol HasSecurityService { var securityService: SecurityService { get } }
extension CommonContext: HasSecurityService {}

// MARK: - Storages

protocol HasConnectionInfoStorage { var connectionInfoStorage: StoresConnectInfo { get } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ enum ClientConstants {
static let apiPath = "api"

static let backendURL = URL(string: "https://BACKEND")!

static let denom = "udvpn"
}

final class ApplicationConfiguration: ClientConnectionConfigurationType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extension DVPNServer {
do {
let api = app.grouped(.init(stringLiteral: ClientConstants.apiPath))
try api.register(collection: NodesRouteCollection(context: context))
try api.register(collection: WalletRouteCollection(context: context))
try api.register(collection:
TunnelRouteCollection(
model: ConnectionModel(context: context),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import GRPC

private struct Constants {
let timeout: TimeInterval = 15
let denom = "udvpn"
}

private let constants = Constants()
Expand Down Expand Up @@ -279,7 +278,7 @@ extension ConnectionNodeModel {
case .success(let balances):
guard balances
.contains(
where: { $0.denom == constants.denom && Int($0.amount) ?? 0 >= self.context.walletService.fee }
where: { $0.denom == ClientConstants.denom && Int($0.amount) ?? 0 >= self.context.walletService.fee }
) else {
self.delegate?.show(warning: WalletServiceError.notEnoughTokens)
self.delegate?.set(isLoading: false)
Expand Down
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
}
}
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))
}
}
}
}

0 comments on commit a7a915d

Please sign in to comment.