mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-01 23:16:51 +00:00
engine/exchanges: Add GetCurrencyTradeURL wrapper func/gRPC endpoint (#1521)
* beginning the concept * expands testing and implementations * test standardisation, expansion * more * finish remainder, add rpc func * lint * grammar? I barely know her! * wow i forgot something wow wow wow wow * rm SendPayload * enFixio! * improve binance long-dated support * update test design, update wrapper funcs * adds kraken futures, adds bybit support * fixes SIMPLE bugs * s is for sucks * fixed option url x2 * fixed silly test
This commit is contained in:
@@ -6016,3 +6016,36 @@ func (s *RPCServer) GetOpenInterest(ctx context.Context, r *gctrpc.GetOpenIntere
|
||||
Data: resp,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetCurrencyTradeURL returns the URL for the trading pair
|
||||
func (s *RPCServer) GetCurrencyTradeURL(ctx context.Context, r *gctrpc.GetCurrencyTradeURLRequest) (*gctrpc.GetCurrencyTradeURLResponse, error) {
|
||||
if r == nil {
|
||||
return nil, fmt.Errorf("%w GetCurrencyTradeURLRequest", common.ErrNilPointer)
|
||||
}
|
||||
exch, err := s.GetExchangeByName(r.Exchange)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exch.IsEnabled() {
|
||||
return nil, fmt.Errorf("%s %w", r.Exchange, errExchangeNotEnabled)
|
||||
}
|
||||
ai, err := asset.New(r.Asset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if r.Pair == nil ||
|
||||
(r.Pair.Base == "" && r.Pair.Quote == "") {
|
||||
return nil, currency.ErrCurrencyPairEmpty
|
||||
}
|
||||
cp, err := exch.MatchSymbolWithAvailablePairs(r.Pair.Base+r.Pair.Quote, ai, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
url, err := exch.GetCurrencyTradeURL(ctx, ai, cp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &gctrpc.GetCurrencyTradeURLResponse{
|
||||
Url: url,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -268,6 +268,10 @@ func (f fExchange) GetHistoricCandlesExtended(_ context.Context, p currency.Pair
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (f fExchange) GetCurrencyTradeURL(_ context.Context, _ asset.Item, _ currency.Pair) (string, error) {
|
||||
return "https://google.com", nil
|
||||
}
|
||||
|
||||
func (f fExchange) GetMarginRatesHistory(context.Context, *margin.RateHistoryRequest) (*margin.RateHistoryResponse, error) {
|
||||
leet := decimal.NewFromInt(1337)
|
||||
rates := []margin.Rate{
|
||||
@@ -4290,3 +4294,55 @@ func TestRPCProxyAuthClient(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCurrencyTradeURL(t *testing.T) {
|
||||
t.Parallel()
|
||||
em := NewExchangeManager()
|
||||
exch, err := em.NewExchangeByName("binance")
|
||||
require.NoError(t, err)
|
||||
|
||||
exch.SetDefaults()
|
||||
b := exch.GetBase()
|
||||
b.Name = fakeExchangeName
|
||||
b.Enabled = true
|
||||
b.CurrencyPairs.Pairs = make(map[asset.Item]*currency.PairStore)
|
||||
err = b.CurrencyPairs.Store(asset.Spot, ¤cy.PairStore{
|
||||
AssetEnabled: convert.BoolPtr(true),
|
||||
Enabled: []currency.Pair{currency.NewPair(currency.BTC, currency.USDT)},
|
||||
Available: []currency.Pair{currency.NewPair(currency.BTC, currency.USDT)},
|
||||
RequestFormat: ¤cy.PairFormat{Uppercase: true},
|
||||
ConfigFormat: ¤cy.PairFormat{Uppercase: true},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
fakeExchange := fExchange{
|
||||
IBotExchange: exch,
|
||||
}
|
||||
err = em.Add(fakeExchange)
|
||||
require.NoError(t, err)
|
||||
|
||||
s := RPCServer{Engine: &Engine{ExchangeManager: em}}
|
||||
_, err = s.GetCurrencyTradeURL(context.Background(), nil)
|
||||
assert.ErrorIs(t, err, common.ErrNilPointer)
|
||||
|
||||
req := &gctrpc.GetCurrencyTradeURLRequest{}
|
||||
_, err = s.GetCurrencyTradeURL(context.Background(), req)
|
||||
assert.ErrorIs(t, err, ErrExchangeNameIsEmpty)
|
||||
|
||||
req.Exchange = fakeExchangeName
|
||||
_, err = s.GetCurrencyTradeURL(context.Background(), req)
|
||||
assert.ErrorIs(t, err, asset.ErrNotSupported)
|
||||
|
||||
req.Asset = "spot"
|
||||
_, err = s.GetCurrencyTradeURL(context.Background(), req)
|
||||
assert.ErrorIs(t, err, currency.ErrCurrencyPairEmpty)
|
||||
|
||||
req.Pair = &gctrpc.CurrencyPair{
|
||||
Delimiter: "-",
|
||||
Base: "btc",
|
||||
Quote: "usdt",
|
||||
}
|
||||
resp, err := s.GetCurrencyTradeURL(context.Background(), req)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, resp.Url)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user