mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-17 07:26:48 +00:00
579 lines
17 KiB
Go
579 lines
17 KiB
Go
package zb
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/common"
|
|
"github.com/thrasher-corp/gocryptotrader/config"
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/websocket/wshandler"
|
|
log "github.com/thrasher-corp/gocryptotrader/logger"
|
|
)
|
|
|
|
// GetDefaultConfig returns a default exchange config
|
|
func (z *ZB) GetDefaultConfig() (*config.ExchangeConfig, error) {
|
|
z.SetDefaults()
|
|
exchCfg := new(config.ExchangeConfig)
|
|
exchCfg.Name = z.Name
|
|
exchCfg.HTTPTimeout = exchange.DefaultHTTPTimeout
|
|
exchCfg.BaseCurrencies = z.BaseCurrencies
|
|
|
|
err := z.SetupDefaults(exchCfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if z.Features.Supports.RESTCapabilities.AutoPairUpdates {
|
|
err = z.UpdateTradablePairs(true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return exchCfg, nil
|
|
}
|
|
|
|
// SetDefaults sets default values for the exchange
|
|
func (z *ZB) SetDefaults() {
|
|
z.Name = "ZB"
|
|
z.Enabled = true
|
|
z.Verbose = true
|
|
z.API.CredentialsValidator.RequiresKey = true
|
|
z.API.CredentialsValidator.RequiresSecret = true
|
|
|
|
z.CurrencyPairs = currency.PairsManager{
|
|
AssetTypes: asset.Items{
|
|
asset.Spot,
|
|
},
|
|
|
|
UseGlobalFormat: true,
|
|
RequestFormat: ¤cy.PairFormat{
|
|
Delimiter: "_",
|
|
},
|
|
ConfigFormat: ¤cy.PairFormat{
|
|
Delimiter: "_",
|
|
Uppercase: true,
|
|
},
|
|
}
|
|
|
|
z.Features = exchange.Features{
|
|
Supports: exchange.FeaturesSupported{
|
|
REST: true,
|
|
Websocket: true,
|
|
RESTCapabilities: exchange.ProtocolFeatures{
|
|
AutoPairUpdates: true,
|
|
TickerBatching: true,
|
|
},
|
|
WithdrawPermissions: exchange.AutoWithdrawCrypto |
|
|
exchange.NoFiatWithdrawals,
|
|
},
|
|
Enabled: exchange.FeaturesEnabled{
|
|
AutoPairUpdates: true,
|
|
},
|
|
}
|
|
|
|
z.Requester = request.New(z.Name,
|
|
request.NewRateLimit(time.Second*10, zbAuthRate),
|
|
request.NewRateLimit(time.Second*10, zbUnauthRate),
|
|
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout))
|
|
|
|
z.API.Endpoints.URLDefault = zbTradeURL
|
|
z.API.Endpoints.URL = z.API.Endpoints.URLDefault
|
|
z.API.Endpoints.URLSecondaryDefault = zbMarketURL
|
|
z.API.Endpoints.URLSecondary = z.API.Endpoints.URLSecondaryDefault
|
|
z.API.Endpoints.WebsocketURL = zbWebsocketAPI
|
|
z.Websocket = wshandler.New()
|
|
z.Websocket.Functionality = wshandler.WebsocketTickerSupported |
|
|
wshandler.WebsocketOrderbookSupported |
|
|
wshandler.WebsocketTradeDataSupported |
|
|
wshandler.WebsocketSubscribeSupported |
|
|
wshandler.WebsocketAuthenticatedEndpointsSupported |
|
|
wshandler.WebsocketAccountDataSupported |
|
|
wshandler.WebsocketCancelOrderSupported |
|
|
wshandler.WebsocketSubmitOrderSupported |
|
|
wshandler.WebsocketMessageCorrelationSupported
|
|
z.WebsocketResponseMaxLimit = exchange.DefaultWebsocketResponseMaxLimit
|
|
z.WebsocketResponseCheckTimeout = exchange.DefaultWebsocketResponseCheckTimeout
|
|
}
|
|
|
|
// Setup sets user configuration
|
|
func (z *ZB) Setup(exch *config.ExchangeConfig) error {
|
|
if !exch.Enabled {
|
|
z.SetEnabled(false)
|
|
return nil
|
|
}
|
|
|
|
err := z.SetupDefaults(exch)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = z.Websocket.Setup(z.WsConnect,
|
|
z.Subscribe,
|
|
nil,
|
|
exch.Name,
|
|
exch.Features.Enabled.Websocket,
|
|
exch.Verbose,
|
|
zbWebsocketAPI,
|
|
exch.API.Endpoints.WebsocketURL,
|
|
exch.API.AuthenticatedWebsocketSupport)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
z.WebsocketConn = &wshandler.WebsocketConnection{
|
|
ExchangeName: z.Name,
|
|
URL: z.Websocket.GetWebsocketURL(),
|
|
ProxyURL: z.Websocket.GetProxyAddress(),
|
|
Verbose: z.Verbose,
|
|
RateLimit: zbWebsocketRateLimit,
|
|
ResponseCheckTimeout: exch.WebsocketResponseCheckTimeout,
|
|
ResponseMaxLimit: exch.WebsocketResponseMaxLimit,
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Start starts the OKEX go routine
|
|
func (z *ZB) Start(wg *sync.WaitGroup) {
|
|
wg.Add(1)
|
|
go func() {
|
|
z.Run()
|
|
wg.Done()
|
|
}()
|
|
}
|
|
|
|
// Run implements the OKEX wrapper
|
|
func (z *ZB) Run() {
|
|
if z.Verbose {
|
|
z.PrintEnabledPairs()
|
|
}
|
|
|
|
if !z.GetEnabledFeatures().AutoPairUpdates {
|
|
return
|
|
}
|
|
|
|
err := z.UpdateTradablePairs(false)
|
|
if err != nil {
|
|
log.Errorf(log.ExchangeSys, "%s failed to update tradable pairs. Err: %s", z.Name, err)
|
|
}
|
|
}
|
|
|
|
// FetchTradablePairs returns a list of the exchanges tradable pairs
|
|
func (z *ZB) FetchTradablePairs(asset asset.Item) ([]string, error) {
|
|
markets, err := z.GetMarkets()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var currencies []string
|
|
for x := range markets {
|
|
currencies = append(currencies, x)
|
|
}
|
|
|
|
return currencies, nil
|
|
}
|
|
|
|
// UpdateTradablePairs updates the exchanges available pairs and stores
|
|
// them in the exchanges config
|
|
func (z *ZB) UpdateTradablePairs(forceUpdate bool) error {
|
|
pairs, err := z.FetchTradablePairs(asset.Spot)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return z.UpdatePairs(currency.NewPairsFromStrings(pairs), asset.Spot, false, forceUpdate)
|
|
}
|
|
|
|
// UpdateTicker updates and returns the ticker for a currency pair
|
|
func (z *ZB) UpdateTicker(p currency.Pair, assetType asset.Item) (ticker.Price, error) {
|
|
var tickerPrice ticker.Price
|
|
|
|
result, err := z.GetTickers()
|
|
if err != nil {
|
|
return tickerPrice, err
|
|
}
|
|
|
|
for _, x := range z.GetEnabledPairs(assetType) {
|
|
// We can't use either pair format here, so format it to lower-
|
|
// case and without any delimiter
|
|
curr := x.Format("", false).String()
|
|
if _, ok := result[curr]; !ok {
|
|
continue
|
|
}
|
|
var tp ticker.Price
|
|
tp.Pair = x
|
|
tp.High = result[curr].High
|
|
tp.Last = result[curr].Last
|
|
tp.Ask = result[curr].Sell
|
|
tp.Bid = result[curr].Buy
|
|
tp.Low = result[curr].Low
|
|
tp.Volume = result[curr].Volume
|
|
|
|
err = ticker.ProcessTicker(z.Name, &tp, assetType)
|
|
if err != nil {
|
|
log.Error(log.Ticker, err)
|
|
}
|
|
}
|
|
|
|
return ticker.GetTicker(z.Name, p, assetType)
|
|
}
|
|
|
|
// FetchTicker returns the ticker for a currency pair
|
|
func (z *ZB) FetchTicker(p currency.Pair, assetType asset.Item) (ticker.Price, error) {
|
|
tickerNew, err := ticker.GetTicker(z.GetName(), p, assetType)
|
|
if err != nil {
|
|
return z.UpdateTicker(p, assetType)
|
|
}
|
|
return tickerNew, nil
|
|
}
|
|
|
|
// FetchOrderbook returns orderbook base on the currency pair
|
|
func (z *ZB) FetchOrderbook(p currency.Pair, assetType asset.Item) (orderbook.Base, error) {
|
|
ob, err := orderbook.Get(z.GetName(), p, assetType)
|
|
if err != nil {
|
|
return z.UpdateOrderbook(p, assetType)
|
|
}
|
|
return ob, nil
|
|
}
|
|
|
|
// UpdateOrderbook updates and returns the orderbook for a currency pair
|
|
func (z *ZB) UpdateOrderbook(p currency.Pair, assetType asset.Item) (orderbook.Base, error) {
|
|
var orderBook orderbook.Base
|
|
currency := z.FormatExchangeCurrency(p, assetType).String()
|
|
|
|
orderbookNew, err := z.GetOrderbook(currency)
|
|
if err != nil {
|
|
return orderBook, err
|
|
}
|
|
|
|
for x := range orderbookNew.Bids {
|
|
data := orderbookNew.Bids[x]
|
|
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: data[1], Price: data[0]})
|
|
}
|
|
|
|
for x := range orderbookNew.Asks {
|
|
data := orderbookNew.Asks[x]
|
|
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: data[1], Price: data[0]})
|
|
}
|
|
|
|
orderBook.Pair = p
|
|
orderBook.AssetType = assetType
|
|
orderBook.ExchangeName = z.GetName()
|
|
|
|
err = orderBook.Process()
|
|
if err != nil {
|
|
return orderBook, err
|
|
}
|
|
|
|
return orderbook.Get(z.Name, p, assetType)
|
|
}
|
|
|
|
// GetAccountInfo retrieves balances for all enabled currencies for the
|
|
// ZB exchange
|
|
func (z *ZB) GetAccountInfo() (exchange.AccountInfo, error) {
|
|
var info exchange.AccountInfo
|
|
bal, err := z.GetAccountInformation()
|
|
if err != nil {
|
|
return info, err
|
|
}
|
|
|
|
var balances []exchange.AccountCurrencyInfo
|
|
for _, data := range bal.Result.Coins {
|
|
hold, err := strconv.ParseFloat(data.Freez, 64)
|
|
if err != nil {
|
|
return info, err
|
|
}
|
|
|
|
avail, err := strconv.ParseFloat(data.Available, 64)
|
|
if err != nil {
|
|
return info, err
|
|
}
|
|
|
|
balances = append(balances, exchange.AccountCurrencyInfo{
|
|
CurrencyName: currency.NewCode(data.EnName),
|
|
TotalValue: hold + avail,
|
|
Hold: hold,
|
|
})
|
|
}
|
|
|
|
info.Exchange = z.GetName()
|
|
info.Accounts = append(info.Accounts, exchange.Account{
|
|
Currencies: balances,
|
|
})
|
|
|
|
return info, nil
|
|
}
|
|
|
|
// GetFundingHistory returns funding history, deposits and
|
|
// withdrawals
|
|
func (z *ZB) GetFundingHistory() ([]exchange.FundHistory, error) {
|
|
var fundHistory []exchange.FundHistory
|
|
return fundHistory, common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// GetExchangeHistory returns historic trade data since exchange opening.
|
|
func (z *ZB) GetExchangeHistory(p currency.Pair, assetType asset.Item) ([]exchange.TradeHistory, error) {
|
|
return nil, common.ErrNotYetImplemented
|
|
}
|
|
|
|
// SubmitOrder submits a new order
|
|
func (z *ZB) SubmitOrder(order *exchange.OrderSubmission) (exchange.SubmitOrderResponse, error) {
|
|
var submitOrderResponse exchange.SubmitOrderResponse
|
|
if order == nil {
|
|
return submitOrderResponse, exchange.ErrOrderSubmissionIsNil
|
|
}
|
|
|
|
if err := order.Validate(); err != nil {
|
|
return submitOrderResponse, err
|
|
}
|
|
|
|
var oT SpotNewOrderRequestParamsType
|
|
if order.OrderSide == exchange.BuyOrderSide {
|
|
oT = SpotNewOrderRequestParamsTypeBuy
|
|
} else {
|
|
oT = SpotNewOrderRequestParamsTypeSell
|
|
}
|
|
|
|
var params = SpotNewOrderRequestParams{
|
|
Amount: order.Amount,
|
|
Price: order.Price,
|
|
Symbol: order.Pair.Lower().String(),
|
|
Type: oT,
|
|
}
|
|
response, err := z.SpotNewOrder(params)
|
|
if response > 0 {
|
|
submitOrderResponse.OrderID = fmt.Sprintf("%v", response)
|
|
}
|
|
if err == nil {
|
|
submitOrderResponse.IsOrderPlaced = true
|
|
}
|
|
return submitOrderResponse, err
|
|
}
|
|
|
|
// ModifyOrder will allow of changing orderbook placement and limit to
|
|
// market conversion
|
|
func (z *ZB) ModifyOrder(action *exchange.ModifyOrder) (string, error) {
|
|
return "", common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// CancelOrder cancels an order by its corresponding ID number
|
|
func (z *ZB) CancelOrder(order *exchange.OrderCancellation) error {
|
|
orderIDInt, err := strconv.ParseInt(order.OrderID, 10, 64)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return z.CancelExistingOrder(orderIDInt, z.FormatExchangeCurrency(order.CurrencyPair,
|
|
order.AssetType).String())
|
|
}
|
|
|
|
// CancelAllOrders cancels all orders associated with a currency pair
|
|
func (z *ZB) CancelAllOrders(_ *exchange.OrderCancellation) (exchange.CancelAllOrdersResponse, error) {
|
|
cancelAllOrdersResponse := exchange.CancelAllOrdersResponse{
|
|
OrderStatus: make(map[string]string),
|
|
}
|
|
var allOpenOrders []Order
|
|
for _, currency := range z.GetEnabledPairs(asset.Spot) {
|
|
// Limiting to 10 pages
|
|
for i := 0; i < 10; i++ {
|
|
openOrders, err := z.GetUnfinishedOrdersIgnoreTradeType(z.FormatExchangeCurrency(currency, asset.Spot).String(), 1, 10)
|
|
if err != nil {
|
|
return cancelAllOrdersResponse, err
|
|
}
|
|
|
|
if len(openOrders) == 0 {
|
|
break
|
|
}
|
|
|
|
allOpenOrders = append(allOpenOrders, openOrders...)
|
|
}
|
|
}
|
|
|
|
for _, openOrder := range allOpenOrders {
|
|
err := z.CancelExistingOrder(openOrder.ID, openOrder.Currency)
|
|
if err != nil {
|
|
cancelAllOrdersResponse.OrderStatus[strconv.FormatInt(openOrder.ID, 10)] = err.Error()
|
|
}
|
|
}
|
|
|
|
return cancelAllOrdersResponse, nil
|
|
}
|
|
|
|
// GetOrderInfo returns information on a current open order
|
|
func (z *ZB) GetOrderInfo(orderID string) (exchange.OrderDetail, error) {
|
|
var orderDetail exchange.OrderDetail
|
|
return orderDetail, common.ErrNotYetImplemented
|
|
}
|
|
|
|
// GetDepositAddress returns a deposit address for a specified currency
|
|
func (z *ZB) GetDepositAddress(cryptocurrency currency.Code, _ string) (string, error) {
|
|
address, err := z.GetCryptoAddress(cryptocurrency)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return address.Message.Data.Key, nil
|
|
}
|
|
|
|
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is
|
|
// submitted
|
|
func (z *ZB) WithdrawCryptocurrencyFunds(withdrawRequest *exchange.CryptoWithdrawRequest) (string, error) {
|
|
return z.Withdraw(withdrawRequest.Currency.Lower().String(), withdrawRequest.Address, withdrawRequest.TradePassword, withdrawRequest.Amount, withdrawRequest.FeeAmount, false)
|
|
}
|
|
|
|
// WithdrawFiatFunds returns a withdrawal ID when a
|
|
// withdrawal is submitted
|
|
func (z *ZB) WithdrawFiatFunds(withdrawRequest *exchange.FiatWithdrawRequest) (string, error) {
|
|
return "", common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// WithdrawFiatFundsToInternationalBank returns a withdrawal ID when a
|
|
// withdrawal is submitted
|
|
func (z *ZB) WithdrawFiatFundsToInternationalBank(withdrawRequest *exchange.FiatWithdrawRequest) (string, error) {
|
|
return "", common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// GetWebsocket returns a pointer to the exchange websocket
|
|
func (z *ZB) GetWebsocket() (*wshandler.Websocket, error) {
|
|
return z.Websocket, nil
|
|
}
|
|
|
|
// GetFeeByType returns an estimate of fee based on type of transaction
|
|
func (z *ZB) GetFeeByType(feeBuilder *exchange.FeeBuilder) (float64, error) {
|
|
if !z.AllowAuthenticatedRequest() && // Todo check connection status
|
|
feeBuilder.FeeType == exchange.CryptocurrencyTradeFee {
|
|
feeBuilder.FeeType = exchange.OfflineTradeFee
|
|
}
|
|
return z.GetFee(feeBuilder)
|
|
}
|
|
|
|
// GetActiveOrders retrieves any orders that are active/open
|
|
// This function is not concurrency safe due to orderSide/orderType maps
|
|
func (z *ZB) GetActiveOrders(getOrdersRequest *exchange.GetOrdersRequest) ([]exchange.OrderDetail, error) {
|
|
var allOrders []Order
|
|
for _, currency := range getOrdersRequest.Currencies {
|
|
var pageNumber int64
|
|
// Limiting to 10 pages
|
|
for i := 0; i < 10; i++ {
|
|
resp, err := z.GetUnfinishedOrdersIgnoreTradeType(z.FormatExchangeCurrency(currency, asset.Spot).String(), pageNumber, 10)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(resp) == 0 {
|
|
break
|
|
}
|
|
|
|
allOrders = append(allOrders, resp...)
|
|
pageNumber++
|
|
}
|
|
}
|
|
|
|
var orders []exchange.OrderDetail
|
|
for _, order := range allOrders {
|
|
symbol := currency.NewPairDelimiter(order.Currency,
|
|
z.GetPairFormat(asset.Spot, false).Delimiter)
|
|
orderDate := time.Unix(int64(order.TradeDate), 0)
|
|
orderSide := orderSideMap[order.Type]
|
|
orders = append(orders, exchange.OrderDetail{
|
|
ID: fmt.Sprintf("%v", order.ID),
|
|
Amount: order.TotalAmount,
|
|
Exchange: z.Name,
|
|
OrderDate: orderDate,
|
|
Price: order.Price,
|
|
OrderSide: orderSide,
|
|
CurrencyPair: symbol,
|
|
})
|
|
}
|
|
|
|
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks,
|
|
getOrdersRequest.EndTicks)
|
|
exchange.FilterOrdersBySide(&orders, getOrdersRequest.OrderSide)
|
|
return orders, nil
|
|
}
|
|
|
|
// GetOrderHistory retrieves account order information
|
|
// Can Limit response to specific order status
|
|
// This function is not concurrency safe due to orderSide/orderType maps
|
|
func (z *ZB) GetOrderHistory(getOrdersRequest *exchange.GetOrdersRequest) ([]exchange.OrderDetail, error) {
|
|
if getOrdersRequest.OrderSide == exchange.AnyOrderSide || getOrdersRequest.OrderSide == "" {
|
|
return nil, errors.New("specific order side is required")
|
|
}
|
|
|
|
var allOrders []Order
|
|
|
|
var side int64
|
|
if getOrdersRequest.OrderSide == exchange.BuyOrderSide {
|
|
side = 1
|
|
}
|
|
|
|
for _, currency := range getOrdersRequest.Currencies {
|
|
var pageNumber int64
|
|
// Limiting to 10 pages
|
|
for i := 0; i < 10; i++ {
|
|
resp, err := z.GetOrders(z.FormatExchangeCurrency(currency, asset.Spot).String(), pageNumber, side)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(resp) == 0 {
|
|
break
|
|
}
|
|
|
|
allOrders = append(allOrders, resp...)
|
|
pageNumber++
|
|
}
|
|
}
|
|
|
|
var orders []exchange.OrderDetail
|
|
for _, order := range allOrders {
|
|
symbol := currency.NewPairDelimiter(order.Currency,
|
|
z.GetPairFormat(asset.Spot, false).Delimiter)
|
|
orderDate := time.Unix(int64(order.TradeDate), 0)
|
|
orderSide := orderSideMap[order.Type]
|
|
orders = append(orders, exchange.OrderDetail{
|
|
ID: fmt.Sprintf("%v", order.ID),
|
|
Amount: order.TotalAmount,
|
|
Exchange: z.Name,
|
|
OrderDate: orderDate,
|
|
Price: order.Price,
|
|
OrderSide: orderSide,
|
|
CurrencyPair: symbol,
|
|
})
|
|
}
|
|
|
|
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks, getOrdersRequest.EndTicks)
|
|
return orders, nil
|
|
}
|
|
|
|
// SubscribeToWebsocketChannels appends to ChannelsToSubscribe
|
|
// which lets websocket.manageSubscriptions handle subscribing
|
|
func (z *ZB) SubscribeToWebsocketChannels(channels []wshandler.WebsocketChannelSubscription) error {
|
|
z.Websocket.SubscribeToChannels(channels)
|
|
return nil
|
|
}
|
|
|
|
// UnsubscribeToWebsocketChannels removes from ChannelsToSubscribe
|
|
// which lets websocket.manageSubscriptions handle unsubscribing
|
|
func (z *ZB) UnsubscribeToWebsocketChannels(channels []wshandler.WebsocketChannelSubscription) error {
|
|
return common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// GetSubscriptions returns a copied list of subscriptions
|
|
func (z *ZB) GetSubscriptions() ([]wshandler.WebsocketChannelSubscription, error) {
|
|
return z.Websocket.GetSubscriptions(), nil
|
|
}
|
|
|
|
// AuthenticateWebsocket sends an authentication message to the websocket
|
|
func (z *ZB) AuthenticateWebsocket() error {
|
|
return common.ErrFunctionNotSupported
|
|
}
|