From 55f08dddd604e79da06d2e5310600762ee8994f0 Mon Sep 17 00:00:00 2001 From: poka Date: Wed, 8 Jun 2022 02:30:19 +0200 Subject: [PATCH] transferKeepAlive for all payments; transferAll to defaultWallet when deleting wallet with non null balance --- lib/providers/substrate_sdk.dart | 13 ++++++---- lib/providers/wallet_options.dart | 14 +++++++++++ lib/screens/settings.dart | 1 + lib/screens/substrate_sandbox.dart | 2 +- lib/screens/transaction_in_progress.dart | 32 ++++++++++++++++++++++-- pubspec.yaml | 2 +- 6 files changed, 55 insertions(+), 9 deletions(-) diff --git a/lib/providers/substrate_sdk.dart b/lib/providers/substrate_sdk.dart index fdcece3..997bea5 100644 --- a/lib/providers/substrate_sdk.dart +++ b/lib/providers/substrate_sdk.dart @@ -372,17 +372,20 @@ class SubstrateSdk with ChangeNotifier { log.d(keyring.current.address); log.d(fromAddress); log.d(password); - log.d(await checkPassword(fromAddress, password)); + // log.d(await checkPassword(fromAddress, password)); + final fromPubkey = await sdk.api.account.decodeAddress([fromAddress]); + log.d(fromPubkey!.keys.first); final sender = TxSenderData( - keyring.current.address, - keyring.current.pubKey, + fromAddress, + fromPubkey.keys.first, ); - final txInfo = TxInfoData('balances', 'transfer', sender); + final txInfo = TxInfoData( + 'balances', amount == -1 ? 'transferAll' : 'transferKeepAlive', sender); try { final hash = await sdk.api.tx.signAndSend( txInfo, - [destAddress, amount * 100], + [destAddress, amount == -1 ? false : (amount * 100)], password, onStatusChange: (status) { log.d('Transaction status: ' + status); diff --git a/lib/providers/wallet_options.dart b/lib/providers/wallet_options.dart index 1077986..8a59d15 100644 --- a/lib/providers/wallet_options.dart +++ b/lib/providers/wallet_options.dart @@ -66,6 +66,20 @@ class WalletOptionsProvider with ChangeNotifier { 'Êtes-vous sûr de vouloir oublier le portefeuille "${wallet.name}" ?')); if (_answer ?? false) { + //Check if balance is null + final _balance = await _sub.getBalance(wallet.address!); + if (_balance != 0) { + MyWalletsProvider _myWalletProvider = + Provider.of(context, listen: false); + final _defaultWallet = _myWalletProvider.getDefaultWallet(); + log.d(_defaultWallet.address); + await _sub.pay( + fromAddress: wallet.address!, + destAddress: _defaultWallet.address!, + amount: -1, + password: _myWalletProvider.pinCode); + } + await walletBox.delete(wallet.key); await _sub.deleteAccounts([wallet.address!]); diff --git a/lib/screens/settings.dart b/lib/screens/settings.dart index e426d67..bccf4d4 100644 --- a/lib/screens/settings.dart +++ b/lib/screens/settings.dart @@ -49,6 +49,7 @@ class SettingsScreen extends StatelessWidget { const SizedBox(height: 60), Row(children: [ Consumer(builder: (context, _sub, _) { + log.d(_sub.sdk.api.connectedNode?.endpoint); return Expanded( child: Row(children: [ Text(' Noeud $currencyName :'), diff --git a/lib/screens/substrate_sandbox.dart b/lib/screens/substrate_sandbox.dart index 5177d0c..a62fdfc 100644 --- a/lib/screens/substrate_sandbox.dart +++ b/lib/screens/substrate_sandbox.dart @@ -36,7 +36,7 @@ class SubstrateSandBox extends StatelessWidget { await _sub.connectNode(context); }, child: Text( - 'Noeud connecté ?: ${_sub.nodeConnected} (${configBox.get('endpoint')})')), + 'Noeud connecté ?: ${_sub.nodeConnected} (${_sub.sdk.api.connectedNode?.endpoint})')), if (_sub.nodeConnected) Text('Noeud "$currencyName", bloc N°${_sub.blocNumber}'), const SizedBox(height: 20), diff --git a/lib/screens/transaction_in_progress.dart b/lib/screens/transaction_in_progress.dart index 7da1e3b..5bff32d 100644 --- a/lib/screens/transaction_in_progress.dart +++ b/lib/screens/transaction_in_progress.dart @@ -22,6 +22,7 @@ class TransactionInProgress extends StatelessWidget { Provider.of(context, listen: false); MyWalletsProvider _myWalletProvider = Provider.of(context, listen: false); + bool isValid = false; String _resultText; bool isLoading = true; @@ -84,8 +85,10 @@ class TransactionInProgress extends StatelessWidget { // jsonResult = json.decode(_result); log.d(_result); if (_result.contains('blockHash: ')) { + isValid = true; _resultText = '$_actionName validé !'; } else { + isValid = false; _resultText = "Une erreur s'est produite:\n"; final List _exceptionSplit = _result.split('Exception: '); String _exception; @@ -94,7 +97,7 @@ class TransactionInProgress extends StatelessWidget { } else { _exception = _exceptionSplit[0]; } - + // log.d('expection: $_exception'); switch (_exception) { case 'cert.NotRespectCertPeriod': case 'identity.CreatorNotAllowedToCreateIdty': @@ -114,6 +117,23 @@ class TransactionInProgress extends StatelessWidget { _resultText += "Ce nom est déjà pris"; } break; + case 'balances.KeepAlive': + { + _resultText = + "Vous devez garder au moins 2ĞD sur votre compte pour le garder actif"; + } + break; + case '1010: Invalid Transaction: Inability to pay some fees , e.g. account balance too low': + { + _resultText = + "Vous devez alimenter ce compte avant\nde pouvoir l'utiliser"; + } + break; + case 'timeout': + { + _resultText += "Le délais d'éxecution est dépassé"; + } + break; default: { @@ -156,7 +176,7 @@ class TransactionInProgress extends StatelessWidget { end: Alignment.bottomCenter, colors: [ yellowC, - const Color(0xfffafafa), + backgroundColor, ], )), child: Column(children: [ @@ -209,6 +229,14 @@ class TransactionInProgress extends StatelessWidget { ), ), ), + Visibility( + visible: !isLoading, + child: Icon( + isValid ? Icons.done_all : Icons.close, + size: 35, + color: isValid ? Colors.greenAccent : Colors.redAccent, + ), + ), const SizedBox(height: 10), Text( _resultText, diff --git a/pubspec.yaml b/pubspec.yaml index 9c8cdac..457c21b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,7 +5,7 @@ description: Pay with G1. # pub.dev using `pub publish`. This is preferred for private packages. publish_to: 'none' # Remove this line if you wish to publish to pub.dev -version: 0.0.8+2 +version: 0.0.8+3 environment: sdk: '>=2.12.0 <3.0.0'