1
1
using System ;
2
+ using System . Diagnostics ;
2
3
using System . Globalization ;
3
4
using System . Numerics ;
4
5
using System . Text . RegularExpressions ;
5
6
6
- namespace TonSdk . Core {
7
- public class CoinsOptions {
7
+ namespace TonSdk . Core
8
+ {
9
+ public class CoinsOptions
10
+ {
8
11
public bool IsNano { get ; set ; }
9
12
public int Decimals { get ; set ; }
10
13
11
- public CoinsOptions ( bool IsNano = false , int Decimals = 9 ) {
14
+ public CoinsOptions ( bool IsNano = false , int Decimals = 9 )
15
+ {
12
16
this . IsNano = IsNano ;
13
17
this . Decimals = Decimals ;
14
18
}
15
19
}
16
20
17
- public class Coins {
21
+ public class Coins
22
+ {
18
23
private decimal Value { get ; set ; }
19
24
private int Decimals { get ; set ; }
20
25
private decimal Multiplier { get ; set ; }
@@ -24,24 +29,29 @@ public class Coins {
24
29
/// </summary>
25
30
/// <param name="value">The value of the coins.</param>
26
31
/// <param name="options">Optional options for customizing the coins.</param>
27
- public Coins ( object value , CoinsOptions ? options = null ) {
32
+ public Coins ( object value , CoinsOptions ? options = null )
33
+ {
28
34
bool isNano = false ;
29
35
int decimals = 9 ;
30
36
31
- if ( options != null ) {
37
+ if ( options != null )
38
+ {
32
39
isNano = options ? . IsNano != null ? options . IsNano : false ;
33
40
decimals = options ? . Decimals != null ? options . Decimals : 9 ;
34
41
}
35
42
36
- var _value = value ? . ToString ( ) . Replace ( "," , "." ) ;
43
+ if ( value is string valueStr )
44
+ {
45
+ value = valueStr . Replace ( "," , "." ) ;
46
+ }
37
47
38
- CheckCoinsType ( _value ) ;
48
+ CheckCoinsType ( value ) ;
39
49
CheckCoinsDecimals ( decimals ) ;
40
50
41
- decimal decimalValue = decimal . Parse ( _value , new CultureInfo ( "en-US" ) ) ;
42
-
51
+ TryConvertToDecimal ( value , out decimal decimalValue ) ;
43
52
int digitsValue = GetDigitsAfterDecimalPoint ( decimalValue ) ;
44
- if ( digitsValue > decimals ) {
53
+ if ( digitsValue > decimals )
54
+ {
45
55
throw new Exception (
46
56
$ "Invalid Coins value, decimals places \" { digitsValue } \" can't be greater than selected \" { decimals } \" ") ;
47
57
}
@@ -56,7 +66,8 @@ public Coins(object value, CoinsOptions? options = null) {
56
66
/// </summary>
57
67
/// <param name="coins">The Coins to add.</param>
58
68
/// <returns>A new Coins instance with the sum of the values.</returns>
59
- public Coins Add ( Coins coins ) {
69
+ public Coins Add ( Coins coins )
70
+ {
60
71
CheckCoins ( coins ) ;
61
72
CompareCoinsDecimals ( this , coins ) ;
62
73
@@ -69,7 +80,8 @@ public Coins Add(Coins coins) {
69
80
/// </summary>
70
81
/// <param name="coins">The Coins to subtract.</param>
71
82
/// <returns>A new Coins instance with the difference of the values.</returns>
72
- public Coins Sub ( Coins coins ) {
83
+ public Coins Sub ( Coins coins )
84
+ {
73
85
CheckCoins ( coins ) ;
74
86
CompareCoinsDecimals ( this , coins ) ;
75
87
@@ -82,7 +94,8 @@ public Coins Sub(Coins coins) {
82
94
/// </summary>
83
95
/// <param name="value">The value to multiply by.</param>
84
96
/// <returns>A new Coins instance with the multiplied value.</returns>
85
- public Coins Mul ( object value ) {
97
+ public Coins Mul ( object value )
98
+ {
86
99
CheckValue ( value ) ;
87
100
CheckConvertibility ( value ) ;
88
101
@@ -97,7 +110,8 @@ public Coins Mul(object value) {
97
110
/// </summary>
98
111
/// <param name="value">The value to divide by.</param>
99
112
/// <returns>A new Coins instance with the divided value.</returns>
100
- public Coins Div ( object value ) {
113
+ public Coins Div ( object value )
114
+ {
101
115
CheckValue ( value ) ;
102
116
CheckConvertibility ( value ) ;
103
117
@@ -112,7 +126,8 @@ public Coins Div(object value) {
112
126
/// </summary>
113
127
/// <param name="coins">The Coins to compare.</param>
114
128
/// <returns>True if the values are equal, false otherwise.</returns>
115
- public bool Eq ( Coins coins ) {
129
+ public bool Eq ( Coins coins )
130
+ {
116
131
CheckCoins ( coins ) ;
117
132
CompareCoinsDecimals ( this , coins ) ;
118
133
return Value == coins . Value ;
@@ -123,7 +138,8 @@ public bool Eq(Coins coins) {
123
138
/// </summary>
124
139
/// <param name="coins">The Coins to compare.</param>
125
140
/// <returns>True if the current value is greater, false otherwise.</returns>
126
- public bool Gt ( Coins coins ) {
141
+ public bool Gt ( Coins coins )
142
+ {
127
143
CheckCoins ( coins ) ;
128
144
CompareCoinsDecimals ( this , coins ) ;
129
145
return Value > coins . Value ;
@@ -134,7 +150,8 @@ public bool Gt(Coins coins) {
134
150
/// </summary>
135
151
/// <param name="coins">The Coins to compare.</param>
136
152
/// <returns>True if the current value is greater or equal, false otherwise.</returns>
137
- public bool Gte ( Coins coins ) {
153
+ public bool Gte ( Coins coins )
154
+ {
138
155
CheckCoins ( coins ) ;
139
156
CompareCoinsDecimals ( this , coins ) ;
140
157
return Value >= coins . Value ;
@@ -145,7 +162,8 @@ public bool Gte(Coins coins) {
145
162
/// </summary>
146
163
/// <param name="coins">The Coins to compare.</param>
147
164
/// <returns>True if the current value is less, false otherwise.</returns>
148
- public bool Lt ( Coins coins ) {
165
+ public bool Lt ( Coins coins )
166
+ {
149
167
CheckCoins ( coins ) ;
150
168
CompareCoinsDecimals ( this , coins ) ;
151
169
return Value < coins . Value ;
@@ -156,7 +174,8 @@ public bool Lt(Coins coins) {
156
174
/// </summary>
157
175
/// <param name="coins">The Coins to compare.</param>
158
176
/// <returns>True if the current value is less or equal, false otherwise.</returns>
159
- public bool Lte ( Coins coins ) {
177
+ public bool Lte ( Coins coins )
178
+ {
160
179
CheckCoins ( coins ) ;
161
180
CompareCoinsDecimals ( this , coins ) ;
162
181
return Value <= coins . Value ;
@@ -190,7 +209,8 @@ public bool Lte(Coins coins) {
190
209
/// Returns a string representation of the Coins value.
191
210
/// </summary>
192
211
/// <returns>A string representation of the Coins value.</returns>
193
- public override string ToString ( ) {
212
+ public override string ToString ( )
213
+ {
194
214
decimal value = Value / Multiplier ;
195
215
string formattedValue = value . ToString ( "F" + Decimals ) ;
196
216
@@ -203,66 +223,96 @@ public override string ToString() {
203
223
return coins ;
204
224
}
205
225
206
- private static void CheckCoinsType ( object value ) {
226
+ private static void CheckCoinsType ( object value )
227
+ {
207
228
if ( IsValid ( value ) && IsConvertable ( value ) ) return ;
208
229
if ( IsCoins ( value ) ) return ;
209
230
210
231
throw new Exception ( "Invalid Coins value" ) ;
211
232
}
212
233
213
- private static void CheckCoinsDecimals ( int decimals ) {
214
- if ( decimals < 0 || decimals > 18 ) {
234
+ private static void CheckCoinsDecimals ( int decimals )
235
+ {
236
+ if ( decimals < 0 || decimals > 18 )
237
+ {
215
238
throw new Exception ( "Invalid decimals value, must be 0-18" ) ;
216
239
}
217
240
}
218
241
219
- private static void CheckCoins ( Coins value ) {
242
+ private static void CheckCoins ( Coins value )
243
+ {
220
244
//if (IsCoins(value)) return;
221
245
//throw new Exception("Invalid value");
222
246
}
223
247
224
- private static void CompareCoinsDecimals ( Coins a , Coins b ) {
225
- if ( a . Decimals != b . Decimals ) {
248
+ private static void CompareCoinsDecimals ( Coins a , Coins b )
249
+ {
250
+ if ( a . Decimals != b . Decimals )
251
+ {
226
252
throw new Exception ( "Can't perform mathematical operation of Coins with different decimals" ) ;
227
253
}
228
254
}
229
255
230
- private static void CheckValue ( object value ) {
256
+ private static void CheckValue ( object value )
257
+ {
231
258
if ( IsValid ( value ) ) return ;
232
259
throw new Exception ( "Invalid value" ) ;
233
260
}
234
261
235
- private static void CheckConvertibility ( object value ) {
262
+ private static void CheckConvertibility ( object value )
263
+ {
236
264
if ( IsConvertable ( value ) ) return ;
237
265
238
266
throw new Exception ( "Invalid value" ) ;
239
267
}
240
268
241
- private static bool IsValid ( object value ) {
269
+ private static bool IsValid ( object value )
270
+ {
242
271
return value is string || value is int || value is decimal || value is double || value is float ||
243
272
value is long ;
244
273
}
245
274
246
- private static bool IsConvertable ( object value ) {
247
- try {
248
- decimal . Parse ( value . ToString ( ) , new CultureInfo ( "en-US" ) ) ;
275
+ private static bool IsConvertable ( object value )
276
+ {
277
+ return TryConvertToDecimal ( value , out _ ) ;
278
+ }
279
+
280
+ private static bool TryConvertToDecimal ( object value , out decimal result )
281
+ {
282
+ var strValue = value . ToString ( ) ;
283
+ if ( double . TryParse ( strValue , NumberStyles . Float , CultureInfo . InvariantCulture ,
284
+ out var doubleValue ) )
285
+ {
286
+ result = ( decimal ) doubleValue ;
249
287
return true ;
250
288
}
251
- catch ( Exception ) {
252
- return false ;
289
+
290
+ if ( decimal . TryParse ( strValue , NumberStyles . Float , CultureInfo . InvariantCulture ,
291
+ out var decimalValue ) )
292
+ {
293
+ result = decimalValue ;
294
+ return true ;
253
295
}
296
+
297
+ result = 0 ;
298
+ return false ;
254
299
}
255
300
256
- private static bool IsCoins ( object value ) {
301
+
302
+ private static bool IsCoins ( object value )
303
+ {
257
304
return value is Coins ;
258
305
}
259
306
260
- private static int GetDigitsAfterDecimalPoint ( decimal number ) {
307
+ private static int GetDigitsAfterDecimalPoint ( decimal number )
308
+ {
261
309
string [ ] parts = number . ToString ( new CultureInfo ( "en-US" ) ) . Split ( '.' ) ;
262
- if ( parts . Length == 2 ) {
310
+ if ( parts . Length == 2 )
311
+ {
263
312
return parts [ 1 ] . Length ;
264
313
}
265
- else {
314
+ else
315
+ {
266
316
return 0 ;
267
317
}
268
318
}
@@ -273,7 +323,8 @@ private static int GetDigitsAfterDecimalPoint(decimal number) {
273
323
/// <param name="value">The value in nano.</param>
274
324
/// <param name="decimals">The number of decimal places.</param>
275
325
/// <returns>A new Coins instance representing the value in nano.</returns>
276
- public static Coins FromNano ( object value , int decimals = 9 ) {
326
+ public static Coins FromNano ( object value , int decimals = 9 )
327
+ {
277
328
CheckCoinsType ( value ) ;
278
329
CheckCoinsDecimals ( decimals ) ;
279
330
@@ -284,14 +335,15 @@ public static Coins FromNano(object value, int decimals = 9) {
284
335
/// Converts the value of the Coins instance to a BigInteger.
285
336
/// </summary>
286
337
/// <returns>A BigInteger representation of the value.</returns>
287
- public BigInteger ToBigInt ( ) {
338
+ public BigInteger ToBigInt ( )
339
+ {
288
340
return new BigInteger ( Value ) ;
289
341
}
290
-
342
+
291
343
/// <summary>
292
344
/// Return the pointed value of the Coins instance in decimal.
293
345
/// </summary>
294
346
/// <returns>A Decimal representation of the value.</returns>
295
347
public decimal ToDecimal ( ) => Value / Multiplier ;
296
348
}
297
- }
349
+ }
0 commit comments