diff --git a/Libplanet.Tests/CurrencyTest.cs b/Libplanet.Tests/CurrencyTest.cs index e60be23be8..d1564b8eba 100644 --- a/Libplanet.Tests/CurrencyTest.cs +++ b/Libplanet.Tests/CurrencyTest.cs @@ -125,5 +125,19 @@ public void Serializable() Assert.Equal(currency.Hash, deserialized.Hash); } } + + [Fact] + public void Equal() + { + var currencyA = new Currency(ticker: "GOLD", minter: AddressA); + var currencyB = new Currency(ticker: "GOLD", minter: AddressA); + var currencyC = new Currency(ticker: "GOLD", minter: AddressB); + var currencyD = new Currency(ticker: "SILVER", minter: AddressA); + + Assert.Equal(currencyA, currencyA); + Assert.Equal(currencyA, currencyB); + Assert.NotEqual(currencyA, currencyC); + Assert.NotEqual(currencyA, currencyD); + } } } diff --git a/Libplanet/Currency.cs b/Libplanet/Currency.cs index cfc3935ee3..c695307b69 100644 --- a/Libplanet/Currency.cs +++ b/Libplanet/Currency.cs @@ -21,7 +21,7 @@ namespace Libplanet /// EUR (Euro), not values like $100 or €100. /// [Serializable] - public readonly struct Currency : ISerializable + public readonly struct Currency : ISerializable, IEquatable { /// /// The ticker symbol, e.g., "USD". @@ -124,6 +124,29 @@ public void GetObjectData(SerializationInfo info, StreamingContext context) public override string ToString() => $"{Ticker} ({Hash})"; + [Pure] + public override int GetHashCode() + { + unchecked + { + return -1545866855 + Hash.GetHashCode(); + } + } + + [Pure] + public override bool Equals(object? obj) + { + return obj is IEquatable other + ? other.Equals(this) + : false; + } + + [Pure] + public bool Equals(Currency other) + { + return Hash.Equals(other.Hash); + } + [Pure] private HashDigest GetHash() {