diff --git a/Backend/BackendLoop.cs b/Backend/BackendLoop.cs index f821e5fa..d9619d28 100644 --- a/Backend/BackendLoop.cs +++ b/Backend/BackendLoop.cs @@ -841,6 +841,20 @@ namespace Swarmops.Backend account.CheckForexProfitLoss(); } } + + // Also check the bitcoin hotwallets for forex profit/loss as a special case of the above. + + Organizations allOrganizations = Organizations.GetAll(); + + foreach (Organization organization in allOrganizations) + { + FinancialAccount hotWalletAccount = organization.FinancialAccounts.AssetsBitcoinHot; + + if (hotWalletAccount != null) + { + BitcoinUtility.CheckHotwalletForexProfitLoss(hotWalletAccount); + } + } } catch (Exception e) { diff --git a/Logic/Financial/BitcoinUtility.cs b/Logic/Financial/BitcoinUtility.cs index fe36770d..a489ecd7 100644 --- a/Logic/Financial/BitcoinUtility.cs +++ b/Logic/Financial/BitcoinUtility.cs @@ -54,6 +54,101 @@ namespace Swarmops.Logic.Financial } + public static void CheckHotwalletAddresses(Organization organization) + { + HotBitcoinAddresses addresses = HotBitcoinAddresses.ForOrganization(organization); + + foreach (HotBitcoinAddress address in addresses) + { + if (address.Chain == BitcoinChain.Core) + { + // These shouldn't exist much, so make sure that we have the equivalent Cash address registered + try + { + HotBitcoinAddress.FromAddress(BitcoinChain.Cash, address.ProtocolLevelAddress); + } + catch (ArgumentException) + { + // We didn't have it, so create it + BitcoinUtility.TestUnspents(BitcoinChain.Cash, address.ProtocolLevelAddress); + } + } + } + } + + + public static Dictionary GetHotwalletSatoshisPerChain(Organization organization) + { + Dictionary satoshisTotalLookup = new Dictionary(); + + HotBitcoinAddresses addresses = HotBitcoinAddresses.ForOrganization(organization); + + foreach (HotBitcoinAddress address in addresses) + { + HotBitcoinAddressUnspents unspents = HotBitcoinAddressUnspents.ForAddress(address); + Int64 satoshisUnspentAddress = 0; + + foreach (HotBitcoinAddressUnspent unspent in unspents) + { + satoshisUnspentAddress += unspent.AmountSatoshis; + } + + if (satoshisUnspentAddress > 0) + { + if (!satoshisTotalLookup.ContainsKey(address.Chain)) + { + satoshisTotalLookup[address.Chain] = 0; + } + + satoshisTotalLookup[address.Chain] += satoshisUnspentAddress; + } + } + + return satoshisTotalLookup; + } + + + public static Money GetHotwalletValue (Organization organization) + { + Currency presentationCurrency = organization.Currency; + + Dictionary conversionRateLookup = new Dictionary(); + Dictionary satoshisTotalLookup = new Dictionary(); + + long fiatCentsPerCoreCoin = + new Money(BitcoinUtility.SatoshisPerBitcoin, Currency.BitcoinCore).ToCurrency( + presentationCurrency).Cents; + long fiatCentsPerCashCoin = + new Money(BitcoinUtility.SatoshisPerBitcoin, Currency.BitcoinCash).ToCurrency( + presentationCurrency).Cents; + + conversionRateLookup[BitcoinChain.Cash] = fiatCentsPerCashCoin/1.0/BitcoinUtility.SatoshisPerBitcoin; + // the "/1.0" converts to double implicitly + conversionRateLookup[BitcoinChain.Core] = fiatCentsPerCoreCoin/1.0/BitcoinUtility.SatoshisPerBitcoin; + + satoshisTotalLookup = GetHotwalletSatoshisPerChain(organization); + + Int64 presentationCurrencyCents = 0; + + foreach (BitcoinChain chain in satoshisTotalLookup.Keys) + { + presentationCurrencyCents += (Int64) (satoshisTotalLookup[chain]*conversionRateLookup[chain]); // some precision loss unavoidable here + } + + return new Money(presentationCurrencyCents, presentationCurrency); + } + + + public static void CheckHotwalletForexProfitLoss(FinancialAccount hotWallet) + { + if (hotWallet != null) + { + hotWallet.LogForexProfitLoss(GetHotwalletValue(hotWallet.Organization)); + } + } + + + public static void VerifyBitcoinHotWallet() { // This must only be run from the backend @@ -901,7 +996,7 @@ namespace Swarmops.Logic.Financial private const Int64 _satoshisPerBitcoin = 100 * 1000 * 1000; // written this way to improve readability - important constants - + public static Currency GetCurrencyFromChain(BitcoinChain chain) { diff --git a/Logic/Financial/FinancialAccount.cs b/Logic/Financial/FinancialAccount.cs index 74586a12..fa16a5e3 100644 --- a/Logic/Financial/FinancialAccount.cs +++ b/Logic/Financial/FinancialAccount.cs @@ -132,7 +132,7 @@ namespace Swarmops.Logic.Financial } - public Money ForeignBalanceTotalCents + public Money ForeignCurrencyBalance { get { @@ -143,7 +143,7 @@ namespace Swarmops.Logic.Financial } - public Money GetForeignBalanceDeltaCents(DateTime lowerBoundinclusive, DateTime upperBoundExclusive) + public Money GetForeignCurrencyBalanceDeltaCents(DateTime lowerBoundinclusive, DateTime upperBoundExclusive) { if (this.ForeignCurrency == null) { @@ -174,21 +174,30 @@ namespace Swarmops.Logic.Financial // Compare current balance in native currency with the current balance in foreign currency. If off by more than 100 cents, // log a corrective transaction to account for exchange rate fluctuations. - DateTime compareDateTime = DateTime.UtcNow; + if (ForeignCurrency == null) + { + return; // accounting is in presentation currency only + } + Money foreignCents = new Money(ForeignCurrencyBalance.Cents, ForeignCurrency); + + LogForexProfitLoss(foreignCents); + } + + + public void LogForexProfitLoss(Money accountBalanceInAnyCurrency) + { + Int64 currentNativeValueOfForeignCents = accountBalanceInAnyCurrency.ToCurrency(Organization.Currency).Cents; Int64 nativeCents = BalanceTotalCents; - Money foreignCents = new Money(ForeignBalanceTotalCents.Cents, ForeignCurrency, compareDateTime); - - Int64 currentNativeValueOfForeignCents = foreignCents.ToCurrency (Organization.Currency).Cents; if (nativeCents - 100 > currentNativeValueOfForeignCents) { // log a loss Int64 lossCents = nativeCents - currentNativeValueOfForeignCents; - FinancialTransaction lossTx = FinancialTransaction.Create (Organization, DateTime.UtcNow, "Forex Loss"); - lossTx.AddRow (this, -lossCents, null); - lossTx.AddRow (Organization.FinancialAccounts.CostsCurrencyFluctuations, lossCents, null); + FinancialTransaction lossTx = FinancialTransaction.Create(Organization, DateTime.UtcNow, "Forex Loss"); + lossTx.AddRow(this, -lossCents, null); + lossTx.AddRow(Organization.FinancialAccounts.CostsCurrencyFluctuations, lossCents, null); } else if (nativeCents + 100 < currentNativeValueOfForeignCents) { @@ -282,7 +291,7 @@ namespace Swarmops.Logic.Financial // First, get the current initial balance in account nonpresentation currency: - Int64 initialBalanceForeignCents = GetForeignBalanceDeltaCents(Constants.DateTimeLow, + Int64 initialBalanceForeignCents = GetForeignCurrencyBalanceDeltaCents(Constants.DateTimeLow, new DateTime(this.Organization.FirstFiscalYear, 1, 1)).Cents; Int64 desiredInitialBalanceForeignCents = value.Cents; diff --git a/Site/App_GlobalResources/Pages.Ledgers.designer.cs b/Site/App_GlobalResources/Pages.Ledgers.designer.cs index 6be010d1..2a1fb741 100644 --- a/Site/App_GlobalResources/Pages.Ledgers.designer.cs +++ b/Site/App_GlobalResources/Pages.Ledgers.designer.cs @@ -1987,7 +1987,7 @@ namespace Resources.Pages { } /// - /// Looks up a localized string similar to (which was accounted for with <strong>{0} {1:N2}</strong> in the presentation currency). + /// Looks up a localized string similar to (which was accounted for with <strong>{0} {1:N2}</strong> in the ledger operating currency). /// internal static string UploadBankFiles_ResultsInitialBalanceSetForeignCurrency { get { diff --git a/Site/App_GlobalResources/Pages.Ledgers.resx b/Site/App_GlobalResources/Pages.Ledgers.resx index d9e2c39e..b1c7775f 100644 --- a/Site/App_GlobalResources/Pages.Ledgers.resx +++ b/Site/App_GlobalResources/Pages.Ledgers.resx @@ -975,7 +975,7 @@ {1} {2:N2} is the currency and amount, like USD 2,151.00. - (which was accounted for with <strong>{0} {1:N2}</strong> in the presentation currency) + (which was accounted for with <strong>{0} {1:N2}</strong> in the ledger operating currency) {0} {1:N2} is a currency and amount, which is how an initial bank balance in a foreign currency was entered into the ledgers after converting to the organization's accounting currency \ No newline at end of file diff --git a/Site/Pages/v5/Ledgers/Json-BitcoinHotwalletData.aspx.cs b/Site/Pages/v5/Ledgers/Json-BitcoinHotwalletData.aspx.cs index aff37fde..d4be48a0 100644 --- a/Site/Pages/v5/Ledgers/Json-BitcoinHotwalletData.aspx.cs +++ b/Site/Pages/v5/Ledgers/Json-BitcoinHotwalletData.aspx.cs @@ -19,26 +19,9 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers PageAccessRequired = new Access(CurrentOrganization, AccessAspect.BookkeepingDetails, AccessType.Read); this._authenticationData = GetAuthenticationDataAndCulture(); - HotBitcoinAddresses addresses = HotBitcoinAddresses.ForOrganization (_authenticationData.CurrentOrganization); - - foreach (HotBitcoinAddress address in addresses) - { - if (address.Chain == BitcoinChain.Core) - { - // These shouldn't exist much, so make sure that we have the equivalent Cash address registered - try - { - HotBitcoinAddress.FromAddress(BitcoinChain.Cash, address.ProtocolLevelAddress); - } - catch (ArgumentException) - { - // We didn't have it, so create it - BitcoinUtility.TestUnspents(BitcoinChain.Cash, address.ProtocolLevelAddress); - } - } - - } + BitcoinUtility.CheckHotwalletAddresses(_authenticationData.CurrentOrganization); + HotBitcoinAddresses addresses = HotBitcoinAddresses.ForOrganization(_authenticationData.CurrentOrganization); Response.ContentType = "application/json"; Response.Output.WriteLine(FormatJson(addresses)); Response.End(); diff --git a/Site/Pages/v5/Ledgers/UploadBankFiles.aspx.cs b/Site/Pages/v5/Ledgers/UploadBankFiles.aspx.cs index cf88bf6b..3ac7e754 100644 --- a/Site/Pages/v5/Ledgers/UploadBankFiles.aspx.cs +++ b/Site/Pages/v5/Ledgers/UploadBankFiles.aspx.cs @@ -784,7 +784,7 @@ namespace Swarmops.Site.Pages.Ledgers else { // foreign-currency account - databaseAccountBalanceCents = assetAccount.ForeignBalanceTotalCents.Cents; + databaseAccountBalanceCents = assetAccount.ForeignCurrencyBalance.Cents; }