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