Work on hotwallet

This commit is contained in:
Rick Falkvinge
2015-10-11 16:21:11 +02:00
parent 73fe7d4e42
commit 067a0121a2
7 changed files with 108 additions and 10 deletions
@@ -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
+1 -1
View File
@@ -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
{
+7 -1
View File
@@ -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); }
}
}
}
+46 -2
View File
@@ -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 <string, Int64> satoshiLookup = new Dictionary<string, long>();
@@ -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.
}
}
}
@@ -78,6 +78,28 @@ namespace Swarmops.Logic.Resources {
}
}
/// <summary>
/// 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..
/// </summary>
internal static string Bitcoin_Shortage_Critical_Body {
get {
return ResourceManager.GetString("Bitcoin_Shortage_Critical_Body", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to [[OrganizationName] - ACTION ITEM] Bitcoin hotwallet insufficient for payments - top up immediately.
/// </summary>
internal static string Bitcoin_Shortage_Critical_Subject {
get {
return ResourceManager.GetString("Bitcoin_Shortage_Critical_Subject", resourceCulture);
}
}
/// <summary>
/// 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.
///
@@ -155,6 +155,16 @@ If you did not set this payout address (it may be worth double checking the addr
<data name="Bitcoin_PaidOut_Subject" xml:space="preserve">
<value>[[OrganizationName]] Funds paid using bitcoin: [CurrencyCode] [BudgetAmountFloat]</value>
</data>
<data name="Bitcoin_Shortage_Critical_Body" xml:space="preserve">
<value>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.</value>
</data>
<data name="Bitcoin_Shortage_Critical_Subject" xml:space="preserve">
<value>[[OrganizationName] - ACTION ITEM] Bitcoin hotwallet insufficient for payments - top up immediately</value>
</data>
<data name="CashAdvance_Attested_Body" xml:space="preserve">
<value>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.</value>
</data>
+3
View File
@@ -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);