Skip to content

Commit

Permalink
OpenOrders and CancelOrder fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
aliereno committed Apr 20, 2020
1 parent 63005ce commit de3ef6a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 29 deletions.
4 changes: 4 additions & 0 deletions btcturk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,7 @@ func (c *Client) clearRequest() {
c.params = url.Values{}
c.body = nil
}

func (c *Client) Version() string {
return "0.0.1"
}
78 changes: 49 additions & 29 deletions btcturk/user_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package btcturk
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
)

const (
Expand Down Expand Up @@ -57,7 +60,7 @@ const (
StopLimitOrder string = "stoplimit"
)

type OpenOrders struct {
type OpenOrderModel struct {
ID int32 `json:"id"`
Price string `json:"price"`
Amount string `json:"amount"`
Expand All @@ -71,6 +74,11 @@ type OpenOrders struct {
Status string `json:"status"`
}

type OpenOrderResult struct {
Asks []OpenOrderModel `json:"asks"`
Bids []OpenOrderModel `json:"bids"`
}

type OrderType struct {
ID string `json:"id"`
DateTime string `json:"datetime"`
Expand All @@ -89,49 +97,27 @@ type OrderInput struct {
PairSymbol string `json:"pairSymbol"`
}

func (c *Client) OpenOrders() ([]OpenOrders, error) {
func (c *Client) OpenOrders() ([]OpenOrderResult, error) {
jsonString, err := json.Marshal(c.params)
if err != nil {
return []OpenOrders{}, err
return []OpenOrderResult{}, err
}
req, err := c.newRequest("GET", "/api/v1/openOrders", bytes.NewBuffer(jsonString))
if err != nil {
return []OpenOrders{}, err
return []OpenOrderResult{}, err
}
if err := c.auth(req); err != nil {
return []OpenOrders{}, err
return []OpenOrderResult{}, err
}

var response []OpenOrders
var response []OpenOrderResult
if _, err = c.do(req, &response); err != nil {
return []OpenOrders{}, err
return []OpenOrderResult{}, err
}

return response, nil
}

func (c *Client) CancelOrder() (bool, error) {
req, err := c.newRequest("DELETE", fmt.Sprintf("/api/v1/order?%s", c.params.Encode()), c.body)
if err != nil {
return false, err
}
if err := c.auth(req); err != nil {
return false, err
}

var response GeneralResponse

// TODO : solve
// 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
}

return response.Success, nil
}

func (c *Client) Buy(o *OrderInput) (OrderType, error) {
o.OrderType = "buy"
jsonString, err := json.Marshal(o)
Expand Down Expand Up @@ -177,3 +163,37 @@ func (c *Client) Sell(o *OrderInput) (OrderType, error) {

return response, nil
}

func (c *Client) CancelOrder() (bool, error) {
req, err := c.newRequest("DELETE", fmt.Sprintf("/api/v1/order?%s", c.params.Encode()), c.body)
if err != nil {
return false, err
}
if err := c.auth(req); err != nil {
return false, err
}

resp, err := c.client.Do(req)
if err != nil {
return false, err
}

defer func() {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
c.clearRequest()
}()

var response = &GeneralResponse{}

if json.NewDecoder(resp.Body).Decode(response) != nil {
return false, err
}

if response.Success == true {
return true, nil
} else {
return false, errors.New(*response.Message)
}

}

0 comments on commit de3ef6a

Please sign in to comment.