mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2025-02-26 17:10:34 +01:00
218 lines
9.3 KiB
Swift
218 lines
9.3 KiB
Swift
//
|
|
// WidgetAPI.swift
|
|
// TodayExtension
|
|
//
|
|
// Created by Marcos Rodriguez on 11/2/19.
|
|
|
|
//
|
|
|
|
import Foundation
|
|
|
|
struct CurrencyError: LocalizedError {
|
|
var errorDescription: String = "Failed to parse response"
|
|
}
|
|
|
|
var numberFormatter: NumberFormatter {
|
|
let formatter = NumberFormatter()
|
|
formatter.numberStyle = .decimal
|
|
formatter.maximumFractionDigits = 0
|
|
formatter.locale = Locale.current
|
|
return formatter
|
|
}
|
|
|
|
class WidgetAPI {
|
|
static func fetchPrice(currency: String, completion: @escaping ((WidgetDataStore?, Error?) -> Void)) {
|
|
let currencyToFiatUnit = fiatUnit(currency: currency)
|
|
guard let source = currencyToFiatUnit?.source, let endPointKey = currencyToFiatUnit?.endPointKey else { return }
|
|
|
|
var urlString: String
|
|
switch source {
|
|
case "Yadio":
|
|
urlString = "https://api.yadio.io/json/\(endPointKey)"
|
|
case "YadioConvert":
|
|
urlString = "https://api.yadio.io/convert/1/BTC/\(endPointKey)"
|
|
case "Exir":
|
|
urlString = "https://api.exir.io/v1/ticker?symbol=btc-irt"
|
|
case "wazirx":
|
|
urlString = "https://api.wazirx.com/api/v2/tickers/btcinr"
|
|
case "Bitstamp":
|
|
urlString = "https://www.bitstamp.net/api/v2/ticker/btc\(endPointKey.lowercased())"
|
|
case "Coinbase":
|
|
urlString = "https://api.coinbase.com/v2/prices/BTC-\(endPointKey.uppercased())/buy"
|
|
case "CoinGecko":
|
|
urlString = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=\(endPointKey.lowercased())"
|
|
case "BNR":
|
|
urlString = "https://www.bnr.ro/nbrfxrates.xml"
|
|
default:
|
|
urlString = "https://api.coindesk.com/v1/bpi/currentprice/\(endPointKey).json"
|
|
}
|
|
|
|
guard let url = URL(string:urlString) else { return }
|
|
|
|
if source == "BNR" {
|
|
URLSession.shared.dataTask(with: url) { (data, response, error) in
|
|
if let error = error {
|
|
print("Error fetching data: \(error.localizedDescription)")
|
|
completion(nil, error)
|
|
return
|
|
}
|
|
|
|
guard let data = data else {
|
|
print("No data received")
|
|
completion(nil, nil)
|
|
return
|
|
}
|
|
|
|
// Parse XML data for USD to RON rate
|
|
let parser = XMLParser(data: data)
|
|
let delegate = BNRXMLParserDelegate()
|
|
parser.delegate = delegate
|
|
if parser.parse(), let usdToRonRate = delegate.usdRate {
|
|
// Fetch BTC to USD rate using CoinGecko
|
|
let coinGeckoUrl = URL(string: "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd")!
|
|
URLSession.shared.dataTask(with: coinGeckoUrl) { (data, _, error) in
|
|
guard let data = data, error == nil else {
|
|
completion(nil, error ?? CurrencyError())
|
|
return
|
|
}
|
|
|
|
do {
|
|
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
|
|
let bitcoinDict = json["bitcoin"] as? [String: Double],
|
|
let btcToUsdRate = bitcoinDict["usd"] {
|
|
let btcToRonRate = btcToUsdRate * usdToRonRate
|
|
let lastUpdatedString = ISO8601DateFormatter().string(from: Date())
|
|
let latestRateDataStore = WidgetDataStore(rate: String(btcToRonRate), lastUpdate: lastUpdatedString, rateDouble: btcToRonRate)
|
|
completion(latestRateDataStore, nil)
|
|
} else {
|
|
completion(nil, CurrencyError())
|
|
}
|
|
} catch {
|
|
completion(nil, error)
|
|
}
|
|
}.resume()
|
|
} else {
|
|
print("Error parsing XML")
|
|
completion(nil, CurrencyError())
|
|
}
|
|
}.resume()
|
|
} else {
|
|
URLSession.shared.dataTask(with: url) { (data, response, error) in
|
|
guard let dataResponse = data,
|
|
let json = (try? JSONSerialization.jsonObject(with: dataResponse, options: .mutableContainers) as? Dictionary<String, Any>),
|
|
error == nil
|
|
else {
|
|
print(error?.localizedDescription ?? "Response Error")
|
|
completion(nil, error)
|
|
return
|
|
}
|
|
|
|
var latestRateDataStore: WidgetDataStore?
|
|
switch source {
|
|
case "Yadio":
|
|
guard let rateDict = json[endPointKey] as? [String: Any],
|
|
let rateDouble = rateDict["price"] as? Double,
|
|
let lastUpdated = json["timestamp"] as? Int
|
|
else { break }
|
|
let unix = Double(lastUpdated / 1_000)
|
|
let lastUpdatedString = ISO8601DateFormatter().string(from: Date(timeIntervalSince1970: unix))
|
|
latestRateDataStore = WidgetDataStore(rate: String(rateDouble), lastUpdate: lastUpdatedString, rateDouble: rateDouble)
|
|
case "CoinGecko":
|
|
guard let rateDict = json["bitcoin"] as? [String: Any],
|
|
let rateDouble = rateDict[endPointKey.lowercased()] as? Double
|
|
else { break }
|
|
let lastUpdatedString = ISO8601DateFormatter().string(from: Date())
|
|
latestRateDataStore = WidgetDataStore(rate: String(rateDouble), lastUpdate: lastUpdatedString, rateDouble: rateDouble)
|
|
case "YadioConvert":
|
|
guard let rateDict = json as? [String: Any],
|
|
let rateDouble = rateDict["rate"] as? Double,
|
|
let lastUpdated = json["timestamp"] as? Int
|
|
else { break }
|
|
let unix = Double(lastUpdated / 1_000)
|
|
let lastUpdatedString = ISO8601DateFormatter().string(from: Date(timeIntervalSince1970: unix))
|
|
latestRateDataStore = WidgetDataStore(rate: String(rateDouble), lastUpdate: lastUpdatedString, rateDouble: rateDouble)
|
|
case "Exir":
|
|
guard let rateDouble = json["last"] as? Double else { break }
|
|
let rateString = String(rateDouble)
|
|
let lastUpdatedString = ISO8601DateFormatter().string(from: Date())
|
|
latestRateDataStore = WidgetDataStore(rate: rateString, lastUpdate: lastUpdatedString, rateDouble: rateDouble)
|
|
case "Bitstamp":
|
|
guard let rateString = json["last"] as? String, let rateDouble = Double(rateString) else { break }
|
|
let lastUpdatedString = ISO8601DateFormatter().string(from: Date())
|
|
latestRateDataStore = WidgetDataStore(rate: rateString, lastUpdate: lastUpdatedString, rateDouble: rateDouble)
|
|
case "wazirx":
|
|
guard let tickerDict = json["ticker"] as? [String: Any],
|
|
let rateString = tickerDict["buy"] as? String,
|
|
let rateDouble = Double(rateString)
|
|
else { break }
|
|
let lastUpdatedString = ISO8601DateFormatter().string(from: Date())
|
|
latestRateDataStore = WidgetDataStore(rate: rateString, lastUpdate: lastUpdatedString, rateDouble: rateDouble)
|
|
case "Coinbase":
|
|
guard let data = json["data"] as? Dictionary<String, Any>,
|
|
let rateString = data["amount"] as? String,
|
|
let rateDouble = Double(rateString)
|
|
else { break }
|
|
let lastUpdatedString = ISO8601DateFormatter().string(from: Date())
|
|
latestRateDataStore = WidgetDataStore(rate: rateString, lastUpdate: lastUpdatedString, rateDouble: rateDouble)
|
|
default:
|
|
guard let bpi = json["bpi"] as? Dictionary<String, Any>,
|
|
let preferredCurrency = bpi[endPointKey] as? Dictionary<String, Any>,
|
|
let rateString = preferredCurrency["rate"] as? String,
|
|
let rateDouble = preferredCurrency["rate_float"] as? Double,
|
|
let time = json["time"] as? Dictionary<String, Any>,
|
|
let lastUpdatedString = time["updatedISO"] as? String
|
|
else { break }
|
|
latestRateDataStore = WidgetDataStore(rate: rateString, lastUpdate: lastUpdatedString, rateDouble: rateDouble)
|
|
}
|
|
|
|
if (latestRateDataStore == nil) {
|
|
completion(nil, CurrencyError())
|
|
return
|
|
}
|
|
|
|
completion(latestRateDataStore, nil)
|
|
}.resume()
|
|
}
|
|
}
|
|
|
|
static func getUserPreferredCurrency() -> String {
|
|
|
|
guard let userDefaults = UserDefaults(suiteName: UserDefaultsGroupKey.GroupName.rawValue),
|
|
let preferredCurrency = userDefaults.string(forKey: "preferredCurrency")
|
|
else {
|
|
return "USD"
|
|
}
|
|
|
|
if preferredCurrency != WidgetAPI.getLastSelectedCurrency() {
|
|
UserDefaults.standard.removeObject(forKey: WidgetData.WidgetCachedDataStoreKey)
|
|
UserDefaults.standard.removeObject(forKey: WidgetData.WidgetDataStoreKey)
|
|
UserDefaults.standard.synchronize()
|
|
}
|
|
|
|
return preferredCurrency
|
|
}
|
|
|
|
static func getUserPreferredCurrencyLocale() -> String {
|
|
guard let userDefaults = UserDefaults(suiteName: UserDefaultsGroupKey.GroupName.rawValue),
|
|
let preferredCurrency = userDefaults.string(forKey: "preferredCurrencyLocale")
|
|
else {
|
|
return "en_US"
|
|
}
|
|
return preferredCurrency
|
|
}
|
|
|
|
static func getLastSelectedCurrency() -> String {
|
|
guard let userDefaults = UserDefaults(suiteName: UserDefaultsGroupKey.GroupName.rawValue), let dataStore = userDefaults.string(forKey: "currency") else {
|
|
return "USD"
|
|
}
|
|
|
|
return dataStore
|
|
}
|
|
|
|
static func saveNewSelectedCurrency() {
|
|
UserDefaults.standard.setValue(WidgetAPI.getUserPreferredCurrency(), forKey: "currency")
|
|
}
|
|
|
|
}
|
|
|
|
|