From 9d48503e15a22c8ad1d8cb3192f54d205a735437 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Thu, 21 Dec 2017 11:51:38 +0100 Subject: [PATCH] Fix for Bitpay suddenly including BCH in Fiat rates --- Basic/Types/Financial/BasicCurrency.cs | 6 +- Database/Database-Currencies.cs | 5 +- Logic/Financial/ExchangeRateSnapshot.cs | 85 ++++++++++--------- Site/Master-v5.master.cs | 2 +- .../Pages/v5/Ledgers/BalanceTransactions.aspx | 22 ++++- .../BalanceTransactions.aspx.designer.cs | 72 ++++++++++++++++ 6 files changed, 143 insertions(+), 49 deletions(-) diff --git a/Basic/Types/Financial/BasicCurrency.cs b/Basic/Types/Financial/BasicCurrency.cs index 047ff81d..a85c5fba 100644 --- a/Basic/Types/Financial/BasicCurrency.cs +++ b/Basic/Types/Financial/BasicCurrency.cs @@ -4,16 +4,17 @@ namespace Swarmops.Basic.Types.Financial { public class BasicCurrency : IHasIdentity { - public BasicCurrency (int currencyId, string code, string name, string sign) + public BasicCurrency (int currencyId, string code, string name, string sign, bool isCrypto) { this.CurrencyId = currencyId; this.Code = code; this.Name = name; this.Sign = sign; + this.IsCrypto = isCrypto; } public BasicCurrency (BasicCurrency original) - : this (original.CurrencyId, original.Code, original.Name, original.Sign) + : this (original.CurrencyId, original.Code, original.Name, original.Sign, original.IsCrypto) { // empty copy ctor } @@ -23,6 +24,7 @@ namespace Swarmops.Basic.Types.Financial public string Code { get; private set; } public string Name { get; private set; } public string Sign { get; private set; } + public bool IsCrypto { get; private set; } public int Identity { diff --git a/Database/Database-Currencies.cs b/Database/Database-Currencies.cs index 5b83cfc2..6f13a9f9 100644 --- a/Database/Database-Currencies.cs +++ b/Database/Database-Currencies.cs @@ -12,7 +12,7 @@ namespace Swarmops.Database #region Field reading code private const string currencyFieldSequence = - " CurrencyId,Code,Name,Sign " + // 0-3 + " CurrencyId,Code,Name,Sign,IsCrypto " + // 0-4 "FROM Currencies "; private static BasicCurrency ReadCurrencyFromDataReader (IDataRecord reader) @@ -21,8 +21,9 @@ namespace Swarmops.Database string code = reader.GetString (1); string name = reader.GetString (2); string sign = reader.GetString (3); + int isCrypto = reader.GetInt32(4); - return new BasicCurrency (currencyId, code, name, sign); + return new BasicCurrency (currencyId, code, name, sign, isCrypto != 0? true: false); } #endregion diff --git a/Logic/Financial/ExchangeRateSnapshot.cs b/Logic/Financial/ExchangeRateSnapshot.cs index c7274de4..dffa6ed5 100644 --- a/Logic/Financial/ExchangeRateSnapshot.cs +++ b/Logic/Financial/ExchangeRateSnapshot.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Globalization; using System.Linq; using System.Net; @@ -39,57 +40,21 @@ namespace Swarmops.Logic.Financial public static void Create() { - int exchangeRateSnapshotId = 0; - // We're getting rates against the fiat currencies from BitPay and crypto-to-crypto from Shapeshift. using (WebClient client = new WebClient()) { client.Encoding = Encoding.UTF8; - string fiatRateDataRaw = client.DownloadString("https://bitpay.com/api/rates"); - - // BitPay doesn't provide valid JSON - the rate field isn't enclosed in quotes - so we can't use JSON Deserialization; we'll - // have to use Regex matching instead. - - string regexPattern = @"\{\""code\"":\""([A-Z]+)\"",\""name\"":\""([^\""]+)\"",\""rate\"":([0-9\.]+)\}"; - Regex regex = new Regex (regexPattern); - Match match = regex.Match (fiatRateDataRaw); - - int bitcoinId = GetOrCreateCryptocurrency("BTC"); - - if (match.Success) - { - // We have at least one match, so prepare a new ExchangeRate snapshot - - exchangeRateSnapshotId = SwarmDb.GetDatabaseForWriting().CreateExchangeRateSnapshot(); - } - - while (match.Success) - { - string currencyCode = match.Groups[1].Value; - string currencyName = match.Groups[2].Value; - double btcRate = Double.Parse (match.Groups[3].Value, NumberStyles.AllowDecimalPoint, - CultureInfo.InvariantCulture); - - btcRate /= 1000000.0; // We're operating in microbitcoin, so adjust the stored exchange rate accordingly (right-shift six decimal places) - - int currencyId = GetOrCreateFiatCurrency (currencyCode, currencyName); - - SwarmDb.GetDatabaseForWriting() - .CreateExchangeRateDatapoint (exchangeRateSnapshotId, currencyId, bitcoinId, btcRate); - - match = match.NextMatch(); - } - JavaScriptSerializer serializer = new JavaScriptSerializer(); - BitpayRateDatapoint[] fiatRates = (BitpayRateDatapoint[]) serializer.Deserialize (fiatRateDataRaw); + int bitcoinId = GetOrCreateCryptocurrency("BTC"); + int exchangeRateSnapshotId = SwarmDb.GetDatabaseForWriting().CreateExchangeRateSnapshot(); // Download Shapeshift data string cryptoRateDataRaw = client.DownloadString("https://shapeshift.io/marketinfo"); ShapeshiftRateDatapoint[] cryptoRates = - (ShapeshiftRateDatapoint[]) serializer.Deserialize(cryptoRateDataRaw); + (ShapeshiftRateDatapoint[])serializer.Deserialize(cryptoRateDataRaw); if (exchangeRateSnapshotId > 0) // test that we're making a snapshot first { @@ -103,18 +68,54 @@ namespace Swarmops.Logic.Financial if (coinCode != "BCH") { btcRate /= 1000000.0; - // We're operating in microbitcoin, so adjust the stored exchange rate right six decimal places - // EXCEPT for Bitcoin Cash which ALSO operates in microbitcoin + // We're operating in microbitcoin, so adjust the stored exchange rate right six decimal places + // EXCEPT for Bitcoin Cash which ALSO operates in microbitcoin } int coinId = GetOrCreateCryptocurrency(coinCode); + SwarmDb.GetDatabaseForWriting(). CreateExchangeRateDatapoint(exchangeRateSnapshotId, coinId, bitcoinId, btcRate); } } } - Console.WriteLine(cryptoRateDataRaw); + // Download BitPay data + + string fiatRateDataRaw = client.DownloadString("https://bitpay.com/api/rates"); + + // BitPay doesn't provide valid JSON - the rate field isn't enclosed in quotes - so we can't use JSON Deserialization; we'll + // have to use Regex matching instead. + + string regexPattern = @"\{\""code\"":\""([A-Z]+)\"",\""name\"":\""([^\""]+)\"",\""rate\"":([0-9\.]+)\}"; + Regex regex = new Regex (regexPattern); + Match match = regex.Match (fiatRateDataRaw); + + while (match.Success) + { + string currencyCode = match.Groups[1].Value; + string currencyName = match.Groups[2].Value; + double btcRate = Double.Parse (match.Groups[3].Value, NumberStyles.AllowDecimalPoint, + CultureInfo.InvariantCulture); + + btcRate /= 1000000.0; // We're operating in microbitcoin, so adjust the stored exchange rate accordingly (right-shift six decimal places) + + int currencyId = GetOrCreateFiatCurrency (currencyCode, currencyName); + Currency currency = Currency.FromIdentityAggressive(currencyId); + + // Only store if it's not a cryptocurrency provided by Shapeshift + + if (!currency.IsCrypto) + { + SwarmDb.GetDatabaseForWriting() + .CreateExchangeRateDatapoint(exchangeRateSnapshotId, currencyId, bitcoinId, btcRate); + } + + match = match.NextMatch(); + } + + // BitpayRateDatapoint[] fiatRates = (BitpayRateDatapoint[]) serializer.Deserialize (fiatRateDataRaw); + // Console.WriteLine(cryptoRateDataRaw); } } diff --git a/Site/Master-v5.master.cs b/Site/Master-v5.master.cs index d3fda71f..3e655eab 100644 --- a/Site/Master-v5.master.cs +++ b/Site/Master-v5.master.cs @@ -39,7 +39,7 @@ namespace Swarmops.Frontend // BEGIN TEST CODE - // VatReports.CreateNewReports(); + // ExchangeRateSnapshot.Create(); // END TEST CODE diff --git a/Site/Pages/v5/Ledgers/BalanceTransactions.aspx b/Site/Pages/v5/Ledgers/BalanceTransactions.aspx index b7c7fbcf..4241c440 100644 --- a/Site/Pages/v5/Ledgers/BalanceTransactions.aspx +++ b/Site/Pages/v5/Ledgers/BalanceTransactions.aspx @@ -1,5 +1,6 @@ <%@ Page Title="" Language="C#" MasterPageFile="~/Master-v5.master" AutoEventWireup="true" Inherits="Swarmops.Frontend.Pages.v5.Ledgers.BalanceTransactions" Codebehind="BalanceTransactions.aspx.cs" CodeFile="BalanceTransactions.aspx.cs" %> <%@ Register TagPrefix="Swarmops5" TagName="ComboBudgets" Src="~/Controls/v5/Financial/ComboBudgets.ascx" %> +<%@ Register TagPrefix="Swarmops5" TagName="CurrencyTextBox" Src="~/Controls/v5/Financial/CurrencyTextBox.ascx" %> <%@ Register TagPrefix="Swarmops5" TagName="DropDown" Src="~/Controls/v5/Base/DropDown.ascx" %> <%@ Register TagPrefix="Swarmops5" TagName="ModalDialog" Src="~/Controls/v5/Base/ModalDialog.ascx" %> @@ -298,11 +299,28 @@ +

+
+
+ +
+ + + +
+ +
+

- ​
- + +