From 19b9e2d555d972ff2d692348ec705a07890c2420 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Mon, 12 Oct 2015 02:51:57 +0200 Subject: [PATCH] Work on hotwallets and payouts, dryrun ready --- .../Transmission/NotificationPayload.cs | 1 + Logic/Financial/Payouts.cs | 68 +++++++++++++++++-- ...ansmission.NotificationPayload.Designer.cs | 4 +- ...ions.Transmission.NotificationPayload.resx | 4 +- 4 files changed, 68 insertions(+), 9 deletions(-) diff --git a/Logic/Communications/Transmission/NotificationPayload.cs b/Logic/Communications/Transmission/NotificationPayload.cs index 7df736a0..5353a763 100644 --- a/Logic/Communications/Transmission/NotificationPayload.cs +++ b/Logic/Communications/Transmission/NotificationPayload.cs @@ -162,6 +162,7 @@ namespace Swarmops.Logic.Communications.Transmission Participant_Renewed, Participant_Terminated, Participant_Churned, + Bitcoin_PaidOut, Bitcoin_Shortage, Bitcoin_Shortage_Urgent, Bitcoin_Shortage_Critical diff --git a/Logic/Financial/Payouts.cs b/Logic/Financial/Payouts.cs index 0ef15d26..b4838194 100644 --- a/Logic/Financial/Payouts.cs +++ b/Logic/Financial/Payouts.cs @@ -485,7 +485,9 @@ namespace Swarmops.Logic.Financial Payouts orgPayouts = Payouts.Construct (organization); Payouts bitcoinPayouts = new Payouts(); - Dictionary satoshiLookup = new Dictionary(); + Dictionary satoshiPayoutLookup = new Dictionary(); + Dictionary satoshiPersonLookup = new Dictionary(); + Dictionary nativeCentsPersonLookup = new Dictionary(); Int64 satoshisTotal = 0; // For each ready payout that can automate, add an output to a constructed transaction @@ -508,23 +510,35 @@ namespace Swarmops.Logic.Financial continue; // do not add this payout } + int recipientPersonId = payout.RecipientPerson.Identity; + bitcoinPayouts.Add (payout); + if (!satoshiPersonLookup.ContainsKey (recipientPersonId)) + { + satoshiPersonLookup[recipientPersonId] = 0; + nativeCentsPersonLookup[recipientPersonId] = 0; + } + + nativeCentsPersonLookup[recipientPersonId] += payout.AmountCents; + // Find the amount of satoshis for this payout if (organization.Currency.IsBitcoin) { - satoshiLookup[payout.ProtoIdentity] = payout.AmountCents; + satoshiPayoutLookup[payout.ProtoIdentity] = payout.AmountCents; satoshisTotal += payout.AmountCents; + satoshiPersonLookup[recipientPersonId] += payout.AmountCents; } else { // Convert currency Money payoutAmount = new Money(payout.AmountCents, organization.Currency); Int64 satoshis = payoutAmount.ToCurrency(Currency.Bitcoin).Cents; - satoshiLookup[payout.ProtoIdentity] = satoshis; + satoshiPayoutLookup[payout.ProtoIdentity] = satoshis; satoshisTotal += satoshis; + satoshiPersonLookup[recipientPersonId] += satoshis; } } } @@ -597,8 +611,8 @@ namespace Swarmops.Logic.Financial notificationLookup[recipientPersonId].Add (payout.Specification); txBuilder = txBuilder.Send(new BitcoinAddress(payout.RecipientPerson.BitcoinPayoutAddress), - new Satoshis(satoshiLookup[payout.ProtoIdentity])); - satoshisUsed += satoshiLookup[payout.ProtoIdentity]; + new Satoshis(satoshiPayoutLookup[payout.ProtoIdentity])); + satoshisUsed += satoshiPayoutLookup[payout.ProtoIdentity]; payout.MigrateDependenciesTo (masterPayout); } @@ -619,14 +633,58 @@ namespace Swarmops.Logic.Financial // Sign transaction - ready to execute + Transaction txReady = txBuilder.BuildTransaction (true); + + // Verify that transaction is ready + + if (!txBuilder.Verify (txReady)) + { + throw new InvalidOperationException("Transaction is not signed enough"); + } + // Broadcast transaction + // TODO + // Delete inputs, adjust balance for addresses + // TODO + // inputs.DeleteAll(); + + // Register new balance of change address, should have increased by satoshisInput-satoshisUsed + + // TODO + // BitcoinUtility.CheckSomething (HotAddress.OrganizationWallet(organization)... + // Send notifications + foreach (int personId in notificationLookup.Keys) + { + Person person = Person.FromIdentity (personId); + + string spec = string.Empty; + foreach (string specPart in notificationLookup[personId]) + { + spec += " * " + specPart + "\r\n"; + } + + NotificationStrings primaryStrings = new NotificationStrings(); + NotificationCustomStrings secondaryStrings = new NotificationCustomStrings(); + + primaryStrings[NotificationString.OrganizationName] = organization.Name; + primaryStrings[NotificationString.CurrencyCode] = organization.Currency.DisplayCode; + primaryStrings[NotificationString.EmbeddedPreformattedText] = spec; + secondaryStrings["AmountFloat"] = (nativeCentsPersonLookup[personId]/100.0).ToString ("N2"); + secondaryStrings["BitcoinAmountFloat"] = (satoshiPersonLookup[personId]/100.0).ToString ("N2"); + secondaryStrings["BitcoinAddress"] = person.BitcoinPayoutAddress; // potential rare race condition + + OutboundComm.CreateNotification (organization, NotificationResource.Bitcoin_PaidOut, primaryStrings, + secondaryStrings, People.FromSingle (Person.FromIdentity (1))); + } + // Ledger entries + // TODO } } } diff --git a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs index c63156d5..2d972ac7 100644 --- a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs +++ b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs @@ -61,9 +61,9 @@ namespace Swarmops.Logic.Resources { } /// - /// Looks up a localized string similar to [BitcoinAmount] microcoin ("bits") was just transferred to your bitcoin address ([BitcoinAddress]) by [OrganizationName] for the following: + /// Looks up a localized string similar to [BitcoinAmountFloat] microcoin ("bits") was just transferred to your bitcoin address ([BitcoinAddress]) by [OrganizationName] for the following: /// - ///[PreformattedText] + ///[EmbeddedPreformattedText] /// ///You should already see the transaction pending confirmations and it should achieve spendability within about an hour (six normal confirmations).. /// diff --git a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx index 9be7ed28..fe0d81b4 100644 --- a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx +++ b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx @@ -150,9 +150,9 @@ If you did not set this payout address (it may be worth double checking the addr [[OrganizationName]] Bitcoin payout address set - [BitcoinAmount] microcoin ("bits") was just transferred to your bitcoin address ([BitcoinAddress]) by [OrganizationName] for the following: + [BitcoinAmountFloat] microcoin ("bits") was just transferred to your bitcoin address ([BitcoinAddress]) by [OrganizationName] for the following: -[PreformattedText] +[EmbeddedPreformattedText] You should already see the transaction pending confirmations and it should achieve spendability within about an hour (six normal confirmations).