Ethereum: Creating a Binary Order Book in C
.NET Core
As part of our MarketMaker team, we require a robust solution to fetch crypto market data from Binance without API keys. In this article, we will look at how to achieve this using C
.NET Core.
Requirements
Before we get into the implementation, let’s outline the requirements:
- Public market data
- Order book market depth at specific price levels
Step 1: Set up a new .NET Core project
Create a new .NET Core project using the dotnet CLI:
dotnet new console -o BinanceMarketData
Step 2: Install the required libraries
Install the following NuGet packages:
- Microsoft.Extensions.DependencyInjection
for dependency injection
- Newtonsoft.Json
for JSON serialization and deserialization
Add the following to your “Startup.cs” file:
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace BinanceMarketData
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddJsonSerialization();
}
public void Configure(IApplicationBuilder application, IWebHostEnvironment environment)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
Step 3: Create a market data retrieval service
Create a new file MarketDataService.cs`:
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Binance.NetAPI.Client;
namespace BinanceMarketData.Services
{
public class MarketDataService
{
private readonly HttpClient _httpClient;
public MarketDataService()
{
_httpClient = new HttpClient();
}
public asynchronous task GetPublicMarketDataAsync()
{
// Set the API endpoint and parameters
var apiEndpoint = "
var parameters = new Dictionary
{
{ "symbol", "ETH/BTC" }, // Replace with the desired symbol
{ "limit", "10" } // Number of market data to return
};
// Make an API request and parse the JSON response
var marketData = wait _httpClient.GetAsync(apiEndpoint, parameters).Result;
marketData.EnsureSuccessStatusCode();
var jsonData = await new StreamReader(marketData.Content.ReadAsStringAsync()).ReadToEndAsync();
var marketDataObject = JsonConvert.DeserializeObject(jsonData);
return marketDataObject;
}
public async Task GetOrderBookAsync(string symbol, int depth)
{
// Set the API endpoint and parameters
var apiEndpoint = "
var parameters = new Dictionary
{
{ "symbol", symbol }, // Replace with the desired symbol
{ "limit", depth.ToString()} // Number of order book entries to return
};
// Make an API request and parse the JSON response
var orderBook = wait _httpClient.GetAsync(apiEndpoint, parameters).Result;
orderBook.EnsureSuccessStatusCode();
var jsonData = await new StreamReader(orderBook.Content.ReadAsStringAsync()).ReadToEndAsync();
var orderBookObject = JsonConvert.DeserializeObject(jsonData);
return orderBookObject;
}
}
}
Step 4: Register the service in the DI container
Add the following to your “Startup.