diff --git a/Database/Database-ConnectionsCommands.cs b/Database/Database-ConnectionsCommands.cs index 93c0f94c..7b7af070 100644 --- a/Database/Database-ConnectionsCommands.cs +++ b/Database/Database-ConnectionsCommands.cs @@ -229,7 +229,7 @@ namespace Swarmops.Database } - private string SqlSanitize (string input) + private string SqlSanitize (string input) // TODO: Replace with parameterized queries everywhere. { string[] forbiddenArray = { diff --git a/Database/Database-Currencies.cs b/Database/Database-Currencies.cs index 875a35fe..ed21c414 100644 --- a/Database/Database-Currencies.cs +++ b/Database/Database-Currencies.cs @@ -100,6 +100,7 @@ namespace Swarmops.Database #endregion + #region Creation and manipulation - stored procedures public int CreateCurrency (string code, string name, string sign) diff --git a/Database/Database-ExchangeRates.cs b/Database/Database-ExchangeRates.cs index 7a598fca..6640f351 100644 --- a/Database/Database-ExchangeRates.cs +++ b/Database/Database-ExchangeRates.cs @@ -31,28 +31,42 @@ namespace Swarmops.Database #endregion #region Record reading - SELECT statements - /* - public BasicCurrency GetCurrency(int currencyId) + + public double GetCurrencyExchangeRate (int fromCurrencyId, int toCurrencyId, DateTime valuationDateTime) { using (DbConnection connection = GetMySqlDbConnection()) { connection.Open(); DbCommand command = - GetDbCommand( - "SELECT" + currencyFieldSequence + "WHERE CurrencyId=" + currencyId + ";", connection); + GetDbCommand(String.Format ( + "SELECT CurrencyExchangeRateSnapshotData.CurrencyAId AS CurrencyAId, " + + " CurrencyExchangeRateSnapshotData.CurrencyBId AS CurrencyBId, " + + " CurrencyExchangeRateSnapshotData.APerB AS Rate, " + + " CurrencyExchangeRateSnapshots.DateTime AS RateTime " + + " FROM CurrencyExchangeRateSnapshotData " + + " JOIN CurrencyExchangeRateSnapshots USING (CurrencyExchangeRateSnapshotId)" + + " WHERE CurrencyAId = {0} AND CurrencyBId = {1} AND CurrencyExchangeRateSnapshots.DateTime < @valuationDateTime " + + " ORDER BY CurrencyExchangeRateSnapshots.DateTime DESC " + + " LIMIT 1;", fromCurrencyId, toCurrencyId), connection); + + AddParameterWithName (command, "valuationDateTime", valuationDateTime); using (DbDataReader reader = command.ExecuteReader()) { if (reader.Read()) { - return ReadCurrencyFromDataReader(reader); + DateTime testDate = reader.GetDateTime (3); + + return reader.GetDouble(2); } - throw new ArgumentException("Unknown Currency Id: " + currencyId); + throw new ArgumentException("Unknown Exchange Rate"); } } } + + /* public BasicCurrency GetCurrency(string currencyCode) { diff --git a/Logic/Financial/Currency.cs b/Logic/Financial/Currency.cs index f5312a6b..1033cff0 100644 --- a/Logic/Financial/Currency.cs +++ b/Logic/Financial/Currency.cs @@ -1,3 +1,5 @@ +using System; +using NBitcoin; using Swarmops.Basic.Types; using Swarmops.Database; @@ -23,6 +25,50 @@ namespace Swarmops.Logic.Financial } } + public bool IsBitcoin + { + get { return (base.Code == "BTC"); } + } + + public static Currency Bitcoin + { + get { return Currency.FromCode ("BTC"); } + } + + + public double GetConversionRate (Currency otherCurrency) + { + return GetConversionRate (otherCurrency, DateTime.UtcNow); + } + + + public double GetConversionRate (Currency otherCurrency, DateTime valuationDateTime) + { + if (this.IsBitcoin) + { + return SwarmDb.GetDatabaseForReading() + .GetCurrencyExchangeRate (otherCurrency.Identity, this.Identity, valuationDateTime); + } + else if (otherCurrency.IsBitcoin) + { + return 1.0 / SwarmDb.GetDatabaseForReading() + .GetCurrencyExchangeRate (otherCurrency.Identity, this.Identity, valuationDateTime); + } + else + { + // Neither is bitcoin, so go via bitcoin + + int bitcoinCurrencyId = Currency.Bitcoin.Identity; + double thisPerBitcoin = SwarmDb.GetDatabaseForReading() + .GetCurrencyExchangeRate (this.Identity, bitcoinCurrencyId, valuationDateTime); + double otherPerBitcoin = SwarmDb.GetDatabaseForReading() + .GetCurrencyExchangeRate (otherCurrency.Identity, bitcoinCurrencyId, valuationDateTime); + + return otherPerBitcoin/thisPerBitcoin; // returns other-per-this, allowing multiplication with exchange rate + } + } + + public static Currency FromBasic (BasicCurrency basic) { return new Currency (basic); diff --git a/Logic/Financial/Money.cs b/Logic/Financial/Money.cs new file mode 100644 index 00000000..548715ac --- /dev/null +++ b/Logic/Financial/Money.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Swarmops.Logic.Financial +{ + public class Money + { + public Currency Currency { get; set; } + public Int64 Cents { get; set; } + public DateTime ValuationDateTime { get; set; } + + public Money ToCurrency (Currency newCurrency) + { + double conversionRate = this.Currency.GetConversionRate (newCurrency, this.ValuationDateTime); + + return new Money { + Currency = newCurrency, + Cents = (Int64) (this.Cents * conversionRate), + ValuationDateTime = this.ValuationDateTime }; + } + } +} diff --git a/Logic/Logic.csproj b/Logic/Logic.csproj index 95d2c649..47bf9b71 100644 --- a/Logic/Logic.csproj +++ b/Logic/Logic.csproj @@ -138,6 +138,7 @@ + diff --git a/Site5/Master-v5.master.cs b/Site5/Master-v5.master.cs index 2b977470..9d686710 100644 --- a/Site5/Master-v5.master.cs +++ b/Site5/Master-v5.master.cs @@ -2,11 +2,13 @@ using System; using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Runtime.Serialization; using System.Threading; using System.Web; using System.Web.Security; using System.Web.UI; using Resources; +using Swarmops.Logic.Financial; using Swarmops.Logic.Security; using Swarmops.Logic.Structure; using Swarmops.Logic.Support; @@ -44,6 +46,7 @@ namespace Swarmops _currentOrganization = Organization.Sandbox; } } + } protected void Page_Load (object sender, EventArgs e)