Skip to content

Commit

Permalink
Add TrustAPI provider
Browse files Browse the repository at this point in the history
  • Loading branch information
vikmeup committed Jun 4, 2018
1 parent 3d1fa97 commit 43e7fe5
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 25 deletions.
4 changes: 4 additions & 0 deletions Trust.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@
77872D322027AA4A0032D687 /* SliderTextFieldRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77872D312027AA4A0032D687 /* SliderTextFieldRow.swift */; };
778EA5FD2050A5ED00E81F7F /* WalletTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 778EA5FC2050A5ED00E81F7F /* WalletTests.swift */; };
778EAF7D1FF10AF400C8E2AB /* SettingsCoordinatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 778EAF7C1FF10AF400C8E2AB /* SettingsCoordinatorTests.swift */; };
77A6D8A120C4DE06000A9ADC /* TrustAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77A6D8A020C4DE06000A9ADC /* TrustAPI.swift */; };
77B1EAFF20639DEB007970AA /* NFTokenViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77B1EAFE20639DEB007970AA /* NFTokenViewController.swift */; };
77B1EB012063A198007970AA /* NFTDetailsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77B1EB002063A198007970AA /* NFTDetailsViewModel.swift */; };
77B3BF3C201908ED00EEC15A /* ConfirmCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77B3BF3B201908ED00EEC15A /* ConfirmCoordinator.swift */; };
Expand Down Expand Up @@ -872,6 +873,7 @@
778EA5FC2050A5ED00E81F7F /* WalletTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletTests.swift; sourceTree = "<group>"; };
778EAF7C1FF10AF400C8E2AB /* SettingsCoordinatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsCoordinatorTests.swift; sourceTree = "<group>"; };
779891EA207C10C7008890B8 /* ja */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ja; path = ja.lproj/Localizable.strings; sourceTree = "<group>"; };
77A6D8A020C4DE06000A9ADC /* TrustAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrustAPI.swift; sourceTree = "<group>"; };
77B1EAFE20639DEB007970AA /* NFTokenViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NFTokenViewController.swift; sourceTree = "<group>"; };
77B1EB002063A198007970AA /* NFTDetailsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NFTDetailsViewModel.swift; sourceTree = "<group>"; };
77B3BF3B201908ED00EEC15A /* ConfirmCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfirmCoordinator.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1299,6 +1301,7 @@
children = (
29ED26EB1FB0069B00F7F8F9 /* Models */,
2959961B1FAE3EDF00DB66A8 /* TrustClient.swift */,
77A6D8A020C4DE06000A9ADC /* TrustAPI.swift */,
);
path = TrustClient;
sourceTree = "<group>";
Expand Down Expand Up @@ -3204,6 +3207,7 @@
777871312082DF3D00CF7303 /* GetTransactionReceiptRequest.swift in Sources */,
77B64101209668FF00BEF3EB /* EtherNumberFormatter+Trust.swift in Sources */,
776C114C20605AAC00449539 /* TokensLayout.swift in Sources */,
77A6D8A120C4DE06000A9ADC /* TrustAPI.swift in Sources */,
73ED85A520349BE400593BF3 /* StringFormatter.swift in Sources */,
BB8E747920818E5E001BA99A /* SearchTokenResultsController.swift in Sources */,
293112101FC4ADCB00966EEA /* InCoordinatorViewModel.swift in Sources */,
Expand Down
15 changes: 11 additions & 4 deletions Trust/Core/Network/TrustNetwork.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,23 @@ protocol NetworkProtocol: TrustNetworkProtocol {
}

class TrustNetwork: NetworkProtocol {

static let deleteMissingInternalSeconds: Double = 60.0
static let deleyedTransactionInternalSeconds: Double = 60.0
let provider: MoyaProvider<TrustService>
let APIProvider: MoyaProvider<TrustAPI>
let config: Config
let balanceService: TokensBalanceService
let account: Wallet

required init(provider: MoyaProvider<TrustService>, balanceService: TokensBalanceService, account: Wallet, config: Config) {
required init(
provider: MoyaProvider<TrustService>,
APIProvider: MoyaProvider<TrustAPI>,
balanceService: TokensBalanceService,
account: Wallet,
config: Config
) {
self.provider = provider
self.APIProvider = APIProvider
self.balanceService = balanceService
self.account = account
self.config = config
Expand All @@ -46,7 +53,7 @@ class TrustNetwork: NetworkProtocol {
currency: config.currency.rawValue,
tokens: tokenPrices
)
provider.request(.prices(tokensPriceToFetch)) { result in
APIProvider.request(.prices(tokensPriceToFetch)) { result in
guard case .success(let response) = result else {
completion(nil)
return
Expand Down Expand Up @@ -133,7 +140,7 @@ class TrustNetwork: NetworkProtocol {
currency: config.currency.rawValue,
tokens: tokenPrices
)
provider.request(.prices(tokensPriceToFetch)) { result in
APIProvider.request(.prices(tokensPriceToFetch)) { result in
guard case .success(let response) = result else {
seal.reject(TrustNetworkProtocolError.missingPrices)
return
Expand Down
55 changes: 55 additions & 0 deletions Trust/EtherClient/TrustClient/TrustAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright SIX DAY LLC. All rights reserved.

import Foundation
import Moya

enum TrustAPI {
case prices(TokensPrice)
}

struct TokensPrice: Encodable {
let currency: String
let tokens: [TokenPrice]
}

struct TokenPrice: Encodable {
let contract: String
let symbol: String
}

extension TrustAPI: TargetType {

var baseURL: URL { return Constants.trustAPI }

var path: String {
switch self {
case .prices:
return "/tokenPrices"
}
}

var method: Moya.Method {
switch self {
case .prices: return .post
}
}

var task: Task {
switch self {
case .prices(let tokensPrice):
return .requestJSONEncodable(tokensPrice)
}
}

var sampleData: Data {
return Data()
}

var headers: [String: String]? {
return [
"Content-type": "application/json",
"client": Bundle.main.bundleIdentifier ?? "",
"client-build": Bundle.main.buildNumber ?? "",
]
}
}
16 changes: 0 additions & 16 deletions Trust/EtherClient/TrustClient/TrustClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Foundation
import Moya

enum TrustService {
case prices(TokensPrice)
case getTransactions(address: String, startBlock: Int, page: Int, contract: String?)
case getTokens(address: String, showBalance: Bool)
case getTransaction(ID: String)
Expand All @@ -15,16 +14,6 @@ enum TrustService {
case search(token: String)
}

struct TokensPrice: Encodable {
let currency: String
let tokens: [TokenPrice]
}

struct TokenPrice: Encodable {
let contract: String
let symbol: String
}

extension TrustService: TargetType {

var baseURL: URL { return Config().server.remoteURL }
Expand All @@ -41,8 +30,6 @@ extension TrustService: TargetType {
return "/push/register"
case .unregister:
return "/push/unregister"
case .prices:
return "/tokenPrices"
case .marketplace:
return "/marketplace"
case .assets:
Expand All @@ -59,7 +46,6 @@ extension TrustService: TargetType {
case .getTransaction: return .get
case .register: return .post
case .unregister: return .delete
case .prices: return .post
case .marketplace: return .get
case .assets: return .get
case .search: return .get
Expand All @@ -85,8 +71,6 @@ extension TrustService: TargetType {
return .requestJSONEncodable(device)
case .unregister(let device):
return .requestJSONEncodable(device)
case .prices(let tokensPrice):
return .requestJSONEncodable(tokensPrice)
case .marketplace(let chainID):
return .requestParameters(parameters: ["chainID": chainID], encoding: URLEncoding())
case .assets(let address):
Expand Down
3 changes: 2 additions & 1 deletion Trust/Foundation/TrustNetworkProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import Moya

protocol TrustNetworkProtocol {
var provider: MoyaProvider<TrustService> { get }
var APIProvider: MoyaProvider<TrustAPI> { get }
var balanceService: TokensBalanceService { get }
var account: Wallet { get }
var config: Config { get }
init(provider: MoyaProvider<TrustService>, balanceService: TokensBalanceService, account: Wallet, config: Config)
init(provider: MoyaProvider<TrustService>, APIProvider: MoyaProvider<TrustAPI>, balanceService: TokensBalanceService, account: Wallet, config: Config)
}
8 changes: 7 additions & 1 deletion Trust/InCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ class InCoordinator: Coordinator {
let tokensStorage = TokensDataStore(realm: realm, config: config)
let balanceCoordinator = TokensBalanceService()
let viewModel = InCoordinatorViewModel(config: config)
let trustNetwork = TrustNetwork(provider: TrustProviderFactory.makeProvider(), balanceService: balanceCoordinator, account: account, config: config)
let trustNetwork = TrustNetwork(
provider: TrustProviderFactory.makeProvider(),
APIProvider: TrustProviderFactory.makeAPIProvider(),
balanceService: balanceCoordinator,
account: account,
config: config
)
let balance = BalanceCoordinator(account: account, config: config, storage: tokensStorage)
let transactionsStorage = TransactionsStorage(
realm: realm,
Expand Down
2 changes: 2 additions & 0 deletions Trust/Settings/Types/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public struct Constants {
public static let dappsOpenSea = "https://opensea.io"

public static let images = "https://trustwalletapp.com/images"

public static let trustAPI = URL(string: "https://public.trustwalletapp.com")!
}

public struct UnitConfiguration {
Expand Down
8 changes: 8 additions & 0 deletions Trust/Transactions/Coordinators/TrustProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ struct TrustProviderFactory {
)
return MoyaProvider<TrustService>(manager: manager)
}

static func makeAPIProvider() -> MoyaProvider<TrustAPI> {
let manager = Manager(
configuration: URLSessionConfiguration.default,
serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies)
)
return MoyaProvider<TrustAPI>(manager: manager)
}
}
8 changes: 7 additions & 1 deletion TrustTests/Coordinators/TokensCoordinatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ class TokensCoordinatorTests: XCTestCase {
session: .make(),
keystore: FakeKeystore(),
tokensStorage: FakeTokensDataStore(),
network: FakeTokensNetwork(provider: TrustProviderFactory.makeProvider(), balanceService: FakeGetBalanceCoordinator(), account: .make(), config: .make()), transactionsStore: FakeTransactionsStorage()
network: FakeTokensNetwork(
provider: TrustProviderFactory.makeProvider(),
APIProvider: TrustProviderFactory.makeAPIProvider(),
balanceService: FakeGetBalanceCoordinator(),
account: .make(),
config: .make()
), transactionsStore: FakeTransactionsStorage()
)

coordinator.start()
Expand Down
4 changes: 3 additions & 1 deletion TrustTests/Factories/FakeTokensNetwork.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ enum FakeTokensNetworkError: Error {
class FakeTokensNetwork: NetworkProtocol {

var provider: MoyaProvider<TrustService>
var APIProvider: MoyaProvider<TrustAPI>
var balanceService: TokensBalanceService
var account: Trust.Wallet
var config: Config

required init(provider: MoyaProvider<TrustService>, balanceService: TokensBalanceService, account: Trust.Wallet, config: Config) {
required init(provider: MoyaProvider<TrustService>, APIProvider: MoyaProvider<TrustAPI>, balanceService: TokensBalanceService, account: Trust.Wallet, config: Config) {
self.provider = provider
self.APIProvider = APIProvider
self.balanceService = balanceService
self.account = account
self.config = config
Expand Down
2 changes: 1 addition & 1 deletion TrustTests/Tokens/TokensViewModelTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import XCTest
import BigInt

class TokensViewModelTest: XCTestCase {
let model = TokensViewModel(address: .make(), store: FakeTokensDataStore(), tokensNetwork: FakeTokensNetwork(provider: TrustProviderFactory.makeProvider(), balanceService: FakeGetBalanceCoordinator(), account: .make(), config: .make()))
let model = TokensViewModel(address: .make(), store: FakeTokensDataStore(), tokensNetwork: FakeTokensNetwork(provider: TrustProviderFactory.makeProvider(), APIProvider: TrustProviderFactory.makeAPIProvider(), balanceService: FakeGetBalanceCoordinator(), account: .make(), config: .make()))
let firstItem = IndexPath(row: 0, section: 0)

func testNumberOfTokens() {
Expand Down

0 comments on commit 43e7fe5

Please sign in to comment.