Skip to content
This repository was archived by the owner on Mar 20, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CryptoGramBot/CryptoGramBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.2.0" />
<PackageReference Include="Binance" Version="0.2.0-alpha.13" />
<PackageReference Include="Bittrex.Net" Version="1.3.3" />
<PackageReference Include="Bittrex.Net" Version="2.1.20" />
<PackageReference Include="CsvHelper" Version="2.16.3" />
<PackageReference Include="Enexure.MicroBus" Version="3.6.0" />
<PackageReference Include="Enexure.MicroBus.Autofac" Version="3.6.0" />
Expand All @@ -31,7 +31,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
<PackageReference Include="Serilog" Version="2.6.0" />
<PackageReference Include="Serilog.AspNetCore" Version="2.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
Expand Down
26 changes: 12 additions & 14 deletions CryptoGramBot/Helpers/Convertors/BittrexConvertor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static List<Deposit> BittrexToDeposits(BittrexDeposit[] responseResult)
return list;
}

public static List<OpenOrder> BittrexToOpenOrders(BittrexOrder[] bittrexOrders)
public static List<OpenOrder> BittrexToOpenOrders(BittrexOpenOrdersOrder[] bittrexOrders)
{
var list = new List<OpenOrder>();

Expand All @@ -66,8 +66,8 @@ public static List<OpenOrder> BittrexToOpenOrders(BittrexOrder[] bittrexOrders)
Exchange = Constants.Bittrex,
CommissionPaid = openOrder.CommissionPaid,
OrderUuid = openOrder.OrderUuid.ToString(),
Condition = openOrder.Condition,
ConditionTarget = openOrder.ConditionTarget,
Condition = openOrder.Condition.ToString(),
ConditionTarget = openOrder.ConditionTarget?.ToString(),
CancelInitiated = openOrder.CancelInitiated,
ImmediateOrCancel = openOrder.ImmediateOrCancel,
IsConditional = openOrder.IsConditional,
Expand All @@ -76,7 +76,7 @@ public static List<OpenOrder> BittrexToOpenOrders(BittrexOrder[] bittrexOrders)
Price = openOrder.Price,
Quantity = openOrder.Quantity,
QuantityRemaining = openOrder.QuantityRemaining,
Side = openOrder.OrderType == OrderTypeExtended.LimitBuy ? TradeSide.Buy : TradeSide.Sell
Side = openOrder.OrderType == OrderSideExtended.LimitBuy ? TradeSide.Buy : TradeSide.Sell
};

list.Add(order);
Expand All @@ -85,7 +85,7 @@ public static List<OpenOrder> BittrexToOpenOrders(BittrexOrder[] bittrexOrders)
return list;
}

public static List<Trade> BittrexToTrades(BittrexOrder[] bittrexTrades, ILogger logger)
public static List<Trade> BittrexToTrades(BittrexOrderHistoryOrder[] bittrexTrades, ILogger logger)
{
var tradeList = new List<Trade>();

Expand All @@ -97,24 +97,24 @@ public static List<Trade> BittrexToTrades(BittrexOrder[] bittrexTrades, ILogger
Exchange = Constants.Bittrex,
Base = ccy[0],
Terms = ccy[1],
Commission = completedOrder.CommissionPaid,
Commission = completedOrder.Commission,
ExchangeId = completedOrder.OrderUuid.ToString(),
Limit = completedOrder.Limit,
Quantity = completedOrder.Quantity,
QuantityRemaining = completedOrder.QuantityRemaining,
Timestamp = completedOrder.Closed.GetValueOrDefault(),
Side = completedOrder.OrderType == OrderTypeExtended.LimitBuy ? TradeSide.Buy : TradeSide.Sell
Side = completedOrder.OrderType == OrderSideExtended.LimitBuy ? TradeSide.Buy : TradeSide.Sell
};

if (completedOrder.Closed.HasValue)

if (trade.Side == TradeSide.Buy)
{
trade.Cost = completedOrder.Price + completedOrder.CommissionPaid;
trade.Cost = completedOrder.Price + completedOrder.Commission;
}
else if (trade.Side == TradeSide.Sell)
{
trade.Cost = completedOrder.Price - completedOrder.CommissionPaid;
trade.Cost = completedOrder.Price - completedOrder.Commission;
}
else
{
Expand All @@ -140,12 +140,10 @@ public static List<WalletBalance> BittrexToWalletBalances(BittrexBalance[] respo
Exchange = Constants.Bittrex,
Timestamp = DateTime.Now,
Address = wallet.CryptoAddress,
Available = wallet.Available,
Balance = wallet.Balance,
Pending = wallet.Pending,
Available = wallet.Available ?? 0,
Balance = wallet.Balance ?? 0,
Pending = wallet.Pending ?? 0,
Currency = wallet.Currency,
Requested = wallet.Requested,
Uuid = wallet.Uuid
};

if (String.IsNullOrEmpty(wallet.CryptoAddress))
Expand Down
55 changes: 37 additions & 18 deletions CryptoGramBot/Services/Exchanges/BittrexService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using Bittrex.Net;
using Bittrex.Net.Objects;
using CryptoExchange.Net.Authentication;
using CryptoGramBot.Configuration;
using CryptoGramBot.Helpers;
using CryptoGramBot.Models;
Expand Down Expand Up @@ -40,17 +41,20 @@ public async Task<BalanceInformation> GetBalance()
List<WalletBalance> bittrexBalances = new List<WalletBalance>();
try
{
using (var client = new BittrexClient(_config.Key, _config.Secret))
using (var client = new BittrexClient(new BittrexClientOptions()
{
ApiCredentials = new ApiCredentials(_config.Key, _config.Secret)
}))
{
var response = await client.GetBalancesAsync();

if (response.Success)
{
bittrexBalances = BittrexConvertor.BittrexToWalletBalances(response.Result);
bittrexBalances = BittrexConvertor.BittrexToWalletBalances(response.Data);
}
else
{
_log.LogWarning($"Bittrex returned an error {response.Error.ErrorCode} : {response.Error.ErrorMessage}");
_log.LogWarning($"Bittrex returned an error {response.Error.Code} : {response.Error.Message}");
}
}
}
Expand Down Expand Up @@ -138,17 +142,20 @@ public async Task<List<Deposit>> GetNewDeposits()

try
{
using (var client = new BittrexClient(_config.Key, _config.Secret))
using (var client = new BittrexClient(new BittrexClientOptions()
{
ApiCredentials = new ApiCredentials(_config.Key, _config.Secret)
}))
{
var response = await client.GetDepositHistoryAsync();

if (response.Success)
{
list = BittrexConvertor.BittrexToDeposits(response.Result);
list = BittrexConvertor.BittrexToDeposits(response.Data);
}
else
{
_log.LogWarning($"Bittrex returned an error {response.Error.ErrorCode} : {response.Error.ErrorMessage}");
_log.LogWarning($"Bittrex returned an error {response.Error.Code} : {response.Error.Message}");
}
}
}
Expand All @@ -169,17 +176,20 @@ public async Task<List<OpenOrder>> GetNewOpenOrders(DateTime lastChecked)

try
{
using (var client = new BittrexClient(_config.Key, _config.Secret))
using (var client = new BittrexClient(new BittrexClientOptions()
{
ApiCredentials = new ApiCredentials(_config.Key, _config.Secret)
}))
{
var response = await client.GetOpenOrdersAsync();

if (response.Success)
{
openOrders = BittrexConvertor.BittrexToOpenOrders(response.Result);
openOrders = BittrexConvertor.BittrexToOpenOrders(response.Data);
}
else
{
_log.LogWarning($"Bittrex returned an error {response.Error.ErrorCode} : {response.Error.ErrorMessage}");
_log.LogWarning($"Bittrex returned an error {response.Error.Code} : {response.Error.Message}");
}
}
}
Expand All @@ -199,17 +209,20 @@ public async Task<List<Withdrawal>> GetNewWithdrawals()

try
{
using (var client = new BittrexClient(_config.Key, _config.Secret))
using (var client = new BittrexClient(new BittrexClientOptions()
{
ApiCredentials = new ApiCredentials(_config.Key, _config.Secret)
}))
{
var response = await client.GetWithdrawalHistoryAsync();

if (response.Success)
{
list = BittrexConvertor.BittrexToWithdrawals(response.Result);
list = BittrexConvertor.BittrexToWithdrawals(response.Data);
}
else
{
_log.LogWarning($"Bittrex returned an error {response.Error.ErrorCode} : {response.Error.ErrorMessage}");
_log.LogWarning($"Bittrex returned an error {response.Error.Code} : {response.Error.Message}");
}
}
}
Expand All @@ -229,17 +242,20 @@ public async Task<List<Trade>> GetOrderHistory(DateTime lastChecked)

try
{
using (var client = new BittrexClient(_config.Key, _config.Secret))
using (var client = new BittrexClient(new BittrexClientOptions()
{
ApiCredentials = new ApiCredentials(_config.Key, _config.Secret)
}))
{
var response = await client.GetOrderHistoryAsync();

if (response.Success)
{
list = BittrexConvertor.BittrexToTrades(response.Result, _log);
list = BittrexConvertor.BittrexToTrades(response.Data, _log);
}
else
{
_log.LogWarning($"Bittrex returned an error {response.Error.ErrorCode} : {response.Error.ErrorMessage}");
_log.LogWarning($"Bittrex returned an error {response.Error.Code} : {response.Error.Message}");
}
}
}
Expand Down Expand Up @@ -295,16 +311,19 @@ public async Task<decimal> GetPrice(string baseCcy, string termsCurrency)

private async Task<BittrexPrice> GetTicker(string baseCcy, string termsCurrency)
{
using (var client = new BittrexClient(_config.Key, _config.Secret))
using (var client = new BittrexClient(new BittrexClientOptions()
{
ApiCredentials = new ApiCredentials(_config.Key, _config.Secret)
}))
{
var response = await client.GetTickerAsync($"{baseCcy}-{termsCurrency}");

if (response.Success)
{
return response.Result;
return response.Data;
}

_log.LogWarning($"Bittrex returned an error {response.Error.ErrorCode} : {response.Error.ErrorMessage}");
_log.LogWarning($"Bittrex returned an error {response.Error.Code} : {response.Error.Message}");
return null;
}
}
Expand Down
14 changes: 11 additions & 3 deletions CryptoGramBot/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Autofac.Extensions.DependencyInjection;
using Binance;
using Bittrex.Net;
using Bittrex.Net.RateLimiter;
using CryptoGramBot.Configuration;
using CryptoGramBot.Data;
using CryptoGramBot.Extensions;
Expand All @@ -26,7 +25,10 @@
using Microsoft.Extensions.Options;
using Serilog;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Bittrex.Net.Objects;
using CryptoExchange.Net.RateLimiter;

namespace CryptoGramBot
{
Expand Down Expand Up @@ -284,8 +286,14 @@ private async Task OnStarting()
var limiterTotal = new RateLimiterPerEndpoint(1, TimeSpan.FromSeconds(1));
var limiterPerEndpoint = new RateLimiterPerEndpoint(1, TimeSpan.FromSeconds(1));

BittrexDefaults.AddDefaultRateLimiter(limiterTotal);
BittrexDefaults.AddDefaultRateLimiter(limiterPerEndpoint);
BittrexClient.SetDefaultOptions(new BittrexClientOptions
{
RateLimiters = new List<IRateLimiter>
{
limiterTotal,
limiterPerEndpoint
}
});

startupService.Start();
}
Expand Down