Merge pull request #3277 from vladyslav-iosdev/#3262

Chain selection doesn't work properly in Settings > Select Active Networks if user has added custom chain before app officially supports the same chain #3262
pull/3288/head
Hwee-Boon Yar 3 years ago committed by GitHub
commit 62c4c87369
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      AlphaWallet/Browser/Coordinators/DappBrowserCoordinator.swift
  2. 44
      AlphaWallet/Core/Coordinators/WalletBalance/WalletBalanceFetcher.swift
  3. 8
      AlphaWallet/Core/Initializers/MigrationInitializer.swift
  4. 4
      AlphaWallet/Extensions/Session+PromiseKit.swift
  5. 2
      AlphaWallet/Settings/Types/Config.swift
  6. 28
      AlphaWallet/Settings/Types/RPCServers.swift
  7. 2
      AlphaWallet/Settings/ViewControllers/AddRPCServerViewController.swift
  8. 2
      AlphaWallet/Tokens/Logic/Erc1155TokenIdsFetcher.swift
  9. 2
      AlphaWallet/Transactions/Storage/TransactionsStorage.swift
  10. 1
      AlphaWallet/WalletConnect/WalletConnectServer.swift
  11. 2
      AlphaWalletTests/Settings/Coordinators/ServersCoordinatorTests.swift
  12. 6
      AlphaWalletTests/Settings/RPCServerTests.swift

@ -376,7 +376,7 @@ final class DappBrowserCoordinator: NSObject, Coordinator {
}
func isMagicLink(_ url: URL) -> Bool {
return RPCServer.allCases.contains { $0.magicLinkHost == url.host }
return RPCServer.availableServers.contains { $0.magicLinkHost == url.host }
}
func `switch`(toServer server: RPCServer, url: URL? = nil) {

@ -65,13 +65,7 @@ class WalletBalanceFetcher: NSObject, WalletBalanceFetcherType {
super.init()
for each in servers {
let transactionsStorage = TransactionsStorage(realm: realm, server: each, delegate: nil)
let tokensDatastore = TokensDataStore(realm: realm, account: wallet, server: each)
let balanceFetcher = PrivateBalanceFetcher(account: wallet, tokensDatastore: tokensDatastore, server: each, assetDefinitionStore: assetDefinitionStore, queue: queue)
balanceFetcher.erc721TokenIdsFetcher = transactionsStorage
balanceFetcher.delegate = self
self.services[each] = (tokensDatastore, balanceFetcher, transactionsStorage)
services[each] = createServices(wallet: wallet, server: each)
}
coinTickersFetcher.tickersSubscribable.subscribe { [weak self] _ in
@ -85,11 +79,35 @@ class WalletBalanceFetcher: NSObject, WalletBalanceFetcherType {
}
func transactionsStorage(server: RPCServer) -> TransactionsStorage {
services[server].transactionsStorage
if let services = services[safe: server] {
return services.transactionsStorage
} else {
let subServices = createServices(wallet: wallet, server: server)
services[server] = subServices
return subServices.transactionsStorage
}
}
func tokensDatastore(server: RPCServer) -> TokensDataStore {
services[server].tokensDataStore
if let services = services[safe: server] {
return services.tokensDataStore
} else {
let subServices = createServices(wallet: wallet, server: server)
services[server] = subServices
return subServices.tokensDataStore
}
}
private func createServices(wallet: Wallet, server: RPCServer) -> WalletBalanceFetcherSubServices {
let transactionsStorage = TransactionsStorage(realm: realm, server: server, delegate: nil)
let tokensDatastore = TokensDataStore(realm: realm, account: wallet, server: server)
let balanceFetcher = PrivateBalanceFetcher(account: wallet, tokensDatastore: tokensDatastore, server: server, assetDefinitionStore: assetDefinitionStore, queue: queue)
balanceFetcher.erc721TokenIdsFetcher = transactionsStorage
balanceFetcher.delegate = self
return (tokensDatastore, balanceFetcher, transactionsStorage)
}
func update(servers: [RPCServer]) {
@ -97,13 +115,7 @@ class WalletBalanceFetcher: NSObject, WalletBalanceFetcherType {
if services[safe: each] != nil {
//no-op
} else {
let transactionsStorage = TransactionsStorage(realm: realm, server: each, delegate: nil)
let tokensDatastore = TokensDataStore(realm: realm, account: wallet, server: each)
let balanceFetcher = PrivateBalanceFetcher(account: wallet, tokensDatastore: tokensDatastore, server: each, assetDefinitionStore: assetDefinitionStore, queue: queue)
balanceFetcher.erc721TokenIdsFetcher = transactionsStorage
balanceFetcher.delegate = self
services[each] = (tokensDatastore, balanceFetcher, transactionsStorage)
services[each] = createServices(wallet: wallet, server: each)
}
}

@ -66,7 +66,7 @@ class MigrationInitializer: Initializer {
if oldSchemaVersion < 7 {
//Fix bug where we marked all transactions as completed successfully without checking `isError` from Etherscan
migration.deleteData(forType: Transaction.className())
for each in RPCServer.allCases {
for each in RPCServer.availableServers {
Config.setLastFetchedErc20InteractionBlockNumber(0, server: each, wallet: strongSelf.account.address)
}
migration.deleteData(forType: EventActivity.className())
@ -75,7 +75,7 @@ class MigrationInitializer: Initializer {
//Clear all transactions data so we can fetch them again and capture `LocalizedOperationObject` children correctly
migration.deleteData(forType: Transaction.className())
migration.deleteData(forType: LocalizedOperationObject.className())
for each in RPCServer.allCases {
for each in RPCServer.availableServers {
Config.setLastFetchedErc20InteractionBlockNumber(0, server: each, wallet: strongSelf.account.address)
}
migration.deleteData(forType: EventActivity.className())
@ -136,7 +136,7 @@ extension MigrationInitializer {
do {
try realm.write {
for each in RPCServer.allCases {
for each in RPCServer.availableServers {
let migration = MigrationInitializerForOneChainPerDatabase(account: account, server: each, assetDefinitionStore: assetDefinitionStore)
migration.perform()
let oldPerChainDatabase = try! Realm(configuration: migration.config)
@ -163,7 +163,7 @@ extension MigrationInitializer {
}
}
}
for each in RPCServer.allCases {
for each in RPCServer.availableServers {
let migration = MigrationInitializerForOneChainPerDatabase(account: account, server: each, assetDefinitionStore: assetDefinitionStore)
let realmUrl = migration.config.fileURL!
let realmUrls = [

@ -125,6 +125,6 @@ extension Session {
extension RPCServer {
static func serverWithRpcURL(_ string: String) -> RPCServer? {
RPCServer.allCases.first { $0.rpcURL.absoluteString == string }
RPCServer.availableServers.first { $0.rpcURL.absoluteString == string }
}
}
}

@ -181,7 +181,7 @@ struct Config {
//TODO remote log. Why is this possible? Note it's not nil (which is possible for new installs)
return Constants.defaultEnabledServers
} else {
return chainIds.map { .init(chainID: $0) }
return chainIds.map { .init(chainID: $0) }.filter { $0.conflictedServer == nil }
}
} else {
return Constants.defaultEnabledServers

@ -711,12 +711,12 @@ enum RPCServer: Hashable, CaseIterable {
init(name: String) {
//TODO defaulting to .main is bad
self = Self.allCases.first { $0.name == name } ?? .main
self = Self.availableServers.first { $0.name == name } ?? .main
}
init(chainID: Int) {
//TODO defaulting to .main is bad
self = Self.allCases.first { $0.chainID == chainID } ?? .main
self = Self.availableServers.first { $0.chainID == chainID } ?? .main
}
init?(withMagicLinkHost magicLinkHost: String) {
@ -725,7 +725,7 @@ enum RPCServer: Hashable, CaseIterable {
if magicLinkHost == Constants.legacyMagicLinkHost {
server = .main
} else {
server = Self.allCases.first { $0.magicLinkHost == magicLinkHost }
server = Self.availableServers.first { $0.magicLinkHost == magicLinkHost }
}
guard let createdServer = server else { return nil }
self = createdServer
@ -738,7 +738,7 @@ enum RPCServer: Hashable, CaseIterable {
//We'll have to manually new cases here
//Cannot be `let` as the chains can change dynamically without the app being restarted (i.e. killed). The UI can be restarted though (when switching changes)
static var allCases: [RPCServer] {
static internal var allCases: [RPCServer] {
return [
.main,
.kovan,
@ -765,7 +765,11 @@ enum RPCServer: Hashable, CaseIterable {
.optimistic,
.optimisticKovan,
.cronosTestnet,
] + RPCServer.customServers
]
}
static var availableServers: [RPCServer] {
allCases + RPCServer.customServers
}
private static func convertJsonToCustomRpcs(_ json: String?) -> [CustomRPC] {
@ -827,3 +831,17 @@ extension URL {
}
}
}
extension RPCServer {
var conflictedServer: RPCServer? {
for each in RPCServer.availableServers {
if let index = RPCServer.allCases.index(where: { each == $0 }), each.isCustom {
return RPCServer.allCases[index]
} else {
continue
}
}
return nil
}
}

@ -218,7 +218,7 @@ class AddRPCServerViewController: UIViewController {
}
if let chainId = Int(chainId0xString: chainIDTextField.value.trimmed), chainId > 0 {
if config.enabledServers.contains(where: { $0.chainID == chainId }) {
if RPCServer.availableServers.contains(where: { $0.chainID == chainId }) {
isValid = false
//TODO maybe a prompt with button to enable it instead?
chainIDTextField.status = .error(R.string.localizable.addrpcServerChainIdAlreadySupported())

@ -117,7 +117,7 @@ class Erc1155TokenIdsFetcher {
}
static func deleteForWallet(_ address: AlphaWallet.Address) {
for each in RPCServer.allCases {
for each in RPCServer.availableServers {
let file = fileUrl(forWallet: address, server: each)
try? FileManager.default.removeItem(at: file)
}

@ -322,7 +322,7 @@ class TransactionsStorage: Hashable {
}
static func deleteAllTransactions(realm: Realm) {
for each in RPCServer.allCases {
for each in RPCServer.availableServers {
let transactionsStorage = TransactionsStorage(realm: realm, server: each, delegate: nil)
transactionsStorage.deleteAll()
}

@ -25,7 +25,6 @@ protocol WalletConnectServerDelegate: AnyObject {
}
typealias WalletConnectRequest = WalletConnectSwift.Request
typealias WalletConnectRequestID = WalletConnectSwift.RequestID
extension WalletConnectSession {

@ -5,6 +5,6 @@ import XCTest
class ServersCoordinatorTests: XCTestCase {
func testServerListIsComplete() {
XCTAssertEqual(Set(ServersCoordinator.serversOrdered), Set(RPCServer.allCases))
XCTAssertEqual(Set(ServersCoordinator.serversOrdered), Set(RPCServer.availableServers))
}
}

@ -24,19 +24,19 @@ class RPCServerTests: XCTestCase {
}
func testInitByNameCorrect() {
for each in RPCServer.allCases {
for each in RPCServer.availableServers {
XCTAssertEqual(RPCServer(name: each.name), each)
}
}
func testInitByChainIdCorrect() {
for each in RPCServer.allCases {
for each in RPCServer.availableServers {
XCTAssertEqual(RPCServer(chainID: each.chainID), each)
}
}
func testDisplayOrderPriorityUnique() {
let all = RPCServer.allCases
let all = RPCServer.availableServers
let orders = Set(all.map(\.displayOrderPriority))
XCTAssertEqual(orders.count, all.count)
}

Loading…
Cancel
Save