From 99c559d38c5ed3e479c444441ba8e93fdb019e91 Mon Sep 17 00:00:00 2001 From: poka Date: Thu, 11 Aug 2022 19:19:50 +0200 Subject: [PATCH] improve balance results; workaround batch (wip) --- lib/providers/generate_wallets.dart | 11 +++-- lib/providers/substrate_sdk.dart | 76 +++++++++++++++++++++++------ lib/providers/wallet_options.dart | 11 +++-- lib/screens/wallet_view.dart | 8 +-- 4 files changed, 77 insertions(+), 29 deletions(-) diff --git a/lib/providers/generate_wallets.dart b/lib/providers/generate_wallets.dart index 65b8982..871c177 100644 --- a/lib/providers/generate_wallets.dart +++ b/lib/providers/generate_wallets.dart @@ -385,19 +385,20 @@ class GenerateWalletsProvider with ChangeNotifier { } for (var derivationNbr in [for (var i = 0; i < numberScan; i += 1) i]) { - final addressData = await sub.sdk.api.keyring.addressFromMnemonic(sub.ss58, + final addressData = await sub.sdk.api.keyring.addressFromMnemonic( + sub.ss58, cryptoType: CryptoType.sr25519, mnemonic: generatedMnemonic!, derivePath: '//$derivationNbr'); final balance = await sub.getBalance(addressData.address!).timeout( const Duration(seconds: 1), - onTimeout: () => 0, + onTimeout: () => {}, ); // const balance = 0; log.d(balance); - if (balance != 0) { + if (balance != {}) { isAlive = true; String walletName = scanedWalletNumber == 0 ? 'currentWallet'.tr() @@ -431,11 +432,11 @@ class GenerateWalletsProvider with ChangeNotifier { final balance = await sub.getBalance(addressData.address!).timeout( const Duration(seconds: 1), - onTimeout: () => 0, + onTimeout: () => {}, ); log.d(balance); - if (balance != 0) { + if (balance != {}) { String walletName = 'myRootWallet'.tr(); await sub.importAccount( mnemonic: '', fromMnemonic: true, password: pin.text); diff --git a/lib/providers/substrate_sdk.dart b/lib/providers/substrate_sdk.dart index f60a1fb..a60f322 100644 --- a/lib/providers/substrate_sdk.dart +++ b/lib/providers/substrate_sdk.dart @@ -78,9 +78,8 @@ class SubstrateSdk with ChangeNotifier { 'batchAll', sender, ); - List txOptions = calls; - return [txInfo, txOptions]; + return [txInfo, calls]; } TxSenderData _setSender() { @@ -99,7 +98,9 @@ class SubstrateSdk with ChangeNotifier { for (var element in keyring.allAccounts) { final account = AddressInfo(address: element.address); - account.balance = await getBalance(element.address!); + final globalBalance = await getBalance(element.address!); + account.balance = globalBalance['transferableBalance']!; + result.add(account); } @@ -164,7 +165,7 @@ class SubstrateSdk with ChangeNotifier { // return balance; // } - Future getBalance(String address) async { + Future> getBalance(String address) async { // Get onchain storage values final Map balanceGlobal = await getStorage('system.account("$address")'); final int? idtyIndex = @@ -178,22 +179,26 @@ class SubstrateSdk with ChangeNotifier { await getStorage('universalDividend.pastReevals()'); // Compute amount of claimable UDs - final int newUdsAmount = _computeClaimUds(currentUdIndex, + final int unclaimedUds = _computeUnclaimUds(currentUdIndex, idtyData?['data']?['firstEligibleUd'] ?? 0, pastReevals); // Calculate transferable and potential balance final int transferableBalance = - (balanceGlobal['data']['free'] + newUdsAmount); - final int potentialBalance = - (balanceGlobal['data']['reserved'] + transferableBalance); + (balanceGlobal['data']['free'] + unclaimedUds); - log.i( - 'transferableBalance: $transferableBalance --- potentialBalance: $potentialBalance'); + Map finalBalances = { + 'transferableBalance': transferableBalance / 100, + 'free': balanceGlobal['data']['free'] / 100, + 'unclaimedUds': unclaimedUds / 100, + 'reserved': balanceGlobal['data']['reserved'] / 100, + }; - return transferableBalance / 100; + // log.i(finalBalances); + + return finalBalances; } - int _computeClaimUds( + int _computeUnclaimUds( int currentUdIndex, int firstEligibleUd, List pastReevals) { int totalAmount = 0; @@ -618,9 +623,50 @@ class SubstrateSdk with ChangeNotifier { fromPubkey!.keys.first, ); - final txInfo = TxInfoData( - 'balances', amount == -1 ? 'transferAll' : 'transferKeepAlive', sender); - final txOptions = [destAddress, amount == -1 ? false : amountUnit]; + final globalBalance = await getBalance(fromAddress); + TxInfoData txInfo; + List txOptions; + + log.d(globalBalance); + + // if (globalBalance['unclaimedUds'] != 0) { + // claimUDs(password); + // } + + // txInfo = TxInfoData( + // 'balances', amount == -1 ? 'transferAll' : 'transferKeepAlive', sender); + // txOptions = [destAddress, amount == -1 ? false : amountUnit]; + + if (globalBalance['unclaimedUds'] == 0) { + txInfo = TxInfoData('balances', + amount == -1 ? 'transferAll' : 'transferKeepAlive', sender); + txOptions = [destAddress, amount == -1 ? false : amountUnit]; + } else { + txInfo = TxInfoData( + 'utility', + 'batchAll', + sender, + ); + + txOptions = [ + ['api.tx.universalDividend.claimUds()'] + ]; + // 'balances.transferKeepAlive($destAddress, $amountUnit)' + // amount == -1 + // ? 'balances.transferAll(false)' + // : 'balances.transferKeepAlive([$destAddress, $amountUnit])' + } + + log.d('yooooo: ${txInfo.module}, ${txInfo.call}, $txOptions'); + // Map tata = await sdk.webView! + // .evalJavascript('api.tx.universalDividend.claimUds()', + // wrapPromise: false) + // .timeout( + // const Duration(seconds: 12), + // onTimeout: () => {}, + // ); + + // return tata.toString(); return await executeCall(txInfo, txOptions, password); } diff --git a/lib/providers/wallet_options.dart b/lib/providers/wallet_options.dart index 3aa3bb9..fa0c421 100644 --- a/lib/providers/wallet_options.dart +++ b/lib/providers/wallet_options.dart @@ -56,7 +56,7 @@ class WalletOptionsProvider with ChangeNotifier { if (answer ?? false) { //Check if balance is null final balance = await sub.getBalance(wallet.address!); - if (balance != 0) { + if (balance != {}) { MyWalletsProvider myWalletProvider = Provider.of(context, listen: false); final defaultWallet = myWalletProvider.getDefaultWallet(); @@ -531,9 +531,10 @@ Widget balance(BuildContext context, String address, double size, Consumer(builder: (context, sdk, _) { return FutureBuilder( future: sdk.getBalance(address), - builder: (BuildContext context, AsyncSnapshot balance) { - if (balance.connectionState != ConnectionState.done || - balance.hasError) { + builder: (BuildContext context, + AsyncSnapshot> globalBalance) { + if (globalBalance.connectionState != ConnectionState.done || + globalBalance.hasError) { if (balanceCache[address] != null && balanceCache[address] != -1) { return Text( @@ -551,7 +552,7 @@ Widget balance(BuildContext context, String address, double size, ); } } - balanceCache[address] = balance.data!; + balanceCache[address] = globalBalance.data!['transferableBalance']!; if (balanceCache[address] != -1) { return Text( "${balanceCache[address]!.toString()} $currencyName", diff --git a/lib/screens/wallet_view.dart b/lib/screens/wallet_view.dart index e2cebd9..36f5478 100644 --- a/lib/screens/wallet_view.dart +++ b/lib/screens/wallet_view.dart @@ -518,10 +518,10 @@ class WalletViewScreen extends StatelessWidget { future: sub.getBalance(defaultWallet.address!), builder: (BuildContext context, - AsyncSnapshot balance) { - if (balance.connectionState != + AsyncSnapshot> globalBalance) { + if (globalBalance.connectionState != ConnectionState.done || - balance.hasError) { + globalBalance.hasError) { if (balanceCache[ defaultWallet.address!] != null) { @@ -542,7 +542,7 @@ class WalletViewScreen extends StatelessWidget { } } balanceCache[defaultWallet.address!] = - balance.data!; + globalBalance.data!['transferableBalance']!; return Text( "${balanceCache[defaultWallet.address!]} $currencyName", style: const TextStyle(