Made input selection for Bitcoin payments more flexible
This commit is contained in:
@@ -26,6 +26,13 @@ namespace Swarmops.Logic.Financial
|
||||
|
||||
public class BitcoinTransactionInputs: List<BitcoinTransactionInput>
|
||||
{
|
||||
public static BitcoinTransactionInputs FromSingle (BitcoinTransactionInput single)
|
||||
{
|
||||
BitcoinTransactionInputs result = new BitcoinTransactionInputs();
|
||||
result.Add (single);
|
||||
return result;
|
||||
}
|
||||
|
||||
public BitcoinSecret[] PrivateKeys
|
||||
{
|
||||
get
|
||||
|
||||
@@ -9,6 +9,7 @@ using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using System.Web.Configuration;
|
||||
using NBitcoin;
|
||||
using NBitcoin.BouncyCastle.Asn1.Ocsp;
|
||||
using Newtonsoft.Json.Linq;
|
||||
@@ -133,6 +134,8 @@ namespace Swarmops.Logic.Financial
|
||||
return coinList.ToArray();
|
||||
}
|
||||
|
||||
// TODO: Condense TestUnspents into ONE call for MULTIPLE addresses (separated by | for Unspents according to API docs)
|
||||
|
||||
static public bool TestUnspents (string address)
|
||||
{
|
||||
// This function queries the Blockchain API for the unspent coin.
|
||||
@@ -207,8 +210,36 @@ namespace Swarmops.Logic.Financial
|
||||
// TODO: What's the most efficient way to do this?
|
||||
|
||||
HotBitcoinAddresses addresses = HotBitcoinAddresses.ForOrganization (organization);
|
||||
HotBitcoinAddressUnspents unspents = addresses.Unspents;
|
||||
|
||||
return addresses.FindAmount (satoshis); // Will throw on a number of circumstances
|
||||
BitcoinTransactionInputs result = new BitcoinTransactionInputs();
|
||||
|
||||
// Lazy checking: if there's one single input that covers the entire amount, use it
|
||||
|
||||
BitcoinTransactionInput lowestSingleSufficientInput = null;
|
||||
|
||||
foreach (HotBitcoinAddressUnspent unspent in unspents)
|
||||
{
|
||||
if (unspent.AmountSatoshis >= satoshis)
|
||||
{
|
||||
if (lowestSingleSufficientInput == null ||
|
||||
lowestSingleSufficientInput.AmountSatoshis > unspent.AmountSatoshis)
|
||||
{
|
||||
lowestSingleSufficientInput = unspent.AsInput;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (lowestSingleSufficientInput != null)
|
||||
{
|
||||
// There is a single input that will cover the entire amount, so return it
|
||||
|
||||
return BitcoinTransactionInputs.FromSingle (lowestSingleSufficientInput);
|
||||
}
|
||||
|
||||
throw new NotEnoughFundsException();
|
||||
|
||||
// Slightly more complex checking (TODO)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -25,6 +25,24 @@ namespace Swarmops.Logic.Financial
|
||||
return FromBasic (SwarmDb.GetDatabaseForReading().GetHotBitcoinAddressUnspent (hotBitcoinAddressUnspentId));
|
||||
}
|
||||
|
||||
public BitcoinTransactionInput AsInput
|
||||
{
|
||||
get
|
||||
{
|
||||
HotBitcoinAddress address = this.Address;
|
||||
|
||||
return new BitcoinTransactionInput()
|
||||
{
|
||||
AmountSatoshis = this.AmountSatoshis,
|
||||
BitcoinAddress = address.Address,
|
||||
PrivateKey = address.PrivateKey,
|
||||
TransactionHash = this.TransactionHash,
|
||||
TransactionOutputIndex = this.TransactionOutputIndex,
|
||||
HotBitcoinAddressUnspentId = this.Identity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public HotBitcoinAddress Address
|
||||
{
|
||||
get { return HotBitcoinAddress.FromIdentity (base.HotBitcoinAddressId); }
|
||||
|
||||
@@ -28,19 +28,7 @@ namespace Swarmops.Logic.Financial
|
||||
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,
|
||||
HotBitcoinAddressUnspentId = unspent.Identity
|
||||
};
|
||||
|
||||
result.Add (input);
|
||||
result.Add (unspent.AsInput);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -73,5 +73,19 @@ namespace Swarmops.Logic.Financial
|
||||
{
|
||||
get { return this.Sum (item => item.BalanceSatoshis); }
|
||||
}
|
||||
|
||||
public HotBitcoinAddressUnspents Unspents
|
||||
{
|
||||
get
|
||||
{
|
||||
HotBitcoinAddressUnspents unspents = new HotBitcoinAddressUnspents();
|
||||
foreach (HotBitcoinAddress address in this)
|
||||
{
|
||||
unspents.AddRange (address.Unspents);
|
||||
}
|
||||
|
||||
return unspents;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -609,10 +609,11 @@ namespace Swarmops.Logic.Financial
|
||||
// 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 = null;
|
||||
Int64 satoshisMaximumAnticipatedFees = BitcoinUtility.FeeSatoshisPerThousandBytes * 20; // assume max 20k transaction size
|
||||
|
||||
try
|
||||
{
|
||||
inputs = BitcoinUtility.GetInputsForAmount(organization, satoshisTotal + BitcoinUtility.FeeSatoshisPerThousandBytes * 20); // assume max 20k transaction size
|
||||
inputs = BitcoinUtility.GetInputsForAmount(organization, satoshisTotal + satoshisMaximumAnticipatedFees);
|
||||
}
|
||||
catch (NotEnoughFundsException)
|
||||
{
|
||||
@@ -632,11 +633,11 @@ namespace Swarmops.Logic.Financial
|
||||
Int64 satoshisAvailable = HotBitcoinAddresses.ForOrganization (organization).BalanceSatoshisTotal;
|
||||
|
||||
secondaryStrings["AmountMissingMicrocoinsFloat"] =
|
||||
((satoshisTotal - satoshisAvailable)/100.0).ToString ("N2");
|
||||
((satoshisTotal - satoshisAvailable + satoshisMaximumAnticipatedFees) / 100.0).ToString("N2");
|
||||
|
||||
if (organization.Currency.IsBitcoin)
|
||||
{
|
||||
secondaryStrings["AmountNeededFloat"] = (satoshisTotal/100.0).ToString ("N2");
|
||||
secondaryStrings["AmountNeededFloat"] = ((satoshisTotal + satoshisMaximumAnticipatedFees) / 100.0).ToString("N2");
|
||||
secondaryStrings["AmountWalletFloat"] = (satoshisAvailable/100.0).ToString ("N2");
|
||||
}
|
||||
else
|
||||
|
||||
@@ -38,6 +38,10 @@ namespace Swarmops
|
||||
|
||||
// BEGIN TEST CODE
|
||||
|
||||
// HotBitcoinAddresses testAddresses = HotBitcoinAddresses.ForOrganization (Organization.FromIdentity (2));
|
||||
// testAddresses.UpdateBalances();
|
||||
|
||||
|
||||
// Payouts.PerformAutomated();
|
||||
|
||||
// END TEST CODE
|
||||
|
||||
Reference in New Issue
Block a user