From 49bd0c7416e067ca3f760ec0fb6a247138d5a19a Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Thu, 8 Oct 2015 01:03:50 +0200 Subject: [PATCH] Db0029; some fixing --- Database/Configuration.cs | 2 +- Database/Schemata/revisions.txt | 3 +- Database/Schemata/upgrade-0029.sql | 56 ++++++++++++++++++++++++++++++ Logic/Financial/BitcoinUtility.cs | 6 ++-- 4 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 Database/Schemata/upgrade-0029.sql diff --git a/Database/Configuration.cs b/Database/Configuration.cs index cd97122f..e5d37f07 100644 --- a/Database/Configuration.cs +++ b/Database/Configuration.cs @@ -14,7 +14,7 @@ namespace Swarmops.Database /// public static int DbVersionExpected { - get { return 28; } + get { return 29; } } diff --git a/Database/Schemata/revisions.txt b/Database/Schemata/revisions.txt index efe4cdcd..4a71c856 100644 --- a/Database/Schemata/revisions.txt +++ b/Database/Schemata/revisions.txt @@ -25,4 +25,5 @@ 0025 - Bugfix for 0024 [RF] 0026 - Added first tables, procs for hot bitcoin wallets [RF] 0027 - Fixes and additions to HotBitcoinAddresses, -Unspents [RF] -0028 - Bugfix for 0027 [RF] \ No newline at end of file +0028 - Bugfix for 0027 [RF] +0029 - Added unspent satoshis to hotaddress on create unspent [RF] \ No newline at end of file diff --git a/Database/Schemata/upgrade-0029.sql b/Database/Schemata/upgrade-0029.sql new file mode 100644 index 00000000..48d6ce28 --- /dev/null +++ b/Database/Schemata/upgrade-0029.sql @@ -0,0 +1,56 @@ +DROP procedure IF EXISTS `CreateHotBitcoinAddressUnspentConditional` + + +# + + +CREATE PROCEDURE `CreateHotBitcoinAddressUnspentConditional` ( + IN hotBitcoinAddressId INT, + 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 + (HotBitcoinAddressId,AmountSatoshis,TransactionHash,TransactionOutputIndex,ConfirmationCount) + VALUES + (hotBitcoinAddressId,amountSatoshis,transactionHash,transactionOutputIndex,confirmationCount); + + SELECT LAST_INSERT_ID() INTO unspentId; + + ELSE + + SELECT HotBitcoinAddressUnspents.HotBitcoinAddressUnspentId INTO unspentId + FROM HotBitcoinAddressUnspents + WHERE HotBitcoinAddressUnspents.TransactionHash=transactionHash + AND HotBitcoinAddressUnspents.TransactionOutputIndex=transactionOutputIndex; + + UPDATE HotBitcoinAddressUnspents + SET HotBitcoinAddressUnspents.ConfirmationCount=confirmationCount + WHERE HotBitcoinAddressUnspents.HotBitcoinAddressUnspentId=unspentId; + + END IF; + + UPDATE HotBitcoinAddresses + INNER JOIN + ( + SELECT HotBitcoinAddressId, SUM(AmountSatoshis) AS UnspentAmountSum + FROM HotBitcoinAddressUnspents + WHERE HotBitcoinAddressUnspents.HotBitcoinAddressId=hotBitcoinAddressId + ) innerSelection ON HotBitcoinAddresses.HotBitcoinAddressId = + innerSelection.HotBitcoinAddressId + SET HotBitcoinAddresses.BalanceSatoshis = innerSelection.UnspentAmountSum + WHERE HotBitcoinAddresses.HotBitcoinAddressId=hotBitcoinAddressId; + + SELECT unspentId AS Identity; + +END diff --git a/Logic/Financial/BitcoinUtility.cs b/Logic/Financial/BitcoinUtility.cs index dbcc0222..379f231a 100644 --- a/Logic/Financial/BitcoinUtility.cs +++ b/Logic/Financial/BitcoinUtility.cs @@ -23,7 +23,7 @@ namespace Swarmops.Logic.Financial string addressString = secretKey.PubKey.GetAddress (Network.Main).ToString(); var unspentJsonResult = - JObject.Parse (new WebClient().DownloadString ("https://blockchain.info/unspent?active=" + addressString)); + JObject.Parse(new WebClient().DownloadString("https://blockchain.info/unspent?active=" + addressString + "&api_key=I_am_testing_the_format_please_ignore")); List coinList = new List(); @@ -53,7 +53,7 @@ namespace Swarmops.Logic.Financial var addressInfoResult = JObject.Parse ( new WebClient().DownloadString ( - "https://blockchain.info/address/" + address + "?format=json")); + "https://blockchain.info/address/" + address + "?format=json&api_key=I_am_testing_the_format_please_ignore")); if ((int) addressInfoResult["final_balance"] == 0) { @@ -61,7 +61,7 @@ namespace Swarmops.Logic.Financial } var unspentJsonResult = - JObject.Parse(new WebClient().DownloadString("https://blockchain.info/unspent?active=" + address)); // warn: will 500 if no unspent outpoints + JObject.Parse(new WebClient().DownloadString("https://blockchain.info/unspent?active=" + address + "&api_key=I_am_testing_the_format_please_ignore")); // warn: will 500 if no unspent outpoints foreach (var unspentJson in unspentJsonResult["unspent_outputs"]) {