2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2020-05-31 12:18:29 +02:00
|
|
|
using BTCPayServer.Services.Rates;
|
2018-05-02 11:37:53 +02:00
|
|
|
|
|
|
|
namespace BTCPayServer.Rating
|
|
|
|
{
|
|
|
|
public class CurrencyPair
|
|
|
|
{
|
|
|
|
public CurrencyPair(string left, string right)
|
|
|
|
{
|
|
|
|
if (right == null)
|
|
|
|
throw new ArgumentNullException(nameof(right));
|
|
|
|
if (left == null)
|
|
|
|
throw new ArgumentNullException(nameof(left));
|
|
|
|
Right = right.ToUpperInvariant();
|
|
|
|
Left = left.ToUpperInvariant();
|
|
|
|
}
|
|
|
|
public string Left { get; private set; }
|
|
|
|
public string Right { get; private set; }
|
|
|
|
|
|
|
|
public static CurrencyPair Parse(string str)
|
|
|
|
{
|
|
|
|
if (!TryParse(str, out var result))
|
|
|
|
throw new FormatException("Invalid currency pair");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
public static bool TryParse(string str, out CurrencyPair value)
|
|
|
|
{
|
|
|
|
if (str == null)
|
|
|
|
throw new ArgumentNullException(nameof(str));
|
|
|
|
value = null;
|
2018-05-03 18:46:52 +02:00
|
|
|
str = str.Trim();
|
2018-05-04 08:35:39 +02:00
|
|
|
if (str.Length > 12)
|
2018-05-02 11:37:53 +02:00
|
|
|
return false;
|
2018-05-04 08:35:39 +02:00
|
|
|
var splitted = str.Split(new[] { '_', '-' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
if (splitted.Length == 2)
|
|
|
|
{
|
|
|
|
value = new CurrencyPair(splitted[0], splitted[1]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (splitted.Length == 1)
|
|
|
|
{
|
|
|
|
var currencyPair = splitted[0];
|
|
|
|
if (currencyPair.Length < 6 || currencyPair.Length > 10)
|
|
|
|
return false;
|
2018-05-05 15:59:53 +02:00
|
|
|
if (currencyPair.Length == 6)
|
2020-06-28 10:55:27 +02:00
|
|
|
{
|
|
|
|
value = new CurrencyPair(currencyPair.Substring(0, 3), currencyPair.Substring(3, 3));
|
2018-05-05 15:59:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-06-28 10:55:27 +02:00
|
|
|
|
2018-05-04 08:35:39 +02:00
|
|
|
for (int i = 3; i < 5; i++)
|
|
|
|
{
|
|
|
|
var potentialCryptoName = currencyPair.Substring(0, i);
|
2020-05-31 12:18:29 +02:00
|
|
|
var currency = CurrencyNameTable.Instance.GetCurrencyData(potentialCryptoName, false);
|
|
|
|
if (currency != null)
|
2018-05-04 08:35:39 +02:00
|
|
|
{
|
2020-05-31 12:18:29 +02:00
|
|
|
value = new CurrencyPair(currency.Code, currencyPair.Substring(i));
|
2018-05-04 08:35:39 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2018-05-02 11:37:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
|
|
|
CurrencyPair item = obj as CurrencyPair;
|
|
|
|
if (item == null)
|
|
|
|
return false;
|
|
|
|
return ToString().Equals(item.ToString(), StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
public static bool operator ==(CurrencyPair a, CurrencyPair b)
|
|
|
|
{
|
|
|
|
if (System.Object.ReferenceEquals(a, b))
|
|
|
|
return true;
|
|
|
|
if (((object)a == null) || ((object)b == null))
|
|
|
|
return false;
|
|
|
|
return a.ToString() == b.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator !=(CurrencyPair a, CurrencyPair b)
|
|
|
|
{
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
|
|
|
return ToString().GetHashCode(StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return $"{Left}_{Right}";
|
|
|
|
}
|
2018-05-04 17:40:54 +02:00
|
|
|
|
|
|
|
public CurrencyPair Inverse()
|
|
|
|
{
|
|
|
|
return new CurrencyPair(Right, Left);
|
|
|
|
}
|
2018-05-02 11:37:53 +02:00
|
|
|
}
|
|
|
|
}
|