diff --git a/src/EthSmartContractIO/Builders/DataBuilder.cs b/src/EthSmartContractIO/Builders/DataBuilder.cs new file mode 100644 index 0000000..fbd750c --- /dev/null +++ b/src/EthSmartContractIO/Builders/DataBuilder.cs @@ -0,0 +1,30 @@ +using System.Numerics; +using Nethereum.Hex.HexTypes; +using Net.Web3.EthereumWallet; +using EthSmartContractIO.Extensions; + +namespace EthSmartContractIO.Builders; + +public class DataBuilder +{ + private string data; + + public DataBuilder(string functionName) + { + data = functionName.ToMethodSignature(); + } + + public DataBuilder WithBigInteger(BigInteger parameter) + { + data += new HexBigInteger(parameter).HexValue[2..].PadLeft(64, '0'); + return this; + } + + public DataBuilder WithAddress(EthereumAddress parameter) + { + data += parameter.Address[2..].PadLeft(64, '0'); + return this; + } + + public string Build() => data; +} \ No newline at end of file diff --git a/src/EthSmartContractIO/ContractIO/ContractIO.cs b/src/EthSmartContractIO/ContractIO/ContractIO.cs index d77bde3..15d5f84 100644 --- a/src/EthSmartContractIO/ContractIO/ContractIO.cs +++ b/src/EthSmartContractIO/ContractIO/ContractIO.cs @@ -1,5 +1,4 @@ -using Nethereum.Util; -using EthSmartContractIO.Models; +using EthSmartContractIO.Models; namespace EthSmartContractIO.ContractIO; @@ -34,9 +33,6 @@ public ContractIO(IServiceProvider? serviceProvider) public virtual string ExecuteAction(RpcRequest request) => GetContractIO(request).RunContractAction(); - public static string GetMethodHash(string methodSignature) => - Sha3Keccack.Current.CalculateHash(methodSignature)[..8]; - /// /// Gets the appropriate instance for the given request. /// diff --git a/src/EthSmartContractIO/EthSmartContractIO.csproj b/src/EthSmartContractIO/EthSmartContractIO.csproj index 06d0bb0..1bb4672 100644 --- a/src/EthSmartContractIO/EthSmartContractIO.csproj +++ b/src/EthSmartContractIO/EthSmartContractIO.csproj @@ -40,6 +40,7 @@ + diff --git a/src/EthSmartContractIO/Extensions/StringExtensions.cs b/src/EthSmartContractIO/Extensions/StringExtensions.cs new file mode 100644 index 0000000..d4965d2 --- /dev/null +++ b/src/EthSmartContractIO/Extensions/StringExtensions.cs @@ -0,0 +1,9 @@ +using Nethereum.Util; + +namespace EthSmartContractIO.Extensions; + +public static class StringExtensions +{ + public static string ToMethodSignature(this string functionName) => + "0x" + new Sha3Keccack().CalculateHash(functionName)[..8]; +} \ No newline at end of file diff --git a/tests/EthSmartContractIO.Tests/Builders/DataBuilderTests.cs b/tests/EthSmartContractIO.Tests/Builders/DataBuilderTests.cs new file mode 100644 index 0000000..9698052 --- /dev/null +++ b/tests/EthSmartContractIO.Tests/Builders/DataBuilderTests.cs @@ -0,0 +1,63 @@ +using Xunit; +using System.Numerics; +using FluentAssertions; +using Net.Web3.EthereumWallet; +using EthSmartContractIO.Builders; +using EthSmartContractIO.Extensions; + +namespace EthSmartContractIO.Tests.Builders; + +public class DataBuilderTests +{ + private const string methodName = "transfer(address,uint256)"; + private readonly string methodSignature = methodName.ToMethodSignature(); + private readonly BigInteger bigInt = new(12345); + private readonly EthereumAddress ethereumAddress = new("0x1234567890123456789012345678901234567890"); + private readonly DataBuilder dataBuilder = new(methodName); + + [Fact] + internal void Constructor_InitializesMethodSignatureCorrectly() + { + var result = dataBuilder.Build(); + + result.Should().StartWith(methodSignature); + } + + [Fact] + internal void WithBigInteger_AppendsBigIntegerCorrectly() + { + var expected = bigInt.ToString("X").PadLeft(64, '0'); + + var result = dataBuilder.WithBigInteger(bigInt) + .Build(); + + result.Should().Contain(expected); + } + + [Fact] + internal void WithAddress_AppendsEthereumAddressCorrectly() + { + var expected = ethereumAddress.Address[2..].PadLeft(64, '0'); + + var result = dataBuilder.WithAddress(ethereumAddress) + .Build(); + + result.Should().Contain(expected); + } + + [Fact] + internal void Build_CreatesCorrectDataString() + { + var expectedBigInteger = bigInt.ToString("X").PadLeft(64, '0'); + var expectedAddress = ethereumAddress.Address[2..].PadLeft(64, '0'); + + var expectedData = $"{methodSignature}{expectedAddress}{expectedBigInteger}"; + + var result = dataBuilder + .WithAddress(ethereumAddress) + .WithBigInteger(bigInt) + .Build(); + + result.Should().Be(expectedData); + } +} \ No newline at end of file diff --git a/tests/EthSmartContractIO.Tests/ContractIO/ContractIOTests.cs b/tests/EthSmartContractIO.Tests/ContractIO/ContractIOTests.cs index e808174..6779a40 100644 --- a/tests/EthSmartContractIO.Tests/ContractIO/ContractIOTests.cs +++ b/tests/EthSmartContractIO.Tests/ContractIO/ContractIOTests.cs @@ -2,15 +2,15 @@ using Xunit; using Flurl.Http.Testing; using Newtonsoft.Json.Linq; -using Nethereum.Hex.HexTypes; using Nethereum.RPC.Eth.DTOs; +using Nethereum.Hex.HexTypes; using EthSmartContractIO.Gas; using EthSmartContractIO.Models; using EthSmartContractIO.Builders; -using EthSmartContractIO.Tests.Mocks; using EthSmartContractIO.Transaction; +using EthSmartContractIO.Tests.Mocks; -namespace EthSmartContractIO.ContractIO.Tests; +namespace EthSmartContractIO.Tests.ContractIO; public class ContractIOTests { @@ -44,7 +44,7 @@ internal void ExecuteAction_Read_ExpectedJsonString() .ForCallsTo(RpcUrl) .RespondWithJson(response); - var result = new ContractIO().ExecuteAction(readRequest); + var result = new EthSmartContractIO.ContractIO.ContractIO().ExecuteAction(readRequest); Assert.NotNull(result); Assert.Equal(response["result"]?.ToString(), result); @@ -68,7 +68,7 @@ internal void ExecuteAction_WriteWithMockServices_ExpectedTransactionHex() .AddTransactionSender(mockTransactionSender.Object) .Build(); - var result = new ContractIO(serviceProvider).ExecuteAction(writeRequest); + var result = new EthSmartContractIO.ContractIO.ContractIO(serviceProvider).ExecuteAction(writeRequest); Assert.NotNull(result); Assert.Equal("transactionHash", result); @@ -81,21 +81,9 @@ internal void ExecuteAction_WriteWithMockWeb3_ExpectedTransactionHex() .AddWeb3(MockWeb3.GetMock) .Build(); - var result = new ContractIO(serviceProvider).ExecuteAction(writeRequest); + var result = new EthSmartContractIO.ContractIO.ContractIO(serviceProvider).ExecuteAction(writeRequest); Assert.NotNull(result); Assert.Equal("transactionHash", result); } - - [Fact] - internal void GetMethodHash_TransferSignature_ExpectedHash() - { - const string methodSignature = "Transfer(address,address,uint256)"; - - const string transferSignature = "ddf252ad"; - - var actualValue = ContractIO.GetMethodHash(methodSignature); - - Assert.Equal(transferSignature, actualValue); - } } diff --git a/tests/EthSmartContractIO.Tests/EthSmartContractIO.Tests.csproj b/tests/EthSmartContractIO.Tests/EthSmartContractIO.Tests.csproj index 72241f1..3cc4816 100644 --- a/tests/EthSmartContractIO.Tests/EthSmartContractIO.Tests.csproj +++ b/tests/EthSmartContractIO.Tests/EthSmartContractIO.Tests.csproj @@ -12,6 +12,7 @@ +