diff --git a/backtester/eventhandlers/exchange/exchange_test.go b/backtester/eventhandlers/exchange/exchange_test.go index c4487b8d004..49a2ebb998d 100644 --- a/backtester/eventhandlers/exchange/exchange_test.go +++ b/backtester/eventhandlers/exchange/exchange_test.go @@ -266,7 +266,7 @@ func TestExecuteOrder(t *testing.T) { p := currency.NewPair(currency.BTC, currency.USDT) a := asset.Spot - _, err = exch.FetchOrderbook(context.Background(), p, a) + _, err = exch.UpdateOrderbook(context.Background(), p, a) if err != nil { t.Fatal(err) } @@ -400,7 +400,7 @@ func TestExecuteOrderBuySellSizeLimit(t *testing.T) { } p := currency.NewPair(currency.BTC, currency.AUD) a := asset.Spot - _, err = exch.FetchOrderbook(context.Background(), p, a) + _, err = exch.UpdateOrderbook(context.Background(), p, a) if err != nil { t.Fatal(err) } diff --git a/backtester/eventhandlers/exchange/slippage/slippage_test.go b/backtester/eventhandlers/exchange/slippage/slippage_test.go index ec021b933e6..4eb95318b20 100644 --- a/backtester/eventhandlers/exchange/slippage/slippage_test.go +++ b/backtester/eventhandlers/exchange/slippage/slippage_test.go @@ -26,7 +26,7 @@ func TestCalculateSlippageByOrderbook(t *testing.T) { b.SetDefaults() cp := currency.NewPair(currency.BTC, currency.USD) - ob, err := b.FetchOrderbook(context.Background(), cp, asset.Spot) + ob, err := b.UpdateOrderbook(context.Background(), cp, asset.Spot) require.NoError(t, err, "FetchOrderbook must not error") amountOfFunds := decimal.NewFromInt(1000) diff --git a/cmd/exchange_template/wrapper_file.tmpl b/cmd/exchange_template/wrapper_file.tmpl index 68e1639ca31..dd48d1c71ce 100644 --- a/cmd/exchange_template/wrapper_file.tmpl +++ b/cmd/exchange_template/wrapper_file.tmpl @@ -247,24 +247,6 @@ func ({{.Variable}} *{{.CapitalName}}) UpdateTickers(ctx context.Context, assetT return nil } -// FetchTicker returns the ticker for a currency pair -func ({{.Variable}} *{{.CapitalName}}) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - tickerNew, err := ticker.GetTicker({{.Variable}}.Name, p, assetType) - if err != nil { - return {{.Variable}}.UpdateTicker(ctx, p, assetType) - } - return tickerNew, nil -} - -// FetchOrderbook returns orderbook base on the currency pair -func ({{.Variable}} *{{.CapitalName}}) FetchOrderbook(ctx context.Context, pair currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - ob, err := orderbook.Get({{.Variable}}.Name, pair, assetType) - if err != nil { - return {{.Variable}}.UpdateOrderbook(ctx, pair, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func ({{.Variable}} *{{.CapitalName}}) UpdateOrderbook(ctx context.Context, pair currency.Pair, assetType asset.Item) (*orderbook.Base, error) { book := &orderbook.Base{ @@ -313,21 +295,6 @@ func ({{.Variable}} *{{.CapitalName}}) UpdateAccountInfo(ctx context.Context, as return account.Holdings{}, common.ErrNotYetImplemented } -// FetchAccountInfo retrieves balances for all enabled currencies -func ({{.Variable}} *{{.CapitalName}}) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) { - // Example implementation below: - // creds, err := {{.Variable}}.GetCredentials(ctx) - // if err != nil { - // return account.Holdings{}, err - // } - // acc, err := account.GetHoldings({{.Variable}}.Name, creds, assetType) - // if err != nil { - // return {{.Variable}}.UpdateAccountInfo(ctx, assetType) - // } - // return acc, nil - return account.Holdings{}, common.ErrNotYetImplemented -} - // GetFundingHistory returns funding history, deposits and // withdrawals func ({{.Variable}} *{{.CapitalName}}) GetAccountFundingHistory(ctx context.Context) ([]exchange.FundingHistory, error) { diff --git a/cmd/exchange_wrapper_issues/main.go b/cmd/exchange_wrapper_issues/main.go index 0e1aa3aa350..e0f8012f51e 100644 --- a/cmd/exchange_wrapper_issues/main.go +++ b/cmd/exchange_wrapper_issues/main.go @@ -349,8 +349,8 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config) log.Printf("Executing wrappers for %v %v %v", base.GetName(), assetTypes[i], p) if !authenticatedOnly { - var fetchTickerResponse *ticker.Price - fetchTickerResponse, err = e.FetchTicker(context.TODO(), p, assetTypes[i]) + var updateTickerResponse *ticker.Price + updateTickerResponse, err = e.UpdateTicker(context.TODO(), p, assetTypes[i]) msg = "" if err != nil { msg = err.Error() @@ -358,13 +358,13 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config) } responseContainer.EndpointResponses = append(responseContainer.EndpointResponses, EndpointResponse{ SentParams: jsonifyInterface([]interface{}{p, assetTypes[i]}), - Function: "FetchTicker", + Function: "UpdateTicker", Error: msg, - Response: jsonifyInterface([]interface{}{fetchTickerResponse}), + Response: jsonifyInterface([]interface{}{updateTickerResponse}), }) - var updateTickerResponse *ticker.Price - updateTickerResponse, err = e.UpdateTicker(context.TODO(), p, assetTypes[i]) + var fetchTickerResponse *ticker.Price + fetchTickerResponse, err = e.FetchTicker(context.TODO(), p, assetTypes[i]) msg = "" if err != nil { msg = err.Error() @@ -372,13 +372,13 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config) } responseContainer.EndpointResponses = append(responseContainer.EndpointResponses, EndpointResponse{ SentParams: jsonifyInterface([]interface{}{p, assetTypes[i]}), - Function: "UpdateTicker", + Function: "FetchTicker", Error: msg, - Response: jsonifyInterface([]interface{}{updateTickerResponse}), + Response: jsonifyInterface([]interface{}{fetchTickerResponse}), }) - var fetchOrderbookResponse *orderbook.Base - fetchOrderbookResponse, err = e.FetchOrderbook(context.TODO(), p, assetTypes[i]) + var updateOrderbookResponse *orderbook.Base + updateOrderbookResponse, err = e.UpdateOrderbook(context.TODO(), p, assetTypes[i]) msg = "" if err != nil { msg = err.Error() @@ -386,13 +386,13 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config) } responseContainer.EndpointResponses = append(responseContainer.EndpointResponses, EndpointResponse{ SentParams: jsonifyInterface([]interface{}{p, assetTypes[i]}), - Function: "FetchOrderbook", + Function: "UpdateOrderbook", Error: msg, - Response: jsonifyInterface([]interface{}{fetchOrderbookResponse}), + Response: jsonifyInterface([]interface{}{updateOrderbookResponse}), }) - var updateOrderbookResponse *orderbook.Base - updateOrderbookResponse, err = e.UpdateOrderbook(context.TODO(), p, assetTypes[i]) + var fetchOrderbookResponse *orderbook.Base + fetchOrderbookResponse, err = e.FetchOrderbook(context.TODO(), p, assetTypes[i]) msg = "" if err != nil { msg = err.Error() @@ -400,9 +400,9 @@ func testWrappers(e exchange.IBotExchange, base *exchange.Base, config *Config) } responseContainer.EndpointResponses = append(responseContainer.EndpointResponses, EndpointResponse{ SentParams: jsonifyInterface([]interface{}{p, assetTypes[i]}), - Function: "UpdateOrderbook", + Function: "FetchOrderbook", Error: msg, - Response: jsonifyInterface([]interface{}{updateOrderbookResponse}), + Response: jsonifyInterface([]interface{}{fetchOrderbookResponse}), }) var fetchTradablePairsResponse []currency.Pair diff --git a/cmd/exchange_wrapper_standards/exchange_wrapper_standards_test.go b/cmd/exchange_wrapper_standards/exchange_wrapper_standards_test.go index c0c9fc9043f..98330cf18c3 100644 --- a/cmd/exchange_wrapper_standards/exchange_wrapper_standards_test.go +++ b/cmd/exchange_wrapper_standards/exchange_wrapper_standards_test.go @@ -63,7 +63,7 @@ func TestAllExchangeWrappers(t *testing.T) { // rather than skipping tests where execution is blocked, provide an expired // context, so no executions can take place var cancelFn context.CancelFunc - ctx, cancelFn = context.WithTimeout(context.Background(), 0) + ctx, cancelFn = context.WithCancel(ctx) cancelFn() } exch, assetPairs := setupExchange(ctx, t, name, cfg) @@ -176,57 +176,78 @@ func executeExchangeWrapperTests(ctx context.Context, t *testing.T, exch exchang t.Helper() iExchange := reflect.TypeOf(&exch).Elem() actualExchange := reflect.ValueOf(exch) + + var firstPriority []string + var secondPriority []string + for x := range iExchange.NumMethod() { methodName := iExchange.Method(x).Name if _, ok := excludedMethodNames[methodName]; ok { continue } - method := actualExchange.MethodByName(methodName) - - var assetLen int - for y := range method.Type().NumIn() { - input := method.Type().In(y) - for _, t := range []reflect.Type{ - assetParam, orderSubmitParam, orderModifyParam, orderCancelParam, orderCancelsParam, pairKeySliceParam, getOrdersRequestParam, latestRateRequest, - } { - if input.AssignableTo(t) { - // this allows wrapper functions that support assets types - // to be tested with all supported assets - assetLen = len(assetParams) - 1 - break + + if _, ok := priorityMethodNames[methodName]; ok { + firstPriority = append(firstPriority, methodName) + } else { + secondPriority = append(secondPriority, methodName) + } + } + handleExchangeWrapperTests(ctx, t, actualExchange, firstPriority, exch, assetParams, "PRIORITY GROUP") + handleExchangeWrapperTests(ctx, t, actualExchange, secondPriority, exch, assetParams, "SECONDARY GROUP") +} + +func handleExchangeWrapperTests(ctx context.Context, t *testing.T, actualExchange reflect.Value, methodNames []string, exch exchange.IBotExchange, assetParams []assetPair, groupTestID string) { + t.Helper() + t.Run(groupTestID, func(t *testing.T) { + t.Parallel() + for x := range methodNames { + methodName := methodNames[x] + method := actualExchange.MethodByName(methodNames[x]) + var assetLen int + for y := range method.Type().NumIn() { + input := method.Type().In(y) + for _, t := range []reflect.Type{ + assetParam, orderSubmitParam, orderModifyParam, orderCancelParam, orderCancelsParam, pairKeySliceParam, getOrdersRequestParam, latestRateRequest, + } { + if input.AssignableTo(t) { + // this allows wrapper functions that support assets types + // to be tested with all supported assets + assetLen = len(assetParams) - 1 + break + } } } - } - tt := time.Now() - e := time.Date(tt.Year(), tt.Month(), tt.Day()-1, 0, 0, 0, 0, time.UTC) - s := e.Add(-time.Hour * 24 * 2) - if methodName == "GetHistoricTrades" { - // limit trade history - e = time.Now() - s = e.Add(-time.Minute * 3) - } - for y := 0; y <= assetLen; y++ { - inputs := make([]reflect.Value, method.Type().NumIn()) - argGenerator := &MethodArgumentGenerator{ - Exchange: exch, - AssetParams: assetParams[y], - MethodName: methodName, - Start: s, - End: e, + tt := time.Now() + e := time.Date(tt.Year(), tt.Month(), tt.Day()-1, 0, 0, 0, 0, time.UTC) + s := e.Add(-time.Hour * 24 * 2) + if methodName == "GetHistoricTrades" { + // limit trade history + e = time.Now() + s = e.Add(-time.Minute * 3) } - for z := range method.Type().NumIn() { - argGenerator.MethodInputType = method.Type().In(z) - generatedArg := generateMethodArg(ctx, t, argGenerator) - inputs[z] = *generatedArg + for y := range assetLen { + inputs := make([]reflect.Value, method.Type().NumIn()) + argGenerator := &MethodArgumentGenerator{ + Exchange: exch, + AssetParams: assetParams[y], + MethodName: methodName, + Start: s, + End: e, + } + for z := range method.Type().NumIn() { + argGenerator.MethodInputType = method.Type().In(z) + generatedArg := generateMethodArg(ctx, t, argGenerator) + inputs[z] = *generatedArg + } + assetY := assetParams[y].Asset.String() + pairY := assetParams[y].Pair.String() + t.Run(methodName+"-"+assetY+"-"+pairY, func(t *testing.T) { + t.Parallel() + CallExchangeMethod(t, method, inputs, methodName, exch) + }) } - assetY := assetParams[y].Asset.String() - pairY := assetParams[y].Pair.String() - t.Run(methodName+"-"+assetY+"-"+pairY, func(t *testing.T) { - t.Parallel() - CallExchangeMethod(t, method, inputs, methodName, exch) - }) } - } + }) } // CallExchangeMethod will call an exchange's method using generated arguments @@ -548,6 +569,13 @@ type assetPair struct { Asset asset.Item } +// priorityMethodNames are called before other exchange functions +var priorityMethodNames = map[string]struct{}{ + "UpdateTickers": {}, // Is required before FetchTickers is called + "UpdateTicker": {}, // Is required before FetchTicker is called + "UpdateOrderbook": {}, // Is required before FetchOrderbook is called +} + // excludedMethodNames represent the functions that are not // currently tested under this suite due to irrelevance // or not worth checking yet @@ -589,6 +617,7 @@ var excludedMethodNames = map[string]struct{}{ "GetLeverage": {}, "SetMarginType": {}, "ChangePositionMargin": {}, + "FetchAccountInfo": {}, // Account info is not retrieved in these tests } // blockedCIExchanges are exchanges that are not able to be tested on CI diff --git a/engine/event_manager_test.go b/engine/event_manager_test.go index 70ec4703dc8..8b24ca4b710 100644 --- a/engine/event_manager_test.go +++ b/engine/event_manager_test.go @@ -306,8 +306,7 @@ func TestCheckEventCondition(t *testing.T) { t.Error("expected error") } m.m.Unlock() - _, err = exch.FetchTicker(context.Background(), - currency.NewPair(currency.BTC, currency.USD), asset.Spot) + _, err = exch.UpdateTicker(context.Background(), currency.NewPair(currency.BTC, currency.USD), asset.Spot) if !errors.Is(err, nil) { t.Errorf("error '%v', expected '%v'", err, nil) } @@ -331,8 +330,7 @@ func TestCheckEventCondition(t *testing.T) { } m.m.Unlock() - _, err = exch.FetchOrderbook(context.Background(), - currency.NewPair(currency.BTC, currency.USD), asset.Spot) + _, err = exch.UpdateOrderbook(context.Background(), currency.NewPair(currency.BTC, currency.USD), asset.Spot) if !errors.Is(err, nil) { t.Errorf("error '%v', expected '%v'", err, nil) } diff --git a/engine/helpers.go b/engine/helpers.go index 496705b0c77..70a97814fbe 100644 --- a/engine/helpers.go +++ b/engine/helpers.go @@ -50,10 +50,8 @@ import ( "github.com/thrasher-corp/gocryptotrader/exchanges/kucoin" "github.com/thrasher-corp/gocryptotrader/exchanges/lbank" "github.com/thrasher-corp/gocryptotrader/exchanges/okx" - "github.com/thrasher-corp/gocryptotrader/exchanges/orderbook" "github.com/thrasher-corp/gocryptotrader/exchanges/poloniex" "github.com/thrasher-corp/gocryptotrader/exchanges/stats" - "github.com/thrasher-corp/gocryptotrader/exchanges/ticker" "github.com/thrasher-corp/gocryptotrader/exchanges/yobit" "github.com/thrasher-corp/gocryptotrader/gctscript/vm" "github.com/thrasher-corp/gocryptotrader/log" @@ -574,26 +572,6 @@ func GetRelatableCurrencies(p currency.Pair, incOrig, incUSDT bool) currency.Pai return pairs } -// GetSpecificOrderbook returns a specific orderbook given the currency, -// exchangeName and assetType -func (bot *Engine) GetSpecificOrderbook(ctx context.Context, p currency.Pair, exchangeName string, assetType asset.Item) (*orderbook.Base, error) { - exch, err := bot.GetExchangeByName(exchangeName) - if err != nil { - return nil, err - } - return exch.FetchOrderbook(ctx, p, assetType) -} - -// GetSpecificTicker returns a specific ticker given the currency, -// exchangeName and assetType -func (bot *Engine) GetSpecificTicker(ctx context.Context, p currency.Pair, exchangeName string, assetType asset.Item) (*ticker.Price, error) { - exch, err := bot.GetExchangeByName(exchangeName) - if err != nil { - return nil, err - } - return exch.FetchTicker(ctx, p, assetType) -} - // GetCollatedExchangeAccountInfoByCoin collates individual exchange account // information and turns it into a map string of exchange.AccountCurrencyInfo func GetCollatedExchangeAccountInfoByCoin(accounts []account.Holdings) map[currency.Code]account.Balance { diff --git a/engine/helpers_test.go b/engine/helpers_test.go index a935afe3046..8f921e0cce2 100644 --- a/engine/helpers_test.go +++ b/engine/helpers_test.go @@ -30,10 +30,8 @@ import ( "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/orderbook" "github.com/thrasher-corp/gocryptotrader/exchanges/protocol" "github.com/thrasher-corp/gocryptotrader/exchanges/stats" - "github.com/thrasher-corp/gocryptotrader/exchanges/ticker" "github.com/thrasher-corp/gocryptotrader/gctscript/vm" "github.com/thrasher-corp/gocryptotrader/log" ) @@ -752,98 +750,6 @@ func TestGetExchangeNamesByCurrency(t *testing.T) { } } -func TestGetSpecificOrderbook(t *testing.T) { - t.Parallel() - e := CreateTestBot(t) - - base := orderbook.Base{ - Pair: currency.NewPair(currency.BTC, currency.USD), - Bids: []orderbook.Tranche{{Price: 1000, Amount: 1}}, - Exchange: "Bitstamp", - Asset: asset.Spot, - } - - err := base.Process() - if err != nil { - t.Fatal("Unexpected result", err) - } - - btsusd, err := currency.NewPairFromStrings("BTC", "USD") - if err != nil { - t.Fatal(err) - } - - ob, err := e.GetSpecificOrderbook(context.Background(), - btsusd, testExchange, asset.Spot) - if err != nil { - t.Fatal(err) - } - - if ob.Bids[0].Price != 1000 { - t.Fatal("Unexpected result") - } - - ethltc, err := currency.NewPairFromStrings("ETH", "LTC") - if err != nil { - t.Fatal(err) - } - - _, err = e.GetSpecificOrderbook(context.Background(), - ethltc, testExchange, asset.Spot) - if err == nil { - t.Fatal("Unexpected result") - } - - err = e.UnloadExchange(testExchange) - if err != nil { - t.Error(err) - } -} - -func TestGetSpecificTicker(t *testing.T) { - t.Parallel() - e := CreateTestBot(t) - p, err := currency.NewPairFromStrings("BTC", "USD") - if err != nil { - t.Fatal(err) - } - - err = ticker.ProcessTicker(&ticker.Price{ - Pair: p, - Last: 1000, - AssetType: asset.Spot, - ExchangeName: testExchange}) - if err != nil { - t.Fatal("ProcessTicker error", err) - } - - tick, err := e.GetSpecificTicker(context.Background(), - p, testExchange, asset.Spot) - if err != nil { - t.Fatal(err) - } - - if tick.Last != 1000 { - t.Fatal("Unexpected result") - } - - ethltc, err := currency.NewPairFromStrings("ETH", "LTC") - if err != nil { - t.Fatal(err) - } - - _, err = e.GetSpecificTicker(context.Background(), - ethltc, testExchange, asset.Spot) - if err == nil { - t.Fatal("Unexpected result") - } - - err = e.UnloadExchange(testExchange) - if err != nil { - t.Error(err) - } -} - func TestGetCollatedExchangeAccountInfoByCoin(t *testing.T) { t.Parallel() CreateTestBot(t) diff --git a/engine/rpcserver.go b/engine/rpcserver.go index 72e7e76e8b6..0c3bc4ee06f 100644 --- a/engine/rpcserver.go +++ b/engine/rpcserver.go @@ -404,24 +404,18 @@ func (s *RPCServer) GetTicker(ctx context.Context, r *gctrpc.GetTickerRequest) ( return nil, err } - err = checkParams(r.Exchange, e, a, currency.Pair{ + pair := currency.Pair{ Delimiter: r.Pair.Delimiter, Base: currency.NewCode(r.Pair.Base), Quote: currency.NewCode(r.Pair.Quote), - }) + } + + err = checkParams(r.Exchange, e, a, pair) if err != nil { return nil, err } - t, err := s.GetSpecificTicker(ctx, - currency.Pair{ - Delimiter: r.Pair.Delimiter, - Base: currency.NewCode(r.Pair.Base), - Quote: currency.NewCode(r.Pair.Quote), - }, - r.Exchange, - a, - ) + t, err := e.FetchTicker(ctx, pair, a) if err != nil { return nil, err } @@ -484,38 +478,33 @@ func (s *RPCServer) GetOrderbook(ctx context.Context, r *gctrpc.GetOrderbookRequ return nil, err } - ob, err := s.GetSpecificOrderbook(ctx, - currency.Pair{ - Delimiter: r.Pair.Delimiter, - Base: currency.NewCode(r.Pair.Base), - Quote: currency.NewCode(r.Pair.Quote), - }, - r.Exchange, - a, - ) + e, err := s.GetExchangeByName(r.Exchange) if err != nil { return nil, err } - bids := make([]*gctrpc.OrderbookItem, 0, len(ob.Bids)) - asks := make([]*gctrpc.OrderbookItem, 0, len(ob.Asks)) - ch := make(chan bool) + pair := currency.Pair{ + Delimiter: r.Pair.Delimiter, + Base: currency.NewCode(r.Pair.Base), + Quote: currency.NewCode(r.Pair.Quote), + } + + ob, err := e.FetchOrderbook(ctx, pair, a) + if err != nil { + return nil, err + } + bids := make([]*gctrpc.OrderbookItem, len(ob.Bids)) + ch := make(chan struct{}) go func() { - for _, b := range ob.Bids { - bids = append(bids, &gctrpc.OrderbookItem{ - Amount: b.Amount, - Price: b.Price, - }) + for x := range ob.Bids { + bids[x] = &gctrpc.OrderbookItem{Amount: ob.Bids[x].Amount, Price: ob.Bids[x].Price} } - ch <- true + close(ch) }() - - for _, a := range ob.Asks { - asks = append(asks, &gctrpc.OrderbookItem{ - Amount: a.Amount, - Price: a.Price, - }) + asks := make([]*gctrpc.OrderbookItem, len(ob.Asks)) + for x := range ob.Asks { + asks[x] = &gctrpc.OrderbookItem{Amount: ob.Asks[x].Amount, Price: ob.Asks[x].Price} } <-ch diff --git a/engine/sync_manager.go b/engine/sync_manager.go index 39bf8f6f795..eb76f302346 100644 --- a/engine/sync_manager.go +++ b/engine/sync_manager.go @@ -602,9 +602,7 @@ func (m *SyncManager) syncTicker(c *currencyPairSyncAgent, e exchange.IBotExchan if m.config.Verbose { log.Debugf(log.SyncMgr, "%s Using recent batching cache", exchangeName) } - result, err = e.FetchTicker(context.TODO(), - c.Pair, - c.Key.Asset) + result, err = e.FetchTicker(context.TODO(), c.Pair, c.Key.Asset) } } else { result, err = e.UpdateTicker(context.TODO(), diff --git a/exchanges/account/account.go b/exchanges/account/account.go index d389ddde210..dd348d720b3 100644 --- a/exchanges/account/account.go +++ b/exchanges/account/account.go @@ -19,9 +19,12 @@ func init() { } var ( + // ErrExchangeHoldingsNotFound is a common error for when an exchange + // holdings cannot be found + ErrExchangeHoldingsNotFound = errors.New("exchange holdings not found") + errHoldingsIsNil = errors.New("holdings cannot be nil") errExchangeNameUnset = errors.New("exchange name unset") - errExchangeHoldingsNotFound = errors.New("exchange holdings not found") errAssetHoldingsNotFound = errors.New("asset holdings not found") errExchangeAccountsNotFound = errors.New("exchange accounts not found") errNoExchangeSubAccountBalances = errors.New("no exchange sub account balances") @@ -92,7 +95,7 @@ func GetHoldings(exch string, creds *Credentials, assetType asset.Item) (Holding defer service.mu.Unlock() accounts, ok := service.exchangeAccounts[exch] if !ok { - return Holdings{}, fmt.Errorf("%s %s %w", exch, assetType, errExchangeHoldingsNotFound) + return Holdings{}, fmt.Errorf("%s %s %w", exch, assetType, ErrExchangeHoldingsNotFound) } subAccountHoldings, ok := accounts.SubAccounts[*creds] @@ -164,7 +167,7 @@ func GetBalance(exch, subAccount string, creds *Credentials, ai asset.Item, c cu accounts, ok := service.exchangeAccounts[exch] if !ok { - return nil, fmt.Errorf("%s %w", exch, errExchangeHoldingsNotFound) + return nil, fmt.Errorf("%s %w", exch, ErrExchangeHoldingsNotFound) } subAccounts, ok := accounts.SubAccounts[*creds] diff --git a/exchanges/account/account_test.go b/exchanges/account/account_test.go index a22fe0352ee..e084b5b27da 100644 --- a/exchanges/account/account_test.go +++ b/exchanges/account/account_test.go @@ -155,8 +155,8 @@ func TestGetHoldings(t *testing.T) { } _, err = GetHoldings("bla", happyCredentials, asset.Spot) - if !errors.Is(err, errExchangeHoldingsNotFound) { - t.Fatalf("received: '%v' but expected: '%v'", err, errExchangeHoldingsNotFound) + if !errors.Is(err, ErrExchangeHoldingsNotFound) { + t.Fatalf("received: '%v' but expected: '%v'", err, ErrExchangeHoldingsNotFound) } _, err = GetHoldings("bla", happyCredentials, asset.Empty) @@ -265,8 +265,8 @@ func TestGetBalance(t *testing.T) { } _, err = GetBalance("bruh", "", happyCredentials, asset.Spot, currency.BTC) - if !errors.Is(err, errExchangeHoldingsNotFound) { - t.Fatalf("received: '%v' but expected: '%v'", err, errExchangeHoldingsNotFound) + if !errors.Is(err, ErrExchangeHoldingsNotFound) { + t.Fatalf("received: '%v' but expected: '%v'", err, ErrExchangeHoldingsNotFound) } err = Process(&Holdings{ diff --git a/exchanges/alphapoint/alphapoint_wrapper.go b/exchanges/alphapoint/alphapoint_wrapper.go index a11a6203c12..11d3b34a0f9 100644 --- a/exchanges/alphapoint/alphapoint_wrapper.go +++ b/exchanges/alphapoint/alphapoint_wrapper.go @@ -138,21 +138,6 @@ func (a *Alphapoint) UpdateAccountInfo(ctx context.Context, assetType asset.Item return response, nil } -// FetchAccountInfo retrieves balances for all enabled currencies on the -// Alphapoint exchange -func (a *Alphapoint) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) { - creds, err := a.GetCredentials(ctx) - if err != nil { - return account.Holdings{}, err - } - acc, err := account.GetHoldings(a.Name, creds, assetType) - if err != nil { - return a.UpdateAccountInfo(ctx, assetType) - } - - return acc, nil -} - // UpdateTickers updates the ticker for all currency pairs of a given asset type func (a *Alphapoint) UpdateTickers(_ context.Context, _ asset.Item) error { return common.ErrFunctionNotSupported @@ -189,15 +174,6 @@ func (a *Alphapoint) UpdateTicker(ctx context.Context, p currency.Pair, assetTyp return ticker.GetTicker(a.Name, p, assetType) } -// FetchTicker returns the ticker for a currency pair -func (a *Alphapoint) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - tick, err := ticker.GetTicker(a.Name, p, assetType) - if err != nil { - return a.UpdateTicker(ctx, p, assetType) - } - return tick, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (a *Alphapoint) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if p.IsEmpty() { @@ -240,15 +216,6 @@ func (a *Alphapoint) UpdateOrderbook(ctx context.Context, p currency.Pair, asset return orderbook.Get(a.Name, p, assetType) } -// FetchOrderbook returns the orderbook for a currency pair -func (a *Alphapoint) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - ob, err := orderbook.Get(a.Name, p, assetType) - if err != nil { - return a.UpdateOrderbook(ctx, p, assetType) - } - return ob, nil -} - // GetAccountFundingHistory returns funding history, deposits and // withdrawals func (a *Alphapoint) GetAccountFundingHistory(_ context.Context) ([]exchange.FundingHistory, error) { diff --git a/exchanges/binance/binance_wrapper.go b/exchanges/binance/binance_wrapper.go index 475f274814d..660b8c95fcb 100644 --- a/exchanges/binance/binance_wrapper.go +++ b/exchanges/binance/binance_wrapper.go @@ -531,31 +531,6 @@ func (b *Binance) UpdateTicker(ctx context.Context, p currency.Pair, a asset.Ite return ticker.GetTicker(b.Name, p, a) } -// FetchTicker returns the ticker for a currency pair -func (b *Binance) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - fPair, err := b.FormatExchangeCurrency(p, assetType) - if err != nil { - return nil, err - } - - tickerNew, err := ticker.GetTicker(b.Name, fPair, assetType) - if err != nil { - return b.UpdateTicker(ctx, p, assetType) - } - return tickerNew, nil -} - -// FetchOrderbook returns orderbook base on the currency pair -func (b *Binance) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - ob, err := orderbook.Get(b.Name, p, assetType) - if err != nil { - // TODO: Disconnect update orderbook functionality from fetch orderbook - // functionality across all wrappers as this mutes potential errors. - return b.UpdateOrderbook(ctx, p, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (b *Binance) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if p.IsEmpty() { @@ -721,19 +696,6 @@ func (b *Binance) UpdateAccountInfo(ctx context.Context, assetType asset.Item) ( return info, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (b *Binance) 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 *Binance) GetAccountFundingHistory(_ context.Context) ([]exchange.FundingHistory, error) { diff --git a/exchanges/binanceus/binanceus_wrapper.go b/exchanges/binanceus/binanceus_wrapper.go index d3992d4e73e..5092578abcd 100644 --- a/exchanges/binanceus/binanceus_wrapper.go +++ b/exchanges/binanceus/binanceus_wrapper.go @@ -309,33 +309,6 @@ func (bi *Binanceus) UpdateTickers(ctx context.Context, a asset.Item) error { return nil } -// FetchTicker returns the ticker for a currency pair -func (bi *Binanceus) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - fPairs, er := bi.FormatExchangeCurrency(p, assetType) - if er != nil { - return nil, er - } - - tickerNew, er := ticker.GetTicker(bi.Name, fPairs, assetType) - if er != nil { - return bi.UpdateTicker(ctx, p, assetType) - } - return tickerNew, nil -} - -// FetchOrderbook returns orderbook base on the currency pair -func (bi *Binanceus) FetchOrderbook(ctx context.Context, pair currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - fPair, err := bi.FormatExchangeCurrency(pair, assetType) - if err != nil { - return nil, err - } - ob, err := orderbook.Get(bi.Name, fPair, assetType) - if err != nil { - return bi.UpdateOrderbook(ctx, pair, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (bi *Binanceus) UpdateOrderbook(ctx context.Context, pair currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if pair.IsEmpty() { @@ -415,19 +388,6 @@ func (bi *Binanceus) UpdateAccountInfo(ctx context.Context, assetType asset.Item return info, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (bi *Binanceus) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) { - creds, err := bi.GetCredentials(ctx) - if err != nil { - return account.Holdings{}, err - } - acc, err := account.GetHoldings(bi.Name, creds, assetType) - if err != nil { - return bi.UpdateAccountInfo(ctx, assetType) - } - return acc, nil -} - // GetAccountFundingHistory returns funding history, deposits and withdrawals func (bi *Binanceus) GetAccountFundingHistory(_ context.Context) ([]exchange.FundingHistory, error) { return nil, common.ErrFunctionNotSupported diff --git a/exchanges/bitfinex/bitfinex_wrapper.go b/exchanges/bitfinex/bitfinex_wrapper.go index 04cba828676..ba9be4c306c 100644 --- a/exchanges/bitfinex/bitfinex_wrapper.go +++ b/exchanges/bitfinex/bitfinex_wrapper.go @@ -339,36 +339,6 @@ func (b *Bitfinex) UpdateTicker(ctx context.Context, p currency.Pair, a asset.It return ticker.GetTicker(b.Name, p, a) } -// FetchTicker returns the ticker for a currency pair -func (b *Bitfinex) FetchTicker(ctx context.Context, p currency.Pair, a asset.Item) (*ticker.Price, error) { - fPair, err := b.FormatExchangeCurrency(p, a) - if err != nil { - return nil, err - } - DFPair := fPair - b.appendOptionalDelimiter(&DFPair) - tick, err := ticker.GetTicker(b.Name, DFPair, a) - if err != nil { - return b.UpdateTicker(ctx, fPair, a) - } - return tick, nil -} - -// FetchOrderbook returns the orderbook for a currency pair -func (b *Bitfinex) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - fPair, err := b.FormatExchangeCurrency(p, assetType) - if err != nil { - return nil, err - } - DFPair := fPair - b.appendOptionalDelimiter(&DFPair) - ob, err := orderbook.Get(b.Name, DFPair, assetType) - if err != nil { - return b.UpdateOrderbook(ctx, fPair, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (b *Bitfinex) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if p.IsEmpty() { @@ -493,19 +463,6 @@ func (b *Bitfinex) UpdateAccountInfo(ctx context.Context, assetType asset.Item) return response, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (b *Bitfinex) 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 *Bitfinex) GetAccountFundingHistory(_ context.Context) ([]exchange.FundingHistory, error) { diff --git a/exchanges/bitflyer/bitflyer_test.go b/exchanges/bitflyer/bitflyer_test.go index fe939bd0ff2..c51a2859291 100644 --- a/exchanges/bitflyer/bitflyer_test.go +++ b/exchanges/bitflyer/bitflyer_test.go @@ -156,18 +156,6 @@ func TestCheckFXString(t *testing.T) { } } -func TestFetchTicker(t *testing.T) { - t.Parallel() - testexch.UpdatePairsOnce(t, b) - currencies, err := b.GetAvailablePairs(asset.Spot) - if err != nil { - t.Fatal(err) - } - require.GreaterOrEqual(t, len(currencies), 1) - _, err = b.FetchTicker(context.Background(), currencies[0], asset.Spot) - assert.NoError(t, err) -} - func setFeeBuilder() *exchange.FeeBuilder { return &exchange.FeeBuilder{ Amount: 1, diff --git a/exchanges/bitflyer/bitflyer_wrapper.go b/exchanges/bitflyer/bitflyer_wrapper.go index 767c107be02..adea23c3ce0 100644 --- a/exchanges/bitflyer/bitflyer_wrapper.go +++ b/exchanges/bitflyer/bitflyer_wrapper.go @@ -182,20 +182,6 @@ func (b *Bitflyer) UpdateTicker(ctx context.Context, p currency.Pair, a asset.It return ticker.GetTicker(b.Name, fPair, a) } -// FetchTicker returns the ticker for a currency pair -func (b *Bitflyer) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - fPair, err := b.FormatExchangeCurrency(p, assetType) - if err != nil { - return nil, err - } - - tick, err := ticker.GetTicker(b.Name, fPair, assetType) - if err != nil { - return b.UpdateTicker(ctx, fPair, assetType) - } - return tick, nil -} - // CheckFXString upgrades currency pair if needed func (b *Bitflyer) CheckFXString(p currency.Pair) currency.Pair { if strings.Contains(p.Base.String(), "FX") { @@ -205,20 +191,6 @@ func (b *Bitflyer) CheckFXString(p currency.Pair) currency.Pair { return p } -// FetchOrderbook returns the orderbook for a currency pair -func (b *Bitflyer) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - fPair, err := b.FormatExchangeCurrency(p, assetType) - if err != nil { - return nil, err - } - - ob, err := orderbook.Get(b.Name, fPair, assetType) - if err != nil { - return b.UpdateOrderbook(ctx, fPair, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (b *Bitflyer) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if p.IsEmpty() { @@ -274,19 +246,6 @@ func (b *Bitflyer) UpdateAccountInfo(_ context.Context, _ asset.Item) (account.H return account.Holdings{}, common.ErrNotYetImplemented } -// FetchAccountInfo retrieves balances for all enabled currencies -func (b *Bitflyer) 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 *Bitflyer) GetAccountFundingHistory(_ context.Context) ([]exchange.FundingHistory, error) { diff --git a/exchanges/bithumb/bithumb_wrapper.go b/exchanges/bithumb/bithumb_wrapper.go index 64a463249f6..443eb84735a 100644 --- a/exchanges/bithumb/bithumb_wrapper.go +++ b/exchanges/bithumb/bithumb_wrapper.go @@ -251,24 +251,6 @@ func (b *Bithumb) UpdateTicker(ctx context.Context, p currency.Pair, a asset.Ite 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() { @@ -361,19 +343,6 @@ func (b *Bithumb) UpdateAccountInfo(ctx context.Context, assetType asset.Item) ( 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) { diff --git a/exchanges/bitmex/bitmex_wrapper.go b/exchanges/bitmex/bitmex_wrapper.go index eea24d81326..1303bae680e 100644 --- a/exchanges/bitmex/bitmex_wrapper.go +++ b/exchanges/bitmex/bitmex_wrapper.go @@ -399,34 +399,6 @@ func (b *Bitmex) UpdateTicker(ctx context.Context, p currency.Pair, a asset.Item return ticker.GetTicker(b.Name, fPair, a) } -// FetchTicker returns the ticker for a currency pair -func (b *Bitmex) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - fPair, err := b.FormatExchangeCurrency(p, assetType) - if err != nil { - return nil, err - } - - tickerNew, err := ticker.GetTicker(b.Name, fPair, assetType) - if err != nil { - return b.UpdateTicker(ctx, fPair, assetType) - } - return tickerNew, nil -} - -// FetchOrderbook returns orderbook base on the currency pair -func (b *Bitmex) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - fPair, err := b.FormatExchangeCurrency(p, assetType) - if err != nil { - return nil, err - } - - ob, err := orderbook.Get(b.Name, fPair, assetType) - if err != nil { - return b.UpdateOrderbook(ctx, fPair, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (b *Bitmex) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if p.IsEmpty() { @@ -533,19 +505,6 @@ func (b *Bitmex) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (a return info, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (b *Bitmex) 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 *Bitmex) GetAccountFundingHistory(ctx context.Context) ([]exchange.FundingHistory, error) { diff --git a/exchanges/bitstamp/bitstamp_wrapper.go b/exchanges/bitstamp/bitstamp_wrapper.go index 36d02f80195..1d6c6588dfe 100644 --- a/exchanges/bitstamp/bitstamp_wrapper.go +++ b/exchanges/bitstamp/bitstamp_wrapper.go @@ -274,20 +274,6 @@ func (b *Bitstamp) UpdateTicker(ctx context.Context, p currency.Pair, a asset.It return ticker.GetTicker(b.Name, fPair, a) } -// FetchTicker returns the ticker for a currency pair -func (b *Bitstamp) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - fPair, err := b.FormatExchangeCurrency(p, assetType) - if err != nil { - return nil, err - } - - tick, err := ticker.GetTicker(b.Name, fPair, assetType) - if err != nil { - return b.UpdateTicker(ctx, fPair, assetType) - } - return tick, nil -} - // GetFeeByType returns an estimate of fee based on type of transaction func (b *Bitstamp) GetFeeByType(ctx context.Context, feeBuilder *exchange.FeeBuilder) (float64, error) { if feeBuilder == nil { @@ -300,20 +286,6 @@ func (b *Bitstamp) GetFeeByType(ctx context.Context, feeBuilder *exchange.FeeBui return b.GetFee(ctx, feeBuilder) } -// FetchOrderbook returns the orderbook for a currency pair -func (b *Bitstamp) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - fPair, err := b.FormatExchangeCurrency(p, assetType) - if err != nil { - return nil, err - } - - ob, err := orderbook.Get(b.Name, fPair, assetType) - if err != nil { - return b.UpdateOrderbook(ctx, fPair, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (b *Bitstamp) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if p.IsEmpty() { @@ -399,19 +371,6 @@ func (b *Bitstamp) UpdateAccountInfo(ctx context.Context, assetType asset.Item) return response, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (b *Bitstamp) 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 *Bitstamp) GetAccountFundingHistory(_ context.Context) ([]exchange.FundingHistory, error) { diff --git a/exchanges/btcmarkets/btcmarkets_wrapper.go b/exchanges/btcmarkets/btcmarkets_wrapper.go index e8abb596a0a..57ddfa7c0da 100644 --- a/exchanges/btcmarkets/btcmarkets_wrapper.go +++ b/exchanges/btcmarkets/btcmarkets_wrapper.go @@ -264,29 +264,6 @@ func (b *BTCMarkets) UpdateTicker(ctx context.Context, p currency.Pair, a asset. return ticker.GetTicker(b.Name, p, a) } -// FetchTicker returns the ticker for a currency pair -func (b *BTCMarkets) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - fPair, err := b.FormatExchangeCurrency(p, assetType) - if err != nil { - return nil, err - } - - tickerNew, err := ticker.GetTicker(b.Name, fPair, assetType) - if err != nil { - return b.UpdateTicker(ctx, p, assetType) - } - return tickerNew, nil -} - -// FetchOrderbook returns orderbook base on the currency pair -func (b *BTCMarkets) 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 *BTCMarkets) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if p.IsEmpty() { @@ -369,20 +346,6 @@ func (b *BTCMarkets) UpdateAccountInfo(ctx context.Context, assetType asset.Item return resp, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (b *BTCMarkets) 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 *BTCMarkets) GetAccountFundingHistory(_ context.Context) ([]exchange.FundingHistory, error) { diff --git a/exchanges/btse/btse_wrapper.go b/exchanges/btse/btse_wrapper.go index 72d25f42925..a50b20843a3 100644 --- a/exchanges/btse/btse_wrapper.go +++ b/exchanges/btse/btse_wrapper.go @@ -307,24 +307,6 @@ func (b *BTSE) UpdateTicker(ctx context.Context, p currency.Pair, a asset.Item) return ticker.GetTicker(b.Name, p, a) } -// FetchTicker returns the ticker for a currency pair -func (b *BTSE) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - tickerNew, err := ticker.GetTicker(b.Name, p, assetType) - if err != nil { - return b.UpdateTicker(ctx, p, assetType) - } - return tickerNew, nil -} - -// FetchOrderbook returns orderbook base on the currency pair -func (b *BTSE) 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 *BTSE) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if p.IsEmpty() { @@ -417,20 +399,6 @@ func (b *BTSE) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (acc return a, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (b *BTSE) 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 *BTSE) GetAccountFundingHistory(_ context.Context) ([]exchange.FundingHistory, error) { diff --git a/exchanges/bybit/bybit_wrapper.go b/exchanges/bybit/bybit_wrapper.go index acb3ae66949..a0666e7ae93 100644 --- a/exchanges/bybit/bybit_wrapper.go +++ b/exchanges/bybit/bybit_wrapper.go @@ -476,29 +476,6 @@ func (by *Bybit) UpdateTicker(ctx context.Context, p currency.Pair, assetType as return ticker.GetTicker(by.Name, p, assetType) } -// FetchTicker returns the ticker for a currency pair -func (by *Bybit) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - fPair, err := by.FormatExchangeCurrency(p, assetType) - if err != nil { - return nil, err - } - - tickerNew, err := ticker.GetTicker(by.Name, fPair, assetType) - if err != nil { - return by.UpdateTicker(ctx, p, assetType) - } - return tickerNew, nil -} - -// FetchOrderbook returns orderbook base on the currency pair -func (by *Bybit) FetchOrderbook(ctx context.Context, currency currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - ob, err := orderbook.Get(by.Name, currency, assetType) - if err != nil { - return by.UpdateOrderbook(ctx, currency, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (by *Bybit) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if p.IsEmpty() { @@ -619,20 +596,6 @@ func (by *Bybit) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (a return info, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (by *Bybit) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) { - creds, err := by.GetCredentials(ctx) - if err != nil { - return account.Holdings{}, err - } - acc, err := account.GetHoldings(by.Name, creds, assetType) - if err != nil { - return by.UpdateAccountInfo(ctx, assetType) - } - - return acc, nil -} - // GetAccountFundingHistory returns funding history, deposits and // withdrawals func (by *Bybit) GetAccountFundingHistory(_ context.Context) ([]exchange.FundingHistory, error) { diff --git a/exchanges/coinbasepro/coinbasepro_wrapper.go b/exchanges/coinbasepro/coinbasepro_wrapper.go index d5d9173db78..20f8cf052b4 100644 --- a/exchanges/coinbasepro/coinbasepro_wrapper.go +++ b/exchanges/coinbasepro/coinbasepro_wrapper.go @@ -256,19 +256,6 @@ func (c *CoinbasePro) UpdateAccountInfo(ctx context.Context, assetType asset.Ite return response, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (c *CoinbasePro) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) { - creds, err := c.GetCredentials(ctx) - if err != nil { - return account.Holdings{}, err - } - acc, err := account.GetHoldings(c.Name, creds, assetType) - if err != nil { - return c.UpdateAccountInfo(ctx, assetType) - } - return acc, nil -} - // UpdateTickers updates the ticker for all currency pairs of a given asset type func (c *CoinbasePro) UpdateTickers(_ context.Context, _ asset.Item) error { return common.ErrFunctionNotSupported @@ -311,24 +298,6 @@ func (c *CoinbasePro) UpdateTicker(ctx context.Context, p currency.Pair, a asset return ticker.GetTicker(c.Name, p, a) } -// FetchTicker returns the ticker for a currency pair -func (c *CoinbasePro) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - tickerNew, err := ticker.GetTicker(c.Name, p, assetType) - if err != nil { - return c.UpdateTicker(ctx, p, assetType) - } - return tickerNew, nil -} - -// FetchOrderbook returns orderbook base on the currency pair -func (c *CoinbasePro) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - ob, err := orderbook.Get(c.Name, p, assetType) - if err != nil { - return c.UpdateOrderbook(ctx, p, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (c *CoinbasePro) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if p.IsEmpty() { diff --git a/exchanges/coinut/coinut_wrapper.go b/exchanges/coinut/coinut_wrapper.go index 20ae1cf7094..3ac27943a2f 100644 --- a/exchanges/coinut/coinut_wrapper.go +++ b/exchanges/coinut/coinut_wrapper.go @@ -294,19 +294,6 @@ func (c *COINUT) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (a return info, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (c *COINUT) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) { - creds, err := c.GetCredentials(ctx) - if err != nil { - return account.Holdings{}, err - } - acc, err := account.GetHoldings(c.Name, creds, assetType) - if err != nil { - return c.UpdateAccountInfo(ctx, assetType) - } - return acc, nil -} - // UpdateTickers updates the ticker for all currency pairs of a given asset type func (c *COINUT) UpdateTickers(_ context.Context, _ asset.Item) error { return common.ErrFunctionNotSupported @@ -358,24 +345,6 @@ func (c *COINUT) UpdateTicker(ctx context.Context, p currency.Pair, a asset.Item return ticker.GetTicker(c.Name, p, a) } -// FetchTicker returns the ticker for a currency pair -func (c *COINUT) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - tickerNew, err := ticker.GetTicker(c.Name, p, assetType) - if err != nil { - return c.UpdateTicker(ctx, p, assetType) - } - return tickerNew, nil -} - -// FetchOrderbook returns orderbook base on the currency pair -func (c *COINUT) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - ob, err := orderbook.Get(c.Name, p, assetType) - if err != nil { - return c.UpdateOrderbook(ctx, p, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (c *COINUT) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if p.IsEmpty() { diff --git a/exchanges/exchange.go b/exchanges/exchange.go index 4369b1dd110..334d34840a8 100644 --- a/exchanges/exchange.go +++ b/exchanges/exchange.go @@ -19,6 +19,7 @@ import ( "github.com/thrasher-corp/gocryptotrader/common/key" "github.com/thrasher-corp/gocryptotrader/config" "github.com/thrasher-corp/gocryptotrader/currency" + "github.com/thrasher-corp/gocryptotrader/exchanges/account" "github.com/thrasher-corp/gocryptotrader/exchanges/asset" "github.com/thrasher-corp/gocryptotrader/exchanges/collateral" "github.com/thrasher-corp/gocryptotrader/exchanges/currencystate" @@ -27,6 +28,7 @@ import ( "github.com/thrasher-corp/gocryptotrader/exchanges/kline" "github.com/thrasher-corp/gocryptotrader/exchanges/margin" "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" @@ -1943,3 +1945,30 @@ func (b *Base) GetTradingRequirements() protocol.TradingRequirements { } return b.Features.TradingRequirements } + +// FetchTicker returns the ticker for a currency pair and asset type +// associated with the exchange. +// NOTE: UpdateTicker (or if supported UpdateTickers) method must be +// called first to update the ticker map. +func (b *Base) FetchTicker(_ context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { + return ticker.GetTicker(b.Name, p, assetType) +} + +// FetchOrderbook returns orderbook base on the currency pair and asset type +// associated with the exchange. +// NOTE: UpdateOrderbook method must be called first to update the orderbook +// map. +func (b *Base) FetchOrderbook(_ context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { + return orderbook.Get(b.Name, p, assetType) +} + +// FetchAccountInfo retrieves balances for all enabled currencies +// NOTE: UpdateAccountInfo method must be called first to update the +// account info map. +func (b *Base) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) { + creds, err := b.GetCredentials(ctx) + if err != nil { + return account.Holdings{}, err + } + return account.GetHoldings(b.Name, creds, assetType) +} diff --git a/exchanges/exchange_test.go b/exchanges/exchange_test.go index a5db589d047..6612ccd30c5 100644 --- a/exchanges/exchange_test.go +++ b/exchanges/exchange_test.go @@ -2906,6 +2906,68 @@ func TestCanUseAuthenticatedWebsocketEndpoints(t *testing.T) { assert.True(t, e.CanUseAuthenticatedWebsocketEndpoints()) } +func TestFetchTicker(t *testing.T) { + t.Parallel() + b := Base{Name: "test"} + pair := currency.NewPair(currency.BTC, currency.USDT) + _, err := b.FetchTicker(context.Background(), pair, asset.Spot) + assert.ErrorIs(t, err, ticker.ErrNoTickerFound) + + err = ticker.ProcessTicker(&ticker.Price{ + ExchangeName: "test", + Pair: pair, + AssetType: asset.Spot, + }) + assert.NoError(t, err) + + tickerPrice, err := b.FetchTicker(context.Background(), pair, asset.Spot) + assert.NoError(t, err) + assert.Equal(t, tickerPrice.Pair, pair) +} + +func TestFetchOrderbook(t *testing.T) { + t.Parallel() + b := Base{Name: "test"} + pair := currency.NewPair(currency.BTC, currency.USDT) + _, err := b.FetchOrderbook(context.Background(), pair, asset.Spot) + assert.ErrorIs(t, err, orderbook.ErrCannotFindOrderbook) + + err = (&orderbook.Base{ + Exchange: "test", + Pair: pair, + Asset: asset.Spot, + }).Process() + assert.NoError(t, err) + + ob, err := b.FetchOrderbook(context.Background(), pair, asset.Spot) + assert.NoError(t, err) + assert.Equal(t, ob.Pair, pair) +} + +func TestFetchAccountInfo(t *testing.T) { + t.Parallel() + b := Base{Name: "test"} + + creds := &account.Credentials{ + Key: "test", + Secret: "test", + } + ctx := account.DeployCredentialsToContext(context.Background(), &account.Credentials{ + Key: "test", + Secret: "test", + }) + _, err := b.FetchAccountInfo(ctx, asset.Spot) + assert.ErrorIs(t, err, account.ErrExchangeHoldingsNotFound) + + err = account.Process(&account.Holdings{Exchange: "test", Accounts: []account.SubAccount{ + {AssetType: asset.Spot, Currencies: []account.Balance{{Currency: currency.BTC, Total: 1}}}, + }}, creds) + assert.NoError(t, err) + + _, err = b.FetchAccountInfo(ctx, asset.Spot) + assert.NoError(t, err) +} + // FakeBase is used to override functions type FakeBase struct{ Base } diff --git a/exchanges/exmo/exmo_wrapper.go b/exchanges/exmo/exmo_wrapper.go index 611d387cd61..3c8ae77c675 100644 --- a/exchanges/exmo/exmo_wrapper.go +++ b/exchanges/exmo/exmo_wrapper.go @@ -201,24 +201,6 @@ func (e *EXMO) UpdateTicker(ctx context.Context, p currency.Pair, a asset.Item) return ticker.GetTicker(e.Name, p, a) } -// FetchTicker returns the ticker for a currency pair -func (e *EXMO) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - tick, err := ticker.GetTicker(e.Name, p, assetType) - if err != nil { - return e.UpdateTicker(ctx, p, assetType) - } - return tick, nil -} - -// FetchOrderbook returns the orderbook for a currency pair -func (e *EXMO) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - ob, err := orderbook.Get(e.Name, p, assetType) - if err != nil { - return e.UpdateOrderbook(ctx, p, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (e *EXMO) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if p.IsEmpty() { @@ -363,19 +345,6 @@ func (e *EXMO) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (acc return response, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (e *EXMO) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) { - creds, err := e.GetCredentials(ctx) - if err != nil { - return account.Holdings{}, err - } - acc, err := account.GetHoldings(e.Name, creds, assetType) - if err != nil { - return e.UpdateAccountInfo(ctx, assetType) - } - return acc, nil -} - // GetAccountFundingHistory returns funding history, deposits and // withdrawals func (e *EXMO) GetAccountFundingHistory(ctx context.Context) ([]exchange.FundingHistory, error) { diff --git a/exchanges/gateio/gateio_test.go b/exchanges/gateio/gateio_test.go index d5d4bfe6c98..4fd94fa99c8 100644 --- a/exchanges/gateio/gateio_test.go +++ b/exchanges/gateio/gateio_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "log" "os" "slices" @@ -2711,26 +2712,20 @@ func TestOptionsCandlesticksPushData(t *testing.T) { const ( optionsOrderbookTickerPushDataJSON = `{ "time": 1630650452, "channel": "options.book_ticker", "event": "update", "result": { "t": 1615366379123, "u": 2517661076, "s": "BTC_USDT-20211130-50000-C", "b": "54696.6", "B": 37000, "a": "54696.7", "A": 47061 }}` - optionsOrderbookUpdatePushDataJSON = `{ "time": 1630650445, "channel": "options.order_book_update", "event": "update", "result": { "t": 1615366381417, "s": "BTC_USDT-20211130-50000-C", "U": 2517661101, "u": 2517661113, "b": [ { "p": "54672.1", "s": 95 }, { "p": "54664.5", "s": 58794 } ], "a": [ { "p": "54743.6", "s": 95 }, { "p": "54742", "s": 95 } ] }}` + optionsOrderbookUpdatePushDataJSON = `{ "time": 1630650445, "channel": "options.order_book_update", "event": "update", "result": { "t": 1615366381417, "s": "%s", "U": 2517661101, "u": 2517661113, "b": [ { "p": "54672.1", "s": 95 }, { "p": "54664.5", "s": 58794 } ], "a": [ { "p": "54743.6", "s": 95 }, { "p": "54742", "s": 95 } ] }}` optionsOrderbookSnapshotPushDataJSON = `{ "time": 1630650445, "channel": "options.order_book", "event": "all", "result": { "t": 1541500161123, "contract": "BTC_USDT-20211130-50000-C", "id": 93973511, "asks": [ { "p": "97.1", "s": 2245 }, { "p": "97.2", "s": 2245 } ], "bids": [ { "p": "97.2", "s": 2245 }, { "p": "97.1", "s": 2245 } ] }}` optionsOrderbookSnapshotUpdateEventPushDataJSON = `{"channel": "options.order_book", "event": "update", "time": 1630650445, "result": [ { "p": "49525.6", "s": 7726, "c": "BTC_USDT-20211130-50000-C", "id": 93973511 } ]}` ) func TestOptionsOrderbookPushData(t *testing.T) { t.Parallel() - err := g.WsHandleOptionsData(context.Background(), []byte(optionsOrderbookTickerPushDataJSON)) - if err != nil { - t.Errorf("%s websocket options orderbook ticker push data error: %v", g.Name, err) - } - if err = g.WsHandleOptionsData(context.Background(), []byte(optionsOrderbookSnapshotPushDataJSON)); err != nil { - t.Errorf("%s websocket options orderbook snapshot push data error: %v", g.Name, err) - } - if err = g.WsHandleOptionsData(context.Background(), []byte(optionsOrderbookUpdatePushDataJSON)); err != nil { - t.Errorf("%s websocket options orderbook update push data error: %v", g.Name, err) - } - if err = g.WsHandleOptionsData(context.Background(), []byte(optionsOrderbookSnapshotUpdateEventPushDataJSON)); err != nil { - t.Errorf("%s websocket options orderbook snapshot update event push data error: %v", g.Name, err) - } + testexch.UpdatePairsOnce(t, g) + assert.NoError(t, g.WsHandleOptionsData(context.Background(), []byte(optionsOrderbookTickerPushDataJSON))) + avail, err := g.GetAvailablePairs(asset.Options) + require.NoError(t, err) + assert.NoError(t, g.WsHandleOptionsData(context.Background(), []byte(fmt.Sprintf(optionsOrderbookUpdatePushDataJSON, avail[0].Upper().String())))) + assert.NoError(t, g.WsHandleOptionsData(context.Background(), []byte(optionsOrderbookSnapshotPushDataJSON))) + assert.NoError(t, g.WsHandleOptionsData(context.Background(), []byte(optionsOrderbookSnapshotUpdateEventPushDataJSON))) } const optionsOrderPushDataJSON = `{"time": 1630654851,"channel": "options.orders", "event": "update", "result": [ { "contract": "BTC_USDT-20211130-65000-C", "create_time": 1637897000, "fill_price": 0, "finish_as": "cancelled", "iceberg": 0, "id": 106, "is_close": false, "is_liq": false, "is_reduce_only": false, "left": -10, "mkfr": 0.0004, "price": 15000, "refr": 0, "refu": 0, "size": -10, "status": "finished", "text": "web", "tif": "gtc", "tkfr": 0.0004, "underlying": "BTC_USDT", "user": "9xxx", "time": 1639051907,"time_ms": 1639051907000}]}` diff --git a/exchanges/gateio/gateio_websocket.go b/exchanges/gateio/gateio_websocket.go index ea53f12029a..22e57d7c1fe 100644 --- a/exchanges/gateio/gateio_websocket.go +++ b/exchanges/gateio/gateio_websocket.go @@ -349,7 +349,7 @@ func (g *Gateio) processOrderbookUpdate(incoming []byte, updatePushedAt time.Tim sPair := data.CurrencyPair.String() if !fetchedCurrencyPairSnapshotOrderbook[sPair] { - orderbooks, err := g.FetchOrderbook(context.Background(), data.CurrencyPair, asset.Spot) // currency pair orderbook data for Spot, Margin, and Cross Margin is same + orderbooks, err := g.UpdateOrderbook(context.Background(), data.CurrencyPair, asset.Spot) // currency pair orderbook data for Spot, Margin, and Cross Margin is same if err != nil { return err } diff --git a/exchanges/gateio/gateio_websocket_futures.go b/exchanges/gateio/gateio_websocket_futures.go index ba393277553..31569b744e6 100644 --- a/exchanges/gateio/gateio_websocket_futures.go +++ b/exchanges/gateio/gateio_websocket_futures.go @@ -421,7 +421,7 @@ func (g *Gateio) processFuturesAndOptionsOrderbookUpdate(incoming []byte, assetT } if (assetType == asset.Options && !fetchedOptionsCurrencyPairSnapshotOrderbook[data.ContractName.String()]) || (assetType != asset.Options && !fetchedFuturesCurrencyPairSnapshotOrderbook[data.ContractName.String()]) { - orderbooks, err := g.FetchOrderbook(context.Background(), data.ContractName, assetType) + orderbooks, err := g.UpdateOrderbook(context.Background(), data.ContractName, assetType) if err != nil { return err } diff --git a/exchanges/gateio/gateio_wrapper.go b/exchanges/gateio/gateio_wrapper.go index 9d9929f6e56..556a492da30 100644 --- a/exchanges/gateio/gateio_wrapper.go +++ b/exchanges/gateio/gateio_wrapper.go @@ -438,19 +438,6 @@ func (g *Gateio) UpdateTicker(ctx context.Context, p currency.Pair, a asset.Item return ticker.GetTicker(g.Name, fPair, a) } -// FetchTicker retrieves a list of tickers. -func (g *Gateio) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - fPair, err := g.FormatExchangeCurrency(p, assetType) - if err != nil { - return nil, err - } - tickerNew, err := ticker.GetTicker(g.Name, fPair, assetType) - if err != nil { - return g.UpdateTicker(ctx, fPair, assetType) - } - return tickerNew, nil -} - // FetchTradablePairs returns a list of the exchanges tradable pairs func (g *Gateio) FetchTradablePairs(ctx context.Context, a asset.Item) (currency.Pairs, error) { if !g.SupportsAsset(a) { @@ -704,15 +691,6 @@ func (g *Gateio) UpdateTickers(ctx context.Context, a asset.Item) error { return nil } -// FetchOrderbook returns orderbook base on the currency pair -func (g *Gateio) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - ob, err := orderbook.Get(g.Name, p, assetType) - if err != nil { - return g.UpdateOrderbook(ctx, p, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (g *Gateio) UpdateOrderbook(ctx context.Context, p currency.Pair, a asset.Item) (*orderbook.Base, error) { p, err := g.FormatExchangeCurrency(p, a) @@ -896,19 +874,6 @@ func (g *Gateio) UpdateAccountInfo(ctx context.Context, a asset.Item) (account.H return info, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (g *Gateio) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) { - creds, err := g.GetCredentials(ctx) - if err != nil { - return account.Holdings{}, err - } - acc, err := account.GetHoldings(g.Name, creds, assetType) - if err != nil { - return g.UpdateAccountInfo(ctx, assetType) - } - return acc, nil -} - // GetAccountFundingHistory returns funding history, deposits and // withdrawals func (g *Gateio) GetAccountFundingHistory(_ context.Context) ([]exchange.FundingHistory, error) { diff --git a/exchanges/gemini/gemini_wrapper.go b/exchanges/gemini/gemini_wrapper.go index 6c71964fb5e..69c9437d7fc 100644 --- a/exchanges/gemini/gemini_wrapper.go +++ b/exchanges/gemini/gemini_wrapper.go @@ -251,19 +251,6 @@ func (g *Gemini) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (a return response, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (g *Gemini) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) { - creds, err := g.GetCredentials(ctx) - if err != nil { - return account.Holdings{}, err - } - acc, err := account.GetHoldings(g.Name, creds, assetType) - if err != nil { - return g.UpdateAccountInfo(ctx, assetType) - } - return acc, nil -} - // UpdateTickers updates the ticker for all currency pairs of a given asset type func (g *Gemini) UpdateTickers(_ context.Context, _ asset.Item) error { return common.ErrFunctionNotSupported @@ -298,34 +285,6 @@ func (g *Gemini) UpdateTicker(ctx context.Context, p currency.Pair, a asset.Item return ticker.GetTicker(g.Name, fPair, a) } -// FetchTicker returns the ticker for a currency pair -func (g *Gemini) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - fPair, err := g.FormatExchangeCurrency(p, assetType) - if err != nil { - return nil, err - } - - tickerNew, err := ticker.GetTicker(g.Name, fPair, assetType) - if err != nil { - return g.UpdateTicker(ctx, fPair, assetType) - } - return tickerNew, nil -} - -// FetchOrderbook returns orderbook base on the currency pair -func (g *Gemini) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - fPair, err := g.FormatExchangeCurrency(p, assetType) - if err != nil { - return nil, err - } - - ob, err := orderbook.Get(g.Name, fPair, assetType) - if err != nil { - return g.UpdateOrderbook(ctx, fPair, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (g *Gemini) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if p.IsEmpty() { diff --git a/exchanges/hitbtc/hitbtc_test.go b/exchanges/hitbtc/hitbtc_test.go index e3a1c544a70..fdd21f4ecf7 100644 --- a/exchanges/hitbtc/hitbtc_test.go +++ b/exchanges/hitbtc/hitbtc_test.go @@ -168,10 +168,6 @@ func TestUpdateTicker(t *testing.T) { if err != nil { t.Error(err) } - _, err = h.FetchTicker(context.Background(), pairs[1], asset.Spot) - if err != nil { - t.Error(err) - } } func TestUpdateTickers(t *testing.T) { diff --git a/exchanges/hitbtc/hitbtc_wrapper.go b/exchanges/hitbtc/hitbtc_wrapper.go index 8a57bc7ee78..15b87f03fcc 100644 --- a/exchanges/hitbtc/hitbtc_wrapper.go +++ b/exchanges/hitbtc/hitbtc_wrapper.go @@ -259,24 +259,6 @@ func (h *HitBTC) UpdateTicker(ctx context.Context, p currency.Pair, a asset.Item return ticker.GetTicker(h.Name, p, a) } -// FetchTicker returns the ticker for a currency pair -func (h *HitBTC) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - tickerNew, err := ticker.GetTicker(h.Name, p, assetType) - if err != nil { - return h.UpdateTicker(ctx, p, assetType) - } - return tickerNew, nil -} - -// FetchOrderbook returns orderbook base on the currency pair -func (h *HitBTC) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - ob, err := orderbook.Get(h.Name, p, assetType) - if err != nil { - return h.UpdateOrderbook(ctx, p, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (h *HitBTC) UpdateOrderbook(ctx context.Context, c currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if c.IsEmpty() { @@ -359,20 +341,6 @@ func (h *HitBTC) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (a return response, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (h *HitBTC) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) { - creds, err := h.GetCredentials(ctx) - if err != nil { - return account.Holdings{}, err - } - acc, err := account.GetHoldings(h.Name, creds, assetType) - if err != nil { - return h.UpdateAccountInfo(ctx, assetType) - } - - return acc, nil -} - // GetAccountFundingHistory returns funding history, deposits and // withdrawals func (h *HitBTC) GetAccountFundingHistory(_ context.Context) ([]exchange.FundingHistory, error) { diff --git a/exchanges/huobi/huobi_wrapper.go b/exchanges/huobi/huobi_wrapper.go index 76c461cb196..c14184b22eb 100644 --- a/exchanges/huobi/huobi_wrapper.go +++ b/exchanges/huobi/huobi_wrapper.go @@ -560,24 +560,6 @@ func (h *HUOBI) UpdateTicker(ctx context.Context, p currency.Pair, a asset.Item) return ticker.GetTicker(h.Name, p, a) } -// FetchTicker returns the ticker for a currency pair -func (h *HUOBI) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - tickerNew, err := ticker.GetTicker(h.Name, p, assetType) - if err != nil { - return h.UpdateTicker(ctx, p, assetType) - } - return tickerNew, nil -} - -// FetchOrderbook returns orderbook base on the currency pair -func (h *HUOBI) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - ob, err := orderbook.Get(h.Name, p, assetType) - if err != nil { - return h.UpdateOrderbook(ctx, p, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (h *HUOBI) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if p.IsEmpty() { @@ -865,19 +847,6 @@ func (h *HUOBI) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (ac return info, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (h *HUOBI) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) { - creds, err := h.GetCredentials(ctx) - if err != nil { - return account.Holdings{}, err - } - acc, err := account.GetHoldings(h.Name, creds, assetType) - if err != nil { - return h.UpdateAccountInfo(ctx, assetType) - } - return acc, nil -} - // GetAccountFundingHistory returns funding history, deposits and // withdrawals func (h *HUOBI) GetAccountFundingHistory(_ context.Context) ([]exchange.FundingHistory, error) { diff --git a/exchanges/kraken/kraken_wrapper.go b/exchanges/kraken/kraken_wrapper.go index 610350c27f8..eba13ac71ad 100644 --- a/exchanges/kraken/kraken_wrapper.go +++ b/exchanges/kraken/kraken_wrapper.go @@ -474,24 +474,6 @@ func (k *Kraken) UpdateTicker(ctx context.Context, p currency.Pair, a asset.Item return ticker.GetTicker(k.Name, p, a) } -// FetchTicker returns the ticker for a currency pair -func (k *Kraken) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - tickerNew, err := ticker.GetTicker(k.Name, p, assetType) - if err != nil { - return k.UpdateTicker(ctx, p, assetType) - } - return tickerNew, nil -} - -// FetchOrderbook returns orderbook base on the currency pair -func (k *Kraken) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - ob, err := orderbook.Get(k.Name, p, assetType) - if err != nil { - return k.UpdateOrderbook(ctx, p, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (k *Kraken) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if p.IsEmpty() { @@ -623,19 +605,6 @@ func (k *Kraken) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (a return info, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (k *Kraken) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) { - creds, err := k.GetCredentials(ctx) - if err != nil { - return account.Holdings{}, err - } - acc, err := account.GetHoldings(k.Name, creds, assetType) - if err != nil { - return k.UpdateAccountInfo(ctx, assetType) - } - return acc, nil -} - // GetAccountFundingHistory returns funding history, deposits and // withdrawals func (k *Kraken) GetAccountFundingHistory(_ context.Context) ([]exchange.FundingHistory, error) { diff --git a/exchanges/kucoin/kucoin_test.go b/exchanges/kucoin/kucoin_test.go index 86c3ac38b33..bfd56d493de 100644 --- a/exchanges/kucoin/kucoin_test.go +++ b/exchanges/kucoin/kucoin_test.go @@ -2001,28 +2001,6 @@ func TestUpdateTicker(t *testing.T) { } } -func TestFetchTicker(t *testing.T) { - t.Parallel() - var result *ticker.Price - var err error - for assetType, tp := range assertToTradablePairMap { - result, err = ku.FetchTicker(context.Background(), tp, assetType) - assert.NoError(t, err) - assert.NotNil(t, result) - } -} - -func TestFetchOrderbook(t *testing.T) { - t.Parallel() - var result *orderbook.Base - var err error - for assetType, tp := range assertToTradablePairMap { - result, err = ku.FetchOrderbook(context.Background(), tp, assetType) - assert.NoError(t, err) - assert.NotNil(t, result) - } -} - func TestGetHistoricCandles(t *testing.T) { startTime := time.Now().Add(-time.Hour * 48) endTime := time.Now().Add(-time.Hour * 3) diff --git a/exchanges/kucoin/kucoin_wrapper.go b/exchanges/kucoin/kucoin_wrapper.go index 12a2bc42216..eb5d8fed9c3 100644 --- a/exchanges/kucoin/kucoin_wrapper.go +++ b/exchanges/kucoin/kucoin_wrapper.go @@ -374,32 +374,6 @@ func (ku *Kucoin) UpdateTickers(ctx context.Context, assetType asset.Item) error return errs } -// FetchTicker returns the ticker for a currency pair -func (ku *Kucoin) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - p, err := ku.FormatExchangeCurrency(p, assetType) - if err != nil { - return nil, err - } - tickerNew, err := ticker.GetTicker(ku.Name, p, assetType) - if err != nil { - return ku.UpdateTicker(ctx, p, assetType) - } - return tickerNew, nil -} - -// FetchOrderbook returns orderbook base on the currency pair -func (ku *Kucoin) FetchOrderbook(ctx context.Context, pair currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - pair, err := ku.FormatExchangeCurrency(pair, assetType) - if err != nil { - return nil, err - } - ob, err := orderbook.Get(ku.Name, pair, assetType) - if err != nil { - return ku.UpdateOrderbook(ctx, pair, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (ku *Kucoin) UpdateOrderbook(ctx context.Context, pair currency.Pair, assetType asset.Item) (*orderbook.Base, error) { err := ku.CurrencyPairs.IsAssetEnabled(assetType) @@ -415,14 +389,7 @@ func (ku *Kucoin) UpdateOrderbook(ctx context.Context, pair currency.Pair, asset case asset.Futures: ordBook, err = ku.GetFuturesOrderbook(ctx, pair.String()) case asset.Spot, asset.Margin: - if ku.IsRESTAuthenticationSupported() && ku.AreCredentialsValid(ctx) { - ordBook, err = ku.GetOrderbook(ctx, pair.String()) - if err != nil { - return nil, err - } - } else { - ordBook, err = ku.GetPartOrderbook100(ctx, pair.String()) - } + ordBook, err = ku.GetPartOrderbook100(ctx, pair.String()) default: return nil, fmt.Errorf("%w %v", asset.ErrNotSupported, assetType) } @@ -500,19 +467,6 @@ func (ku *Kucoin) UpdateAccountInfo(ctx context.Context, assetType asset.Item) ( return holding, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (ku *Kucoin) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) { - creds, err := ku.GetCredentials(ctx) - if err != nil { - return account.Holdings{}, err - } - acc, err := account.GetHoldings(ku.Name, creds, assetType) - if err != nil { - return ku.UpdateAccountInfo(ctx, assetType) - } - return acc, nil -} - // GetAccountFundingHistory returns funding history, deposits and // withdrawals func (ku *Kucoin) GetAccountFundingHistory(ctx context.Context) ([]exchange.FundingHistory, error) { diff --git a/exchanges/lbank/lbank_wrapper.go b/exchanges/lbank/lbank_wrapper.go index 486f49bcae9..78555e278da 100644 --- a/exchanges/lbank/lbank_wrapper.go +++ b/exchanges/lbank/lbank_wrapper.go @@ -195,29 +195,6 @@ func (l *Lbank) UpdateTicker(ctx context.Context, p currency.Pair, a asset.Item) return ticker.GetTicker(l.Name, p, a) } -// FetchTicker returns the ticker for a currency pair -func (l *Lbank) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - fPair, err := l.FormatExchangeCurrency(p, assetType) - if err != nil { - return nil, err - } - - tickerNew, err := ticker.GetTicker(l.Name, fPair, assetType) - if err != nil { - return l.UpdateTicker(ctx, p, assetType) - } - return tickerNew, nil -} - -// FetchOrderbook returns orderbook base on the currency pair -func (l *Lbank) FetchOrderbook(ctx context.Context, c currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - ob, err := orderbook.Get(l.Name, c, assetType) - if err != nil { - return l.UpdateOrderbook(ctx, c, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (l *Lbank) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if p.IsEmpty() { @@ -324,19 +301,6 @@ func (l *Lbank) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (ac return info, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (l *Lbank) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) { - creds, err := l.GetCredentials(ctx) - if err != nil { - return account.Holdings{}, err - } - acc, err := account.GetHoldings(l.Name, creds, assetType) - if err != nil { - return l.UpdateAccountInfo(ctx, assetType) - } - return acc, nil -} - // GetAccountFundingHistory returns funding history, deposits and // withdrawals func (l *Lbank) GetAccountFundingHistory(_ context.Context) ([]exchange.FundingHistory, error) { diff --git a/exchanges/okx/okx_test.go b/exchanges/okx/okx_test.go index 14138bbea33..b3f28711708 100644 --- a/exchanges/okx/okx_test.go +++ b/exchanges/okx/okx_test.go @@ -1977,24 +1977,6 @@ func TestUpdateTickers(t *testing.T) { } } -func TestFetchTicker(t *testing.T) { - t.Parallel() - _, err := ok.FetchTicker(contextGenerate(), currency.NewPair(currency.BTC, currency.NewCode("USDT-SWAP")), asset.PerpetualSwap) - if err != nil { - t.Error("Okx FetchTicker() error", err) - } - if _, err = ok.FetchTicker(contextGenerate(), currency.NewPair(currency.BTC, currency.USDT), asset.Spot); err != nil { - t.Error("Okx FetchTicker() error", err) - } -} - -func TestFetchOrderbook(t *testing.T) { - t.Parallel() - if _, err := ok.FetchOrderbook(contextGenerate(), currency.NewPair(currency.BTC, currency.USDT), asset.Spot); err != nil { - t.Error("Okx FetchOrderbook() error", err) - } -} - func TestUpdateOrderbook(t *testing.T) { t.Parallel() if _, err := ok.UpdateOrderbook(contextGenerate(), currency.NewPair(currency.BTC, currency.NewCode("USDT-SWAP")), asset.Spot); err != nil { diff --git a/exchanges/okx/okx_wrapper.go b/exchanges/okx/okx_wrapper.go index 344fa4ba883..eeadf7d0cd8 100644 --- a/exchanges/okx/okx_wrapper.go +++ b/exchanges/okx/okx_wrapper.go @@ -424,28 +424,6 @@ func (ok *Okx) UpdateTickers(ctx context.Context, assetType asset.Item) error { return nil } -// FetchTicker returns the ticker for a currency pair -func (ok *Okx) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - formattedPair, err := ok.FormatExchangeCurrency(p, assetType) - if err != nil { - return nil, err - } - tickerNew, err := ticker.GetTicker(ok.Name, formattedPair, assetType) - if err != nil { - return ok.UpdateTicker(ctx, p, assetType) - } - return tickerNew, nil -} - -// FetchOrderbook returns orderbook base on the currency pair -func (ok *Okx) FetchOrderbook(ctx context.Context, pair currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - ob, err := orderbook.Get(ok.Name, pair, assetType) - if err != nil { - return ok.UpdateOrderbook(ctx, pair, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (ok *Okx) UpdateOrderbook(ctx context.Context, pair currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if pair.IsEmpty() { @@ -545,19 +523,6 @@ func (ok *Okx) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (acc return info, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (ok *Okx) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) { - creds, err := ok.GetCredentials(ctx) - if err != nil { - return account.Holdings{}, err - } - acc, err := account.GetHoldings(ok.Name, creds, assetType) - if err != nil { - return ok.UpdateAccountInfo(ctx, assetType) - } - return acc, nil -} - // GetAccountFundingHistory returns funding history, deposits and withdrawals func (ok *Okx) GetAccountFundingHistory(ctx context.Context) ([]exchange.FundingHistory, error) { depositHistories, err := ok.GetCurrencyDepositHistory(ctx, "", "", "", time.Time{}, time.Time{}, -1, 0) diff --git a/exchanges/orderbook/orderbook.go b/exchanges/orderbook/orderbook.go index 5773864ffcd..b021e38e637 100644 --- a/exchanges/orderbook/orderbook.go +++ b/exchanges/orderbook/orderbook.go @@ -37,7 +37,7 @@ func SubscribeToExchangeOrderbooks(exchange string) (dispatch.Pipe, error) { exch, ok := service.books[strings.ToLower(exchange)] if !ok { return dispatch.Pipe{}, fmt.Errorf("%w for %s exchange", - errCannotFindOrderbook, exchange) + ErrCannotFindOrderbook, exchange) } return service.Mux.Subscribe(exch.ID) } @@ -130,7 +130,7 @@ func (s *Service) GetDepth(exchange string, p currency.Pair, a asset.Item) (*Dep m1, ok := s.books[strings.ToLower(exchange)] if !ok { return nil, fmt.Errorf("%w for %s exchange", - errCannotFindOrderbook, exchange) + ErrCannotFindOrderbook, exchange) } book, ok := m1.m[key.PairAsset{ @@ -139,7 +139,7 @@ func (s *Service) GetDepth(exchange string, p currency.Pair, a asset.Item) (*Dep Asset: a, }] if !ok { - return nil, fmt.Errorf("%w associated with base currency %s", errCannotFindOrderbook, p.Quote) + return nil, fmt.Errorf("%w associated with base currency %s", ErrCannotFindOrderbook, p.Quote) } return book, nil } @@ -158,7 +158,7 @@ func (s *Service) Retrieve(exchange string, p currency.Pair, a asset.Item) (*Bas defer s.mu.Unlock() m1, ok := s.books[strings.ToLower(exchange)] if !ok { - return nil, fmt.Errorf("%w for %s exchange", errCannotFindOrderbook, exchange) + return nil, fmt.Errorf("%w for %s exchange", ErrCannotFindOrderbook, exchange) } book, ok := m1.m[key.PairAsset{ Base: p.Base.Item, @@ -166,7 +166,7 @@ func (s *Service) Retrieve(exchange string, p currency.Pair, a asset.Item) (*Bas Asset: a, }] if !ok { - return nil, fmt.Errorf("%w associated with base currency %s", errCannotFindOrderbook, p.Quote) + return nil, fmt.Errorf("%w associated with currency %s %s", ErrCannotFindOrderbook, p, a) } return book.Retrieve() } diff --git a/exchanges/orderbook/orderbook_test.go b/exchanges/orderbook/orderbook_test.go index 127393b600c..b8668d830f2 100644 --- a/exchanges/orderbook/orderbook_test.go +++ b/exchanges/orderbook/orderbook_test.go @@ -26,8 +26,8 @@ func TestMain(m *testing.M) { func TestSubscribeToExchangeOrderbooks(t *testing.T) { _, err := SubscribeToExchangeOrderbooks("") - if !errors.Is(err, errCannotFindOrderbook) { - t.Fatalf("expected: %v but received: %v", errCannotFindOrderbook, err) + if !errors.Is(err, ErrCannotFindOrderbook) { + t.Fatalf("expected: %v but received: %v", ErrCannotFindOrderbook, err) } p := currency.NewPair(currency.BTC, currency.USD) @@ -202,14 +202,14 @@ func TestGetOrderbook(t *testing.T) { } _, err = Get("nonexistent", c, asset.Spot) - if !errors.Is(err, errCannotFindOrderbook) { - t.Fatalf("received '%v', expected '%v'", err, errCannotFindOrderbook) + if !errors.Is(err, ErrCannotFindOrderbook) { + t.Fatalf("received '%v', expected '%v'", err, ErrCannotFindOrderbook) } c.Base = currency.NewCode("blah") _, err = Get("Exchange", c, asset.Spot) - if !errors.Is(err, errCannotFindOrderbook) { - t.Fatalf("received '%v', expected '%v', using invalid first currency", err, errCannotFindOrderbook) + if !errors.Is(err, ErrCannotFindOrderbook) { + t.Fatalf("received '%v', expected '%v', using invalid first currency", err, ErrCannotFindOrderbook) } newCurrency, err := currency.NewPairFromStrings("BTC", "AUD") @@ -217,8 +217,8 @@ func TestGetOrderbook(t *testing.T) { t.Fatal(err) } _, err = Get("Exchange", newCurrency, asset.Spot) - if !errors.Is(err, errCannotFindOrderbook) { - t.Fatalf("received '%v', expected '%v', using invalid second currency", err, errCannotFindOrderbook) + if !errors.Is(err, ErrCannotFindOrderbook) { + t.Fatalf("received '%v', expected '%v', using invalid second currency", err, ErrCannotFindOrderbook) } base.Pair = newCurrency @@ -261,14 +261,14 @@ func TestGetDepth(t *testing.T) { } _, err = GetDepth("nonexistent", c, asset.Spot) - if !errors.Is(err, errCannotFindOrderbook) { - t.Fatalf("expecting %s error but received %v", errCannotFindOrderbook, err) + if !errors.Is(err, ErrCannotFindOrderbook) { + t.Fatalf("expecting %s error but received %v", ErrCannotFindOrderbook, err) } c.Base = currency.NewCode("blah") _, err = GetDepth("Exchange", c, asset.Spot) - if !errors.Is(err, errCannotFindOrderbook) { - t.Fatalf("expecting %s error but received %v", errCannotFindOrderbook, err) + if !errors.Is(err, ErrCannotFindOrderbook) { + t.Fatalf("expecting %s error but received %v", ErrCannotFindOrderbook, err) } newCurrency, err := currency.NewPairFromStrings("BTC", "DOGE") @@ -276,8 +276,8 @@ func TestGetDepth(t *testing.T) { t.Fatal(err) } _, err = GetDepth("Exchange", newCurrency, asset.Futures) - if !errors.Is(err, errCannotFindOrderbook) { - t.Fatalf("expecting %s error but received %v", errCannotFindOrderbook, err) + if !errors.Is(err, ErrCannotFindOrderbook) { + t.Fatalf("expecting %s error but received %v", ErrCannotFindOrderbook, err) } base.Pair = newCurrency @@ -287,8 +287,8 @@ func TestGetDepth(t *testing.T) { } _, err = GetDepth("Exchange", newCurrency, asset.Empty) - if !errors.Is(err, errCannotFindOrderbook) { - t.Fatalf("expecting %s error but received %v", errCannotFindOrderbook, err) + if !errors.Is(err, ErrCannotFindOrderbook) { + t.Fatalf("expecting %s error but received %v", ErrCannotFindOrderbook, err) } } @@ -305,8 +305,8 @@ func TestBaseGetDepth(t *testing.T) { Asset: asset.Spot, } - if _, err = base.GetDepth(); !errors.Is(err, errCannotFindOrderbook) { - t.Errorf("expecting %s error but received %v", errCannotFindOrderbook, err) + if _, err = base.GetDepth(); !errors.Is(err, ErrCannotFindOrderbook) { + t.Errorf("expecting %s error but received %v", ErrCannotFindOrderbook, err) } if err = base.Process(); err != nil { diff --git a/exchanges/orderbook/orderbook_types.go b/exchanges/orderbook/orderbook_types.go index dc1929544a3..28d7df7cabd 100644 --- a/exchanges/orderbook/orderbook_types.go +++ b/exchanges/orderbook/orderbook_types.go @@ -12,19 +12,20 @@ import ( "github.com/thrasher-corp/gocryptotrader/exchanges/asset" ) -// const values for orderbook package const ( bidLoadBookFailure = "cannot load book for exchange %s pair %s asset %s for Bids: %w" askLoadBookFailure = "cannot load book for exchange %s pair %s asset %s for Asks: %w" bookLengthIssue = "Potential book issue for exchange %s pair %s asset %s length Bids %d length Asks %d" ) -// Vars for the orderbook package var ( + // ErrCannotFindOrderbook is a common error for when an orderbook cannot be + // found + ErrCannotFindOrderbook = errors.New("cannot find orderbook(s)") + errExchangeNameUnset = errors.New("orderbook exchange name not set") errPairNotSet = errors.New("orderbook currency pair not set") errAssetTypeNotSet = errors.New("orderbook asset type not set") - errCannotFindOrderbook = errors.New("cannot find orderbook(s)") errPriceNotSet = errors.New("price cannot be zero") errAmountInvalid = errors.New("amount cannot be less or equal to zero") errPriceOutOfOrder = errors.New("pricing out of order") diff --git a/exchanges/orderbook/simulator/simulator_test.go b/exchanges/orderbook/simulator/simulator_test.go index 6ebb2c34d71..90d8b3ae654 100644 --- a/exchanges/orderbook/simulator/simulator_test.go +++ b/exchanges/orderbook/simulator/simulator_test.go @@ -25,7 +25,7 @@ func TestSimulate(t *testing.T) { }, }, } - o, err := b.FetchOrderbook(context.Background(), + o, err := b.UpdateOrderbook(context.Background(), currency.NewPair(currency.BTC, currency.USD), asset.Spot) if err != nil { t.Fatal(err) diff --git a/exchanges/poloniex/poloniex_wrapper.go b/exchanges/poloniex/poloniex_wrapper.go index 183e96dd29c..4d499c5393c 100644 --- a/exchanges/poloniex/poloniex_wrapper.go +++ b/exchanges/poloniex/poloniex_wrapper.go @@ -273,24 +273,6 @@ func (p *Poloniex) UpdateTicker(ctx context.Context, currencyPair currency.Pair, return ticker.GetTicker(p.Name, currencyPair, a) } -// FetchTicker returns the ticker for a currency pair -func (p *Poloniex) FetchTicker(ctx context.Context, currencyPair currency.Pair, assetType asset.Item) (*ticker.Price, error) { - tickerNew, err := ticker.GetTicker(p.Name, currencyPair, assetType) - if err != nil { - return p.UpdateTicker(ctx, currencyPair, assetType) - } - return tickerNew, nil -} - -// FetchOrderbook returns orderbook base on the currency pair -func (p *Poloniex) FetchOrderbook(ctx context.Context, currencyPair currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - ob, err := orderbook.Get(p.Name, currencyPair, assetType) - if err != nil { - return p.UpdateOrderbook(ctx, currencyPair, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (p *Poloniex) UpdateOrderbook(ctx context.Context, pair currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if pair.IsEmpty() { @@ -392,19 +374,6 @@ func (p *Poloniex) UpdateAccountInfo(ctx context.Context, assetType asset.Item) return response, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (p *Poloniex) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) { - creds, err := p.GetCredentials(ctx) - if err != nil { - return account.Holdings{}, err - } - acc, err := account.GetHoldings(p.Name, creds, assetType) - if err != nil { - return p.UpdateAccountInfo(ctx, assetType) - } - return acc, nil -} - // GetAccountFundingHistory returns funding history, deposits and // withdrawals func (p *Poloniex) GetAccountFundingHistory(ctx context.Context) ([]exchange.FundingHistory, error) { diff --git a/exchanges/sharedtestvalues/customex.go b/exchanges/sharedtestvalues/customex.go index e7735dd793d..34b4b8e1244 100644 --- a/exchanges/sharedtestvalues/customex.go +++ b/exchanges/sharedtestvalues/customex.go @@ -56,11 +56,6 @@ func (c *CustomEx) ValidateAPICredentials(_ context.Context, _ asset.Item) error return nil } -// FetchTicker is a mock method for CustomEx -func (c *CustomEx) FetchTicker(_ context.Context, _ currency.Pair, _ asset.Item) (*ticker.Price, error) { - return nil, nil -} - // UpdateTickers is a mock method for CustomEx func (c *CustomEx) UpdateTickers(_ context.Context, _ asset.Item) error { return nil @@ -71,11 +66,6 @@ func (c *CustomEx) UpdateTicker(_ context.Context, _ currency.Pair, _ asset.Item return nil, nil } -// FetchOrderbook is a mock method for CustomEx -func (c *CustomEx) FetchOrderbook(_ context.Context, _ currency.Pair, _ asset.Item) (*orderbook.Base, error) { - return nil, nil -} - // UpdateOrderbook is a mock method for CustomEx func (c *CustomEx) UpdateOrderbook(_ context.Context, _ currency.Pair, _ asset.Item) (*orderbook.Base, error) { return nil, nil @@ -101,11 +91,6 @@ func (c *CustomEx) GetAvailablePairs(_ asset.Item) (currency.Pairs, error) { return nil, nil } -// FetchAccountInfo is a mock method for CustomEx -func (c *CustomEx) FetchAccountInfo(_ context.Context, _ asset.Item) (account.Holdings, error) { - return account.Holdings{}, nil -} - // UpdateAccountInfo is a mock method for CustomEx func (c *CustomEx) UpdateAccountInfo(_ context.Context, _ asset.Item) (account.Holdings, error) { return account.Holdings{}, nil diff --git a/exchanges/yobit/yobit_wrapper.go b/exchanges/yobit/yobit_wrapper.go index 251c64b6f48..eaea6b4eaaa 100644 --- a/exchanges/yobit/yobit_wrapper.go +++ b/exchanges/yobit/yobit_wrapper.go @@ -191,24 +191,6 @@ func (y *Yobit) UpdateTicker(ctx context.Context, p currency.Pair, a asset.Item) return ticker.GetTicker(y.Name, p, a) } -// FetchTicker returns the ticker for a currency pair -func (y *Yobit) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) { - tick, err := ticker.GetTicker(y.Name, p, assetType) - if err != nil { - return y.UpdateTicker(ctx, p, assetType) - } - return tick, nil -} - -// FetchOrderbook returns the orderbook for a currency pair -func (y *Yobit) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { - ob, err := orderbook.Get(y.Name, p, assetType) - if err != nil { - return y.UpdateOrderbook(ctx, p, assetType) - } - return ob, nil -} - // UpdateOrderbook updates and returns the orderbook for a currency pair func (y *Yobit) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) { if p.IsEmpty() { @@ -296,19 +278,6 @@ func (y *Yobit) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (ac return response, nil } -// FetchAccountInfo retrieves balances for all enabled currencies -func (y *Yobit) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) { - creds, err := y.GetCredentials(ctx) - if err != nil { - return account.Holdings{}, err - } - acc, err := account.GetHoldings(y.Name, creds, assetType) - if err != nil { - return y.UpdateAccountInfo(ctx, assetType) - } - return acc, nil -} - // GetAccountFundingHistory returns funding history, deposits and // withdrawals func (y *Yobit) GetAccountFundingHistory(_ context.Context) ([]exchange.FundingHistory, error) { diff --git a/gctscript/wrappers/gct/exchange/exchange.go b/gctscript/wrappers/gct/exchange/exchange.go index 32f612abec3..d06359aca01 100644 --- a/gctscript/wrappers/gct/exchange/exchange.go +++ b/gctscript/wrappers/gct/exchange/exchange.go @@ -45,18 +45,21 @@ func (e Exchange) IsEnabled(exch string) bool { } // Orderbook returns current orderbook requested exchange, pair and asset -func (e Exchange) Orderbook(ctx context.Context, exch string, pair currency.Pair, item asset.Item) (*orderbook.Base, error) { - return engine.Bot.GetSpecificOrderbook(ctx, pair, exch, item) +func (e Exchange) Orderbook(ctx context.Context, exch string, pair currency.Pair, a asset.Item) (*orderbook.Base, error) { + ex, err := e.GetExchange(exch) + if err != nil { + return nil, err + } + return ex.UpdateOrderbook(ctx, pair, a) } // Ticker returns ticker for provided currency pair & asset type -func (e Exchange) Ticker(ctx context.Context, exch string, pair currency.Pair, item asset.Item) (*ticker.Price, error) { +func (e Exchange) Ticker(ctx context.Context, exch string, pair currency.Pair, a asset.Item) (*ticker.Price, error) { ex, err := e.GetExchange(exch) if err != nil { return nil, err } - - return ex.FetchTicker(ctx, pair, item) + return ex.UpdateTicker(ctx, pair, a) } // Pairs returns either all or enabled currency pairs