mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-16 15:09:57 +00:00
* few fixes and add ratelimiter * adds test * revert configtest.json changes * configtest updated * WIP: adds public endpoint support * WIP: adds public endpoint support * adds public endpoint support * WIP: adds auth. endpoint support * adds test for auth. endpoint * fixes * adds auth. endpoint support * WIP: ws support * WIP * WIP * WIP * WIP * WIP * WIP * WIP * Testing * Complete WS spot testing * adds support for ws events * minor change * WIP: adds REST support for CoinMarginedFutures * Fixes * WIP: adds REST support for CoinMarginedFutures * Fixes * improvement in SPOT REST * Typo fix * WIP: add REST support for CMF Account API * minor fixes * WIP: add support for CMF conditional orders and few minor fixes * complete support for CMF conditional orders * adds support for public CMF endpoint * adds support for CMF position API * Complete REST CMF support * WIP * Testing REST CMF support * Testing REST CMF support * Testing REST CMF support completed * WIP: add support for UMF * completed non-auth UMF * WIP: add support for REST Auth. UMF * WIP: add support for REST Auth. UMF and some improvements * WIP * WIP * WIP * completed REST UMF * renaming * adds REST support for futures * add testcases for UMF and some optimizations * add testcases for futures * Testing UMF, futures and its changes * Fixes * Fixes after testing * WIP * WIP * WIP * completed ws USDT futures support * WIP: ws support for futures * fixes in WS futures * fixes in WS support * roll back changes made for WS CMF, USDT and Futures * fixes * WIP * WIP * fixes * Steps for new PR * WIP * WIP * WIP * WIP * complete PR setup * fixes for successfully running tests * update in symbol for futures pair in test file * WIP * Fixes in test file and other minor fix * fix testdata/configtest.json * reset CONTRIBUTORS file * review changes * remove unwanted file * remove redundant code * improvisation * adds comment for exported functions * remove unwanted TODO and commented code * fix * improvisation * fix * defined errors * improvisation * improvisation * improvisation * updates test * adds comment for exported types * review changes * review changes * fix * fixes * Changes for making BYBIT compatible with existing code base * Test file changes * Changes for making BYBIT compatible with existing code base * Changes for making BYBIT compatible with existing code base * fix lint issues * fix * review changes * review changes * review changes * review changes * review changes * review changes * review changes * review changes * review changes * review changes * WIP * add test cases for new API's * minor improvements * add missing API and their tests * minor fixes * add bybitTime * add bybitTimeSec, bybitTimeMilliSec, bybitTimeNanoSec and necessary support * fix GetTradeHistory function * error handling * test fixes * add GetServerTime API * adds GetHistoricCandlesExtended and review changes * test fixes * minor fix * integrating CMF Bybit recent change log * minor fixes * adds extractCurrencyPair * minor fixes * minor fix * review changes * adds variable declaration of error * review commit * adds embeddable type in API response for all API and integrate it * fixes * adds authentication WS connection * review changes * review changes * compatible changes * adds asset to GetWithdrawalsHistory * adds asset_type in rpc.proto * adds asset argument in gctcli withdrawal request command * improve error handling in exchange API error * web socket fix * review changes * improvements * improvements * minor fix * review changes * fixing wrapper issues * fixes * fixes * review changes * add test cases * fix for GetActiveOrders * lint fixes * fixes in websocket * adds wrapper testcases * adds wrapper testcases * adds wrapper testcases * fixes * fix issue with GetHistoricCandlesExtended * fix merge issues * improving error reporting * adds wrapper testcases and a minor fix * gctrpc changes * adds test cases fixes in websocket * review changes for ws * review changes in WS * fix gctrpc * merge fixes * review changes * WIP * updates pair in configs * adds new asset USDCMarginedFutures * adds URL const for USDCMarginedFutures * adds API support * minor fixes * adds kline API * minor fix * adds API * adds API * adds API * WIP * WIP * WIP * adds support for USDC auth requests to SendAuthHTTPRequest * adds SendUSDCAuthHTTPRequest * run test and fix them * rollback support added for Auth. USDC request inside SendAuthHTTPRequest * adds API and test cases * adds API and test cases * adds APIs and test cases * adds APIs * adds rate limit for USDC * adds USDCMarginedFutures to wrapper * adds USDC testcases in wrapper and fix few issues * minor test fixes * minor test fixes * fix lint issues * WIP * Merge changes * minor fixes * remove "else" and optimize * review changes * review changes * review changes * fix lint issue * merge fix * fix test * fix templates and run them * changes after merge * review changes and improvements * code improvement * fixes with respect to changes in API response in documentation * fixed review change in test * adds check in CancelExistingOrder * update exchange template * review changes * adds GetDepositAddress API * WIP: adds GetOrderHistory * complete GetOrderHistory * fixes * adds test case * fixes and add WithdrawFund API * WIP * WIP * updating all SendAuthHTTPRequest call * adds WithdrawCryptocurrencyFunds * update test cases * fix lint issues * fixes after merge * adds GetAvailableTransferChains and few fixes * minor fix in GetDepositAddress * minor fix with WS ping/pong handling * add ping handler for WS Auth. * fix typo mistake * update doc
809 lines
23 KiB
Go
809 lines
23 KiB
Go
package okgroup
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/common"
|
|
"github.com/thrasher-corp/gocryptotrader/common/convert"
|
|
"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/account"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/deposit"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
|
|
"github.com/thrasher-corp/gocryptotrader/log"
|
|
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
|
|
)
|
|
|
|
// 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
|
|
|
|
var errNoAccountDepositAddress = errors.New("no account deposit address")
|
|
|
|
// Setup sets user exchange configuration settings
|
|
func (o *OKGroup) Setup(exch *config.Exchange) error {
|
|
err := exch.Validate()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !exch.Enabled {
|
|
o.SetEnabled(false)
|
|
return nil
|
|
}
|
|
err = o.SetupDefaults(exch)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
wsEndpoint, err := o.API.Endpoints.GetURL(exchange.WebsocketSpot)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = o.Websocket.Setup(&stream.WebsocketSetup{
|
|
ExchangeConfig: exch,
|
|
DefaultURL: wsEndpoint,
|
|
RunningURL: wsEndpoint,
|
|
Connector: o.WsConnect,
|
|
Subscriber: o.Subscribe,
|
|
Unsubscriber: o.Unsubscribe,
|
|
GenerateSubscriptions: o.GenerateDefaultSubscriptions,
|
|
Features: &o.Features.Supports.WebsocketCapabilities,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return o.Websocket.SetupNewConnection(stream.ConnectionSetup{
|
|
RateLimit: okGroupWsRateLimit,
|
|
ResponseCheckTimeout: exch.WebsocketResponseCheckTimeout,
|
|
ResponseMaxLimit: exch.WebsocketResponseMaxLimit,
|
|
})
|
|
}
|
|
|
|
// FetchOrderbook returns orderbook base on the currency pair
|
|
func (o *OKGroup) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
|
|
fPair, err := o.FormatExchangeCurrency(p, assetType)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ob, err := orderbook.Get(o.Name, fPair, assetType)
|
|
if err != nil {
|
|
return o.UpdateOrderbook(ctx, fPair, assetType)
|
|
}
|
|
return ob, nil
|
|
}
|
|
|
|
// UpdateOrderbook updates and returns the orderbook for a currency pair
|
|
func (o *OKGroup) UpdateOrderbook(ctx context.Context, p currency.Pair, a asset.Item) (*orderbook.Base, error) {
|
|
book := &orderbook.Base{
|
|
Exchange: o.Name,
|
|
Pair: p,
|
|
Asset: a,
|
|
VerifyOrderbook: o.CanVerifyOrderbook,
|
|
}
|
|
|
|
if a == asset.Index {
|
|
return book, errors.New("no orderbooks for index")
|
|
}
|
|
|
|
fPair, err := o.FormatExchangeCurrency(p, a)
|
|
if err != nil {
|
|
return book, err
|
|
}
|
|
|
|
orderbookNew, err := o.GetOrderBook(ctx,
|
|
&GetOrderBookRequest{
|
|
InstrumentID: fPair.String(),
|
|
Size: 200,
|
|
}, a)
|
|
if err != nil {
|
|
return book, err
|
|
}
|
|
|
|
book.Bids = make(orderbook.Items, len(orderbookNew.Bids))
|
|
for x := range orderbookNew.Bids {
|
|
amount, convErr := strconv.ParseFloat(orderbookNew.Bids[x][1], 64)
|
|
if convErr != nil {
|
|
return book, err
|
|
}
|
|
price, convErr := strconv.ParseFloat(orderbookNew.Bids[x][0], 64)
|
|
if convErr != nil {
|
|
return book, err
|
|
}
|
|
|
|
var liquidationOrders, orderCount int64
|
|
// Contract specific variables
|
|
if len(orderbookNew.Bids[x]) == 4 {
|
|
liquidationOrders, convErr = strconv.ParseInt(orderbookNew.Bids[x][2], 10, 64)
|
|
if convErr != nil {
|
|
return book, err
|
|
}
|
|
|
|
orderCount, convErr = strconv.ParseInt(orderbookNew.Bids[x][3], 10, 64)
|
|
if convErr != nil {
|
|
return book, err
|
|
}
|
|
}
|
|
|
|
book.Bids[x] = orderbook.Item{
|
|
Amount: amount,
|
|
Price: price,
|
|
LiquidationOrders: liquidationOrders,
|
|
OrderCount: orderCount,
|
|
}
|
|
}
|
|
|
|
book.Asks = make(orderbook.Items, len(orderbookNew.Asks))
|
|
for x := range orderbookNew.Asks {
|
|
amount, convErr := strconv.ParseFloat(orderbookNew.Asks[x][1], 64)
|
|
if convErr != nil {
|
|
return book, err
|
|
}
|
|
price, convErr := strconv.ParseFloat(orderbookNew.Asks[x][0], 64)
|
|
if convErr != nil {
|
|
return book, err
|
|
}
|
|
|
|
var liquidationOrders, orderCount int64
|
|
// Contract specific variables
|
|
if len(orderbookNew.Asks[x]) == 4 {
|
|
liquidationOrders, convErr = strconv.ParseInt(orderbookNew.Asks[x][2], 10, 64)
|
|
if convErr != nil {
|
|
return book, err
|
|
}
|
|
|
|
orderCount, convErr = strconv.ParseInt(orderbookNew.Asks[x][3], 10, 64)
|
|
if convErr != nil {
|
|
return book, err
|
|
}
|
|
}
|
|
|
|
book.Asks[x] = orderbook.Item{
|
|
Amount: amount,
|
|
Price: price,
|
|
LiquidationOrders: liquidationOrders,
|
|
OrderCount: orderCount,
|
|
}
|
|
}
|
|
|
|
err = book.Process()
|
|
if err != nil {
|
|
return book, err
|
|
}
|
|
|
|
return orderbook.Get(o.Name, fPair, a)
|
|
}
|
|
|
|
// UpdateAccountInfo retrieves balances for all enabled currencies
|
|
func (o *OKGroup) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
|
|
currencies, err := o.GetSpotTradingAccounts(ctx)
|
|
if err != nil {
|
|
return account.Holdings{}, err
|
|
}
|
|
|
|
var resp account.Holdings
|
|
resp.Exchange = o.Name
|
|
currencyAccount := account.SubAccount{AssetType: assetType}
|
|
|
|
for i := range currencies {
|
|
hold, parseErr := strconv.ParseFloat(currencies[i].Hold, 64)
|
|
if parseErr != nil {
|
|
return resp, parseErr
|
|
}
|
|
totalValue, parseErr := strconv.ParseFloat(currencies[i].Balance, 64)
|
|
if parseErr != nil {
|
|
return resp, parseErr
|
|
}
|
|
currencyAccount.Currencies = append(currencyAccount.Currencies,
|
|
account.Balance{
|
|
CurrencyName: currency.NewCode(currencies[i].Currency),
|
|
Total: totalValue,
|
|
Hold: hold,
|
|
Free: totalValue - hold,
|
|
})
|
|
}
|
|
|
|
resp.Accounts = append(resp.Accounts, currencyAccount)
|
|
|
|
creds, err := o.GetCredentials(ctx)
|
|
if err != nil {
|
|
return account.Holdings{}, err
|
|
}
|
|
err = account.Process(&resp, creds)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
|
|
return resp, nil
|
|
}
|
|
|
|
// FetchAccountInfo retrieves balances for all enabled currencies
|
|
func (o *OKGroup) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
|
|
creds, err := o.GetCredentials(ctx)
|
|
if err != nil {
|
|
return account.Holdings{}, err
|
|
}
|
|
acc, err := account.GetHoldings(o.Name, creds, assetType)
|
|
if err != nil {
|
|
return o.UpdateAccountInfo(ctx, assetType)
|
|
}
|
|
return acc, nil
|
|
}
|
|
|
|
// GetFundingHistory returns funding history, deposits and
|
|
// withdrawals
|
|
func (o *OKGroup) GetFundingHistory(ctx context.Context) (resp []exchange.FundHistory, err error) {
|
|
accountDepositHistory, err := o.GetAccountDepositHistory(ctx, "")
|
|
if err != nil {
|
|
return
|
|
}
|
|
for x := range accountDepositHistory {
|
|
orderStatus := ""
|
|
switch accountDepositHistory[x].Status {
|
|
case 0:
|
|
orderStatus = "waiting"
|
|
case 1:
|
|
orderStatus = "confirmation account"
|
|
case 2:
|
|
orderStatus = "recharge success"
|
|
}
|
|
|
|
resp = append(resp, exchange.FundHistory{
|
|
Amount: accountDepositHistory[x].Amount,
|
|
Currency: accountDepositHistory[x].Currency,
|
|
ExchangeName: o.Name,
|
|
Status: orderStatus,
|
|
Timestamp: accountDepositHistory[x].Timestamp,
|
|
TransferID: accountDepositHistory[x].TransactionID,
|
|
TransferType: "deposit",
|
|
})
|
|
}
|
|
accountWithdrawlHistory, err := o.GetAccountWithdrawalHistory(ctx, "")
|
|
for i := range accountWithdrawlHistory {
|
|
resp = append(resp, exchange.FundHistory{
|
|
Amount: accountWithdrawlHistory[i].Amount,
|
|
Currency: accountWithdrawlHistory[i].Currency,
|
|
ExchangeName: o.Name,
|
|
Status: OrderStatus[accountWithdrawlHistory[i].Status],
|
|
Timestamp: accountWithdrawlHistory[i].Timestamp,
|
|
TransferID: accountWithdrawlHistory[i].TransactionID,
|
|
TransferType: "withdrawal",
|
|
})
|
|
}
|
|
return resp, err
|
|
}
|
|
|
|
// SubmitOrder submits a new order
|
|
func (o *OKGroup) SubmitOrder(ctx context.Context, s *order.Submit) (*order.SubmitResponse, error) {
|
|
if err := s.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fpair, err := o.FormatExchangeCurrency(s.Pair, s.AssetType)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
request := PlaceOrderRequest{
|
|
ClientOID: s.ClientID,
|
|
InstrumentID: fpair.String(),
|
|
Side: s.Side.Lower(),
|
|
Type: s.Type.Lower(),
|
|
Size: strconv.FormatFloat(s.Amount, 'f', -1, 64),
|
|
}
|
|
if s.Type == order.Limit {
|
|
request.Price = strconv.FormatFloat(s.Price, 'f', -1, 64)
|
|
}
|
|
|
|
orderResponse, err := o.PlaceSpotOrder(ctx, &request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !orderResponse.Result {
|
|
return nil, order.ErrUnableToPlaceOrder
|
|
}
|
|
return s.DeriveSubmitResponse(orderResponse.OrderID)
|
|
}
|
|
|
|
// ModifyOrder will allow of changing orderbook placement and limit to
|
|
// market conversion
|
|
func (o *OKGroup) ModifyOrder(_ context.Context, _ *order.Modify) (*order.ModifyResponse, error) {
|
|
return nil, common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// CancelOrder cancels an order by its corresponding ID number
|
|
func (o *OKGroup) CancelOrder(ctx context.Context, cancel *order.Cancel) (err error) {
|
|
err = cancel.Validate(cancel.StandardCancel())
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
orderID, err := strconv.ParseInt(cancel.OrderID, 10, 64)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
fpair, err := o.FormatExchangeCurrency(cancel.Pair,
|
|
cancel.AssetType)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
orderCancellationResponse, err := o.CancelSpotOrder(ctx,
|
|
CancelSpotOrderRequest{
|
|
InstrumentID: fpair.String(),
|
|
OrderID: orderID,
|
|
})
|
|
|
|
if !orderCancellationResponse.Result {
|
|
err = fmt.Errorf("order %d failed to be cancelled",
|
|
orderCancellationResponse.OrderID)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// CancelAllOrders cancels all orders associated with a currency pair
|
|
func (o *OKGroup) CancelAllOrders(ctx context.Context, orderCancellation *order.Cancel) (order.CancelAllResponse, error) {
|
|
if err := orderCancellation.Validate(); err != nil {
|
|
return order.CancelAllResponse{}, err
|
|
}
|
|
|
|
orderIDs := strings.Split(orderCancellation.OrderID, ",")
|
|
resp := order.CancelAllResponse{}
|
|
resp.Status = make(map[string]string)
|
|
orderIDNumbers := make([]int64, 0, len(orderIDs))
|
|
for i := range orderIDs {
|
|
orderIDNumber, err := strconv.ParseInt(orderIDs[i], 10, 64)
|
|
if err != nil {
|
|
resp.Status[orderIDs[i]] = err.Error()
|
|
continue
|
|
}
|
|
orderIDNumbers = append(orderIDNumbers, orderIDNumber)
|
|
}
|
|
|
|
fpair, err := o.FormatExchangeCurrency(orderCancellation.Pair,
|
|
orderCancellation.AssetType)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
|
|
cancelOrdersResponse, err := o.CancelMultipleSpotOrders(ctx,
|
|
CancelMultipleSpotOrdersRequest{
|
|
InstrumentID: fpair.String(),
|
|
OrderIDs: orderIDNumbers,
|
|
})
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
|
|
for x := range cancelOrdersResponse {
|
|
for y := range cancelOrdersResponse[x] {
|
|
resp.Status[strconv.FormatInt(cancelOrdersResponse[x][y].OrderID, 10)] = strconv.FormatBool(cancelOrdersResponse[x][y].Result)
|
|
}
|
|
}
|
|
|
|
return resp, err
|
|
}
|
|
|
|
// GetOrderInfo returns order information based on order ID
|
|
func (o *OKGroup) GetOrderInfo(ctx context.Context, orderID string, pair currency.Pair, assetType asset.Item) (resp order.Detail, err error) {
|
|
if assetType != asset.Spot {
|
|
return resp, fmt.Errorf("%s %w", assetType, asset.ErrNotSupported)
|
|
}
|
|
|
|
mOrder, err := o.GetSpotOrder(ctx, GetSpotOrderRequest{OrderID: orderID})
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
format, err := o.GetPairFormat(assetType, false)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
|
|
p, err := currency.NewPairDelimiter(mOrder.InstrumentID, format.Delimiter)
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
|
|
status, err := order.StringToOrderStatus(mOrder.Status)
|
|
if err != nil {
|
|
log.Errorf(log.ExchangeSys, "%s %v", o.Name, err)
|
|
}
|
|
|
|
side, err := order.StringToOrderSide(mOrder.Side)
|
|
if err != nil {
|
|
log.Errorf(log.ExchangeSys, "%s %v", o.Name, err)
|
|
}
|
|
resp = order.Detail{
|
|
Amount: mOrder.Size,
|
|
Pair: p,
|
|
Exchange: o.Name,
|
|
Date: mOrder.Timestamp,
|
|
ExecutedAmount: mOrder.FilledSize,
|
|
Status: status,
|
|
Side: side,
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
// GetDepositAddress returns a deposit address for a specified currency
|
|
func (o *OKGroup) GetDepositAddress(ctx context.Context, c currency.Code, _, _ string) (*deposit.Address, error) {
|
|
wallet, err := o.GetAccountDepositAddressForCurrency(ctx, c.Lower().String())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(wallet) == 0 {
|
|
return nil, fmt.Errorf("%w for currency %s",
|
|
errNoAccountDepositAddress,
|
|
c)
|
|
}
|
|
return &deposit.Address{
|
|
Address: wallet[0].Address,
|
|
Tag: wallet[0].Tag,
|
|
}, nil
|
|
}
|
|
|
|
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is
|
|
// submitted
|
|
func (o *OKGroup) WithdrawCryptocurrencyFunds(ctx context.Context, withdrawRequest *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
|
if err := withdrawRequest.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
withdrawal, err := o.AccountWithdraw(ctx,
|
|
AccountWithdrawRequest{
|
|
Amount: withdrawRequest.Amount,
|
|
Currency: withdrawRequest.Currency.Lower().String(),
|
|
Destination: 4, // 1, 2, 3 are all internal
|
|
Fee: withdrawRequest.Crypto.FeeAmount,
|
|
ToAddress: withdrawRequest.Crypto.Address,
|
|
TradePwd: withdrawRequest.TradePassword,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !withdrawal.Result {
|
|
return nil,
|
|
fmt.Errorf("could not withdraw currency %s to %s, no error specified",
|
|
withdrawRequest.Currency,
|
|
withdrawRequest.Crypto.Address)
|
|
}
|
|
|
|
return &withdraw.ExchangeResponse{
|
|
ID: strconv.FormatInt(withdrawal.WithdrawalID, 10),
|
|
}, nil
|
|
}
|
|
|
|
// WithdrawFiatFunds returns a withdrawal ID when a
|
|
// withdrawal is submitted
|
|
func (o *OKGroup) WithdrawFiatFunds(_ context.Context, _ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
|
return nil, common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// WithdrawFiatFundsToInternationalBank returns a withdrawal ID when a
|
|
// withdrawal is submitted
|
|
func (o *OKGroup) WithdrawFiatFundsToInternationalBank(_ context.Context, _ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
|
|
return nil, common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// GetWithdrawalsHistory returns previous withdrawals data
|
|
func (o *OKGroup) GetWithdrawalsHistory(ctx context.Context, c currency.Code, a asset.Item) (resp []exchange.WithdrawalHistory, err error) {
|
|
return nil, common.ErrNotYetImplemented
|
|
}
|
|
|
|
// GetActiveOrders retrieves any orders that are active/open
|
|
func (o *OKGroup) GetActiveOrders(ctx context.Context, req *order.GetOrdersRequest) (resp []order.Detail, err error) {
|
|
err = req.Validate()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for x := range req.Pairs {
|
|
var fPair currency.Pair
|
|
fPair, err = o.FormatExchangeCurrency(req.Pairs[x], asset.Spot)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var spotOpenOrders []GetSpotOrderResponse
|
|
spotOpenOrders, err = o.GetSpotOpenOrders(ctx,
|
|
GetSpotOpenOrdersRequest{
|
|
InstrumentID: fPair.String(),
|
|
})
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
for i := range spotOpenOrders {
|
|
var status order.Status
|
|
status, err = order.StringToOrderStatus(spotOpenOrders[i].Status)
|
|
if err != nil {
|
|
log.Errorf(log.ExchangeSys, "%s %v", o.Name, err)
|
|
}
|
|
var side order.Side
|
|
side, err = order.StringToOrderSide(spotOpenOrders[i].Side)
|
|
if err != nil {
|
|
log.Errorf(log.ExchangeSys, "%s %v", o.Name, err)
|
|
}
|
|
var orderType order.Type
|
|
orderType, err = order.StringToOrderType(spotOpenOrders[i].Type)
|
|
if err != nil {
|
|
log.Errorf(log.ExchangeSys, "%s %v", o.Name, err)
|
|
}
|
|
resp = append(resp, order.Detail{
|
|
OrderID: spotOpenOrders[i].OrderID,
|
|
Price: spotOpenOrders[i].Price,
|
|
Amount: spotOpenOrders[i].Size,
|
|
Pair: req.Pairs[x],
|
|
Exchange: o.Name,
|
|
Side: side,
|
|
Type: orderType,
|
|
ExecutedAmount: spotOpenOrders[i].FilledSize,
|
|
Date: spotOpenOrders[i].Timestamp,
|
|
Status: status,
|
|
})
|
|
}
|
|
}
|
|
return resp, err
|
|
}
|
|
|
|
// GetOrderHistory retrieves account order information
|
|
// Can Limit response to specific order status
|
|
func (o *OKGroup) GetOrderHistory(ctx context.Context, req *order.GetOrdersRequest) (resp []order.Detail, err error) {
|
|
err = req.Validate()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for x := range req.Pairs {
|
|
var fPair currency.Pair
|
|
fPair, err = o.FormatExchangeCurrency(req.Pairs[x], asset.Spot)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var spotOrders []GetSpotOrderResponse
|
|
spotOrders, err = o.GetSpotOrders(ctx,
|
|
GetSpotOrdersRequest{
|
|
Status: strings.Join([]string{"filled", "cancelled", "failure"}, "|"),
|
|
InstrumentID: fPair.String(),
|
|
})
|
|
if err != nil {
|
|
return resp, err
|
|
}
|
|
for i := range spotOrders {
|
|
var status order.Status
|
|
status, err = order.StringToOrderStatus(spotOrders[i].Status)
|
|
if err != nil {
|
|
log.Errorf(log.ExchangeSys, "%s %v", o.Name, err)
|
|
}
|
|
var side order.Side
|
|
side, err = order.StringToOrderSide(spotOrders[i].Side)
|
|
if err != nil {
|
|
log.Errorf(log.ExchangeSys, "%s %v", o.Name, err)
|
|
}
|
|
var orderType order.Type
|
|
orderType, err = order.StringToOrderType(spotOrders[i].Type)
|
|
if err != nil {
|
|
log.Errorf(log.ExchangeSys, "%s %v", o.Name, err)
|
|
}
|
|
detail := order.Detail{
|
|
OrderID: spotOrders[i].OrderID,
|
|
Price: spotOrders[i].Price,
|
|
AverageExecutedPrice: spotOrders[i].PriceAvg,
|
|
Amount: spotOrders[i].Size,
|
|
ExecutedAmount: spotOrders[i].FilledSize,
|
|
RemainingAmount: spotOrders[i].Size - spotOrders[i].FilledSize,
|
|
Pair: req.Pairs[x],
|
|
Exchange: o.Name,
|
|
Side: side,
|
|
Type: orderType,
|
|
Date: spotOrders[i].Timestamp,
|
|
Status: status,
|
|
}
|
|
detail.InferCostsAndTimes()
|
|
resp = append(resp, detail)
|
|
}
|
|
}
|
|
return resp, err
|
|
}
|
|
|
|
// GetFeeByType returns an estimate of fee based on type of transaction
|
|
func (o *OKGroup) GetFeeByType(ctx context.Context, feeBuilder *exchange.FeeBuilder) (float64, error) {
|
|
if feeBuilder == nil {
|
|
return 0, fmt.Errorf("%T %w", feeBuilder, common.ErrNilPointer)
|
|
}
|
|
if !o.AreCredentialsValid(ctx) && // Todo check connection status
|
|
feeBuilder.FeeType == exchange.CryptocurrencyTradeFee {
|
|
feeBuilder.FeeType = exchange.OfflineTradeFee
|
|
}
|
|
return o.GetFee(ctx, feeBuilder)
|
|
}
|
|
|
|
// GetWithdrawCapabilities returns the types of withdrawal methods permitted by the exchange
|
|
func (o *OKGroup) GetWithdrawCapabilities() uint32 {
|
|
return o.GetWithdrawPermissions()
|
|
}
|
|
|
|
// AuthenticateWebsocket sends an authentication message to the websocket
|
|
func (o *OKGroup) AuthenticateWebsocket(ctx context.Context) error {
|
|
return o.WsLogin(ctx)
|
|
}
|
|
|
|
// ValidateCredentials validates current credentials used for wrapper
|
|
// functionality
|
|
func (o *OKGroup) ValidateCredentials(ctx context.Context, assetType asset.Item) error {
|
|
_, err := o.UpdateAccountInfo(ctx, assetType)
|
|
return o.CheckTransientError(err)
|
|
}
|
|
|
|
// GetHistoricTrades returns historic trade data within the timeframe provided
|
|
func (o *OKGroup) GetHistoricTrades(_ context.Context, _ currency.Pair, _ asset.Item, _, _ time.Time) ([]trade.Data, error) {
|
|
return nil, common.ErrFunctionNotSupported
|
|
}
|
|
|
|
// GetHistoricCandles returns candles between a time period for a set time interval
|
|
func (o *OKGroup) GetHistoricCandles(ctx context.Context, pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
|
if err := o.ValidateKline(pair, a, interval); err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
|
|
formattedPair, err := o.FormatExchangeCurrency(pair, a)
|
|
if err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
|
|
req := &GetMarketDataRequest{
|
|
Asset: a,
|
|
Start: start.UTC().Format(time.RFC3339),
|
|
End: end.UTC().Format(time.RFC3339),
|
|
Granularity: o.FormatExchangeKlineInterval(interval),
|
|
InstrumentID: formattedPair.String(),
|
|
}
|
|
|
|
candles, err := o.GetMarketData(ctx, req)
|
|
if err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
|
|
ret := kline.Item{
|
|
Exchange: o.Name,
|
|
Pair: pair,
|
|
Asset: a,
|
|
Interval: interval,
|
|
}
|
|
|
|
for x := range candles {
|
|
t, ok := candles[x].([]interface{})
|
|
if !ok {
|
|
return kline.Item{}, errors.New("unable to type asset candle data")
|
|
}
|
|
if len(t) < 6 {
|
|
return kline.Item{}, errors.New("incorrect candles data length")
|
|
}
|
|
v, ok := t[0].(string)
|
|
if !ok {
|
|
return kline.Item{}, errors.New("unable to type asset time data")
|
|
}
|
|
var tempCandle kline.Candle
|
|
if tempCandle.Time, err = time.Parse(time.RFC3339, v); err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
if tempCandle.Open, err = convert.FloatFromString(t[1]); err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
if tempCandle.High, err = convert.FloatFromString(t[2]); err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
if tempCandle.Low, err = convert.FloatFromString(t[3]); err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
if tempCandle.Close, err = convert.FloatFromString(t[4]); err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
if tempCandle.Volume, err = convert.FloatFromString(t[5]); err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
ret.Candles = append(ret.Candles, tempCandle)
|
|
}
|
|
|
|
ret.SortCandlesByTimestamp(false)
|
|
return ret, nil
|
|
}
|
|
|
|
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
|
|
func (o *OKGroup) GetHistoricCandlesExtended(ctx context.Context, pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
|
|
if err := o.ValidateKline(pair, a, interval); err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
|
|
ret := kline.Item{
|
|
Exchange: o.Name,
|
|
Pair: pair,
|
|
Asset: a,
|
|
Interval: interval,
|
|
}
|
|
|
|
dates, err := kline.CalculateCandleDateRanges(start, end, interval, o.Features.Enabled.Kline.ResultLimit)
|
|
if err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
formattedPair, err := o.FormatExchangeCurrency(pair, a)
|
|
if err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
|
|
for x := range dates.Ranges {
|
|
req := &GetMarketDataRequest{
|
|
Asset: a,
|
|
Start: dates.Ranges[x].Start.Time.UTC().Format(time.RFC3339),
|
|
End: dates.Ranges[x].End.Time.UTC().Format(time.RFC3339),
|
|
Granularity: o.FormatExchangeKlineInterval(interval),
|
|
InstrumentID: formattedPair.String(),
|
|
}
|
|
|
|
var candles GetMarketDataResponse
|
|
candles, err = o.GetMarketData(ctx, req)
|
|
if err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
|
|
for i := range candles {
|
|
t, ok := candles[i].([]interface{})
|
|
if !ok {
|
|
return kline.Item{}, errors.New("unable to type assert candles data")
|
|
}
|
|
if len(t) < 6 {
|
|
return kline.Item{}, errors.New("candle data length invalid")
|
|
}
|
|
v, ok := t[0].(string)
|
|
if !ok {
|
|
return kline.Item{}, errors.New("unable to type assert time value")
|
|
}
|
|
var tempCandle kline.Candle
|
|
if tempCandle.Time, err = time.Parse(time.RFC3339, v); err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
if tempCandle.Open, err = convert.FloatFromString(t[1]); err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
if tempCandle.High, err = convert.FloatFromString(t[2]); err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
|
|
if tempCandle.Low, err = convert.FloatFromString(t[3]); err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
|
|
if tempCandle.Close, err = convert.FloatFromString(t[4]); err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
|
|
if tempCandle.Volume, err = convert.FloatFromString(t[5]); err != nil {
|
|
return kline.Item{}, err
|
|
}
|
|
ret.Candles = append(ret.Candles, tempCandle)
|
|
}
|
|
}
|
|
|
|
dates.SetHasDataFromCandles(ret.Candles)
|
|
summary := dates.DataSummary(false)
|
|
if len(summary) > 0 {
|
|
log.Warnf(log.ExchangeSys, "%v - %v", o.Base.Name, summary)
|
|
}
|
|
ret.RemoveDuplicates()
|
|
ret.RemoveOutsideRange(start, end)
|
|
ret.SortCandlesByTimestamp(false)
|
|
return ret, nil
|
|
}
|