From 8be5c5c6571810d87c3279647809f9e2942db023 Mon Sep 17 00:00:00 2001 From: Hwee-Boon Yar Date: Mon, 26 Aug 2019 20:34:17 +0800 Subject: [PATCH] =?UTF-8?q?Refactor=20GetBalanceCoordinator=20to=202=20sep?= =?UTF-8?q?arate=20coordinators=20=E2=80=94=20for=20native=20crypto=20curr?= =?UTF-8?q?ency=20and=20ERC=2020?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AlphaWallet.xcodeproj/project.pbxproj | 12 ++++--- .../Coordinators/AccountsCoordinator.swift | 2 +- .../ViewModels/AccountsViewController.swift | 6 ++-- .../NativeCryptoCurrencyBalanceView.swift | 6 ++-- ...swift => GetERC20BalanceCoordinator.swift} | 30 ++++-------------- ...tiveCryptoCurrencyBalanceCoordinator.swift | 31 +++++++++++++++++++ .../SingleChainTokenCoordinator.swift | 2 +- .../Tokens/Types/TokensDataStore.swift | 24 +++++++++----- .../FakeGetBalanceCoordinator(.swift | 2 +- 9 files changed, 71 insertions(+), 44 deletions(-) rename AlphaWallet/Tokens/Coordinators/{GetBalanceCoordinator.swift => GetERC20BalanceCoordinator.swift} (57%) create mode 100644 AlphaWallet/Tokens/Coordinators/GetNativeCryptoCurrencyBalanceCoordinator.swift diff --git a/AlphaWallet.xcodeproj/project.pbxproj b/AlphaWallet.xcodeproj/project.pbxproj index 8b64a66df..e1cc7d3bf 100644 --- a/AlphaWallet.xcodeproj/project.pbxproj +++ b/AlphaWallet.xcodeproj/project.pbxproj @@ -165,7 +165,7 @@ 29E6E06E1FE897EE0079265A /* BrowserViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29E6E06D1FE897EE0079265A /* BrowserViewController.swift */; }; 29E6E0701FEA12910079265A /* TransactionConfigurator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29E6E06F1FEA12910079265A /* TransactionConfigurator.swift */; }; 29E6E0721FEA200D0079265A /* ConfirmPaymentDetailsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29E6E0711FEA200D0079265A /* ConfirmPaymentDetailsViewModel.swift */; }; - 29E9CFCB1FE70D5B00017744 /* GetBalanceCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29E9CFCA1FE70D5B00017744 /* GetBalanceCoordinator.swift */; }; + 29E9CFCB1FE70D5B00017744 /* GetNativeCryptoCurrencyBalanceCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29E9CFCA1FE70D5B00017744 /* GetNativeCryptoCurrencyBalanceCoordinator.swift */; }; 29E9CFCD1FE7343C00017744 /* NewTokenViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29E9CFCC1FE7343C00017744 /* NewTokenViewModel.swift */; }; 29E9CFCF1FE7347200017744 /* ERCToken.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29E9CFCE1FE7347200017744 /* ERCToken.swift */; }; 29E9CFD21FE737FE00017744 /* TrustRealmConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29E9CFD11FE737FE00017744 /* TrustRealmConfiguration.swift */; }; @@ -400,6 +400,7 @@ 5E7C7788984F7ADCFE5B4DE0 /* AddressTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C75B5AF76279A71395FC7 /* AddressTextField.swift */; }; 5E7C7788FA549A0402BB33CB /* HiddenContract.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C7C01F8C42D7A43792C26 /* HiddenContract.swift */; }; 5E7C7793AB6B577906F2BCA3 /* SettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C7AFE9AF9FE6B58C925D4 /* SettingsViewController.swift */; }; + 5E7C779E0415809A7166A85F /* GetERC20BalanceCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C72AF0046EE87D00D4C56 /* GetERC20BalanceCoordinator.swift */; }; 5E7C77AD9FAAC18211B6F355 /* TransferTokensCardQuantitySelectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C7419F47CC8B2996AA8F9 /* TransferTokensCardQuantitySelectionViewController.swift */; }; 5E7C77AF8CA540D8F1404B6F /* AssetAttribute.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C7B878AC9FDC342C435F7 /* AssetAttribute.swift */; }; 5E7C77B397BCB0E254F359A8 /* DappsAutoCompletionCellViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C74BE9900543A755CB76A /* DappsAutoCompletionCellViewModel.swift */; }; @@ -853,7 +854,7 @@ 29E6E06D1FE897EE0079265A /* BrowserViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrowserViewController.swift; sourceTree = ""; }; 29E6E06F1FEA12910079265A /* TransactionConfigurator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionConfigurator.swift; sourceTree = ""; }; 29E6E0711FEA200D0079265A /* ConfirmPaymentDetailsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfirmPaymentDetailsViewModel.swift; sourceTree = ""; }; - 29E9CFCA1FE70D5B00017744 /* GetBalanceCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetBalanceCoordinator.swift; sourceTree = ""; }; + 29E9CFCA1FE70D5B00017744 /* GetNativeCryptoCurrencyBalanceCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetNativeCryptoCurrencyBalanceCoordinator.swift; sourceTree = ""; }; 29E9CFCC1FE7343C00017744 /* NewTokenViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTokenViewModel.swift; sourceTree = ""; }; 29E9CFCE1FE7347200017744 /* ERCToken.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ERCToken.swift; sourceTree = ""; }; 29E9CFD11FE737FE00017744 /* TrustRealmConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrustRealmConfiguration.swift; sourceTree = ""; }; @@ -959,6 +960,7 @@ 5E7C727CF8549291E71C1640 /* Ether.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Ether.swift; sourceTree = ""; }; 5E7C7287B9288EAA0D66BAC4 /* PreferenceOption.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreferenceOption.swift; sourceTree = ""; }; 5E7C728B3CA6A429AB5EE5DF /* ContainerViewWithShadow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContainerViewWithShadow.swift; sourceTree = ""; }; + 5E7C72AF0046EE87D00D4C56 /* GetERC20BalanceCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GetERC20BalanceCoordinator.swift; sourceTree = ""; }; 5E7C72B3371522B5C1B1B5BE /* GeneralisedTime.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GeneralisedTime.swift; sourceTree = ""; }; 5E7C72BEB789700C49FF64A6 /* DeletedContract.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeletedContract.swift; sourceTree = ""; }; 5E7C72CD0C22247A6AF7C95E /* GetERC721BalanceEncode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GetERC721BalanceEncode.swift; sourceTree = ""; }; @@ -2375,7 +2377,7 @@ 29F114EA1FA448DE00114A29 /* Coordinators */ = { isa = PBXGroup; children = ( - 29E9CFCA1FE70D5B00017744 /* GetBalanceCoordinator.swift */, + 29E9CFCA1FE70D5B00017744 /* GetNativeCryptoCurrencyBalanceCoordinator.swift */, 442FC630874D29159799EEB6 /* GetNameCoordinator.swift */, 442FCB2726DB345481C1FDA1 /* GetSymbolCoordinator.swift */, 442FC002CDEA792B8ECD3E13 /* GetERC875BalanceCoordinator.swift */, @@ -2388,6 +2390,7 @@ 76F1D4F77311FBF3A442E4B5 /* GetIsERC721ContractCoordinator.swift */, 76F1DE7BEE799DDFB68D0F54 /* GetENSOwnerCoordinator.swift */, 5E7C7A3D7408DC690C0F601C /* SingleChainTokenCoordinator.swift */, + 5E7C72AF0046EE87D00D4C56 /* GetERC20BalanceCoordinator.swift */, ); path = Coordinators; sourceTree = ""; @@ -3921,7 +3924,7 @@ AA26C61F20412A1E00318B9B /* TokensCardViewController.swift in Sources */, 29D72A2A1F6A8D1500CE9209 /* AppCoordinator.swift in Sources */, 293E626F1FA2ED1400CB0A66 /* InCoordinator.swift in Sources */, - 29E9CFCB1FE70D5B00017744 /* GetBalanceCoordinator.swift in Sources */, + 29E9CFCB1FE70D5B00017744 /* GetNativeCryptoCurrencyBalanceCoordinator.swift in Sources */, 2963A2881FC401490095447D /* LocalizedOperation.swift in Sources */, 29C80D371FB2CD230037B1E0 /* PendingTransaction.swift in Sources */, 613D048B1FDE162B008DE72E /* AlphaWalletProviderFactory.swift in Sources */, @@ -4358,6 +4361,7 @@ 5E7C7233A9A5F9D1A89FF569 /* TokensViewControllerTableViewSectionHeader.swift in Sources */, 5E7C756B31278B71E68D5E10 /* TokensViewControllerCollectiblesCollectionViewHeader.swift in Sources */, 5E7C721B569E1CD1C68295F6 /* LayoutConstraintsWrapper.swift in Sources */, + 5E7C779E0415809A7166A85F /* GetERC20BalanceCoordinator.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/AlphaWallet/Accounts/Coordinators/AccountsCoordinator.swift b/AlphaWallet/Accounts/Coordinators/AccountsCoordinator.swift index 487b5af51..d30dc44f3 100644 --- a/AlphaWallet/Accounts/Coordinators/AccountsCoordinator.swift +++ b/AlphaWallet/Accounts/Coordinators/AccountsCoordinator.swift @@ -14,7 +14,7 @@ class AccountsCoordinator: Coordinator { private let config: Config //Only show Ether balances from mainnet for now - private let balanceCoordinator = GetBalanceCoordinator(forServer: .main) + private let balanceCoordinator = GetNativeCryptoCurrencyBalanceCoordinator(forServer: .main) private let keystore: Keystore private let promptBackupCoordinator: PromptBackupCoordinator diff --git a/AlphaWallet/Accounts/ViewModels/AccountsViewController.swift b/AlphaWallet/Accounts/ViewModels/AccountsViewController.swift index 4a55a31b0..3f66855de 100644 --- a/AlphaWallet/Accounts/ViewModels/AccountsViewController.swift +++ b/AlphaWallet/Accounts/ViewModels/AccountsViewController.swift @@ -19,7 +19,7 @@ class AccountsViewController: UIViewController { private var watchedWallets: [Wallet] = [] private var balances: [AlphaWallet.Address: Balance?] = [:] private let keystore: Keystore - private let balanceCoordinator: GetBalanceCoordinator + private let balanceCoordinator: GetNativeCryptoCurrencyBalanceCoordinator private var etherKeystore = try? EtherKeystore() weak var delegate: AccountsViewControllerDelegate? @@ -28,7 +28,7 @@ class AccountsViewController: UIViewController { return !keystore.wallets.isEmpty } - init(keystore: Keystore, balanceCoordinator: GetBalanceCoordinator) { + init(keystore: Keystore, balanceCoordinator: GetNativeCryptoCurrencyBalanceCoordinator) { self.keystore = keystore self.balanceCoordinator = balanceCoordinator super.init(nibName: nil, bundle: nil) @@ -123,7 +123,7 @@ class AccountsViewController: UIViewController { let addresses = (hdWallets + keystoreWallets + watchedWallets).compactMap { $0.address } var counter = 0 for address in addresses { - balanceCoordinator.getEthBalance(for: address, completion: { [weak self] (result) in + balanceCoordinator.getBalance(for: address, completion: { [weak self] (result) in self?.balances[address] = result.value counter += 1 if counter == addresses.count { diff --git a/AlphaWallet/Browser/Views/NativeCryptoCurrencyBalanceView.swift b/AlphaWallet/Browser/Views/NativeCryptoCurrencyBalanceView.swift index 4ffd62fc4..e3da527b0 100644 --- a/AlphaWallet/Browser/Views/NativeCryptoCurrencyBalanceView.swift +++ b/AlphaWallet/Browser/Views/NativeCryptoCurrencyBalanceView.swift @@ -8,14 +8,14 @@ class NativeCryptoCurrencyBalanceView: UIView { var session: WalletSession { didSet { configure() - balanceCoordinator = GetBalanceCoordinator(forServer: server) + balanceCoordinator = GetNativeCryptoCurrencyBalanceCoordinator(forServer: server) refreshWalletBalance() } } private let rightMargin: CGFloat private var balances: [AlphaWallet.Address: Balance] = [:] //TODO should let someone else fetch the balance instead of doing it here - private lazy var balanceCoordinator = GetBalanceCoordinator(forServer: server) + private lazy var balanceCoordinator = GetNativeCryptoCurrencyBalanceCoordinator(forServer: server) private let label = UILabel() private let horizontalMarginAroundLabel = CGFloat(7) private let verticalMarginAroundLabel = CGFloat(4) @@ -97,7 +97,7 @@ class NativeCryptoCurrencyBalanceView: UIView { private func refreshWalletBalance() { let address = session.account.address - balanceCoordinator.getEthBalance(for: address, completion: { [weak self] (result) in + balanceCoordinator.getBalance(for: address, completion: { [weak self] (result) in guard let strongSelf = self else { return } strongSelf.balances[address] = result.value strongSelf.configure() diff --git a/AlphaWallet/Tokens/Coordinators/GetBalanceCoordinator.swift b/AlphaWallet/Tokens/Coordinators/GetERC20BalanceCoordinator.swift similarity index 57% rename from AlphaWallet/Tokens/Coordinators/GetBalanceCoordinator.swift rename to AlphaWallet/Tokens/Coordinators/GetERC20BalanceCoordinator.swift index 5fc97b16f..15bdcc4fc 100644 --- a/AlphaWallet/Tokens/Coordinators/GetBalanceCoordinator.swift +++ b/AlphaWallet/Tokens/Coordinators/GetERC20BalanceCoordinator.swift @@ -1,23 +1,21 @@ -// Copyright © 2018 Stormbird PTE. LTD. +// Copyright © 2019 Stormbird PTE. LTD. import Foundation import BigInt -import JSONRPCKit -import APIKit import Result import web3swift -class GetBalanceCoordinator { - let server: RPCServer +class GetERC20BalanceCoordinator { + private let server: RPCServer init(forServer server: RPCServer) { self.server = server } func getBalance( - for address: AlphaWallet.Address, - contract: AlphaWallet.Address, - completion: @escaping (ResultResult.t) -> Void + for address: AlphaWallet.Address, + contract: AlphaWallet.Address, + completion: @escaping (ResultResult.t) -> Void ) { let functionName = "balanceOf" callSmartContract(withServer: server, contract: contract, functionName: functionName, abiString: web3swift.Web3.Utils.erc20ABI, parameters: [address.eip55String] as [AnyObject]).done { balanceResult in @@ -35,20 +33,4 @@ class GetBalanceCoordinator { completion(.failure(AnyError($0))) } } - - func getEthBalance( - for address: AlphaWallet.Address, - completion: @escaping (ResultResult.t) -> Void - ) { - let request = EtherServiceRequest(server: server, batch: BatchFactory().create(BalanceRequest(address: address))) - Session.send(request) { result in - switch result { - case .success(let balance): - completion(.success(balance)) - case .failure(let error): - completion(.failure(AnyError(error))) - } - } - } } - diff --git a/AlphaWallet/Tokens/Coordinators/GetNativeCryptoCurrencyBalanceCoordinator.swift b/AlphaWallet/Tokens/Coordinators/GetNativeCryptoCurrencyBalanceCoordinator.swift new file mode 100644 index 000000000..995eaf613 --- /dev/null +++ b/AlphaWallet/Tokens/Coordinators/GetNativeCryptoCurrencyBalanceCoordinator.swift @@ -0,0 +1,31 @@ +// Copyright © 2018 Stormbird PTE. LTD. + +import Foundation +import BigInt +import JSONRPCKit +import APIKit +import Result +import web3swift + +class GetNativeCryptoCurrencyBalanceCoordinator { + let server: RPCServer + + init(forServer server: RPCServer) { + self.server = server + } + + func getBalance( + for address: AlphaWallet.Address, + completion: @escaping (ResultResult.t) -> Void + ) { + let request = EtherServiceRequest(server: server, batch: BatchFactory().create(BalanceRequest(address: address))) + Session.send(request) { result in + switch result { + case .success(let balance): + completion(.success(balance)) + case .failure(let error): + completion(.failure(AnyError(error))) + } + } + } +} diff --git a/AlphaWallet/Tokens/Coordinators/SingleChainTokenCoordinator.swift b/AlphaWallet/Tokens/Coordinators/SingleChainTokenCoordinator.swift index 36e5be4bc..57d91dbec 100644 --- a/AlphaWallet/Tokens/Coordinators/SingleChainTokenCoordinator.swift +++ b/AlphaWallet/Tokens/Coordinators/SingleChainTokenCoordinator.swift @@ -208,7 +208,7 @@ class SingleChainTokenCoordinator: Coordinator { } } case .erc20: - let balanceCoordinator = GetBalanceCoordinator(forServer: self.session.server) + let balanceCoordinator = GetERC20BalanceCoordinator(forServer: self.session.server) balanceCoordinator.getBalance(for: address, contract: each) { [weak self] result in guard let strongSelf = self else { contractsProcessed += 1 diff --git a/AlphaWallet/Tokens/Types/TokensDataStore.swift b/AlphaWallet/Tokens/Types/TokensDataStore.swift index 4df9d6330..4165bc5ef 100644 --- a/AlphaWallet/Tokens/Types/TokensDataStore.swift +++ b/AlphaWallet/Tokens/Types/TokensDataStore.swift @@ -16,10 +16,6 @@ protocol TokensDataStoreDelegate: class { } class TokensDataStore { - private lazy var getBalanceCoordinator: GetBalanceCoordinator = { - return GetBalanceCoordinator(forServer: server) - }() - private lazy var getNameCoordinator: GetNameCoordinator = { return GetNameCoordinator(forServer: server) }() @@ -28,6 +24,14 @@ class TokensDataStore { return GetSymbolCoordinator(forServer: server) }() + private lazy var getNativeCryptoCurrencyBalanceCoordinator: GetNativeCryptoCurrencyBalanceCoordinator = { + return GetNativeCryptoCurrencyBalanceCoordinator(forServer: server) + }() + + private lazy var getERC20BalanceCoordinator: GetERC20BalanceCoordinator = { + return GetERC20BalanceCoordinator(forServer: server) + }() + private lazy var getERC875BalanceCoordinator: GetERC875BalanceCoordinator = { return GetERC875BalanceCoordinator(forServer: server) }() @@ -253,6 +257,12 @@ class TokensDataStore { } } + func getERC20Balance(for address: AlphaWallet.Address, completion: @escaping (ResultResult.t) -> Void) { + getERC20BalanceCoordinator.getBalance(for: account.address, contract: address) { result in + completion(result) + } + } + func getERC875Balance(for address: AlphaWallet.Address, completion: @escaping (ResultResult<[String], AnyError>.t) -> Void) { getERC875BalanceCoordinator.getERC875TokenBalance(for: account.address, contract: address) { result in @@ -359,7 +369,7 @@ class TokensDataStore { case .nativeCryptocurrency: incrementCountAndUpdateDelegate() case .erc20: - getBalanceCoordinator.getBalance(for: account.address, contract: tokenObject.contractAddress) { [weak self] result in + getERC20Balance(for: tokenObject.contractAddress, completion: { [weak self] result in defer { incrementCountAndUpdateDelegate() } guard let strongSelf = self else { return } switch result { @@ -368,7 +378,7 @@ class TokensDataStore { case .failure: break } - } + }) case .erc875: getERC875Balance(for: tokenObject.contractAddress, completion: { [weak self] result in defer { incrementCountAndUpdateDelegate() } @@ -458,7 +468,7 @@ class TokensDataStore { } func refreshETHBalance() { - getBalanceCoordinator.getEthBalance(for: account.address) { [weak self] result in + getNativeCryptoCurrencyBalanceCoordinator.getBalance(for: account.address) { [weak self] result in guard let strongSelf = self else { return } switch result { case .success(let balance): diff --git a/AlphaWalletTests/Factories/FakeGetBalanceCoordinator(.swift b/AlphaWalletTests/Factories/FakeGetBalanceCoordinator(.swift index bacc34988..4f188f598 100644 --- a/AlphaWalletTests/Factories/FakeGetBalanceCoordinator(.swift +++ b/AlphaWalletTests/Factories/FakeGetBalanceCoordinator(.swift @@ -3,7 +3,7 @@ import Foundation @testable import AlphaWallet -class FakeGetBalanceCoordinator: GetBalanceCoordinator { +class FakeGetBalanceCoordinator: GetNativeCryptoCurrencyBalanceCoordinator { convenience init() { self.init(forServer: .main) }