diff --git a/Basic/Types/Financial/BasicHotBitcoinAddress.cs b/Basic/Types/Financial/BasicHotBitcoinAddress.cs index 42b97147..a9141321 100644 --- a/Basic/Types/Financial/BasicHotBitcoinAddress.cs +++ b/Basic/Types/Financial/BasicHotBitcoinAddress.cs @@ -10,18 +10,19 @@ namespace Swarmops.Basic.Types.Financial public class BasicHotBitcoinAddress: IHasIdentity { public BasicHotBitcoinAddress (int hotBitcoinAddressId, int organizationId, string derivationPath, - Int64 balanceSatoshis, Int64 throughputSatoshis) + string address, Int64 balanceSatoshis, Int64 throughputSatoshis) { this.HotBitcoinAddressId = hotBitcoinAddressId; this.OrganizationId = organizationId; this.DerivationPath = derivationPath; + this.Address = address; this.BalanceSatoshis = balanceSatoshis; this.ThroughputSatoshis = throughputSatoshis; } public BasicHotBitcoinAddress (BasicHotBitcoinAddress original) : this ( - original.HotBitcoinAddressId, original.OrganizationId, original.DerivationPath, original.BalanceSatoshis, + original.HotBitcoinAddressId, original.OrganizationId, original.DerivationPath, original.Address, original.BalanceSatoshis, original.ThroughputSatoshis) { // copy ctor @@ -30,6 +31,7 @@ namespace Swarmops.Basic.Types.Financial public int HotBitcoinAddressId { get; private set; } public int OrganizationId { get; private set; } public string DerivationPath { get; private set; } + public string Address { get; private set; } public Int64 BalanceSatoshis { get; protected set; } public Int64 ThroughputSatoshis { get; protected set; } diff --git a/Database/Configuration.cs b/Database/Configuration.cs index 0c0e553b..80bde133 100644 --- a/Database/Configuration.cs +++ b/Database/Configuration.cs @@ -14,7 +14,7 @@ namespace Swarmops.Database /// public static int DbVersionExpected { - get { return 26; } + get { return 27; } } diff --git a/Database/Database-HotBitcoinAddresses.cs b/Database/Database-HotBitcoinAddresses.cs new file mode 100644 index 00000000..83accce2 --- /dev/null +++ b/Database/Database-HotBitcoinAddresses.cs @@ -0,0 +1,172 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Common; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Swarmops.Basic.Types.Financial; +using Swarmops.Basic.Types.Governance; + +namespace Swarmops.Database +{ + public partial class SwarmDb + { + #region Field reading code + + private const string hotBitcoinAddressFieldSequence = + " HotBitcoinAddressId,OrganizationId,DerivationPath,AddressString,BalanceSatoshis," + // 0-4 + " ThroughputSatoshis " + // 5 + " FROM HotBitcoinAddresses "; + + private const string hotBitcoinAddressUnspentFieldSequence = + " HotBitcoinAddressUnspentId,TransactionHash,TransactionOutputIndex,AmountSatoshis,ConfirmationCount" + // 0-4 + " FROM HotBitcoinAddressUnspents "; + + private BasicHotBitcoinAddress ReadHotBitcoinAddressFromDataReader(IDataRecord reader) + { + int hotBitcoinAddressId = reader.GetInt32 (0); + int organizationId = reader.GetInt32 (1); + string derivationPath = reader.GetString (2); + string addressString = reader.GetString (3); + Int64 balanceSatoshis = reader.GetInt64 (4); + Int64 throughputSatoshis = reader.GetInt64 (5); + + return new BasicHotBitcoinAddress (hotBitcoinAddressId, organizationId, derivationPath, addressString, balanceSatoshis, throughputSatoshis); + } + + private BasicHotBitcoinAddressUnspent ReadHotBitcoinAddressUnspentFromDataReader(IDataRecord reader) + { + int hotBitcoinAddressUnspentId = reader.GetInt32 (0); + string transactionHash = reader.GetString (1); + int transactionOutputIndex = reader.GetInt32 (2); + Int64 amountSatoshis = reader.GetInt64 (3); + int confirmationCount = reader.GetInt32 (4); + + return new BasicHotBitcoinAddressUnspent (hotBitcoinAddressUnspentId, transactionHash, transactionOutputIndex, amountSatoshis, confirmationCount); + } + + #endregion + + #region Record reading code + + public BasicHotBitcoinAddress GetHotBitcoinAddress (int hotBitcoinAddressId) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand( + "SELECT" + hotBitcoinAddressFieldSequence + "WHERE HotBitcoinAddressId=" + hotBitcoinAddressId + ";", connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + if (reader.Read()) + { + return ReadHotBitcoinAddressFromDataReader(reader); + } + + throw new ArgumentException("Unknown HotBitcoinAddress Id: " + hotBitcoinAddressId); + } + } + } + + public BasicHotBitcoinAddress[] GetHotBitcoinAddresses(params object[] conditions) // the typical condition would be an Organization, here + { + List result = new List(); + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand( + "SELECT" + hotBitcoinAddressFieldSequence + ConstructWhereClause("HotBitcoinAddresses", conditions), connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + while (reader.Read()) + { + result.Add(ReadHotBitcoinAddressFromDataReader(reader)); + } + + return result.ToArray(); + } + } + } + + + public BasicHotBitcoinAddressUnspent[] GetHotBitcoinAddressUnspents(params object[] conditions) // the typical condition would be a HotBitcoinAddress, here + { + List result = new List(); + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand( + "SELECT" + hotBitcoinAddressUnspentFieldSequence + ConstructWhereClause("HotBitcoinAddressUnspents", conditions), connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + while (reader.Read()) + { + result.Add(ReadHotBitcoinAddressUnspentFromDataReader(reader)); + } + + return result.ToArray(); + } + } + } + + + + #endregion + + #region Creation and manipulation + + public int CreateHotBitcoinAddressConditional(int organizationId, string derivationPath, string address) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + using (DbCommand command = GetDbCommand("CreateHotBitcoinAddressConditional", connection)) + { + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "organizationId", organizationId); + AddParameterWithName(command, "derivationPath", derivationPath); + AddParameterWithName(command, "addressString", address); + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + } + + public int CreateHotBitcoinAddressUnspentConditional(int hotBitcoinAddressId, string transactionHash, int transactionOutputIndex, Int64 amountSatoshis, int confirmationCount) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + using (DbCommand command = GetDbCommand("CreateHotBitcoinAddressUnspentConditional", connection)) + { + command.CommandType = CommandType.StoredProcedure; + + AddParameterWithName(command, "hotBitcoinAddressId", hotBitcoinAddressId); + AddParameterWithName(command, "transactionHash", transactionHash); + AddParameterWithName(command, "transactionOutputIndex", transactionOutputIndex); + AddParameterWithName(command, "amountSatoshis", amountSatoshis); + AddParameterWithName(command, "confirmationCount", confirmationCount); + + return Convert.ToInt32(command.ExecuteScalar()); + } + } + } + + #endregion + } +} diff --git a/Database/Database.csproj b/Database/Database.csproj index 64730017..394d4ca2 100644 --- a/Database/Database.csproj +++ b/Database/Database.csproj @@ -65,6 +65,7 @@ + diff --git a/Database/Schemata/revisions.txt b/Database/Schemata/revisions.txt index d8325ea3..66cb7c93 100644 --- a/Database/Schemata/revisions.txt +++ b/Database/Schemata/revisions.txt @@ -24,3 +24,4 @@ 0024 - Maint on CreatePayrollItem [RF] 0025 - Bugfix for 0024 [RF] 0026 - Added first tables, procs for hot bitcoin wallets [RF] +0027 - Fixes and additions to HotBitcoinAddresses, -Unspents [RF] \ No newline at end of file diff --git a/Database/Schemata/upgrade-0026.sql b/Database/Schemata/upgrade-0026.sql index df9f9828..1074c712 100644 --- a/Database/Schemata/upgrade-0026.sql +++ b/Database/Schemata/upgrade-0026.sql @@ -8,6 +8,7 @@ CREATE TABLE `HotBitcoinAddresses` ( PRIMARY KEY (`HotBitcoinAddressId`), INDEX `Ix_Organization` (`OrganizationId` ASC), INDEX `Ix_Balance` (`BalanceSatoshis` ASC), + INDEX `Ix_Address` (`AddressString` ASC), INDEX `Ix_Used` (`ThroughputSatoshis` ASC)) diff --git a/Database/Schemata/upgrade-0027.sql b/Database/Schemata/upgrade-0027.sql new file mode 100644 index 00000000..7b8bf67c --- /dev/null +++ b/Database/Schemata/upgrade-0027.sql @@ -0,0 +1,121 @@ +ALTER TABLE `HotBitcoinAddresses` +ADD UNIQUE INDEX `Ix_Address` (`AddressString` ASC); + + +# + + +ALTER TABLE `activizr-dev`.`HotBitcoinAddressUnspents` +ADD UNIQUE INDEX `Ix_HashIndex` (`TransactionHash` ASC, `TransactionOutputIndex` ASC); + + +# + + +DROP procedure IF EXISTS `CreateHotBitcoinAddressConditional` + + +# + + +CREATE PROCEDURE `CreateHotBitcoinAddressConditional` ( + IN organizationId INT, + IN derivationPath VARCHAR(256), + IN addressString VARCHAR(48) +) +BEGIN + + IF ((SELECT COUNT(*) FROM HotBitcoinAddresses WHERE HotBitcoinAddresses.AddressString=addressString) = 0) + THEN + + INSERT INTO HotBitcoinAddresses + (OrganizationId,DerivationPath,AddressString,BalanceSatoshis,ThroughputSatoshis) + VALUES + (organizationId,derivationPath,addressString,0,0); + + SELECT LAST_INSERT_ID() AS Identity; + + ELSE + + SELECT HotBitcoinAddresses.HotBitcoinAddressId AS Identity + WHERE HotBitcoinAddresses.AddressString=addressString; + + END IF; + +END + + +# + + + +DROP procedure IF EXISTS `CreateHotBitcoinAddressUnspent` + + +# + + +CREATE PROCEDURE `CreateHotBitcoinAddressUnspent` ( + IN amountSatoshis BIGINT, + IN transactionHash VARCHAR(128), + IN transactionOutputIndex INT, + IN confirmationCount INT +) +BEGIN + INSERT INTO HotBitcoinAddressUnspents + (AmountSatoshis,TransactionHash,TransactionOutputIndex,ConfirmationCount) + VALUES + (amountSatoshis,transactionHash,transactionOutputIndex,confirmationCount); + + SELECT LAST_INSERT_ID() AS Identity; + +END + + +# + + +DROP procedure IF EXISTS `CreateHotBitcoinAddressUnspentConditional` + + +# + + +CREATE PROCEDURE `CreateHotBitcoinAddressUnspentConditional` ( + IN amountSatoshis BIGINT, + IN transactionHash VARCHAR(128), + IN transactionOutputIndex INT, + IN confirmationCount INT +) +BEGIN + + DECLARE unspentId INT; + + IF ((SELECT COUNT(*) FROM HotBitcoinAddressUnspents + WHERE HotBitcoinAddressUnspents.TransactionHash=transactionHash + AND HotBitcoinAddressUnspents.TransactionOutputIndex=transactionOutputIndex) = 0) + THEN + + INSERT INTO HotBitcoinAddressUnspents + (AmountSatoshis,TransactionHash,TransactionOutputIndex,ConfirmationCount) + VALUES + (amountSatoshis,transactionHash,transactionOutputIndex,confirmationCount); + + SELECT LAST_INSERT_ID() AS Identity; + + ELSE + + SELECT HotBitcionAddressUnspents.HotBitcoinAddressUnspentId INTO unspentId + FROM HotBitcoinAddressUnspents + WHERE HotBitcoinAddressUnspents.TransactionHash=transactionHash + AND HotBitcoinAddressUnspents.TransactionOutputIndex=transactionOutputIndex; + + UPDATE HotBitcoinAddressUnspents + SET HotBitcoinAddressUnspents.ConfirmationCount=confirmationCount + WHERE HotBitcoinAddressUnspents.HotBitcoinAddressUnspentId=unspentId; + + SELECT unspentId AS Identity; + + END IF; + +END diff --git a/Logic/Financial/HotBitcoinAddresses.cs b/Logic/Financial/HotBitcoinAddresses.cs new file mode 100644 index 00000000..e6dd771e --- /dev/null +++ b/Logic/Financial/HotBitcoinAddresses.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Swarmops.Basic.Types.Financial; +using Swarmops.Logic.Support; + +namespace Swarmops.Logic.Financial +{ + public class HotBitcoinAddresses: PluralBase + { + // to be implemented + } +} diff --git a/Logic/Logic.csproj b/Logic/Logic.csproj index 2574fe02..f992995e 100644 --- a/Logic/Logic.csproj +++ b/Logic/Logic.csproj @@ -120,6 +120,7 @@ +