diff --git a/Framework/NethereumWorkflow/NethereumInteraction.cs b/Framework/NethereumWorkflow/NethereumInteraction.cs index 89a46ea2..c5873cee 100644 --- a/Framework/NethereumWorkflow/NethereumInteraction.cs +++ b/Framework/NethereumWorkflow/NethereumInteraction.cs @@ -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)); diff --git a/Framework/Utils/TestTokenExtensions.cs b/Framework/Utils/TestTokenExtensions.cs index 7104ad76..5220f929 100644 --- a/Framework/Utils/TestTokenExtensions.cs +++ b/Framework/Utils/TestTokenExtensions.cs @@ -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); - } } } diff --git a/ProjectPlugins/GethPlugin/GethNode.cs b/ProjectPlugins/GethPlugin/GethNode.cs index 970bb3b2..15fa0d4e 100644 --- a/ProjectPlugins/GethPlugin/GethNode.cs +++ b/ProjectPlugins/GethPlugin/GethNode.cs @@ -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(string contractAddress, TFunction function) where TFunction : FunctionMessage, new() diff --git a/Tests/CodexReleaseTests/MarketTests/JustTokensTest.cs b/Tests/CodexReleaseTests/MarketTests/JustTokensTest.cs index 093b5637..9fbf5c7a 100644 --- a/Tests/CodexReleaseTests/MarketTests/JustTokensTest.cs +++ b/Tests/CodexReleaseTests/MarketTests/JustTokensTest.cs @@ -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)); + } } }