Remove grouping tokens while fetching tickers, move xdai to predefined ticker ids #4536

pull/4537/head
Krypto Pank 3 years ago
parent 595879d62c
commit 85b26bef89
  1. 28
      AlphaWallet/Core/CoinTicker/CoinTickersFetcher.swift
  2. 18
      AlphaWallet/Core/CoinTicker/Types/TickerIdFilter.swift
  3. 6
      AlphaWallet/Core/CoinTicker/Types/TokenMappedToTicker.swift
  4. 22
      AlphaWallet/Core/Coordinators/WalletBalance/WalletBalanceFetcher.swift
  5. 4
      AlphaWalletTests/Coordinators/InCoordinatorTests.swift

@ -8,12 +8,13 @@
import PromiseKit
import Moya
import Combine
import Foundation
protocol CoinTickersFetcherType: AnyObject {
var tickersUpdatedPublisher: AnyPublisher<Void, Never> { get }
func ticker(for addressAndPRCServer: AddressAndRPCServer) -> CoinTicker?
func fetchPrices(forTokens tokens: [TokenMappedToTicker]) -> Promise<Void>
func fetchPrices(forTokens tokens: [TokenMappedToTicker])
func fetchChartHistories(addressToRPCServerKey: AddressAndRPCServer, force: Bool, periods: [ChartHistoryPeriod]) -> Promise<[ChartHistory]>
}
@ -82,15 +83,14 @@ class CoinTickersFetcher: CoinTickersFetcherType {
return promise
}
func fetchPrices(forTokens tokens: [TokenMappedToTicker]) -> Promise<Void> {
let cache = self.cache
return firstly {
func fetchPrices(forTokens tokens: [TokenMappedToTicker]) {
firstly {
fetchTickers(forTokens: tokens)
}.get { tickers, tickerIds in
cache.tickers = tickers
cache.lastFetchedTickerIds = tickerIds
cache.lastFetchedDate = Date()
}.map { _ in }
}.done { [weak cache] tickers, tickerIds in
cache?.tickers = tickers
cache?.lastFetchedTickerIds = tickerIds
cache?.lastFetchedDate = Date()
}.cauterize()
}
func fetchChartHistories(addressToRPCServerKey addressAndPRCServer: AddressAndRPCServer, force: Bool, periods: [ChartHistoryPeriod]) -> Promise<[ChartHistory]> {
@ -154,15 +154,15 @@ class CoinTickersFetcher: CoinTickersFetcherType {
return firstly {
fetchSupportedTickerIds()
}.compactMap(on: CoinTickersFetcher.queue, { tickerIds -> [MappedCoinTickerId] in
let mappedTokensToCoinTickerIds = tokens.compactMap { tokenObject -> MappedCoinTickerId? in
let spamNeedle = AddressAndRPCServer(address: tokenObject.contractAddress, server: tokenObject.server)
let mappedTokensToCoinTickerIds = tokens.compactMap { token -> MappedCoinTickerId? in
let spamNeedle = AddressAndRPCServer(address: token.contractAddress, server: token.server)
if spamTokens.isSpamToken(spamNeedle) {
return nil
}
if let tickerId = tickerIds.first(where: { tickerIdFilter.matches(tokenObject: tokenObject, tickerId: $0) }) {
let tickerId = tokenObject.overridenCoinGeckoTickerId(tickerId: tickerId.id)
return MappedCoinTickerId(tickerId: tickerId, contractAddress: tokenObject.contractAddress, server: tokenObject.server)
if let tickerId = tickerIds.first(where: { tickerIdFilter.matches(token: token, tickerId: $0) }) {
let tickerId = token.overridenCoinGeckoTickerId(tickerId: tickerId.id)
return MappedCoinTickerId(tickerId: tickerId, contractAddress: token.contractAddress, server: token.server)
} else {
return nil
}

@ -12,7 +12,7 @@ struct TickerIdFilter {
//https://polygonscan.com/address/0x0000000000000000000000000000000000001010
static private let polygonMaticContract = AlphaWallet.Address(string: "0x0000000000000000000000000000000000001010")!
func matches(tokenObject: TokenMappedToTicker, tickerId: TickerId) -> Bool {
func matches(token: TokenMappedToTicker, tickerId: TickerId) -> Bool {
//We just filter out those that we don't think are supported by the API. One problem this helps to alleviate is in the API output, certain tickers have a non-empty platform yet the platform list might not be complete, eg. Ether on Ethereum mainnet:
//{
// "symbol" : "eth",
@ -26,8 +26,8 @@ struct TickerIdFilter {
//This means we can only match solely by symbol, ignoring platform matches. But this means it's easy to match the wrong ticker (by symbol only). Hence, we at least remove those chains we don't think are supported
//NOTE maybe its need to handle values like: `"0x270DE58F54649608D316fAa795a9941b355A2Bd0/token-transfers"`
guard isServerSupported(tokenObject.server) else { return false }
if let (_, maybeContractValue) = tickerId.platforms.first(where: { platformMatches($0.key, server: tokenObject.server) }) {
guard isServerSupported(token.server) else { return false }
if let (_, maybeContractValue) = tickerId.platforms.first(where: { platformMatches($0.key, server: token.server) }) {
func maybeAddressValue(from str: String) -> AlphaWallet.Address? {
let rawValue = str.trimmed
if rawValue.isEmpty {
@ -45,18 +45,16 @@ struct TickerIdFilter {
}
if contract.sameContract(as: Constants.nullAddress) {
return tickerId.symbol.localizedLowercase == tokenObject.symbol.localizedLowercase
} else if contract.sameContract(as: tokenObject.contractAddress) {
return tickerId.symbol.localizedLowercase == token.symbol.localizedLowercase
} else if contract.sameContract(as: token.contractAddress) {
return true
} else if tokenObject.server == .polygon && tokenObject.contractAddress == Constants.nativeCryptoAddressInDatabase && contract.sameContract(as: Self.polygonMaticContract) {
return true
} else if tokenObject.server == .klaytnCypress && tokenObject.contractAddress == Constants.nativeCryptoAddressInDatabase {
} else if token.server == .polygon && token.contractAddress == Constants.nativeCryptoAddressInDatabase && contract.sameContract(as: Self.polygonMaticContract) {
return true
} else {
return tokenObject.canPassFiltering
return token.canPassFiltering
}
} else {
return tickerId.symbol.localizedLowercase == tokenObject.symbol.localizedLowercase && tickerId.name.localizedLowercase == tokenObject.name.localizedLowercase
return tickerId.symbol.localizedLowercase == token.symbol.localizedLowercase && tickerId.name.localizedLowercase == token.name.localizedLowercase
}
}

@ -27,6 +27,10 @@ struct TokenMappedToTicker: Hashable {
return true
} else if server == .binance_smart_chain && contractAddress == Constants.nativeCryptoAddressInDatabase {
return true
} else if server == .klaytnCypress && contractAddress == Constants.nativeCryptoAddressInDatabase {
return true
} else if server == .xDai && contractAddress == Constants.nativeCryptoAddressInDatabase {
return true
} else {
return false
}
@ -41,6 +45,8 @@ struct TokenMappedToTicker: Hashable {
return "binancecoin"
} else if server == .klaytnCypress && contractAddress == Constants.nativeCryptoAddressInDatabase {
return "klay-token"
} else if server == .xDai && contractAddress == Constants.nativeCryptoAddressInDatabase {
return "gnosis"
} else {
return tickerId
}

@ -97,27 +97,9 @@ class WalletBalanceFetcher: NSObject, WalletBalanceFetcherType {
.receive(on: queue)
.sink { [weak coinTickersFetcher] tokens in
let tokens = (tokens + Self.nativeCryptoForAllChains).filter { !$0.server.isTestnet }
let uniqueTokens = Set(tokens)
let uniqueTokens = Set(tokens).map { TokenMappedToTicker(token: $0) }
var store = ServerDictionary<[TokenMappedToTicker]>()
for each in uniqueTokens {
var array: [TokenMappedToTicker]
if let value = store[safe: each.server] {
array = value
} else {
array = .init()
}
array.append(TokenMappedToTicker(token: each))
store[each.server] = array
}
coinTickersFetcher?.fetchPrices(forTokens: store.values.flatMap({ $0 }))
.done { _ in }
.cauterize()
coinTickersFetcher?.fetchPrices(forTokens: uniqueTokens)
}.store(in: &cancelable)
}

@ -276,8 +276,8 @@ import Combine
final class FakeCoinTickersFetcher: CoinTickersFetcherType {
func fetchPrices(forTokens tokens: [TokenMappedToTicker]) -> Promise<Void> {
return .value(())
func fetchPrices(forTokens tokens: [TokenMappedToTicker]) {
}
var tickersUpdatedPublisher: AnyPublisher<Void, Never> {

Loading…
Cancel
Save