Merge branch 'Dev/Decimals'

This commit is contained in:
Stedd 2022-02-13 16:39:25 +01:00
commit 457aeb4eca
4 changed files with 70 additions and 24 deletions

View File

@ -1,9 +1,19 @@
using System; using System;
using System.Globalization;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace CryptoCalc namespace CryptoCalc
{ {
public enum TransactionTypes
{
Buy,
Sell,
Transfer,
Mining
}
public interface IDBClasses public interface IDBClasses
{ {
static string DBName { get; } static string DBName { get; }
@ -21,8 +31,8 @@ namespace CryptoCalc
public virtual string DBVariables { get; set; } public virtual string DBVariables { get; set; }
public string DBSaveDataString => $"({Regex.Replace(DBVariables, "@", "")}) values ({DBVariables})"; public string DBSaveDataString => $"({Regex.Replace(DBVariables, "@", "")}) values ({DBVariables})";
public int Index { get; set; } public long Index { get; set; }
public long UnixTime { get; set; } public ulong UnixTime { get; set; }
#endregion #endregion
@ -31,20 +41,20 @@ namespace CryptoCalc
public void SaveUnixTimeNow() public void SaveUnixTimeNow()
{ {
UnixTime = (long)DateTime.UtcNow.Subtract(DateTime.UnixEpoch).TotalSeconds; UnixTime = (ulong)DateTime.UtcNow.Subtract(DateTime.UnixEpoch).TotalSeconds;
} }
public static long GetUnixTime(DateTime dateTime) public static ulong GetUnixTime(DateTime dateTime)
{ {
return (long)dateTime.Subtract(DateTime.UnixEpoch).TotalSeconds; return (ulong)dateTime.Subtract(DateTime.UnixEpoch).TotalSeconds;
} }
public static DateTime GetUTCTimeFromUnixTime(long unix) public static DateTime GetUTCTimeFromUnixTime(ulong unix)
{ {
return DateTime.UnixEpoch.AddSeconds(unix).ToUniversalTime(); return DateTime.UnixEpoch.AddSeconds(unix).ToUniversalTime();
} }
public static DateTime GetLocalTimeFromUnixTime(long unix) public static DateTime GetLocalTimeFromUnixTime(ulong unix)
{ {
return DateTime.UnixEpoch.AddSeconds(unix).ToLocalTime(); return DateTime.UnixEpoch.AddSeconds(unix).ToLocalTime();
} }
@ -60,12 +70,14 @@ namespace CryptoCalc
public override string DBVariables { get => base.DBVariables; set => base.DBVariables = value; } public override string DBVariables { get => base.DBVariables; set => base.DBVariables = value; }
public override string DBTableName => "Transactions"; public override string DBTableName => "Transactions";
public int WalletID { get; set; } public long WalletID { get; set; }
public string Currency { get; set; } public string Currency { get; set; }
public decimal Amount { get; set; } public long Amount { get; set; }
public ulong AmountDecimal { get; set; }
public string TransactionType { get; set; } public string TransactionType { get; set; }
public string FeeCurrency { get; set; } public string FeeCurrency { get; set; }
public decimal FeeAmount { get; set; } public long FeeAmount { get; set; }
public ulong FeeAmountDecimal { get; set; }
public string Platform { get; set; } public string Platform { get; set; }
public string Note { get; set; } public string Note { get; set; }
@ -75,12 +87,12 @@ namespace CryptoCalc
public Transaction() { SetDBStrings(); } public Transaction() { SetDBStrings(); }
public Transaction(int walletID, string currency, decimal amount, string type) public Transaction(long walletID, string currency, decimal amount, string type)
{ {
SaveUnixTimeNow(); SaveUnixTimeNow();
WalletID = walletID; WalletID = walletID;
Currency = currency; Currency = currency;
Amount = amount; (Amount, AmountDecimal) = Util.SplitDecimal(amount);
TransactionType = type; TransactionType = type;
Platform = ""; Platform = "";
@ -93,10 +105,10 @@ namespace CryptoCalc
{ {
SaveUnixTimeNow(); SaveUnixTimeNow();
Currency = currency; Currency = currency;
Amount = amount; (Amount, AmountDecimal) = Util.SplitDecimal(amount);
TransactionType = type; TransactionType = type;
FeeCurrency = feeCurrency; FeeCurrency = feeCurrency;
FeeAmount = feeAmount; (FeeAmount, FeeAmountDecimal) = Util.SplitDecimal(feeAmount);
Platform = ""; Platform = "";
Note = ""; Note = "";
@ -114,14 +126,16 @@ namespace CryptoCalc
+ $"@{nameof(UnixTime)}," + $"@{nameof(UnixTime)},"
+ $"@{nameof(Currency)}," + $"@{nameof(Currency)},"
+ $"@{nameof(Amount)}," + $"@{nameof(Amount)},"
+ $"@{nameof(AmountDecimal)},"
+ $"@{nameof(TransactionType)}," + $"@{nameof(TransactionType)},"
+ $"@{nameof(FeeCurrency)}," + $"@{nameof(FeeCurrency)},"
+ $"@{nameof(FeeAmount)}," + $"@{nameof(FeeAmount)},"
+ $"@{nameof(FeeAmountDecimal)},"
+ $"@{nameof(Platform)}," + $"@{nameof(Platform)},"
+ $"@{nameof(Note)}"; + $"@{nameof(Note)}";
} }
public string FullInfo => $"{ Index } {WalletID} { GetLocalTimeFromUnixTime(UnixTime) } { Currency } { Amount } { TransactionType }"; public string FullInfo => $"{ Index } {WalletID} { GetLocalTimeFromUnixTime(UnixTime) } { Currency } { Util.CombineDecimal(Amount, AmountDecimal) } { TransactionType }";
public string CreateTable(string walletTableName) public string CreateTable(string walletTableName)
{ {
@ -131,10 +145,12 @@ namespace CryptoCalc
+ $"\"{nameof(WalletID)}\" INTEGER NOT NULL," + $"\"{nameof(WalletID)}\" INTEGER NOT NULL,"
+ $"\"{nameof(UnixTime)}\" INTEGER NOT NULL," + $"\"{nameof(UnixTime)}\" INTEGER NOT NULL,"
+ $"\"{nameof(Currency)}\" TEXT NOT NULL," + $"\"{nameof(Currency)}\" TEXT NOT NULL,"
+ $"\"{nameof(Amount)}\" REAL NOT NULL," + $"\"{nameof(Amount)}\" INTEGER NOT NULL,"
+ $"\"{nameof(AmountDecimal)}\" INTEGER NOT NULL,"
+ $"\"{nameof(TransactionType)}\" TEXT NOT NULL," + $"\"{nameof(TransactionType)}\" TEXT NOT NULL,"
+ $"\"{nameof(FeeCurrency)}\" TEXT," + $"\"{nameof(FeeCurrency)}\" TEXT,"
+ $"\"{nameof(FeeAmount)}\" REAL," + $"\"{nameof(FeeAmount)}\" INTEGER,"
+ $"\"{nameof(FeeAmountDecimal)}\" INTEGER,"
+ $"\"{nameof(Platform)}\" TEXT," + $"\"{nameof(Platform)}\" TEXT,"
+ $"\"{nameof(Note)}\" TEXT," + $"\"{nameof(Note)}\" TEXT,"
+ $"PRIMARY KEY(\"{nameof(Index)}\" AUTOINCREMENT)," + $"PRIMARY KEY(\"{nameof(Index)}\" AUTOINCREMENT),"
@ -152,11 +168,12 @@ namespace CryptoCalc
public override string DBVariables { get => base.DBVariables; set => base.DBVariables = value; } public override string DBVariables { get => base.DBVariables; set => base.DBVariables = value; }
public override string DBTableName => "Wallets"; public override string DBTableName => "Wallets";
public long UnixTimeCreated { get; set; } public ulong UnixTimeCreated { get; set; }
public string Platform { get; set; } public string Platform { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string Currency { get; set; } public string Currency { get; set; }
public decimal Balance { get; set; } public long Balance { get; set; }
public ulong BalanceDecimal { get; set; }
public int DefaultWallet { get; set; } public int DefaultWallet { get; set; }
public string Note { get; set; } public string Note { get; set; }
@ -173,7 +190,7 @@ namespace CryptoCalc
Platform = platform; Platform = platform;
Name = name; Name = name;
Currency = currency; Currency = currency;
Balance = 0M; (Balance, BalanceDecimal) = Util.SplitDecimal(0);
DefaultWallet = 0; DefaultWallet = 0;
Note = note; Note = note;
@ -193,6 +210,7 @@ namespace CryptoCalc
+ $"@{nameof(Name)}," + $"@{nameof(Name)},"
+ $"@{nameof(Currency)}," + $"@{nameof(Currency)},"
+ $"@{nameof(Balance)}," + $"@{nameof(Balance)},"
+ $"@{nameof(BalanceDecimal)},"
+ $"@{nameof(DefaultWallet)}," + $"@{nameof(DefaultWallet)},"
+ $"@{nameof(Note)}"; + $"@{nameof(Note)}";
} }
@ -209,7 +227,8 @@ namespace CryptoCalc
+ $"\"{nameof(Platform)}\" TEXT," + $"\"{nameof(Platform)}\" TEXT,"
+ $"\"{nameof(Name)}\" TEXT," + $"\"{nameof(Name)}\" TEXT,"
+ $"\"{nameof(Currency)}\" TEXT NOT NULL," + $"\"{nameof(Currency)}\" TEXT NOT NULL,"
+ $"\"{nameof(Balance)}\" REAL NOT NULL," + $"\"{nameof(Balance)}\" INTEGER NOT NULL,"
+ $"\"{nameof(BalanceDecimal)}\" INTEGER NOT NULL,"
+ $"\"{nameof(DefaultWallet)}\" INTEGER," + $"\"{nameof(DefaultWallet)}\" INTEGER,"
+ $"\"{nameof(Note)}\" TEXT," + $"\"{nameof(Note)}\" TEXT,"
+ $"PRIMARY KEY(\"{nameof(Index)}\" AUTOINCREMENT)" + $"PRIMARY KEY(\"{nameof(Index)}\" AUTOINCREMENT)"

View File

@ -23,6 +23,7 @@
<ListBox x:Name="transactionsFoundListBox" Margin="27,66,331,10"/> <ListBox x:Name="transactionsFoundListBox" Margin="27,66,331,10"/>
<TextBox x:Name="currencyText" HorizontalAlignment="Left" Margin="89,21,0,0" Text="SOL" TextWrapping="Wrap" VerticalAlignment="Top" Width="75" Height="22" LostFocus="currencyText_LostFocus"/> <TextBox x:Name="currencyText" HorizontalAlignment="Left" Margin="89,21,0,0" Text="SOL" TextWrapping="Wrap" VerticalAlignment="Top" Width="75" Height="22" LostFocus="currencyText_LostFocus"/>
<Button x:Name="searchButton" Content="Search" HorizontalAlignment="Left" Margin="169,20,0,0" VerticalAlignment="Top" Click="searchButton_Click" Height="25" Width="55"/> <Button x:Name="searchButton" Content="Search" HorizontalAlignment="Left" Margin="169,20,0,0" VerticalAlignment="Top" Click="searchButton_Click" Height="25" Width="55"/>
<DatePicker x:Name="insertTransactionDatePicker" HorizontalAlignment="Right" Margin="0,0,60,157" VerticalAlignment="Bottom" Width="120" FirstDayOfWeek="Monday" SelectedDateFormat="Short"/>
</Grid> </Grid>
</TabItem> </TabItem>
<TabItem Header="Overview"> <TabItem Header="Overview">

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
@ -49,7 +50,7 @@ namespace CryptoCalc
new Transaction( new Transaction(
selectedWallet.Index, selectedWallet.Index,
inputCurrency.Text, inputCurrency.Text,
Convert.ToDecimal(inputAmount.Text), Convert.ToDecimal(inputAmount.Text, CultureInfo.InvariantCulture),
inputType.Text inputType.Text
) )
); );
@ -110,7 +111,7 @@ namespace CryptoCalc
t.WalletID = selectedWallet.Index; t.WalletID = selectedWallet.Index;
t.SaveUnixTimeNow(); t.SaveUnixTimeNow();
t.Currency = "SOL"; t.Currency = "SOL";
t.Amount = Convert.ToDecimal(30 * rand.NextDouble()); (t.Amount, t.AmountDecimal) = Util.SplitDecimal(Convert.ToDecimal(30m * (decimal)rand.NextDouble()));
t.TransactionType = "BUY"; t.TransactionType = "BUY";
t.Platform = "Firi"; t.Platform = "Firi";
t.Note = "Test"; t.Note = "Test";
@ -134,7 +135,8 @@ namespace CryptoCalc
private void inputAmount_LostFocus(object sender, RoutedEventArgs e) private void inputAmount_LostFocus(object sender, RoutedEventArgs e)
{ {
inputAmount.Text = inputAmount.Text.Replace(".", ","); //inputAmount.Text = inputAmount.Text.Replace(".", ",");
inputAmount.Text = inputAmount.Text.Replace(",", ".");
} }
private void currencyText_LostFocus(object sender, RoutedEventArgs e) private void currencyText_LostFocus(object sender, RoutedEventArgs e)

24
Util.cs
View File

@ -4,9 +4,33 @@ namespace CryptoCalc
{ {
public class Util public class Util
{ {
private static readonly decimal lowestUnit = 1e18m;
public static string GetConnectionString(string name = "Default") public static string GetConnectionString(string name = "Default")
{ {
return ConfigurationManager.ConnectionStrings[name].ConnectionString; return ConfigurationManager.ConnectionStrings[name].ConnectionString;
} }
public static ulong ConvToULong(decimal value)
{
return decimal.ToUInt64(value * lowestUnit);
}
public static decimal ConvToDecimal(ulong value)
{
return value / lowestUnit;
}
public static (long wholeNumber, ulong fractionNumber) SplitDecimal(decimal value)
{
long w = (long)value;
ulong f = ConvToULong((value % 1));
return (w, f);
}
public static decimal CombineDecimal(long amount, ulong amountDecimal)
{
return amount + ConvToDecimal(amountDecimal);
}
} }
} }