mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-01 15:10:44 +00:00
* Basic concept commit * Initial changes to support bitfinex v2. Reverts linter changes as they suck. Exports bitfinex ws types * Adds ticker, trade and orderbook support * Candles sub that returns no data COMPLETE * Adds authenticated ws support * Adds the barebones endpoints to support * Adds more endpoints * Even more endpoints * minicommit to switch and test * All the interactive types * Adds support for simultaneous connections. Updates tests. Nothing is working * Successfully adds place order. Moves all authenticated endpoints to new switch case * Cancel order and modify order * Cancel all orders, cancel multi orders * Finalises implementation. Uses testMain * Adds WS wrapper support for some funcs * Fixing rebasing issues * Replaces use of currency as a variable. Updates a lot of coinut websocket auth endpoint stuff * Fixes some fun for loops with GetEnabledPairs * Fixes tests impacted by currency var change * Adds coinut support for WS functions. Replaces `order` vars with `ord`. Fixes some for loops too. Removes verbose from bitfinex * So many panics * I'm fixing a hole, where the panics get in, and stops my mind from wandering, where it will go * Moves func `CanUseAuthenticatedWebsocketEndpoint` to Websocket package as it fits better. Adds test coverage of `CanUseAuthenticatedWebsocketEndpoint` * Finishes up all of coinuts ws implementations. * GateIO implementation * Adds some helper funcs for types, sides and status. Adds support for huobi. Removes unnecessary type * Adds forgotten huobi endpoint * Fixes cancel order endpoint * go hates my formatting and so do I * The process to get authenticated kraken websocket to work. Uses testmain. Adds new auth channel, auth subscriptions, auth data handling. Not working yet * Finishes open orders handling * Mini update for status only updates * Fixes some kraken points * Finishes WS kraken since it doesn't work * Unfinished commit, cleaning up types * Finishes the const replacing * Fixes extra GetNAmes after rebase * An end to the cleanup. testmain for gateio * Adds ZB support * Bitfinex cleanup. Renamed func * Testmain-47s for everyone!!! yayaaaaaaa * Adds kraken websocket wrapper support * Fixes rebase issues * Fixes tests from rebase * Adds test for conversion. Fixes for loop. Updates test order pricing. Fixes some poor made tests. Adds proper error handling for ws responses instead of logging them. Fixed issue where commented code ruined kraken ws. * Fixes secret linting issues. Prioritises bitfinex channelID responses over authorised * Fixes sloppy error/var declarations * Fixes crazy bad logic where submit order errors weren't really considered. Parralols alphapoint/alphapoint_test.go. Removes buffer for multi-websocket comms channel. * Removal of inline string and removal of redundant nil checkerinos * Fixes err checks. Checks whether float has decimal. Fixes append. Drops omitempties. Parallel to some tests. Moves var declarations * Replaces my lazy sprintfs with strconv.FormatInt(time.Now().Unix(), 10) * Adds shiny new FullyMatched bool. Fixes coinbene buy sell consts * Fixes oopsie with coinbene const replacement * Fixes currency issue * Cleans up new places that use JSONDecode * Fixes huge panic bug from string int conversion. Adds large testtable for strings to order types * Fixes some more strconversion issues. Fixes table test var usage. Changes mapperino name * Added some new scenarios for number splitting * Fixes lint issues * negative num fix * Typo fix * Accuracy warning comment
889 lines
26 KiB
Go
889 lines
26 KiB
Go
package coinut
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"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/order"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/protocol"
|
|
"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 (c *COINUT) GetDefaultConfig() (*config.ExchangeConfig, error) {
|
|
c.SetDefaults()
|
|
exchCfg := new(config.ExchangeConfig)
|
|
exchCfg.Name = c.Name
|
|
exchCfg.HTTPTimeout = exchange.DefaultHTTPTimeout
|
|
exchCfg.BaseCurrencies = c.BaseCurrencies
|
|
|
|
err := c.SetupDefaults(exchCfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if c.Features.Supports.RESTCapabilities.AutoPairUpdates {
|
|
err = c.UpdateTradablePairs(true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return exchCfg, nil
|
|
}
|
|
|
|
// SetDefaults sets current default values
|
|
func (c *COINUT) SetDefaults() {
|
|
c.Name = "COINUT"
|
|
c.Enabled = true
|
|
c.Verbose = true
|
|
c.API.CredentialsValidator.RequiresKey = true
|
|
c.API.CredentialsValidator.RequiresClientID = true
|
|
|
|
c.CurrencyPairs = currency.PairsManager{
|
|
AssetTypes: asset.Items{
|
|
asset.Spot,
|
|
},
|
|
UseGlobalFormat: true,
|
|
RequestFormat: ¤cy.PairFormat{
|
|
Uppercase: true,
|
|
},
|
|
ConfigFormat: ¤cy.PairFormat{
|
|
Uppercase: true,
|
|
Delimiter: "-",
|
|
},
|
|
}
|
|
|
|
c.Features = exchange.Features{
|
|
Supports: exchange.FeaturesSupported{
|
|
REST: true,
|
|
Websocket: true,
|
|
RESTCapabilities: protocol.Features{
|
|
TickerFetching: true,
|
|
TradeFetching: true,
|
|
OrderbookFetching: true,
|
|
AutoPairUpdates: true,
|
|
AccountInfo: true,
|
|
GetOrders: true,
|
|
CancelOrders: true,
|
|
CancelOrder: true,
|
|
SubmitOrder: true,
|
|
SubmitOrders: true,
|
|
UserTradeHistory: true,
|
|
TradeFee: true,
|
|
FiatDepositFee: true,
|
|
FiatWithdrawalFee: true,
|
|
},
|
|
WebsocketCapabilities: protocol.Features{
|
|
AccountBalance: true,
|
|
GetOrders: true,
|
|
CancelOrders: true,
|
|
CancelOrder: true,
|
|
SubmitOrder: true,
|
|
SubmitOrders: true,
|
|
UserTradeHistory: true,
|
|
TickerFetching: true,
|
|
TradeFetching: true,
|
|
OrderbookFetching: true,
|
|
AccountInfo: true,
|
|
Subscribe: true,
|
|
Unsubscribe: true,
|
|
AuthenticatedEndpoints: true,
|
|
MessageCorrelation: true,
|
|
},
|
|
WithdrawPermissions: exchange.WithdrawCryptoViaWebsiteOnly |
|
|
exchange.WithdrawFiatViaWebsiteOnly,
|
|
},
|
|
Enabled: exchange.FeaturesEnabled{
|
|
AutoPairUpdates: true,
|
|
},
|
|
}
|
|
|
|
c.Requester = request.New(c.Name,
|
|
request.NewRateLimit(time.Second, coinutAuthRate),
|
|
request.NewRateLimit(time.Second, coinutUnauthRate),
|
|
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout))
|
|
|
|
c.API.Endpoints.URLDefault = coinutAPIURL
|
|
c.API.Endpoints.URL = c.API.Endpoints.URLDefault
|
|
c.API.Endpoints.WebsocketURL = coinutWebsocketURL
|
|
c.Websocket = wshandler.New()
|
|
c.WebsocketResponseMaxLimit = exchange.DefaultWebsocketResponseMaxLimit
|
|
c.WebsocketResponseCheckTimeout = exchange.DefaultWebsocketResponseCheckTimeout
|
|
c.WebsocketOrderbookBufferLimit = exchange.DefaultWebsocketOrderbookBufferLimit
|
|
}
|
|
|
|
// Setup sets the current exchange configuration
|
|
func (c *COINUT) Setup(exch *config.ExchangeConfig) error {
|
|
if !exch.Enabled {
|
|
c.SetEnabled(false)
|
|
return nil
|
|
}
|
|
|
|
err := c.SetupDefaults(exch)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = c.Websocket.Setup(
|
|
&wshandler.WebsocketSetup{
|
|
Enabled: exch.Features.Enabled.Websocket,
|
|
Verbose: exch.Verbose,
|
|
AuthenticatedWebsocketAPISupport: exch.API.AuthenticatedWebsocketSupport,
|
|
WebsocketTimeout: exch.WebsocketTrafficTimeout,
|
|
DefaultURL: coinutWebsocketURL,
|
|
ExchangeName: exch.Name,
|
|
RunningURL: exch.API.Endpoints.WebsocketURL,
|
|
Connector: c.WsConnect,
|
|
Subscriber: c.Subscribe,
|
|
UnSubscriber: c.Unsubscribe,
|
|
Features: &c.Features.Supports.WebsocketCapabilities,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
c.WebsocketConn = &wshandler.WebsocketConnection{
|
|
ExchangeName: c.Name,
|
|
URL: c.Websocket.GetWebsocketURL(),
|
|
ProxyURL: c.Websocket.GetProxyAddress(),
|
|
Verbose: c.Verbose,
|
|
ResponseCheckTimeout: exch.WebsocketResponseCheckTimeout,
|
|
ResponseMaxLimit: exch.WebsocketResponseMaxLimit,
|
|
}
|
|
|
|
c.Websocket.Orderbook.Setup(
|
|
exch.WebsocketOrderbookBufferLimit,
|
|
true,
|
|
true,
|
|
true,
|
|
false,
|
|
exch.Name)
|
|
return nil
|
|
}
|
|
|
|
// Start starts the COINUT go routine
|
|
func (c *COINUT) Start(wg *sync.WaitGroup) {
|
|
wg.Add(1)
|
|
go func() {
|
|
c.Run()
|
|
wg.Done()
|
|
}()
|
|
}
|
|
|
|
// Run implements the COINUT wrapper
|
|
func (c *COINUT) Run() {
|
|
if c.Verbose {
|
|
log.Debugf(log.ExchangeSys, "%s Websocket: %s. (url: %s).\n", c.Name, common.IsEnabled(c.Websocket.IsEnabled()), coinutWebsocketURL)
|
|
c.PrintEnabledPairs()
|
|
}
|
|
|
|
forceUpdate := false
|
|
delim := c.GetPairFormat(asset.Spot, false).Delimiter
|
|
if !common.StringDataContains(c.CurrencyPairs.GetPairs(asset.Spot,
|
|
true).Strings(), delim) ||
|
|
!common.StringDataContains(c.CurrencyPairs.GetPairs(asset.Spot,
|
|
false).Strings(), delim) {
|
|
enabledPairs := currency.NewPairsFromStrings(
|
|
[]string{fmt.Sprintf("LTC%sUSDT", delim)},
|
|
)
|
|
log.Warn(log.ExchangeSys,
|
|
"Enabled pairs for Coinut reset due to config upgrade, please enable the ones you would like to use again")
|
|
forceUpdate = true
|
|
|
|
err := c.UpdatePairs(enabledPairs, asset.Spot, true, true)
|
|
if err != nil {
|
|
log.Errorf(log.ExchangeSys, "%s failed to update currencies. Err: %s\n", c.Name, err)
|
|
}
|
|
}
|
|
|
|
if !c.GetEnabledFeatures().AutoPairUpdates && !forceUpdate {
|
|
return
|
|
}
|
|
|
|
err := c.UpdateTradablePairs(forceUpdate)
|
|
if err != nil {
|
|
log.Errorf(log.ExchangeSys, "%s failed to update tradable pairs. Err: %s", c.Name, err)
|
|
}
|
|
}
|
|
|
|
// FetchTradablePairs returns a list of the exchanges tradable pairs
|
|
func (c *COINUT) FetchTradablePairs(asset asset.Item) ([]string, error) {
|
|
var instruments map[string][]InstrumentBase
|
|
var resp Instruments
|
|
var err error
|
|
if c.Websocket.IsConnected() {
|
|
resp, err = c.WsGetInstruments()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
} else {
|
|
resp, err = c.GetInstruments()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
instruments = resp.Instruments
|
|
var pairs []string
|
|
for i := range instruments {
|
|
c.instrumentMap.Seed(instruments[i][0].Base+instruments[i][0].Quote, instruments[i][0].InstID)
|
|
p := instruments[i][0].Base + c.GetPairFormat(asset, false).Delimiter + instruments[i][0].Quote
|
|
pairs = append(pairs, p)
|
|
}
|
|
|
|
return pairs, nil
|
|
}
|
|
|
|
// UpdateTradablePairs updates the exchanges available pairs and stores
|
|
// them in the exchanges config
|
|
func (c *COINUT) UpdateTradablePairs(forceUpdate bool) error {
|
|
pairs, err := c.FetchTradablePairs(asset.Spot)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.UpdatePairs(currency.NewPairsFromStrings(pairs),
|
|
asset.Spot, false, forceUpdate)
|
|
}
|
|
|
|
// GetAccountInfo retrieves balances for all enabled currencies for the
|
|
// COINUT exchange
|
|
func (c *COINUT) GetAccountInfo() (exchange.AccountInfo, error) {
|
|
var info exchange.AccountInfo
|
|
var bal *UserBalance
|
|
var err error
|
|
if c.Websocket.CanUseAuthenticatedWebsocketForWrapper() {
|
|
var resp *UserBalance
|
|
resp, err = c.wsGetAccountBalance()
|
|
if err != nil {
|
|
return info, err
|
|
}
|
|
bal = resp
|
|
} else {
|
|
bal, err = c.GetUserBalance()
|
|
if err != nil {
|
|
return info, err
|
|
}
|
|
}
|
|
|
|
var balances = []exchange.AccountCurrencyInfo{
|
|
{
|
|
CurrencyName: currency.BCH,
|
|
TotalValue: bal.BCH,
|
|
},
|
|
{
|
|
CurrencyName: currency.BTC,
|
|
TotalValue: bal.BTC,
|
|
},
|
|
{
|
|
CurrencyName: currency.BTG,
|
|
TotalValue: bal.BTG,
|
|
},
|
|
{
|
|
CurrencyName: currency.CAD,
|
|
TotalValue: bal.CAD,
|
|
},
|
|
{
|
|
CurrencyName: currency.ETC,
|
|
TotalValue: bal.ETC,
|
|
},
|
|
{
|
|
CurrencyName: currency.ETH,
|
|
TotalValue: bal.ETH,
|
|
},
|
|
{
|
|
CurrencyName: currency.LCH,
|
|
TotalValue: bal.LCH,
|
|
},
|
|
{
|
|
CurrencyName: currency.LTC,
|
|
TotalValue: bal.LTC,
|
|
},
|
|
{
|
|
CurrencyName: currency.MYR,
|
|
TotalValue: bal.MYR,
|
|
},
|
|
{
|
|
CurrencyName: currency.SGD,
|
|
TotalValue: bal.SGD,
|
|
},
|
|
{
|
|
CurrencyName: currency.USD,
|
|
TotalValue: bal.USD,
|
|
},
|
|
{
|
|
CurrencyName: currency.USDT,
|
|
TotalValue: bal.USDT,
|
|
},
|
|
{
|
|
CurrencyName: currency.XMR,
|
|
TotalValue: bal.XMR,
|
|
},
|
|
{
|
|
CurrencyName: currency.ZEC,
|
|
TotalValue: bal.ZEC,
|
|
},
|
|
}
|
|
info.Exchange = c.Name
|
|
info.Accounts = append(info.Accounts, exchange.Account{
|
|
Currencies: balances,
|
|
})
|
|
|
|
return info, nil
|
|
}
|
|
|
|
// UpdateTicker updates and returns the ticker for a currency pair
|
|
func (c *COINUT) UpdateTicker(p currency.Pair, assetType asset.Item) (ticker.Price, error) {
|
|
var tickerPrice ticker.Price
|
|
err := c.loadInstrumentsIfNotLoaded()
|
|
if err != nil {
|
|
return tickerPrice, err
|
|
}
|
|
|
|
instID := c.instrumentMap.LookupID(c.FormatExchangeCurrency(p,
|
|
assetType).String())
|
|
if instID == 0 {
|
|
return tickerPrice, errors.New("unable to lookup instrument ID")
|
|
}
|
|
var tick Ticker
|
|
tick, err = c.GetInstrumentTicker(instID)
|
|
if err != nil {
|
|
return tickerPrice, err
|
|
}
|
|
tickerPrice = ticker.Price{
|
|
Last: tick.Last,
|
|
High: tick.High24,
|
|
Low: tick.Low24,
|
|
Bid: tick.HighestBuy,
|
|
Ask: tick.LowestSell,
|
|
Volume: tick.Volume24,
|
|
Pair: p,
|
|
LastUpdated: time.Unix(0, tick.Timestamp),
|
|
}
|
|
err = ticker.ProcessTicker(c.Name, &tickerPrice, assetType)
|
|
if err != nil {
|
|
return tickerPrice, err
|
|
}
|
|
|
|
return ticker.GetTicker(c.Name, p, assetType)
|
|
}
|
|
|
|
// FetchTicker returns the ticker for a currency pair
|
|
func (c *COINUT) FetchTicker(p currency.Pair, assetType asset.Item) (ticker.Price, error) {
|
|
tickerNew, err := ticker.GetTicker(c.Name, p, assetType)
|
|
if err != nil {
|
|
return c.UpdateTicker(p, assetType)
|
|
}
|
|
return tickerNew, nil
|
|
}
|
|
|
|
// FetchOrderbook returns orderbook base on the currency pair
|
|
func (c *COINUT) FetchOrderbook(p currency.Pair, assetType asset.Item) (orderbook.Base, error) {
|
|
ob, err := orderbook.Get(c.Name, p, assetType)
|
|
if err != nil {
|
|
return c.UpdateOrderbook(p, assetType)
|
|
}
|
|
return ob, nil
|
|
}
|
|
|
|
// UpdateOrderbook updates and returns the orderbook for a currency pair
|
|
func (c *COINUT) UpdateOrderbook(p currency.Pair, assetType asset.Item) (orderbook.Base, error) {
|
|
var orderBook orderbook.Base
|
|
err := c.loadInstrumentsIfNotLoaded()
|
|
if err != nil {
|
|
return orderBook, err
|
|
}
|
|
|
|
instID := c.instrumentMap.LookupID(c.FormatExchangeCurrency(p,
|
|
assetType).String())
|
|
if instID == 0 {
|
|
return orderBook, errLookupInstrumentID
|
|
}
|
|
|
|
orderbookNew, err := c.GetInstrumentOrderbook(instID, 200)
|
|
if err != nil {
|
|
return orderBook, err
|
|
}
|
|
|
|
for x := range orderbookNew.Buy {
|
|
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: orderbookNew.Buy[x].Quantity, Price: orderbookNew.Buy[x].Price})
|
|
}
|
|
|
|
for x := range orderbookNew.Sell {
|
|
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: orderbookNew.Sell[x].Quantity, Price: orderbookNew.Sell[x].Price})
|
|
}
|
|
|
|
orderBook.Pair = p
|
|
orderBook.ExchangeName = c.Name
|
|
orderBook.AssetType = assetType
|
|
|
|
err = orderBook.Process()
|
|
if err != nil {
|
|
return orderBook, err
|
|
}
|
|
|
|
return orderbook.Get(c.Name, p, assetType)
|
|
}
|
|
|
|
// GetFundingHistory returns funding history, deposits and
|
|
// withdrawals
|
|
func (c *COINUT) GetFundingHistory() ([]exchange.FundHistory, error) {
|
|
return nil, common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// GetExchangeHistory returns historic trade data since exchange opening.
|
|
func (c *COINUT) GetExchangeHistory(p currency.Pair, assetType asset.Item) ([]exchange.TradeHistory, error) {
|
|
return nil, common.ErrNotYetImplemented
|
|
}
|
|
|
|
// SubmitOrder submits a new order
|
|
func (c *COINUT) SubmitOrder(o *order.Submit) (order.SubmitResponse, error) {
|
|
var submitOrderResponse order.SubmitResponse
|
|
var err error
|
|
if _, err = strconv.Atoi(o.ClientID); err != nil {
|
|
return submitOrderResponse, fmt.Errorf("%s - ClientID must be a number, received: %s", c.Name, o.ClientID)
|
|
}
|
|
err = o.Validate()
|
|
|
|
if err != nil {
|
|
return submitOrderResponse, err
|
|
}
|
|
|
|
if c.Websocket.CanUseAuthenticatedWebsocketForWrapper() {
|
|
var response *WsStandardOrderResponse
|
|
response, err = c.wsSubmitOrder(&WsSubmitOrderParameters{
|
|
Currency: o.Pair,
|
|
Side: o.OrderSide,
|
|
Amount: o.Amount,
|
|
Price: o.Price,
|
|
})
|
|
if err != nil {
|
|
return submitOrderResponse, err
|
|
}
|
|
submitOrderResponse.OrderID = strconv.FormatInt(response.OrderID, 10)
|
|
submitOrderResponse.IsOrderPlaced = true
|
|
} else {
|
|
err = c.loadInstrumentsIfNotLoaded()
|
|
if err != nil {
|
|
return submitOrderResponse, err
|
|
}
|
|
|
|
currencyID := c.instrumentMap.LookupID(c.FormatExchangeCurrency(o.Pair,
|
|
asset.Spot).String())
|
|
if currencyID == 0 {
|
|
return submitOrderResponse, errLookupInstrumentID
|
|
}
|
|
|
|
var APIResponse interface{}
|
|
var clientIDInt uint64
|
|
isBuyOrder := o.OrderSide == order.Buy
|
|
clientIDInt, err = strconv.ParseUint(o.ClientID, 0, 32)
|
|
if err != nil {
|
|
return submitOrderResponse, err
|
|
}
|
|
clientIDUint := uint32(clientIDInt)
|
|
APIResponse, err = c.NewOrder(currencyID, o.Amount, o.Price,
|
|
isBuyOrder, clientIDUint)
|
|
if err != nil {
|
|
return submitOrderResponse, err
|
|
}
|
|
responseMap := APIResponse.(map[string]interface{})
|
|
switch responseMap["reply"].(string) {
|
|
case "order_rejected":
|
|
return submitOrderResponse, fmt.Errorf("clientOrderID: %v was rejected: %v", o.ClientID, responseMap["reasons"])
|
|
case "order_filled":
|
|
orderID := responseMap["order_id"].(float64)
|
|
submitOrderResponse.OrderID = strconv.FormatFloat(orderID, 'f', -1, 64)
|
|
submitOrderResponse.IsOrderPlaced = true
|
|
submitOrderResponse.FullyMatched = true
|
|
return submitOrderResponse, nil
|
|
case "order_accepted":
|
|
orderID := responseMap["order_id"].(float64)
|
|
submitOrderResponse.OrderID = strconv.FormatFloat(orderID, 'f', -1, 64)
|
|
submitOrderResponse.IsOrderPlaced = true
|
|
return submitOrderResponse, nil
|
|
}
|
|
}
|
|
return submitOrderResponse, nil
|
|
}
|
|
|
|
// ModifyOrder will allow of changing orderbook placement and limit to
|
|
// market conversion
|
|
func (c *COINUT) ModifyOrder(action *order.Modify) (string, error) {
|
|
return "", common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// CancelOrder cancels an order by its corresponding ID number
|
|
func (c *COINUT) CancelOrder(o *order.Cancel) error {
|
|
err := c.loadInstrumentsIfNotLoaded()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
orderIDInt, err := strconv.ParseInt(o.OrderID, 10, 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
currencyID := c.instrumentMap.LookupID(c.FormatExchangeCurrency(
|
|
o.CurrencyPair,
|
|
asset.Spot).String(),
|
|
)
|
|
if c.Websocket.CanUseAuthenticatedWebsocketForWrapper() {
|
|
var resp *CancelOrdersResponse
|
|
resp, err = c.wsCancelOrder(&WsCancelOrderParameters{
|
|
Currency: o.CurrencyPair,
|
|
OrderID: orderIDInt,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(resp.Status) >= 1 && resp.Status[0] != "OK" {
|
|
return errors.New(c.Name + " - Failed to cancel order " + o.OrderID)
|
|
}
|
|
} else {
|
|
if currencyID == 0 {
|
|
return errLookupInstrumentID
|
|
}
|
|
_, err = c.CancelExistingOrder(currencyID, orderIDInt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// CancelAllOrders cancels all orders associated with a currency pair
|
|
func (c *COINUT) CancelAllOrders(details *order.Cancel) (order.CancelAllResponse, error) {
|
|
var cancelAllOrdersResponse order.CancelAllResponse
|
|
err := c.loadInstrumentsIfNotLoaded()
|
|
if err != nil {
|
|
return cancelAllOrdersResponse, err
|
|
}
|
|
cancelAllOrdersResponse.Status = make(map[string]string)
|
|
if c.Websocket.CanUseAuthenticatedWebsocketForWrapper() {
|
|
openOrders, err := c.wsGetOpenOrders(details.CurrencyPair.String())
|
|
if err != nil {
|
|
return cancelAllOrdersResponse, err
|
|
}
|
|
var ordersToCancel []WsCancelOrderParameters
|
|
for i := range openOrders.Orders {
|
|
if openOrders.Orders[i].InstID == c.instrumentMap.LookupID(c.FormatExchangeCurrency(details.CurrencyPair, asset.Spot).String()) {
|
|
ordersToCancel = append(ordersToCancel, WsCancelOrderParameters{
|
|
Currency: details.CurrencyPair,
|
|
OrderID: openOrders.Orders[i].OrderID,
|
|
})
|
|
}
|
|
}
|
|
resp, err := c.wsCancelOrders(ordersToCancel)
|
|
if err != nil {
|
|
return cancelAllOrdersResponse, err
|
|
}
|
|
for i := range resp.Results {
|
|
if openOrders.Orders[i].Status[0] != "OK" {
|
|
cancelAllOrdersResponse.Status[strconv.FormatInt(openOrders.Orders[i].OrderID, 10)] = strings.Join(openOrders.Orders[i].Status, ",")
|
|
}
|
|
}
|
|
} else {
|
|
var allTheOrders []OrderResponse
|
|
ids := c.instrumentMap.GetInstrumentIDs()
|
|
for x := range ids {
|
|
if ids[x] == c.instrumentMap.LookupID(c.FormatExchangeCurrency(details.CurrencyPair, asset.Spot).String()) {
|
|
openOrders, err := c.GetOpenOrders(ids[x])
|
|
if err != nil {
|
|
return cancelAllOrdersResponse, err
|
|
}
|
|
allTheOrders = append(allTheOrders, openOrders.Orders...)
|
|
}
|
|
}
|
|
|
|
var allTheOrdersToCancel []CancelOrders
|
|
for i := range allTheOrders {
|
|
cancelOrder := CancelOrders{
|
|
InstrumentID: allTheOrders[i].InstrumentID,
|
|
OrderID: allTheOrders[i].OrderID,
|
|
}
|
|
allTheOrdersToCancel = append(allTheOrdersToCancel, cancelOrder)
|
|
}
|
|
|
|
if len(allTheOrdersToCancel) > 0 {
|
|
resp, err := c.CancelOrders(allTheOrdersToCancel)
|
|
if err != nil {
|
|
return cancelAllOrdersResponse, err
|
|
}
|
|
|
|
for i := range resp.Results {
|
|
if resp.Results[i].Status != "OK" {
|
|
cancelAllOrdersResponse.Status[strconv.FormatInt(resp.Results[i].OrderID, 10)] = resp.Results[i].Status
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return cancelAllOrdersResponse, nil
|
|
}
|
|
|
|
// GetOrderInfo returns information on a current open order
|
|
func (c *COINUT) GetOrderInfo(orderID string) (order.Detail, error) {
|
|
return order.Detail{}, common.ErrNotYetImplemented
|
|
}
|
|
|
|
// GetDepositAddress returns a deposit address for a specified currency
|
|
func (c *COINUT) GetDepositAddress(cryptocurrency currency.Code, accountID string) (string, error) {
|
|
return "", common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is
|
|
// submitted
|
|
func (c *COINUT) WithdrawCryptocurrencyFunds(withdrawRequest *exchange.CryptoWithdrawRequest) (string, error) {
|
|
return "", common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// WithdrawFiatFunds returns a withdrawal ID when a
|
|
// withdrawal is submitted
|
|
func (c *COINUT) WithdrawFiatFunds(withdrawRequest *exchange.FiatWithdrawRequest) (string, error) {
|
|
return "", common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// WithdrawFiatFundsToInternationalBank returns a withdrawal ID when a
|
|
// withdrawal is submitted
|
|
func (c *COINUT) WithdrawFiatFundsToInternationalBank(withdrawRequest *exchange.FiatWithdrawRequest) (string, error) {
|
|
return "", common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// GetWebsocket returns a pointer to the exchange websocket
|
|
func (c *COINUT) GetWebsocket() (*wshandler.Websocket, error) {
|
|
return c.Websocket, nil
|
|
}
|
|
|
|
// GetFeeByType returns an estimate of fee based on type of transaction
|
|
func (c *COINUT) GetFeeByType(feeBuilder *exchange.FeeBuilder) (float64, error) {
|
|
if !c.AllowAuthenticatedRequest() && // Todo check connection status
|
|
feeBuilder.FeeType == exchange.CryptocurrencyTradeFee {
|
|
feeBuilder.FeeType = exchange.OfflineTradeFee
|
|
}
|
|
return c.GetFee(feeBuilder)
|
|
}
|
|
|
|
// GetActiveOrders retrieves any orders that are active/open
|
|
func (c *COINUT) GetActiveOrders(req *order.GetOrdersRequest) ([]order.Detail, error) {
|
|
err := c.loadInstrumentsIfNotLoaded()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var orders []order.Detail
|
|
var currenciesToCheck []string
|
|
if len(req.Currencies) == 0 {
|
|
for i := range req.Currencies {
|
|
currenciesToCheck = append(currenciesToCheck, c.FormatExchangeCurrency(req.Currencies[i], asset.Spot).String())
|
|
}
|
|
} else {
|
|
for k := range c.instrumentMap.Instruments {
|
|
currenciesToCheck = append(currenciesToCheck, k)
|
|
}
|
|
}
|
|
if c.Websocket.CanUseAuthenticatedWebsocketForWrapper() {
|
|
for x := range currenciesToCheck {
|
|
openOrders, err := c.wsGetOpenOrders(currenciesToCheck[x])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for i := range openOrders.Orders {
|
|
orders = append(orders, order.Detail{
|
|
Exchange: c.Name,
|
|
ID: strconv.FormatInt(openOrders.Orders[i].OrderID, 10),
|
|
CurrencyPair: c.FormatExchangeCurrency(currency.NewPairFromString(currenciesToCheck[x]), asset.Spot),
|
|
OrderSide: order.Side(openOrders.Orders[i].Side),
|
|
OrderDate: time.Unix(0, openOrders.Orders[i].Timestamp),
|
|
Status: order.Active,
|
|
Price: openOrders.Orders[i].Price,
|
|
Amount: openOrders.Orders[i].Qty,
|
|
ExecutedAmount: openOrders.Orders[i].Qty - openOrders.Orders[i].OpenQty,
|
|
RemainingAmount: openOrders.Orders[i].OpenQty,
|
|
})
|
|
}
|
|
}
|
|
} else {
|
|
var instrumentsToUse []int64
|
|
if len(req.Currencies) > 0 {
|
|
for x := range req.Currencies {
|
|
curr := c.FormatExchangeCurrency(req.Currencies[x],
|
|
asset.Spot).String()
|
|
instrumentsToUse = append(instrumentsToUse,
|
|
c.instrumentMap.LookupID(curr))
|
|
}
|
|
} else {
|
|
instrumentsToUse = c.instrumentMap.GetInstrumentIDs()
|
|
}
|
|
|
|
if len(instrumentsToUse) == 0 {
|
|
return nil, errors.New("no instrument IDs to use")
|
|
}
|
|
|
|
for x := range instrumentsToUse {
|
|
openOrders, err := c.GetOpenOrders(instrumentsToUse[x])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for y := range openOrders.Orders {
|
|
curr := c.instrumentMap.LookupInstrument(instrumentsToUse[x])
|
|
p := currency.NewPairFromFormattedPairs(curr,
|
|
c.GetEnabledPairs(asset.Spot),
|
|
c.GetPairFormat(asset.Spot, true))
|
|
orderSide := order.Side(strings.ToUpper(openOrders.Orders[y].Side))
|
|
orderDate := time.Unix(openOrders.Orders[y].Timestamp, 0)
|
|
orders = append(orders, order.Detail{
|
|
ID: strconv.FormatInt(openOrders.Orders[y].OrderID, 10),
|
|
Amount: openOrders.Orders[y].Quantity,
|
|
Price: openOrders.Orders[y].Price,
|
|
Exchange: c.Name,
|
|
OrderSide: orderSide,
|
|
OrderDate: orderDate,
|
|
CurrencyPair: p,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
order.FilterOrdersByTickRange(&orders, req.StartTicks, req.EndTicks)
|
|
order.FilterOrdersBySide(&orders, req.OrderSide)
|
|
return orders, nil
|
|
}
|
|
|
|
// GetOrderHistory retrieves account order information
|
|
// Can Limit response to specific order status
|
|
func (c *COINUT) GetOrderHistory(req *order.GetOrdersRequest) ([]order.Detail, error) {
|
|
err := c.loadInstrumentsIfNotLoaded()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var allOrders []order.Detail
|
|
if c.Websocket.CanUseAuthenticatedWebsocketForWrapper() {
|
|
for i := range req.Currencies {
|
|
for j := int64(0); ; j += 100 {
|
|
trades, err := c.wsGetTradeHistory(req.Currencies[i], j, 100)
|
|
if err != nil {
|
|
return allOrders, err
|
|
}
|
|
for x := range trades.Trades {
|
|
curr := c.instrumentMap.LookupInstrument(trades.Trades[x].InstID)
|
|
allOrders = append(allOrders, order.Detail{
|
|
Exchange: c.Name,
|
|
ID: strconv.FormatInt(trades.Trades[x].OrderID, 10),
|
|
CurrencyPair: currency.NewPairFromString(curr),
|
|
OrderSide: order.Side(trades.Trades[x].Side),
|
|
OrderDate: time.Unix(0, trades.Trades[x].Timestamp),
|
|
Status: order.Filled,
|
|
Price: trades.Trades[x].Price,
|
|
Amount: trades.Trades[x].Qty,
|
|
ExecutedAmount: trades.Trades[x].Qty,
|
|
RemainingAmount: trades.Trades[x].OpenQty,
|
|
})
|
|
}
|
|
if len(trades.Trades) < 100 {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
var instrumentsToUse []int64
|
|
if len(req.Currencies) > 0 {
|
|
for x := range req.Currencies {
|
|
curr := c.FormatExchangeCurrency(req.Currencies[x],
|
|
asset.Spot).String()
|
|
instrumentID := c.instrumentMap.LookupID(curr)
|
|
if instrumentID > 0 {
|
|
instrumentsToUse = append(instrumentsToUse, instrumentID)
|
|
}
|
|
}
|
|
} else {
|
|
instrumentsToUse = c.instrumentMap.GetInstrumentIDs()
|
|
}
|
|
|
|
if len(instrumentsToUse) == 0 {
|
|
return nil, errors.New("no instrument IDs to use")
|
|
}
|
|
for x := range instrumentsToUse {
|
|
orders, err := c.GetTradeHistory(instrumentsToUse[x], -1, -1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for y := range orders.Trades {
|
|
curr := c.instrumentMap.LookupInstrument(instrumentsToUse[x])
|
|
p := currency.NewPairFromFormattedPairs(curr,
|
|
c.GetEnabledPairs(asset.Spot),
|
|
c.GetPairFormat(asset.Spot, true))
|
|
orderSide := order.Side(strings.ToUpper(orders.Trades[y].Order.Side))
|
|
orderDate := time.Unix(orders.Trades[y].Order.Timestamp, 0)
|
|
allOrders = append(allOrders, order.Detail{
|
|
ID: strconv.FormatInt(orders.Trades[y].Order.OrderID, 10),
|
|
Amount: orders.Trades[y].Order.Quantity,
|
|
Price: orders.Trades[y].Order.Price,
|
|
Exchange: c.Name,
|
|
OrderSide: orderSide,
|
|
OrderDate: orderDate,
|
|
CurrencyPair: p,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
order.FilterOrdersByTickRange(&allOrders, req.StartTicks, req.EndTicks)
|
|
order.FilterOrdersBySide(&allOrders, req.OrderSide)
|
|
return allOrders, nil
|
|
}
|
|
|
|
// SubscribeToWebsocketChannels appends to ChannelsToSubscribe
|
|
// which lets websocket.manageSubscriptions handle subscribing
|
|
func (c *COINUT) SubscribeToWebsocketChannels(channels []wshandler.WebsocketChannelSubscription) error {
|
|
c.Websocket.SubscribeToChannels(channels)
|
|
return nil
|
|
}
|
|
|
|
// UnsubscribeToWebsocketChannels removes from ChannelsToSubscribe
|
|
// which lets websocket.manageSubscriptions handle unsubscribing
|
|
func (c *COINUT) UnsubscribeToWebsocketChannels(channels []wshandler.WebsocketChannelSubscription) error {
|
|
c.Websocket.RemoveSubscribedChannels(channels)
|
|
return nil
|
|
}
|
|
|
|
// GetSubscriptions returns a copied list of subscriptions
|
|
func (c *COINUT) GetSubscriptions() ([]wshandler.WebsocketChannelSubscription, error) {
|
|
return c.Websocket.GetSubscriptions(), nil
|
|
}
|
|
|
|
// AuthenticateWebsocket sends an authentication message to the websocket
|
|
func (c *COINUT) AuthenticateWebsocket() error {
|
|
return c.wsAuthenticate()
|
|
}
|
|
|
|
func (c *COINUT) loadInstrumentsIfNotLoaded() error {
|
|
if !c.instrumentMap.IsLoaded() {
|
|
if c.Websocket.IsConnected() {
|
|
_, err := c.WsGetInstruments()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
err := c.SeedInstruments()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|