From 067a0121a2ffa74e5cdb0963bf14f3b0e33cb489 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sun, 11 Oct 2015 16:21:11 +0200 Subject: [PATCH] Work on hotwallet --- .../Transmission/NotificationPayload.cs | 25 +++++++--- Logic/Financial/Currency.cs | 2 +- Logic/Financial/HotBitcoinAddresses.cs | 8 +++- Logic/Financial/Payouts.cs | 48 ++++++++++++++++++- ...ansmission.NotificationPayload.Designer.cs | 22 +++++++++ ...ions.Transmission.NotificationPayload.resx | 10 ++++ Site5/Pages/v5/Financial/Donate.aspx.cs | 3 ++ 7 files changed, 108 insertions(+), 10 deletions(-) diff --git a/Logic/Communications/Transmission/NotificationPayload.cs b/Logic/Communications/Transmission/NotificationPayload.cs index 4ab6f043..7df736a0 100644 --- a/Logic/Communications/Transmission/NotificationPayload.cs +++ b/Logic/Communications/Transmission/NotificationPayload.cs @@ -76,16 +76,26 @@ namespace Swarmops.Logic.Communications.Transmission // Loop through supplied strings and replace them in the resource. Not very efficient but who cares - foreach (NotificationString notificationString in Strings.Keys) + if (Strings != null) { - // TODO: Check if string ends in Float, and if so, parse and culturize it + foreach (NotificationString notificationString in Strings.Keys) + { + // TODO: Check if string ends in Float, and if so, parse and culturize it - input = input.Replace ("[" + notificationString /* this is an enum converted to string */ + "]", Strings[notificationString]); + input = input.Replace ("[" + notificationString /* this is an enum converted to string */+ "]", + Strings[notificationString]); + } } - foreach (string notificationCustomString in CustomStrings.Keys) + if (CustomStrings != null) { - input = input.Replace ("[" + notificationCustomString /* this is a System.String as opposed to above */ + "]", CustomStrings[notificationCustomString]); + foreach (string notificationCustomString in CustomStrings.Keys) + { + input = + input.Replace ( + "[" + notificationCustomString /* this is a System.String as opposed to above */+ "]", + CustomStrings[notificationCustomString]); + } } return input; @@ -151,7 +161,10 @@ namespace Swarmops.Logic.Communications.Transmission Participant_Volunteer, Participant_Renewed, Participant_Terminated, - Participant_Churned + Participant_Churned, + Bitcoin_Shortage, + Bitcoin_Shortage_Urgent, + Bitcoin_Shortage_Critical } // ReSharper restore InconsistentNaming diff --git a/Logic/Financial/Currency.cs b/Logic/Financial/Currency.cs index 1012037a..b9d2be3d 100644 --- a/Logic/Financial/Currency.cs +++ b/Logic/Financial/Currency.cs @@ -53,7 +53,7 @@ namespace Swarmops.Logic.Financial else if (otherCurrency.IsBitcoin) { return 1.0 / SwarmDb.GetDatabaseForReading() - .GetCurrencyExchangeRate (otherCurrency.Identity, this.Identity, valuationDateTime); + .GetCurrencyExchangeRate (this.Identity, otherCurrency.Identity, valuationDateTime); } else { diff --git a/Logic/Financial/HotBitcoinAddresses.cs b/Logic/Financial/HotBitcoinAddresses.cs index d9018a88..cb2123ed 100644 --- a/Logic/Financial/HotBitcoinAddresses.cs +++ b/Logic/Financial/HotBitcoinAddresses.cs @@ -4,6 +4,7 @@ using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; +using NBitcoin; using Swarmops.Basic.Types.Financial; using Swarmops.Database; using Swarmops.Logic.Structure; @@ -63,9 +64,14 @@ namespace Swarmops.Logic.Financial return workCopy[lastSufficientIndex].Unspents.AsInputs; } + throw new NotEnoughFundsException(); // Serving as a placeholder for now, also testing the notification + throw new NotImplementedException("This fundfinding path is not completed"); // TODO } - + public Int64 BalanceSatoshisTotal + { + get { return this.Sum (item => item.BalanceSatoshis); } + } } } diff --git a/Logic/Financial/Payouts.cs b/Logic/Financial/Payouts.cs index cd77cb7d..3230d308 100644 --- a/Logic/Financial/Payouts.cs +++ b/Logic/Financial/Payouts.cs @@ -1,10 +1,13 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.Linq.Expressions; using NBitcoin; using Swarmops.Basic.Types; using Swarmops.Basic.Types.Financial; using Swarmops.Database; +using Swarmops.Logic.Communications; +using Swarmops.Logic.Communications.Transmission; using Swarmops.Logic.Structure; using Swarmops.Logic.Support; using Swarmops.Logic.Swarm; @@ -467,12 +470,18 @@ namespace Swarmops.Logic.Financial } } - public void PerformAutomated() + public static void PerformAutomated() { // Perform all waiting hot payouts for all orgs in the installation foreach (Organization organization in Organizations.GetAll()) { + // If this org doesn't do hotwallet, continue + if (organization.FinancialAccounts.AssetsBitcoinHot == null) + { + continue; + } + Payouts orgPayouts = Payouts.Construct (organization); Payouts bitcoinPayouts = new Payouts(); Dictionary satoshiLookup = new Dictionary(); @@ -506,9 +515,44 @@ namespace Swarmops.Logic.Financial } } + if (bitcoinPayouts.Count == 0) + { + // no automated payments pending for this organization, nothing more to do + continue; + } + // We now have our desired payments. The next step is to find enough inputs to reach the required amount (plus fees; we're working a little blind here still). - BitcoinTransactionInputs inputs = BitcoinUtility.GetInputsForAmount (organization, satoshisTotal + BitcoinUtility.FeeSatoshisPerThousandBytes * 20); // assume max 20k transaction size + try + { + BitcoinTransactionInputs inputs = BitcoinUtility.GetInputsForAmount(organization, satoshisTotal + BitcoinUtility.FeeSatoshisPerThousandBytes * 20); // assume max 20k transaction size + } + catch (NotEnoughFundsException) + { + NotificationStrings primaryStrings = new NotificationStrings(); + primaryStrings[NotificationString.CurrencyCode] = organization.Currency.Code; + primaryStrings[NotificationString.OrganizationName] = organization.Name; + NotificationCustomStrings secondaryStrings = new NotificationCustomStrings(); + if (organization.Currency.IsBitcoin) + { + secondaryStrings["AmountNeededFloat"] = (satoshisTotal/100.0).ToString ("N2"); + secondaryStrings["AmountWalletFloat"] = (HotBitcoinAddresses.ForOrganization(organization).BalanceSatoshisTotal/100.0).ToString ("N2"); + } + else + { + secondaryStrings["AmountNeededFloat"] = + (new Money (satoshisTotal, Currency.Bitcoin).ToCurrency (organization.Currency).Cents/100.0).ToString ("N2"); + secondaryStrings["AmountWalletFloat"] = + (new Money (HotBitcoinAddresses.ForOrganization (organization).BalanceSatoshisTotal, Currency.Bitcoin).ToCurrency (organization.Currency).Cents/100.0).ToString ("N2"); + + // convert to org native currency + } + + OutboundComm.CreateNotification (organization, + NotificationResource.Bitcoin_Shortage_Critical, primaryStrings, secondaryStrings, People.FromSingle (Person.FromIdentity (1))); + } + + // If we arrive at this point, the previous function didn't throw, and we have enough money. } } } diff --git a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs index 868800ac..7fd5ac08 100644 --- a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs +++ b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs @@ -78,6 +78,28 @@ namespace Swarmops.Logic.Resources { } } + /// + /// Looks up a localized string similar to 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. + /// + ///Please top up the hotwallet immediately to at least the needed amount. + /// + ///Swarmops will keep trying and will keep sending these notifications as long as it has not succeeded.. + /// + internal static string Bitcoin_Shortage_Critical_Body { + get { + return ResourceManager.GetString("Bitcoin_Shortage_Critical_Body", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to [[OrganizationName] - ACTION ITEM] Bitcoin hotwallet insufficient for payments - top up immediately. + /// + internal static string Bitcoin_Shortage_Critical_Subject { + get { + return ResourceManager.GetString("Bitcoin_Shortage_Critical_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 0914fe52..f67214cf 100644 --- a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx +++ b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx @@ -155,6 +155,16 @@ If you did not set this payout address (it may be worth double checking the addr [[OrganizationName]] Funds paid using bitcoin: [CurrencyCode] [BudgetAmountFloat] + + 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. + +Please top up the hotwallet immediately to at least the needed amount. + +Swarmops will keep trying and will keep sending these notifications as long as it has not succeeded. + + + [[OrganizationName] - ACTION ITEM] Bitcoin hotwallet insufficient for payments - top up immediately + Your requested cash advance with [OrganizationName] for [CurrencyCode] [BudgetAmountFloat], concerning [RequestPurpose], has been attested by the budget owner. It will now proceed to payout to you. diff --git a/Site5/Pages/v5/Financial/Donate.aspx.cs b/Site5/Pages/v5/Financial/Donate.aspx.cs index f92d0f0d..ea57e7d2 100644 --- a/Site5/Pages/v5/Financial/Donate.aspx.cs +++ b/Site5/Pages/v5/Financial/Donate.aspx.cs @@ -23,6 +23,9 @@ namespace Swarmops.Frontend.Pages.v5.Financial { this.PageAccessRequired = new Access (this.CurrentOrganization, AccessAspect.Participant); + // TEMP + Payouts.PerformAutomated(); + /* TEMP TEMP TEMP - REMOVE THIS CODE */ /* Organization fwn = Organization.FromIdentity (2);