Refactor GetBalanceCoordinator to 2 separate coordinators — for native crypto currency and ERC 20

pull/1389/head
Hwee-Boon Yar 5 years ago
parent 15f304f3b7
commit 8be5c5c657
  1. 12
      AlphaWallet.xcodeproj/project.pbxproj
  2. 2
      AlphaWallet/Accounts/Coordinators/AccountsCoordinator.swift
  3. 6
      AlphaWallet/Accounts/ViewModels/AccountsViewController.swift
  4. 6
      AlphaWallet/Browser/Views/NativeCryptoCurrencyBalanceView.swift
  5. 30
      AlphaWallet/Tokens/Coordinators/GetERC20BalanceCoordinator.swift
  6. 31
      AlphaWallet/Tokens/Coordinators/GetNativeCryptoCurrencyBalanceCoordinator.swift
  7. 2
      AlphaWallet/Tokens/Coordinators/SingleChainTokenCoordinator.swift
  8. 24
      AlphaWallet/Tokens/Types/TokensDataStore.swift
  9. 2
      AlphaWalletTests/Factories/FakeGetBalanceCoordinator(.swift

@ -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 = "<group>"; };
29E6E06F1FEA12910079265A /* TransactionConfigurator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionConfigurator.swift; sourceTree = "<group>"; };
29E6E0711FEA200D0079265A /* ConfirmPaymentDetailsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfirmPaymentDetailsViewModel.swift; sourceTree = "<group>"; };
29E9CFCA1FE70D5B00017744 /* GetBalanceCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetBalanceCoordinator.swift; sourceTree = "<group>"; };
29E9CFCA1FE70D5B00017744 /* GetNativeCryptoCurrencyBalanceCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetNativeCryptoCurrencyBalanceCoordinator.swift; sourceTree = "<group>"; };
29E9CFCC1FE7343C00017744 /* NewTokenViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTokenViewModel.swift; sourceTree = "<group>"; };
29E9CFCE1FE7347200017744 /* ERCToken.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ERCToken.swift; sourceTree = "<group>"; };
29E9CFD11FE737FE00017744 /* TrustRealmConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrustRealmConfiguration.swift; sourceTree = "<group>"; };
@ -959,6 +960,7 @@
5E7C727CF8549291E71C1640 /* Ether.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Ether.swift; sourceTree = "<group>"; };
5E7C7287B9288EAA0D66BAC4 /* PreferenceOption.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreferenceOption.swift; sourceTree = "<group>"; };
5E7C728B3CA6A429AB5EE5DF /* ContainerViewWithShadow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContainerViewWithShadow.swift; sourceTree = "<group>"; };
5E7C72AF0046EE87D00D4C56 /* GetERC20BalanceCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GetERC20BalanceCoordinator.swift; sourceTree = "<group>"; };
5E7C72B3371522B5C1B1B5BE /* GeneralisedTime.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GeneralisedTime.swift; sourceTree = "<group>"; };
5E7C72BEB789700C49FF64A6 /* DeletedContract.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeletedContract.swift; sourceTree = "<group>"; };
5E7C72CD0C22247A6AF7C95E /* GetERC721BalanceEncode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GetERC721BalanceEncode.swift; sourceTree = "<group>"; };
@ -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 = "<group>";
@ -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;
};

@ -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

@ -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 {

@ -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()

@ -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<BigInt, AnyError>.t) -> Void
for address: AlphaWallet.Address,
contract: AlphaWallet.Address,
completion: @escaping (ResultResult<BigInt, AnyError>.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<Balance, AnyError>.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)))
}
}
}
}

@ -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<Balance, AnyError>.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)))
}
}
}
}

@ -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

@ -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<BigInt, AnyError>.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):

@ -3,7 +3,7 @@
import Foundation
@testable import AlphaWallet
class FakeGetBalanceCoordinator: GetBalanceCoordinator {
class FakeGetBalanceCoordinator: GetNativeCryptoCurrencyBalanceCoordinator {
convenience init() {
self.init(forServer: .main)
}

Loading…
Cancel
Save