From 3e04c98cf64ac4c3d36122a04e8f0bcaa756d193 Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Tue, 25 Nov 2014 18:50:31 +1100 Subject: [PATCH] Added support for LakeBTC exchange. --- lakebtchttp.go | 175 +++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 8 ++- 2 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 lakebtchttp.go diff --git a/lakebtchttp.go b/lakebtchttp.go new file mode 100644 index 00000000..d263e01e --- /dev/null +++ b/lakebtchttp.go @@ -0,0 +1,175 @@ +package main + +import ( + "net/http" + "net/url" + "strconv" + "crypto/hmac" + "crypto/sha256" + "encoding/base64" + "encoding/hex" + "errors" + "strings" + "time" + "io/ioutil" + "fmt" +) + +const ( + LAKEBTC_API_URL = "https://www.LakeBTC.com/api_v1/" + LAKEBTC_TICKER = "ticker" + LAKEBTC_ORDERBOOK = "bcorderbook" + LAKEBTC_ORDERBOOK_CNY = "bcorderbook_cny" + LAKEBTC_TRADES = "bctrades" + LAKEBTC_GET_ACCOUNT_INFO = "getAccountInfo" + LAKEBTC_BUY_ORDER = "buyOrder" + LAKEBTC_SELL_ORDER = "sellOrder" + LAKEBTC_GET_ORDERS = "getOrders" + LAKEBTC_CANCEL_ORDER = "cancelOrder" + LAKEBTC_GET_TRADES = "getTrades" +) + +type LakeBTC struct { + Email, APISecret string +} + +type LakeBTCTicker struct { + Last float64 + Bid float64 + Ask float64 + High float64 + Low float64 +} + +type LakeBTCTickerResponse struct { + USD LakeBTCTicker + CNY LakeBTCTicker +} + +func (l *LakeBTC) GetTicker() (LakeBTCTickerResponse) { + response := LakeBTCTickerResponse{} + err := SendHTTPRequest(LAKEBTC_API_URL + LAKEBTC_TICKER, true, &response) + if err != nil { + fmt.Println(err) + return response + } + return response +} + +func (l *LakeBTC) GetOrderBook(currency string) (bool) { + req := LAKEBTC_ORDERBOOK + if currency == "CNY" { + req = LAKEBTC_ORDERBOOK_CNY + } + + err := SendHTTPRequest(LAKEBTC_API_URL + req, true, nil) + if err != nil { + fmt.Println(err) + return false + } + return true +} + +func (l *LakeBTC) GetTradeHistory() (bool) { + err := SendHTTPRequest(LAKEBTC_API_URL + LAKEBTC_TRADES, true, nil) + if err != nil { + fmt.Println(err) + return false + } + return true +} + +func (l *LakeBTC) GetAccountInfo() { + err := l.SendAuthenticatedHTTPRequest(LAKEBTC_GET_ACCOUNT_INFO, "") + + if err != nil { + fmt.Println(err) + } +} + +func (l *LakeBTC) Trade(orderType int, amount, price float64, currency string) { + params := strconv.FormatFloat(price, 'f', 8, 64) + "," + strconv.FormatFloat(amount, 'f', 8, 64) + "," + currency + err := errors.New("") + + if orderType == 0 { + err = l.SendAuthenticatedHTTPRequest(LAKEBTC_BUY_ORDER, params) + } else { + err = l.SendAuthenticatedHTTPRequest(LAKEBTC_SELL_ORDER, params) + } + + if err != nil { + fmt.Println(err) + } +} + +func (l *LakeBTC) GetOrders() { + err := l.SendAuthenticatedHTTPRequest(LAKEBTC_GET_ORDERS, "") + if err != nil { + fmt.Println(err) + } +} + +func (l *LakeBTC) CancelOrder(orderID int64) { + params := strconv.FormatInt(orderID, 10) + err := l.SendAuthenticatedHTTPRequest(LAKEBTC_CANCEL_ORDER, params) + if err != nil { + fmt.Println(err) + } +} + +func (l *LakeBTC) GetTrades(timestamp time.Time) { + params := "" + + if !timestamp.IsZero() { + params = strconv.FormatInt(timestamp.Unix(), 10) + } + + err := l.SendAuthenticatedHTTPRequest(LAKEBTC_GET_TRADES, params) + if err != nil { + fmt.Println(err) + } +} + +func (l *LakeBTC) SendAuthenticatedHTTPRequest(method, params string) (err error) { + nonce := strconv.FormatInt(time.Now().Unix(), 10) + v := url.Values{} + v.Set("tnonce", nonce) + v.Set("accesskey", l.Email) + v.Set("requestmethod", "POST") + v.Set("id", nonce) + v.Set("method", method) + v.Set("params", params) + + hmac := hmac.New(sha256.New, []byte(l.APISecret)) + encoded := v.Encode() + hmac.Write([]byte(encoded)) + + + fmt.Printf("Sending POST request to %s calling method %s with params %s\n", LAKEBTC_API_URL, method, encoded) + reqBody := strings.NewReader(encoded) + hash := hex.EncodeToString(hmac.Sum(nil)) + b64 := base64.StdEncoding.EncodeToString([]byte(l.Email + ":" + hash)) + + req, err := http.NewRequest("POST", LAKEBTC_API_URL, reqBody) + + if err != nil { + return err + } + + req.Header.Add("Json-Rpc-Tonce", nonce) + req.Header.Add("Authorization: Basic", b64) + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + + client := &http.Client{} + resp, err := client.Do(req) + + if err != nil { + return errors.New("PostRequest: Unable to send request") + } + + contents, _ := ioutil.ReadAll(resp.Body) + fmt.Printf("Recieved raw: %s\n", string(contents)) + resp.Body.Close() + return nil + +} \ No newline at end of file diff --git a/main.go b/main.go index 43b8c6da..5e1d79c7 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ type Exchange struct { okcoinChina OKCoin okcoinIntl OKCoin itbit ItBit + lakebtc LakeBTC huobi HUOBI } @@ -29,9 +30,14 @@ func main() { log.Fatalln(err) return } - + //temp until proper asynchronous method of getting pricing/order books is coded for { + go func() { + LakeBTCTickerResponse := exchange.lakebtc.GetTicker() + log.Printf("LakeBTC USD: Last %f (%f) High %f (%f) Low %f (%f)\n", LakeBTCTickerResponse.USD.Last, LakeBTCTickerResponse.CNY.Last, LakeBTCTickerResponse.USD.High, LakeBTCTickerResponse.CNY.High, LakeBTCTickerResponse.USD.Low, LakeBTCTickerResponse.CNY.Low) + }() + go func() { BTCChinaBTC := exchange.btcchina.GetTicker("btccny") BTCChinaBTCLastUSD, _ := ConvertCurrency(BTCChinaBTC.Last, "CNY", "USD")