Skip to content

Commit

Permalink
Merge pull request #23 from Ko1ors/refactoring
Browse files Browse the repository at this point in the history
ETH/ETC Refactoring Improvements
  • Loading branch information
Ko1ors committed Jul 3, 2022
2 parents 2b61e10 + 8c6aa62 commit ea3271a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 27 deletions.
4 changes: 2 additions & 2 deletions ETCModule/Models/EtcPriceResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public class EtcPriceResult : ResultBase
public class EtcPrice
{
[JsonProperty("coin_btc")]
public double CoinBtc;
public double? CoinBtc;

[JsonProperty("coin_btc_timestamp")]
public string CoinBtcTimestamp;

[JsonProperty("coin_usd")]
public double CoinUsd;
public double? CoinUsd;

[JsonProperty("coin_usd_timestamp")]
public string CoinUsdTimestamp;
Expand Down
8 changes: 4 additions & 4 deletions ETCModule/Services/EtcService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ public async Task<EtcPriceResult> GetEtcPriceAsync()
var priceModel = await _clientService.GetPriceAsync();
if (priceModel.Status == "1" && priceModel.Message == "OK")
{
priceModel.Price.CoinBtc = Math.Round(priceModel.Price.CoinBtc, 8);
priceModel.Price.CoinUsd = Math.Round(priceModel.Price.CoinUsd, 2);
priceModel.Price.CoinBtc = Math.Round(priceModel.Price.CoinBtc ?? -1, 8);
priceModel.Price.CoinUsd = Math.Round(priceModel.Price.CoinUsd ?? -1, 2);
return priceModel;
}
Thread.Sleep(500 * (i + 1));
await Task.Delay(500 * (i + 1));
}
return null;
}
Expand All @@ -101,7 +101,7 @@ public async Task<double> GetEtcWalletBalanceAsync(string wallet)
{
return Math.Round(float.Parse(walletBalanceModel.Result) / _divisor, 4);
}
Thread.Sleep(500 * (i + 1));
await Task.Delay(500 * (i + 1));
}
return -1;
}
Expand Down
22 changes: 20 additions & 2 deletions ETCModule/ViewModels/EtcPriceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,26 @@ public EtcPrice Price
}
}

public string PriceText => Price is null ?
"$ーー ❙ ーー BTC" : $"${Price.CoinUsd}{Math.Round(Price.CoinBtc, 5).ToString().Replace(",", ".")} BTC";
public string PriceText
{
get
{
if (Price is null)
return "$ーー ❙ ーー BTC";

string priceText;
if (Price.CoinUsd.HasValue && Price.CoinUsd.Value >= 0)
priceText = $"${Price.CoinUsd}";
else
priceText = "$ーー ❙";

if (Price.CoinBtc.HasValue && Price.CoinBtc.Value >= 0)
priceText += $" {Math.Round(Price.CoinBtc.Value, 5).ToString().Replace(",", ".")} BTC";
else
priceText += " ーー BTC";
return priceText;
}
}

public EtcPriceViewModel(IEtcService etcService)
{
Expand Down
18 changes: 16 additions & 2 deletions ETCModule/ViewModels/EtcWalletBalanceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,22 @@ public EtcCompositeResult Result
}
}

public string WalletBalanceText => Result is null ?
"ーー ETC ❙ $ーー" : $"{Result.WalletBalance.ToString().Replace(",", ".")} ETC ❙ ${Math.Round(Result.WalletBalance * Result.Price.CoinUsd, 2).ToString().Replace(",", ".")}";
public string WalletBalanceText
{
get
{
if (Result is null || Result.WalletBalance < 0)
return "ーー ETC ❙ $ーー";

string priceText = $"{Result.WalletBalance.ToString().Replace(",", ".")} ETC ❙";

if (Result.Price.CoinUsd.HasValue && Result.Price.CoinUsd.Value >= 0)
priceText += $" ${Math.Round(Result.WalletBalance * Result.Price.CoinUsd.Value, 2).ToString().Replace(",", ".")}";
else
priceText += " $ーー";
return priceText;
}
}

public EtcWalletBalanceViewModel(IEtcService etcService)
{
Expand Down
33 changes: 16 additions & 17 deletions ETHModule/Services/ETHService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using ETHModule.Data;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ETHModule.Services
Expand All @@ -24,35 +23,35 @@ public class EthService : IETHService
return model;
}

public Task<EthPrice> GetPriceAsync(string apiKey)
public async Task<EthPrice> GetPriceAsync(string apiKey)
{
for (int i = 0; i < maxTries; i++)
{
var result = EthRequest.GetPrice(apiKey);
if (result.Status != "0" && result.Message != "NOTOK")
{
return Task.FromResult(result);
return result;
}
Thread.Sleep(500 * (i + 1));
await Task.Delay(500 * (i + 1));
}
return Task.FromResult(default(EthPrice));
return default;
}

public Task<EthGasPrice> GetGasPriceAsync(string apiKey)
public async Task<EthGasPrice> GetGasPriceAsync(string apiKey)
{
for (int i = 0; i < maxTries; i++)
{
var result = EthRequest.GetGasPrice(apiKey);
if (result.Status != "0" && result.Message != "NOTOK")
{
return Task.FromResult(result);
return result;
}
Thread.Sleep(500 * (i + 1));
await Task.Delay(500 * (i + 1));
}
return Task.FromResult(default(EthGasPrice));
return default;
}

public Task<double> GetAvgBlockRewardAsync(string apiKey, int lastBlock)
public async Task<double> GetAvgBlockRewardAsync(string apiKey, int lastBlock)
{
double blockreward = 0;
bool success;
Expand All @@ -69,26 +68,26 @@ public Task<double> GetAvgBlockRewardAsync(string apiKey, int lastBlock)
blockreward += double.Parse(result.Result.BlockReward) / 1000000000000000000;
break;
}
Thread.Sleep(500 * (j + 1));
await Task.Delay(500 * (j + 1));
}
if (!success)
return Task.FromResult(default(double));
return default;
}
return Task.FromResult(Math.Round(blockreward / i, 5));
return Math.Round(blockreward / i, 5);
}

public Task<double> GetWalletBalanceAsync(string apiKey, string wallet)
public async Task<double> GetWalletBalanceAsync(string apiKey, string wallet)
{
for (int i = 0; i < maxTries; i++)
{
var result = EthRequest.GetWalletBalance(apiKey, wallet);
if (result.Status != "0" && result.Message != "NOTOK")
{
return Task.FromResult(Math.Round(double.Parse(result.Result) / 1000000000000000000, 5));
return Math.Round(double.Parse(result.Result) / 1000000000000000000, 5);
}
Thread.Sleep(500 * (i + 1));
await Task.Delay(500 * (i + 1));
}
return Task.FromResult(default(double));
return default;
}
}
}

0 comments on commit ea3271a

Please sign in to comment.