Skip to content

Commit

Permalink
libraries update; fixing coinmate API bug with
Browse files Browse the repository at this point in the history
resolves #21
  • Loading branch information
Crynners committed Mar 20, 2022
1 parent ce1a3b3 commit 3e9c7df
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 120 deletions.
9 changes: 7 additions & 2 deletions CryptoBotCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.31911.260
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CryptoBotCore", "CryptoBotCore\CryptoBotCore.csproj", "{6F35ECF7-EF01-4473-A344-2FBD0C3D36D5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CryptoBotCore", "CryptoBotCore\CryptoBotCore.csproj", "{6F35ECF7-EF01-4473-A344-2FBD0C3D36D5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CryptoBotFunction", "CryptoBotFunction\CryptoBotFunction.csproj", "{E6C11A63-713F-4653-9514-8A65ACDAF036}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CryptoBotFunction", "CryptoBotFunction\CryptoBotFunction.csproj", "{E6C11A63-713F-4653-9514-8A65ACDAF036}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
Test|Any CPU = Test|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6F35ECF7-EF01-4473-A344-2FBD0C3D36D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6F35ECF7-EF01-4473-A344-2FBD0C3D36D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F35ECF7-EF01-4473-A344-2FBD0C3D36D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F35ECF7-EF01-4473-A344-2FBD0C3D36D5}.Release|Any CPU.Build.0 = Release|Any CPU
{6F35ECF7-EF01-4473-A344-2FBD0C3D36D5}.Test|Any CPU.ActiveCfg = Test|Any CPU
{6F35ECF7-EF01-4473-A344-2FBD0C3D36D5}.Test|Any CPU.Build.0 = Test|Any CPU
{E6C11A63-713F-4653-9514-8A65ACDAF036}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E6C11A63-713F-4653-9514-8A65ACDAF036}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E6C11A63-713F-4653-9514-8A65ACDAF036}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E6C11A63-713F-4653-9514-8A65ACDAF036}.Release|Any CPU.Build.0 = Release|Any CPU
{E6C11A63-713F-4653-9514-8A65ACDAF036}.Test|Any CPU.ActiveCfg = Test|Any CPU
{E6C11A63-713F-4653-9514-8A65ACDAF036}.Test|Any CPU.Build.0 = Test|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
22 changes: 10 additions & 12 deletions CryptoBotCore/API/BinanceAPI.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
using Binance.Net;
using Binance.Net.Clients;
using Binance.Net.Objects;
using CryptoBotCore.Models;
using CryptoExchange.Net.Authentication;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CryptoBotCore.API
{
Expand Down Expand Up @@ -41,7 +36,10 @@ public BinanceAPI(string pair, Dictionary<ExchangeCredentialType, string> creden

public async Task<string> buyOrderAsync(decimal amount)
{
var callResult = await client.Spot.Order.PlaceOrderAsync($"{pair_base}{pair_quote}", Binance.Net.Enums.OrderSide.Buy, Binance.Net.Enums.OrderType.Market, quoteOrderQuantity: (decimal?)amount);
var callResult = await client.SpotApi.Trading.PlaceOrderAsync($"{pair_base}{pair_quote}",
Binance.Net.Enums.OrderSide.Buy,
Binance.Net.Enums.SpotOrderType.Market,
quoteQuantity: (decimal?)amount);
// Make sure to check if the call was successful
if (!callResult.Success)
{
Expand All @@ -57,7 +55,7 @@ public async Task<string> buyOrderAsync(decimal amount)

public async Task<List<WalletBalances>> getBalancesAsync()
{
var callResult = await client.General.GetAccountInfoAsync();
var callResult = await client.SpotApi.Account.GetAccountInfoAsync();
// Make sure to check if the call was successful
if (!callResult.Success)
{
Expand All @@ -73,7 +71,7 @@ public async Task<List<WalletBalances>> getBalancesAsync()

foreach (var item in balances)
{
wallets.Add(new WalletBalances(item.Asset, item.Free));
wallets.Add(new WalletBalances(item.Asset, item.Available));
}

return wallets;
Expand All @@ -82,7 +80,7 @@ public async Task<List<WalletBalances>> getBalancesAsync()

public async Task<decimal> getTakerFee()
{
var callResult = await client.Spot.Market.GetTradeFeeAsync($"{pair_base}{pair_quote}");
var callResult = await client.SpotApi.ExchangeData.GetTradeFeeAsync($"{pair_base}{pair_quote}");

// Make sure to check if the call was successful
if (!callResult.Success)
Expand All @@ -100,7 +98,7 @@ public async Task<decimal> getTakerFee()

public async Task<decimal> getWithdrawalFeeAsync(decimal? amount = null, string? destinationAddress = null)
{
var callResult = await client.WithdrawDeposit.GetAssetDetailsAsync();
var callResult = await client.SpotApi.ExchangeData.GetAssetDetailsAsync();

// Make sure to check if the call was successful
if (!callResult.Success)
Expand All @@ -118,7 +116,7 @@ public async Task<decimal> getWithdrawalFeeAsync(decimal? amount = null, string?

public async Task<WithdrawalStateEnum> withdrawAsync(decimal amount, string destinationAddress)
{
var callResult = await client.WithdrawDeposit.WithdrawAsync(this.pair_base, destinationAddress, Convert.ToDecimal(amount));
var callResult = await client.SpotApi.Account.WithdrawAsync(this.pair_base, destinationAddress, Convert.ToDecimal(amount));
// Make sure to check if the call was successful
if (!callResult.Success)
{
Expand Down
28 changes: 12 additions & 16 deletions CryptoBotCore/API/BitfinexAPI.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@

using Bitfinex.Net;
using Bitfinex.Net.Clients;
using Bitfinex.Net.Objects;
using Bittrex.Net;
using Bittrex.Net.Objects;
using CryptoBotCore.Models;
using CryptoExchange.Net.Authentication;
using Kucoin.Net.Objects;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace CryptoBotCore.API
{
Expand Down Expand Up @@ -45,7 +41,7 @@ public BitfinexAPI(string pair, Dictionary<ExchangeCredentialType, string> crede

private async Task<decimal> getCurrentPrice()
{
var callResult = await client.GetTickerAsync($"{pair_base}{pair_quote}");
var callResult = await client.SpotApi.ExchangeData.GetTickerAsync($"{pair_base}{pair_quote}");
// Make sure to check if the call was successful
if (!callResult.Success)
{
Expand All @@ -55,17 +51,17 @@ private async Task<decimal> getCurrentPrice()
else
{
// Call succeeded, callResult.Data will have the resulting data
return callResult.Data.Where(x => x.Symbol == $"{pair_base}{pair_quote}").First().Ask;
return callResult.Data.BestAskPrice;
}
}

public async Task<string> buyOrderAsync(decimal amount)
{
var currentPrice = await getCurrentPrice();
var baseAmount = (decimal)amount / currentPrice;
var callResult = await client.PlaceOrderAsync($"{pair_base}{pair_quote}",
Bitfinex.Net.Objects.OrderSide.Buy,
Bitfinex.Net.Objects.OrderType.Market,
var callResult = await client.SpotApi.Trading.PlaceOrderAsync($"{pair_base}{pair_quote}",
Bitfinex.Net.Enums.OrderSide.Buy,
Bitfinex.Net.Enums.OrderType.Market,
currentPrice,
baseAmount);
// Make sure to check if the call was successful
Expand All @@ -83,7 +79,7 @@ public async Task<string> buyOrderAsync(decimal amount)

public async Task<List<WalletBalances>> getBalancesAsync()
{
var callResult = await client.GetBalancesAsync();
var callResult = await client.SpotApi.Account.GetBalancesAsync();
// Make sure to check if the call was successful
if (!callResult.Success)
{
Expand All @@ -96,7 +92,7 @@ public async Task<List<WalletBalances>> getBalancesAsync()

foreach (var account in callResult.Data)
{
wallets.Add(new WalletBalances(account.Currency, account.BalanceAvailable??0m));
wallets.Add(new WalletBalances(account.Asset, account.Available??0m));
}

return wallets;
Expand All @@ -105,7 +101,7 @@ public async Task<List<WalletBalances>> getBalancesAsync()

public async Task<decimal> getTakerFee()
{
var callResult = await client.GetAccountInfoAsync();
var callResult = await client.SpotApi.Account.GetAccountInfoAsync();

// Make sure to check if the call was successful
if (!callResult.Success)
Expand All @@ -124,7 +120,7 @@ public async Task<decimal> getTakerFee()

public async Task<decimal> getWithdrawalFeeAsync(decimal? amount = null, string? destinationAddress = null)
{
var callResult = await client.GetWithdrawalFeesAsync();
var callResult = await client.SpotApi.Account.GetWithdrawalFeesAsync();

// Make sure to check if the call was successful
if (!callResult.Success)
Expand Down Expand Up @@ -161,7 +157,7 @@ public async Task<WithdrawalStateEnum> withdrawAsync(decimal amount, string dest
withdrawal_type = this.pair_base.ToLower();
}

var callResult = await client.WithdrawAsync(withdrawal_type, WithdrawWallet.Trading, (decimal)amount, destinationAddress);
var callResult = await client.SpotApi.Account.WithdrawAsync(withdrawal_type, Bitfinex.Net.Enums.WithdrawWallet.Trading, (decimal)amount, destinationAddress);

// Make sure to check if the call was successful
if (!callResult.Success)
Expand Down
27 changes: 11 additions & 16 deletions CryptoBotCore/API/BittrexAPI.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@

using Bittrex.Net;
using Bittrex.Net.Clients;
using Bittrex.Net.Objects;
using CryptoBotCore.Models;
using CryptoExchange.Net.Authentication;
using Kucoin.Net.Objects;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CryptoBotCore.API
{
Expand Down Expand Up @@ -43,7 +38,7 @@ public BittrexAPI(string pair, Dictionary<ExchangeCredentialType, string> creden

private async Task<decimal> getCurrentPrice()
{
var callResult = await client.GetOrderBookAsync($"{pair_base}{pair_quote}", 0);
var callResult = await client.SpotApi.ExchangeData.GetOrderBookAsync($"{pair_base}{pair_quote}", 0);
// Make sure to check if the call was successful
if (!callResult.Success)
{
Expand All @@ -53,17 +48,17 @@ private async Task<decimal> getCurrentPrice()
else
{
// Call succeeded, callResult.Data will have the resulting data
return callResult.Data.Ask.First().Price;
return callResult.Data.Asks.First().Price;
}
}

public async Task<string> buyOrderAsync(decimal amount)
{
var baseAmount = (decimal)amount / (await getCurrentPrice());
var callResult = await client.PlaceOrderAsync($"{pair_base}{pair_quote}",
Bittrex.Net.Objects.OrderSide.Buy,
Bittrex.Net.Objects.OrderType.Market,
TimeInForce.FillOrKill,
var callResult = await client.SpotApi.Trading.PlaceOrderAsync($"{pair_base}{pair_quote}",
Bittrex.Net.Enums.OrderSide.Buy,
Bittrex.Net.Enums.OrderType.Market,
Bittrex.Net.Enums.TimeInForce.FillOrKill,
baseAmount);
// Make sure to check if the call was successful
if (!callResult.Success)
Expand All @@ -80,7 +75,7 @@ public async Task<string> buyOrderAsync(decimal amount)

public async Task<List<WalletBalances>> getBalancesAsync()
{
var callResult = await client.GetBalancesAsync();
var callResult = await client.SpotApi.Account.GetBalancesAsync();
// Make sure to check if the call was successful
if (!callResult.Success)
{
Expand All @@ -93,7 +88,7 @@ public async Task<List<WalletBalances>> getBalancesAsync()

foreach (var account in callResult.Data)
{
wallets.Add(new WalletBalances(account.Currency, account.Available));
wallets.Add(new WalletBalances(account.Asset, account.Available));
}


Expand All @@ -103,7 +98,7 @@ public async Task<List<WalletBalances>> getBalancesAsync()

public async Task<decimal> getTakerFee()
{
var callResult = await client.GetTradingFeesAsync();
var callResult = await client.SpotApi.Account.GetTradingFeesAsync();

// Make sure to check if the call was successful
if (!callResult.Success)
Expand All @@ -127,7 +122,7 @@ public Task<decimal> getWithdrawalFeeAsync(decimal? amount = null, string? desti

public async Task<WithdrawalStateEnum> withdrawAsync(decimal amount, string destinationAddress)
{
var callResult = await client.WithdrawAsync(this.pair_base, (decimal)amount, destinationAddress);
var callResult = await client.SpotApi.Account.WithdrawAsync(this.pair_base, (decimal)amount, destinationAddress);

// Make sure to check if the call was successful
if (!callResult.Success)
Expand Down
29 changes: 23 additions & 6 deletions CryptoBotCore/API/CoinmateAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private string getSecuredHeaderPart()
public async Task<string> buyOrderAsync(decimal amount)
{
int wait = 0;
var attempt = 0;
do
{
try
Expand All @@ -104,9 +105,14 @@ public async Task<string> buyOrderAsync(decimal amount)
}
catch (Exception ex)
{
attempt++;
if (attempt >= 3)
{
throw new Exception(JsonConvert.SerializeObject(ex));
}
Log.LogError(JsonConvert.SerializeObject(ex));
wait = (wait == 0) ? 200 : wait * 2;
Thread.Sleep(wait);
await Task.Delay(wait);
}
} while (true);
}
Expand All @@ -125,6 +131,7 @@ public async Task<decimal> getWithdrawalFeeAsync(decimal? amount = null, string?
}

int wait = 0;
var attempt = 0;
do
{
try
Expand All @@ -144,10 +151,15 @@ public async Task<decimal> getWithdrawalFeeAsync(decimal? amount = null, string?
}
catch (Exception ex)
{
attempt++;
if (attempt >= 3)
{
throw new Exception(JsonConvert.SerializeObject(ex));
}
Log.LogError(JsonConvert.SerializeObject(ex));

wait = (wait == 0) ? 200 : wait * 2;
Thread.Sleep(wait);
await Task.Delay(wait);
}
} while (true);
}
Expand Down Expand Up @@ -192,13 +204,13 @@ public async Task<WithdrawalStateEnum> withdrawAsync(decimal amount, string dest
Log.LogError(JsonConvert.SerializeObject(ex));
attempt++;

if(attempt >= 5)
if(attempt >= 3)
{
throw new Exception(JsonConvert.SerializeObject(ex));
}

wait = (wait == 0) ? 200 : wait * 2;
Thread.Sleep(wait);
await Task.Delay(wait);
}
} while (true);
}
Expand Down Expand Up @@ -249,10 +261,15 @@ public async Task<List<WalletBalances>> getBalancesAsync()
{
Log.LogError(JsonConvert.SerializeObject(ex));
attempt++;

if (attempt >= 3)
{
throw new Exception(JsonConvert.SerializeObject(ex));
}
wait = (wait == 0) ? 200 : wait * 2;
Thread.Sleep(wait);
await Task.Delay(wait);
}
} while (attempt < 5);
} while (true);

throw new Exception("Coinmate API is currently not available.");
}
Expand Down
Loading

0 comments on commit 3e9c7df

Please sign in to comment.