-
Notifications
You must be signed in to change notification settings - Fork 23
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
Develop #6
base: master
Are you sure you want to change the base?
Develop #6
Changes from 3 commits
b6e6299
35faa93
4fd2784
886a07b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,30 @@ | |
|
||
import Foundation | ||
|
||
public enum OrderType: String { | ||
case Buy = "Buy" | ||
case Sell = "Sell" | ||
} | ||
|
||
public protocol OrderProtocol { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move OrderProtocol and Order class into its own file, say, OpenOrder.swift |
||
var type: OrderType? { get } | ||
var rate: NSDecimalNumber? { get } | ||
} | ||
|
||
open class Order: OrderProtocol { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename Order to OpenOrder. Reason: To distinguish between completed Orders. |
||
|
||
public var id: String? | ||
public var type: OrderType? | ||
public var rate: NSDecimalNumber? | ||
public var amount: NSDecimalNumber? | ||
public var remaining: NSDecimalNumber? | ||
public var total: NSDecimalNumber? | ||
public var market: CurrencyPair? | ||
|
||
init(json: [String: Any]) { | ||
} | ||
} | ||
|
||
public protocol BalanceType { | ||
var currency: Currency { get } | ||
var quantity: NSDecimalNumber { get } | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,14 +60,20 @@ public extension Currency { | |
static let INR = Currency(name: "Indian rupee", code: "INR") | ||
static let BRL = Currency(name: "Brazilian real", code: "BRL") | ||
static let ZAR = Currency(name: "South African rand", code: "ZAR") | ||
|
||
static let Bitcoin = Currency(name: "Bitcoin", code: "BTC") | ||
static let Ethereum = Currency(name: "Ethereum", code: "ETH") | ||
static let Litecoin = Currency(name: "Litecoin", code: "LTC") | ||
static let Ripple = Currency(name: "Ripple", code: "XRP") | ||
static let Cardano = Currency(name: "Cardano", code: "ADA") | ||
static let NEM = Currency(name: "NEM", code: "XEM") | ||
static let USDT = Currency(name: "Tether USD", code: "USDT") | ||
static let ETC = Currency(name: "Ethereum Classic", code: "ETC") | ||
static let BCH = Currency(name: "Bitcoin Cash", code: "BCH") | ||
static let DOGE = Currency(name: "Dogecoin", code: "DOGE") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please remove these altcoin currency static members, since I've already added several of them in 0.0.4? Also, I would like to name them using the name instead of their code. Like |
||
static let XMR = Currency(name: "Monero", code: "XMR") | ||
static let ZEC = Currency(name: "Zcash", code: "ZEC") | ||
static let DASH = Currency(name: "Dash", code: "DASH") | ||
|
||
static let currencies: [Currency] = [ | ||
USD, EUR, JPY, GBP, AUD, CAD, CHF, CNY, SEK, NZD, MXN, SGD, HKD, NOK, KRW, TRY, RUB, INR, BRL, ZAR, | ||
Bitcoin, | ||
|
@@ -76,6 +82,12 @@ public extension Currency { | |
Litecoin, | ||
Cardano, | ||
NEM, | ||
USDT | ||
] | ||
USDT, | ||
ETC, | ||
BCH, | ||
DOGE, | ||
XMR, | ||
ZEC, | ||
DASH | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,6 +102,27 @@ public struct Cryptopia { | |
} | ||
} | ||
|
||
public class CryptopiaOrder: Order { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename CryptopiaOrder to just Order. Reason: It is already inside the struct, Cryptopia. |
||
|
||
override init(json: [String: Any]) { | ||
super.init(json: json) | ||
if let orderId = json["OrderId"] { | ||
self.id = String(describing: orderId) | ||
} | ||
self.rate = NSDecimalNumber(json["Rate"]) | ||
self.amount = NSDecimalNumber(json["Amount"]) | ||
self.remaining = NSDecimalNumber(json["Remaining"]) | ||
self.total = NSDecimalNumber(json["Total"]) | ||
if let market = json["Market"] as? String { | ||
let split = market.components(separatedBy: "/") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use convenience init method from CurrencyPair extension in Cryptopia.swift to create CurrencyPair in Order init to avoid redundant code. |
||
if split.count == 2 { | ||
self.market = CurrencyPair(quantity: Currency(code: split[1]), price: Currency(code: split[0])) | ||
} | ||
} | ||
|
||
self.type = OrderType(rawValue: (json["Type"] as? String) ?? "") | ||
} | ||
} | ||
public class CryptopiaCurrency: Currency { | ||
public var id: Int | ||
public var symbol: String | ||
|
@@ -154,12 +175,13 @@ public struct Cryptopia { | |
public enum API { | ||
case getMarkets | ||
case getBalance | ||
case getOpenOrders | ||
// | ||
case getCurrencies | ||
case getTradePairs | ||
} | ||
|
||
public class Store: ExchangeDataStore<Market, Balance> { | ||
public class Store: ExchangeDataStore<Market, Balance, Order> { | ||
public static var shared = Store() | ||
|
||
override private init() { | ||
|
@@ -170,6 +192,8 @@ public struct Cryptopia { | |
|
||
public var tickersResponse: HTTPURLResponse? = nil | ||
public var balanceResponse: HTTPURLResponse? = nil | ||
public var openOrdersResponse: HTTPURLResponse? = nil | ||
|
||
public var currenciesResponse: (response: HTTPURLResponse?, currencies: [CryptopiaCurrency]) = (nil, []) | ||
} | ||
|
||
|
@@ -232,6 +256,22 @@ public struct Cryptopia { | |
} | ||
} | ||
|
||
public func getOpenOrders(completion: @escaping (ResponseType) -> Void) { | ||
let apiType = Cryptopia.API.getOpenOrders | ||
|
||
if apiType.checkInterval(response: store.openOrdersResponse) { | ||
completion(.cached) | ||
} else { | ||
cryptopiaDataTaskFor(api: apiType) { (json, response, error) in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When you pull the new changes, you would see the new ResponseType as the parameter of dataTask completion block here. Could you fix this? |
||
guard let json = json as? [[String: Any]] else { return } | ||
let openOrders = json.map { CryptopiaOrder(json: $0) } | ||
self.store.openOrders = openOrders | ||
self.store.openOrdersResponse = response | ||
completion(.fetched) | ||
}.resume() | ||
} | ||
} | ||
|
||
public func getBalances(completion: @escaping (ResponseType) -> Void) { | ||
let apiType = Cryptopia.API.getBalance | ||
|
||
|
@@ -242,14 +282,12 @@ public struct Cryptopia { | |
} else { | ||
|
||
cryptopiaDataTaskFor(api: apiType) { (json, response, error) in | ||
|
||
guard let json = json as? [[String: Any]] else { return } | ||
let balances = json.map { Balance(json: $0, currencyStore: self.activeCurrencyStore()) }.filter { $0.available != .zero } | ||
self.store.balances = balances | ||
self.store.balanceResponse = response | ||
completion(.fetched) | ||
|
||
}.resume() | ||
}.resume() | ||
} | ||
} | ||
|
||
|
@@ -322,6 +360,7 @@ extension Cryptopia.API: APIType { | |
case .getTradePairs: return "/GetTradePairs" | ||
case .getMarkets: return "/GetMarkets" | ||
case .getBalance: return "/GetBalance" | ||
case .getOpenOrders: return "/GetOpenOrders" | ||
} | ||
} | ||
|
||
|
@@ -331,6 +370,7 @@ extension Cryptopia.API: APIType { | |
case .getTradePairs: return .GET | ||
case .getMarkets: return .GET | ||
case .getBalance: return .POST | ||
case .getOpenOrders: return .POST | ||
} | ||
} | ||
|
||
|
@@ -340,6 +380,7 @@ extension Cryptopia.API: APIType { | |
case .getTradePairs: return false | ||
case .getMarkets: return false | ||
case .getBalance: return true | ||
case .getOpenOrders: return true | ||
} | ||
} | ||
|
||
|
@@ -349,6 +390,7 @@ extension Cryptopia.API: APIType { | |
case .getTradePairs: return .url | ||
case .getMarkets: return .url | ||
case .getBalance: return .url | ||
case .getOpenOrders: return .url | ||
} | ||
} | ||
|
||
|
@@ -358,6 +400,7 @@ extension Cryptopia.API: APIType { | |
case .getTradePairs: return [:] | ||
case .getMarkets: return [:] | ||
case .getBalance: return [:] | ||
case .getOpenOrders: return [:] | ||
} | ||
} | ||
|
||
|
@@ -367,6 +410,7 @@ extension Cryptopia.API: APIType { | |
case .getTradePairs: return .aMonth | ||
case .getMarkets: return .aMinute | ||
case .getBalance: return .aMinute | ||
case .getOpenOrders: return .aMinute | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String enum values are implicit. No need to specify explicitly unless the value is different from enum case name.