2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2022-09-29 08:45:27 +02:00
|
|
|
using System.Linq;
|
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)
|
|
|
|
{
|
2021-12-28 09:39:54 +01:00
|
|
|
ArgumentNullException.ThrowIfNull(right);
|
|
|
|
ArgumentNullException.ThrowIfNull(left);
|
2018-05-02 11:37:53 +02:00
|
|
|
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)
|
|
|
|
{
|
2021-12-28 09:39:54 +01:00
|
|
|
ArgumentNullException.ThrowIfNull(str);
|
2018-05-02 11:37:53 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-29 08:45:27 +02:00
|
|
|
else if (splitted.Length > 2)
|
|
|
|
{
|
|
|
|
// Some shitcoin have _ their own ticker name... Since we don't care about those, let's
|
|
|
|
// parse it anyway assuming the first part is one currency.
|
|
|
|
value = new CurrencyPair(splitted[0], string.Join("_", splitted.Skip(1).ToArray()));
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-04 08:35:39 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|