Fixes eth conversion issue in nethereum sendEth. Adds test to cover this.

This commit is contained in:
Ben 2025-08-05 15:02:52 +02:00
parent 547043a381
commit 6f5d6d2c03
No known key found for this signature in database
GPG Key ID: 0F16E812E736C24B
4 changed files with 56 additions and 19 deletions

View File

@ -25,11 +25,17 @@ namespace NethereumWorkflow
this.blockCache = blockCache;
}
public string SendEth(string toAddress, BigInteger ethAmount)
public string SendEth(string toAddress, Ether eth)
{
var asDecimal = ((decimal)eth.Wei) / (decimal)TokensIntExtensions.WeiPerEth;
return SendEth(toAddress, asDecimal);
}
public string SendEth(string toAddress, decimal ethAmount)
{
return DebugLogWrap(() =>
{
var receipt = Time.Wait(web3.Eth.GetEtherTransferService().TransferEtherAndWaitForReceiptAsync(toAddress, ((decimal)ethAmount)));
var receipt = Time.Wait(web3.Eth.GetEtherTransferService().TransferEtherAndWaitForReceiptAsync(toAddress, ethAmount));
if (!receipt.Succeeded()) throw new Exception("Unable to send Eth");
return receipt.TransactionHash;
}, nameof(SendEth));

View File

@ -95,11 +95,6 @@ namespace Utils
return new TestToken(i);
}
public static TestToken TstWei(this string s)
{
return new TestToken(BigInteger.Parse(s));
}
public static TestToken Tst(this int i)
{
return Tst(Convert.ToDecimal(i));
@ -107,17 +102,14 @@ namespace Utils
public static TestToken Tst(this decimal i)
{
return new TestToken(new BigInteger(i) * TestToken.WeiFactor);
var v = i * ((decimal)TestToken.WeiFactor);
return new TestToken(new BigInteger(v));
}
public static TestToken Tst(this BigInteger i)
{
return new TestToken(i * TestToken.WeiFactor);
}
public static TestToken Tst(this string s)
{
return new TestToken(BigInteger.Parse(s) * TestToken.WeiFactor);
}
}
}

View File

@ -155,7 +155,7 @@ namespace GethPlugin
public string SendEth(EthAddress account, Ether eth)
{
return StartInteraction().SendEth(account.Address, eth.Eth);
return StartInteraction().SendEth(account.Address, eth);
}
public TResult Call<TFunction, TResult>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new()

View File

@ -15,7 +15,7 @@ namespace CodexReleaseTests.MarketTests
private readonly EthAccount user2 = EthAccountGenerator.GenerateNew();
[Test]
public void CanTransferTokens()
public void CanTransferTestTokens()
{
var node = Ci.StartCodexNode();
var blockCache = new BlockCache();
@ -33,11 +33,32 @@ namespace CodexReleaseTests.MarketTests
var contracts1 = contracts.WithDifferentGeth(geth1);
var contracts2 = contracts.WithDifferentGeth(geth2);
contracts1.TransferTestTokens(user2.EthAddress, 5.Tst());
Balances(contracts, 5.Tst(), 5.Tst());
contracts1.TransferTestTokens(user2.EthAddress, (0.5m).Tst());
Balances(contracts, (9.5m).Tst(), (0.5m).Tst());
contracts2.TransferTestTokens(user1.EthAddress, 2.Tst());
Balances(contracts, 7.Tst(), 3.Tst());
contracts2.TransferTestTokens(user1.EthAddress, (0.2m).Tst());
Balances(contracts, (9.7m).Tst(), (0.3m).Tst());
}
[Test]
public void CanTransferEth()
{
var blockCache = new BlockCache();
var geth = Ci.StartGethNode(blockCache, s => s.IsMiner());
geth.SendEth(user1.EthAddress, 1.Eth());
geth.SendEth(user2.EthAddress, 1.Eth());
Balances(geth, 1.Eth(), 1.Eth());
var geth1 = geth.WithDifferentAccount(user1);
var geth2 = geth.WithDifferentAccount(user2);
geth1.SendEth(user2.EthAddress, (0.5m).Eth());
Balances(geth, (0.5m).Eth(), (1.5m).Eth());
geth2.SendEth(user1.EthAddress, (0.2m).Eth());
Balances(geth, (0.7m).Eth(), (1.3m).Eth());
}
private void Balances(ICodexContracts contracts, TestToken one, TestToken two)
@ -47,5 +68,23 @@ namespace CodexReleaseTests.MarketTests
Assert.That(balance1, Is.EqualTo(one));
Assert.That(balance2, Is.EqualTo(two));
}
private void Balances(IGethNode geth, Ether one, Ether two)
{
var balance1 = geth.GetEthBalance(user1.EthAddress);
var balance2 = geth.GetEthBalance(user2.EthAddress);
InRange(balance1, one);
InRange(balance2, two);
}
private void InRange(Ether balance, Ether expected)
{
var gasTolerance = (0.001m).Eth();
var max = expected + gasTolerance;
var min = expected - gasTolerance;
Assert.That(balance, Is.LessThanOrEqualTo(max).And.GreaterThanOrEqualTo(min));
}
}
}