fix huobib/binance api bug (#129)

This commit is contained in:
Frank
2018-06-04 17:20:05 +08:00
committed by Adrian Gallagher
parent 4903c788b1
commit 0478c55b45
5 changed files with 69 additions and 28 deletions

View File

@@ -38,6 +38,7 @@ const (
priceChange = "/api/v1/ticker/24hr"
symbolPrice = "/api/v3/ticker/price"
bestPrice = "/api/v3/ticker/bookTicker"
accountInfo = "/api/v3/account"
// Authenticated endpoints
newOrderTest = "/api/v3/order/test"
@@ -442,6 +443,29 @@ func (b *Binance) QueryOrder(symbol, origClientOrderID string, orderID int64) (Q
return resp, nil
}
// GetAccount returns binance user accounts
func (b *Binance) GetAccount() (*Account, error) {
type respone struct {
Response
Account
}
var resp respone
path := fmt.Sprintf("%s%s", apiURL, accountInfo)
params := url.Values{}
if err := b.SendAuthHTTPRequest("GET", path, params, &resp); err != nil {
return &resp.Account, err
}
if resp.Code != 0 {
return &resp.Account, errors.New(resp.Msg)
}
return &resp.Account, nil
}
// SendHTTPRequest sends an unauthenticated request
func (b *Binance) SendHTTPRequest(path string, result interface{}) error {
return b.SendPayload("GET", path, nil, nil, result, false, b.Verbose)
@@ -471,8 +495,9 @@ func (b *Binance) SendAuthHTTPRequest(method, path string, params url.Values, re
if b.Verbose {
log.Printf("sent path: \n%s\n", path)
}
path = common.EncodeURLValues(path, params)
return b.SendPayload(method, path, headers, bytes.NewBufferString(params.Encode()), result, true, b.Verbose)
return b.SendPayload(method, path, headers, bytes.NewBufferString(""), result, true, b.Verbose)
}
// CheckLimit checks value against a variable list

View File

@@ -1,5 +1,10 @@
package binance
type Response struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
// ExchangeInfo holds the full exchange information type
type ExchangeInfo struct {
Code int `json:"code"`
@@ -202,3 +207,23 @@ type QueryOrderData struct {
Time int64 `json:"time"`
IsWorking bool `json:"isWorking"`
}
// QueryOrderData holds query order data
type Blance struct {
Asset string `json:"asset"`
Free string `json:"free"`
Locked string `json:"locked"`
}
// AccountInfo holds the account data
type Account struct {
MakerCommission int `json:"makerCommission"`
TakerCommission int `json:"takerCommission"`
BuyerCommission int `json:"buyerCommission"`
SellerCommission int `json:"sellerCommission"`
CanTrade bool `json:"canTrade"`
CanWithdraw bool `json:"canWithdraw"`
CanDeposit bool `json:"canDeposit"`
UpdateTime int64 `json:"updateTime"`
Blances []Blance `json:"balances"`
}

View File

@@ -110,7 +110,7 @@ func (h *HUOBI) GetFee() float64 {
}
// GetKline returns kline data
func (h *HUOBI) GetKline(symbol, period, size string) ([]Klines, error) {
func (h *HUOBI) GetKline(symbol, period, size string) ([]KlineItem, error) {
vals := url.Values{}
vals.Set("symbol", symbol)
@@ -124,7 +124,7 @@ func (h *HUOBI) GetKline(symbol, period, size string) ([]Klines, error) {
type response struct {
Response
Data []Klines `json:"data"`
Data []KlineItem `json:"data"`
}
var result response
@@ -315,10 +315,10 @@ func (h *HUOBI) GetAccounts() ([]Account, error) {
}
// GetAccountBalance returns the users Huobi account balance
func (h *HUOBI) GetAccountBalance(accountID string) ([]AccountBalance, error) {
func (h *HUOBI) GetAccountBalance(accountID string) ([]AccountBalanceDetail, error) {
type response struct {
Response
AccountData []AccountBalance `json:"list"`
AccountBalanceData AccountBalance `json:"data"`
}
var result response
@@ -328,7 +328,7 @@ func (h *HUOBI) GetAccountBalance(accountID string) ([]AccountBalance, error) {
if result.ErrorMessage != "" {
return nil, errors.New(result.ErrorMessage)
}
return result.AccountData, err
return result.AccountBalanceData.AccountBalanceDetails, err
}
// PlaceOrder submits an order to Huobi

View File

@@ -21,11 +21,6 @@ type KlineItem struct {
Count int `json:"count"`
}
// Klines stores tan array of kline items
type Klines struct {
Klines []KlineItem `json:"data"`
}
// DetailMerged stores the ticker detail merged data
type DetailMerged struct {
Detail
@@ -88,8 +83,16 @@ type Account struct {
UserID int64 `json:"user-id"`
}
// AccountBalance stores the user account balance
// AccountBalance stores the user all account balance
type AccountBalance struct {
ID int64 `json:"id"`
Type string `json:"type"`
State string `json:"state"`
AccountBalanceDetails []AccountBalanceDetail `json:"list"`
}
// AccountBalanceDetail stores the user account balance
type AccountBalanceDetail struct {
Currency string `json:"currency"`
Type string `json:"type"`
Balance float64 `json:"balance,string"`

View File

@@ -207,14 +207,7 @@ func (r *Requester) DoRequest(req *http.Request, method, path string, headers ma
log.Printf("%s exchange request path: %s", r.Name, path)
}
var resp *http.Response
var err error
if method != "GET" {
resp, err = r.HTTPClient.Do(req)
} else {
resp, err = r.HTTPClient.Get(path)
}
resp, err := r.HTTPClient.Do(req)
if err != nil {
if r.RequiresRateLimiter() {
@@ -261,14 +254,9 @@ func (r *Requester) SendPayload(method, path string, headers map[string]string,
return errors.New("invalid path")
}
var req *http.Request
var err error
if method != "GET" {
req, err = r.checkRequest(method, path, body, headers)
if err != nil {
return err
}
req, err := r.checkRequest(method, path, body, headers)
if err != nil {
return err
}
if !r.RequiresRateLimiter() {