Added check for hotwallet forex profit/loss

This commit is contained in:
Rick Falkvinge
2018-05-15 17:15:41 +02:00
parent c60ee5309e
commit afea96aced
7 changed files with 134 additions and 33 deletions
+14
View File
@@ -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)
{
+96 -1
View File
@@ -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<BitcoinChain, Int64> GetHotwalletSatoshisPerChain(Organization organization)
{
Dictionary<BitcoinChain, Int64> satoshisTotalLookup = new Dictionary<BitcoinChain, long>();
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<BitcoinChain, double> conversionRateLookup = new Dictionary<BitcoinChain, double>();
Dictionary<BitcoinChain, Int64> satoshisTotalLookup = new Dictionary<BitcoinChain, long>();
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)
{
+19 -10
View File
@@ -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;
+1 -1
View File
@@ -1987,7 +1987,7 @@ namespace Resources.Pages {
}
/// <summary>
/// Looks up a localized string similar to (which was accounted for with &lt;strong&gt;{0} {1:N2}&lt;/strong&gt; in the presentation currency).
/// Looks up a localized string similar to (which was accounted for with &lt;strong&gt;{0} {1:N2}&lt;/strong&gt; in the ledger operating currency).
/// </summary>
internal static string UploadBankFiles_ResultsInitialBalanceSetForeignCurrency {
get {
+1 -1
View File
@@ -975,7 +975,7 @@
<comment>{1} {2:N2} is the currency and amount, like USD 2,151.00.</comment>
</data>
<data name="UploadBankFiles_ResultsInitialBalanceSetForeignCurrency" xml:space="preserve">
<value>(which was accounted for with &lt;strong&gt;{0} {1:N2}&lt;/strong&gt; in the presentation currency)</value>
<value>(which was accounted for with &lt;strong&gt;{0} {1:N2}&lt;/strong&gt; in the ledger operating currency)</value>
<comment>{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</comment>
</data>
</root>
@@ -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();
@@ -784,7 +784,7 @@ namespace Swarmops.Site.Pages.Ledgers
else
{
// foreign-currency account
databaseAccountBalanceCents = assetAccount.ForeignBalanceTotalCents.Cents;
databaseAccountBalanceCents = assetAccount.ForeignCurrencyBalance.Cents;
}