Only store the result from getName RPC call for token in datastore. Localized token type name is only appended at runtime

pull/672/head
Hwee-Boon Yar 6 years ago
parent 785075d77f
commit a31a7db1a3
  1. 6
      AlphaWallet/AssetDefinition/XMLHandler.swift
  2. 15
      AlphaWallet/Core/Initializers/MigrationInitializer.swift
  3. 7
      AlphaWallet/Settings/Types/Config.swift
  4. 1
      AlphaWallet/Settings/Types/Constants.swift
  5. 2
      AlphaWallet/Tokens/Coordinators/TokensCoordinator.swift
  6. 19
      AlphaWallet/Tokens/Types/TokenObject.swift
  7. 28
      AlphaWallet/Tokens/Types/TokensDataStore.swift

@ -83,12 +83,10 @@ private class PrivateXMLHandler {
func getName() -> String {
let lang = getLang()
if let name = contract?["name"].getElementWithLangAttribute(equals: lang)?.text {
if contractAddress.sameContract(as: Constants.ticketContractAddress) || contractAddress.sameContract(as: Constants.ticketContractAddressRopsten ) {
return "\(Constants.fifaWorldCup2018TokenNamePrefix) \(name)"
}
return name
} else {
return "N/A"
}
return "N/A"
}
func getTokenTypeName(_ type: SingularOrPlural = .plural, titlecase: TitlecaseOrNot = .titlecase) -> String {

@ -20,7 +20,7 @@ class MigrationInitializer: Initializer {
}
func perform() {
config.schemaVersion = 48
config.schemaVersion = 49
config.migrationBlock = { migration, oldSchemaVersion in
switch oldSchemaVersion {
case 0...32:
@ -53,6 +53,19 @@ class MigrationInitializer: Initializer {
}
}
}
if oldSchemaVersion < 49 {
//In schemaVersion 49, we clear the token's `name` because we want it to only contain the name returned by the RPC name call and not the localized text
migration.enumerateObjects(ofType: TokenObject.className()) { oldObject, newObject in
guard let oldObject = oldObject else { return }
guard let newObject = newObject else { return }
if let contract = oldObject["contract"] as? String {
let tokenTypeName = XMLHandler(contract: contract).getName()
if tokenTypeName != "N/A" {
newObject["name"] = ""
}
}
}
}
}
}
}

@ -129,13 +129,6 @@ struct Config {
///Debugging flag. Set to false to disable auto fetching prices, etc to cut down on network calls
let isAutoFetchingDisabled = false
//TODO move?
func getContractLocalizedName(forContract contract: String) -> String? {
guard let contractAddress = Address(string: contract) else { return nil }
let xmlHandler = XMLHandler(contract: contract)
return xmlHandler.getName()
}
func addToWalletAddressesAlreadyPromptedForBackup(address: String) {
var addresses: [String]
if let value = defaults.array(forKey: Keys.walletAddressesAlreadyPromptedForBackUp) {

@ -40,7 +40,6 @@ public struct Constants {
// FIFA hardcoded FIFA token address
public static let ticketContractAddress = "0xA66A3F08068174e8F005112A8b2c7A507a822335"
public static let ticketContractAddressRopsten = "0xD8e5F58DE3933E1E35f9c65eb72cb188674624F3"
public static let fifaWorldCup2018TokenNamePrefix = "FIFA WC2018"
//etherscan APIs
public static let mainnetEtherscanAPI = "https://api.etherscan.io/api?module=account&action=txlist&address="

@ -74,7 +74,7 @@ class TokensCoordinator: Coordinator {
private func refreshUponAssetDefinitionChanges() {
assetDefinitionStore.subscribe { [weak self] _ in
self?.storage.updateERC875TokensToLocalizedName()
self?.storage.fetchTokenNamesForNonFungibleTokensIfEmpty()
}
}

@ -68,7 +68,24 @@ class TokenObject: Object {
}
var title: String {
return name.isEmpty ? symbol : (name + " (" + symbol + ")")
let localizedName = XMLHandler(contract: contract).getName()
let compositeName: String
//TODO improve and remove the check for "N/A". Maybe a constant
if localizedName == "N/A" {
compositeName = name
} else {
if name.isEmpty {
compositeName = localizedName
} else {
compositeName = "\(name) \(localizedName)"
}
}
if compositeName.isEmpty {
return symbol
} else {
return "\(compositeName) (\(symbol))"
}
}
}

@ -114,7 +114,7 @@ class TokensDataStore {
self.scheduledTimerForPricesUpdate()
self.scheduledTimerForEthBalanceUpdate()
updateERC875TokensToLocalizedName()
fetchTokenNamesForNonFungibleTokensIfEmpty()
}
private func addEthToken() {
//Check if we have previos values.
@ -171,13 +171,7 @@ class TokensDataStore {
completion: @escaping (ResultResult<String, AnyError>.t) -> Void) {
let address = Address(string: addressString)
getNameCoordinator.getName(for: address!) { (result) in
let xmlName = XMLHandler(contract: address!.eip55String).getName()
if xmlName == "N/A" {
completion(result)
return
}
let fullName = try! result.dematerialize().description + " " + xmlName
completion(.success(fullName))
completion(result)
}
}
@ -527,12 +521,20 @@ class TokensDataStore {
}, selector: #selector(Operation.main), userInfo: nil, repeats: true)
}
public func updateERC875TokensToLocalizedName() {
public func fetchTokenNamesForNonFungibleTokensIfEmpty() {
assetDefinitionStore.forEachContractWithXML { contract in
if let localizedName = config.getContractLocalizedName(forContract: contract) {
if let storedToken = enabledObject.first(where: { $0.contract.sameContract(as: contract) }) {
//TODO multiple realm writes in a loop. Should we group them together?
updateTokenName(token: storedToken, to: localizedName)
let localizedName = XMLHandler(contract: contract).getName()
if localizedName != "N/A" {
if let storedToken = self.enabledObject.first(where: { $0.contract.sameContract(as: contract) }), storedToken.name.isEmpty {
getContractName(for: contract) { result in
switch result {
case .success(let name):
//TODO multiple realm writes in a loop. Should we group them together?
self.updateTokenName(token: storedToken, to: name)
case .failure:
break
}
}
}
}
}

Loading…
Cancel
Save