Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf committed Jan 27, 2025
1 parent ad2d636 commit 76dd919
Show file tree
Hide file tree
Showing 28 changed files with 409 additions and 203 deletions.
2 changes: 1 addition & 1 deletion Bitfinex.Net/Bitfinex.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
<LangVersion>12.0</LangVersion>
</PropertyGroup>
<PropertyGroup>
<PackageId>Bitfinex.Net</PackageId>
Expand Down
33 changes: 27 additions & 6 deletions Bitfinex.Net/Bitfinex.Net.xml

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

20 changes: 12 additions & 8 deletions Bitfinex.Net/BitfinexEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ public class BitfinexEnvironment : TradeEnvironment
/// </summary>
public string SocketAddress { get; }

internal BitfinexEnvironment(string name, string restAddress, string socketAddress) :
/// <summary>
/// Socket client address
/// </summary>
public string SocketPublicAddress { get; }

internal BitfinexEnvironment(string name, string restAddress, string socketAddress, string socketPublicAddress) :
base(name)
{
RestAddress = restAddress;
SocketAddress = socketAddress;
SocketPublicAddress = socketPublicAddress;
}

/// <summary>
Expand Down Expand Up @@ -51,20 +57,18 @@ public BitfinexEnvironment() : base(TradeEnvironmentNames.Live)
public static BitfinexEnvironment Live { get; }
= new BitfinexEnvironment(TradeEnvironmentNames.Live,
BitfinexApiAddresses.Default.RestClientAddress,
BitfinexApiAddresses.Default.SocketClientAddress);
BitfinexApiAddresses.Default.SocketClientAddress,
BitfinexApiAddresses.Default.SocketClientPublicAddress);

/// <summary>
/// Create a custom environment
/// </summary>
/// <param name="name"></param>
/// <param name="restAddress"></param>
/// <param name="socketAddress"></param>
/// <returns></returns>
public static BitfinexEnvironment CreateCustom(
string name,
string restAddress,
string socketAddress)
=> new BitfinexEnvironment(name, restAddress, socketAddress);
string socketAddress,
string socketPublicAddress)
=> new BitfinexEnvironment(name, restAddress, socketAddress, socketPublicAddress);

}
}
51 changes: 51 additions & 0 deletions Bitfinex.Net/BitfinexExchange.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using CryptoExchange.Net.Objects;

Check failure on line 1 in Bitfinex.Net/BitfinexExchange.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Objects' does not exist in the namespace 'CryptoExchange.Net' (are you missing an assembly reference?)
using CryptoExchange.Net.RateLimiting;

Check failure on line 2 in Bitfinex.Net/BitfinexExchange.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'RateLimiting' does not exist in the namespace 'CryptoExchange.Net' (are you missing an assembly reference?)
using CryptoExchange.Net.RateLimiting.Filters;

Check failure on line 3 in Bitfinex.Net/BitfinexExchange.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'RateLimiting' does not exist in the namespace 'CryptoExchange.Net' (are you missing an assembly reference?)
using CryptoExchange.Net.RateLimiting.Guards;

Check failure on line 4 in Bitfinex.Net/BitfinexExchange.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'RateLimiting' does not exist in the namespace 'CryptoExchange.Net' (are you missing an assembly reference?)
using CryptoExchange.Net.RateLimiting.Interfaces;

Check failure on line 5 in Bitfinex.Net/BitfinexExchange.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'RateLimiting' does not exist in the namespace 'CryptoExchange.Net' (are you missing an assembly reference?)
using CryptoExchange.Net.SharedApis;

Check failure on line 6 in Bitfinex.Net/BitfinexExchange.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'SharedApis' does not exist in the namespace 'CryptoExchange.Net' (are you missing an assembly reference?)
using System;

Expand Down Expand Up @@ -59,5 +63,52 @@ public static string FormatSymbol(string baseAsset, string quoteAsset, TradingMo

return $"t{baseAsset.ToUpperInvariant()}{quoteAsset.ToUpperInvariant()}";
}

/// <summary>
/// Rate limiter configuration for the Bitfinex API
/// </summary>
public static BitfinexRateLimiters RateLimiter { get; } = new BitfinexRateLimiters();

}

/// <summary>
/// Rate limiter configuration for the GateIo API
/// </summary>
public class BitfinexRateLimiters
{
/// <summary>
/// Event for when a rate limit is triggered
/// </summary>
public event Action<RateLimitEvent> RateLimitTriggered;

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
internal BitfinexRateLimiters()
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
{
Initialize();
}

private void Initialize()
{
Overal = new RateLimitGate("Overal");
RestConf = new RateLimitGate("Rest Config")
.AddGuard(new RateLimitGuard(RateLimitGuard.PerHost, [], 90, TimeSpan.FromSeconds(60), RateLimitWindowType.Sliding)); // 90 requests per minute shared by all /conf endpoints
RestStats = new RateLimitGate("Rest Stats")
.AddGuard(new RateLimitGuard(RateLimitGuard.PerHost, [], 15, TimeSpan.FromSeconds(60), RateLimitWindowType.Sliding)); // 15 requests per minute shared by all /stats endpoints
Websocket = new RateLimitGate("Websocket")
.AddGuard(new RateLimitGuard(RateLimitGuard.PerHost, [new HostFilter("wss://api.bitfinex.com"), new LimitItemTypeFilter(RateLimitItemType.Connection)], 5, TimeSpan.FromSeconds(15), RateLimitWindowType.Sliding)) // Limit of 5 connection requests per 15 seconds
.AddGuard(new RateLimitGuard(RateLimitGuard.PerHost, [new HostFilter("wss://api-pub.bitfinex.com"), new LimitItemTypeFilter(RateLimitItemType.Connection)], 20, TimeSpan.FromSeconds(60), RateLimitWindowType.Sliding)); // Limit of 20 connection requests per 60 seconds

Overal.RateLimitTriggered += (x) => RateLimitTriggered?.Invoke(x);
RestConf.RateLimitTriggered += (x) => RateLimitTriggered?.Invoke(x);
RestStats.RateLimitTriggered += (x) => RateLimitTriggered?.Invoke(x);
Websocket.RateLimitTriggered += (x) => RateLimitTriggered?.Invoke(x);
}


internal IRateLimitGate Overal { get; private set; }
internal IRateLimitGate RestConf { get; private set; }
internal IRateLimitGate RestStats { get; private set; }
internal IRateLimitGate Websocket { get; private set; }
}
}
1 change: 0 additions & 1 deletion Bitfinex.Net/Clients/BitfinexRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.Extensions.Logging;
using System.Net.Http;
using Bitfinex.Net.Objects.Options;
using Microsoft.Extensions.DependencyInjection;
using CryptoExchange.Net.Clients;
using Microsoft.Extensions.Options;
using CryptoExchange.Net.Objects.Options;
Expand Down
24 changes: 11 additions & 13 deletions Bitfinex.Net/Clients/GeneralApi/BitfinexRestClientGeneralApi.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Bitfinex.Net.Interfaces.Clients.GeneralApi;
using Bitfinex.Net.Objects.Internal;
using Bitfinex.Net.Objects.Options;
using CryptoExchange.Net;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Clients;
using CryptoExchange.Net.Converters.MessageParsing;
Expand Down Expand Up @@ -48,25 +47,24 @@ internal BitfinexRestClientGeneralApi(ILogger logger, HttpClient? httpClient, Bi
protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials)
=> new BitfinexAuthenticationProvider(credentials, ClientOptions.NonceProvider ?? new BitfinexNonceProvider());

internal Task<WebCallResult<T>> SendRequestAsync<T>(
Uri uri,
HttpMethod method,
CancellationToken cancellationToken,
Dictionary<string, object>? parameters = null,
bool signed = false) where T : class
=> base.SendRequestAsync<T>(uri, method, cancellationToken, parameters, signed, requestWeight: 0);
internal Task<WebCallResult<T>> SendAsync<T>(
RequestDefinition definition,
ParameterCollection? parameters,
CancellationToken cancellationToken) where T : class
=> SendToAddressAsync<T>(BaseAddress, definition, parameters, cancellationToken);

internal Task<WebCallResult<T>> SendToAddressAsync<T>(
string uri,
RequestDefinition definition,
ParameterCollection? parameters,
CancellationToken cancellationToken) where T : class
=> base.SendAsync<T>(uri, definition, parameters, cancellationToken);

/// <inheritdoc />
protected override IMessageSerializer CreateSerializer() => new SystemTextJsonMessageSerializer();
/// <inheritdoc />
protected override IStreamMessageAccessor CreateAccessor() => new SystemTextJsonStreamMessageAccessor();

internal Uri GetUrl(string endpoint, string version)
{
return new Uri(BaseAddress.AppendPath($"v{version}", endpoint));
}

/// <inheritdoc />
public override string FormatSymbol(string baseAsset, string quoteAsset, TradingMode tradingMode, DateTime? deliverTime = null) => $"t{baseAsset.ToUpperInvariant()}{quoteAsset.ToUpperInvariant()}";

Expand Down
Loading

0 comments on commit 76dd919

Please sign in to comment.