-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EthSmartContractIO: Create
DataBuilder
(#142)
* - add `DataBuilder` * - add tests * - remove `GetMethodHash` method * - cleanup * - fix test --------- Co-authored-by: Stan Goldin <[email protected]>
- Loading branch information
Showing
7 changed files
with
111 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
63
tests/EthSmartContractIO.Tests/Builders/DataBuilderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters