feat: Int overflow issue (#1482)

Adegoke David committed Jun 14, 2024 at 14:24 UTC fc2c9a2bcc6dc192fdaa5fd36fefb49b62091a83
1 file changed +17 -9
cw_evm/lib/evm_chain_formatter.dart
+17 -9
@@ -1,15 +1,13 @@
1 -import 'package:intl/intl.dart';
2 -
3 -const evmChainAmountLength = 12;
4 -const evmChainAmountDivider = 1000000000000;
5 -final evmChainAmountFormat = NumberFormat()
6 - ..maximumFractionDigits = evmChainAmountLength
7 - ..minimumFractionDigits = 1;
1 +import 'dart:math';
2
3 class EVMChainFormatter {
4 + static int _divider = 0;
5 +
6 static int parseEVMChainAmount(String amount) {
7 try {
12 - return (double.parse(amount) * evmChainAmountDivider).round();
8 + final decimalLength = _getDividerForInput(amount);
9 + _divider = decimalLength;
10 + return (double.parse(amount) * pow(10, decimalLength)).round();
11 } catch (_) {
12 return 0;
13 }
@@ -17,9 +15,19 @@ class EVMChainFormatter {
15
16 static double parseEVMChainAmountToDouble(int amount) {
17 try {
20 - return amount / evmChainAmountDivider;
18 + return amount / pow(10, _divider);
19 } catch (_) {
20 return 0;
21 }
22 }
23 +
24 + static int _getDividerForInput(String amount) {
25 + final result = amount.split('.');
26 + if (result.length > 1) {
27 + final decimalLength = result[1].length;
28 + return decimalLength;
29 + } else {
30 + return 0;
31 + }
32 + }
33 }