diff --git a/Common/Common.csproj b/Common/Common.csproj index 38bb40ce..ea604378 100644 --- a/Common/Common.csproj +++ b/Common/Common.csproj @@ -51,6 +51,7 @@ + diff --git a/Common/Enums/FinancialForeignIdType.cs b/Common/Enums/FinancialForeignIdType.cs new file mode 100644 index 00000000..5dab40fb --- /dev/null +++ b/Common/Enums/FinancialForeignIdType.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Swarmops.Common.Enums +{ + public enum FinancialForeignIdType + { + Unknown = 0, + BitcoinTransaction = 1 + } +} diff --git a/Database/Configuration.cs b/Database/Configuration.cs index 051e5272..620c7603 100644 --- a/Database/Configuration.cs +++ b/Database/Configuration.cs @@ -14,7 +14,7 @@ namespace Swarmops.Database /// public static int DbVersionExpected { - get { return 31; } + get { return 32; } } diff --git a/Database/Database-Financials.cs b/Database/Database-Financials.cs index f8b2d1d9..fe40226b 100644 --- a/Database/Database-Financials.cs +++ b/Database/Database-Financials.cs @@ -614,10 +614,10 @@ namespace Swarmops.Database { connection.Open(); - DbCommand command = + using (DbCommand command = GetDbCommand ( "select AmountCents from FinancialAccountBudgetsMonthly where Year=" + year + - " AND FinancialAccountId=" + financialAccountId + " ORDER BY Month;", connection); + " AND FinancialAccountId=" + financialAccountId + " ORDER BY Month;", connection)) using (DbDataReader reader = command.ExecuteReader()) { @@ -1042,6 +1042,50 @@ namespace Swarmops.Database } + public void SetFinancialTransactionForeignId(int financialTransactionId, FinancialForeignIdType foreignIdType, string foreignId) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SetFinancialTransactionForeignId", connection); + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "financialTransactionId", financialTransactionId); + AddParameterWithName(command, "financialTransactionForeignIdType", (int) foreignIdType); + AddParameterWithName(command, "foreignId", foreignId); + + command.ExecuteNonQuery(); + } + } + + + + public Int64 GetFinancialTransactionFromForeignId(FinancialForeignIdType foreignIdType, string foreignId) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand( + "Select FinancialTransactionId from FinancialTransactionForeignIds WHERE FinancialTransactionForeignIdType=" + (int) foreignIdType + + " AND ForeignId='" + SqlSanitize (foreignId) + "';", connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + if (reader.Read()) + { + return reader.GetInt32 (0); + } + + return 0; // none found. Todo: throw exception instead? This behavior is inconsistent + } + } + } + + + public DateTime GetFinancialAccountFirstTransactionDate (int financialAccountId) { using (DbConnection connection = GetMySqlDbConnection()) diff --git a/Database/Schemata/revisions.txt b/Database/Schemata/revisions.txt index e1fb7777..8b4dcc26 100644 --- a/Database/Schemata/revisions.txt +++ b/Database/Schemata/revisions.txt @@ -28,4 +28,5 @@ 0028 - Bugfix for 0027 [RF] 0029 - Added unspent satoshis to hotaddress on create unspent [RF] 0030 - Added flexibility for Resolver classes for OutboundComms [RF] -0031 - Added foreign ID tables, procs for transactions, to track blockchain [RF] \ No newline at end of file +0031 - Added foreign ID tables, procs for transactions, to track blockchain [RF] +0032 - Added native currency to transaction rows, for multi-currency accounting [RF] \ No newline at end of file diff --git a/Database/Schemata/upgrade-0032.sql b/Database/Schemata/upgrade-0032.sql new file mode 100644 index 00000000..c9e4e178 --- /dev/null +++ b/Database/Schemata/upgrade-0032.sql @@ -0,0 +1,42 @@ +CREATE TABLE `FinancialTransactionRowsNativeCurrency` ( + `FinancialTransactionRowId` INT NOT NULL, + `CurrencyId` INT NOT NULL, + `NativeAmountCents` BIGINT NOT NULL, + PRIMARY KEY (`FinancialTransactionRowId`)) + + +# + + +DROP procedure IF EXISTS `SetFinancialTransactionRowNativeCurrency` + + +# + + + +CREATE PROCEDURE `SetFinancialTransactionRowNativeCurrency`( + IN financialTransactionRowId INT, + IN currencyId INT, + IN nativeAmountCents BIGINT +) +BEGIN + + IF ((SELECT COUNT(*) FROM FinancialTransactionRows + WHERE FinancialTransactionRows.FinancialTransactionRowId=financialTransactionRowId) = 0) + THEN + + INSERT INTO FinancialTransactionRowsNativeCurrency (FinancialTransactionRowId,CurrencyId,NativeAmountCents) + VALUES (financialTransactionRowId,currencyId,nativeAmountCents); + + ELSE + + UPDATE FinancialTransactionRowsNativeCurrency + SET FinancialTransactionRowsNativeCurrency.CurrencyId=currencyId, + FinancialTransactionRowsNativeCurrency.NativeAmountCents=nativeAmountCents + WHERE FinancialTransactionRowsNativeCurrency.FinancialTransactionRowId= + financialTransactionRowId; + + END IF; + +END diff --git a/Logic/Financial/BitcoinUtility.cs b/Logic/Financial/BitcoinUtility.cs index 59165549..9cec1e6f 100644 --- a/Logic/Financial/BitcoinUtility.cs +++ b/Logic/Financial/BitcoinUtility.cs @@ -53,8 +53,6 @@ namespace Swarmops.Logic.Financial } } } - - } public static ExtKey BitcoinHotPrivateRoot @@ -179,6 +177,57 @@ namespace Swarmops.Logic.Financial return addresses.FindAmount (satoshis); // Will throw on a number of circumstances } + + static public FinancialTransaction GetSwarmopsTransactionFromBlockchainId (string transactionId) + { + return null; + } + + + static public void CheckColdStorageForOrganization (Organization organization) + { + FinancialAccount coldRoot = organization.FinancialAccounts.AssetsBitcoinCold; + + if (coldRoot == null) + { + return; // no cold assets to check + } + } + + // https://blockchain.info/address/1Axd4QQhRSCMAuHe93uVKRjNSFj6WypcZa?format=json&api_key=I_am_testing_the_format_please_ignore + + static private void CheckColdStorageRecurse (FinancialAccount parent) + { + foreach (FinancialAccount child in parent.Children) + { + CheckColdStorageRecurse (child); + } + + // After recursing, get all transactions for this account and verify against our records + + // is the account name a valid bitcoin address on the main network? + + string address = parent.Name.Trim(); + + try + { + BitcoinAddress addressTest = new BitcoinAddress (BitcoinTestAddress, Network.Main); + } + catch (Exception e) + { + // the name wasn't an address, so return + } + + var addressData = + JObject.Parse( + new WebClient().DownloadString("https://blockchain.info/address/" + address + "?format=json&api_key=" + + SystemSettings.BlockchainSwarmopsApiKey)); // warn: will 500 if no unspent outpoints + + // TODO: PARSE (awaiting functions for native currency to be added to FinancialTransactionRows) + + } + + public const int BitcoinWalletIndex = 1; public const int BitcoinDonationsIndex = 2; public const int BitcoinLoansIndex = 3;