Replaces decimal with bigint in Eth abstraction

This commit is contained in:
ThatBen 2025-07-30 09:04:07 +02:00
parent d9ca905cd9
commit a80c5c0d08
No known key found for this signature in database
GPG Key ID: E020A7DDCD52E1AB
4 changed files with 43 additions and 14 deletions

View File

@ -1,4 +1,5 @@
using BlockchainUtils;
using System.Numerics;
using BlockchainUtils;
using Logging;
using Nethereum.ABI.FunctionEncoding.Attributes;
using Nethereum.Contracts;
@ -22,25 +23,26 @@ namespace NethereumWorkflow
this.blockCache = blockCache;
}
public string SendEth(string toAddress, decimal ethAmount)
public string SendEth(string toAddress, BigInteger ethAmount)
{
log.Debug();
var receipt = Time.Wait(web3.Eth.GetEtherTransferService().TransferEtherAndWaitForReceiptAsync(toAddress, ethAmount));
var receipt = Time.Wait(web3.Eth.GetEtherTransferService().TransferEtherAndWaitForReceiptAsync(toAddress, ((decimal)ethAmount)));
if (!receipt.Succeeded()) throw new Exception("Unable to send Eth");
return receipt.TransactionHash;
}
public decimal GetEthBalance()
public BigInteger GetEthBalance()
{
log.Debug();
return GetEthBalance(web3.TransactionManager.Account.Address);
}
public decimal GetEthBalance(string address)
public BigInteger GetEthBalance(string address)
{
log.Debug();
var balance = Time.Wait(web3.Eth.GetBalance.SendRequestAsync(address));
return Web3.Convert.FromWei(balance.Value);
return balance.Value;
}
public TResult Call<TFunction, TResult>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new()
@ -57,6 +59,13 @@ namespace NethereumWorkflow
return Time.Wait(handler.QueryAsync<TResult>(contractAddress, function, new BlockParameter(blockNumber)));
}
public void Call<TFunction>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new()
{
log.Debug(typeof(TFunction).ToString());
var handler = web3.Eth.GetContractQueryHandler<TFunction>();
Time.Wait(handler.QueryRawAsync(contractAddress, function));
}
public void Call<TFunction>(string contractAddress, TFunction function, ulong blockNumber) where TFunction : FunctionMessage, new()
{
log.Debug(typeof(TFunction).ToString());

View File

@ -1,15 +1,17 @@
namespace Utils
using System.Numerics;
namespace Utils
{
public class Ether : IComparable<Ether>
{
public Ether(decimal wei)
public Ether(BigInteger wei)
{
Wei = wei;
Eth = wei / TokensIntExtensions.WeiPerEth;
}
public decimal Wei { get; }
public decimal Eth { get; }
public BigInteger Wei { get; }
public BigInteger Eth { get; }
public int CompareTo(Ether? other)
{
@ -75,7 +77,7 @@
public static class TokensIntExtensions
{
public const decimal WeiPerEth = 1000000000000000000;
public static readonly BigInteger WeiPerEth = new BigInteger(1000000000000000000);
public static Ether Eth(this int i)
{
@ -89,10 +91,22 @@
public static Ether Eth(this decimal i)
{
return new Ether(i * WeiPerEth);
var a = new BigInteger(i);
return new Ether(a * WeiPerEth);
}
public static Ether Wei(this decimal i)
{
var a = new BigInteger(i);
return new Ether(a);
}
public static Ether Eth(this BigInteger i)
{
return new Ether(i * WeiPerEth);
}
public static Ether Wei(this BigInteger i)
{
return new Ether(i);
}

View File

@ -21,6 +21,7 @@ namespace GethPlugin
string SendEth(EthAddress account, Ether eth);
TResult Call<TFunction, TResult>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new();
TResult Call<TFunction, TResult>(string contractAddress, TFunction function, ulong blockNumber) where TFunction : FunctionMessage, new();
void Call<TFunction>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new();
void Call<TFunction>(string contractAddress, TFunction function, ulong blockNumber) where TFunction : FunctionMessage, new();
string SendTransaction<TFunction>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new();
Transaction GetTransaction(string transactionHash);
@ -137,7 +138,7 @@ namespace GethPlugin
public Ether GetEthBalance(EthAddress address)
{
return StartInteraction().GetEthBalance(address.Address).Eth();
return StartInteraction().GetEthBalance(address.Address).Wei();
}
public string SendEth(IHasEthAddress owner, Ether eth)
@ -160,6 +161,11 @@ namespace GethPlugin
return StartInteraction().Call<TFunction, TResult>(contractAddress, function, blockNumber);
}
public void Call<TFunction>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new()
{
StartInteraction().Call(contractAddress, function);
}
public void Call<TFunction>(string contractAddress, TFunction function, ulong blockNumber) where TFunction : FunctionMessage, new()
{
StartInteraction().Call(contractAddress, function, blockNumber);

View File

@ -93,7 +93,7 @@ namespace BiblioTech.Commands
private bool ShouldSendEth(IGethNode gethNode, EthAddress addr)
{
var eth = gethNode.GetEthBalance(addr);
return eth.Eth < Program.Config.SendEth;
return ((decimal)eth.Eth) < Program.Config.SendEth;
}
private string FormatTransactionLink(string transaction)