From bf58e8e3d13cb911bf233dfd8fc443519aa896c6 Mon Sep 17 00:00:00 2001 From: poka Date: Mon, 12 Sep 2022 12:04:08 +0200 Subject: [PATCH] refactor Ud ratio compute --- lib/globals.dart | 2 ++ lib/providers/home.dart | 5 ++++- lib/providers/substrate_sdk.dart | 17 ++++++++++++----- lib/screens/activity.dart | 2 +- lib/screens/settings.dart | 2 +- lib/screens/wallet_view.dart | 5 ----- 6 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/globals.dart b/lib/globals.dart index b05b616..542ed8e 100644 --- a/lib/globals.dart +++ b/lib/globals.dart @@ -48,3 +48,5 @@ String currencyName = 'ĞD'; const debugPin = true; String indexerEndpoint = ''; +late double balanceRatio; +late int udValue; diff --git a/lib/providers/home.dart b/lib/providers/home.dart index 8d62040..ee2b3d8 100644 --- a/lib/providers/home.dart +++ b/lib/providers/home.dart @@ -13,6 +13,7 @@ import 'package:gecko/globals.dart'; import 'package:gecko/models/wallet_data.dart'; import 'package:gecko/models/widgets_keys.dart'; import 'package:gecko/providers/my_wallets.dart'; +import 'package:gecko/providers/substrate_sdk.dart'; import 'package:gecko/providers/wallet_options.dart'; import 'package:gecko/providers/wallets_profiles.dart'; import 'package:gecko/screens/myWallets/unlocking_wallet.dart'; @@ -64,10 +65,12 @@ class HomeProvider with ChangeNotifier { } } - Future changeCurrencyUnit() async { + Future changeCurrencyUnit(BuildContext context) async { + final sub = Provider.of(context, listen: false); final bool isUdUnit = configBox.get('isUdUnit') ?? false; await configBox.put('isUdUnit', !isUdUnit); balanceCache = {}; + sub.getBalanceRatio(); notifyListeners(); } diff --git a/lib/providers/substrate_sdk.dart b/lib/providers/substrate_sdk.dart index 497d540..b4ed980 100644 --- a/lib/providers/substrate_sdk.dart +++ b/lib/providers/substrate_sdk.dart @@ -160,6 +160,13 @@ class SubstrateSdk with ChangeNotifier { return udValue; } + Future getBalanceRatio() async { + udValue = await getUdValue(); + balanceRatio = + (configBox.get('isUdUnit') ?? false) ? round(udValue / 100, 6) : 1; + return balanceRatio; + } + Future> getBalance(String address) async { // log.d('currencyParameters: $currencyParameters'); @@ -192,10 +199,6 @@ class SubstrateSdk with ChangeNotifier { final int transferableBalance = (balanceGlobal['data']['free'] + unclaimedUds); - final bool isUdUnit = configBox.get('isUdUnit') ?? false; - final udValue = await getUdValue(); - final double balanceRatio = isUdUnit ? round(udValue / 1000, 6) : 1; - // log.d('udValue: $udValue'); Map finalBalances = { @@ -428,6 +431,8 @@ class SubstrateSdk with ChangeNotifier { }); await initCurrencyParameters(); + await getBalanceRatio(); + notifyListeners(); homeProvider.changeMessage( "wellConnectedToNode" @@ -691,7 +696,6 @@ class SubstrateSdk with ChangeNotifier { required double amount, required String password}) async { transactionStatus = ''; - final int amountUnit = (amount * 100).toInt(); final sender = await _setSender(fromAddress); @@ -710,12 +714,15 @@ class SubstrateSdk with ChangeNotifier { txOptions = [destAddress, false]; tx2 = 'api.tx.balances.transferAll("$destAddress", false)'; } else { + int amountUnit; if (isUdUnit) { palette = 'universalDividend'; call = 'transferUd'; + amountUnit = (amount * 1000).toInt(); } else { palette = 'balances'; call = 'transferKeepAlive'; + amountUnit = (amount * 100).toInt(); } txOptions = [destAddress, amountUnit]; tx2 = 'api.tx.$palette.$call("$destAddress", $amountUnit)'; diff --git a/lib/screens/activity.dart b/lib/screens/activity.dart index 1eb8ba2..8f0736a 100644 --- a/lib/screens/activity.dart +++ b/lib/screens/activity.dart @@ -281,7 +281,7 @@ class ActivityScreen extends StatelessWidget with ChangeNotifier { amount = repository[4] == 'RECEIVED' ? repository[3] : repository[3] * -1; if (isUdUnit) { - amount = round(amount / (sub.udValue / 1000)); + amount = round(amount / balanceRatio); finalAmount = 'ud'.tr(args: ['$amount ']); } else { finalAmount = '$amount $currencyName'; diff --git a/lib/screens/settings.dart b/lib/screens/settings.dart index 4ce6de2..4cb9f90 100644 --- a/lib/screens/settings.dart +++ b/lib/screens/settings.dart @@ -85,7 +85,7 @@ class SettingsScreen extends StatelessWidget { return InkWell( key: keyUdUnit, onTap: () async { - await homeProvider.changeCurrencyUnit(); + await homeProvider.changeCurrencyUnit(context); }, child: SizedBox( height: 50, diff --git a/lib/screens/wallet_view.dart b/lib/screens/wallet_view.dart index 0b79288..93132f0 100644 --- a/lib/screens/wallet_view.dart +++ b/lib/screens/wallet_view.dart @@ -398,7 +398,6 @@ void paymentPopup(BuildContext context, String toAddress) { final myWalletProvider = Provider.of(context, listen: false); - final sub = Provider.of(context, listen: false); const double shapeSize = 20; WalletData? defaultWallet = myWalletProvider.getDefaultWallet(); @@ -406,10 +405,6 @@ void paymentPopup(BuildContext context, String toAddress) { bool canValidate = false; - final bool isUdUnit = configBox.get('isUdUnit') ?? false; - final udValue = sub.udValue; - final double balanceRatio = isUdUnit ? round(udValue / 1000, 6) : 1; - final toWalletData = myWalletProvider.getWalletDataByAddress(toAddress); Future executeTransfert() async {