From 58ad3a59f248485bd832146348f46f234e04436f Mon Sep 17 00:00:00 2001 From: aliereno Date: Sun, 19 Apr 2020 21:03:10 +0300 Subject: [PATCH] improvements --- README.md | 24 ++++++++++++++++++++---- btcturk/client.go | 31 ++++++++----------------------- btcturk/params.go | 6 ++++++ btcturk/trades.go | 13 +++++++++---- btcturk/user_balance.go | 2 +- btcturk/user_orders.go | 23 ++++++++++++++--------- 6 files changed, 58 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index eb9cedc..5daf459 100644 --- a/README.md +++ b/README.md @@ -47,9 +47,8 @@ func main() { ``` -## Passing params +## Examples -[Endpoint Params](https://github.com/aliereno/go-btcturk/blob/master/btcturk/params.go) ```go package main @@ -59,9 +58,26 @@ import ( func main() { api := btcturk.NewBTCTurkClient() - api.SetAuthKey("publicKey", "privateKey") - api.Quantity(0.001). + //PUBLIC ENDPOINTS + + //TICKER + _, _ = api.PairSymbol(btcturk.BTCTRY).Ticker() + + //ORDER BOOK + _, _ = api.PairSymbol(btcturk.BTCTRY).Limit(10).OrderBook() // limit optional + + //TRADES + _, _ = api.PairSymbol(btcturk.BTCTRY).Trades() + + + //PRIVATE ENDPOINTS + + _, _ = api.SetAuthKey("publicKey", "privateKey") + + _, _ = api.Balance() + + _, _ = api.Quantity(0.001). Price(50000). StopPrice(0). OrderMethod("limit"). diff --git a/btcturk/client.go b/btcturk/client.go index 8dab1d0..2c1622f 100644 --- a/btcturk/client.go +++ b/btcturk/client.go @@ -28,8 +28,8 @@ type Client struct { } type GeneralResponse struct { Success bool `json:"success"` - Message string `json:"message"` - Code int8 `json:"code"` + Message *string `json:"message"` + Code int `json:"code"` Data interface{} `json:"data"` } @@ -68,22 +68,6 @@ func (c *Client) newRequest(method, relURL string, body io.Reader) (*http.Reques return req, nil } -func (c *Client) newRequestCustomURL(method, relURL string, body io.Reader) (*http.Request, error) { - rel, err := url.Parse(relURL) - if err != nil { - return nil, err - } - - req, err := http.NewRequest(method, c.baseURL.ResolveReference(rel).String(), body) - if err != nil { - return nil, err - } - - req = req.WithContext(context.Background()) - - return req, nil -} - func (c *Client) do(r *http.Request, v interface{}) (*http.Response, error) { resp, err := c.client.Do(r) if err != nil { @@ -104,10 +88,10 @@ func (c *Client) do(r *http.Request, v interface{}) (*http.Response, error) { return nil, err } - if response.Success == true { + if response.Code == 0 { v = response.Data } else { - return nil, errors.New("response -> 'success = false'") + return nil, errors.New(*response.Message) } return resp, nil @@ -115,11 +99,11 @@ func (c *Client) do(r *http.Request, v interface{}) (*http.Response, error) { func (c *Client) auth(req *http.Request) error { if c.privateKey == "" { - return errors.New("Private Key is not set") + return errors.New("private key is not set") } if c.publicKey == "" { - return errors.New("Public Key is not set") + return errors.New("public key is not set") } key, err := base64.StdEncoding.DecodeString(c.privateKey) @@ -127,7 +111,7 @@ func (c *Client) auth(req *http.Request) error { return err } - stamp := fmt.Sprint(time.Now().Unix()) + stamp := fmt.Sprint(time.Now().Unix() * 1000) message := c.publicKey + stamp h := hmac.New(sha256.New, key) @@ -136,6 +120,7 @@ func (c *Client) auth(req *http.Request) error { req.Header.Set("X-PCK", c.publicKey) req.Header.Set("X-Stamp", stamp) req.Header.Set("X-Signature", base64.StdEncoding.EncodeToString(h.Sum(nil))) + req.Header.Add("Content-Type", "application/json") return nil } diff --git a/btcturk/params.go b/btcturk/params.go index 5bd249d..8539ab6 100644 --- a/btcturk/params.go +++ b/btcturk/params.go @@ -47,6 +47,12 @@ func (c *Client) PairSymbol(v string) *Client { return c } +// Custom Param +func (c *Client) AddCustomParam(k string, v string) *Client { + c.params.Add(k, v) + return c +} + func (c *Client) addParamInt(key string, value int) *Client { c.params.Add(key, strconv.Itoa(value)) return c diff --git a/btcturk/trades.go b/btcturk/trades.go index 6436679..42755df 100644 --- a/btcturk/trades.go +++ b/btcturk/trades.go @@ -3,10 +3,15 @@ package btcturk import "fmt" type Trade struct { - TimeStamp float64 `json:"date"` - TID string `json:"tid"` - Price string `json:"price"` - Amount string `json:"amount"` + Pair string `json:"pair"` + PairNormalized string `json:"pairNormalized"` + Numerator string `json:"numerator"` + Denominator string `json:"denominator"` + TimeStamp float64 `json:"date"` + TID string `json:"tid"` + Price string `json:"price"` + Amount string `json:"amount"` + Side string `json:"side"` } func (c *Client) Trades() ([]Trade, error) { diff --git a/btcturk/user_balance.go b/btcturk/user_balance.go index ee7b88a..6238008 100644 --- a/btcturk/user_balance.go +++ b/btcturk/user_balance.go @@ -9,7 +9,7 @@ type Balance struct { } func (c *Client) Balance() ([]Balance, error) { - req, err := c.newRequest("GET", "/api/v1/users/balances", c.body) + req, err := c.newRequest("GET", "/api/v1/users/balances", nil) if err != nil { return []Balance{}, err } diff --git a/btcturk/user_orders.go b/btcturk/user_orders.go index 1450d56..7e6956e 100644 --- a/btcturk/user_orders.go +++ b/btcturk/user_orders.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "fmt" - "strings" ) const ( @@ -81,7 +80,8 @@ type OrderType struct { } func (c *Client) OpenOrders() ([]OpenOrders, error) { - req, err := c.newRequest("GET", fmt.Sprintf("/api/v1/openOrders?%s", c.params.Encode()), nil) + jsonString, err := json.Marshal(c.params) + req, err := c.newRequest("GET", "/api/v1/openOrders", bytes.NewBuffer(jsonString)) if err != nil { return []OpenOrders{}, err } @@ -98,9 +98,7 @@ func (c *Client) OpenOrders() ([]OpenOrders, error) { } func (c *Client) CancelOrder() (bool, error) { - c.body = strings.NewReader(c.params.Encode()) - - req, err := c.newRequest("POST", "/api/v1/order", c.body) + req, err := c.newRequest("DELETE", fmt.Sprintf("/api/v1/order?%s", c.params.Encode()), c.body) if err != nil { return false, err } @@ -109,6 +107,11 @@ func (c *Client) CancelOrder() (bool, error) { } var response GeneralResponse + + // TODO + // API returns `"code":""` + // my code expects `"code":0` an integer + // so it will return error if _, err = c.do(req, &response); err != nil { return false, err } @@ -119,6 +122,9 @@ func (c *Client) CancelOrder() (bool, error) { func (c *Client) Buy() (OrderType, error) { c.params.Add("orderType", "buy") jsonString, err := json.Marshal(c.params) + if err != nil { + return OrderType{}, err + } req, err := c.newRequest("POST", "/api/v1/order", bytes.NewBuffer(jsonString)) if err != nil { @@ -128,8 +134,6 @@ func (c *Client) Buy() (OrderType, error) { return OrderType{}, err } - req.Header.Add("content-type", "application/json") - var response OrderType if _, err = c.do(req, &response); err != nil { return OrderType{}, err @@ -141,6 +145,9 @@ func (c *Client) Buy() (OrderType, error) { func (c *Client) Sell() (OrderType, error) { c.params.Add("orderType", "sell") jsonString, err := json.Marshal(c.params) + if err != nil { + return OrderType{}, err + } req, err := c.newRequest("POST", "/api/v1/order", bytes.NewBuffer(jsonString)) if err != nil { @@ -150,8 +157,6 @@ func (c *Client) Sell() (OrderType, error) { return OrderType{}, err } - req.Header.Add("content-type", "application/json") - var response OrderType if _, err = c.do(req, &response); err != nil { return OrderType{}, err