Work on hotwallets and payouts. Almost there...
This commit is contained in:
@@ -170,6 +170,24 @@ namespace Swarmops.Database
|
||||
}
|
||||
}
|
||||
|
||||
public int SetHotBitcoinAddressBalance (int hotBitcoinAddressId, Int64 balanceSatoshis)
|
||||
{
|
||||
using (DbConnection connection = GetMySqlDbConnection())
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
using (DbCommand command = GetDbCommand("SetHotBitcoinAddressBalance", connection))
|
||||
{
|
||||
command.CommandType = CommandType.StoredProcedure;
|
||||
|
||||
AddParameterWithName(command, "hotBitcoinAddressId", hotBitcoinAddressId);
|
||||
AddParameterWithName(command, "balanceSatoshis", balanceSatoshis);
|
||||
|
||||
return Convert.ToInt32(command.ExecuteScalar());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int CreateHotBitcoinAddressUnspentConditional(int hotBitcoinAddressId, string transactionHash, int transactionOutputIndex, Int64 amountSatoshis, int confirmationCount)
|
||||
{
|
||||
using (DbConnection connection = GetMySqlDbConnection())
|
||||
@@ -191,6 +209,23 @@ namespace Swarmops.Database
|
||||
}
|
||||
}
|
||||
|
||||
public int DeleteHotBitcoinAddressUnspent(int hotBitcoinAddressUnspentId)
|
||||
{
|
||||
using (DbConnection connection = GetMySqlDbConnection())
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
using (DbCommand command = GetDbCommand("DeleteHotBitcoinAddressUnspent", connection))
|
||||
{
|
||||
command.CommandType = CommandType.StoredProcedure;
|
||||
|
||||
AddParameterWithName(command, "hotBitcoinAddressUnspentId", hotBitcoinAddressUnspentId);
|
||||
|
||||
return Convert.ToInt32(command.ExecuteScalar());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Swarmops.Basic.Types.Financial;
|
||||
using Swarmops.Database;
|
||||
|
||||
namespace Swarmops.Logic.Financial
|
||||
{
|
||||
@@ -23,5 +24,11 @@ namespace Swarmops.Logic.Financial
|
||||
{
|
||||
get { return HotBitcoinAddress.FromIdentity (base.HotBitcoinAddressId); }
|
||||
}
|
||||
|
||||
public void Delete()
|
||||
{
|
||||
SwarmDb.GetDatabaseForWriting().DeleteHotBitcoinAddressUnspent (this.Identity);
|
||||
SwarmDb.GetDatabaseForWriting().SetHotBitcoinAddressBalance (base.HotBitcoinAddressId, this.Address.Unspents.AmountSatoshisTotal); // re-fetches from db
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,12 @@ namespace Swarmops.Logic.Financial
|
||||
{
|
||||
static public HotBitcoinAddressUnspents ForAddress (HotBitcoinAddress address)
|
||||
{
|
||||
return FromArray (SwarmDb.GetDatabaseForReading().GetHotBitcoinAddressUnspents (address));
|
||||
return FromArray (SwarmDb.GetDatabaseForWriting().GetHotBitcoinAddressUnspents (address)); // "ForWriting" is intentional here
|
||||
}
|
||||
|
||||
public Int64 AmountSatoshisTotal
|
||||
{
|
||||
get { return this.Sum (item => item.AmountSatoshis); }
|
||||
}
|
||||
|
||||
public BitcoinTransactionInputs AsInputs
|
||||
@@ -40,5 +45,25 @@ namespace Swarmops.Logic.Financial
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteAll()
|
||||
{
|
||||
Dictionary<int, bool> addressIdLookup = new Dictionary<int, bool>();
|
||||
this.ForEach (item => addressIdLookup[item.HotBitcoinAddressId] = true);
|
||||
SwarmDb db = SwarmDb.GetDatabaseForWriting();
|
||||
|
||||
// Delete the unspents in this collection
|
||||
|
||||
this.ForEach (item => db.DeleteHotBitcoinAddressUnspent (item.Identity));
|
||||
|
||||
// Recalculate the amount remaining in the addresses
|
||||
|
||||
foreach (int addressId in addressIdLookup.Keys)
|
||||
{
|
||||
db.SetHotBitcoinAddressBalance (addressId,
|
||||
HotBitcoinAddress.FromIdentity (addressId).Unspents.AmountSatoshisTotal);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,8 +43,18 @@ namespace Swarmops.Logic.Financial
|
||||
// return payout;
|
||||
}
|
||||
|
||||
public static Payout Empty
|
||||
{
|
||||
get { return new Payout (new BasicPayout (0, 0, string.Empty, string.Empty, string.Empty, 0, DateTime.UtcNow, true, DateTime.UtcNow, 0)); }
|
||||
}
|
||||
|
||||
private void LoadDependencies()
|
||||
{
|
||||
if (this.DependentCashAdvancesPayback != null && Identity == 0)
|
||||
{
|
||||
return; // if inited and identity zero, return
|
||||
}
|
||||
|
||||
this.DependentExpenseClaims = new ExpenseClaims();
|
||||
this.DependentInvoices = new InboundInvoices();
|
||||
this.DependentSalariesNet = new Salaries();
|
||||
@@ -52,6 +62,11 @@ namespace Swarmops.Logic.Financial
|
||||
this.DependentCashAdvancesPayout = new CashAdvances();
|
||||
this.DependentCashAdvancesPayback = new CashAdvances();
|
||||
|
||||
if (Identity == 0)
|
||||
{
|
||||
return; // never progress past here if identity zero
|
||||
}
|
||||
|
||||
BasicFinancialDependency[] dependencies = SwarmDb.GetDatabaseForReading().GetPayoutDependencies (Identity);
|
||||
|
||||
foreach (BasicFinancialDependency dependency in dependencies)
|
||||
@@ -608,7 +623,30 @@ namespace Swarmops.Logic.Financial
|
||||
|
||||
public void MigrateDependenciesTo (Payout migrationTarget)
|
||||
{
|
||||
SwarmDb.GetDatabaseForWriting().MovePayoutDependencies (Identity, migrationTarget.Identity);
|
||||
if (Identity > 0 && migrationTarget.Identity > 0)
|
||||
{
|
||||
// Persisted payout migration
|
||||
|
||||
SwarmDb.GetDatabaseForWriting().MovePayoutDependencies (Identity, migrationTarget.Identity);
|
||||
}
|
||||
else
|
||||
{
|
||||
// In-memory migration: this payout isn't in database yet
|
||||
|
||||
this.DependentCashAdvancesPayback.ForEach(item => migrationTarget.DependentCashAdvancesPayback.Add (item));
|
||||
this.DependentCashAdvancesPayout.ForEach (item => migrationTarget.DependentCashAdvancesPayout.Add (item));
|
||||
this.DependentExpenseClaims.ForEach(item => migrationTarget.DependentExpenseClaims.Add(item));
|
||||
this.DependentInvoices.ForEach(item => migrationTarget.DependentInvoices.Add(item));
|
||||
this.DependentSalariesNet.ForEach(item => migrationTarget.DependentSalariesNet.Add(item));
|
||||
this.DependentSalariesTax.ForEach(item => migrationTarget.DependentSalariesTax.Add(item));
|
||||
|
||||
this.DependentCashAdvancesPayback = new CashAdvances();
|
||||
this.DependentCashAdvancesPayout = new CashAdvances();
|
||||
this.DependentExpenseClaims = new ExpenseClaims();
|
||||
this.DependentInvoices = new InboundInvoices();
|
||||
this.DependentSalariesNet = new Salaries();
|
||||
this.DependentSalariesTax = new Salaries();
|
||||
}
|
||||
migrationTarget.RecalculateAmount();
|
||||
RecalculateAmount();
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ using Swarmops.Logic.Communications.Transmission;
|
||||
using Swarmops.Logic.Structure;
|
||||
using Swarmops.Logic.Support;
|
||||
using Swarmops.Logic.Swarm;
|
||||
using Satoshis = NBitcoin.Money;
|
||||
|
||||
namespace Swarmops.Logic.Financial
|
||||
{
|
||||
@@ -495,6 +496,19 @@ namespace Swarmops.Logic.Financial
|
||||
if (payout.RecipientPerson != null && payout.RecipientPerson.BitcoinPayoutAddress.Length > 2 &&
|
||||
payout.Account.Length < 4)
|
||||
{
|
||||
// Test the payout address - is it valid and can we handle it?
|
||||
|
||||
try
|
||||
{
|
||||
new BitcoinAddress (payout.RecipientPerson.BitcoinPayoutAddress);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// TODO: Send notification of invalid address, if we're on the whole hour AND the whole day
|
||||
continue; // do not add this payout
|
||||
}
|
||||
|
||||
|
||||
bitcoinPayouts.Add (payout);
|
||||
|
||||
// Find the amount of satoshis for this payout
|
||||
@@ -557,6 +571,8 @@ namespace Swarmops.Logic.Financial
|
||||
|
||||
OutboundComm.CreateNotification (organization,
|
||||
NotificationResource.Bitcoin_Shortage_Critical, primaryStrings, secondaryStrings, People.FromSingle (Person.FromIdentity (1)));
|
||||
|
||||
continue; // with next organization
|
||||
}
|
||||
|
||||
// If we arrive at this point, the previous function didn't throw, and we have enough money. Add the inputs to the transaction.
|
||||
@@ -568,18 +584,41 @@ namespace Swarmops.Logic.Financial
|
||||
// Add outputs and prepare notifications
|
||||
|
||||
Int64 satoshisUsed = 0;
|
||||
Dictionary<int, string> notificationLookup = new Dictionary<int, string>();
|
||||
Dictionary<int, List<string>> notificationLookup = new Dictionary<int, List<string>>();
|
||||
Payout masterPayout = Payout.Empty;
|
||||
|
||||
foreach (Payout payout in bitcoinPayouts)
|
||||
{
|
||||
int recipientPersonId = payout.RecipientPerson.Identity;
|
||||
if (!notificationLookup.ContainsKey (recipientPersonId))
|
||||
{
|
||||
notificationLookup[recipientPersonId] = new List<string>();
|
||||
}
|
||||
notificationLookup[recipientPersonId].Add (payout.Specification);
|
||||
|
||||
txBuilder = txBuilder.Send(new BitcoinAddress(payout.RecipientPerson.BitcoinPayoutAddress),
|
||||
new Satoshis(satoshiLookup[payout.ProtoIdentity]));
|
||||
satoshisUsed += satoshiLookup[payout.ProtoIdentity];
|
||||
|
||||
payout.MigrateDependenciesTo (masterPayout);
|
||||
}
|
||||
|
||||
// Set change address to wallet slush
|
||||
|
||||
txBuilder.SetChange (new BitcoinAddress (HotBitcoinAddress.OrganizationWalletZero (organization).Address));
|
||||
|
||||
// Add fee
|
||||
|
||||
// Add change to main wallet zero-slush address
|
||||
int transactionSizeBytes = txBuilder.EstimateSize (txBuilder.BuildTransaction (false)) + inputs.Count;
|
||||
// +inputs.Count for size variability
|
||||
|
||||
Int64 feeSatoshis = (transactionSizeBytes/1000 + 1)*BitcoinUtility.FeeSatoshisPerThousandBytes;
|
||||
|
||||
txBuilder = txBuilder.SendFees (new Satoshis (feeSatoshis));
|
||||
satoshisUsed += feeSatoshis;
|
||||
|
||||
// Sign transaction - ready to execute
|
||||
|
||||
// Prepare notifications after execution
|
||||
|
||||
// Broadcast transaction
|
||||
|
||||
// Delete inputs, adjust balance for addresses
|
||||
|
||||
@@ -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 */
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user