03 Feb Ethereum: Binance gets C# .Net Core order book.
Ethereum: Building a Binary Order Book in C
.NET Core
As part of our MarketMaker team, we needed a robust solution to extract crypto market data from Binance without API keys. In this article, we will explore how to achieve this using C
.NET Core.
Requirements
Before we dive into the implementation, let’s outline the requirements:
- Public market data
- Market depth of the order book 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 yourStartup.csfile:
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 app, IWebHostEnvironment env)
{
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 read-only HttpClient _httpClient;
public MarketDataService()
{
_httpClient = new HttpClient();
}
public async Task GetPublicMarketDataAsync()
{
// Set API endpoint and parameters
var apiEndpoint = "
variable parameters = new Dictionary
{
{ "symbol", "ETH/BTC" }, // Replace with your desired symbol
{ "limit", "10" } // Number of market data records to return
};
// Make an API request and parse the JSON response
var marketData = await _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 API endpoint and parameters
var apiEndpoint = "
variable parameters = new Dictionary
{
{ "symbol", symbol }, // Replace with your desired symbol
{ "limit", depth.ToString()} // Number of order book entries to return
};
// Make an API request and parse the JSON response
var orderBook = await _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.
No Comments