Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add name helpers #77

Merged
6 commits merged into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Unreleased changes
- Added
- New GRPC-endpoints: `GetBlocks`, `GetFinalizedBlocks`, `GetBranches`, `GetAncestors`, `GetBlockPendingUpdates`
- Added helpers to get new type `ContractIdentifier` on `ReceiveName` and `ContractName`. This new type only holds the contract name part of `ReceiveName` and `ContractName`.
Also added helper to get entrypoint on `ReceiveName`.

## 4.1.0
- Bugfix
Expand Down
7 changes: 7 additions & 0 deletions src/Types/ContractIdentifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Concordium.Sdk.Types;

/// <summary>
/// Contains the identification name of the contract.
/// </summary>
/// <param name="ContractName">Name of contract</param>
public sealed record ContractIdentifier(string ContractName);
6 changes: 6 additions & 0 deletions src/Types/ContractName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public static bool TryParse(string name, out (ContractName? ContractName, Valida
return validate;
}

/// <summary>
/// Get the contract name part of <see cref="Name"/>.
/// </summary>
/// <returns>Contract identification name</returns>
public ContractIdentifier GetContractName() => new(this.Name[(this.Name.IndexOf('_') + 1)..]);

/// <summary>
/// Validation error of contract name.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions src/Types/EntryPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Concordium.Sdk.Types;

/// <summary>
/// Entry point on a smart contract.
/// </summary>
/// <param name="Name">Entry point name</param>
public sealed record EntryPoint(string Name);
12 changes: 12 additions & 0 deletions src/Types/ReceiveName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ public static bool TryParse(string name, out (ReceiveName? ReceiveName, Validati
return validate;
}

/// <summary>
/// Get the contract name part of <see cref="Receive"/>.
/// </summary>
/// <returns>Contract identification name</returns>
public ContractIdentifier GetContractName() => new(this.Receive[..this.Receive.IndexOf('.')]);

/// <summary>
/// Get entrypoint part of <see cref="Receive"/> which is the entrypoint called on the contract.
/// </summary>
/// <returns>Entrypoint</returns>
public EntryPoint GetEntrypoint() => new(this.Receive[(this.Receive.IndexOf('.') + 1)..]);

/// <summary>
/// Validation error of receive name.
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions tests/UnitTests/Types/ContractNameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,19 @@ public void WhenCallingTryParse_ValidatesAndParsesReceiveName(
initName.Should().BeNull();
}
}

[Fact]
public void WhenGetContractNamePart_ThenReturnContractName()
{
// Arrange
const string prefix = "init_";
const string expected = "awesome";
_ = ContractName.TryParse($"{prefix}{expected}", out var result);

// Act
var actual = result.ContractName!.GetContractName();

// Assert
actual.ContractName.Should().Be(expected);
}
}
32 changes: 32 additions & 0 deletions tests/UnitTests/Types/ReceiveNameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,36 @@ public void WhenCallingTryParse_ValidatesAndParsesReceiveName(
receiveName.Should().BeNull();
}
}

[Fact]
public void WhenGetContractNamePart_ThenReturnName()
{
// Arrange
const string contractName = "some_contract";
const string contractEntrypoint = "some_entrypoint";

_ = ReceiveName.TryParse($"{contractName}.{contractEntrypoint}", out var result);

// Act
var actual = result.ReceiveName!.GetContractName();

// Assert
actual.ContractName.Should().Be(contractName);
}

[Fact]
public void WhenGetEntrypointPart_ThenReturnEntrypoint()
{
// Arrange
const string contractName = "some_contract";
const string contractEntrypoint = "some_entrypoint";

_ = ReceiveName.TryParse($"{contractName}.{contractEntrypoint}", out var result);

// Act
var actual = result.ReceiveName!.GetEntrypoint();

// Assert
actual.Name.Should().Be(contractEntrypoint);
}
}
Loading