2025-01-16 11:31:50 +01:00
|
|
|
|
namespace Utils
|
2023-04-18 10:22:11 +02:00
|
|
|
|
{
|
2023-04-24 16:07:32 +02:00
|
|
|
|
public class Ether : IComparable<Ether>
|
2023-04-18 10:22:11 +02:00
|
|
|
|
{
|
|
|
|
|
|
public Ether(decimal wei)
|
|
|
|
|
|
{
|
|
|
|
|
|
Wei = wei;
|
2023-09-19 11:51:59 +02:00
|
|
|
|
Eth = wei / TokensIntExtensions.WeiPerEth;
|
2023-04-18 10:22:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public decimal Wei { get; }
|
2023-09-19 11:51:59 +02:00
|
|
|
|
public decimal Eth { get; }
|
2023-04-19 10:42:08 +02:00
|
|
|
|
|
2023-04-24 16:07:32 +02:00
|
|
|
|
public int CompareTo(Ether? other)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Wei.CompareTo(other!.Wei);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-19 10:42:08 +02:00
|
|
|
|
public override bool Equals(object? obj)
|
|
|
|
|
|
{
|
|
|
|
|
|
return obj is Ether ether && Wei == ether.Wei;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
|
{
|
|
|
|
|
|
return HashCode.Combine(Wei);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
2024-09-30 10:59:22 +02:00
|
|
|
|
var weiOnly = Wei % TokensIntExtensions.WeiPerEth;
|
|
|
|
|
|
|
|
|
|
|
|
var tokens = new List<string>();
|
|
|
|
|
|
if (Eth > 0) tokens.Add($"{Eth} Eth");
|
|
|
|
|
|
if (weiOnly > 0) tokens.Add($"{weiOnly} Wei");
|
|
|
|
|
|
|
|
|
|
|
|
return string.Join(" + ", tokens);
|
2023-04-19 10:42:08 +02:00
|
|
|
|
}
|
2025-03-05 15:55:10 +01:00
|
|
|
|
|
|
|
|
|
|
public static Ether operator +(Ether a, Ether b)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Ether(a.Wei + b.Wei);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Ether operator -(Ether a, Ether b)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Ether(a.Wei - b.Wei);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Ether operator *(Ether a, int b)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Ether(a.Wei * b);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static bool operator <(Ether a, Ether b)
|
|
|
|
|
|
{
|
|
|
|
|
|
return a.Wei < b.Wei;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static bool operator >(Ether a, Ether b)
|
|
|
|
|
|
{
|
|
|
|
|
|
return a.Wei > b.Wei;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static bool operator ==(Ether a, Ether b)
|
|
|
|
|
|
{
|
|
|
|
|
|
return a.Wei == b.Wei;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static bool operator !=(Ether a, Ether b)
|
|
|
|
|
|
{
|
|
|
|
|
|
return a.Wei != b.Wei;
|
|
|
|
|
|
}
|
2023-04-18 10:22:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static class TokensIntExtensions
|
|
|
|
|
|
{
|
2023-09-19 11:51:59 +02:00
|
|
|
|
public const decimal WeiPerEth = 1000000000000000000;
|
2023-04-18 10:22:11 +02:00
|
|
|
|
|
|
|
|
|
|
public static Ether Eth(this int i)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Eth(Convert.ToDecimal(i));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Ether Wei(this int i)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Wei(Convert.ToDecimal(i));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Ether Eth(this decimal i)
|
|
|
|
|
|
{
|
2023-09-19 11:51:59 +02:00
|
|
|
|
return new Ether(i * WeiPerEth);
|
2023-04-18 10:22:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Ether Wei(this decimal i)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Ether(i);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|