From 87ecd419222af9bf556c7826347192b3ac80d524 Mon Sep 17 00:00:00 2001 From: Krypto Pank Date: Tue, 8 Mar 2022 10:38:25 +0200 Subject: [PATCH] Fetching prices for chains: Avax, Fantom, bnb #3888 --- .../Core/CoinTicker/CoinTickersFetcher.swift | 40 ++++++++++++++----- .../WalletBalanceCoordinator.swift | 3 +- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/AlphaWallet/Core/CoinTicker/CoinTickersFetcher.swift b/AlphaWallet/Core/CoinTicker/CoinTickersFetcher.swift index 31d79772d..e7115edd9 100644 --- a/AlphaWallet/Core/CoinTicker/CoinTickersFetcher.swift +++ b/AlphaWallet/Core/CoinTicker/CoinTickersFetcher.swift @@ -14,19 +14,36 @@ struct TokenMappedToTicker: Hashable { let contractAddress: AlphaWallet.Address let server: RPCServer - init(tokenObject: TokenObject) { - symbol = tokenObject.symbol - name = tokenObject.name - contractAddress = tokenObject.contractAddress - server = tokenObject.server - } - init(token: Activity.AssignedToken) { symbol = token.symbol name = token.name contractAddress = token.contractAddress server = token.server } + + var canPassFiltering: Bool { + if server == .avalanche && contractAddress == Constants.nativeCryptoAddressInDatabase { + return true + } else if server == .fantom && contractAddress == Constants.nativeCryptoAddressInDatabase { + return true + } else if server == .binance_smart_chain && contractAddress == Constants.nativeCryptoAddressInDatabase { + return true + } else { + return false + } + } + + func overridenCoinGeckoTickerId(tickerId: String) -> String { + if server == .avalanche && contractAddress == Constants.nativeCryptoAddressInDatabase { + return "avalanche-2" + } else if server == .fantom && contractAddress == Constants.nativeCryptoAddressInDatabase { + return "fantom" + } else if server == .binance_smart_chain && contractAddress == Constants.nativeCryptoAddressInDatabase { + return "binancecoin" + } else { + return tickerId + } + } } protocol CoinTickersFetcherType { @@ -181,7 +198,8 @@ class CoinTickersFetcher: CoinTickersFetcherType { }.compactMap(on: CoinTickersFetcher.queue, { tickers -> [MappedCoinTickerId] in let mappedTokensToCoinTickerIds = tokens.compactMap { tokenObject -> MappedCoinTickerId? in if let ticker = tickers.first(where: { $0.matches(tokenObject: tokenObject) }) { - return MappedCoinTickerId(tickerId: ticker.id, contractAddress: tokenObject.contractAddress, server: tokenObject.server) + let tickerId = tokenObject.overridenCoinGeckoTickerId(tickerId: ticker.id) + return MappedCoinTickerId(tickerId: tickerId, contractAddress: tokenObject.contractAddress, server: tokenObject.server) } else { return nil } @@ -303,7 +321,7 @@ fileprivate struct Ticker: Codable { } else if tokenObject.server == .polygon && tokenObject.contractAddress == Constants.nativeCryptoAddressInDatabase && contract.sameContract(as: Self.polygonMaticContract) { return true } else { - return false + return tokenObject.canPassFiltering } } else { return symbol.localizedLowercase == tokenObject.symbol.localizedLowercase && name.localizedLowercase == tokenObject.name.localizedLowercase @@ -349,7 +367,9 @@ fileprivate struct Ticker: Codable { case .avalanche: return true case .polygon: return true case .arbitrum: return true - case .poa, .kovan, .sokol, .callisto, .goerli, .artis_sigma1, .artis_tau1, .binance_smart_chain_testnet, .ropsten, .rinkeby, .heco, .heco_testnet, .fantom, .fantom_testnet, .avalanche_testnet, .mumbai_testnet, .custom, .optimistic, .optimisticKovan, .cronosTestnet, .palm, .palmTestnet, .arbitrumRinkeby: + case .fantom: return true + case .palm: return true + case .poa, .kovan, .sokol, .callisto, .goerli, .artis_sigma1, .artis_tau1, .binance_smart_chain_testnet, .ropsten, .rinkeby, .heco, .heco_testnet, .fantom_testnet, .avalanche_testnet, .mumbai_testnet, .custom, .optimistic, .optimisticKovan, .cronosTestnet, .palmTestnet, .arbitrumRinkeby: return false } } diff --git a/AlphaWallet/Core/Coordinators/WalletBalance/WalletBalanceCoordinator.swift b/AlphaWallet/Core/Coordinators/WalletBalance/WalletBalanceCoordinator.swift index a10164a21..9a9b9ff4a 100644 --- a/AlphaWallet/Core/Coordinators/WalletBalance/WalletBalanceCoordinator.swift +++ b/AlphaWallet/Core/Coordinators/WalletBalance/WalletBalanceCoordinator.swift @@ -127,7 +127,8 @@ class WalletBalanceCoordinator: NSObject, WalletBalanceCoordinatorType { seal.fulfill(tokenObjects + Self.nativeCryptoForAllChains) } }.map(on: queue, { objects -> ServerDictionary<[TokenMappedToTicker]> in - let uniqueTokenObjectsOfAllWallets = Set(objects) + let tokenObjects = objects.filter { !$0.server.isTestnet } + let uniqueTokenObjectsOfAllWallets = Set(tokenObjects) var tokens = ServerDictionary<[TokenMappedToTicker]>()