Skip to content

Commit

Permalink
EthSmartContractIO: Create DataBuilder (#142)
Browse files Browse the repository at this point in the history
* - add `DataBuilder`

* - add tests

* - remove `GetMethodHash` method

* - cleanup

* - fix test

---------

Co-authored-by: Stan Goldin <[email protected]>
  • Loading branch information
ArdenHide and Lomet authored Feb 12, 2024
1 parent 140d7bc commit 0664abb
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 23 deletions.
30 changes: 30 additions & 0 deletions src/EthSmartContractIO/Builders/DataBuilder.cs
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 1 addition & 5 deletions src/EthSmartContractIO/ContractIO/ContractIO.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Nethereum.Util;
using EthSmartContractIO.Models;
using EthSmartContractIO.Models;

namespace EthSmartContractIO.ContractIO;

Expand Down Expand Up @@ -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];

/// <summary>
/// Gets the appropriate <see cref="IContractIO"/> instance for the given request.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/EthSmartContractIO/EthSmartContractIO.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<PackageReference Include="FluentValidation" Version="11.5.2" />
<PackageReference Include="Flurl.Http" Version="3.2.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Net.Web3.EthereumWallet" Version="1.0.0" />
<PackageReference Include="Nethereum.HdWallet" Version="4.14.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
Expand Down
9 changes: 9 additions & 0 deletions src/EthSmartContractIO/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -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];
}
63 changes: 63 additions & 0 deletions tests/EthSmartContractIO.Tests/Builders/DataBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
24 changes: 6 additions & 18 deletions tests/EthSmartContractIO.Tests/ContractIO/ContractIOTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="xunit" Version="2.4.2" />
Expand Down

0 comments on commit 0664abb

Please sign in to comment.