Skip to content

Commit

Permalink
Added symbol checks
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf committed Sep 17, 2023
1 parent 3c541bb commit 3bff60f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
12 changes: 12 additions & 0 deletions Bitfinex.Net/Bitfinex.Net.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions Bitfinex.Net/BitfinexHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,31 @@ public static void ValidateBitfinexSymbol(this string symbolString)
throw new ArgumentException($"{symbolString} is not a valid Bitfinex symbol. Should be [t][QuoteAsset][BaseAsset] for trading pairs " +
"or [f][Asset] for margin symbols, e.g. tBTCUSD or fUSD");
}

/// <summary>
/// Validate the string is a valid Bitfinex symbol.
/// </summary>
/// <param name="symbolString">string to validate</param>
public static void ValidateBitfinexFundingSymbol(this string symbolString)
{
if (string.IsNullOrEmpty(symbolString))
throw new ArgumentException("Symbol is not provided");

if (!Regex.IsMatch(symbolString, "^([f]([A-Z0-9]{3,}))$"))
throw new ArgumentException($"{symbolString} is not a valid Bitfinex funding symbol. Should be [f][Asset] for funding symbols, e.g. fUSD");
}

/// <summary>
/// Validate the string is a valid Bitfinex symbol.
/// </summary>
/// <param name="symbolString">string to validate</param>
public static void ValidateBitfinexTradingSymbol(this string symbolString)
{
if (string.IsNullOrEmpty(symbolString))
throw new ArgumentException("Symbol is not provided");

if (!Regex.IsMatch(symbolString, "^([t]([A-Z0-9|:]{6,}))$"))
throw new ArgumentException($"{symbolString} is not a valid Bitfinex symbol. Should be [t][QuoteAsset][BaseAsset] for trading pairs, e.g. tBTCUSD");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ public async Task<WebCallResult<BitfinexDerivativesFees>> GetDerivativesFeesAsyn
#region Get Ticker
/// <inheritdoc />
public async Task<WebCallResult<BitfinexTicker>> GetTickerAsync(string symbol, CancellationToken ct = default)
{
{
symbol.ValidateBitfinexTradingSymbol();

var ticker = await GetTickersAsync(new[] { symbol }, ct).ConfigureAwait(false);
if(!ticker)
return ticker.As<BitfinexTicker>(null);
Expand All @@ -244,6 +246,8 @@ public async Task<WebCallResult<BitfinexTicker>> GetTickerAsync(string symbol, C
/// <inheritdoc />
public async Task<WebCallResult<BitfinexFundingTicker>> GetFundingTickerAsync(string symbol, CancellationToken ct = default)
{
symbol.ValidateBitfinexFundingSymbol();

var ticker = await GetFundingTickersAsync(new[] { symbol }, ct).ConfigureAwait(false);
if (!ticker)
return ticker.As<BitfinexFundingTicker>(null);
Expand Down Expand Up @@ -317,7 +321,7 @@ public async Task<WebCallResult<IEnumerable<BitfinexTradeSimple>>> GetTradeHisto
/// <inheritdoc />
public async Task<WebCallResult<BitfinexOrderBook>> GetOrderBookAsync(string symbol, Precision precision, int? limit = null, CancellationToken ct = default)
{
symbol.ValidateBitfinexSymbol();
symbol.ValidateBitfinexTradingSymbol();
limit?.ValidateIntValues("limit", 1, 25, 100);
if (precision == Precision.R0)
throw new ArgumentException("Precision can not be R0. Use PrecisionLevel0 to get aggregated trades for each price point or GetRawOrderBook to get the raw order book instead");
Expand Down Expand Up @@ -347,7 +351,7 @@ public async Task<WebCallResult<BitfinexOrderBook>> GetOrderBookAsync(string sym
/// <inheritdoc />
public async Task<WebCallResult<BitfinexFundingOrderBook>> GetFundingOrderBookAsync(string symbol, Precision precision, int? limit = null, CancellationToken ct = default)
{
symbol.ValidateBitfinexSymbol();
symbol.ValidateBitfinexFundingSymbol();
limit?.ValidateIntValues("limit", 1, 25, 100);
if (precision == Precision.R0)
throw new ArgumentException("Precision can not be R0. Use PrecisionLevel0 to get aggregated trades for each price point or GetRawOrderBook to get the raw order book instead");
Expand Down Expand Up @@ -377,7 +381,7 @@ public async Task<WebCallResult<BitfinexFundingOrderBook>> GetFundingOrderBookAs
/// <inheritdoc />
public async Task<WebCallResult<BitfinexRawOrderBook>> GetRawOrderBookAsync(string symbol, int? limit = null, CancellationToken ct = default)
{
symbol.ValidateBitfinexSymbol();
symbol.ValidateBitfinexTradingSymbol();
limit?.ValidateIntValues("limit", 25, 100);

var parameters = new Dictionary<string, object>();
Expand Down Expand Up @@ -405,7 +409,7 @@ public async Task<WebCallResult<BitfinexRawOrderBook>> GetRawOrderBookAsync(stri
/// <inheritdoc />
public async Task<WebCallResult<BitfinexRawFundingOrderBook>> GetRawFundingOrderBookAsync(string symbol, int? limit = null, CancellationToken ct = default)
{
symbol.ValidateBitfinexSymbol();
symbol.ValidateBitfinexFundingSymbol();
limit?.ValidateIntValues("limit", 25, 100);

var parameters = new Dictionary<string, object>();
Expand Down
12 changes: 6 additions & 6 deletions Bitfinex.Net/Clients/SpotApi/BitfinexSocketClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected override AuthenticationProvider CreateAuthenticationProvider(ApiCreden
/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToTickerUpdatesAsync(string symbol, Action<DataEvent<BitfinexTicker>> handler, CancellationToken ct = default)
{
symbol.ValidateBitfinexSymbol();
symbol.ValidateBitfinexTradingSymbol();
var internalHandler = new Action<DataEvent<JToken>>(data =>
{
HandleData("Ticker", (JArray)data.Data[1]!, symbol, data, handler);
Expand All @@ -71,7 +71,7 @@ public async Task<CallResult<UpdateSubscription>> SubscribeToTickerUpdatesAsync(
/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToFundingTickerUpdatesAsync(string symbol, Action<DataEvent<BitfinexStreamFundingTicker>> handler, CancellationToken ct = default)
{
symbol.ValidateBitfinexSymbol();
symbol.ValidateBitfinexFundingSymbol();
var internalHandler = new Action<DataEvent<JToken>>(data =>
{
HandleData("Ticker", (JArray)data.Data[1]!, symbol, data, handler);
Expand All @@ -82,7 +82,7 @@ public async Task<CallResult<UpdateSubscription>> SubscribeToFundingTickerUpdate
/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToOrderBookUpdatesAsync(string symbol, Precision precision, Frequency frequency, int length, Action<DataEvent<IEnumerable<BitfinexOrderBookEntry>>> handler, Action<DataEvent<int>>? checksumHandler = null, CancellationToken ct = default)
{
symbol.ValidateBitfinexSymbol();
symbol.ValidateBitfinexTradingSymbol();
length.ValidateIntValues(nameof(length), 1, 25, 100, 250);
if (precision == Precision.R0)
throw new ArgumentException("Invalid precision R0, use SubscribeToRawBookUpdatesAsync instead");
Expand Down Expand Up @@ -123,7 +123,7 @@ public async Task<CallResult<UpdateSubscription>> SubscribeToOrderBookUpdatesAsy
/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToFundingOrderBookUpdatesAsync(string symbol, Precision precision, Frequency frequency, int length, Action<DataEvent<IEnumerable<BitfinexOrderBookFundingEntry>>> handler, Action<DataEvent<int>>? checksumHandler = null, CancellationToken ct = default)
{
symbol.ValidateBitfinexSymbol();
symbol.ValidateBitfinexFundingSymbol();
length.ValidateIntValues(nameof(length), 1, 25, 100, 250);
if (precision == Precision.R0)
throw new ArgumentException("Invalid precision R0, use SubscribeToFundingRawOrderBookUpdatesAsync instead");
Expand Down Expand Up @@ -164,7 +164,7 @@ public async Task<CallResult<UpdateSubscription>> SubscribeToFundingOrderBookUpd
/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToRawOrderBookUpdatesAsync(string symbol, int limit, Action<DataEvent<IEnumerable<BitfinexRawOrderBookEntry>>> handler, Action<DataEvent<int>>? checksumHandler = null, CancellationToken ct = default)
{
symbol.ValidateBitfinexSymbol();
symbol.ValidateBitfinexTradingSymbol();
var internalHandler = new Action<DataEvent<JToken>>(data =>
{
if (data.Data[1]?.ToString() == "cs")
Expand All @@ -187,7 +187,7 @@ public async Task<CallResult<UpdateSubscription>> SubscribeToRawOrderBookUpdates
/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToRawFundingOrderBookUpdatesAsync(string symbol, int limit, Action<DataEvent<IEnumerable<BitfinexRawOrderBookFundingEntry>>> handler, Action<DataEvent<int>>? checksumHandler = null, CancellationToken ct = default)
{
symbol.ValidateBitfinexSymbol();
symbol.ValidateBitfinexFundingSymbol();
var internalHandler = new Action<DataEvent<JToken>>(data =>
{
if (data.Data[1]?.ToString() == "cs")
Expand Down
2 changes: 1 addition & 1 deletion Bitfinex.Net/SymbolOrderBooks/BitfinexSymbolOrderBook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public BitfinexSymbolOrderBook(string symbol,
ILogger<BitfinexSymbolOrderBook>? logger,
IBitfinexSocketClient? socketClient) : base(logger, "Bitfinex", symbol)
{
symbol.ValidateBitfinexSymbol();
symbol.ValidateBitfinexTradingSymbol();

var options = BitfinexOrderBookOptions.Default.Copy();
if (optionsDelegate != null)
Expand Down

0 comments on commit 3bff60f

Please sign in to comment.