2023-09-19 09:51:59 +00:00
|
|
|
|
namespace GethPlugin
|
2023-04-18 08:22:11 +00:00
|
|
|
|
{
|
2023-04-24 14:07:32 +00:00
|
|
|
|
public class Ether : IComparable<Ether>
|
2023-04-18 08:22:11 +00:00
|
|
|
|
{
|
|
|
|
|
public Ether(decimal wei)
|
|
|
|
|
{
|
|
|
|
|
Wei = wei;
|
2023-09-19 09:51:59 +00:00
|
|
|
|
Eth = wei / TokensIntExtensions.WeiPerEth;
|
2023-04-18 08:22:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public decimal Wei { get; }
|
2023-09-19 09:51:59 +00:00
|
|
|
|
public decimal Eth { get; }
|
2023-04-19 08:42:08 +00:00
|
|
|
|
|
2023-04-24 14:07:32 +00:00
|
|
|
|
public int CompareTo(Ether? other)
|
|
|
|
|
{
|
|
|
|
|
return Wei.CompareTo(other!.Wei);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 08:42:08 +00: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 08:59:22 +00: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 08:42:08 +00:00
|
|
|
|
}
|
2023-04-18 08:22:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class TokensIntExtensions
|
|
|
|
|
{
|
2023-09-19 09:51:59 +00:00
|
|
|
|
public const decimal WeiPerEth = 1000000000000000000;
|
2023-04-18 08:22:11 +00: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 09:51:59 +00:00
|
|
|
|
return new Ether(i * WeiPerEth);
|
2023-04-18 08:22:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Ether Wei(this decimal i)
|
|
|
|
|
{
|
|
|
|
|
return new Ether(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|