From fc2c9a2bcc6dc192fdaa5fd36fefb49b62091a83 Mon Sep 17 00:00:00 2001 From: Adegoke David <64401859+Blazebrain@users.noreply.github.com> Date: Fri, 14 Jun 2024 14:24:10 +0100 Subject: [PATCH] feat: Int overflow issue (#1482) --- cw_evm/lib/evm_chain_formatter.dart | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/cw_evm/lib/evm_chain_formatter.dart b/cw_evm/lib/evm_chain_formatter.dart index cb9b7346c..797728885 100644 --- a/cw_evm/lib/evm_chain_formatter.dart +++ b/cw_evm/lib/evm_chain_formatter.dart @@ -1,15 +1,13 @@ -import 'package:intl/intl.dart'; - -const evmChainAmountLength = 12; -const evmChainAmountDivider = 1000000000000; -final evmChainAmountFormat = NumberFormat() - ..maximumFractionDigits = evmChainAmountLength - ..minimumFractionDigits = 1; +import 'dart:math'; class EVMChainFormatter { + static int _divider = 0; + static int parseEVMChainAmount(String amount) { try { - return (double.parse(amount) * evmChainAmountDivider).round(); + final decimalLength = _getDividerForInput(amount); + _divider = decimalLength; + return (double.parse(amount) * pow(10, decimalLength)).round(); } catch (_) { return 0; } @@ -17,9 +15,19 @@ class EVMChainFormatter { static double parseEVMChainAmountToDouble(int amount) { try { - return amount / evmChainAmountDivider; + return amount / pow(10, _divider); } catch (_) { return 0; } } + + static int _getDividerForInput(String amount) { + final result = amount.split('.'); + if (result.length > 1) { + final decimalLength = result[1].length; + return decimalLength; + } else { + return 0; + } + } }