Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Swift v4 #9

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cartfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github "danthorpe/Money" >= 1.5.1
github "antitypical/Result" >= 1.0.0
github "SwiftyJSON/SwiftyJSON" >= 2.3.0
#github "danthorpe/Money" "feature/MNY-79_updates_for_xcode_9"
github "Mandelkind/Money" "decimals"
github "antitypical/Result" ~> 3.2
github "SwiftyJSON/SwiftyJSON" ~> 4.0
9 changes: 4 additions & 5 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
github "venmo/DVR" "v0.2.1"
github "antitypical/Result" "1.0.2"
github "SwiftyJSON/SwiftyJSON" "2.3.3"
github "danthorpe/ValueCoding" "1.3.0"
github "danthorpe/Money" "1.7.0"
github "Mandelkind/Money" "6a735daf416d0f0c575b547468d4d7026553dc06"
github "SwiftyJSON/SwiftyJSON" "4.0.0"
github "antitypical/Result" "3.2.4"
github "venmo/DVR" "v1.1.0"
65 changes: 22 additions & 43 deletions FX.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

226 changes: 113 additions & 113 deletions Sources/Shared/Bitcoin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,117 +24,117 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Foundation
import Result
import SwiftyJSON
import Money

// MARK - cex.io FX

/**
CEX.io Supported fiat currencies

CEX only supports USD, EUR and RUB.

- see: https://cex.io
*/
public protocol CEXSupportedFiatCurrencyType: ISOCurrencyType {

/**
CEX.io charge a percentage based commission with FX transactions.
- returns: a BankersDecimal representing the % commission.
*/
static var cex_commissionPercentage: BankersDecimal { get }
}

extension Currency.USD: CEXSupportedFiatCurrencyType {

/// - returns: the commission charged for USD transactions, a BankersDecimal
public static let cex_commissionPercentage: BankersDecimal = 0.2
}

extension Currency.EUR: CEXSupportedFiatCurrencyType {

/// - returns: the commission charged for EUR transactions, a BankersDecimal
public static let cex_commissionPercentage: BankersDecimal = 0.2
}

extension Currency.RUB: CEXSupportedFiatCurrencyType {

/// - returns: the commission charged for RUB transactions, a BankersDecimal
public static let cex_commissionPercentage: BankersDecimal = 0
}

struct _CEXBuy<Base: MoneyType where Base.Currency: CEXSupportedFiatCurrencyType>: CryptoCurrencyMarketTransactionType {
typealias BaseMoney = Base
typealias CounterMoney = BTC
typealias FiatCurrency = Base.Currency
static var transactionKind: CurrencyMarketTransactionKind { return .Buy }
}

struct _CEXSell<Counter: MoneyType where Counter.Currency: CEXSupportedFiatCurrencyType>: CryptoCurrencyMarketTransactionType {
typealias BaseMoney = BTC
typealias CounterMoney = Counter
typealias FiatCurrency = Counter.Currency
static var transactionKind: CurrencyMarketTransactionKind { return .Sell }
}

class _CEX<T: CryptoCurrencyMarketTransactionType where T.FiatCurrency: CEXSupportedFiatCurrencyType>: FXRemoteProvider<T.BaseMoney, T.CounterMoney>, FXRemoteProviderType {

static func name() -> String {
return "CEX.IO \(BaseMoney.Currency.code)\(CounterMoney.Currency.code)"
}

static func request() -> NSURLRequest {
let url = NSURL(string: "https://cex.io/api/convert/\(BTC.Currency.code)/\(T.FiatCurrency.code)")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
do {
let data = try JSON(["amnt": Double(1.0)]).rawData()
request.HTTPBody = data
} catch {
print("Caught error: \(error) accessing JSON data for `amnt`.")
}
return request
}

static func quoteFromNetworkResult(result: Result<(NSData?, NSURLResponse?), NSError>) -> Result<FXQuote, FXError> {
return result.analysis(

ifSuccess: { data, response in

guard let data = data else {
return Result(error: .NoData)
}

let json = JSON(data: data)

if json.isEmpty {
return Result(error: .InvalidData(data))
}

guard let rateLiteral = json["amnt"].double else {
return Result(error: .RateNotFound(name()))
}

let rate: BankersDecimal

switch T.transactionKind {
case .Buy:
rate = BankersDecimal(floatLiteral: rateLiteral).reciprocal
case .Sell:
rate = BankersDecimal(floatLiteral: rateLiteral)
}

return Result(value: FXQuote(rate: rate, percentage: T.FiatCurrency.cex_commissionPercentage))
},

ifFailure: { error in
return Result(error: .NetworkError(error))
})
}
}
//import Foundation
//import Result
//import SwiftyJSON
//import Money
//
//// MARK - cex.io FX
//
///**
// CEX.io Supported fiat currencies
//
// CEX only supports USD, EUR and RUB.
//
// - see: https://cex.io
//*/
//public protocol CEXSupportedFiatCurrencyType: ISOCurrencyType {
//
// /**
// CEX.io charge a percentage based commission with FX transactions.
// - returns: a Decimal representing the % commission.
// */
// static var cex_commissionPercentage: Decimal { get }
//}
//
//extension Currency.USD: CEXSupportedFiatCurrencyType {
//
// /// - returns: the commission charged for USD transactions, a Decimal
// public static let cex_commissionPercentage: Decimal = 0.2
//}
//
//extension Currency.EUR: CEXSupportedFiatCurrencyType {
//
// /// - returns: the commission charged for EUR transactions, a Decimal
// public static let cex_commissionPercentage: Decimal = 0.2
//}
//
//extension Currency.RUB: CEXSupportedFiatCurrencyType {
//
// /// - returns: the commission charged for RUB transactions, a Decimal
// public static let cex_commissionPercentage: Decimal = 0
//}
//
//struct _CEXBuy<Currency: ISOCurrencyProtocol where Currency: CEXSupportedFiatCurrencyType>: CryptoCurrencyMarketTransactionType {
// typealias BaseMoney = ISOMoney<Currency>
// typealias CounterMoney = BTC
// typealias FiatCurrency = Currency
// static var transactionKind: CurrencyMarketTransactionKind { return .Buy }
//}
//
//struct _CEXSell<CounterCurrency: ISOCurrencyProtocol where CounterCurrency: CEXSupportedFiatCurrencyType>: CryptoCurrencyMarketTransactionType {
// typealias BaseMoney = BTC
// typealias CounterMoney = ISOMoney<CounterCurrency>
// typealias FiatCurrency = CounterCurrency
// static var transactionKind: CurrencyMarketTransactionKind { return .Sell }
//}
//
//class _CEX<T: CryptoCurrencyMarketTransactionType where T.FiatCurrency: CEXSupportedFiatCurrencyType>: FXRemoteProvider<T.BaseMoney, T.CounterMoney>, FXRemoteProviderType {
//
// static func name() -> String {
// return "CEX.IO \(BaseMoney.Currency.code)\(CounterMoney.Currency.code)"
// }
//
// static func request() -> NSURLRequest {
// let url = NSURL(string: "https://cex.io/api/convert/\(BTC.Currency.code)/\(T.FiatCurrency.code)")
// let request = NSMutableURLRequest(URL: url!)
// request.HTTPMethod = "POST"
// request.setValue("application/json", forHTTPHeaderField: "Content-Type")
// do {
// let data = try JSON(["amnt": Double(1.0)]).rawData()
// request.HTTPBody = data
// } catch {
// print("Caught error: \(error) accessing JSON data for `amnt`.")
// }
// return request
// }
//
// static func quoteFromNetworkResult(result: Result<(NSData?, NSURLResponse?), NSError>) -> Result<FXQuote, FXError> {
// return result.analysis(
//
// ifSuccess: { data, response in
//
// guard let data = data else {
// return Result(error: .NoData)
// }
//
// let json = JSON(data: data)
//
// if json.isEmpty {
// return Result(error: .InvalidData(data))
// }
//
// guard let rateLiteral = json["amnt"].double else {
// return Result(error: .RateNotFound(name()))
// }
//
// let rate: Decimal
//
// switch T.transactionKind {
// case .Buy:
// rate = Decimal(floatLiteral: rateLiteral).reciprocal
// case .Sell:
// rate = Decimal(floatLiteral: rateLiteral)
// }
//
// return Result(value: FXQuote(rate: rate, percentage: T.FiatCurrency.cex_commissionPercentage))
// },
//
// ifFailure: { error in
// return Result(error: .NetworkError(error))
// })
// }
//}

/**
Represents the purchase of bitcoin using CEX.io.
Expand All @@ -150,7 +150,7 @@ class _CEX<T: CryptoCurrencyMarketTransactionType where T.FiatCurrency: CEXSuppo

The above sample represents buying US$1,000 worth of BTC using CEX.io.
*/
public final class CEXBuy<Base: MoneyType where Base.Currency: CEXSupportedFiatCurrencyType>: _CEX<_CEXBuy<Base>> { }
//public final class CEXBuy<Currency: ISOCurrencyProtocol>: _CEX<_CEXBuy<Base>> where Currency: CEXSupportedFiatCurrencyType { }

/**
Represents the sale of bitcoin using CEX.io.
Expand All @@ -166,4 +166,4 @@ public final class CEXBuy<Base: MoneyType where Base.Currency: CEXSupportedFiatC

The above sample represents selling 10 bitcoins for euros using CEX.io.
*/
public final class CEXSell<Counter: MoneyType where Counter.Currency: CEXSupportedFiatCurrencyType>: _CEX<_CEXSell<Counter>> { }
//public final class CEXSell<CounterCurrency: ISOCurrencyProtocol where CounterCurrency: CEXSupportedFiatCurrencyType>: _CEX<_CEXSell<Counter>> { }
Loading