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

Develop #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions Binance/Binance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public struct Binance {
balances = []
}
}

public class Balance: Cryptex.Balance {
public var locked: NSDecimalNumber

Expand All @@ -51,7 +51,7 @@ public struct Binance {
}
}

public class Store: ExchangeDataStore<Ticker, Account.Balance> {
public class Store: ExchangeDataStore<Ticker, Account.Balance, Order> {
public static var shared = Store()


Expand Down
2 changes: 1 addition & 1 deletion BitGrail/BitGrail.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public struct BitGrail {
case getBalance
}

public class Store: ExchangeDataStore<Market, Balance> {
public class Store: ExchangeDataStore<Market, Balance, Order> {
public static var shared = Store()

override private init() {
Expand Down
4 changes: 2 additions & 2 deletions Bitfinex/Bitfinex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public struct Bitfinex {
super.init(currency: currencyStore.forCode(json["currency"] ?? ""), quantity: available)
}
}
public class Store: ExchangeDataStore<Ticker, Balance> {

public class Store: ExchangeDataStore<Ticker, Balance, Order> {
public static var shared = Store()

override private init() {
Expand Down
4 changes: 2 additions & 2 deletions CoinExchange/CoinExchange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ public struct CoinExchange {
case getmarketsummaries
case getBalance
}
public class Store: ExchangeDataStore<MarketSummary, Balance> {

public class Store: ExchangeDataStore<MarketSummary, Balance, Order> {
public static var shared = Store()

override private init() {
Expand Down
2 changes: 1 addition & 1 deletion CoinMarketCap/CoinMarketCap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public struct CoinMarketCap {
case getGlobal
}

public class Store: ExchangeDataStore<CMCTicker, Balance> {
public class Store: ExchangeDataStore<CMCTicker, Balance, Order> {
public static var shared = Store()

override private init() {
Expand Down
24 changes: 24 additions & 0 deletions Common/Balance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@

import Foundation

public enum OrderType: String {
case Buy = "Buy"
Copy link
Owner

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.

case Sell = "Sell"
}

public protocol OrderProtocol {
Copy link
Owner

Choose a reason for hiding this comment

The 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 {
Copy link
Owner

Choose a reason for hiding this comment

The 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 }
Expand Down
18 changes: 15 additions & 3 deletions Common/Currency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Owner

Choose a reason for hiding this comment

The 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 bitcoin instead of BTC. (I know USDT is a typo there. Will rename it as USDTether in future release, Also, NEM is an exception there)

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,
Expand All @@ -76,6 +82,12 @@ public extension Currency {
Litecoin,
Cardano,
NEM,
USDT
]
USDT,
ETC,
BCH,
DOGE,
XMR,
ZEC,
DASH
]
}
5 changes: 3 additions & 2 deletions Common/ExchangeDataStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

public class ExchangeDataStore<T: TickerType, U: BalanceType> {
public class ExchangeDataStore<T: TickerType, U: BalanceType, O: OrderProtocol> {

public var name = "ExchangeDataStore"
public var accountingCurrency: Currency = .USD
Expand All @@ -18,7 +18,8 @@ public class ExchangeDataStore<T: TickerType, U: BalanceType> {
public var tickerByName: [T] = []

public var balances: [U] = []

public var openOrders: [O] = []

public var tickersDictionary: [String: T] = [:] {
didSet {
let tickers = tickersDictionary.values.flatMap{$0}
Expand Down
52 changes: 48 additions & 4 deletions Cryptopia/Cryptopia.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,27 @@ public struct Cryptopia {
}
}

public class CryptopiaOrder: Order {
Copy link
Owner

@trsathya trsathya Jan 28, 2018

Choose a reason for hiding this comment

The 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.
Also, Subclass Order from Cryptex.Order to avoid name collision after renaming.


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: "/")
Copy link
Owner

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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() {
Expand All @@ -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, [])
}

Expand Down Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The 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

Expand All @@ -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()
}
}

Expand Down Expand Up @@ -322,6 +360,7 @@ extension Cryptopia.API: APIType {
case .getTradePairs: return "/GetTradePairs"
case .getMarkets: return "/GetMarkets"
case .getBalance: return "/GetBalance"
case .getOpenOrders: return "/GetOpenOrders"
}
}

Expand All @@ -331,6 +370,7 @@ extension Cryptopia.API: APIType {
case .getTradePairs: return .GET
case .getMarkets: return .GET
case .getBalance: return .POST
case .getOpenOrders: return .POST
}
}

Expand All @@ -340,6 +380,7 @@ extension Cryptopia.API: APIType {
case .getTradePairs: return false
case .getMarkets: return false
case .getBalance: return true
case .getOpenOrders: return true
}
}

Expand All @@ -349,6 +390,7 @@ extension Cryptopia.API: APIType {
case .getTradePairs: return .url
case .getMarkets: return .url
case .getBalance: return .url
case .getOpenOrders: return .url
}
}

Expand All @@ -358,6 +400,7 @@ extension Cryptopia.API: APIType {
case .getTradePairs: return [:]
case .getMarkets: return [:]
case .getBalance: return [:]
case .getOpenOrders: return [:]
}
}

Expand All @@ -367,6 +410,7 @@ extension Cryptopia.API: APIType {
case .getTradePairs: return .aMonth
case .getMarkets: return .aMinute
case .getBalance: return .aMinute
case .getOpenOrders: return .aMinute
}
}
}
4 changes: 2 additions & 2 deletions GDAX/GDAX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public struct GDAX {
super.init(currency: currencyStore.forCode( json["currency"] as? String ?? "" ), quantity: NSDecimalNumber(json["balance"]))
}
}
public class Store: ExchangeDataStore<Ticker, Account> {

public class Store: ExchangeDataStore<Ticker, Account, Order> {
public static var shared = Store()

override private init() {
Expand Down
4 changes: 2 additions & 2 deletions Gemini/Gemini.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ public struct Gemini {
isAuctionFill = json["is_auction_fill"] as! Bool
}
}
public class Store: ExchangeDataStore<Ticker, Balance> {

public class Store: ExchangeDataStore<Ticker, Balance, Order> {
public static var shared = Store()

override private init() {
Expand Down
2 changes: 1 addition & 1 deletion Koinex/Koinex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public extension CurrencyPair {

public struct Koinex {

public class Store: ExchangeDataStore<Ticker, Balance> {
public class Store: ExchangeDataStore<Ticker, Balance, Order> {
public static var shared = Store()

override private init() {
Expand Down
4 changes: 2 additions & 2 deletions Kraken/Kraken.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public struct Kraken {
super.init(symbol: symbol, price: lastPrice)
}
}

public class Balance: Cryptex.Balance {

public let type: String
Expand All @@ -48,7 +48,7 @@ public struct Kraken {
}
}

public class Store: ExchangeDataStore<Ticker, Balance> {
public class Store: ExchangeDataStore<Ticker, Balance, Order> {
public static var shared = Store()

override private init() {
Expand Down
4 changes: 2 additions & 2 deletions Poloniex/Poloniex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public struct Poloniex {
status = json["status"] as? String ?? ""
}
}

public struct Withdrawal {
public var withdrawalNumber: Int
public var currency: Currency
Expand All @@ -155,7 +155,7 @@ public struct Poloniex {
}
}

public class Store: ExchangeDataStore<Poloniex.Ticker, Balance> {
public class Store: ExchangeDataStore<Poloniex.Ticker, Balance, Order> {
public static var shared = Store()

override private init() {
Expand Down