mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-10-17 05:41:29 +02:00
feat: replace manual BigInt conversion with parseFixed for dEuro Savings (#2344)
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
BigInt parseFixed(String value, int? decimals) {
|
||||
decimals ??= 0;
|
||||
final multiplier = getMultiplier(decimals);
|
||||
|
||||
// Is it negative?
|
||||
final negative = (value.substring(0, 1) == "-");
|
||||
if (negative) value = value.substring(1);
|
||||
|
||||
if (value == ".") throw Exception("missing value, value, $value");
|
||||
|
||||
// Split it into a whole and fractional part
|
||||
final comps = value.split(".");
|
||||
if (comps.length > 2) {
|
||||
throw Exception("too many decimal points, value, $value");
|
||||
}
|
||||
|
||||
var whole = comps.isNotEmpty ? comps[0] : "0";
|
||||
var fraction = (comps.length == 2 ? comps[1] : "0").padRight(decimals, "0");
|
||||
|
||||
// Check the fraction doesn't exceed our decimals size
|
||||
if (fraction.length > multiplier.length - 1) {
|
||||
throw Exception(
|
||||
"fractional component exceeds decimals, underflow, parseFixed");
|
||||
}
|
||||
|
||||
final wholeValue = BigInt.parse(whole);
|
||||
final fractionValue = BigInt.parse(fraction);
|
||||
final multiplierValue = BigInt.parse(multiplier);
|
||||
|
||||
var wei = (wholeValue * multiplierValue) + fractionValue;
|
||||
|
||||
if (negative) wei *= BigInt.from(-1);
|
||||
|
||||
return wei;
|
||||
}
|
||||
|
||||
// Returns a string "1" followed by decimal "0"s
|
||||
String getMultiplier(int decimals) => "1".padRight(decimals + 1, "0");
|
||||
@@ -1,11 +1,10 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:cake_wallet/core/execution_state.dart';
|
||||
import 'package:cake_wallet/ethereum/ethereum.dart';
|
||||
import 'package:cake_wallet/store/app_store.dart';
|
||||
import 'package:cake_wallet/view_model/send/send_view_model_state.dart';
|
||||
import 'package:cw_core/pending_transaction.dart';
|
||||
import 'package:cw_core/wallet_type.dart';
|
||||
import 'package:cw_core/parse_fixed.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
|
||||
part 'deuro_view_model.g.dart';
|
||||
@@ -82,7 +81,7 @@ abstract class DEuroViewModelBase with Store {
|
||||
Future<void> prepareSavingsEdit(String amountRaw, bool isAdding) async {
|
||||
try {
|
||||
state = TransactionCommitting();
|
||||
final amount = BigInt.from(num.parse(amountRaw) * pow(10, 18));
|
||||
final amount = parseFixed(amountRaw, 18);
|
||||
final priority = _appStore.settingsStore.priority[WalletType.ethereum]!;
|
||||
transaction = await (isAdding
|
||||
? ethereum!.addDEuroSaving(_appStore.wallet!, amount, priority)
|
||||
|
||||
Reference in New Issue
Block a user