From 1a7c4aab3422deedfb8868b76fbfed3aeab725d6 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Thu, 7 Jan 2016 11:46:46 +0100 Subject: [PATCH] Fine tuned bitcoin transaction input selection --- Logic/Financial/Payouts.cs | 19 ++++++++++++++----- ...ansmission.NotificationPayload.Designer.cs | 2 +- ...ions.Transmission.NotificationPayload.resx | 2 +- Site5/Master-v5.master.cs | 1 + 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Logic/Financial/Payouts.cs b/Logic/Financial/Payouts.cs index cc4f4894..9d798e92 100644 --- a/Logic/Financial/Payouts.cs +++ b/Logic/Financial/Payouts.cs @@ -668,6 +668,7 @@ namespace Swarmops.Logic.Financial Dictionary> notificationSpecLookup = new Dictionary>(); Dictionary> notificationAmountLookup = new Dictionary>(); Payout masterPayoutPrototype = Payout.Empty; + HotBitcoinAddress changeAddress = HotBitcoinAddress.OrganizationWalletZero (organization); foreach (Payout payout in bitcoinPayouts) { @@ -689,7 +690,7 @@ namespace Swarmops.Logic.Financial // Set change address to wallet slush - txBuilder.SetChange (new BitcoinAddress (HotBitcoinAddress.OrganizationWalletZero (organization).Address)); + txBuilder.SetChange (new BitcoinAddress (changeAddress.Address)); // Add fee @@ -724,18 +725,26 @@ namespace Swarmops.Logic.Financial BitcoinUtility.BroadcastTransaction (txReady); + // Note the transaction hash + + string transactionHash = txReady.GetHash().ToString(); + // Delete all old inputs, adjust balance for addresses (re-register unused inputs) inputs.AsUnspents.DeleteAll(); - foreach (BitcoinTransactionInput input in inputs) + // Log the new unspent created by change (if there is any) + + if (satoshisInput - satoshisUsed > 0) { - BitcoinUtility.TestUnspents (input.BitcoinAddress); + SwarmDb.GetDatabaseForWriting() + .CreateHotBitcoinAddressUnspentConditional(changeAddress.Identity, transactionHash, + + /* the change address seems to always get index 0? is this a safe assumption? */ 0, satoshisInput - satoshisUsed, /* confirmation count*/ 0); } // Register new balance of change address, should have increased by (satoshisInput-satoshisUsed) - BitcoinUtility.TestUnspents (HotBitcoinAddress.OrganizationWalletZero (organization).Address); + // TODO // Send notifications @@ -788,7 +797,7 @@ namespace Swarmops.Logic.Financial FinancialTransaction ledgerTransaction = FinancialTransaction.Create (organization, utcNow, "Bitcoin automated payout"); ledgerTransaction.AddRow (organization.FinancialAccounts.AssetsBitcoinHot, -masterPayoutPrototype.AmountCents, null).NativeAmountCents = new Swarmops.Logic.Financial.Money(satoshisUsed, Currency.Bitcoin); - ledgerTransaction.BlockchainHash = txReady.GetHash().ToString(); // TODO: VERIFY THIS + ledgerTransaction.BlockchainHash = transactionHash; masterPayout.BindToTransactionAndClose (ledgerTransaction, null); } diff --git a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs index 25a9e49c..2706e4cc 100644 --- a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs +++ b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs @@ -61,7 +61,7 @@ namespace Swarmops.Logic.Resources { } /// - /// Looks up a localized string similar to The bitcoin hotwallet for [OrganizationName] just made [PaymentCount] automatic payments totalling [CurrencyCode] [AmountFloat], or expressed in bitcoin, [BitcoinAmountFloat] microbitcoins.. + /// Looks up a localized string similar to The bitcoin hotwallet for [OrganizationName] just made [PaymentCount] automatic payment(s) totalling [CurrencyCode] [AmountFloat] ([BitcoinAmountFloat] microbitcoin or "bits").. /// internal static string Bitcoin_Hotwallet_Outflow_Body { get { diff --git a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx index 760e33ac..ff926333 100644 --- a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx +++ b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx @@ -158,7 +158,7 @@ If you did not set this payout address (it may be worth double checking the addr [[OrganizationName]] Bitcoin payout address set - The bitcoin hotwallet for [OrganizationName] just made [PaymentCount] automatic payments totalling [CurrencyCode] [AmountFloat], or expressed in bitcoin, [BitcoinAmountFloat] microbitcoins. + The bitcoin hotwallet for [OrganizationName] just made [PaymentCount] automatic payment(s) totalling [CurrencyCode] [AmountFloat] ([BitcoinAmountFloat] microbitcoin or "bits"). [[OrganizationName]] Hotwallet Payout: [CurrencyCode] [AmountFloat] diff --git a/Site5/Master-v5.master.cs b/Site5/Master-v5.master.cs index cb84c736..0e5fac7e 100644 --- a/Site5/Master-v5.master.cs +++ b/Site5/Master-v5.master.cs @@ -41,6 +41,7 @@ namespace Swarmops // HotBitcoinAddresses testAddresses = HotBitcoinAddresses.ForOrganization (Organization.FromIdentity (2)); // testAddresses.UpdateBalances(); + // BitcoinUtility.TestUnspents ("19WEHvYWG5gvPf7X3USXwS7QdFfE1dGSQF"); // Payouts.PerformAutomated();