Skip to content
This repository has been archived by the owner on Oct 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #367 from balancemymoney/bug/miota
Browse files Browse the repository at this point in the history
Bug/miota
  • Loading branch information
Ben Baron authored Dec 14, 2017
2 parents 82294f1 + 1f6414a commit 0595f4c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 50 deletions.
56 changes: 29 additions & 27 deletions Balance/Shared/Data Model/Other Models/Currency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,21 @@ public enum Currency: Equatable, Hashable {

// Known/popular crypto currencies
public enum CryptoCurrency: String {
case btc = "BTC"
case xbt = "XBT" // Alternate symbol for BTC
case bch = "BCH"
case eth = "ETH"
case ltc = "LTC"
case zec = "ZEC"
case dash = "DASH"
case xrp = "XRP"
case xmr = "XMR"
case gnt = "GNT"
case zrx = "ZRX"
case usdt = "USDT"
case iot = "IOT" // Alternate symbol for IOTA
case btc = "BTC"
case xbt = "XBT" // Alternate symbol for BTC
case bch = "BCH"
case eth = "ETH"
case ltc = "LTC"
case zec = "ZEC"
case dash = "DASH"
case xrp = "XRP"
case xmr = "XMR"
case gnt = "GNT"
case zrx = "ZRX"
case usdt = "USDT"
case miota = "MIOTA"
case iota = "IOTA" // Alternate symbol for MIOTA
case iot = "IOT" // Alternate symbol for MIOTA

public var code: String {
return rawValue
Expand All @@ -128,18 +130,18 @@ public enum CryptoCurrency: String {

public var name: String {
switch self {
case .btc, .xbt: return "Bitcoin"
case .bch: return "Bitcoin Cash"
case .eth: return "Ether"
case .ltc: return "Litecoin"
case .zec: return "Zcash"
case .dash: return "Dash"
case .xrp: return "Ripple"
case .xmr: return "Monero"
case .gnt: return "Golem"
case .zrx: return "0x"
case .usdt: return "Tether"
case .iot: return "IOTA"
case .btc, .xbt: return "Bitcoin"
case .bch: return "Bitcoin Cash"
case .eth: return "Ether"
case .ltc: return "Litecoin"
case .zec: return "Zcash"
case .dash: return "Dash"
case .xrp: return "Ripple"
case .xmr: return "Monero"
case .gnt: return "Golem"
case .zrx: return "0x"
case .usdt: return "Tether"
case .miota, .iota, .iot: return "IOTA"
}
}

Expand All @@ -153,8 +155,8 @@ public enum CryptoCurrency: String {
return true
}

// Connect alternate IOTA sumbol IOT
if (lhs.code == "IOTA" || lhs.code == "IOT") && (rhs.code == "IOTA" || rhs.code == "IOT") {
// Connect alternate MIOTA symbols IOT and IOTA
if (lhs.code == "IOTA" || lhs.code == "IOT" || lhs.code == "MIOTA") && (rhs.code == "IOTA" || rhs.code == "IOT" || rhs.code == "MIOTA") {
return true
}

Expand Down
54 changes: 31 additions & 23 deletions Balance/Shared/Data Model/Singleton/CurrentExchangeRates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,27 @@ class CurrentExchangeRates {
}

func convertTicker(amount: Double, from: Currency, to: Currency) -> Double? {
// TODO: Fix MIOTA conversions properly, no time for 1.0 release
var fromCode = from.code
switch from {
case .crypto(let crypto):
if crypto == .miota {
fromCode = "IOT"
}
default:
break
}

if from == to { return 1 }
if let exchangeRate = cachedRates.get(valueForKey: "\(from.code)-\(to.code)") {
return amount * exchangeRate.rate
} else {
for masterCurrency in ExchangeRateSource.mainCurrencies {
if let fromRate = cachedRates.get(valueForKey: "\(from.code)-\(masterCurrency.code)"), let toRate = cachedRates.get(valueForKey: "\(masterCurrency.code)-\(to.code)") {
if let fromRate = cachedRates.get(valueForKey: "\(fromCode)-\(masterCurrency.code)"), let toRate = cachedRates.get(valueForKey: "\(masterCurrency.code)-\(to.code)") {
return amount * fromRate.rate * toRate.rate
}
}
guard let rateBTC = cachedRates.get(valueForKey: "\(from.code)-\(Currency.btc.code)") else {
guard let rateBTC = cachedRates.get(valueForKey: "\(fromCode)-\(Currency.btc.code)") else {
return nil
}
if let finalRate = cachedRates.get(valueForKey: "\(Currency.usd.code)-\(to.code)"), let dollarRate = cachedRates.get(valueForKey: "\(Currency.btc.code)-\(Currency.usd.code)") {
Expand Down Expand Up @@ -145,25 +155,6 @@ class CurrentExchangeRates {
self.persist(data: data)
NotificationCenter.postOnMainThread(name: Notifications.exchangeRatesUpdated)
}

let tempCachedRates = SimpleCache<String, Rate>()
let allRates = self.cache.getAll().flatMap({$0.value})

//create hash of exchanges averaging all exchanges rates
for exchangeKey in self.cache.getAll().keys {
for exchangeRate in self.cache.get(valueForKey: exchangeKey)! {
if tempCachedRates.get(valueForKey: "\(exchangeRate.from.code)-\(exchangeRate.to.code)") != nil { continue }
let sameRates = allRates.filter({$0.from == exchangeRate.from && $0.to == exchangeRate.to})
guard let averageRate = self.average(rates: sameRates) else { continue }

let rate = Rate(from: exchangeRate.from, to: exchangeRate.to, rate: averageRate)
tempCachedRates.set(value: rate, forKey: rate.key)

let opositeRate = Rate(from: exchangeRate.to, to: exchangeRate.from, rate: 1/exchangeRate.rate)
tempCachedRates.set(value: opositeRate, forKey: opositeRate.key)
}
}
self.cachedRates.replaceAll(values: tempCachedRates.getAll())
}
task.resume()
}
Expand Down Expand Up @@ -209,8 +200,25 @@ class CurrentExchangeRates {
self.cache.set(value: exchangeRates, forKey: source)
}
}

//set cacheRates

let tempCachedRates = SimpleCache<String, Rate>()
let allRates = self.cache.getAll().flatMap({$0.value})

//create hash of exchanges averaging all exchanges rates
for exchangeKey in self.cache.getAll().keys {
for exchangeRate in self.cache.get(valueForKey: exchangeKey)! {
if tempCachedRates.get(valueForKey: "\(exchangeRate.from.code)-\(exchangeRate.to.code)") != nil { continue }
let sameRates = allRates.filter({$0.from == exchangeRate.from && $0.to == exchangeRate.to})
guard let averageRate = self.average(rates: sameRates) else { continue }

let rate = Rate(from: exchangeRate.from, to: exchangeRate.to, rate: averageRate)
tempCachedRates.set(value: rate, forKey: rate.key)

let opositeRate = Rate(from: exchangeRate.to, to: exchangeRate.from, rate: 1/exchangeRate.rate)
tempCachedRates.set(value: opositeRate, forKey: opositeRate.key)
}
}
self.cachedRates.replaceAll(values: tempCachedRates.getAll())

return true
}
Expand Down

0 comments on commit 0595f4c

Please sign in to comment.