Work on hotwallets and payouts
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using NBitcoin;
|
||||
using Satoshis=NBitcoin.Money;
|
||||
|
||||
namespace Swarmops.Logic.Financial
|
||||
{
|
||||
@@ -18,6 +20,46 @@ namespace Swarmops.Logic.Financial
|
||||
|
||||
public class BitcoinTransactionInputs: List<BitcoinTransactionInput>
|
||||
{
|
||||
// typedef the name
|
||||
public BitcoinSecret[] PrivateKeys
|
||||
{
|
||||
get
|
||||
{
|
||||
List<BitcoinSecret> keyList = new List<BitcoinSecret>();
|
||||
|
||||
foreach (BitcoinTransactionInput input in this)
|
||||
{
|
||||
keyList.Add (input.PrivateKey);
|
||||
}
|
||||
|
||||
return keyList.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public Coin[] Coins
|
||||
{
|
||||
get
|
||||
{
|
||||
List<Coin> coinList = new List<Coin>();
|
||||
|
||||
foreach (BitcoinTransactionInput input in this)
|
||||
{
|
||||
coinList.Add (new Coin (
|
||||
new OutPoint()
|
||||
{
|
||||
Hash = uint256.Parse (input.TransactionHash),
|
||||
N = (uint) input.TransactionOutputIndex
|
||||
},
|
||||
new TxOut (new Satoshis (input.AmountSatoshis),
|
||||
new BitcoinAddress (input.BitcoinAddress, Network.Main))));
|
||||
}
|
||||
|
||||
return coinList.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public Int64 AmountSatoshisTotal
|
||||
{
|
||||
get { return this.Sum (input => input.AmountSatoshis); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ namespace Swarmops.Logic.Financial
|
||||
|
||||
string addressString = secretKey.PubKey.GetAddress (Network.Main).ToString();
|
||||
var unspentJsonResult =
|
||||
JObject.Parse(new WebClient().DownloadString("https://blockchain.info/unspent?active=" + addressString + "&api_key=I_am_testing_the_format_please_ignore"));
|
||||
JObject.Parse(new WebClient().DownloadString("https://blockchain.info/unspent?active=" + addressString + "&api_key=" + SystemSettings.BlockchainSwarmopsApiKey));
|
||||
|
||||
List<Coin> coinList = new List<Coin>();
|
||||
|
||||
@@ -110,7 +110,7 @@ namespace Swarmops.Logic.Financial
|
||||
var addressInfoResult =
|
||||
JObject.Parse (
|
||||
new WebClient().DownloadString (
|
||||
"https://blockchain.info/address/" + address + "?format=json&api_key=I_am_testing_the_format_please_ignore"));
|
||||
"https://blockchain.info/address/" + address + "?format=json&api_key=" + SystemSettings.BlockchainSwarmopsApiKey));
|
||||
|
||||
if ((int) addressInfoResult["final_balance"] == 0)
|
||||
{
|
||||
@@ -118,7 +118,9 @@ namespace Swarmops.Logic.Financial
|
||||
}
|
||||
|
||||
var unspentJsonResult =
|
||||
JObject.Parse(new WebClient().DownloadString("https://blockchain.info/unspent?active=" + address + "&api_key=I_am_testing_the_format_please_ignore")); // warn: will 500 if no unspent outpoints
|
||||
JObject.Parse (
|
||||
new WebClient().DownloadString ("https://blockchain.info/unspent?active=" + address + "&api_key=" +
|
||||
SystemSettings.BlockchainSwarmopsApiKey)); // warn: will 500 if no unspent outpoints
|
||||
|
||||
foreach (var unspentJson in unspentJsonResult["unspent_outputs"])
|
||||
{
|
||||
|
||||
@@ -76,6 +76,11 @@ namespace Swarmops.Logic.Financial
|
||||
return FromBasic(SwarmDb.GetDatabaseForReading().GetHotBitcoinAddress(bitcoinAddress));
|
||||
}
|
||||
|
||||
public static HotBitcoinAddress OrganizationWalletZero (Organization organization)
|
||||
{
|
||||
return Create (organization, BitcoinUtility.BitcoinWalletIndex, 0);
|
||||
}
|
||||
|
||||
public BitcoinSecret PrivateKey
|
||||
{
|
||||
get
|
||||
|
||||
@@ -523,9 +523,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;
|
||||
|
||||
try
|
||||
{
|
||||
BitcoinTransactionInputs inputs = BitcoinUtility.GetInputsForAmount(organization, satoshisTotal + BitcoinUtility.FeeSatoshisPerThousandBytes * 20); // assume max 20k transaction size
|
||||
inputs = BitcoinUtility.GetInputsForAmount(organization, satoshisTotal + BitcoinUtility.FeeSatoshisPerThousandBytes * 20); // assume max 20k transaction size
|
||||
}
|
||||
catch (NotEnoughFundsException)
|
||||
{
|
||||
@@ -536,7 +538,7 @@ namespace Swarmops.Logic.Financial
|
||||
Int64 satoshisAvailable = HotBitcoinAddresses.ForOrganization (organization).BalanceSatoshisTotal;
|
||||
|
||||
secondaryStrings["AmountMissingMicrocoinsFloat"] =
|
||||
((satoshisAvailable - satoshisTotal)/100.0).ToString ("N2");
|
||||
((satoshisTotal - satoshisAvailable)/100.0).ToString ("N2");
|
||||
|
||||
if (organization.Currency.IsBitcoin)
|
||||
{
|
||||
@@ -545,19 +547,47 @@ namespace Swarmops.Logic.Financial
|
||||
}
|
||||
else
|
||||
{
|
||||
// convert to org native currency
|
||||
|
||||
secondaryStrings["AmountNeededFloat"] =
|
||||
(new Money (satoshisTotal, Currency.Bitcoin).ToCurrency (organization.Currency).Cents/100.0).ToString ("N2");
|
||||
secondaryStrings["AmountWalletFloat"] =
|
||||
(new Money (satoshisAvailable, 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.
|
||||
// If we arrive at this point, the previous function didn't throw, and we have enough money. Add the inputs to the transaction.
|
||||
|
||||
txBuilder = txBuilder.AddCoins (inputs.Coins);
|
||||
txBuilder = txBuilder.AddKeys (inputs.PrivateKeys);
|
||||
Int64 satoshisInput = inputs.AmountSatoshisTotal;
|
||||
|
||||
// Add outputs and prepare notifications
|
||||
|
||||
Int64 satoshisUsed = 0;
|
||||
Dictionary<int, string> notificationLookup = new Dictionary<int, string>();
|
||||
|
||||
|
||||
|
||||
// Add fee
|
||||
|
||||
// Add change to main wallet zero-slush address
|
||||
|
||||
// Sign transaction - ready to execute
|
||||
|
||||
// Prepare notifications after execution
|
||||
|
||||
// Broadcast transaction
|
||||
|
||||
// Delete inputs, adjust balance for addresses
|
||||
|
||||
// Send notifications
|
||||
|
||||
// Ledger entries
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+6
-2
@@ -61,7 +61,11 @@ namespace Swarmops.Logic.Resources {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to [BitcoinAmount] millicoin was just transferred to your address by [OrganizationName]..
|
||||
/// Looks up a localized string similar to [BitcoinAmount] microcoin ("bits") was just transferred to your bitcoin address ([BitcoinAddress]) by [OrganizationName] for the following:
|
||||
///
|
||||
///[PreformattedText]
|
||||
///
|
||||
///You should already see the transaction pending confirmations and it should achieve spendability within about an hour (six normal confirmations)..
|
||||
/// </summary>
|
||||
internal static string Bitcoin_PaidOut_Body {
|
||||
get {
|
||||
@@ -70,7 +74,7 @@ namespace Swarmops.Logic.Resources {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to [[OrganizationName]] Funds paid using bitcoin: [CurrencyCode] [BudgetAmountFloat].
|
||||
/// Looks up a localized string similar to [[OrganizationName]] Funds paid using bitcoin: [CurrencyCode] [AmountFloat].
|
||||
/// </summary>
|
||||
internal static string Bitcoin_PaidOut_Subject {
|
||||
get {
|
||||
|
||||
@@ -150,10 +150,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>[BitcoinAmount] millicoin was just transferred to your address by [OrganizationName].</value>
|
||||
<value>[BitcoinAmount] microcoin ("bits") was just transferred to your bitcoin address ([BitcoinAddress]) by [OrganizationName] for the following:
|
||||
|
||||
[PreformattedText]
|
||||
|
||||
You should already see the transaction pending confirmations 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] [BudgetAmountFloat]</value>
|
||||
<value>[[OrganizationName]] Funds paid using bitcoin: [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.
|
||||
|
||||
@@ -216,6 +216,12 @@ namespace Swarmops.Logic.Support
|
||||
}
|
||||
|
||||
|
||||
public static string BlockchainSwarmopsApiKey
|
||||
{
|
||||
get { return "77e11665-14a9-4d72-9111-7dce7a6748f6"; }
|
||||
}
|
||||
|
||||
|
||||
public static string EtcFolder
|
||||
{
|
||||
get
|
||||
|
||||
@@ -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 */
|
||||
/*
|
||||
@@ -39,8 +39,6 @@ namespace Swarmops.Frontend.Pages.v5.Financial
|
||||
}
|
||||
}*/
|
||||
|
||||
// TODO: This page needs a check on whether currentorganization.financialaccounts.assetbitcoinhot is null
|
||||
|
||||
this.PageTitle = Resources.Pages.Financial.Donate_PageTitle;
|
||||
this.InfoBoxLiteral = Resources.Pages.Financial.Donate_Info;
|
||||
this.LabelStatus.Text = Resources.Pages.Financial.Donate_StatusInitial;
|
||||
|
||||
Reference in New Issue
Block a user