Files
gocryptotrader/exchanges/bithumb/bithumb_wrapper.go
Gareth Kirwan 52c6b3bf0b Websocket: Various refactors and test improvements (#1466)
* Websocket: Remove IsInit and simplify SetProxyAddress

IsInit was basically the same as IsConnected.
Any time Connect was called both would be set to true.
Any time we had a disconnect they'd both be set to false
Shutdown() incorrectly didn't setInit(false)

SetProxyAddress simplified to only reconnect a connected Websocket.
Any other state means it hasn't been Connected, or it's about to
reconnect anyway.
There's no handling for IsConnecting previously, either, so I've wrapped
that behind the main mutex.

* Websocket: Expand and Assertify tests

* Websocket: Simplify state transistions

* Websocket: Simplify Connecting/Connected state

* Websocket: Tests and errors for websocket

* Websocket: Make WebsocketNotEnabled a real error

This allows for testing and avoids the repetition.
If each returned error is a error.New() you can never use errors.Is()

* Websocket: Add more testable errors

* Websocket: Improve GenerateMessageID test

Testing just the last id doesn't feel very robust

* Websocket: Protect Setup() from races

* Websocket: Use atomics instead of mutex

This was spurred by looking at the setState call in trafficMonitor and
the effect on blocking and efficiency.
With the new atomic types in Go 1.19, and the small types in use here,
atomics should be safe for our usage. bools should be truly atomic,
and uint32 is atomic when the accepted value range is less than one byte/uint8 since
that can be written atomicly by concurrent processors.
Maybe that's not even a factor any more, however we don't even have to worry enough to check.

* Websocket: Fix and simplify traffic monitor

trafficMonitor had a check throttle at the end of the for loop to stop it just gobbling the (blocking) trafficAlert channel non-stop.
That makes sense, except that nothing is sent to the trafficAlert channel if there's no listener.
So that means that it's out by one second on the trafficAlert, because any traffic received during the pause is doesn't try to send a traffic alert.

The unstopped timer is deliberately leaked for later GC when shutdown.
It won't delay/block anything, and it's a trivial memory leak during an infrequent event.

Deliberately Choosing to recreate the timer each time instead of using Stop, drain and reset

* Websocket: Split traficMonitor test on behaviours

* Websocket: Remove trafficMonitor connected status

trafficMonitor does not need to set the connection to be connected.
Connect() does that. Anything after that should result in a full
shutdown and restart. It can't and shouldn't become connected
unexpectedly, and this is most likely a race anyway.

Also dropped trafficCheckInterval to 100ms to mitigate races of traffic
alerts being buffered for too long.

* Websocket: Set disconnected earlier in Shutdown

This caused a possible race where state is still connected, but we start
to trigger interested actors via ShutdownC and Wait.
They may check state and then call Shutdown again, such as
trafficMonitor

* Websocket: Wait 5s for slow tests to pass traffic draining

Keep getting failures upstream on test rigs.
Think they can be very contended, so this pushes the boundary right out
to 5s
2024-02-23 18:39:25 +11:00

890 lines
27 KiB
Go

package bithumb
import (
"context"
"errors"
"fmt"
"math"
"sort"
"strconv"
"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/currencystate"
"github.com/thrasher-corp/gocryptotrader/exchanges/deposit"
"github.com/thrasher-corp/gocryptotrader/exchanges/fundingrate"
"github.com/thrasher-corp/gocryptotrader/exchanges/futures"
"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/protocol"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
"github.com/thrasher-corp/gocryptotrader/log"
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
)
const wsRateLimitMillisecond = 1000
var errNotEnoughPairs = errors.New("at least one currency is required to fetch order history")
// GetDefaultConfig returns a default exchange config
func (b *Bithumb) GetDefaultConfig(ctx context.Context) (*config.Exchange, error) {
b.SetDefaults()
exchCfg, err := b.GetStandardConfig()
if err != nil {
return nil, err
}
err = b.SetupDefaults(exchCfg)
if err != nil {
return nil, err
}
if b.Features.Supports.RESTCapabilities.AutoPairUpdates {
err = b.UpdateTradablePairs(ctx, true)
if err != nil {
return nil, err
}
}
return exchCfg, nil
}
// SetDefaults sets the basic defaults for Bithumb
func (b *Bithumb) SetDefaults() {
b.Name = "Bithumb"
b.Enabled = true
b.Verbose = true
b.API.CredentialsValidator.RequiresKey = true
b.API.CredentialsValidator.RequiresSecret = true
requestFmt := &currency.PairFormat{Uppercase: true, Delimiter: currency.UnderscoreDelimiter}
configFmt := &currency.PairFormat{Uppercase: true, Delimiter: currency.DashDelimiter}
err := b.SetGlobalPairsManager(requestFmt, configFmt, asset.Spot)
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
b.Features = exchange.Features{
Supports: exchange.FeaturesSupported{
REST: true,
RESTCapabilities: protocol.Features{
TickerBatching: true,
TickerFetching: true,
TradeFetching: true,
OrderbookFetching: true,
AutoPairUpdates: true,
AccountInfo: true,
CryptoWithdrawal: true,
FiatDeposit: true,
FiatWithdraw: true,
GetOrder: true,
CancelOrder: true,
SubmitOrder: true,
DepositHistory: true,
WithdrawalHistory: true,
UserTradeHistory: true,
TradeFee: true,
FiatWithdrawalFee: true,
CryptoDepositFee: true,
CryptoWithdrawalFee: true,
KlineFetching: true,
},
Websocket: true,
WebsocketCapabilities: protocol.Features{
TradeFetching: true,
TickerFetching: true,
OrderbookFetching: true,
Subscribe: true,
},
WithdrawPermissions: exchange.AutoWithdrawCrypto |
exchange.AutoWithdrawFiat,
Kline: kline.ExchangeCapabilitiesSupported{
Intervals: true,
},
},
Enabled: exchange.FeaturesEnabled{
AutoPairUpdates: true,
Kline: kline.ExchangeCapabilitiesEnabled{
Intervals: kline.DeployExchangeIntervals(
kline.IntervalCapacity{Interval: kline.OneMin},
kline.IntervalCapacity{Interval: kline.ThreeMin},
kline.IntervalCapacity{Interval: kline.FiveMin},
kline.IntervalCapacity{Interval: kline.TenMin},
kline.IntervalCapacity{Interval: kline.ThirtyMin},
kline.IntervalCapacity{Interval: kline.OneHour},
// NOTE: The supported time intervals below are returned
// offset to the Asia/Seoul time zone. This may lead to
// issues with candle quality and conversion as the
// intervals may be broken up. Therefore the below intervals
// are constructed from hourly candles.
// kline.IntervalCapacity{Interval: kline.SixHour},
// kline.IntervalCapacity{Interval: kline.TwelveHour},
// kline.IntervalCapacity{Interval: kline.OneDay},
),
GlobalResultLimit: 1500,
},
},
}
b.Requester, err = request.New(b.Name,
common.NewHTTPClientWithTimeout(exchange.DefaultHTTPTimeout),
request.WithLimiter(SetRateLimit()))
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
b.API.Endpoints = b.NewEndpoints()
err = b.API.Endpoints.SetDefaultEndpoints(map[exchange.URL]string{
exchange.RestSpot: apiURL,
exchange.WebsocketSpot: wsEndpoint,
})
if err != nil {
log.Errorln(log.ExchangeSys, err)
}
b.Websocket = stream.NewWebsocket()
b.WebsocketResponseMaxLimit = exchange.DefaultWebsocketResponseMaxLimit
b.WebsocketResponseCheckTimeout = exchange.DefaultWebsocketResponseCheckTimeout
}
// Setup takes in the supplied exchange configuration details and sets params
func (b *Bithumb) Setup(exch *config.Exchange) error {
err := exch.Validate()
if err != nil {
return err
}
if !exch.Enabled {
b.SetEnabled(false)
return nil
}
err = b.SetupDefaults(exch)
if err != nil {
return err
}
location, err = time.LoadLocation("Asia/Seoul")
if err != nil {
return err
}
ePoint, err := b.API.Endpoints.GetURL(exchange.WebsocketSpot)
if err != nil {
return err
}
err = b.Websocket.Setup(&stream.WebsocketSetup{
ExchangeConfig: exch,
DefaultURL: wsEndpoint,
RunningURL: ePoint,
Connector: b.WsConnect,
Subscriber: b.Subscribe,
GenerateSubscriptions: b.GenerateSubscriptions,
Features: &b.Features.Supports.WebsocketCapabilities,
})
if err != nil {
return err
}
return b.Websocket.SetupNewConnection(stream.ConnectionSetup{
ResponseCheckTimeout: exch.WebsocketResponseCheckTimeout,
ResponseMaxLimit: exch.WebsocketResponseMaxLimit,
RateLimit: wsRateLimitMillisecond,
})
}
// FetchTradablePairs returns a list of the exchanges tradable pairs
func (b *Bithumb) FetchTradablePairs(ctx context.Context, _ asset.Item) (currency.Pairs, error) {
currencies, err := b.GetTradablePairs(ctx)
if err != nil {
return nil, err
}
pairs := make([]currency.Pair, len(currencies))
for x := range currencies {
var pair currency.Pair
pair, err = currency.NewPairFromStrings(currencies[x], "KRW")
if err != nil {
return nil, err
}
pairs[x] = pair
}
return pairs, nil
}
// UpdateTradablePairs updates the exchanges available pairs and stores
// them in the exchanges config
func (b *Bithumb) UpdateTradablePairs(ctx context.Context, forceUpdate bool) error {
pairs, err := b.FetchTradablePairs(ctx, asset.Spot)
if err != nil {
return err
}
err = b.UpdatePairs(pairs, asset.Spot, false, forceUpdate)
if err != nil {
return err
}
return b.EnsureOnePairEnabled()
}
// UpdateTickers updates the ticker for all currency pairs of a given asset type
func (b *Bithumb) UpdateTickers(ctx context.Context, a asset.Item) error {
tickers, err := b.GetAllTickers(ctx)
if err != nil {
return err
}
pairs, err := b.GetEnabledPairs(a)
if err != nil {
return err
}
for i := range pairs {
curr := pairs[i].Base.String()
t, ok := tickers[curr]
if !ok {
return fmt.Errorf("enabled pair %s [%s] not found in returned ticker map %v",
pairs[i], pairs, tickers)
}
err = ticker.ProcessTicker(&ticker.Price{
High: t.MaxPrice,
Low: t.MinPrice,
Volume: t.UnitsTraded24Hr,
Open: t.OpeningPrice,
Close: t.ClosingPrice,
Pair: pairs[i],
ExchangeName: b.Name,
AssetType: a,
})
if err != nil {
return err
}
}
return nil
}
// UpdateTicker updates and returns the ticker for a currency pair
func (b *Bithumb) UpdateTicker(ctx context.Context, p currency.Pair, a asset.Item) (*ticker.Price, error) {
if err := b.UpdateTickers(ctx, a); err != nil {
return nil, err
}
return ticker.GetTicker(b.Name, p, a)
}
// FetchTicker returns the ticker for a currency pair
func (b *Bithumb) FetchTicker(ctx context.Context, p currency.Pair, a asset.Item) (*ticker.Price, error) {
tickerNew, err := ticker.GetTicker(b.Name, p, a)
if err != nil {
return b.UpdateTicker(ctx, p, a)
}
return tickerNew, nil
}
// FetchOrderbook returns orderbook base on the currency pair
func (b *Bithumb) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
ob, err := orderbook.Get(b.Name, p, assetType)
if err != nil {
return b.UpdateOrderbook(ctx, p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (b *Bithumb) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
if p.IsEmpty() {
return nil, currency.ErrCurrencyPairEmpty
}
if err := b.CurrencyPairs.IsAssetEnabled(assetType); err != nil {
return nil, err
}
book := &orderbook.Base{
Exchange: b.Name,
Pair: p,
Asset: assetType,
VerifyOrderbook: b.CanVerifyOrderbook,
}
curr := p.Base.String()
orderbookNew, err := b.GetOrderBook(ctx, curr)
if err != nil {
return book, err
}
book.Bids = make(orderbook.Items, len(orderbookNew.Data.Bids))
for i := range orderbookNew.Data.Bids {
book.Bids[i] = orderbook.Item{
Amount: orderbookNew.Data.Bids[i].Quantity,
Price: orderbookNew.Data.Bids[i].Price,
}
}
book.Asks = make(orderbook.Items, len(orderbookNew.Data.Asks))
for i := range orderbookNew.Data.Asks {
book.Asks[i] = orderbook.Item{
Amount: orderbookNew.Data.Asks[i].Quantity,
Price: orderbookNew.Data.Asks[i].Price,
}
}
err = book.Process()
if err != nil {
return book, err
}
return orderbook.Get(b.Name, p, assetType)
}
// UpdateAccountInfo retrieves balances for all enabled currencies for the
// Bithumb exchange
func (b *Bithumb) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
var info account.Holdings
bal, err := b.GetAccountBalance(ctx, "ALL")
if err != nil {
return info, err
}
exchangeBalances := make([]account.Balance, 0, len(bal.Total))
for key, totalAmount := range bal.Total {
hold, ok := bal.InUse[key]
if !ok {
return info, fmt.Errorf("getAccountInfo error - in use item not found for currency %s",
key)
}
avail, ok := bal.Available[key]
if !ok {
avail = totalAmount - hold
}
exchangeBalances = append(exchangeBalances, account.Balance{
Currency: currency.NewCode(key),
Total: totalAmount,
Hold: hold,
Free: avail,
})
}
info.Accounts = append(info.Accounts, account.SubAccount{
Currencies: exchangeBalances,
AssetType: assetType,
})
info.Exchange = b.Name
creds, err := b.GetCredentials(ctx)
if err != nil {
return account.Holdings{}, err
}
err = account.Process(&info, creds)
if err != nil {
return account.Holdings{}, err
}
return info, nil
}
// FetchAccountInfo retrieves balances for all enabled currencies
func (b *Bithumb) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
creds, err := b.GetCredentials(ctx)
if err != nil {
return account.Holdings{}, err
}
acc, err := account.GetHoldings(b.Name, creds, assetType)
if err != nil {
return b.UpdateAccountInfo(ctx, assetType)
}
return acc, nil
}
// GetAccountFundingHistory returns funding history, deposits and
// withdrawals
func (b *Bithumb) GetAccountFundingHistory(_ context.Context) ([]exchange.FundingHistory, error) {
return nil, common.ErrFunctionNotSupported
}
// GetWithdrawalsHistory returns previous withdrawals data
func (b *Bithumb) GetWithdrawalsHistory(ctx context.Context, c currency.Code, _ asset.Item) ([]exchange.WithdrawalHistory, error) {
transactions, err := b.GetUserTransactions(ctx, 0, 0, 3, c, currency.EMPTYCODE)
if err != nil {
return nil, err
}
resp := make([]exchange.WithdrawalHistory, len(transactions.Data))
for i := range transactions.Data {
resp[i] = exchange.WithdrawalHistory{
Timestamp: time.UnixMilli(transactions.Data[i].TransferDate),
Currency: transactions.Data[i].OrderCurrency.String(),
Amount: transactions.Data[i].Amount,
Fee: transactions.Data[i].Fee,
}
}
return resp, nil
}
// GetRecentTrades returns the most recent trades for a currency and asset
func (b *Bithumb) GetRecentTrades(ctx context.Context, p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
var err error
p, err = b.FormatExchangeCurrency(p, assetType)
if err != nil {
return nil, err
}
tradeData, err := b.GetTransactionHistory(ctx, p.String())
if err != nil {
return nil, err
}
resp := make([]trade.Data, len(tradeData.Data))
for i := range tradeData.Data {
var side order.Side
side, err = order.StringToOrderSide(tradeData.Data[i].Type)
if err != nil {
return nil, err
}
var t time.Time
t, err = time.Parse(time.DateTime, tradeData.Data[i].TransactionDate)
if err != nil {
return nil, err
}
resp[i] = trade.Data{
Exchange: b.Name,
CurrencyPair: p,
AssetType: assetType,
Side: side,
Price: tradeData.Data[i].Price,
Amount: tradeData.Data[i].UnitsTraded,
Timestamp: t,
}
}
err = b.AddTradesToBuffer(resp...)
if err != nil {
return nil, err
}
sort.Sort(trade.ByDate(resp))
return resp, nil
}
// GetHistoricTrades returns historic trade data within the timeframe provided
func (b *Bithumb) GetHistoricTrades(_ context.Context, _ currency.Pair, _ asset.Item, _, _ time.Time) ([]trade.Data, error) {
return nil, common.ErrFunctionNotSupported
}
// SubmitOrder submits a new order
// TODO: Fill this out to support limit orders
func (b *Bithumb) SubmitOrder(ctx context.Context, s *order.Submit) (*order.SubmitResponse, error) {
if err := s.Validate(); err != nil {
return nil, err
}
fPair, err := b.FormatExchangeCurrency(s.Pair, s.AssetType)
if err != nil {
return nil, err
}
var orderID string
if s.Side.IsLong() {
var result MarketBuy
result, err = b.MarketBuyOrder(ctx, fPair, s.Amount)
if err != nil {
return nil, err
}
orderID = result.OrderID
} else if s.Side.IsShort() {
var result MarketSell
result, err = b.MarketSellOrder(ctx, fPair, s.Amount)
if err != nil {
return nil, err
}
orderID = result.OrderID
}
return s.DeriveSubmitResponse(orderID)
}
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (b *Bithumb) ModifyOrder(_ context.Context, _ *order.Modify) (*order.ModifyResponse, error) {
return nil, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number
func (b *Bithumb) CancelOrder(ctx context.Context, o *order.Cancel) error {
if err := o.Validate(o.StandardCancel()); err != nil {
return err
}
_, err := b.CancelTrade(ctx, o.Side.String(), o.OrderID, o.Pair.Base.String())
return err
}
// CancelBatchOrders cancels an orders by their corresponding ID numbers
func (b *Bithumb) CancelBatchOrders(_ context.Context, _ []order.Cancel) (*order.CancelBatchResponse, error) {
return nil, common.ErrFunctionNotSupported
}
// CancelAllOrders cancels all orders associated with a currency pair
func (b *Bithumb) CancelAllOrders(ctx context.Context, orderCancellation *order.Cancel) (order.CancelAllResponse, error) {
if err := orderCancellation.Validate(); err != nil {
return order.CancelAllResponse{}, err
}
cancelAllOrdersResponse := order.CancelAllResponse{
Status: make(map[string]string),
}
var allOrders []OrderData
currs, err := b.GetEnabledPairs(asset.Spot)
if err != nil {
return cancelAllOrdersResponse, err
}
for i := range currs {
orders, err := b.GetOrders(ctx, "", orderCancellation.Side.String(), 100, time.Time{}, currs[i].Base, currency.EMPTYCODE)
if err != nil {
return cancelAllOrdersResponse, err
}
allOrders = append(allOrders, orders.Data...)
}
for i := range allOrders {
_, err := b.CancelTrade(ctx,
orderCancellation.Side.String(),
allOrders[i].OrderID,
orderCancellation.Pair.Base.String())
if err != nil {
cancelAllOrdersResponse.Status[allOrders[i].OrderID] = err.Error()
}
}
return cancelAllOrdersResponse, nil
}
// GetOrderInfo returns order information based on order ID
func (b *Bithumb) GetOrderInfo(ctx context.Context, orderID string, pair currency.Pair, _ asset.Item) (*order.Detail, error) {
if pair.IsEmpty() {
return nil, currency.ErrCurrencyPairEmpty
}
orders, err := b.GetOrders(ctx, orderID, "", 0, time.Time{}, pair.Base, currency.EMPTYCODE)
if err != nil {
return nil, err
}
for i := range orders.Data {
if orders.Data[i].OrderID != orderID {
continue
}
orderDetail := order.Detail{
Amount: orders.Data[i].Units,
Exchange: b.Name,
ExecutedAmount: orders.Data[i].Units - orders.Data[i].UnitsRemaining,
OrderID: orders.Data[i].OrderID,
Date: orders.Data[i].OrderDate.Time(),
Price: orders.Data[i].Price,
RemainingAmount: orders.Data[i].UnitsRemaining,
Pair: pair,
}
if orders.Data[i].Type == "bid" {
orderDetail.Side = order.Buy
} else if orders.Data[i].Type == "ask" {
orderDetail.Side = order.Sell
}
return &orderDetail, nil
}
return nil, fmt.Errorf("%w %v", order.ErrOrderNotFound, orderID)
}
// GetDepositAddress returns a deposit address for a specified currency
func (b *Bithumb) GetDepositAddress(ctx context.Context, cryptocurrency currency.Code, _, _ string) (*deposit.Address, error) {
addr, err := b.GetWalletAddress(ctx, cryptocurrency)
if err != nil {
return nil, err
}
return &deposit.Address{
Address: addr.Data.WalletAddress,
Tag: addr.Data.Tag,
}, nil
}
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is
// submitted
func (b *Bithumb) WithdrawCryptocurrencyFunds(ctx context.Context, withdrawRequest *withdraw.Request) (*withdraw.ExchangeResponse, error) {
if err := withdrawRequest.Validate(); err != nil {
return nil, err
}
v, err := b.WithdrawCrypto(ctx,
withdrawRequest.Crypto.Address,
withdrawRequest.Crypto.AddressTag,
withdrawRequest.Currency.String(),
withdrawRequest.Amount)
if err != nil {
return nil, err
}
return &withdraw.ExchangeResponse{
ID: v.Message,
Status: v.Status,
}, err
}
// WithdrawFiatFunds returns a withdrawal ID when a
// withdrawal is submitted
func (b *Bithumb) WithdrawFiatFunds(ctx context.Context, withdrawRequest *withdraw.Request) (*withdraw.ExchangeResponse, error) {
if err := withdrawRequest.Validate(); err != nil {
return nil, err
}
if math.Trunc(withdrawRequest.Amount) != withdrawRequest.Amount {
return nil, errors.New("currency KRW does not support decimal places")
}
if !withdrawRequest.Currency.Equal(currency.KRW) {
return nil, fmt.Errorf("only KRW supported, received '%v'", withdrawRequest.Currency)
}
bankDetails := strconv.FormatFloat(withdrawRequest.Fiat.Bank.BankCode, 'f', -1, 64) +
"_" + withdrawRequest.Fiat.Bank.BankName
resp, err := b.RequestKRWWithdraw(ctx,
bankDetails,
withdrawRequest.Fiat.Bank.AccountNumber,
int64(withdrawRequest.Amount))
if err != nil {
return nil, err
}
if resp.Status != "0000" {
return nil, errors.New(resp.Message)
}
return &withdraw.ExchangeResponse{
Status: resp.Status,
}, nil
}
// WithdrawFiatFundsToInternationalBank is not supported as Bithumb only withdraws KRW to South Korean banks
func (b *Bithumb) WithdrawFiatFundsToInternationalBank(_ context.Context, _ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
return nil, common.ErrFunctionNotSupported
}
// GetFeeByType returns an estimate of fee based on type of transaction
func (b *Bithumb) GetFeeByType(ctx context.Context, feeBuilder *exchange.FeeBuilder) (float64, error) {
if feeBuilder == nil {
return 0, fmt.Errorf("%T %w", feeBuilder, common.ErrNilPointer)
}
if !b.AreCredentialsValid(ctx) && // Todo check connection status
feeBuilder.FeeType == exchange.CryptocurrencyTradeFee {
feeBuilder.FeeType = exchange.OfflineTradeFee
}
return b.GetFee(feeBuilder)
}
// GetActiveOrders retrieves any orders that are active/open
func (b *Bithumb) GetActiveOrders(ctx context.Context, req *order.MultiOrderRequest) (order.FilteredOrders, error) {
err := req.Validate()
if err != nil {
return nil, err
}
if len(req.Pairs) == 0 {
return nil, errNotEnoughPairs
}
format, err := b.GetPairFormat(req.AssetType, false)
if err != nil {
return nil, err
}
var orders []order.Detail
for x := range req.Pairs {
var resp Orders
resp, err = b.GetOrders(ctx, "", "", 1000, time.Time{}, req.Pairs[x].Base, currency.EMPTYCODE)
if err != nil {
return nil, err
}
for i := range resp.Data {
if resp.Data[i].Status != "placed" {
continue
}
orderDetail := order.Detail{
Amount: resp.Data[i].Units,
Exchange: b.Name,
ExecutedAmount: resp.Data[i].Units - resp.Data[i].UnitsRemaining,
OrderID: resp.Data[i].OrderID,
Date: resp.Data[i].OrderDate.Time(),
Price: resp.Data[i].Price,
RemainingAmount: resp.Data[i].UnitsRemaining,
Status: order.Active,
Pair: currency.NewPairWithDelimiter(resp.Data[i].OrderCurrency,
resp.Data[i].PaymentCurrency,
format.Delimiter),
}
if resp.Data[i].Type == "bid" {
orderDetail.Side = order.Buy
} else if resp.Data[i].Type == "ask" {
orderDetail.Side = order.Sell
}
orders = append(orders, orderDetail)
}
}
return req.Filter(b.Name, orders), nil
}
// GetOrderHistory retrieves account order information
// Can Limit response to specific order status
func (b *Bithumb) GetOrderHistory(ctx context.Context, req *order.MultiOrderRequest) (order.FilteredOrders, error) {
err := req.Validate()
if err != nil {
return nil, err
}
if len(req.Pairs) == 0 {
return nil, errNotEnoughPairs
}
format, err := b.GetPairFormat(req.AssetType, false)
if err != nil {
return nil, err
}
var orders []order.Detail
for x := range req.Pairs {
var resp Orders
resp, err = b.GetOrders(ctx, "", "", 1000, time.Time{}, req.Pairs[x].Base, currency.EMPTYCODE)
if err != nil {
return nil, err
}
for i := range resp.Data {
if resp.Data[i].Status == "placed" {
continue
}
orderDetail := order.Detail{
Amount: resp.Data[i].Units,
ExecutedAmount: resp.Data[i].Units - resp.Data[i].UnitsRemaining,
RemainingAmount: resp.Data[i].UnitsRemaining,
Exchange: b.Name,
OrderID: resp.Data[i].OrderID,
Date: resp.Data[i].OrderDate.Time(),
Price: resp.Data[i].Price,
Pair: currency.NewPairWithDelimiter(resp.Data[i].OrderCurrency,
resp.Data[i].PaymentCurrency,
format.Delimiter),
}
if resp.Data[i].Type == "bid" {
orderDetail.Side = order.Buy
} else if resp.Data[i].Type == "ask" {
orderDetail.Side = order.Sell
}
orderDetail.InferCostsAndTimes()
orders = append(orders, orderDetail)
}
}
return req.Filter(b.Name, orders), nil
}
// ValidateAPICredentials validates current credentials used for wrapper
// functionality
func (b *Bithumb) ValidateAPICredentials(ctx context.Context, assetType asset.Item) error {
_, err := b.UpdateAccountInfo(ctx, assetType)
return b.CheckTransientError(err)
}
// FormatExchangeKlineInterval returns Interval to exchange formatted string
func (b *Bithumb) FormatExchangeKlineInterval(in kline.Interval) string {
return in.Short()
}
// GetHistoricCandles returns candles between a time period for a set time interval
func (b *Bithumb) GetHistoricCandles(ctx context.Context, pair currency.Pair, a asset.Item, interval kline.Interval, start, end time.Time) (*kline.Item, error) {
req, err := b.GetKlineRequest(pair, a, interval, start, end, true)
if err != nil {
return nil, err
}
candle, err := b.GetCandleStick(ctx,
req.RequestFormatted.String(),
b.FormatExchangeKlineInterval(req.ExchangeInterval))
if err != nil {
return nil, err
}
timeSeries := make([]kline.Candle, 0, len(candle.Data))
for x := range candle.Data {
var tempCandle kline.Candle
if tempCandle.Time, err = convert.TimeFromUnixTimestampFloat(candle.Data[x][0]); err != nil {
return nil, fmt.Errorf("unable to convert timestamp: %w", err)
}
if tempCandle.Time.Before(req.Start) {
continue
}
if tempCandle.Time.After(req.End) {
break
}
if tempCandle.Open, err = convert.FloatFromString(candle.Data[x][1]); err != nil {
return nil, fmt.Errorf("kline open conversion failed: %w", err)
}
if tempCandle.High, err = convert.FloatFromString(candle.Data[x][2]); err != nil {
return nil, fmt.Errorf("kline high conversion failed: %w", err)
}
if tempCandle.Low, err = convert.FloatFromString(candle.Data[x][3]); err != nil {
return nil, fmt.Errorf("kline low conversion failed: %w", err)
}
if tempCandle.Close, err = convert.FloatFromString(candle.Data[x][4]); err != nil {
return nil, fmt.Errorf("kline close conversion failed: %w", err)
}
if tempCandle.Volume, err = convert.FloatFromString(candle.Data[x][5]); err != nil {
return nil, fmt.Errorf("kline volume conversion failed: %w", err)
}
timeSeries = append(timeSeries, tempCandle)
}
return req.ProcessResponse(timeSeries)
}
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
func (b *Bithumb) GetHistoricCandlesExtended(_ context.Context, _ currency.Pair, _ asset.Item, _ kline.Interval, _, _ time.Time) (*kline.Item, error) {
return nil, common.ErrFunctionNotSupported
}
// UpdateOrderExecutionLimits sets exchange executions for a required asset type
func (b *Bithumb) UpdateOrderExecutionLimits(ctx context.Context, _ asset.Item) error {
limits, err := b.FetchExchangeLimits(ctx)
if err != nil {
return fmt.Errorf("cannot update exchange execution limits: %w", err)
}
return b.LoadLimits(limits)
}
// UpdateCurrencyStates updates currency states for exchange
func (b *Bithumb) UpdateCurrencyStates(ctx context.Context, a asset.Item) error {
status, err := b.GetAssetStatusAll(ctx)
if err != nil {
return err
}
payload := make(map[currency.Code]currencystate.Options)
for coin, options := range status.Data {
payload[currency.NewCode(coin)] = currencystate.Options{
Withdraw: convert.BoolPtr(options.WithdrawalStatus == 1),
Deposit: convert.BoolPtr(options.DepositStatus == 1),
}
}
return b.States.UpdateAll(a, payload)
}
// GetServerTime returns the current exchange server time.
func (b *Bithumb) GetServerTime(_ context.Context, _ asset.Item) (time.Time, error) {
return time.Time{}, common.ErrFunctionNotSupported
}
// GetFuturesContractDetails returns all contracts from the exchange by asset type
func (b *Bithumb) GetFuturesContractDetails(context.Context, asset.Item) ([]futures.Contract, error) {
return nil, common.ErrFunctionNotSupported
}
// GetLatestFundingRates returns the latest funding rates data
func (b *Bithumb) GetLatestFundingRates(context.Context, *fundingrate.LatestRateRequest) ([]fundingrate.LatestRateResponse, error) {
return nil, common.ErrFunctionNotSupported
}