/ Framework / Utils / TestTokenExtensions.cs
TestTokenExtensions.cs
  1  using System.Numerics;
  2  
  3  namespace Utils
  4  {
  5      public class TestToken : IComparable<TestToken>
  6      {
  7          public static BigInteger WeiFactor = new BigInteger(1000000000000000000);
  8  
  9          public TestToken(BigInteger tstWei)
 10          {
 11              TstWei = tstWei;
 12              Tst = tstWei / WeiFactor;
 13          }
 14  
 15          public BigInteger TstWei { get; }
 16          public BigInteger Tst { get; }
 17  
 18          public int CompareTo(TestToken? other)
 19          {
 20              return TstWei.CompareTo(other!.TstWei);
 21          }
 22  
 23          public override bool Equals(object? obj)
 24          {
 25              return obj is TestToken token && TstWei == token.TstWei;
 26          }
 27  
 28          public override int GetHashCode()
 29          {
 30              return HashCode.Combine(TstWei);
 31          }
 32  
 33          public override string ToString()
 34          {
 35              var weiOnly = TstWei % WeiFactor;
 36  
 37              var tokens = new List<string>();
 38              if (Tst > 0) tokens.Add($"{Tst} TST");
 39              if (weiOnly > 0) tokens.Add($"{weiOnly} TSTWEI");
 40              
 41              if (tokens.Count == 0) return "0 TST + 0 TSTWEI";
 42              return string.Join(" + ", tokens);
 43          }
 44  
 45          public static TestToken operator +(TestToken a, TestToken b)
 46          {
 47              return new TestToken(a.TstWei + b.TstWei);
 48          }
 49  
 50          public static TestToken operator -(TestToken a, TestToken b)
 51          {
 52              return new TestToken(a.TstWei - b.TstWei);
 53          }
 54  
 55          public static TestToken operator *(TestToken a, int b)
 56          {
 57              return new TestToken(a.TstWei * b);
 58          }
 59  
 60          public static bool operator <(TestToken a, TestToken b)
 61          {
 62              return a.TstWei < b.TstWei;
 63          }
 64  
 65          public static bool operator >(TestToken a, TestToken b)
 66          {
 67              return a.TstWei > b.TstWei;
 68          }
 69  
 70          public static bool operator ==(TestToken a, TestToken b)
 71          {
 72              return a.TstWei == b.TstWei;
 73          }
 74  
 75          public static bool operator !=(TestToken a, TestToken b)
 76          {
 77              return a.TstWei != b.TstWei;
 78          }
 79      }
 80  
 81      public static class TestTokensExtensions
 82      {
 83          public static TestToken TstWei(this int i)
 84          {
 85              return new TestToken(new BigInteger(i));
 86          }
 87  
 88          public static TestToken TstWei(this decimal i)
 89          {
 90              return new TestToken(new BigInteger(i));
 91          }
 92  
 93          public static TestToken TstWei(this BigInteger i)
 94          {
 95              return new TestToken(i);
 96          }
 97  
 98          public static TestToken Tst(this int i)
 99          {
100              return Tst(Convert.ToDecimal(i));
101          }
102  
103          public static TestToken Tst(this decimal i)
104          {
105              var v = i * ((decimal)TestToken.WeiFactor);
106  
107              return new TestToken(new BigInteger(v));
108          }
109  
110          public static TestToken Tst(this BigInteger i)
111          {
112              return new TestToken(i * TestToken.WeiFactor);
113          }
114      }
115  }