From ac50462cb6147d40f0f716613bd11563f065239d Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Tue, 28 Nov 2017 10:08:30 +0100 Subject: [PATCH] Moved second ledger entry to BSO --- .../ReturnBitcoinEchoUtxoOrder.cs | 12 +++++ Logic/Support/BlockchainTransaction.cs | 53 +++++++++++++++++-- Site/Pages/v5/Admin/BitcoinEchoTest.aspx.cs | 15 +----- 3 files changed, 64 insertions(+), 16 deletions(-) diff --git a/Logic/Support/BackendServices/ReturnBitcoinEchoUtxoOrder.cs b/Logic/Support/BackendServices/ReturnBitcoinEchoUtxoOrder.cs index cdb5c99f..d3a5f6be 100644 --- a/Logic/Support/BackendServices/ReturnBitcoinEchoUtxoOrder.cs +++ b/Logic/Support/BackendServices/ReturnBitcoinEchoUtxoOrder.cs @@ -35,7 +35,10 @@ namespace Swarmops.Logic.Support.BackendServices string returnAddress = BitcoinUtility.GetInputAddressesForTransaction(BitcoinChain.Cash, utxoToReturn.TransactionHash)[0]; // assumes at least one input address -- not coinbase + // Return the money + BitcoinTransactionInputs inputs = utxoToReturn.AsInputs; + Int64 satoshisToReturn = utxoToReturn.AmountSatoshis; Coin[] coins = inputs.Coins; ICoin[] iCoins = coins; @@ -53,6 +56,15 @@ namespace Swarmops.Logic.Support.BackendServices BitcoinUtility.BroadcastTransaction(tx, BitcoinChain.Cash); utxoToReturn.Delete(); utxoAddress.UpdateTotal(); + + // Update the ledger + + string tx2Description = "Bitcoin echo test repayment"; + FinancialTransaction ledgerTx2 = FinancialTransaction.Create(this.Organization, + DateTime.UtcNow, tx2Description); + ledgerTx2.AddRow(this.Organization.FinancialAccounts.DebtsOther, satoshisToReturn, this.Person); + ledgerTx2.AddRow(this.Organization.FinancialAccounts.AssetsBitcoinHot, -satoshisToReturn, this.Person); + ledgerTx2.BlockchainHash = tx.GetHash().ToString(); } public int UtxoIdentity { get; set; } diff --git a/Logic/Support/BlockchainTransaction.cs b/Logic/Support/BlockchainTransaction.cs index 1f96958d..607e389a 100644 --- a/Logic/Support/BlockchainTransaction.cs +++ b/Logic/Support/BlockchainTransaction.cs @@ -15,7 +15,7 @@ namespace Swarmops.Logic.Support System.DateTime transactionDateTimeUtc = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); thisTx.TransactionDateTimeUtc = transactionDateTimeUtc.AddSeconds(Int64.Parse((string)json["time"])); - thisTx.TransactionHash = (string) json["hash"]; + thisTx.TransactionHash = (string)json["hash"]; Int64 satoshiCount = 0; thisTx.Inputs = new List(); @@ -23,8 +23,8 @@ namespace Swarmops.Logic.Support foreach (JObject inputRow in json["inputs"]) { BlockchainTransactionRow newRow = - BlockchainTransactionRow.FromBlockchainInfoJson ((JObject) inputRow["prev_out"]); - thisTx.Inputs.Add (newRow); + BlockchainTransactionRow.FromBlockchainInfoJson((JObject)inputRow["prev_out"]); + thisTx.Inputs.Add(newRow); satoshiCount += newRow.ValueSatoshis; } foreach (JObject outputRow in json["out"]) @@ -40,12 +40,48 @@ namespace Swarmops.Logic.Support return thisTx; } + + static public BlockchainTransaction FromInsightInfoJson(JObject json) + { + BlockchainTransaction thisTx = new BlockchainTransaction(); + + System.DateTime transactionDateTimeUtc = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); + thisTx.TransactionDateTimeUtc = transactionDateTimeUtc.AddSeconds(Int64.Parse((string)json["time"])); + + thisTx.TransactionHash = (string)json["txid"]; + + Int64 satoshiCount = 0; + thisTx.Inputs = new List(); + thisTx.Outputs = new List(); + foreach (JObject inputRow in json["vin"]) + { + BlockchainTransactionRow newRow = + BlockchainTransactionRow.FromInsightInfoJson(inputRow, true); + thisTx.Inputs.Add(newRow); + satoshiCount += newRow.ValueSatoshis; + } + foreach (JObject outputRow in json["vout"]) + { + BlockchainTransactionRow newRow = + BlockchainTransactionRow.FromInsightInfoJson(outputRow); + thisTx.Outputs.Add(newRow); + satoshiCount -= newRow.ValueSatoshis; + } + + thisTx.FeeSatoshis = satoshiCount; // inputs minus outputs equals fee + + return thisTx; + } + + + public DateTime TransactionDateTimeUtc { get; private set; } public Int64 FeeSatoshis { get; private set; } public string TransactionHash { get; private set; } public List Inputs { get; private set; } public List Outputs { get; private set; } + } public class BlockchainTransactionRow @@ -61,6 +97,17 @@ namespace Swarmops.Logic.Support return newRow; } + static public BlockchainTransactionRow FromInsightInfoJson(JObject json, bool inRow = false) + { + BlockchainTransactionRow newRow = new BlockchainTransactionRow(); + newRow.Address = (string)json["addr"]; + newRow.ValueSatoshis = Int64.Parse((string)json["valueSat"]); + newRow.Index = Int32.Parse((string)json["n"]); + newRow.Spent = inRow && (bool)(json["spentTxId"] == null? false: true); + + return newRow; + } + public string Address { get; private set; } public bool Spent { get; private set; } public Int64 ValueSatoshis { get; private set; } diff --git a/Site/Pages/v5/Admin/BitcoinEchoTest.aspx.cs b/Site/Pages/v5/Admin/BitcoinEchoTest.aspx.cs index 54048c13..b0b92324 100644 --- a/Site/Pages/v5/Admin/BitcoinEchoTest.aspx.cs +++ b/Site/Pages/v5/Admin/BitcoinEchoTest.aspx.cs @@ -117,7 +117,6 @@ namespace Swarmops.Frontend.Pages.v5.Admin backendOrder.Create(authData.CurrentOrganization, authData.CurrentUser); string tx1Description = "Bitcoin technical echo test (will be repaid immediately)"; - string tx2Description = "Bitcoin echo test repayment"; if (authData.CurrentOrganization.Currency.IsBitcoinCash) @@ -130,11 +129,7 @@ namespace Swarmops.Frontend.Pages.v5.Admin ledgerTx1.AddRow(authData.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot, satoshisReceived, authData.CurrentUser); ledgerTx1.BlockchainHash = txHash; - FinancialTransaction ledgerTx2 = FinancialTransaction.Create(authData.CurrentOrganization, - DateTime.UtcNow, tx2Description); - ledgerTx2.AddRow(authData.CurrentOrganization.FinancialAccounts.DebtsOther, satoshisReceived, authData.CurrentUser); - ledgerTx2.AddRow(authData.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot, -satoshisReceived, authData.CurrentUser); - // ledgerTx2.BlockchainHash = returnTxHash; // TODO? Unknown at this point + // The return payment will be logged when made, so its hash can be recorded if (satoshisReceived % 100 == 0) { @@ -158,16 +153,10 @@ namespace Swarmops.Frontend.Pages.v5.Admin ledgerTx1.AddRow(authData.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot, orgNativeCents, authData.CurrentUser).AmountForeignCents = new Swarmops.Logic.Financial.Money(satoshisReceived, Currency.BitcoinCash); ledgerTx1.BlockchainHash = txHash; - FinancialTransaction ledgerTx2 = FinancialTransaction.Create(authData.CurrentOrganization, - DateTime.UtcNow, tx2Description); - ledgerTx2.AddRow(authData.CurrentOrganization.FinancialAccounts.DebtsOther, orgNativeCents, authData.CurrentUser); - ledgerTx2.AddRow(authData.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot, -orgNativeCents, authData.CurrentUser).AmountForeignCents = new Swarmops.Logic.Financial.Money(-satoshisReceived, Currency.BitcoinCash); - // ledgerTx2.BlockchainHash = returnTxHash; // TODO: How do we solve this, the recording of the return hash? It's not known here + // The second transaction is logged when executed in the back-end order successMessage = string.Format(Resources.Pages.Admin.BitcoinEchoTest_FundsReceived, authData.CurrentOrganization.Currency.DisplayCode, orgNativeCents/100.0, satoshisReceived/100.0); - - // TODO: Second tx } return new AjaxCallResult() {DisplayMessage = successMessage, Success = true};