Skip to content

Commit

Permalink
Fixed linter issues for HitBTC exchange.
Browse files Browse the repository at this point in the history
  • Loading branch information
shazbert committed Feb 26, 2018
1 parent aef0b5a commit 9a04d06
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 146 deletions.
162 changes: 75 additions & 87 deletions exchanges/hitbtc/hitbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@ import (

const (
// API
APIURL = "https://api.hitbtc.com"
apiURL = "https://api.hitbtc.com"

// Public
APIv2Trades = "api/2/public/trades"
APIv2Currency = "api/2/public/currency"
APIv2Symbol = "api/2/public/symbol"
APIv2Ticker = "api/2/public/ticker"
APIv2OrderBook = "api/2/public/orderbook"
APIv2Candles = "api/2/public/candles"
apiV2Trades = "api/2/public/trades"
apiV2Currency = "api/2/public/currency"
apiV2Symbol = "api/2/public/symbol"
apiV2Ticker = "api/2/public/ticker"
apiV2Orderbook = "api/2/public/orderbook"
apiV2Candles = "api/2/public/candles"

// Authenticated
APIv2Balance = "api/2/trading/balance"
APIv2CryptoAddress = "api/2/account/crypto/address"
APIv2CryptoWithdraw = "api/2/account/crypto/withdraw"
APIv2TradeHistory = "api/2/history/trades"
APIv2FeeInfo = "api/2/trading/fee"
Orders = "order"
OrderBuy = "buy"
OrderSell = "sell"
OrderCancel = "cancelOrder"
OrderMove = "moveOrder"
TradableBalances = "returnTradableBalances"
TransferBalance = "transferBalance"
apiV2Balance = "api/2/trading/balance"
apiV2CryptoAddress = "api/2/account/crypto/address"
apiV2CryptoWithdraw = "api/2/account/crypto/withdraw"
apiV2TradeHistory = "api/2/history/trades"
apiV2FeeInfo = "api/2/trading/fee"
orders = "order"
orderBuy = "buy"
orderSell = "sell"
orderCancel = "cancelOrder"
orderMove = "moveOrder"
tradableBalances = "returnTradableBalances"
transferBalance = "transferBalance"
)

// HitBTC is the overarching type across the hitbtc package
Expand Down Expand Up @@ -94,36 +94,35 @@ func (p *HitBTC) GetFee() float64 {
// Public Market Data
// https://api.hitbtc.com/?python#market-data

// GetCurrencies
// Return the actual list of available currencies, tokens, ICO etc.
// GetCurrencies returns the actual list of available currencies, tokens, ICO
// etc.
func (p *HitBTC) GetCurrencies(currency string) (map[string]Currencies, error) {
type Response struct {
Data []Currencies
}
resp := Response{}
path := fmt.Sprintf("%s/%s/%s", APIURL, APIv2Currency, currency)
path := fmt.Sprintf("%s/%s/%s", apiURL, apiV2Currency, currency)
err := common.SendHTTPGetRequest(path, true, p.Verbose, &resp.Data)
ret := make(map[string]Currencies)
for _, id := range resp.Data {
ret[id.Id] = id
ret[id.ID] = id
}

return ret, err
}

// GetSymbols
// Return the actual list of currency symbols (currency pairs) traded on HitBTC exchange.
// The first listed currency of a symbol is called the base currency, and the second currency
// is called the quote currency. The currency pair indicates how much of the quote currency
// is needed to purchase one unit of the base currency.
// GetSymbols Return the actual list of currency symbols (currency pairs) traded
// on HitBTC exchange. The first listed currency of a symbol is called the base
// currency, and the second currency is called the quote currency. The currency
// pair indicates how much of the quote currency is needed to purchase one unit
// of the base currency.
func (p *HitBTC) GetSymbols(symbol string) ([]string, error) {

resp := []Symbol{}
path := fmt.Sprintf("%s/%s/%s", APIURL, APIv2Symbol, symbol)
path := fmt.Sprintf("%s/%s/%s", apiURL, apiV2Symbol, symbol)
err := common.SendHTTPGetRequest(path, true, p.Verbose, &resp)
ret := make([]string, 0, len(resp))
for _, x := range resp {
ret = append(ret, x.Id)
ret = append(ret, x.ID)
}

return ret, err
Expand All @@ -133,19 +132,17 @@ func (p *HitBTC) GetSymbols(symbol string) ([]string, error) {
// all ther details.
func (p *HitBTC) GetSymbolsDetailed() ([]Symbol, error) {
resp := []Symbol{}
path := fmt.Sprintf("%s/%s", APIURL, APIv2Symbol)
path := fmt.Sprintf("%s/%s", apiURL, apiV2Symbol)
return resp, common.SendHTTPGetRequest(path, true, p.Verbose, &resp)
}

// GetTicker
// Return ticker information
// GetTicker returns ticker information
func (p *HitBTC) GetTicker(symbol string) (map[string]Ticker, error) {

resp1 := []TickerResponse{}
resp2 := TickerResponse{}
ret := make(map[string]TickerResponse)
result := make(map[string]Ticker)
path := fmt.Sprintf("%s/%s/%s", APIURL, APIv2Ticker, symbol)
path := fmt.Sprintf("%s/%s/%s", apiURL, apiV2Ticker, symbol)
var err error

if symbol == "" {
Expand Down Expand Up @@ -236,14 +233,13 @@ func (p *HitBTC) GetTrades(currencyPair, from, till, limit, offset, by, sort str
}

resp := []TradeHistory{}
path := fmt.Sprintf("%s/%s/%s?%s", APIURL, APIv2Trades, currencyPair, vals.Encode())
path := fmt.Sprintf("%s/%s/%s?%s", apiURL, apiV2Trades, currencyPair, vals.Encode())

return resp, common.SendHTTPGetRequest(path, true, p.Verbose, &resp)
}

// GetOrderbook
// An order book is an electronic list of buy and sell orders for a specific
// symbol, organized by price level.
// GetOrderbook an order book is an electronic list of buy and sell orders for a
// specific symbol, organized by price level.
func (p *HitBTC) GetOrderbook(currencyPair string, limit int) (Orderbook, error) {
// limit Limit of orderbook levels, default 100. Set 0 to view full orderbook levels
vals := url.Values{}
Expand All @@ -253,7 +249,7 @@ func (p *HitBTC) GetOrderbook(currencyPair string, limit int) (Orderbook, error)
}

resp := OrderbookResponse{}
path := fmt.Sprintf("%s/%s/%s?%s", APIURL, APIv2OrderBook, currencyPair, vals.Encode())
path := fmt.Sprintf("%s/%s/%s?%s", apiURL, apiV2Orderbook, currencyPair, vals.Encode())

err := common.SendHTTPGetRequest(path, true, p.Verbose, &resp)
if err != nil {
Expand All @@ -271,8 +267,7 @@ func (p *HitBTC) GetOrderbook(currencyPair string, limit int) (Orderbook, error)
return ob, nil
}

// GetCandles
// A candles used for OHLC a specific symbol.
// GetCandles returns candles which is used for OHLC a specific symbol.
// Note: Result contain candles only with non zero volume.
func (p *HitBTC) GetCandles(currencyPair, limit, period string) ([]ChartData, error) {
// limit Limit of candles, default 100.
Expand All @@ -288,24 +283,18 @@ func (p *HitBTC) GetCandles(currencyPair, limit, period string) ([]ChartData, er
}

resp := []ChartData{}
path := fmt.Sprintf("%s/%s/%s?%s", APIURL, APIv2Candles, currencyPair, vals.Encode())

err := common.SendHTTPGetRequest(path, true, p.Verbose, &resp)
if err != nil {
return nil, err
}
path := fmt.Sprintf("%s/%s/%s?%s", apiURL, apiV2Candles, currencyPair, vals.Encode())

return resp, nil
return resp, common.SendHTTPGetRequest(path, true, p.Verbose, &resp)
}

// Authenticated Market Data
// https://api.hitbtc.com/?python#market-data

// GetBalances
// GetBalances returns full balance for your account
func (p *HitBTC) GetBalances() (map[string]Balance, error) {

result := []Balance{}
err := p.SendAuthenticatedHTTPRequest("GET", APIv2Balance, url.Values{}, &result)
err := p.SendAuthenticatedHTTPRequest("GET", apiV2Balance, url.Values{}, &result)
ret := make(map[string]Balance)

if err != nil {
Expand All @@ -319,30 +308,31 @@ func (p *HitBTC) GetBalances() (map[string]Balance, error) {
return ret, nil
}

// GetDepositAddresses returns a deposit address for a specific currency
func (p *HitBTC) GetDepositAddresses(currency string) (DepositCryptoAddresses, error) {
resp := DepositCryptoAddresses{}
err := p.SendAuthenticatedHTTPRequest("GET", APIv2CryptoAddress+"/"+currency, url.Values{}, &resp)
err := p.SendAuthenticatedHTTPRequest("GET", apiV2CryptoAddress+"/"+currency, url.Values{}, &resp)

return resp, err
}

// GenerateNewAddress generates a new deposit address for a currency
func (p *HitBTC) GenerateNewAddress(currency string) (DepositCryptoAddresses, error) {

resp := DepositCryptoAddresses{}
err := p.SendAuthenticatedHTTPRequest("POST", APIv2CryptoAddress+"/"+currency, url.Values{}, &resp)
err := p.SendAuthenticatedHTTPRequest("POST", apiV2CryptoAddress+"/"+currency, url.Values{}, &resp)

return resp, err
}

// Get Active orders
func (p *HitBTC) GetActiveOrders(currency string) ([]Order, error) {

// GetActiveorders returns all your active orders
func (p *HitBTC) GetActiveorders(currency string) ([]Order, error) {
resp := []Order{}
err := p.SendAuthenticatedHTTPRequest("GET", Orders+"?symbol="+currency, url.Values{}, &resp)
err := p.SendAuthenticatedHTTPRequest("GET", orders+"?symbol="+currency, url.Values{}, &resp)

return resp, err
}

// GetAuthenticatedTradeHistory returns your trade history
func (p *HitBTC) GetAuthenticatedTradeHistory(currency, start, end string) (interface{}, error) {
values := url.Values{}

Expand All @@ -357,35 +347,26 @@ func (p *HitBTC) GetAuthenticatedTradeHistory(currency, start, end string) (inte
if currency != "" && currency != "all" {
values.Set("currencyPair", currency)
result := AuthenticatedTradeHistoryResponse{}
err := p.SendAuthenticatedHTTPRequest("POST", APIv2TradeHistory, values, &result.Data)

if err != nil {
return result, err
}

return result, nil
} else {
values.Set("currencyPair", "all")
result := AuthenticatedTradeHistoryAll{}
err := p.SendAuthenticatedHTTPRequest("POST", APIv2TradeHistory, values, &result.Data)
return result, p.SendAuthenticatedHTTPRequest("POST", apiV2TradeHistory, values, &result.Data)
}

if err != nil {
return result, err
}
values.Set("currencyPair", "all")
result := AuthenticatedTradeHistoryAll{}

return result, nil
}
return result, p.SendAuthenticatedHTTPRequest("POST", apiV2TradeHistory, values, &result.Data)
}

// PlaceOrder places an order on the exchange
func (p *HitBTC) PlaceOrder(currency string, rate, amount float64, immediate, fillOrKill, buy bool) (OrderResponse, error) {
result := OrderResponse{}
values := url.Values{}

var orderType string
if buy {
orderType = OrderBuy
orderType = orderBuy
} else {
orderType = OrderSell
orderType = orderSell
}

values.Set("currencyPair", currency)
Expand All @@ -409,12 +390,13 @@ func (p *HitBTC) PlaceOrder(currency string, rate, amount float64, immediate, fi
return result, nil
}

// CancelOrder cancels a specific order by OrderID
func (p *HitBTC) CancelOrder(orderID int64) (bool, error) {
result := GenericResponse{}
values := url.Values{}
values.Set("orderNumber", strconv.FormatInt(orderID, 10))

err := p.SendAuthenticatedHTTPRequest("POST", OrderCancel, values, &result)
err := p.SendAuthenticatedHTTPRequest("POST", orderCancel, values, &result)

if err != nil {
return false, err
Expand All @@ -427,6 +409,7 @@ func (p *HitBTC) CancelOrder(orderID int64) (bool, error) {
return true, nil
}

// MoveOrder generates a new move order
func (p *HitBTC) MoveOrder(orderID int64, rate, amount float64) (MoveOrderResponse, error) {
result := MoveOrderResponse{}
values := url.Values{}
Expand All @@ -437,7 +420,7 @@ func (p *HitBTC) MoveOrder(orderID int64, rate, amount float64) (MoveOrderRespon
values.Set("amount", strconv.FormatFloat(amount, 'f', -1, 64))
}

err := p.SendAuthenticatedHTTPRequest("POST", OrderMove, values, &result)
err := p.SendAuthenticatedHTTPRequest("POST", orderMove, values, &result)

if err != nil {
return result, err
Expand All @@ -450,6 +433,7 @@ func (p *HitBTC) MoveOrder(orderID int64, rate, amount float64) (MoveOrderRespon
return result, nil
}

// Withdraw allows for the withdrawal to a specific address
func (p *HitBTC) Withdraw(currency, address string, amount float64) (bool, error) {
result := Withdraw{}
values := url.Values{}
Expand All @@ -458,7 +442,7 @@ func (p *HitBTC) Withdraw(currency, address string, amount float64) (bool, error
values.Set("amount", strconv.FormatFloat(amount, 'f', -1, 64))
values.Set("address", address)

err := p.SendAuthenticatedHTTPRequest("POST", APIv2CryptoWithdraw, values, &result)
err := p.SendAuthenticatedHTTPRequest("POST", apiV2CryptoWithdraw, values, &result)

if err != nil {
return false, err
Expand All @@ -471,20 +455,22 @@ func (p *HitBTC) Withdraw(currency, address string, amount float64) (bool, error
return true, nil
}

// GetFeeInfo returns current fee information
func (p *HitBTC) GetFeeInfo(currencyPair string) (Fee, error) {
result := Fee{}
err := p.SendAuthenticatedHTTPRequest("GET", APIv2FeeInfo+"/"+currencyPair, url.Values{}, &result)
err := p.SendAuthenticatedHTTPRequest("GET", apiV2FeeInfo+"/"+currencyPair, url.Values{}, &result)

return result, err
}

// GetTradableBalances returns current tradable balances
func (p *HitBTC) GetTradableBalances() (map[string]map[string]float64, error) {
type Response struct {
Data map[string]map[string]interface{}
}
result := Response{}

err := p.SendAuthenticatedHTTPRequest("POST", TradableBalances, url.Values{}, &result.Data)
err := p.SendAuthenticatedHTTPRequest("POST", tradableBalances, url.Values{}, &result.Data)

if err != nil {
return nil, err
Expand All @@ -502,6 +488,7 @@ func (p *HitBTC) GetTradableBalances() (map[string]map[string]float64, error) {
return balances, nil
}

// TransferBalance transfers a balance
func (p *HitBTC) TransferBalance(currency, from, to string, amount float64) (bool, error) {
values := url.Values{}
result := GenericResponse{}
Expand All @@ -511,7 +498,7 @@ func (p *HitBTC) TransferBalance(currency, from, to string, amount float64) (boo
values.Set("fromAccount", from)
values.Set("toAccount", to)

err := p.SendAuthenticatedHTTPRequest("POST", TransferBalance, values, &result)
err := p.SendAuthenticatedHTTPRequest("POST", transferBalance, values, &result)

if err != nil {
return false, err
Expand All @@ -524,24 +511,25 @@ func (p *HitBTC) TransferBalance(currency, from, to string, amount float64) (boo
return true, nil
}

// SendAuthenticatedHTTPRequest sends an authenticated http request
func (p *HitBTC) SendAuthenticatedHTTPRequest(method, endpoint string, values url.Values, result interface{}) error {
if !p.AuthenticatedAPISupport {
return fmt.Errorf(exchange.WarningAuthenticatedRequestWithoutCredentialsSet, p.Name)
}
headers := make(map[string]string)
headers["Authorization"] = "Basic " + common.Base64Encode([]byte(p.APIKey+":"+p.APISecret))

path := fmt.Sprintf("%s/%s", APIURL, endpoint)
resp, err := common.SendHTTPRequest(method, path, headers, bytes.NewBufferString(values.Encode()))
path := fmt.Sprintf("%s/%s", apiURL, endpoint)

resp, err := common.SendHTTPRequest(method, path, headers, bytes.NewBufferString(values.Encode()))
if err != nil {
return err
}

err = common.JSONDecode([]byte(resp), &result)

if err != nil {
return errors.New("Unable to JSON Unmarshal response.")
return err
}

return nil
}
Loading

0 comments on commit 9a04d06

Please sign in to comment.