Skip to content
This repository has been archived by the owner on Jul 2, 2018. It is now read-only.

Commit

Permalink
Merge branch 'release/1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
danthorpe committed Nov 8, 2015
2 parents e137751 + f1561a1 commit daeba22
Show file tree
Hide file tree
Showing 24 changed files with 1,363 additions and 530 deletions.
2 changes: 1 addition & 1 deletion .fastlane/Fastfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
before_all do |lane|
carthage(platform: "all")
carthage
end

platform :ios do
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 1.2.0
1. [[MNY-18](https://github.com/danthorpe/Money/pull/18)]: Adds Bitcoin currency types and support for % commission with FX.
* Creates `BTC` and `XBT` types.
* Refactors `FXQuote` into a struct (no longer subclass-able) but with a percentage commission property. Commission defaults to 0%.
* FX method `quote`, now returns `FXTransaction` as the value of the `Result`. This new value type composes the original base money, commission (in the same base money currency), the exchange rate, and the counter money. The type supports `ValueCoding`.
* A new FX provider, CEX.IO get support for buying and selling bitcoin using `USD`, `EUR` and `RUB`.

# 1.1.0
1. [[MNY-16](https://github.com/danthorpe/Money/pull/16)]: Grab bag of minor issues post 1.0 release.
* Cleans up some minor mistakes (spelling etc).
Expand Down
2 changes: 1 addition & 1 deletion Examples/Custom Money/Custom Money/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ViewController: UIViewController {

print("You have \(hearts) and \(bees)")

let total = Bank<Hearts,Bees>.fx(hearts) + bees
let total = Bank<Hearts,Bees>.fx(hearts).counter + bees

print("Exchanging your \(hearts) into \(Currency.Bee.symbol) via the bank gives you \(total) in total.")
}
Expand Down
4 changes: 2 additions & 2 deletions Examples/Custom Money/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- Money (1.0.0):
- Money (1.1.0):
- Result (= 0.6.0-beta.6)
- SwiftyJSON
- ValueCoding
Expand All @@ -15,7 +15,7 @@ EXTERNAL SOURCES:
:path: "../../"

SPEC CHECKSUMS:
Money: fdc3b1c89c626afe7ae0290742cd95d24bf6bb4b
Money: 9b12218fa90413e429f9f623f52619bb89791722
Result: dc390d0b58f9ec43fcd536f1ebdd130803cc6cbc
SwiftyJSON: 592b53bee5ef3dd9b3bebc6b9cb7ee35426ae8c3
ValueCoding: 54486fde2d7b1c2f1eb46de260b95340abed5bde
Expand Down
7 changes: 4 additions & 3 deletions Examples/Custom Money/Pods/Local Podspecs/Money.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Examples/Custom Money/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

598 changes: 309 additions & 289 deletions Examples/Custom Money/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Money.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Money"
s.version = "1.1.0"
s.version = "1.2.0"
s.summary = "Swift types for working with Money."
s.description = <<-DESC
Expand All @@ -21,7 +21,7 @@ Pod::Spec.new do |s|
s.osx.deployment_target = '10.10'
s.tvos.deployment_target = '9.0'
s.watchos.deployment_target = '2.0'
s.source_files = ['Money/*.swift', 'Money/Decimal/*.swift']
s.source_files = ['Money/*.swift', 'Money/Decimal/*.swift', 'Money/FX/*.swift']

s.dependency 'ValueCoding'
s.dependency 'Result', '0.6.0-beta.6'
Expand Down
102 changes: 90 additions & 12 deletions Money.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Money/Currency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,8 @@ public struct Currency {
}


// MARK: - Crypto Currencies

public protocol CryptoCurrencyType: CurrencyType { }


1 change: 0 additions & 1 deletion Money/Decimal/Decimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ public final class _DecimalCoder<Behavior: DecimalNumberBehaviorType>: NSObject,
}



// TODO: - Move these into DecimalNumberType

extension NSNumberFormatter {
Expand Down
169 changes: 169 additions & 0 deletions Money/FX/Bitcoin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
//
// Bitcoin.swift
// Money
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Daniel Thorpe
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Foundation
import Result
import SwiftyJSON


// MARK: - Bitcoin Currency

protocol BitcoinCurrencyType: CryptoCurrencyType { }

extension Currency {

/**
# Currency.XBT
This is the ISO 4217 currency code, however at the moment
it is unofficial.
*/
public struct XBT: BitcoinCurrencyType {
public static let code = "XBT"
/// unicode \u{20bf} was accepted as the Bitcoin currency
/// symbol in November
public static let symbol = "Ƀ"
/// The smallest unit of Bitcoin is the Satoshi
/// - see: https://en.bitcoin.it/wiki/Satoshi_(unit)
public static let scale: Int = 8
public static let formatter: NSNumberFormatter = {
let fmtr = NSNumberFormatter()
fmtr.numberStyle = .CurrencyStyle
fmtr.maximumFractionDigits = scale
fmtr.currencySymbol = symbol
return fmtr
}()
}

/**
# Currency.BTC
This is the common code used for Bitcoin, although it can never become
the ISO standard as BT is the country code for Bhutan.
*/
public struct BTC: BitcoinCurrencyType {
public static let code = "BTC"
public static let symbol = Currency.XBT.symbol
public static let scale = Currency.XBT.scale
public static let formatter = Currency.XBT.formatter
}
}

public typealias XBT = _Money<Currency.XBT>
public typealias BTC = _Money<Currency.BTC>


// MARK - cex.io FX

public protocol CEXSupportedFiatCurrencyType: CurrencyType {
static var cex_commissionPercentage: BankersDecimal { get }
}

extension Currency.USD: CEXSupportedFiatCurrencyType {
public static let cex_commissionPercentage: BankersDecimal = 0.2
}

extension Currency.EUR: CEXSupportedFiatCurrencyType {
public static let cex_commissionPercentage: BankersDecimal = 0.2
}

extension Currency.RUB: CEXSupportedFiatCurrencyType {
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")
let data = try! JSON(["amnt": Double(1.0)]).rawData()
request.HTTPBody = data
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))
}
)
}
}

public final class CEXBuy<Base: MoneyType where Base.Currency: CEXSupportedFiatCurrencyType>: _CEX<_CEXBuy<Base>> { }
public final class CEXSell<Counter: MoneyType where Counter.Currency: CEXSupportedFiatCurrencyType>: _CEX<_CEXSell<Counter>> { }





Loading

0 comments on commit daeba22

Please sign in to comment.