From 960d1fd6e4391c3d37069675116ed8b898339a8b Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Mon, 12 Oct 2015 12:08:42 +0200 Subject: [PATCH] More work on hot payouts - doing dryruns --- .../Transmission/NotificationPayload.cs | 7 +- Logic/Financial/Payouts.cs | 67 +++++++++++++++---- ...ansmission.NotificationPayload.Designer.cs | 26 ++++++- ...ions.Transmission.NotificationPayload.resx | 14 +++- Site5/Pages/v5/Financial/Donate.aspx.cs | 2 +- 5 files changed, 96 insertions(+), 20 deletions(-) diff --git a/Logic/Communications/Transmission/NotificationPayload.cs b/Logic/Communications/Transmission/NotificationPayload.cs index 5353a763..d1514f8d 100644 --- a/Logic/Communications/Transmission/NotificationPayload.cs +++ b/Logic/Communications/Transmission/NotificationPayload.cs @@ -165,7 +165,12 @@ namespace Swarmops.Logic.Communications.Transmission Bitcoin_PaidOut, Bitcoin_Shortage, Bitcoin_Shortage_Urgent, - Bitcoin_Shortage_Critical + Bitcoin_Shortage_Critical, + BitcoinPayoutAddress_Set, + BitcoinPayoutAddress_Bad, + BitcoinPayoutAddress_PleaseSet, + BitcoinPayoutAddress_OfficerNotify, + BitcoinPayoutAddress_Changed } // ReSharper restore InconsistentNaming diff --git a/Logic/Financial/Payouts.cs b/Logic/Financial/Payouts.cs index b4838194..612637b7 100644 --- a/Logic/Financial/Payouts.cs +++ b/Logic/Financial/Payouts.cs @@ -475,6 +475,8 @@ namespace Swarmops.Logic.Financial { // Perform all waiting hot payouts for all orgs in the installation + DateTime utcNow = DateTime.UtcNow; + foreach (Organization organization in Organizations.GetAll()) { // If this org doesn't do hotwallet, continue @@ -485,19 +487,35 @@ namespace Swarmops.Logic.Financial Payouts orgPayouts = Payouts.Construct (organization); Payouts bitcoinPayouts = new Payouts(); - Dictionary satoshiPayoutLookup = new Dictionary(); - Dictionary satoshiPersonLookup = new Dictionary(); + Dictionary satoshiPayoutLookup = new Dictionary(); + Dictionary nativeCentsPayoutLookup = new Dictionary(); + Dictionary satoshiPersonLookup = new Dictionary(); Dictionary nativeCentsPersonLookup = new Dictionary(); Int64 satoshisTotal = 0; + string currencyCode = organization.Currency.Code; + // For each ready payout that can automate, add an output to a constructed transaction TransactionBuilder txBuilder = new TransactionBuilder(); foreach (Payout payout in orgPayouts) { + if (payout.ExpectedTransactionDate < utcNow) + { + continue; // payout is not due yet + } + if (payout.RecipientPerson != null && payout.RecipientPerson.BitcoinPayoutAddress.Length > 2 && payout.Account.Length < 4) { + // If the payout address is still in quarantine, don't pay out yet + + string addressSetTime = payout.RecipientPerson.BitcoinPayoutAddressTimeSet; + if (addressSetTime.Length > 4 && DateTime.Parse (addressSetTime, CultureInfo.InvariantCulture).AddHours (48) < utcNow) + { + continue; // still in quarantine + } + // Test the payout address - is it valid and can we handle it? try @@ -506,15 +524,31 @@ namespace Swarmops.Logic.Financial } catch (Exception) { - // TODO: Send notification of invalid address, if we're on the whole hour AND the whole day + // Notify person that address is invalid, then clear it + + NotificationStrings primaryStrings = new NotificationStrings(); + NotificationCustomStrings secondaryStrings = new NotificationCustomStrings(); + primaryStrings[NotificationString.OrganizationName] = organization.Name; + secondaryStrings["BitcoinAddress"] = payout.RecipientPerson.BitcoinPayoutAddress; + + OutboundComm.CreateNotification(organization, NotificationResource.BitcoinPayoutAddress_Bad, + primaryStrings, secondaryStrings, + People.FromSingle(payout.RecipientPerson)); + OutboundComm.CreateNotification(organization, NotificationResource.BitcoinPayoutAddress_Bad, + primaryStrings, secondaryStrings, + People.FromSingle(Person.FromIdentity (1))); // temporary copy to 1 to see what the notification looks like + + payout.RecipientPerson.BitcoinPayoutAddress = string.Empty; + continue; // do not add this payout } - int recipientPersonId = payout.RecipientPerson.Identity; - + // Ok, so it seems we're making this payout at this time. bitcoinPayouts.Add (payout); + int recipientPersonId = payout.RecipientPerson.Identity; + if (!satoshiPersonLookup.ContainsKey (recipientPersonId)) { satoshiPersonLookup[recipientPersonId] = 0; @@ -528,6 +562,7 @@ namespace Swarmops.Logic.Financial if (organization.Currency.IsBitcoin) { satoshiPayoutLookup[payout.ProtoIdentity] = payout.AmountCents; + nativeCentsPayoutLookup[payout.ProtoIdentity] = payout.AmountCents; satoshisTotal += payout.AmountCents; satoshiPersonLookup[recipientPersonId] += payout.AmountCents; } @@ -537,6 +572,7 @@ namespace Swarmops.Logic.Financial Money payoutAmount = new Money(payout.AmountCents, organization.Currency); Int64 satoshis = payoutAmount.ToCurrency(Currency.Bitcoin).Cents; satoshiPayoutLookup[payout.ProtoIdentity] = satoshis; + nativeCentsPayoutLookup[payout.ProtoIdentity] = payout.AmountCents; satoshisTotal += satoshis; satoshiPersonLookup[recipientPersonId] += satoshis; } @@ -598,17 +634,20 @@ namespace Swarmops.Logic.Financial // Add outputs and prepare notifications Int64 satoshisUsed = 0; - Dictionary> notificationLookup = new Dictionary>(); + Dictionary> notificationSpecLookup = new Dictionary>(); + Dictionary> notificationAmountLookup = new Dictionary>(); Payout masterPayout = Payout.Empty; foreach (Payout payout in bitcoinPayouts) { int recipientPersonId = payout.RecipientPerson.Identity; - if (!notificationLookup.ContainsKey (recipientPersonId)) + if (!notificationSpecLookup.ContainsKey (recipientPersonId)) { - notificationLookup[recipientPersonId] = new List(); + notificationSpecLookup[recipientPersonId] = new List(); + notificationAmountLookup[recipientPersonId] = new List(); } - notificationLookup[recipientPersonId].Add (payout.Specification); + notificationSpecLookup[recipientPersonId].Add (payout.Specification); + notificationAmountLookup[recipientPersonId].Add (payout.AmountCents); txBuilder = txBuilder.Send(new BitcoinAddress(payout.RecipientPerson.BitcoinPayoutAddress), new Satoshis(satoshiPayoutLookup[payout.ProtoIdentity])); @@ -658,16 +697,18 @@ namespace Swarmops.Logic.Financial // Send notifications - foreach (int personId in notificationLookup.Keys) + foreach (int personId in notificationSpecLookup.Keys) { Person person = Person.FromIdentity (personId); string spec = string.Empty; - foreach (string specPart in notificationLookup[personId]) + for (int index = 0; index < notificationSpecLookup[personId].Count; index++) { - spec += " * " + specPart + "\r\n"; + spec += String.Format(" * {0,-30} {1,14:N2} {2:-4}\r\n", notificationSpecLookup[personId][index], (notificationAmountLookup[personId][index]/100.0).ToString("N2"), currencyCode); } + spec = spec.TrimEnd(); + NotificationStrings primaryStrings = new NotificationStrings(); NotificationCustomStrings secondaryStrings = new NotificationCustomStrings(); @@ -682,6 +723,8 @@ namespace Swarmops.Logic.Financial secondaryStrings, People.FromSingle (Person.FromIdentity (1))); } + // Create the master payout from its prototype + // Ledger entries // TODO diff --git a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs index 2d972ac7..aa1f3533 100644 --- a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs +++ b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs @@ -61,11 +61,11 @@ namespace Swarmops.Logic.Resources { } /// - /// Looks up a localized string similar to [BitcoinAmountFloat] microcoin ("bits") was just transferred to your bitcoin address ([BitcoinAddress]) by [OrganizationName] for the following: + /// Looks up a localized string similar to [BitcoinAmountFloat] microbitcoin ("bits") was just transferred to your bitcoin address ([BitcoinAddress]) by [OrganizationName] for the following: /// ///[EmbeddedPreformattedText] /// - ///You should already see the transaction pending confirmations and it should achieve spendability within about an hour (six normal confirmations).. + ///You should already see the transaction in your wallet and it should achieve spendability within about an hour (six normal confirmations).. /// internal static string Bitcoin_PaidOut_Body { get { @@ -74,7 +74,7 @@ namespace Swarmops.Logic.Resources { } /// - /// Looks up a localized string similar to [[OrganizationName]] Funds paid using bitcoin: [CurrencyCode] [AmountFloat]. + /// Looks up a localized string similar to [[OrganizationName]] Funds paid to you: [CurrencyCode] [AmountFloat]. /// internal static string Bitcoin_PaidOut_Subject { get { @@ -104,6 +104,26 @@ namespace Swarmops.Logic.Resources { } } + /// + /// Looks up a localized string similar to On attempting a payout to your bitcoin payout address, the address ([BitcoinAddress]) was rejected by the underlying payout engine as invalid. It has therefore been cleared from your profile. + /// + ///Please log on to [InstallationName] at [ExternalUrl] and set a new bitcoin payout address.. + /// + internal static string BitcoinPayoutAddress_Bad_Body { + get { + return ResourceManager.GetString("BitcoinPayoutAddress_Bad_Body", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to [[OrganizationName] - ACTION ITEM] Bad bitcoin payout address. + /// + internal static string BitcoinPayoutAddress_Bad_Subject { + get { + return ResourceManager.GetString("BitcoinPayoutAddress_Bad_Subject", resourceCulture); + } + } + /// /// Looks up a localized string similar to You have changed your bitcoin payout address from [BitcoinOldAddress] to [BitcoinAddress]. As a security precaution, all payouts are quarantied for the next 48 hours. /// diff --git a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx index fe0d81b4..9dc29120 100644 --- a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx +++ b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx @@ -117,6 +117,14 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + On attempting a payout to your bitcoin payout address, the address ([BitcoinAddress]) was rejected by the underlying payout engine as invalid. It has therefore been cleared from your profile. + +Please log on to [InstallationName] at [ExternalUrl] and set a new bitcoin payout address. + + + [[OrganizationName] - ACTION ITEM] Bad bitcoin payout address + You have changed your bitcoin payout address from [BitcoinOldAddress] to [BitcoinAddress]. As a security precaution, all payouts are quarantied for the next 48 hours. @@ -150,14 +158,14 @@ If you did not set this payout address (it may be worth double checking the addr [[OrganizationName]] Bitcoin payout address set - [BitcoinAmountFloat] microcoin ("bits") was just transferred to your bitcoin address ([BitcoinAddress]) by [OrganizationName] for the following: + [BitcoinAmountFloat] microbitcoin ("bits") was just transferred to your bitcoin address ([BitcoinAddress]) by [OrganizationName] for the following: [EmbeddedPreformattedText] -You should already see the transaction pending confirmations and it should achieve spendability within about an hour (six normal confirmations). +You should already see the transaction in your wallet and it should achieve spendability within about an hour (six normal confirmations). - [[OrganizationName]] Funds paid using bitcoin: [CurrencyCode] [AmountFloat] + [[OrganizationName]] Funds paid to you: [CurrencyCode] [AmountFloat] Swarmops is trying to make payments of [CurrencyCode] [AmountNeededFloat], but only [CurrencyCode] [AmountWalletFloat] is available in the bitcoin hotwallet. Swarmops is unable to make further payments at this time. diff --git a/Site5/Pages/v5/Financial/Donate.aspx.cs b/Site5/Pages/v5/Financial/Donate.aspx.cs index 61a90fa0..0375f22f 100644 --- a/Site5/Pages/v5/Financial/Donate.aspx.cs +++ b/Site5/Pages/v5/Financial/Donate.aspx.cs @@ -24,7 +24,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial this.PageAccessRequired = new Access (this.CurrentOrganization, AccessAspect.Participant); // TEMP - //Payouts.PerformAutomated(); + Payouts.PerformAutomated(); /* TEMP TEMP TEMP - REMOVE THIS CODE */ /*