Optimization of the requests and models.

pull/2/head
Oleg Gordiichuk 7 years ago committed by Michael Scoff
parent 5755803c77
commit a9a253424a
  1. 4
      Trust.xcodeproj/project.pbxproj
  2. 2
      Trust/Exchange/Coordinators/ExchangeTokensCoordinator.swift
  3. 18
      Trust/InCoordinator.swift
  4. 57
      Trust/Settings/Coordinators/ExchangeRateCoordinator.swift
  5. 24
      Trust/Tokens/Coordinators/GetBalanceCoordinator.swift
  6. 66
      Trust/Transactions/Coordinators/BalanceCoordinator.swift
  7. 16
      Trust/Transactions/Storage/Session.swift

@ -75,7 +75,6 @@
293112331FC971F600966EEA /* ExchangeToken.swift in Sources */ = {isa = PBXBuildFile; fileRef = 293112321FC971F600966EEA /* ExchangeToken.swift */; };
293112351FC9A0D500966EEA /* CallRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 293112341FC9A0D500966EEA /* CallRequest.swift */; };
293112371FC9A24600966EEA /* UIGestureRecognizer+Closure.swift in Sources */ = {isa = PBXBuildFile; fileRef = 293112361FC9A24600966EEA /* UIGestureRecognizer+Closure.swift */; };
293204581F8DC6B20095B7C1 /* ExchangeRateCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 293204571F8DC6B20095B7C1 /* ExchangeRateCoordinator.swift */; };
2932045C1F8DCD6E0095B7C1 /* CurrencyRate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2932045B1F8DCD6E0095B7C1 /* CurrencyRate.swift */; };
2932045E1F8EEE760095B7C1 /* BalanceCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2932045D1F8EEE760095B7C1 /* BalanceCoordinator.swift */; };
293248841F88CCD2008A9818 /* SplashState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 293248831F88CCD2008A9818 /* SplashState.swift */; };
@ -409,7 +408,6 @@
293112321FC971F600966EEA /* ExchangeToken.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExchangeToken.swift; sourceTree = "<group>"; };
293112341FC9A0D500966EEA /* CallRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CallRequest.swift; sourceTree = "<group>"; };
293112361FC9A24600966EEA /* UIGestureRecognizer+Closure.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIGestureRecognizer+Closure.swift"; sourceTree = "<group>"; };
293204571F8DC6B20095B7C1 /* ExchangeRateCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExchangeRateCoordinator.swift; sourceTree = "<group>"; };
2932045B1F8DCD6E0095B7C1 /* CurrencyRate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencyRate.swift; sourceTree = "<group>"; };
2932045D1F8EEE760095B7C1 /* BalanceCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BalanceCoordinator.swift; sourceTree = "<group>"; };
293248831F88CCD2008A9818 /* SplashState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplashState.swift; sourceTree = "<group>"; };
@ -1150,7 +1148,6 @@
isa = PBXGroup;
children = (
2996F1471F6C9AE5005C33AE /* SettingsCoordinator.swift */,
293204571F8DC6B20095B7C1 /* ExchangeRateCoordinator.swift */,
290B2B6F1F930AB90053C83E /* PushNotificationsRegistrar.swift */,
);
path = Coordinators;
@ -2472,7 +2469,6 @@
2963A2881FC401490095447D /* LocalizedOperation.swift in Sources */,
29C80D371FB2CD230037B1E0 /* PendingTransaction.swift in Sources */,
613D048B1FDE162B008DE72E /* TrustProvider.swift in Sources */,
293204581F8DC6B20095B7C1 /* ExchangeRateCoordinator.swift in Sources */,
61C359E02002AA5A0097B04D /* TransactionSigning.swift in Sources */,
29C0FCE1200DA94A004A13CB /* SignMessageCoordinator.swift in Sources */,
29B933F81F8609FF009FCABB /* PaymentFlow.swift in Sources */,

@ -18,7 +18,7 @@ class ExchangeTokensCoordinator {
let exchangeConfig = ExchangeConfig(server: Config().server)
private lazy var getBalanceCoordinator: GetBalanceCoordinator = {
return GetBalanceCoordinator(session: session)
return GetBalanceCoordinator(web3: session.web3)
}()
var viewModel: ExchangeTokensViewModel {

@ -64,19 +64,25 @@ class InCoordinator: Coordinator {
}
func showTabBar(for account: Wallet) {
let web3: Web3Swift = {
let web3 = Web3Swift(url: config.rpcURL)
web3.start()
return web3
}()
let tokensStorage = TokensDataStore(account: account, config: config, web3: web3, configuration: RealmConfiguration.configuration(for: account, chainID: config.chainID))
let balance = BalanceCoordinator(account: account, storage: tokensStorage)
let session = WalletSession(
account: account,
config: config
config: config,
web3: web3,
balanceCoordinator: balance
)
MigrationInitializer(account: account, chainID: config.chainID).perform()
let realm = try! Realm()
let tokensStorage = TokensDataStore(
session: session,
realm: realm
)
MigrationInitializer(account: account, chainID: config.chainID).perform()
let realm = try! Realm()
let transactionsStorage = TransactionsStorage(
realm: realm
)

@ -1,57 +0,0 @@
// Copyright SIX DAY LLC. All rights reserved.
import Foundation
import Moya
import RealmSwift
protocol ExchangeRateCoordinatorDelegate: class {
func didUpdate(rate: CurrencyRate, in coordinator: ExchangeRateCoordinator)
}
class ExchangeRateCoordinator: NSObject {
weak var delegate: ExchangeRateCoordinatorDelegate?
private let provider = TrustProviderFactory.makeProvider()
let config: Config
init(
config: Config
) {
self.config = config
}
func start() {
fetch()
}
func fetch() {
let tokens = [TokenPrice(contract: "0x", symbol: config.server.symbol)]
let tokensPrice = TokensPrice(
currency: config.currency.rawValue,
tokens: tokens
)
provider.request(.prices(tokensPrice)) { [weak self] result in
guard let `self` = self else { return }
guard case .success(let response) = result else { return }
do {
guard let ticker = try response.map([CoinTicker].self, atKeyPath: "response", using: JSONDecoder()).first else { return }
self.update(ticker: ticker)
} catch { }
}
}
func update(ticker: CoinTicker) {
let rate = CurrencyRate(
currency: ticker.symbol,
rates: [
Rate(
code: ticker.symbol,
price: Double(ticker.price) ?? 0,
contract: ticker.contract
),
]
)
delegate?.didUpdate(rate: rate, in: self)
}
}

@ -9,12 +9,12 @@ import TrustKeystore
class GetBalanceCoordinator {
private let session: WalletSession
private let web3: Web3Swift
init(
session: WalletSession
web3: Web3Swift
) {
self.session = session
self.web3 = web3
}
func getBalance(
@ -23,7 +23,7 @@ class GetBalanceCoordinator {
completion: @escaping (Result<BigInt, AnyError>) -> Void
) {
let request = GetERC20BalanceEncode(address: address)
session.web3.request(request: request) { result in
web3.request(request: request) { result in
switch result {
case .success(let res):
let request2 = EtherServiceRequest(
@ -33,7 +33,7 @@ class GetBalanceCoordinator {
switch result2 {
case .success(let balance):
let request = GetERC20BalanceDecode(data: balance)
self?.session.web3.request(request: request) { result in
self?.web3.request(request: request) { result in
switch result {
case .success(let res):
completion(.success(BigInt(res) ?? BigInt()))
@ -53,4 +53,18 @@ class GetBalanceCoordinator {
}
}
}
func getEthBalance(
for address: Address,
completion: @escaping (Result<Balance, AnyError>) -> Void
) {
let request = EtherServiceRequest(batch: BatchFactory().create(BalanceRequest(address: address.address)))
Session.send(request) { result in
switch result {
case .success(let balance):
completion(.success(balance))
case .failure(let error):
completion(.failure(AnyError(error)))
}
}
}
}

@ -3,69 +3,43 @@
import Foundation
import APIKit
import JSONRPCKit
import Result
import BigInt
protocol BalanceCoordinatorDelegate: class {
func didUpdate(viewModel: BalanceViewModel)
}
class BalanceCoordinator {
lazy var exchangeRateCoordinator: ExchangeRateCoordinator = {
return ExchangeRateCoordinator(config: self.session.config)
}()
let session: WalletSession
let account: Wallet
let storage: TokensDataStore
var balance: Balance?
var currencyRate: CurrencyRate?
weak var delegate: BalanceCoordinatorDelegate?
var balance: Balance? {
didSet { update() }
}
var currencyRate: CurrencyRate? {
didSet { update() }
}
var viewModel: BalanceViewModel {
return BalanceViewModel(
balance: balance,
rate: currencyRate
)
}
init(session: WalletSession) {
self.session = session
}
func start() {
exchangeRateCoordinator.delegate = self
exchangeRateCoordinator.start()
fetchBalance()
}
func fetch() {
exchangeRateCoordinator.fetch()
fetchBalance()
}
func fetchBalance() {
let request = EtherServiceRequest(batch: BatchFactory().create(BalanceRequest(address: session.account.address.address)))
Session.send(request) { [weak self] result in
switch result {
case .success(let balance):
self?.balance = balance
case .failure: break
init(account: Wallet, storage: TokensDataStore) {
self.account = account
self.storage = storage
self.storage.refreshEthBalance()
storage.tokensModel.subscribe {[weak self] tokensModel in
guard let tokens = tokensModel, let eth = tokens.first(where: { $0.contract == "0x" }) else {
return
}
var ticker = storage.coinTicker(for: eth)
self?.balance = Balance(value: BigInt(eth.value, radix: 10) ?? BigInt(0))
self?.currencyRate = ticker?.rate
self?.update()
}
}
func refresh() {
self.storage.refreshEthBalance()
}
func update() {
delegate?.didUpdate(viewModel: viewModel)
}
}
extension BalanceCoordinator: ExchangeRateCoordinatorDelegate {
func didUpdate(rate: CurrencyRate, in coordinator: ExchangeRateCoordinator) {
currencyRate = rate
}
}

@ -10,28 +10,26 @@ enum RefreshType {
class WalletSession {
let account: Wallet
let web3: Web3Swift
let balanceCoordinator: BalanceCoordinator
let config: Config
let chainState: ChainState
var balance: Balance? {
return balanceCoordinator.balance
}
private lazy var balanceCoordinator: BalanceCoordinator = {
return BalanceCoordinator(session: self)
}()
var balanceViewModel: Subscribable<BalanceBaseViewModel> = Subscribable(nil)
init(
account: Wallet,
config: Config
config: Config,
web3: Web3Swift,
balanceCoordinator: BalanceCoordinator
) {
self.account = account
self.config = config
self.web3 = Web3Swift(url: config.rpcURL)
self.web3 = web3
self.chainState = ChainState(config: config)
self.web3.start()
self.balanceCoordinator.start()
self.balanceCoordinator = balanceCoordinator
self.balanceCoordinator.delegate = self
self.chainState.start()
@ -40,7 +38,7 @@ class WalletSession {
func refresh(_ type: RefreshType) {
switch type {
case .balance:
balanceCoordinator.fetch()
balanceCoordinator.refresh()
}
}

Loading…
Cancel
Save