From d8291b6cdbd5a85d9ee0a071ce86b32da82000cb Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sat, 16 Jan 2016 23:37:18 +0100 Subject: [PATCH] REFACTOR: 'NativeCents' to 'ForeignCents' --- Database/Database-Financials.cs | 4 ++-- Logic/Financial/BitcoinUtility.cs | 4 ++-- Logic/Financial/FinancialAccount.cs | 4 ++-- Logic/Financial/FinancialTransactionRow.cs | 19 ++++++++++++++----- Logic/Financial/Payouts.cs | 2 +- Site5/Pages/v5/Admin/BitcoinEchoTest.aspx.cs | 4 ++-- Site5/Pages/v5/Financial/Donate.aspx.cs | 4 ++-- 7 files changed, 25 insertions(+), 16 deletions(-) diff --git a/Database/Database-Financials.cs b/Database/Database-Financials.cs index cfd641d5..8f44efe8 100644 --- a/Database/Database-Financials.cs +++ b/Database/Database-Financials.cs @@ -1069,7 +1069,7 @@ namespace Swarmops.Database - public Int64 GetFinancialTransactionRowNativeAmountCents(int financialTransactionRowId, int currencyId) + public Int64 GetFinancialTransactionRowAmountForeignCents(int financialTransactionRowId, int currencyId) { using (DbConnection connection = GetMySqlDbConnection()) { @@ -1094,7 +1094,7 @@ namespace Swarmops.Database - public void SetFinancialTransactionRowNativeAmountCents(int financialTransactionRowId, int currencyId, Int64 nativeAmountCents) + public void SetFinancialTransactionRowAmountForeignCents(int financialTransactionRowId, int currencyId, Int64 nativeAmountCents) { using (DbConnection connection = GetMySqlDbConnection()) { diff --git a/Logic/Financial/BitcoinUtility.cs b/Logic/Financial/BitcoinUtility.cs index 724f789c..30847d59 100644 --- a/Logic/Financial/BitcoinUtility.cs +++ b/Logic/Financial/BitcoinUtility.cs @@ -387,10 +387,10 @@ namespace Swarmops.Logic.Financial // this is a known blockchain row, note its ledgered value in satoshis if (!organizationLedgerUsesBitcoin) { - Money nativeMoney = row.NativeAmountCents; + Money nativeMoney = row.AmountForeignCents; if (nativeMoney.Currency.IsBitcoin) // it damn well should be, but just checking { - satoshisLookup[row.AmountCents] = row.NativeAmountCents.Cents; + satoshisLookup[row.AmountCents] = row.AmountForeignCents.Cents; } } } diff --git a/Logic/Financial/FinancialAccount.cs b/Logic/Financial/FinancialAccount.cs index 6b52c07f..73ddf20e 100644 --- a/Logic/Financial/FinancialAccount.cs +++ b/Logic/Financial/FinancialAccount.cs @@ -115,11 +115,11 @@ namespace Swarmops.Logic.Financial } - public Currency NativeCurrency + public Currency ForeignCurrency { get { - int currencyId = OptionalData.GetOptionalDataInt (ObjectOptionalDataType.FinancialAccountNativeCurrency); + int currencyId = OptionalData.GetOptionalDataInt (ObjectOptionalDataType.FinancialAccountNativeCurrency); // "Native" word is legacy return (currencyId != 0 ? Currency.FromIdentity (currencyId) : null); } set diff --git a/Logic/Financial/FinancialTransactionRow.cs b/Logic/Financial/FinancialTransactionRow.cs index f54c986e..a4106722 100644 --- a/Logic/Financial/FinancialTransactionRow.cs +++ b/Logic/Financial/FinancialTransactionRow.cs @@ -59,18 +59,27 @@ namespace Swarmops.Logic.Financial } } - public Money NativeAmountCents + public Money AmountForeignCents { get { - Currency currency = this.Account.NativeCurrency; + Currency currency = this.Account.ForeignCurrency; int currencyId = currency.Identity; - Int64 nativeCents = SwarmDb.GetDatabaseForReading().GetFinancialTransactionRowNativeAmountCents (this.Identity, currencyId); - return new Money(nativeCents, currency, this.CreatedDateTime); // includes the valuation datetime, as it's a non-org currency + Int64 foreignCents = SwarmDb.GetDatabaseForReading().GetFinancialTransactionRowAmountForeignCents (this.Identity, currencyId); + return new Money(foreignCents, currency, this.CreatedDateTime); // includes the valuation datetime, as it's a non-org currency } set { - SwarmDb.GetDatabaseForWriting().SetFinancialTransactionRowNativeAmountCents (this.Identity, value.Currency.Identity, value.Cents); + if (this.Account.ForeignCurrency == null) + { + this.Account.ForeignCurrency = value.Currency; + } + else if (this.Account.ForeignCurrency.Identity != value.Currency.Identity) + { + throw new InvalidOperationException("Trying to assign foreign-currency value to account with a different foreign currency set"); + } + + SwarmDb.GetDatabaseForWriting().SetFinancialTransactionRowAmountForeignCents (this.Identity, value.Currency.Identity, value.Cents); } } diff --git a/Logic/Financial/Payouts.cs b/Logic/Financial/Payouts.cs index 7370f849..cc2b7f97 100644 --- a/Logic/Financial/Payouts.cs +++ b/Logic/Financial/Payouts.cs @@ -820,7 +820,7 @@ 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).NativeAmountCents = new Money(satoshisUsed, Currency.Bitcoin); + ledgerTransaction.AddRow(organization.FinancialAccounts.AssetsBitcoinHot, -(masterPayoutPrototype.AmountCents + feeCentsLedger), null).AmountForeignCents = new Money(satoshisUsed, Currency.Bitcoin); ledgerTransaction.AddRow (organization.FinancialAccounts.CostsBitcoinFees, feeCentsLedger, null); } diff --git a/Site5/Pages/v5/Admin/BitcoinEchoTest.aspx.cs b/Site5/Pages/v5/Admin/BitcoinEchoTest.aspx.cs index acf18239..054dee31 100644 --- a/Site5/Pages/v5/Admin/BitcoinEchoTest.aspx.cs +++ b/Site5/Pages/v5/Admin/BitcoinEchoTest.aspx.cs @@ -115,7 +115,7 @@ namespace Swarmops.Frontend.Pages.v5.Admin Currency.Bitcoin); // Make sure that the hotwallet native currency is bitcoin - authData.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot.NativeCurrency = Currency.Bitcoin; + authData.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot.ForeignCurrency = Currency.Bitcoin; // Create success message and ledger transaction string successMessage = string.Empty; @@ -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).NativeAmountCents = 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.Bitcoin); ledgerTx.BlockchainHash = txHash; successMessage = string.Format(Resources.Pages.Admin.BitcoinEchoTest_FundsReceived, diff --git a/Site5/Pages/v5/Financial/Donate.aspx.cs b/Site5/Pages/v5/Financial/Donate.aspx.cs index e8decfad..1e107467 100644 --- a/Site5/Pages/v5/Financial/Donate.aspx.cs +++ b/Site5/Pages/v5/Financial/Donate.aspx.cs @@ -121,7 +121,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial Currency.Bitcoin); // Make sure that the hotwallet native currency is bitcoin - authData.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot.NativeCurrency = Currency.Bitcoin; + authData.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot.ForeignCurrency = Currency.Bitcoin; // Create success message and ledger transaction string successMessage = string.Empty; @@ -169,7 +169,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).NativeAmountCents = 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.Bitcoin); ledgerTx.BlockchainHash = txHash; successMessage = string.Format (Resources.Pages.Financial.Donate_FundsReceived,