Skip to content

Commit

Permalink
support binance
Browse files Browse the repository at this point in the history
  • Loading branch information
zlq4863947 committed Oct 3, 2018
1 parent fa92f6d commit 6ee9177
Show file tree
Hide file tree
Showing 16 changed files with 331 additions and 49 deletions.
36 changes: 32 additions & 4 deletions CoinQuotor.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@
55C0C68021625F27003CBFE3 /* EventMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55C0C67F21625F27003CBFE3 /* EventMonitor.swift */; };
55C0C688216271ED003CBFE3 /* QuotesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55C0C687216271ED003CBFE3 /* QuotesViewController.swift */; };
55C0C68C2163792A003CBFE3 /* BitMEX.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55C0C68B2163792A003CBFE3 /* BitMEX.swift */; };
55C0C69421637D95003CBFE3 /* BitMEX+Pairs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55C0C69321637D95003CBFE3 /* BitMEX+Pairs.swift */; };
55C0C6982163C800003CBFE3 /* BitMEXDataTarget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55C0C6972163C800003CBFE3 /* BitMEXDataTarget.swift */; };
55C0C69B2163DD1F003CBFE3 /* Instrument.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55C0C69A2163DD1F003CBFE3 /* Instrument.swift */; };
55C0C69D2163EA25003CBFE3 /* APIError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55C0C69C2163EA25003CBFE3 /* APIError.swift */; };
55C0C6A121648996003CBFE3 /* Double.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55C0C6A021648996003CBFE3 /* Double.swift */; };
55C0C6A421651525003CBFE3 /* CacheStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55C0C6A321651525003CBFE3 /* CacheStore.swift */; };
55C0C6A721652A7E003CBFE3 /* Binance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55C0C6A621652A7E003CBFE3 /* Binance.swift */; };
55C0C6A921652AAE003CBFE3 /* BinanceDataTarget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55C0C6A821652AAE003CBFE3 /* BinanceDataTarget.swift */; };
55C0C6AE21652BD9003CBFE3 /* Symbol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55C0C6AD21652BD9003CBFE3 /* Symbol.swift */; };
55C0C6B0216532F7003CBFE3 /* Ex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55C0C6AF216532F7003CBFE3 /* Ex.swift */; };
A96808C18FB6FDF43A5F66C9 /* Pods_CoinQuotor.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8B7F9D9DA33F124FB02E1629 /* Pods_CoinQuotor.framework */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -83,12 +86,15 @@
55C0C67F21625F27003CBFE3 /* EventMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventMonitor.swift; sourceTree = "<group>"; };
55C0C687216271ED003CBFE3 /* QuotesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuotesViewController.swift; sourceTree = "<group>"; };
55C0C68B2163792A003CBFE3 /* BitMEX.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BitMEX.swift; sourceTree = "<group>"; };
55C0C69321637D95003CBFE3 /* BitMEX+Pairs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BitMEX+Pairs.swift"; sourceTree = "<group>"; };
55C0C6972163C800003CBFE3 /* BitMEXDataTarget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BitMEXDataTarget.swift; sourceTree = "<group>"; };
55C0C69A2163DD1F003CBFE3 /* Instrument.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Instrument.swift; sourceTree = "<group>"; };
55C0C69C2163EA25003CBFE3 /* APIError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIError.swift; sourceTree = "<group>"; };
55C0C6A021648996003CBFE3 /* Double.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Double.swift; sourceTree = "<group>"; };
55C0C6A321651525003CBFE3 /* CacheStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CacheStore.swift; sourceTree = "<group>"; };
55C0C6A621652A7E003CBFE3 /* Binance.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Binance.swift; sourceTree = "<group>"; };
55C0C6A821652AAE003CBFE3 /* BinanceDataTarget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BinanceDataTarget.swift; sourceTree = "<group>"; };
55C0C6AD21652BD9003CBFE3 /* Symbol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Symbol.swift; sourceTree = "<group>"; };
55C0C6AF216532F7003CBFE3 /* Ex.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Ex.swift; sourceTree = "<group>"; };
8B7F9D9DA33F124FB02E1629 /* Pods_CoinQuotor.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_CoinQuotor.framework; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -207,8 +213,10 @@
55C0C6892163024E003CBFE3 /* ExAPI */ = {
isa = PBXGroup;
children = (
55C0C6A521652A1B003CBFE3 /* Binance */,
55C0C68A21637800003CBFE3 /* BitMEX */,
55C0C69C2163EA25003CBFE3 /* APIError.swift */,
55C0C6AF216532F7003CBFE3 /* Ex.swift */,
);
path = ExAPI;
sourceTree = "<group>";
Expand All @@ -218,7 +226,6 @@
children = (
55C0C6992163DCEC003CBFE3 /* Model */,
55C0C68B2163792A003CBFE3 /* BitMEX.swift */,
55C0C69321637D95003CBFE3 /* BitMEX+Pairs.swift */,
55C0C6972163C800003CBFE3 /* BitMEXDataTarget.swift */,
);
path = BitMEX;
Expand All @@ -240,6 +247,24 @@
path = Extensions;
sourceTree = "<group>";
};
55C0C6A521652A1B003CBFE3 /* Binance */ = {
isa = PBXGroup;
children = (
55C0C6AC21652BCA003CBFE3 /* Model */,
55C0C6A621652A7E003CBFE3 /* Binance.swift */,
55C0C6A821652AAE003CBFE3 /* BinanceDataTarget.swift */,
);
path = Binance;
sourceTree = "<group>";
};
55C0C6AC21652BCA003CBFE3 /* Model */ = {
isa = PBXGroup;
children = (
55C0C6AD21652BD9003CBFE3 /* Symbol.swift */,
);
path = Model;
sourceTree = "<group>";
};
FA2F759E283F6B3756C860CA /* Pods */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -381,7 +406,6 @@
files = (
55C0C67421625EB9003CBFE3 /* HistoricalDataResponse.swift in Sources */,
55C0C67821625EB9003CBFE3 /* GetHistoricalHourlyRequest.swift in Sources */,
55C0C69421637D95003CBFE3 /* BitMEX+Pairs.swift in Sources */,
55C0C67C21625EB9003CBFE3 /* GetCustomAverageRequest.swift in Sources */,
55C0C63F21622D29003CBFE3 /* ViewController.swift in Sources */,
55C0C67621625EB9003CBFE3 /* GetHistoricalDailyRequest.swift in Sources */,
Expand All @@ -391,13 +415,17 @@
55C0C68C2163792A003CBFE3 /* BitMEX.swift in Sources */,
55C0C69D2163EA25003CBFE3 /* APIError.swift in Sources */,
55C0C69B2163DD1F003CBFE3 /* Instrument.swift in Sources */,
55C0C6AE21652BD9003CBFE3 /* Symbol.swift in Sources */,
55C0C67021625EB9003CBFE3 /* CustomAverageResponse.swift in Sources */,
55C0C6982163C800003CBFE3 /* BitMEXDataTarget.swift in Sources */,
55C0C6A721652A7E003CBFE3 /* Binance.swift in Sources */,
55C0C67D21625EB9003CBFE3 /* GetSymbolsFullDataRequest.swift in Sources */,
55C0C63D21622D29003CBFE3 /* AppDelegate.swift in Sources */,
55C0C688216271ED003CBFE3 /* QuotesViewController.swift in Sources */,
55C0C68021625F27003CBFE3 /* EventMonitor.swift in Sources */,
55C0C66C21625EB9003CBFE3 /* ResponseValidator.swift in Sources */,
55C0C6B0216532F7003CBFE3 /* Ex.swift in Sources */,
55C0C6A921652AAE003CBFE3 /* BinanceDataTarget.swift in Sources */,
55C0C67321625EB9003CBFE3 /* CryptoCompareError.swift in Sources */,
55C0C66B21625EB9003CBFE3 /* RequestBuilder.swift in Sources */,
55C0C66D21625EB9003CBFE3 /* ValueParameter.swift in Sources */,
Expand Down
21 changes: 19 additions & 2 deletions CoinQuotor/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let api = CryptoCompareAPI(applicationName: "MyApp")
let request = GetSymbolPriceRequest(fsym: "XBTUSD", tsyms: "USD", e: .bitmex)


var ex: Ex! = nil
let bitmex = BitMEX()
let binance = Binance()
var quotePair: String?

func applicationDidFinishLaunching(_ aNotification: Notification) {
self.initEx()
quotePair = CacheStore.getPair()
// Insert code here to initialize your application
if let button = statusItem.button {
button.image = NSImage(named: NSImage.Name("bitmex")) // StatusBarButtonImage
button.image = NSImage(named: NSImage.Name(ex.name)) // StatusBarButtonImage
button.imagePosition = .imageLeft
if quotePair == nil {
button.title = "Select a quote pair"
Expand All @@ -51,10 +55,23 @@ class AppDelegate: NSObject, NSApplicationDelegate {
self.timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.onUpdate), userInfo: nil, repeats: true)
}

private func initEx() {
let exName = CacheStore.getExchange()
if exName != nil {
if exName == "bitmex" {
ex = bitmex
} else if exName == "binance" {
ex = binance
}
} else {
ex = bitmex
}
}

@objc func onUpdate() {

if quotePair != nil {
bitmex.getLastPrice(pair: quotePair!) { (price, err) in
ex.getLastPrice(pair: quotePair!) { (price, err) in
if price != nil {
DispatchQueue.main.async { // Make sure you're on the main thread here
self.statusItem.button?.title = "\(self.quotePair!) - \(price!.avoidNotation)"
Expand Down
21 changes: 21 additions & 0 deletions CoinQuotor/Assets.xcassets/binance.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "binance.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion CoinQuotor/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -692,12 +692,13 @@
<comboBox verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="gZp-Zc-lfn" userLabel="combo ex">
<rect key="frame" x="72" y="49" width="99" height="25"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<comboBoxCell key="cell" controlSize="small" enabled="NO" sendsActionOnEndEditing="YES" borderStyle="bezel" title="Bitmex" placeholderString="exchange" drawsBackground="YES" numberOfVisibleItems="5" id="Dl1-5e-HIM">
<comboBoxCell key="cell" controlSize="small" sendsActionOnEndEditing="YES" borderStyle="bezel" title="Bitmex" placeholderString="exchange" drawsBackground="YES" numberOfVisibleItems="5" id="Dl1-5e-HIM">
<font key="font" size="15" name="AppleBraille-Outline6Dot"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
<objectValues>
<string>BitMEX</string>
<string>Binance</string>
</objectValues>
</comboBoxCell>
</comboBox>
Expand Down Expand Up @@ -730,6 +731,7 @@
</view>
<connections>
<outlet property="exCombo" destination="gZp-Zc-lfn" id="9sQ-N2-X54"/>
<outlet property="exImage" destination="Yg3-PA-NfA" id="sEv-63-00l"/>
<outlet property="pairCombo" destination="6ZU-Pf-bdm" id="CkA-af-IwD"/>
</connections>
</viewController>
Expand Down
12 changes: 11 additions & 1 deletion CoinQuotor/CacheStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import DefaultsKit
class CacheStore {
private static let sharedInstance = CacheStore()
private let defaults: Defaults
private let exKey: Key<String>

private let pairKey: Key<String>

private init() {
self.defaults = Defaults()
self.exKey = Key<String>("cacheExKey")
self.pairKey = Key<String>("cachePairKey")
}

Expand All @@ -27,4 +29,12 @@ class CacheStore {
class func getPair() -> String? {
return sharedInstance.defaults.get(for: sharedInstance.pairKey)
}

class func setExchange(ex: String) {
sharedInstance.defaults.set(ex, for: sharedInstance.exKey)
}

class func getExchange() -> String? {
return sharedInstance.defaults.get(for: sharedInstance.exKey)
}
}
68 changes: 68 additions & 0 deletions CoinQuotor/ExAPI/Binance/Binance.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// Binance.swift
// CoinQuotor
//
// Created by syaku on 2018/10/04.
// Copyright © 2018年 syaku. All rights reserved.
//

import Foundation
import Moya
import RxSwift

class Binance: Ex {
override var name: String {
get {
return "binance"
}
set {}
}

let provider = MoyaProvider<BinanceDataTarget>()

private func request(target: BinanceDataTarget, completed: @escaping (Data?, Error?) -> () ) {
provider.rx.request(target)
.subscribe({ event in
switch event {
case let .success(response):
completed(response.data, nil)
case let .error(error):
completed(nil, error)
}
})
.disposed(by: disposeBag)
}

override func getLastPrice(pair: String, completed: @escaping (Double?, Error?) -> ()) {
request(target: .lastPrice(pair: pair)) { (res, err) in
if res != nil {
do {
let symbol = try JSONDecoder().decode(Ticker.self, from: res! )
completed(Double(symbol.price), nil)
} catch {
completed(nil, APIError.castError("getLastPrice converting data error!"))
}
} else {
completed(nil, err)
}
}
}

override func getPairs(completed: @escaping ([String]?, Error?) -> ()) {
request(target: .pairs()) { (res, err) in
if res != nil {
do {
let res = try JSONDecoder().decode(Symbols.self, from: res! )
let pairs = res.symbols.compactMap { symbol -> String? in
return symbol.symbol
}
completed(pairs, nil)
} catch {
completed(nil, APIError.castError("getPairs converting data error!"))
}
} else {
completed(nil, err)
}
}
}
}
55 changes: 55 additions & 0 deletions CoinQuotor/ExAPI/Binance/BinanceDataTarget.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// BinanceDataTarget.swift
// CoinQuotor
//
// Created by syaku on 2018/10/04.
// Copyright © 2018年 syaku. All rights reserved.
//

import Foundation
import Moya
import RxCocoa
import RxSwift

enum BinanceDataTarget {
case lastPrice(pair: String)
case pairs()
}

extension BinanceDataTarget: TargetType {
var sampleData: Data {
return "".data(using: String.Encoding.utf8)!
}

var baseURL:URL {
return URL.init(string: "https://api.binance.com/api")!
}

var path: String {
switch self {
case .lastPrice(_):
return "/v3/ticker/price"
case .pairs():
return "/v1/exchangeInfo"
}
}

var method: Moya.Method {
return .get
}

var task: Task {
switch self {
case .lastPrice(let pair):
return .requestParameters(parameters: [
"symbol": pair
], encoding: URLEncoding.queryString)
case .pairs:
return .requestPlain
}
}

var headers: [String : String]? {
return ["Content-Type": "application/json"]
}
}
37 changes: 37 additions & 0 deletions CoinQuotor/ExAPI/Binance/Model/Symbol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Symbol.swift
// CoinQuotor
//
// Created by syaku on 2018/10/04.
// Copyright © 2018年 syaku. All rights reserved.
//

import Foundation

extension Binance {
struct Ticker: Codable {
let symbol: String
let price: String

enum CodingKeys : String, CodingKey {
case symbol = "symbol"
case price = "price"
}
}

struct Symbol: Codable {
let symbol: String

enum CodingKeys : String, CodingKey {
case symbol = "symbol"
}
}

struct Symbols: Codable {
let symbols: [Symbol]

enum CodingKeys : String, CodingKey {
case symbols = "symbols"
}
}
}
Loading

0 comments on commit 6ee9177

Please sign in to comment.