From ce6fcf57e57fd01e27ac082e922e7e5067a8a30e Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Mon, 6 Mar 2017 20:56:16 +1100 Subject: [PATCH] Fix remaining LakeBTC REST API requests and remove non-needed Printlns --- anxhttp.go | 1 - bitfinexhttp.go | 2 -- btcchttp.go | 1 - common.go | 1 - lakebtchttp.go | 79 ++++++++++++++++++++++++++++--------------------- 5 files changed, 46 insertions(+), 38 deletions(-) diff --git a/anxhttp.go b/anxhttp.go index 840e52cc..486d69a7 100644 --- a/anxhttp.go +++ b/anxhttp.go @@ -339,7 +339,6 @@ func (a *ANX) OrderInfo(orderID string) (ANXOrderResponse, error) { err := a.SendAuthenticatedHTTPRequest(ANX_ORDER_INFO, request, &response) if err != nil { - log.Println(err) return ANXOrderResponse{}, err } diff --git a/bitfinexhttp.go b/bitfinexhttp.go index 7d3250c7..bb720e99 100644 --- a/bitfinexhttp.go +++ b/bitfinexhttp.go @@ -415,8 +415,6 @@ func (b *Bitfinex) GetAccountInfo() ([]BitfinexAccountInfo, error) { if err != nil { log.Println(err) } - - log.Println(response) return response, nil } diff --git a/btcchttp.go b/btcchttp.go index 860e28d6..0426bfa7 100644 --- a/btcchttp.go +++ b/btcchttp.go @@ -243,7 +243,6 @@ func (b *BTCC) Run() { for _, x := range b.EnabledPairs { currency := x go func() { - log.Println(currency) ticker, err := b.GetTickerPrice(StringToLower(currency)) if err != nil { log.Println(err) diff --git a/common.go b/common.go index e7fbc4be..47c255c6 100644 --- a/common.go +++ b/common.go @@ -253,7 +253,6 @@ func SendHTTPGetRequest(url string, jsonDecode bool, result interface{}) (err er err := JSONDecode(contents, &result) if err != nil { - log.Println(url) return err } } else { diff --git a/lakebtchttp.go b/lakebtchttp.go index 06508bf4..c7346522 100644 --- a/lakebtchttp.go +++ b/lakebtchttp.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "log" - "net/url" "strconv" "strings" "time" @@ -285,8 +284,8 @@ type LakeBTCAccountInfo struct { Locked map[string]string `json:"locked"` Profile struct { Email string `json:"email"` - UID int64 `json:"uid,string"` - BTCDepositAddress string `json:"btc_deposit_address"` + UID string `json:"uid"` + BTCDepositAddress string `json:"btc_deposit_addres"` } `json:"profile"` } @@ -303,19 +302,23 @@ func (l *LakeBTC) GetAccountInfo() (LakeBTCAccountInfo, error) { func (l *LakeBTC) GetExchangeAccountInfo() (ExchangeAccountInfo, error) { var response ExchangeAccountInfo - /* to-do response.ExchangeName = l.GetName() - accountBalance, err := l.GetAccountInfo() + response.ExchangeName = l.GetName() + accountInfo, err := l.GetAccountInfo() if err != nil { return response, err } - currencies := l.AvailablePairs - for i := 0; i < len(currencies); i++ { - var exchangeCurrency ExchangeAccountCurrencyInfo - exchangeCurrency.CurrencyName = currencies[i] - /exchangeCurrency.TotalValue = accountBalance.Currency[currencies[i]] to-do - response.Currencies = append(response.Currencies, exchangeCurrency) + + for x, y := range accountInfo.Balance { + for z, w := range accountInfo.Locked { + if z == x { + var exchangeCurrency ExchangeAccountCurrencyInfo + exchangeCurrency.CurrencyName = StringToUpper(x) + exchangeCurrency.TotalValue, _ = strconv.ParseFloat(y, 64) + exchangeCurrency.Hold, _ = strconv.ParseFloat(w, 64) + response.Currencies = append(response.Currencies, exchangeCurrency) + } + } } - */ return response, nil } @@ -329,7 +332,7 @@ func (l *LakeBTC) Trade(orderType int, amount, price float64, currency string) ( params := strconv.FormatFloat(price, 'f', -1, 64) + "," + strconv.FormatFloat(amount, 'f', -1, 64) + "," + currency err := errors.New("") - if orderType == 0 { + if orderType == 1 { err = l.SendAuthenticatedHTTPRequest(LAKEBTC_BUY_ORDER, params, &resp) } else { err = l.SendAuthenticatedHTTPRequest(LAKEBTC_SELL_ORDER, params, &resp) @@ -461,13 +464,13 @@ type LakeBTCWithdraw struct { State string `json:"state"` Source string `json:"source"` ExternalAccountID int64 `json:"external_account_id,string"` - At int64 `json:"at,string"` + At int64 `json:"at"` } /* Only for BTC */ func (l *LakeBTC) CreateWithdraw(amount float64, accountID int64) (LakeBTCWithdraw, error) { resp := LakeBTCWithdraw{} - params := strconv.FormatFloat(amount, 'f', -1, 64) + ",btc" + strconv.FormatInt(accountID, 10) + params := strconv.FormatFloat(amount, 'f', -1, 64) + ",btc," + strconv.FormatInt(accountID, 10) err := l.SendAuthenticatedHTTPRequest(LAKEBTC_CREATE_WITHDRAW, params, &resp) if err != nil { return resp, err @@ -477,33 +480,29 @@ func (l *LakeBTC) CreateWithdraw(amount float64, accountID int64) (LakeBTCWithdr func (l *LakeBTC) SendAuthenticatedHTTPRequest(method, params string, result interface{}) (err error) { nonce := strconv.FormatInt(time.Now().UnixNano(), 10) - id := "1" - v := url.Values{} - v.Set("tnonce", nonce) - v.Set("accesskey", l.APIKey) - v.Set("requestmethod", "POST") - v.Set("id", id) - v.Set("method", method) - v.Set("params", params) - encoded := v.Encode() - hmac := GetHMAC(HASH_SHA1, []byte(encoded), []byte(l.APISecret)) + req := fmt.Sprintf("tonce=%s&accesskey=%s&requestmethod=post&id=1&method=%s¶ms=%s", nonce, l.APIKey, method, params) + hmac := GetHMAC(HASH_SHA1, []byte(req), []byte(l.APISecret)) if l.Verbose { - log.Printf("Sending POST request to %s calling method %s with params %s\n", LAKEBTC_API_URL, method, encoded) + log.Printf("Sending POST request to %s calling method %s with params %s\n", LAKEBTC_API_URL, method, req) } - v = url.Values{} - v.Set("method", method) - v.Set("id", id) - v.Set("params", params) + postData := make(map[string]interface{}) + postData["method"] = method + postData["id"] = 1 + postData["params"] = SplitStrings(params, ",") + + data, err := JSONEncode(postData) + if err != nil { + return err + } headers := make(map[string]string) headers["Json-Rpc-Tonce"] = nonce headers["Authorization"] = "Basic " + Base64Encode([]byte(l.APIKey+":"+HexEncodeToString(hmac))) - headers["Content-Type"] = "application/x-www-form-urlencoded" - - resp, err := SendHTTPRequest("POST", LAKEBTC_API_URL, headers, strings.NewReader(v.Encode())) + headers["Content-Type"] = "application/json-rpc" + resp, err := SendHTTPRequest("POST", LAKEBTC_API_URL, headers, strings.NewReader(string(data))) if err != nil { return err } @@ -512,6 +511,20 @@ func (l *LakeBTC) SendAuthenticatedHTTPRequest(method, params string, result int log.Printf("Recieved raw: %s\n", resp) } + type ErrorResponse struct { + Error string `json:"error"` + } + + errResponse := ErrorResponse{} + err = JSONDecode([]byte(resp), &errResponse) + if err != nil { + return errors.New("Unable to check response for error.") + } + + if errResponse.Error != "" { + return errors.New(errResponse.Error) + } + err = JSONDecode([]byte(resp), &result) if err != nil {