[Refactor] Move base urls for AlphaWalletService to Constants #4353

pull/4354/head
Krypto Pank 3 years ago
parent e8548d7fc2
commit be1ba553b9
  1. 5
      AlphaWallet/Core/BuyToken/Ramp/Ramp.swift
  2. 6
      AlphaWallet/Core/CoinTicker/CoinTickersFetcher.swift
  3. 5
      AlphaWallet/Core/SwapToken/Oneinch/Oneinch.swift
  4. 70
      AlphaWallet/EtherClient/TrustClient/AlphaWalletService.swift
  5. 6
      AlphaWallet/Settings/Types/Config.swift
  6. 16
      AlphaWallet/Settings/Types/Constants.swift
  7. 2
      AlphaWallet/Transactions/EtherscanSingleChainTransactionProvider.swift

@ -66,17 +66,16 @@ class Ramp: TokenActionsProvider, BuyTokenURLProviderType {
private static var assets: [Asset] = []
func fetchSupportedTokens() {
let config = Config()
let provider = AlphaWalletProviderFactory.makeProvider()
provider.request(.rampAssets(config: config), callbackQueue: queue).map(on: queue, { response -> RampAssetsResponse in
provider.request(.rampAssets, callbackQueue: queue).map(on: queue, { response -> RampAssetsResponse in
try JSONDecoder().decode(RampAssetsResponse.self, from: response.data)
}).map(on: queue, { data -> [Asset] in
return data.assets
}).done(on: queue, { response in
Self.assets = response
}).catch(on: queue, { error in
let service = AlphaWalletService.rampAssets(config: config)
let service = AlphaWalletService.rampAssets
let url = service.baseURL.appendingPathComponent(service.path)
RemoteLogger.instance.logRpcOrOtherWebError("Ramp error | \(error)", url: url.absoluteString)
})

@ -68,7 +68,7 @@ class CoinTickersFetcher: CoinTickersFetcherType {
if let promise = fetchSupportedTickerIdsPromise { return promise }
let promise: Promise<[TickerId]> = firstly {
provider.request(.tokensThatHasPrices(config: config))
provider.request(.tokensThatHasPrices)
}.map(on: CoinTickersFetcher.queue, { response -> [TickerId] in
return try response.map([TickerId].self, using: JSONDecoder())
}).recover { _ -> Promise<[TickerId]> in
@ -110,7 +110,7 @@ class CoinTickersFetcher: CoinTickersFetcherType {
private func fetchChartHistory(period: ChartHistoryPeriod, ticker: CoinTicker) -> Promise<ChartHistory> {
firstly {
provider.request(.priceHistoryOfToken(config: config, id: ticker.id, currency: Constants.Currency.usd, days: period.rawValue))
provider.request(.priceHistoryOfToken(id: ticker.id, currency: Constants.Currency.usd, days: period.rawValue))
}.map(on: CoinTickersFetcher.queue, { response -> ChartHistory in
try response.map(ChartHistory.self, using: JSONDecoder())
}).recover(on: CoinTickersFetcher.queue, { _ -> Promise<ChartHistory> in
@ -208,7 +208,7 @@ class CoinTickersFetcher: CoinTickersFetcherType {
private static func fetchPricesPage(provider: MoyaProvider<AlphaWalletService>, config: Config, ids: String, mappedCoinTickerIds: [MappedCoinTickerId], tickerIds: [String], page: Int, shouldRetry: Bool) -> Promise<[AddressAndRPCServer: CoinTicker]> {
firstly {
provider.request(.pricesOfTokens(config: config, ids: ids, currency: Constants.Currency.usd, page: page))
provider.request(.pricesOfTokens(ids: ids, currency: Constants.Currency.usd, page: page))
}.map(on: CoinTickersFetcher.queue, { response -> [AddressAndRPCServer: CoinTicker] in
let tickers = try response.map([CoinTicker].self, using: JSONDecoder())
var resultTickers: [AddressAndRPCServer: CoinTicker] = [:]

@ -75,10 +75,9 @@ class Oneinch: TokenActionsProvider, SwapTokenURLProviderType {
}
func fetchSupportedTokens() {
let config = Config()
let provider = AlphaWalletProviderFactory.makeProvider()
provider.request(.oneInchTokens(config: config), callbackQueue: queue).map(on: queue, { response -> [String: Oneinch.ERC20Token] in
provider.request(.oneInchTokens, callbackQueue: queue).map(on: queue, { response -> [String: Oneinch.ERC20Token] in
try JSONDecoder().decode(ApiResponsePayload.self, from: response.data).tokens
}).map(on: queue, { data -> [Oneinch.ERC20Token] in
data.map { $0.value }
@ -87,7 +86,7 @@ class Oneinch: TokenActionsProvider, SwapTokenURLProviderType {
self.availableTokens[token.address] = token
}
}).catch(on: queue, { error in
let service = AlphaWalletService.oneInchTokens(config: config)
let service = AlphaWalletService.oneInchTokens
let url = service.baseURL.appendingPathComponent(service.path)
RemoteLogger.instance.logRpcOrOtherWebError("Oneinch error | \(error)", url: url.absoluteString)
})

@ -4,13 +4,13 @@ import Foundation
import Moya
enum AlphaWalletService {
case tokensThatHasPrices(config: Config)
case pricesOfTokens(config: Config, ids: String, currency: String, page: Int)
case getTransactions(config: Config, server: RPCServer, address: AlphaWallet.Address, startBlock: Int, endBlock: Int, sortOrder: SortOrder)
case oneInchTokens(config: Config)
case honeySwapTokens(config: Config)
case rampAssets(config: Config)
case priceHistoryOfToken(config: Config, id: String, currency: String, days: Int)
case tokensThatHasPrices
case pricesOfTokens(ids: String, currency: String, page: Int)
case getTransactions(server: RPCServer, address: AlphaWallet.Address, startBlock: Int, endBlock: Int, sortOrder: SortOrder)
case oneInchTokens
case honeySwapTokens
case rampAssets
case priceHistoryOfToken(id: String, currency: String, days: Int)
enum SortOrder: String {
case asc
@ -21,35 +21,32 @@ enum AlphaWalletService {
extension AlphaWalletService: TargetType {
var baseURL: URL {
switch self {
case .tokensThatHasPrices(let config):
return config.priceInfoEndpoints
case .pricesOfTokens(let config, _, _, _):
return config.priceInfoEndpoints
case .getTransactions(_, let server, _, _, _, _):
case .tokensThatHasPrices:
return Constants.Coingecko.baseUrl
case .pricesOfTokens:
return Constants.Coingecko.baseUrl
case .getTransactions(let server, _, _, _, _):
if let url = server.transactionInfoEndpoints {
return url
} else {
//HACK: we intentionally return an invalid, but non-nil URL because that's what the function needs to return. Keeps the code simple, yet still harmless
return URL(string: "x")!
}
case .oneInchTokens(let config):
return config.oneInch
case .honeySwapTokens(let config):
return config.honeySwapTokens
case .rampAssets(let config):
return config.rampAssets
case .priceHistoryOfToken(let config, _, _, _):
return config.priceInfoEndpoints
case .oneInchTokens:
return Constants.OneInch.exchangeUrl
case .honeySwapTokens:
return Constants.HoneySwap.exchangeUrl
case .rampAssets:
return Constants.Ramp.exchangeUrl
case .priceHistoryOfToken:
return Constants.Coingecko.baseUrl
}
}
var path: String {
switch self {
case .getTransactions(_, let server, _, _, _, _):
switch server {
case .main, .classic, .callisto, .kovan, .ropsten, .custom, .rinkeby, .poa, .sokol, .goerli, .xDai, .artis_sigma1, .artis_tau1, .binance_smart_chain, .binance_smart_chain_testnet, .heco, .heco_testnet, .fantom, .fantom_testnet, .avalanche, .avalanche_testnet, .polygon, .mumbai_testnet, .optimistic, .optimisticKovan, .cronosTestnet, .arbitrum, .arbitrumRinkeby, .palm, .palmTestnet:
return ""
}
case .getTransactions:
return ""
case .oneInchTokens:
return "/v3.0/1/tokens"
case .honeySwapTokens:
@ -60,7 +57,7 @@ extension AlphaWalletService: TargetType {
return "/api/v3/coins/list"
case .pricesOfTokens:
return "/api/v3/coins/markets"
case .priceHistoryOfToken(_, let id, _, _):
case .priceHistoryOfToken(let id, _, _):
return "/api/v3/coins/\(id)/market_chart"
}
}
@ -79,7 +76,7 @@ extension AlphaWalletService: TargetType {
var task: Task {
switch self {
case .getTransactions(_, let server, let address, let startBlock, let endBlock, let sortOrder):
case .getTransactions(let server, let address, let startBlock, let endBlock, let sortOrder):
var parameters: [String: Any] = [
"module": "account",
"action": "txlist",
@ -94,7 +91,7 @@ extension AlphaWalletService: TargetType {
//no-op
}
return .requestParameters(parameters: parameters, encoding: URLEncoding())
case .pricesOfTokens(_, let ids, let currency, let page):
case .pricesOfTokens(let ids, let currency, let page):
return .requestParameters(parameters: [
"vs_currency": currency,
"ids": ids,
@ -107,7 +104,7 @@ extension AlphaWalletService: TargetType {
return .requestPlain
case .tokensThatHasPrices:
return .requestParameters(parameters: ["include_platform": "true"], encoding: URLEncoding())
case .priceHistoryOfToken(_, _, let currency, let days):
case .priceHistoryOfToken(_, let currency, let days):
return .requestParameters(parameters: [
"vs_currency": currency,
"days": days
@ -121,15 +118,12 @@ extension AlphaWalletService: TargetType {
var headers: [String: String]? {
switch self {
case .getTransactions(_, let server, _, _, _, _):
switch server {
case .main, .classic, .callisto, .kovan, .ropsten, .custom, .rinkeby, .poa, .sokol, .goerli, .xDai, .artis_sigma1, .artis_tau1, .binance_smart_chain, .binance_smart_chain_testnet, .heco, .heco_testnet, .fantom, .fantom_testnet, .avalanche, .avalanche_testnet, .polygon, .mumbai_testnet, .optimistic, .optimisticKovan, .cronosTestnet, .arbitrum, .arbitrumRinkeby, .palm, .palmTestnet:
return [
"Content-type": "application/json",
"client": Bundle.main.bundleIdentifier ?? "",
"client-build": Bundle.main.buildNumber ?? "",
]
}
case .getTransactions(let server, _, _, _, _):
return [
"Content-type": "application/json",
"client": Bundle.main.bundleIdentifier ?? "",
"client-build": Bundle.main.buildNumber ?? "",
]
case .priceHistoryOfToken, .tokensThatHasPrices, .pricesOfTokens:
return [
"Content-type": "application/json",

@ -273,8 +273,6 @@ struct Config {
self.defaults = defaults
}
let priceInfoEndpoints = URL(string: "https://api.coingecko.com")!
var oldWalletAddressesAlreadyPromptedForBackUp: [String] {
//We hard code the key here because it's used for migrating off the old value, there should be no reason why this key will change in the next line
//There *is* a trailing space in the key
@ -296,10 +294,6 @@ struct Config {
defaults.setValue(addresses, forKey: Keys.walletAddressesAlreadyPromptedForBackUp)
}
let oneInch = URL(string: "https://api.1inch.exchange")!
let honeySwapTokens = URL(string: "https://tokens.honeyswap.org/")!
let rampAssets = URL(string: "https://api-instant.ramp.network")!
func anyEnabledServer() -> RPCServer {
let servers = enabledServers
if servers.contains(.main) {

@ -189,6 +189,22 @@ public struct Constants {
static let unreadCountEndpoint = URL(string: "https://blockscan-chat-proxy.herokuapp.com/blockscanchat/unreadcount")!
}
enum Coingecko {
static let baseUrl = URL(string: "https://api.coingecko.com")!
}
enum OneInch {
static let exchangeUrl = URL(string: "https://api.1inch.exchange")!
}
enum HoneySwap {
static let exchangeUrl = URL(string: "https://tokens.honeyswap.org/")!
}
enum Ramp {
static let exchangeUrl = URL(string: "https://api-instant.ramp.network")!
}
enum WalletConnect {
static let relayURL = URL(string: "https://relay.walletconnect.com")!
static let server = "AlphaWallet"

@ -318,7 +318,7 @@ extension EtherscanSingleChainTransactionProvider.functional {
}
static func fetchTransactions(startBlock: Int, endBlock: Int = 999_999_999, sortOrder: AlphaWalletService.SortOrder, session: WalletSession, alphaWalletProvider: MoyaProvider<AlphaWalletService>, tokensDataStore: TokensDataStore, queue: DispatchQueue) -> Promise<[TransactionInstance]> {
let target: AlphaWalletService = .getTransactions(config: session.config, server: session.server, address: session.account.address, startBlock: startBlock, endBlock: endBlock, sortOrder: sortOrder)
let target: AlphaWalletService = .getTransactions(server: session.server, address: session.account.address, startBlock: startBlock, endBlock: endBlock, sortOrder: sortOrder)
return firstly {
alphaWalletProvider.request(target)
}.then(on: queue) { response -> Promise<[TransactionInstance]> in

Loading…
Cancel
Save