|
1 | 1 | using System; |
2 | | -using System.Collections.Generic; |
3 | 2 | using System.Collections.Immutable; |
4 | 3 | using System.Diagnostics.CodeAnalysis; |
5 | 4 | using System.Diagnostics.Contracts; |
|
13 | 12 | using Bencodex; |
14 | 13 | using Bencodex.Types; |
15 | 14 | using Libplanet.Common; |
16 | | -using Libplanet.Common.Serialization; |
17 | 15 | using Libplanet.Crypto; |
18 | 16 |
|
19 | 17 | namespace Libplanet.Types.Assets |
@@ -51,6 +49,8 @@ namespace Libplanet.Types.Assets |
51 | 49 | [Serializable] |
52 | 50 | public sealed class Currency : IEquatable<Currency>, ISerializable |
53 | 51 | { |
| 52 | + private static Codec _codec = new Codec(); |
| 53 | + |
54 | 54 | private readonly (BigInteger Major, BigInteger Minor)? _maximumSupply; |
55 | 55 |
|
56 | 56 | private HashDigest<SHA1>? _hash; |
@@ -151,65 +151,8 @@ private Currency(Dictionary dictionary) |
151 | 151 | } |
152 | 152 |
|
153 | 153 | private Currency(SerializationInfo info, StreamingContext context) |
| 154 | + : this(_codec.Decode((byte[])info.GetValue("ByteEncoded", typeof(byte[]))!)) |
154 | 155 | { |
155 | | - Ticker = info.GetValue<string>(nameof(Ticker)); |
156 | | - DecimalPlaces = info.GetValue<byte>(nameof(DecimalPlaces)); |
157 | | - |
158 | | - if (info.TryGetValue(nameof(Minters), out List<byte[]> minters)) |
159 | | - { |
160 | | - Minters = minters.Select(m => new Address(m)).ToImmutableHashSet(); |
161 | | - } |
162 | | - else |
163 | | - { |
164 | | - Minters = default; |
165 | | - } |
166 | | - |
167 | | - TotalSupplyTrackable = false; |
168 | | - if (info.TryGetValue(nameof(TotalSupplyTrackable), out bool totalSupplyTrackable)) |
169 | | - { |
170 | | - if (!totalSupplyTrackable) |
171 | | - { |
172 | | - throw new ArgumentException( |
173 | | - $"{nameof(TotalSupplyTrackable)} must be true if it exists in the" |
174 | | - + "SerializationInfo.", |
175 | | - nameof(TotalSupplyTrackable)); |
176 | | - } |
177 | | - |
178 | | - TotalSupplyTrackable = totalSupplyTrackable; |
179 | | - } |
180 | | - |
181 | | - if (info.TryGetValue(nameof(MaximumSupply), out (BigInteger, BigInteger) maximumSupply)) |
182 | | - { |
183 | | - if (!TotalSupplyTrackable) |
184 | | - { |
185 | | - throw new ArgumentException( |
186 | | - $"Maximum supply is not available for legacy untracked currencies.", |
187 | | - nameof(info)); |
188 | | - } |
189 | | - |
190 | | - _maximumSupply = maximumSupply; |
191 | | - } |
192 | | - else |
193 | | - { |
194 | | - _maximumSupply = null; |
195 | | - } |
196 | | - |
197 | | - if (_maximumSupply is var (major, minor)) |
198 | | - { |
199 | | - if (major < 0 || minor < 0) |
200 | | - { |
201 | | - var msg = $"Both the major ({major}) and minor ({minor}) units of" |
202 | | - + $" {nameof(maximumSupply)} must not be a negative number."; |
203 | | - throw new ArgumentException(msg, nameof(maximumSupply)); |
204 | | - } |
205 | | - |
206 | | - if (minor > 0 && Math.Floor(BigInteger.Log10(minor)) >= DecimalPlaces) |
207 | | - { |
208 | | - var msg = $"The given minor unit {minor} of the maximum supply value is too" |
209 | | - + $" big for the given decimal places {DecimalPlaces}."; |
210 | | - throw new ArgumentException(msg, nameof(minor)); |
211 | | - } |
212 | | - } |
213 | 156 | } |
214 | 157 |
|
215 | 158 | /// <summary> |
@@ -455,23 +398,7 @@ public static Currency Legacy( |
455 | 398 | /// <inheritdoc cref="ISerializable.GetObjectData(SerializationInfo, StreamingContext)"/> |
456 | 399 | void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) |
457 | 400 | { |
458 | | - info.AddValue(nameof(Ticker), Ticker); |
459 | | - info.AddValue(nameof(DecimalPlaces), DecimalPlaces); |
460 | | - |
461 | | - if (Minters is IImmutableSet<Address> minters) |
462 | | - { |
463 | | - info.AddValue(nameof(Minters), minters.Select(m => m.ToByteArray()).ToList()); |
464 | | - } |
465 | | - |
466 | | - if (_maximumSupply is { } maximumSupply) |
467 | | - { |
468 | | - info.AddValue(nameof(MaximumSupply), maximumSupply); |
469 | | - } |
470 | | - |
471 | | - if (TotalSupplyTrackable) |
472 | | - { |
473 | | - info.AddValue(nameof(TotalSupplyTrackable), TotalSupplyTrackable); |
474 | | - } |
| 401 | + info.AddValue("ByteEncoded", _codec.Encode(Serialize())); |
475 | 402 | } |
476 | 403 |
|
477 | 404 | /// <inheritdoc cref="object.ToString()"/> |
|
0 commit comments