-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathTableExtensions.cs
357 lines (309 loc) · 10.6 KB
/
TableExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#nullable enable
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.Linq;
using System.Numerics;
using Libplanet.Crypto;
using Libplanet.Types.Assets;
using Nekoyume.TableData.Swap;
namespace Nekoyume.TableData
{
public static class TableExtensions
{
public static bool TryParseDecimal(string value, out decimal result) =>
decimal.TryParse(value, NumberStyles.Number, NumberFormatInfo.InvariantInfo, out result);
public static bool TryParseFloat(string value, out float result) =>
float.TryParse(value, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out result);
public static bool TryParseLong(string value, out long result) =>
long.TryParse(value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out result);
public static bool TryParseInt(string value, out int result) =>
int.TryParse(value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out result);
public static bool TryParseBigInteger(string value, out BigInteger result) =>
BigInteger.TryParse(value, out result);
public static bool ParseBool(string value, bool defaultValue) =>
bool.TryParse(value, out var result) ? result : defaultValue;
public static int ParseInt(string value)
{
if (TryParseInt(value, out var result))
{
return result;
}
throw new ArgumentException(value);
}
public static int ParseInt(string value, int defaultValue) =>
TryParseInt(value, out var result) ? result : defaultValue;
public static decimal ParseDecimal(string value)
{
if (TryParseDecimal(value, out var result))
{
return result;
}
throw new ArgumentException(value);
}
public static decimal ParseDecimal(string value, decimal defaultValue) =>
TryParseDecimal(value, out var result) ? result : defaultValue;
public static long ParseLong(string value)
{
if (TryParseLong(value, out var result))
{
return result;
}
throw new ArgumentException(value);
}
public static long ParseLong(string value, long defaultValue) =>
TryParseLong(value, out var result) ? result : defaultValue;
public static BigInteger ParseBigInteger(string value)
{
if (BigInteger.TryParse(value, out var result))
{
return result;
}
throw new ArgumentException(value);
}
/// <summary>
/// Parse a fraction from a string.
/// </summary>
/// <param name="value">
/// The string to parse.
/// Fraction should be in the form of "numerator/denominator".
/// </param>
/// <returns>
/// The parsed fraction.
/// </returns>
/// <exception cref="ArgumentException">
/// The string is not a valid fraction.
/// </exception>
public static SwapRateSheet.Fraction ParseFraction(string value)
{
if (TryParseFraction(value, out var result))
{
return result;
}
throw new ArgumentException(value);
}
/// <summary>
/// Try to parse a fraction from a string.
/// </summary>
/// <param name="value">
/// The string to parse.
/// Fraction should be in the form of "numerator/denominator".
/// </param>
/// <param name="result">
/// The parsed fraction.
/// </param>
/// <returns></returns>
public static bool TryParseFraction(string value, out SwapRateSheet.Fraction result)
{
result = new SwapRateSheet.Fraction(BigInteger.One, BigInteger.One);
List<string> fractionStrings = value.Split("/").ToList();
if (fractionStrings.Count != 2)
{
return false;
}
if (!BigInteger.TryParse(fractionStrings[0].Trim(), out var numerator))
{
return false;
}
if (!BigInteger.TryParse(fractionStrings[1].Trim(), out var denominator))
{
return false;
}
result = new SwapRateSheet.Fraction(numerator, denominator);
return true;
}
/// <summary>
/// Parse a currency from a string.
/// </summary>
/// <param name="value">
/// The string to parse.
/// Currency should be in the form of "ticker;decimalPlaces;minters;totalSupplyTractable;maximumSupply".
/// </param>
/// <returns>
/// The parsed currency.
/// </returns>
/// <exception cref="ArgumentException">
/// The string is not a valid currency.
/// </exception>
public static Currency ParseCurrency(string value)
{
if (TryParseCurrency(value, out var result))
{
return result;
}
throw new ArgumentException(value);
}
/// <summary>
/// Try to parse a currency from a string.
/// </summary>
/// <param name="value">
/// The string to parse.
/// Currency should be in the form of "ticker;decimalPlaces;minters;totalSupplyTractable;maximumSupply".
/// </param>
/// <param name="currency">
/// The parsed currency.
/// </param>
/// <returns>
/// Whether the parsing is successful.
/// </returns>
public static bool TryParseCurrency(string value, out Currency currency)
{
currency = default;
List<string> currencyStrings = value.Split(";").Select(v => v.Trim()).ToList();
if (currencyStrings.Count != 5)
{
return false;
}
string ticker = currencyStrings[0];
if (!TryParseInt(currencyStrings[1], out var decimalPlacesInt))
{
return false;
}
byte decimalPlaces = (byte)decimalPlacesInt;
if (!TryParseMinters(currencyStrings[2], out var minters))
{
return false;
}
bool totalSupplyTractable = ParseBool(currencyStrings[3], true);
if (!TryParseMaximumSupply(currencyStrings[4], out var maximumSupply))
{
return false;
}
if (!totalSupplyTractable)
{
if (maximumSupply.HasValue)
{
return false;
}
try
{
currency = Currency.Legacy(ticker, decimalPlaces, minters);
return true;
}
catch
{
return false;
}
}
if (maximumSupply.HasValue)
{
try
{
currency = Currency.Capped(ticker, decimalPlaces, maximumSupply.Value, minters);
return true;
}
catch
{
return false;
}
}
try
{
currency = Currency.Uncapped(ticker, decimalPlaces, minters);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Try to parse a set of minters from a string.
/// </summary>
/// <param name="value">
/// The string to parse.
/// Minters should be in the form of "minter1:minter2:minter3...".
/// </param>
/// <param name="result">
/// The parsed minters.
/// </param>
/// <returns>
/// Whether the parsing is successful.
/// </returns>
private static bool TryParseMinters(string value, out ImmutableHashSet<Address>? result)
{
result = null;
if (value.ToLowerInvariant() == "null")
{
return true;
}
List<string> mintersStrings = value.Split(":").ToList();
result = ImmutableHashSet<Address>.Empty;
foreach (string minterString in mintersStrings)
{
if (TryParseAddress(minterString, out var minter))
{
result = result.Add(minter);
}
else
{
result = null;
return false;
}
}
return true;
}
/// <summary>
/// Try to parse a maximum supply from a string.
/// </summary>
/// <param name="value">
/// The string to parse.
/// Maximum supply should be in the form of "major:minor".
/// </param>
/// <param name="result">
/// The parsed maximum supply.
/// </param>
/// <returns>
/// Whether the parsing is successful.
/// </returns>
private static bool TryParseMaximumSupply(string value, out (BigInteger Major, BigInteger Minor)? result)
{
result = null;
if (value.ToLowerInvariant() == "null")
{
return true;
}
List<string> maximumSupplyStrings = value.Split(":").ToList();
if (maximumSupplyStrings.Count != 2)
{
return false;
}
if (!BigInteger.TryParse(maximumSupplyStrings[0].Trim(), out var major))
{
return false;
}
if (!BigInteger.TryParse(maximumSupplyStrings[1].Trim(), out var minor))
{
return false;
}
result = (major, minor);
return true;
}
/// <summary>
/// Try to parse an address from a string.
/// </summary>
/// <param name="value">
/// The string to parse.
/// </param>
/// <param name="result">
/// The parsed address.
/// </param>
/// <returns>
/// Whether the parsing is successful.
/// </returns>
private static bool TryParseAddress(string value, out Address result)
{
try
{
result = new Address(value);
return true;
}
catch
{
result = default;
return false;
}
}
}
}