More work on hot payouts - doing dryruns

This commit is contained in:
Rick Falkvinge
2015-10-12 12:08:42 +02:00
parent 19b9e2d555
commit 960d1fd6e4
5 changed files with 96 additions and 20 deletions
@@ -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
+55 -12
View File
@@ -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 <string, Int64> satoshiPayoutLookup = new Dictionary<string, long>();
Dictionary <int, Int64> satoshiPersonLookup = new Dictionary<int, long>();
Dictionary<string, Int64> satoshiPayoutLookup = new Dictionary<string, long>();
Dictionary<string, Int64> nativeCentsPayoutLookup = new Dictionary<string, long>();
Dictionary<int, Int64> satoshiPersonLookup = new Dictionary<int, long>();
Dictionary <int, Int64> nativeCentsPersonLookup = new Dictionary<int, long>();
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<int, List<string>> notificationLookup = new Dictionary<int, List<string>>();
Dictionary<int, List<string>> notificationSpecLookup = new Dictionary<int, List<string>>();
Dictionary<int, List<Int64>> notificationAmountLookup = new Dictionary<int, List<Int64>>();
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<string>();
notificationSpecLookup[recipientPersonId] = new List<string>();
notificationAmountLookup[recipientPersonId] = new List<Int64>();
}
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
@@ -61,11 +61,11 @@ namespace Swarmops.Logic.Resources {
}
/// <summary>
/// Looks up a localized string similar to [BitcoinAmountFloat] microcoin (&quot;bits&quot;) was just transferred to your bitcoin address ([BitcoinAddress]) by [OrganizationName] for the following:
/// Looks up a localized string similar to [BitcoinAmountFloat] microbitcoin (&quot;bits&quot;) 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)..
/// </summary>
internal static string Bitcoin_PaidOut_Body {
get {
@@ -74,7 +74,7 @@ namespace Swarmops.Logic.Resources {
}
/// <summary>
/// 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].
/// </summary>
internal static string Bitcoin_PaidOut_Subject {
get {
@@ -104,6 +104,26 @@ namespace Swarmops.Logic.Resources {
}
}
/// <summary>
/// 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..
/// </summary>
internal static string BitcoinPayoutAddress_Bad_Body {
get {
return ResourceManager.GetString("BitcoinPayoutAddress_Bad_Body", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to [[OrganizationName] - ACTION ITEM] Bad bitcoin payout address.
/// </summary>
internal static string BitcoinPayoutAddress_Bad_Subject {
get {
return ResourceManager.GetString("BitcoinPayoutAddress_Bad_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.
///
@@ -117,6 +117,14 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="BitcoinPayoutAddress_Bad_Body" xml:space="preserve">
<value>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.</value>
</data>
<data name="BitcoinPayoutAddress_Bad_Subject" xml:space="preserve">
<value>[[OrganizationName] - ACTION ITEM] Bad bitcoin payout address</value>
</data>
<data name="BitcoinPayoutAddress_Changed_Body" xml:space="preserve">
<value>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
<value>[[OrganizationName]] Bitcoin payout address set</value>
</data>
<data name="Bitcoin_PaidOut_Body" xml:space="preserve">
<value>[BitcoinAmountFloat] microcoin ("bits") was just transferred to your bitcoin address ([BitcoinAddress]) by [OrganizationName] for the following:
<value>[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).</value>
You should already see the transaction in your wallet and it should achieve spendability within about an hour (six normal confirmations).</value>
</data>
<data name="Bitcoin_PaidOut_Subject" xml:space="preserve">
<value>[[OrganizationName]] Funds paid using bitcoin: [CurrencyCode] [AmountFloat]</value>
<value>[[OrganizationName]] Funds paid to you: [CurrencyCode] [AmountFloat]</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.
+1 -1
View File
@@ -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 */
/*