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:
Scott
2024-05-03 17:01:17 +10:00
committed by GitHub
parent 0676c78bec
commit f1ff951199
83 changed files with 3922 additions and 2265 deletions

View File

@@ -35,6 +35,7 @@ type Bybit struct {
const (
bybitAPIURL = "https://api.bybit.com"
bybitAPIVersion = "/v5/"
tradeBaseURL = "https://www.bybit.com/"
defaultRecvWindow = "5000" // 5000 milli second

View File

@@ -10,6 +10,7 @@ import (
"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/thrasher-corp/gocryptotrader/common/key"
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
@@ -21,6 +22,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
)
@@ -3530,3 +3532,16 @@ func TestIsPerpetualFutureCurrency(t *testing.T) {
assert.NoError(t, err)
assert.True(t, is, fmt.Sprintf("%s %s should be a perp", asset.USDCMarginedFutures, usdcMarginedTradablePair))
}
func TestGetCurrencyTradeURL(t *testing.T) {
t.Parallel()
testexch.UpdatePairsOnce(t, b)
for _, a := range b.GetAssetTypes(false) {
pairs, err := b.CurrencyPairs.GetPairs(a, false)
require.NoError(t, err, "cannot get pairs for %s", a)
require.NotEmpty(t, pairs, "no pairs for %s", a)
resp, err := b.GetCurrencyTradeURL(context.Background(), a, pairs[0])
require.NoError(t, err)
assert.NotEmpty(t, resp)
}
}

View File

@@ -2024,3 +2024,58 @@ func (by *Bybit) GetOpenInterest(ctx context.Context, k ...key.PairAsset) ([]fut
}
return resp, nil
}
// GetCurrencyTradeURL returns the URL to the exchange's trade page for the given asset and currency pair
func (by *Bybit) GetCurrencyTradeURL(ctx context.Context, a asset.Item, cp currency.Pair) (string, error) {
_, err := by.CurrencyPairs.IsPairEnabled(cp, a)
if err != nil {
return "", err
}
switch a {
case asset.Spot:
cp.Delimiter = currency.ForwardSlashDelimiter
return tradeBaseURL + "en/trade/spot/" + cp.Upper().String(), nil
case asset.CoinMarginedFutures:
if cp.Quote.Equal(currency.USD) {
cp.Delimiter = ""
return tradeBaseURL + "trade/inverse/" + cp.Upper().String(), nil
}
var symbol string
symbol, err = by.FormatSymbol(cp, a)
if err != nil {
return "", err
}
// convert long-dated to static contracts
var io *InstrumentsInfo
io, err = by.GetInstrumentInfo(ctx, getCategoryName(a), symbol, "", "", "", 1000)
if err != nil {
return "", err
}
if len(io.List) != 1 {
return "", fmt.Errorf("%w %v", currency.ErrCurrencyNotFound, cp)
}
var length futures.ContractType
length, err = getContractLength(io.List[0].DeliveryTime.Time().Sub(io.List[0].LaunchTime.Time()))
if err != nil {
return "", err
}
// bybit inverse long-dated contracts are currently only quarterly or bi-quarterly
if length == futures.Quarterly {
cp = currency.NewPair(currency.NewCode(cp.Base.String()+currency.USD.String()), currency.NewCode("Q"))
} else {
cp = currency.NewPair(currency.NewCode(cp.Base.String()+currency.USD.String()), currency.NewCode("BIQ"))
}
cp.Delimiter = currency.UnderscoreDelimiter
return tradeBaseURL + "trade/inverse/futures/" + cp.Upper().String(), nil
case asset.USDTMarginedFutures:
cp.Delimiter = ""
return tradeBaseURL + "trade/usdt/" + cp.Upper().String(), nil
case asset.USDCMarginedFutures:
cp.Delimiter = currency.DashDelimiter
return tradeBaseURL + "trade/futures/usdc/" + cp.Upper().String(), nil
case asset.Options:
return tradeBaseURL + "trade/option/usdc/" + cp.Base.Upper().String(), nil
default:
return "", fmt.Errorf("%w %v", asset.ErrNotSupported, a)
}
}