From c2f272149cbfc42dd49f6e586ed9a5049e4b3a3e Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Wed, 15 Nov 2017 14:36:16 +0100 Subject: [PATCH] Added more Bitcoin Core / Cash support --- Backend/BackendLoop.cs | 2 +- Logic/Financial/BitcoinUtility.cs | 18 ++++++------ Logic/Financial/Currency.cs | 28 +++++++++++++++---- Logic/Financial/Payouts.cs | 18 ++++++------ Site/Pages/v5/Admin/BitcoinEchoTest.aspx.cs | 8 +++--- Site/Pages/v5/Admin/OrgSettings.aspx.cs | 2 +- Site/Pages/v5/Financial/Donate.aspx.cs | 8 +++--- .../Ledgers/Json-BitcoinHotwalletData.aspx.cs | 4 +-- Site/Pages/v5/Public/Initialize.aspx.cs | 3 +- 9 files changed, 54 insertions(+), 37 deletions(-) diff --git a/Backend/BackendLoop.cs b/Backend/BackendLoop.cs index 28d02590..7caec5e4 100644 --- a/Backend/BackendLoop.cs +++ b/Backend/BackendLoop.cs @@ -1046,7 +1046,7 @@ namespace Swarmops.Backend Currency currency = hotAddress.Organization.Currency; json["OrganizationId"] = hotAddress.OrganizationId.ToString(); json["Currency"] = currency.Code; - Swarmops.Logic.Financial.Money organizationCents = new Money(satoshis, Currency.Bitcoin).ToCurrency(currency); + Swarmops.Logic.Financial.Money organizationCents = new Money(satoshis, Currency.BitcoinCore).ToCurrency(currency); json["Satoshis"] = satoshis.ToString(); json["Cents"] = organizationCents.Cents.ToString(); json["CentsFormatted"] = String.Format("{0:N2}", organizationCents.Cents/100.0); diff --git a/Logic/Financial/BitcoinUtility.cs b/Logic/Financial/BitcoinUtility.cs index 1a1222ee..dcac42fa 100644 --- a/Logic/Financial/BitcoinUtility.cs +++ b/Logic/Financial/BitcoinUtility.cs @@ -349,7 +349,7 @@ namespace Swarmops.Logic.Financial } Organization organization = parent.Organization; - bool organizationLedgerUsesBitcoin = organization.Currency.IsBitcoin; + bool organizationLedgerUsesBitcoin = organization.Currency.IsBitcoinCore; JObject addressData = JObject.Parse( new WebClient().DownloadString("https://blockchain.info/address/" + address + @@ -403,7 +403,7 @@ namespace Swarmops.Logic.Financial if (!organizationLedgerUsesBitcoin) { Money nativeMoney = row.AmountForeignCents; - if (nativeMoney != null && nativeMoney.Currency.IsBitcoin) // it damn well should be, but just checking + if (nativeMoney != null && nativeMoney.Currency.IsBitcoinCore) // it damn well should be, but just checking { satoshisLookup[row.AmountCents] = row.AmountForeignCents.Cents; } @@ -436,7 +436,7 @@ namespace Swarmops.Logic.Financial else { Int64 ledgerCents = - new Money (inputRow.ValueSatoshis, Currency.Bitcoin, ourTx.DateTime).ToCurrency ( + new Money (inputRow.ValueSatoshis, Currency.BitcoinCore, ourTx.DateTime).ToCurrency ( organization.Currency).Cents; transactionReconstructedRows[financialAccountId] += -ledgerCents; // note the negation! @@ -468,7 +468,7 @@ namespace Swarmops.Logic.Financial else { Int64 ledgerCents = - new Money(outputRow.ValueSatoshis, Currency.Bitcoin, ourTx.DateTime).ToCurrency( + new Money(outputRow.ValueSatoshis, Currency.BitcoinCore, ourTx.DateTime).ToCurrency( organization.Currency).Cents; transactionReconstructedRows[financialAccountId] += ledgerCents; @@ -491,7 +491,7 @@ namespace Swarmops.Logic.Financial else { Int64 feeLedgerCents = - new Money (blockchainTx.FeeSatoshis, Currency.Bitcoin, + new Money (blockchainTx.FeeSatoshis, Currency.BitcoinCore, blockchainTx.TransactionDateTimeUtc).ToCurrency (organization.Currency).Cents; transactionReconstructedRows[organization.FinancialAccounts.CostsBitcoinFees.Identity] = feeLedgerCents; @@ -520,16 +520,16 @@ namespace Swarmops.Logic.Financial if (satoshisLookup.ContainsKey (row.AmountCents)) { - row.AmountForeignCents = new Money(satoshisLookup[row.AmountCents], Currency.Bitcoin, ourTx.DateTime); + row.AmountForeignCents = new Money(satoshisLookup[row.AmountCents], Currency.BitcoinCore, ourTx.DateTime); } else if (satoshisLookup.ContainsKey (-row.AmountCents)) // the negative counterpart { - row.AmountForeignCents = new Money(-satoshisLookup[-row.AmountCents], Currency.Bitcoin, ourTx.DateTime); + row.AmountForeignCents = new Money(-satoshisLookup[-row.AmountCents], Currency.BitcoinCore, ourTx.DateTime); } else { // There's a last case which may happen if the row is an addition to a previous row; if so, calculate - row.AmountForeignCents = new Money(row.AmountCents, organization.Currency, ourTx.DateTime).ToCurrency (Currency.Bitcoin); + row.AmountForeignCents = new Money(row.AmountCents, organization.Currency, ourTx.DateTime).ToCurrency (Currency.BitcoinCore); } } } @@ -658,7 +658,7 @@ namespace Swarmops.Logic.Financial // Make a small test payment to a multisig address TransactionBuilder txBuilder = new TransactionBuilder(); - Int64 satoshis = new Money(100, Currency.FromCode ("SEK")).ToCurrency (Currency.Bitcoin).Cents; + Int64 satoshis = new Money(100, Currency.FromCode ("SEK")).ToCurrency (Currency.BitcoinCore).Cents; BitcoinTransactionInputs inputs = null; Int64 satoshisMaximumAnticipatedFees = BitcoinUtility.GetRecommendedFeePerThousandBytesSatoshis() * 20; // assume max 20k transaction size diff --git a/Logic/Financial/Currency.cs b/Logic/Financial/Currency.cs index 98fa3072..3b77b342 100644 --- a/Logic/Financial/Currency.cs +++ b/Logic/Financial/Currency.cs @@ -22,20 +22,36 @@ namespace Swarmops.Logic.Financial return "µBTC"; // HTML entity -- for display only } + if (base.Code == "BCH") + { + return "µBCH"; // HTML entity as above + } + return base.Code; } } - public bool IsBitcoin + public bool IsBitcoinCore { get { return (base.Code == "BTC"); } } - public static Currency Bitcoin + + public bool IsBitcoinCash + { + get { return (base.Code == "BCH"); } + } + + public static Currency BitcoinCore { get { return Currency.FromCode ("BTC"); } } + public static Currency BitcoinCash + { + get { return Currency.FromCode("BCH"); } + } + public double GetConversionRate (Currency otherCurrency) { @@ -45,21 +61,21 @@ namespace Swarmops.Logic.Financial public double GetConversionRate (Currency otherCurrency, DateTime valuationDateTime) { - if (this.IsBitcoin) + if (this.IsBitcoinCore) { return SwarmDb.GetDatabaseForReading() .GetCurrencyExchangeRate (otherCurrency.Identity, this.Identity, valuationDateTime); } - else if (otherCurrency.IsBitcoin) + else if (otherCurrency.IsBitcoinCore) { return 1.0 / SwarmDb.GetDatabaseForReading() .GetCurrencyExchangeRate (this.Identity, otherCurrency.Identity, valuationDateTime); } else { - // Neither is bitcoin, so go via bitcoin + // Neither is bitcoin core, so go via bitcoin core - int bitcoinCurrencyId = Currency.Bitcoin.Identity; + int bitcoinCurrencyId = Currency.BitcoinCore.Identity; double thisPerBitcoin = SwarmDb.GetDatabaseForReading() .GetCurrencyExchangeRate (this.Identity, bitcoinCurrencyId, valuationDateTime); double otherPerBitcoin = SwarmDb.GetDatabaseForReading() diff --git a/Logic/Financial/Payouts.cs b/Logic/Financial/Payouts.cs index 5b36676c..8219dee5 100644 --- a/Logic/Financial/Payouts.cs +++ b/Logic/Financial/Payouts.cs @@ -542,7 +542,7 @@ namespace Swarmops.Logic.Financial // Find the amount of satoshis for this payout - if (organization.Currency.IsBitcoin) + if (organization.Currency.IsBitcoinCore) { satoshiPayoutLookup[payout.ProtoIdentity] = payout.AmountCents; nativeCentsPayoutLookup[payout.ProtoIdentity] = payout.AmountCents; @@ -553,7 +553,7 @@ namespace Swarmops.Logic.Financial { // Convert currency Money payoutAmount = new Money(payout.AmountCents, organization.Currency); - Int64 satoshis = payoutAmount.ToCurrency(Currency.Bitcoin).Cents; + Int64 satoshis = payoutAmount.ToCurrency(Currency.BitcoinCore).Cents; satoshiPayoutLookup[payout.ProtoIdentity] = satoshis; nativeCentsPayoutLookup[payout.ProtoIdentity] = payout.AmountCents; satoshisTotal += satoshis; @@ -618,7 +618,7 @@ namespace Swarmops.Logic.Financial secondaryStrings["AmountMissingMicrocoinsFloat"] = ((satoshisTotal - satoshisAvailable + satoshisMaximumAnticipatedFees) / 100.0).ToString("N2"); - if (organization.Currency.IsBitcoin) + if (organization.Currency.IsBitcoinCore) { secondaryStrings["AmountNeededFloat"] = ((satoshisTotal + satoshisMaximumAnticipatedFees) / 100.0).ToString("N2"); secondaryStrings["AmountWalletFloat"] = (satoshisAvailable/100.0).ToString ("N2"); @@ -628,9 +628,9 @@ namespace Swarmops.Logic.Financial // convert to org native currency secondaryStrings["AmountNeededFloat"] = - (new Money (satoshisTotal, Currency.Bitcoin).ToCurrency (organization.Currency).Cents/100.0).ToString ("N2"); + (new Money (satoshisTotal, Currency.BitcoinCore).ToCurrency (organization.Currency).Cents/100.0).ToString ("N2"); secondaryStrings["AmountWalletFloat"] = - (new Money (satoshisAvailable, Currency.Bitcoin).ToCurrency (organization.Currency).Cents/100.0).ToString ("N2"); + (new Money (satoshisAvailable, Currency.BitcoinCore).ToCurrency (organization.Currency).Cents/100.0).ToString ("N2"); } OutboundComm.CreateNotification (organization, @@ -787,7 +787,7 @@ namespace Swarmops.Logic.Financial masterPrimaryStrings[NotificationString.OrganizationName] = organization.Name; masterPrimaryStrings[NotificationString.CurrencyCode] = organization.Currency.DisplayCode; masterSecondaryStrings["AmountFloat"] = - (new Swarmops.Logic.Financial.Money (satoshisUsed, Currency.Bitcoin).ToCurrency ( + (new Swarmops.Logic.Financial.Money (satoshisUsed, Currency.BitcoinCore).ToCurrency ( organization.Currency).Cents/100.0).ToString ("N2", CultureInfo.InvariantCulture); masterSecondaryStrings["BitcoinAmountFloat"] = (satoshisUsed/100.0).ToString ("N2", CultureInfo.InvariantCulture); masterSecondaryStrings["PaymentCount"] = bitcoinPayouts.Count.ToString("N0", CultureInfo.InvariantCulture); @@ -800,7 +800,7 @@ namespace Swarmops.Logic.Financial FinancialTransaction ledgerTransaction = FinancialTransaction.Create (organization, utcNow, "Bitcoin automated payout"); - if (organization.Currency.IsBitcoin) + if (organization.Currency.IsBitcoinCore) { ledgerTransaction.AddRow(organization.FinancialAccounts.AssetsBitcoinHot, -(masterPayoutPrototype.AmountCents + feeSatoshis), null); ledgerTransaction.AddRow (organization.FinancialAccounts.CostsBitcoinFees, feeSatoshis, null); @@ -809,8 +809,8 @@ namespace Swarmops.Logic.Financial { // If the ledger isn't using bitcoin natively, we need to translate the miner fee paid to ledger cents before entering it into ledger - Int64 feeCentsLedger = new Money (feeSatoshis, Currency.Bitcoin).ToCurrency (organization.Currency).Cents; - ledgerTransaction.AddRow(organization.FinancialAccounts.AssetsBitcoinHot, -(masterPayoutPrototype.AmountCents + feeCentsLedger), null).AmountForeignCents = new Money(satoshisUsed, Currency.Bitcoin); + Int64 feeCentsLedger = new Money (feeSatoshis, Currency.BitcoinCore).ToCurrency (organization.Currency).Cents; + ledgerTransaction.AddRow(organization.FinancialAccounts.AssetsBitcoinHot, -(masterPayoutPrototype.AmountCents + feeCentsLedger), null).AmountForeignCents = new Money(satoshisUsed, Currency.BitcoinCore); ledgerTransaction.AddRow (organization.FinancialAccounts.CostsBitcoinFees, feeCentsLedger, null); } diff --git a/Site/Pages/v5/Admin/BitcoinEchoTest.aspx.cs b/Site/Pages/v5/Admin/BitcoinEchoTest.aspx.cs index 5b31c1c8..93ad8a32 100644 --- a/Site/Pages/v5/Admin/BitcoinEchoTest.aspx.cs +++ b/Site/Pages/v5/Admin/BitcoinEchoTest.aspx.cs @@ -112,10 +112,10 @@ namespace Swarmops.Frontend.Pages.v5.Admin } Swarmops.Logic.Financial.Money moneyReceived = new Swarmops.Logic.Financial.Money(satoshisReceived, - Currency.Bitcoin); + Currency.BitcoinCore); // Make sure that the hotwallet native currency is bitcoin - authData.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot.ForeignCurrency = Currency.Bitcoin; + authData.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot.ForeignCurrency = Currency.BitcoinCore; // Create success message and ledger transaction string successMessage = string.Empty; @@ -124,7 +124,7 @@ namespace Swarmops.Frontend.Pages.v5.Admin string returnAddress = BitcoinUtility.GetInputAddressesForTransaction(txHash) [0]; // assumes at least one input address - if (authData.CurrentOrganization.Currency.IsBitcoin) + if (authData.CurrentOrganization.Currency.IsBitcoinCore) { // The ledger is native bitcoin, so units are Satoshis @@ -156,7 +156,7 @@ namespace Swarmops.Frontend.Pages.v5.Admin FinancialTransaction ledgerTx = FinancialTransaction.Create(authData.CurrentOrganization, DateTime.UtcNow, "Bitcoin echo test (will be repaid immediately)"); ledgerTx.AddRow(authData.CurrentOrganization.FinancialAccounts.DebtsOther, -orgNativeCents, authData.CurrentUser); - ledgerTx.AddRow(authData.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot, orgNativeCents, authData.CurrentUser).AmountForeignCents = new Swarmops.Logic.Financial.Money(satoshisReceived, Currency.Bitcoin); + ledgerTx.AddRow(authData.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot, orgNativeCents, authData.CurrentUser).AmountForeignCents = new Swarmops.Logic.Financial.Money(satoshisReceived, Currency.BitcoinCore); ledgerTx.BlockchainHash = txHash; successMessage = string.Format(Resources.Pages.Admin.BitcoinEchoTest_FundsReceived, diff --git a/Site/Pages/v5/Admin/OrgSettings.aspx.cs b/Site/Pages/v5/Admin/OrgSettings.aspx.cs index fad78035..0d88b4e4 100644 --- a/Site/Pages/v5/Admin/OrgSettings.aspx.cs +++ b/Site/Pages/v5/Admin/OrgSettings.aspx.cs @@ -430,7 +430,7 @@ namespace Swarmops.Frontend.Pages.Admin { get { - return CurrentOrganization.Currency.IsBitcoin.ToString().ToLowerInvariant(); // the tostring, tolower makes 'False' into 'false' the way JS wants it + return CurrentOrganization.Currency.IsBitcoinCore.ToString().ToLowerInvariant(); // the tostring, tolower makes 'False' into 'false' the way JS wants it } } diff --git a/Site/Pages/v5/Financial/Donate.aspx.cs b/Site/Pages/v5/Financial/Donate.aspx.cs index 1ae5ed26..865c42a4 100644 --- a/Site/Pages/v5/Financial/Donate.aspx.cs +++ b/Site/Pages/v5/Financial/Donate.aspx.cs @@ -117,10 +117,10 @@ namespace Swarmops.Frontend.Pages.v5.Financial } Swarmops.Logic.Financial.Money moneyReceived = new Swarmops.Logic.Financial.Money(satoshisReceived, - Currency.Bitcoin); + Currency.BitcoinCore); // Make sure that the hotwallet native currency is bitcoin - authData.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot.ForeignCurrency = Currency.Bitcoin; + authData.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot.ForeignCurrency = Currency.BitcoinCore; // Create success message and ledger transaction string successMessage = string.Empty; @@ -139,7 +139,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial // This exception is expected - the transaction should not yet exist } - if (authData.CurrentOrganization.Currency.IsBitcoin) + if (authData.CurrentOrganization.Currency.IsBitcoinCore) { // The ledger is native bitcoin, so units are Satoshis @@ -168,7 +168,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial FinancialTransaction ledgerTx = FinancialTransaction.Create(authData.CurrentOrganization, DateTime.UtcNow, "Donation (bitcoin to hotwallet)"); ledgerTx.AddRow(authData.CurrentOrganization.FinancialAccounts.IncomeDonations, -orgNativeCents, authData.CurrentUser); - ledgerTx.AddRow(authData.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot, orgNativeCents, authData.CurrentUser).AmountForeignCents = new Swarmops.Logic.Financial.Money(satoshisReceived, Currency.Bitcoin); + ledgerTx.AddRow(authData.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot, orgNativeCents, authData.CurrentUser).AmountForeignCents = new Swarmops.Logic.Financial.Money(satoshisReceived, Currency.BitcoinCore); ledgerTx.BlockchainHash = txHash; successMessage = string.Format (Resources.Pages.Financial.Donate_FundsReceived, diff --git a/Site/Pages/v5/Ledgers/Json-BitcoinHotwalletData.aspx.cs b/Site/Pages/v5/Ledgers/Json-BitcoinHotwalletData.aspx.cs index 3525050c..94e6ad62 100644 --- a/Site/Pages/v5/Ledgers/Json-BitcoinHotwalletData.aspx.cs +++ b/Site/Pages/v5/Ledgers/Json-BitcoinHotwalletData.aspx.cs @@ -31,9 +31,9 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers StringBuilder result = new StringBuilder(16384); double conversionRate = 1.0; - if (!this._authenticationData.CurrentOrganization.Currency.IsBitcoin) + if (!this._authenticationData.CurrentOrganization.Currency.IsBitcoinCore) { - long fiatCentsPerCoin = new Money(100000000, Currency.Bitcoin).ToCurrency (_authenticationData.CurrentOrganization.Currency).Cents; + long fiatCentsPerCoin = new Money(100000000, Currency.BitcoinCore).ToCurrency (_authenticationData.CurrentOrganization.Currency).Cents; conversionRate = fiatCentsPerCoin/100000000.0; // on satoshi level } diff --git a/Site/Pages/v5/Public/Initialize.aspx.cs b/Site/Pages/v5/Public/Initialize.aspx.cs index f7c726ef..18d777a1 100644 --- a/Site/Pages/v5/Public/Initialize.aspx.cs +++ b/Site/Pages/v5/Public/Initialize.aspx.cs @@ -480,7 +480,8 @@ namespace Swarmops.Frontend.Pages.v5.Public Currency.CreateFiat("ISK", "Icelandic Krona", string.Empty); Currency.CreateFiat("CHF", "Swiss Franc", string.Empty); Currency.CreateFiat("GBP", "Pounds Sterling", "£"); - Currency.CreateFiat("BTC", "Bitcoin", "฿"); + Currency.CreateCrypto("BTC", "Bitcoin Core", "฿"); + Currency.CreateCrypto("BCH", "Bitcoin Cash", "฿"); // Fetch the first set of exchange rates, completing the currency collection