Filter delegate contracts when autodetecting tokens

pull/645/head
Hwee-Boon Yar 6 years ago
parent 1a74d9228c
commit 188e8f1d6f
  1. 4
      AlphaWallet.xcodeproj/project.pbxproj
  2. 2
      AlphaWallet/Core/Initializers/MigrationInitializer.swift
  3. 36
      AlphaWallet/Tokens/Coordinators/TokensCoordinator.swift
  4. 13
      AlphaWallet/Tokens/Types/DelegateContract.swift
  5. 10
      AlphaWallet/Tokens/Types/TokensDataStore.swift

@ -337,6 +337,7 @@
5E7C764D3C130AAB26E80EC1 /* AmountTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C73617E3A4C0B9A90A5F8 /* AmountTextField.swift */; };
5E7C76696EF7F27EC0788CDD /* GenerateTransferMagicLinkViewControllerViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C7EEAAE9C23B68419E9F5 /* GenerateTransferMagicLinkViewControllerViewModel.swift */; };
5E7C7669BBE6255A2377E070 /* SetSellTokensCardExpiryDateViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C7962AE417E12F13FF58E /* SetSellTokensCardExpiryDateViewController.swift */; };
5E7C767C9166726E96ED4C07 /* DelegateContract.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C72DDBF109139E4C661D5 /* DelegateContract.swift */; };
5E7C7692C981580CD32228EB /* ChooseTokenCardTransferModeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C70FB40612BB02594EC00 /* ChooseTokenCardTransferModeViewController.swift */; };
5E7C769D2BFC2809F0EA5AA3 /* GeneralisedTime.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C7142F1598ECC93F3A673 /* GeneralisedTime.swift */; };
5E7C76A0365D128B7F19A0C2 /* ProtectionCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C74BEC095303B66FB4B1E /* ProtectionCoordinator.swift */; };
@ -871,6 +872,7 @@
5E7C72BEB789700C49FF64A6 /* DeletedContract.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeletedContract.swift; sourceTree = "<group>"; };
5E7C72CEFD7E32ACE303AB1F /* BookmarkViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = BookmarkViewCell.xib; sourceTree = "<group>"; };
5E7C72D0E7CA03ADE5CFAE7A /* ImportMagicTokenViewControllerViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImportMagicTokenViewControllerViewModel.swift; sourceTree = "<group>"; };
5E7C72DDBF109139E4C661D5 /* DelegateContract.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DelegateContract.swift; sourceTree = "<group>"; };
5E7C7318B6059C18BE87ECAE /* ABIValue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ABIValue.swift; sourceTree = "<group>"; };
5E7C731B6F01534683227123 /* NonFungibleTokenViewCellViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NonFungibleTokenViewCellViewModel.swift; sourceTree = "<group>"; };
5E7C73495E0C0A207152EC25 /* LockEnterPasscodeViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = LockEnterPasscodeViewController.swift; path = AlphaWallet/AlphaWalletLock/ViewControllers/LockEnterPasscodeViewController.swift; sourceTree = SOURCE_ROOT; };
@ -1824,6 +1826,7 @@
5E7C7382EAC8B9CE5EE0668D /* CryptoKitty.swift */,
5E7C758EEBD945A3451C96C8 /* CryptoKittyHandling.swift */,
5E7C7B6FAFE62FBAADB85228 /* Web3Error.swift */,
5E7C72DDBF109139E4C661D5 /* DelegateContract.swift */,
);
path = Types;
sourceTree = "<group>";
@ -3886,6 +3889,7 @@
5E7C71A2EAA5124E07AA54B6 /* Favicon.swift in Sources */,
5E7C7EC725C711F5EA12CB1C /* History.swift in Sources */,
5E7C7226D3B95E63ED9EDD33 /* FetchAssetDefinitionsCoordinator.swift in Sources */,
5E7C767C9166726E96ED4C07 /* DelegateContract.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

@ -20,7 +20,7 @@ class MigrationInitializer: Initializer {
}
func perform() {
config.schemaVersion = 46
config.schemaVersion = 47
config.migrationBlock = { migration, oldSchemaVersion in
switch oldSchemaVersion {
case 0...32:

@ -19,6 +19,7 @@ private enum ContractData {
case decimals(UInt8)
case nonFungibleTokenComplete(name: String, symbol: String, balance: [String], tokenType: TokenType)
case fungibleTokenComplete(name: String, symbol: String, decimals: UInt8)
case delegateTokenComplete
case failed(networkReachable: Bool?)
}
@ -92,7 +93,8 @@ class TokensCoordinator: Coordinator {
let alreadyAddedContracts = self.storage.enabledObject.map { $0.address.eip55String.lowercased() }
let deletedContracts = self.storage.deletedContracts.map { $0.contract.lowercased() }
let hiddenContracts = self.storage.hiddenContracts.map { $0.contract.lowercased() }
let contractsToAdd = detectedContracts - alreadyAddedContracts - deletedContracts - hiddenContracts
let delegateContracts = self.storage.delegateContracts.map { $0.contract.lowercased() }
let contractsToAdd = detectedContracts - alreadyAddedContracts - deletedContracts - hiddenContracts - delegateContracts
var contractsPulled = 0
var hasRefreshedAfterAddingAllContracts = false
for eachContract in contractsToAdd {
@ -141,6 +143,9 @@ class TokensCoordinator: Coordinator {
)
self.storage.add(tokens: [token])
completion()
case .delegateTokenComplete:
self.storage.add(delegateContracts: [DelegateContract(contract: contract)])
completion()
case .failed(let networkReachable):
if let networkReachable = networkReachable, networkReachable {
self.storage.add(deadContracts: [DeletedContract(contract: contract)])
@ -200,14 +205,6 @@ class TokensCoordinator: Coordinator {
var completedTokenType: TokenType?
var failed = false
func callCompletionOnAllData() {
if let completedName = completedName, let completedSymbol = completedSymbol, let completedBalance = completedBalance, let tokenType = completedTokenType {
completion(.nonFungibleTokenComplete(name: completedName, symbol: completedSymbol, balance: completedBalance, tokenType: tokenType))
} else if let completedName = completedName, let completedSymbol = completedSymbol, let completedDecimals = completedDecimals {
completion(.fungibleTokenComplete(name: completedName, symbol: completedSymbol, decimals: completedDecimals))
}
}
func callCompletionFailed() {
guard !failed else { return }
failed = true
@ -215,6 +212,24 @@ class TokensCoordinator: Coordinator {
completion(.failed(networkReachable: NetworkReachabilityManager()?.isReachable))
}
func callCompletionOnAllData() {
if let completedName = completedName, let completedSymbol = completedSymbol, let completedBalance = completedBalance, let tokenType = completedTokenType {
completion(.nonFungibleTokenComplete(name: completedName, symbol: completedSymbol, balance: completedBalance, tokenType: tokenType))
} else if let completedName = completedName, let completedSymbol = completedSymbol, let completedDecimals = completedDecimals {
if completedSymbol.isEmpty {
//Must check because we also get an empty symbol (and name) if there's no connectivity
//TODO maybe better to share an instance of the reachability manager
if let reachabilityManager = NetworkReachabilityManager(), reachabilityManager.isReachable {
completion(.delegateTokenComplete)
} else {
callCompletionFailed()
}
} else {
completion(.fungibleTokenComplete(name: completedName, symbol: completedSymbol, decimals: completedDecimals))
}
}
}
assetDefinitionStore.fetchXML(forContract: address)
storage.getContractName(for: address) { result in
@ -329,6 +344,9 @@ extension TokensCoordinator: NewTokenViewControllerDelegate {
viewController.updateForm(forTokenType: tokenType)
case .fungibleTokenComplete:
viewController.updateForm(forTokenType: .erc20)
case .delegateTokenComplete:
viewController.updateForm(forTokenType: .erc20)
break
case .failed:
break
}

@ -0,0 +1,13 @@
// Copyright © 2018 Stormbird PTE. LTD.
import Foundation
import RealmSwift
class DelegateContract: Object {
@objc dynamic var contract: String = ""
convenience init(contract: String) {
self.init()
self.contract = contract
}
}

@ -140,6 +140,10 @@ class TokensDataStore {
return Array(realm.objects(DeletedContract.self))
}
var delegateContracts: [DelegateContract] {
return Array(realm.objects(DelegateContract.self))
}
var hiddenContracts: [HiddenContract] {
return Array(realm.objects(HiddenContract.self))
}
@ -441,6 +445,12 @@ class TokensDataStore {
}
}
func add(delegateContracts: [DelegateContract]) {
try! realm.write {
realm.add(delegateContracts, update: false)
}
}
func add(hiddenContracts: [HiddenContract]) {
try! realm.write {
realm.add(hiddenContracts, update: false)

Loading…
Cancel
Save