mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-24 07:26:47 +00:00
* Initial commit * Successful authenticated request implementation. * Removes data from okcoin, okex. Implements some account okgroup endpoints. Adds tests * Finishes account API endpoint implementations. * Implements and adds tests for the following okgroup v3 API endpoints: GetSpotTradingAccounts, GetSpotTradingAccountForCurrency, GetSpotBillDetailsForCurrency, PlaceSpotOrder, PlaceMultipleSpotOrders, CancelSpotOrder, CancelMultipleSpotOrders, GetSpotOrders, GetSpotOpenOrders, GetSpotOrder, GetSpotTransactionDetails, GetSpotTokenPairDetails, GetSpotOrderBook, GetSpotAllTokenPairsInformation, GetSpotAllTokenPairsInformationForCurrency, GetSpotFilledOrdersInformation, GetSpotMarketData * Implements and adds tests for all margin api endpoints: GetMarginTradingAccounts, GetMarginTradingAccountsForCurrency, GetMarginBillDetails, GetMarginAccountSettings, GetMarginLoanHistory, OpenMarginLoan, RepayMarginLoan, PlaceMarginOrder, PlaceMultipleMarginOrders, CancelMarginOrder, CancelMultipleMarginOrders, GetMarginOrders, GetMarginOpenOrders, GetMarginOrder, GetMarginTransactionDetails. Simplifies some Spot endpoints combining ForCurrency funcs where possible * Adds all 29 Futures endpoints with tests. Updates comments and naming conventions. Adds standard realordertest func. Adds ability to make public API requests. Adds test purpose comments * Adds 29 endpoints for SWAP API support. Adds tests for each endpoint. Declares response variables in function declaration. Simplifies URL parameter formatting * Adds all ETT endpoints with tests * Creates OKCoin and OKEX struct types. Moves okgroup tests to okcoin and okex exchanges. Updates withdraw fee calculation. Updates exchange.go exchange declaration to point to new types. Streamlines wrapper tests. Begins websocket integration * Rebase fixes * Deletes okcoin_types.go, okcoin_wrapper.go, okex_types.go, okex_wrapper.go. Combines okex,okcoin wrappers in okgroup_wrapper.go. Removes boilerplate url.values with new request struct type parsing. Adds github.com/google/go-querystring to go modules. Replaces USDT with USD for OKCoin tests. Moves OKEX specific endpoints (futures, swap & ett) to okex.go. Fixes recieving receiving again * Maps the wrapper * Parses json into time.Time instead of string + conversion * Renames websocket.SetEnabled to websocket.SetWsStatusAndConnection. Maps main spot websocket functions for okgroup. Adds some basic ws tests * Updates websocket default URLS, adds checksum tests, removes setdefaults from okgroup, adds WebsocketDataWrapper to wrap all okgroup websocket data responses, handles spot, swap, index and futures ticker, candle, trade, orderbook, funding fee websocket responses. Partially implements checksum validation on orderbook data. Fixes all linting issues * Handles the calculation of okgroup websocket checksums. Adds tests * Now all orderbook checksums are validated. Cleans up implementations and removes invalid checksum calculator functions. Adds function to parse ordertype. Puts verbose logs behind verbose check * Removes parallel from okgroup tests. Removes unused code. Adds GetWsChannelWithoutOrderType. Improves handling of WS data types. Adds types for more ws channels. Simplifies update orderbook handling * updates btse func name * linting * Fixes websocket connection issue with tests. Removes test verbosity * Updates checksum calculation to handle more than 7 decimal places. Adds rate limiters. Uses != "" instead of len > 0. Adds new test to handle checksum calculation with 8 decimal places. * Removes logging. Fixes orderbook wrapper references * Adds slightly more robust resubscribe func. Adds websocket tests that can detect websocket errors * Fixes linting issues * Adds new WS func IsConnected() to expose ws status. Tests protect against channel timeout * Adds test comments. Fixes parallel issues for futures tests * Fixes error output for wrapper function
412 lines
13 KiB
Go
412 lines
13 KiB
Go
package okgroup
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/thrasher-/gocryptotrader/common"
|
|
"github.com/thrasher-/gocryptotrader/currency/pair"
|
|
exchange "github.com/thrasher-/gocryptotrader/exchanges"
|
|
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
|
|
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
|
log "github.com/thrasher-/gocryptotrader/logger"
|
|
)
|
|
|
|
// Note: GoCryptoTrader wrapper funcs currently only support SPOT trades.
|
|
// Therefore this OKGroup_Wrapper can be shared between OKEX and OKCoin.
|
|
// When circumstances change, wrapper funcs can be split appropriately
|
|
|
|
// Start starts the OKGroup go routine
|
|
func (o *OKGroup) Start(wg *sync.WaitGroup) {
|
|
wg.Add(1)
|
|
go func() {
|
|
o.Run()
|
|
wg.Done()
|
|
}()
|
|
}
|
|
|
|
// Run implements the OKEX wrapper
|
|
func (o *OKGroup) Run() {
|
|
if o.Verbose {
|
|
log.Debugf("%s Websocket: %s. (url: %s).\n", o.GetName(), common.IsEnabled(o.Websocket.IsEnabled()), o.WebsocketURL)
|
|
log.Debugf("%s polling delay: %ds.\n", o.GetName(), o.RESTPollingDelay)
|
|
log.Debugf("%s %d currencies enabled: %s.\n", o.GetName(), len(o.EnabledPairs), o.EnabledPairs)
|
|
}
|
|
|
|
prods, err := o.GetSpotTokenPairDetails()
|
|
if err != nil {
|
|
log.Errorf("%v failed to obtain available spot instruments. Err: %s", o.Name, err)
|
|
return
|
|
}
|
|
|
|
var pairs []string
|
|
for x := range prods {
|
|
pairs = append(pairs, prods[x].BaseCurrency+"_"+prods[x].QuoteCurrency)
|
|
}
|
|
|
|
err = o.UpdateCurrencies(pairs, false, false)
|
|
if err != nil {
|
|
log.Errorf("%v failed to update available currencies. Err: %s", o.Name, err)
|
|
return
|
|
}
|
|
}
|
|
|
|
// UpdateTicker updates and returns the ticker for a currency pair
|
|
func (o *OKGroup) UpdateTicker(p pair.CurrencyPair, assetType string) (tickerData ticker.Price, err error) {
|
|
resp, err := o.GetSpotAllTokenPairsInformationForCurrency(exchange.FormatExchangeCurrency(o.Name, p).String())
|
|
if err != nil {
|
|
return
|
|
}
|
|
tickerData = ticker.Price{
|
|
Ask: resp.BestAsk,
|
|
Bid: resp.BestBid,
|
|
CurrencyPair: exchange.FormatExchangeCurrency(o.Name, p).String(),
|
|
High: resp.High24h,
|
|
Last: resp.Last,
|
|
LastUpdated: resp.Timestamp,
|
|
Low: resp.Low24h,
|
|
Pair: p,
|
|
Volume: resp.BaseVolume24h,
|
|
}
|
|
|
|
ticker.ProcessTicker(o.Name, p, tickerData, assetType)
|
|
|
|
return
|
|
}
|
|
|
|
// GetTickerPrice returns the ticker for a currency pair
|
|
func (o *OKGroup) GetTickerPrice(p pair.CurrencyPair, assetType string) (tickerData ticker.Price, err error) {
|
|
tickerData, err = ticker.GetTicker(o.GetName(), p, assetType)
|
|
if err != nil {
|
|
return o.UpdateTicker(p, assetType)
|
|
}
|
|
return
|
|
}
|
|
|
|
// GetOrderbookEx returns orderbook base on the currency pair
|
|
func (o *OKGroup) GetOrderbookEx(p pair.CurrencyPair, assetType string) (resp orderbook.Base, err error) {
|
|
ob, err := orderbook.GetOrderbook(o.GetName(), p, assetType)
|
|
if err != nil {
|
|
return o.UpdateOrderbook(p, assetType)
|
|
}
|
|
return ob, nil
|
|
}
|
|
|
|
// UpdateOrderbook updates and returns the orderbook for a currency pair
|
|
func (o *OKGroup) UpdateOrderbook(p pair.CurrencyPair, assetType string) (resp orderbook.Base, err error) {
|
|
orderbookNew, err := o.GetSpotOrderBook(GetSpotOrderBookRequest{
|
|
InstrumentID: exchange.FormatExchangeCurrency(o.Name, p).String(),
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
for x := range orderbookNew.Bids {
|
|
amount, err := strconv.ParseFloat(orderbookNew.Bids[x][1], 64)
|
|
if err != nil {
|
|
log.Errorf("Could not convert %v to float64", orderbookNew.Bids[x][1])
|
|
}
|
|
price, err := strconv.ParseFloat(orderbookNew.Bids[x][0], 64)
|
|
if err != nil {
|
|
log.Errorf("Could not convert %v to float64", orderbookNew.Bids[x][0])
|
|
}
|
|
resp.Bids = append(resp.Bids, orderbook.Item{
|
|
Amount: amount,
|
|
Price: price,
|
|
})
|
|
}
|
|
|
|
for x := range orderbookNew.Asks {
|
|
amount, err := strconv.ParseFloat(orderbookNew.Asks[x][1], 64)
|
|
if err != nil {
|
|
log.Errorf("Could not convert %v to float64", orderbookNew.Asks[x][1])
|
|
}
|
|
price, err := strconv.ParseFloat(orderbookNew.Asks[x][0], 64)
|
|
if err != nil {
|
|
log.Errorf("Could not convert %v to float64", orderbookNew.Asks[x][0])
|
|
}
|
|
resp.Asks = append(resp.Asks, orderbook.Item{
|
|
Amount: amount,
|
|
Price: price,
|
|
})
|
|
}
|
|
|
|
orderbook.ProcessOrderbook(o.GetName(), p, resp, assetType)
|
|
return orderbook.GetOrderbook(o.Name, p, assetType)
|
|
}
|
|
|
|
// GetAccountInfo retrieves balances for all enabled currencies
|
|
func (o *OKGroup) GetAccountInfo() (resp exchange.AccountInfo, err error) {
|
|
resp.Exchange = o.Name
|
|
currencies, err := o.GetSpotTradingAccounts()
|
|
currencyAccount := exchange.Account{}
|
|
|
|
for _, curr := range currencies {
|
|
hold, err := strconv.ParseFloat(curr.Hold, 64)
|
|
if err != nil {
|
|
log.Errorf("Could not convert %v to float64", curr.Hold)
|
|
}
|
|
totalValue, err := strconv.ParseFloat(curr.Balance, 64)
|
|
if err != nil {
|
|
log.Errorf("Could not convert %v to float64", curr.Balance)
|
|
}
|
|
currencyAccount.Currencies = append(currencyAccount.Currencies, exchange.AccountCurrencyInfo{
|
|
CurrencyName: curr.ID,
|
|
Hold: hold,
|
|
TotalValue: totalValue,
|
|
})
|
|
}
|
|
|
|
resp.Accounts = append(resp.Accounts, currencyAccount)
|
|
return
|
|
}
|
|
|
|
// GetFundingHistory returns funding history, deposits and
|
|
// withdrawals
|
|
func (o *OKGroup) GetFundingHistory() (resp []exchange.FundHistory, err error) {
|
|
accountDepositHistory, err := o.GetAccountDepositHistory("")
|
|
if err != nil {
|
|
return
|
|
}
|
|
for _, deposit := range accountDepositHistory {
|
|
orderStatus := ""
|
|
switch deposit.Status {
|
|
case 0:
|
|
orderStatus = "waiting"
|
|
case 1:
|
|
orderStatus = "confirmation account"
|
|
case 2:
|
|
orderStatus = "recharge success"
|
|
}
|
|
|
|
resp = append(resp, exchange.FundHistory{
|
|
Amount: deposit.Amount,
|
|
Currency: deposit.Currency,
|
|
ExchangeName: o.Name,
|
|
Status: orderStatus,
|
|
Timestamp: deposit.Timestamp,
|
|
TransferID: deposit.TransactionID,
|
|
TransferType: "deposit",
|
|
})
|
|
}
|
|
accountWithdrawlHistory, err := o.GetAccountWithdrawalHistory("")
|
|
for _, withdrawal := range accountWithdrawlHistory {
|
|
resp = append(resp, exchange.FundHistory{
|
|
Amount: withdrawal.Amount,
|
|
Currency: withdrawal.Currency,
|
|
ExchangeName: o.Name,
|
|
Status: OrderStatus[withdrawal.Status],
|
|
Timestamp: withdrawal.Timestamp,
|
|
TransferID: withdrawal.Txid,
|
|
TransferType: "withdrawal",
|
|
})
|
|
}
|
|
return resp, err
|
|
}
|
|
|
|
// GetExchangeHistory returns historic trade data since exchange opening.
|
|
func (o *OKGroup) GetExchangeHistory(p pair.CurrencyPair, assetType string) ([]exchange.TradeHistory, error) {
|
|
return nil, common.ErrNotYetImplemented
|
|
}
|
|
|
|
// SubmitOrder submits a new order
|
|
func (o *OKGroup) SubmitOrder(p pair.CurrencyPair, side exchange.OrderSide, orderType exchange.OrderType, amount, price float64, clientID string) (resp exchange.SubmitOrderResponse, err error) {
|
|
request := PlaceSpotOrderRequest{
|
|
ClientOID: clientID,
|
|
InstrumentID: exchange.FormatExchangeCurrency(o.Name, p).String(),
|
|
Side: strings.ToLower(side.ToString()),
|
|
Type: strings.ToLower(orderType.ToString()),
|
|
Size: strconv.FormatFloat(amount, 'f', -1, 64),
|
|
}
|
|
if orderType == exchange.LimitOrderType {
|
|
request.Price = strconv.FormatFloat(price, 'f', -1, 64)
|
|
}
|
|
|
|
orderResponse, err := o.PlaceSpotOrder(request)
|
|
if err != nil {
|
|
return
|
|
}
|
|
resp.IsOrderPlaced = orderResponse.Result
|
|
resp.OrderID = orderResponse.OrderID
|
|
|
|
return
|
|
}
|
|
|
|
// ModifyOrder will allow of changing orderbook placement and limit to
|
|
// market conversion
|
|
func (o *OKGroup) ModifyOrder(action exchange.ModifyOrder) (string, error) {
|
|
return "", common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// CancelOrder cancels an order by its corresponding ID number
|
|
func (o *OKGroup) CancelOrder(orderCancellation exchange.OrderCancellation) (err error) {
|
|
orderID, err := strconv.ParseInt(orderCancellation.OrderID, 10, 64)
|
|
if err != nil {
|
|
return
|
|
}
|
|
orderCancellationResponse, err := o.CancelSpotOrder(CancelSpotOrderRequest{
|
|
InstrumentID: exchange.FormatExchangeCurrency(o.Name, orderCancellation.CurrencyPair).String(),
|
|
OrderID: orderID,
|
|
})
|
|
if !orderCancellationResponse.Result {
|
|
err = fmt.Errorf("order %v failed to be cancelled", orderCancellationResponse.OrderID)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// CancelAllOrders cancels all orders associated with a currency pair
|
|
func (o *OKGroup) CancelAllOrders(orderCancellation exchange.OrderCancellation) (resp exchange.CancelAllOrdersResponse, _ error) {
|
|
orderIDs := strings.Split(orderCancellation.OrderID, ",")
|
|
var orderIDNumbers []int64
|
|
for _, i := range orderIDs {
|
|
orderIDNumber, err := strconv.ParseInt(i, 10, 64)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
orderIDNumbers = append(orderIDNumbers, orderIDNumber)
|
|
}
|
|
|
|
cancelOrdersResponse, err := o.CancelMultipleSpotOrders(CancelMultipleSpotOrdersRequest{
|
|
InstrumentID: exchange.FormatExchangeCurrency(o.Name, orderCancellation.CurrencyPair).String(),
|
|
OrderIDs: orderIDNumbers,
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
for _, orderMap := range cancelOrdersResponse {
|
|
for _, cancelledOrder := range orderMap {
|
|
resp.OrderStatus[fmt.Sprintf("%v", cancelledOrder.OrderID)] = fmt.Sprintf("%v", cancelledOrder.Result)
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// GetOrderInfo returns information on a current open order
|
|
func (o *OKGroup) GetOrderInfo(orderID string) (resp exchange.OrderDetail, err error) {
|
|
order, err := o.GetSpotOrder(GetSpotOrderRequest{OrderID: orderID})
|
|
if err != nil {
|
|
return
|
|
}
|
|
resp = exchange.OrderDetail{
|
|
Amount: order.Size,
|
|
CurrencyPair: pair.NewCurrencyPairDelimiter(order.InstrumentID, o.ConfigCurrencyPairFormat.Delimiter),
|
|
Exchange: o.Name,
|
|
OrderDate: order.Timestamp,
|
|
ExecutedAmount: order.FilledSize,
|
|
Status: order.Status,
|
|
OrderSide: exchange.OrderSide(order.Side),
|
|
}
|
|
return
|
|
}
|
|
|
|
// GetDepositAddress returns a deposit address for a specified currency
|
|
func (o *OKGroup) GetDepositAddress(p pair.CurrencyItem, accountID string) (_ string, err error) {
|
|
wallet, err := o.GetAccountDepositAddressForCurrency(p.Lower().String())
|
|
if err != nil {
|
|
return
|
|
}
|
|
return wallet[0].Address, nil
|
|
}
|
|
|
|
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is
|
|
// submitted
|
|
func (o *OKGroup) WithdrawCryptocurrencyFunds(withdrawRequest exchange.WithdrawRequest) (string, error) {
|
|
withdrawal, err := o.AccountWithdraw(AccountWithdrawRequest{
|
|
Amount: withdrawRequest.Amount,
|
|
Currency: withdrawRequest.Currency.Lower().String(),
|
|
Destination: 4, // 1, 2, 3 are all internal
|
|
Fee: withdrawRequest.FeeAmount,
|
|
ToAddress: withdrawRequest.Address,
|
|
TradePwd: withdrawRequest.TradePassword,
|
|
})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if !withdrawal.Result {
|
|
return fmt.Sprintf("%v", withdrawal.WithdrawalID), fmt.Errorf("could not withdraw currency %v to %v, no error specified", withdrawRequest.Currency.String(), withdrawRequest.Address)
|
|
}
|
|
|
|
return fmt.Sprintf("%v", withdrawal.WithdrawalID), nil
|
|
}
|
|
|
|
// WithdrawFiatFunds returns a withdrawal ID when a
|
|
// withdrawal is submitted
|
|
func (o *OKGroup) WithdrawFiatFunds(withdrawRequest exchange.WithdrawRequest) (string, error) {
|
|
return "", common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// WithdrawFiatFundsToInternationalBank returns a withdrawal ID when a
|
|
// withdrawal is submitted
|
|
func (o *OKGroup) WithdrawFiatFundsToInternationalBank(withdrawRequest exchange.WithdrawRequest) (string, error) {
|
|
return "", common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// GetActiveOrders retrieves any orders that are active/open
|
|
func (o *OKGroup) GetActiveOrders(getOrdersRequest exchange.GetOrdersRequest) (resp []exchange.OrderDetail, err error) {
|
|
for _, currency := range getOrdersRequest.Currencies {
|
|
spotOpenOrders, err := o.GetSpotOpenOrders(GetSpotOpenOrdersRequest{
|
|
InstrumentID: exchange.FormatExchangeCurrency(o.Name, currency).String(),
|
|
})
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
for _, openOrder := range spotOpenOrders {
|
|
resp = append(resp, exchange.OrderDetail{
|
|
Amount: openOrder.Size,
|
|
CurrencyPair: currency,
|
|
Exchange: o.Name,
|
|
ExecutedAmount: openOrder.FilledSize,
|
|
OrderDate: openOrder.Timestamp,
|
|
Status: openOrder.Status,
|
|
})
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// GetOrderHistory retrieves account order information
|
|
// Can Limit response to specific order status
|
|
func (o *OKGroup) GetOrderHistory(getOrdersRequest exchange.GetOrdersRequest) (resp []exchange.OrderDetail, err error) {
|
|
for _, currency := range getOrdersRequest.Currencies {
|
|
spotOpenOrders, err := o.GetSpotOrders(GetSpotOrdersRequest{
|
|
InstrumentID: exchange.FormatExchangeCurrency(o.Name, currency).String(),
|
|
})
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
for _, openOrder := range spotOpenOrders {
|
|
resp = append(resp, exchange.OrderDetail{
|
|
Amount: openOrder.Size,
|
|
CurrencyPair: currency,
|
|
Exchange: o.Name,
|
|
ExecutedAmount: openOrder.FilledSize,
|
|
OrderDate: openOrder.Timestamp,
|
|
Status: openOrder.Status,
|
|
})
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// GetWebsocket returns a pointer to the exchange websocket
|
|
func (o *OKGroup) GetWebsocket() (*exchange.Websocket, error) {
|
|
return o.Websocket, nil
|
|
}
|
|
|
|
// GetFeeByType returns an estimate of fee based on type of transaction
|
|
func (o *OKGroup) GetFeeByType(feeBuilder exchange.FeeBuilder) (float64, error) {
|
|
return o.GetFee(feeBuilder)
|
|
}
|
|
|
|
// GetWithdrawCapabilities returns the types of withdrawal methods permitted by the exchange
|
|
func (o *OKGroup) GetWithdrawCapabilities() uint32 {
|
|
return o.GetWithdrawPermissions()
|
|
}
|