Work on hotwallets and payouts

This commit is contained in:
Rick Falkvinge
2015-10-10 21:56:18 +02:00
parent 27db2a6b17
commit 73fe7d4e42
12 changed files with 186 additions and 10 deletions
@@ -9,10 +9,11 @@ namespace Swarmops.Basic.Types.Financial
{
public class BasicHotBitcoinAddressUnspent: IHasIdentity
{
public BasicHotBitcoinAddressUnspent (int hotBitcoinAddressUnspentId, string transactionHash,
public BasicHotBitcoinAddressUnspent (int hotBitcoinAddressUnspentId, int hotBitcoinAddressId, string transactionHash,
int transactionOutputIndex, Int64 amountSatoshis, int confirmationCount)
{
this.HotBitcoinAddressUnspentId = hotBitcoinAddressUnspentId;
this.HotBitcoinAddressId = hotBitcoinAddressId;
this.TransactionHash = transactionHash;
this.TransactionOutputIndex = transactionOutputIndex;
this.AmountSatoshis = amountSatoshis;
@@ -21,13 +22,14 @@ namespace Swarmops.Basic.Types.Financial
public BasicHotBitcoinAddressUnspent (BasicHotBitcoinAddressUnspent original)
: this (
original.HotBitcoinAddressUnspentId, original.TransactionHash, original.TransactionOutputIndex,
original.HotBitcoinAddressUnspentId, original.HotBitcoinAddressId, original.TransactionHash, original.TransactionOutputIndex,
original.AmountSatoshis, original.ConfirmationCount)
{
// copy ctor
}
public int HotBitcoinAddressUnspentId { get; private set; }
public int HotBitcoinAddressId { get; private set; }
public string TransactionHash { get; private set; }
public int TransactionOutputIndex { get; private set; }
public Int64 AmountSatoshis { get; private set; }
+8 -6
View File
@@ -20,7 +20,8 @@ namespace Swarmops.Database
" FROM HotBitcoinAddresses ";
private const string hotBitcoinAddressUnspentFieldSequence =
" HotBitcoinAddressUnspentId,TransactionHash,TransactionOutputIndex,AmountSatoshis,ConfirmationCount" + // 0-4
" HotBitcoinAddressUnspentId,HotBitcoinAddressId,TransactionHash,TransactionOutputIndex,AmountSatoshis," + // 0-4
" ConfirmationCount" + // 5
" FROM HotBitcoinAddressUnspents ";
private BasicHotBitcoinAddress ReadHotBitcoinAddressFromDataReader(IDataRecord reader)
@@ -38,12 +39,13 @@ namespace Swarmops.Database
private BasicHotBitcoinAddressUnspent ReadHotBitcoinAddressUnspentFromDataReader(IDataRecord reader)
{
int hotBitcoinAddressUnspentId = reader.GetInt32 (0);
string transactionHash = reader.GetString (1);
int transactionOutputIndex = reader.GetInt32 (2);
Int64 amountSatoshis = reader.GetInt64 (3);
int confirmationCount = reader.GetInt32 (4);
int hotBitcoinAddressId = reader.GetInt32 (1);
string transactionHash = reader.GetString (2);
int transactionOutputIndex = reader.GetInt32 (3);
Int64 amountSatoshis = reader.GetInt64 (4);
int confirmationCount = reader.GetInt32 (5);
return new BasicHotBitcoinAddressUnspent (hotBitcoinAddressUnspentId, transactionHash, transactionOutputIndex, amountSatoshis, confirmationCount);
return new BasicHotBitcoinAddressUnspent (hotBitcoinAddressUnspentId, hotBitcoinAddressId, transactionHash, transactionOutputIndex, amountSatoshis, confirmationCount);
}
#endregion
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NBitcoin;
namespace Swarmops.Logic.Financial
{
public class BitcoinTransactionInput
{
public BitcoinSecret PrivateKey { get; set; }
public Int64 AmountSatoshis { get; set; }
public string TransactionHash { get; set; }
public Int32 TransactionOutputIndex { get; set; }
public string BitcoinAddress { get; set; }
}
public class BitcoinTransactionInputs: List<BitcoinTransactionInput>
{
// typedef the name
}
}
+12
View File
@@ -13,6 +13,7 @@ using NBitcoin;
using NBitcoin.BouncyCastle.Asn1.Ocsp;
using Newtonsoft.Json.Linq;
using Swarmops.Database;
using Swarmops.Logic.Structure;
using Swarmops.Logic.Support;
namespace Swarmops.Logic.Financial
@@ -165,6 +166,17 @@ namespace Swarmops.Logic.Financial
}
}
static public BitcoinTransactionInputs GetInputsForAmount (Organization organization, Int64 satoshis)
{
// TODO: Verify inputs up to date?
// TODO: What's the most efficient way to do this?
HotBitcoinAddresses addresses = HotBitcoinAddresses.ForOrganization (organization);
return addresses.FindAmount (satoshis); // Will throw on a number of circumstances
}
public const int BitcoinWalletIndex = 1;
public const int BitcoinDonationsIndex = 2;
public const int BitcoinLoansIndex = 3;
@@ -18,5 +18,10 @@ namespace Swarmops.Logic.Financial
{
return new HotBitcoinAddressUnspent (basic);
}
public HotBitcoinAddress Address
{
get { return HotBitcoinAddress.FromIdentity (base.HotBitcoinAddressId); }
}
}
}
@@ -15,5 +15,30 @@ namespace Swarmops.Logic.Financial
{
return FromArray (SwarmDb.GetDatabaseForReading().GetHotBitcoinAddressUnspents (address));
}
public BitcoinTransactionInputs AsInputs
{
get
{
BitcoinTransactionInputs result = new BitcoinTransactionInputs();
foreach (HotBitcoinAddressUnspent unspent in this)
{
HotBitcoinAddress address = unspent.Address;
BitcoinTransactionInput input = new BitcoinTransactionInput()
{
AmountSatoshis = unspent.AmountSatoshis,
BitcoinAddress = address.Address,
PrivateKey = address.PrivateKey,
TransactionHash = unspent.TransactionHash,
TransactionOutputIndex = unspent.TransactionOutputIndex
};
result.Add (input);
}
return result;
}
}
}
}
+57 -1
View File
@@ -1,15 +1,71 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Swarmops.Basic.Types.Financial;
using Swarmops.Database;
using Swarmops.Logic.Structure;
using Swarmops.Logic.Support;
namespace Swarmops.Logic.Financial
{
public class HotBitcoinAddresses: PluralBase<HotBitcoinAddresses, HotBitcoinAddress, BasicHotBitcoinAddress>
{
// to be implemented
public static HotBitcoinAddresses ForOrganization (Organization organization)
{
return FromArray (SwarmDb.GetDatabaseForReading().GetHotBitcoinAddresses (organization));
}
public HotBitcoinAddresses InMainWallet
{
get
{
HotBitcoinAddresses subset = new HotBitcoinAddresses();
string startMatch = BitcoinUtility.BitcoinWalletIndex.ToString (CultureInfo.InvariantCulture) + " ";
foreach (HotBitcoinAddress address in this)
{
if (address.DerivationPath.StartsWith (startMatch))
{
subset.Add (address);
}
}
return subset;
}
}
public BitcoinTransactionInputs FindAmount (Int64 satoshisRequired)
{
// Sort addresses by known balance, richest first
HotBitcoinAddresses workCopy = this;
workCopy.Sort((a,b) => Math.Sign(b.BalanceSatoshis - a.BalanceSatoshis)); // Descending order
// Do linear search from top for last address with sufficient money on its own. TODO: Binary search.
int lastSufficientIndex = -1;
for (int index = 0; index < workCopy.Count; index++)
{
if (workCopy[index].BalanceSatoshis >= satoshisRequired)
{
lastSufficientIndex = index;
}
}
if (lastSufficientIndex >= 0)
{
// There exists at least one single input which is sufficient to meet the requirement.
// The lowest input fulfilling this criterion has been chosen as return value.
return workCopy[lastSufficientIndex].Unspents.AsInputs;
}
throw new NotImplementedException("This fundfinding path is not completed"); // TODO
}
}
}
+46
View File
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using NBitcoin;
using Swarmops.Basic.Types;
using Swarmops.Basic.Types.Financial;
using Swarmops.Database;
@@ -465,5 +466,50 @@ namespace Swarmops.Logic.Financial
}
}
}
public void PerformAutomated()
{
// Perform all waiting hot payouts for all orgs in the installation
foreach (Organization organization in Organizations.GetAll())
{
Payouts orgPayouts = Payouts.Construct (organization);
Payouts bitcoinPayouts = new Payouts();
Dictionary <string, Int64> satoshiLookup = new Dictionary<string, long>();
Int64 satoshisTotal = 0;
// 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.RecipientPerson != null && payout.RecipientPerson.BitcoinPayoutAddress.Length > 2 &&
payout.Account.Length < 4)
{
bitcoinPayouts.Add (payout);
// Find the amount of satoshis for this payout
if (organization.Currency.IsBitcoin)
{
satoshiLookup[payout.ProtoIdentity] = payout.AmountCents;
satoshisTotal += payout.AmountCents;
}
else
{
// Convert currency
Money payoutAmount = new Money(payout.AmountCents, organization.Currency);
Int64 satoshis = payoutAmount.ToCurrency(Currency.Bitcoin).Cents;
satoshiLookup[payout.ProtoIdentity] = satoshis;
satoshisTotal += satoshis;
}
}
}
// 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
}
}
}
}
+1
View File
@@ -118,6 +118,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Financial\BitcoinTransactionInput.cs" />
<Compile Include="Financial\BitcoinUtility.cs" />
<Compile Include="Financial\HotBitcoinAddress.cs" />
<Compile Include="Financial\HotBitcoinAddresses.cs" />
@@ -346,6 +346,9 @@
<data name="Donate_StatusInitial" xml:space="preserve">
<value>Waiting for transaction...</value>
</data>
<data name="Donate_TransactionDetected" xml:space="preserve">
<value>Transaction detected, verifying...</value>
</data>
<data name="Donate_TxLabel" xml:space="preserve">
<value>Donation to {0}</value>
<comment>The {0} is the organization name</comment>
+1 -1
View File
@@ -42,7 +42,7 @@
// transaction received at address
// call page to check if funds received server side
alertify.log("Detected transaction, verifying...");
alertify.log(decodeURIComponent('<asp:Literal ID="LiteralTxDetected" runat="server" />'));
checkTransactionReceived(message.x.hash);
}
};
+1
View File
@@ -41,6 +41,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial
this.PageTitle = Resources.Pages.Financial.Donate_PageTitle;
this.InfoBoxLiteral = Resources.Pages.Financial.Donate_Info;
this.LabelStatus.Text = Resources.Pages.Financial.Donate_StatusInitial;
this.LiteralTxDetected.Text = JavascriptEscape (Resources.Pages.Financial.Donate_TransactionDetected);
if (this.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot == null)
{