From 4dbf30a613d94a48651fb22cfd583dd4fbf57e89 Mon Sep 17 00:00:00 2001 From: Francois Date: Mon, 20 Jan 2025 11:44:35 +0200 Subject: [PATCH] remove reference to deprecated `get_enabled_coins` RPC fixes the 'my_address' is deprecated error messages --- lib/bloc/coins_bloc/coins_repo.dart | 122 ++---------------- .../fiat/fiat_onramp_form/fiat_form_bloc.dart | 8 +- .../rpc/get_enabled_coins_request.dart | 11 -- 3 files changed, 12 insertions(+), 129 deletions(-) delete mode 100644 lib/mm2/mm2_api/rpc/get_enabled_coins_request.dart diff --git a/lib/bloc/coins_bloc/coins_repo.dart b/lib/bloc/coins_bloc/coins_repo.dart index 5df800993a..d0ad4f5f7b 100644 --- a/lib/bloc/coins_bloc/coins_repo.dart +++ b/lib/bloc/coins_bloc/coins_repo.dart @@ -15,7 +15,6 @@ import 'package:web_dex/mm2/mm2.dart'; import 'package:web_dex/mm2/mm2_api/rpc/base.dart'; import 'package:web_dex/mm2/mm2_api/rpc/bloc_response.dart'; import 'package:web_dex/mm2/mm2_api/rpc/disable_coin/disable_coin_req.dart'; -import 'package:web_dex/mm2/mm2_api/rpc/get_enabled_coins_request.dart'; import 'package:web_dex/mm2/mm2_api/rpc/withdraw/withdraw_errors.dart'; import 'package:web_dex/mm2/mm2_api/rpc/withdraw/withdraw_request.dart'; import 'package:web_dex/model/cex_price.dart'; @@ -138,7 +137,7 @@ class CoinsRepo { final coinsMap = Map.fromEntries(entries); for (final coinId in coinsMap.keys) { final coin = coinsMap[coinId]!; - final coinAddress = await _getCoinAddress(coin.abbr, coinsMap); + final coinAddress = await getFirstPubkey(coin.abbr); coinsMap[coinId] = coin.copyWith( address: coinAddress, state: CoinState.active, @@ -268,73 +267,15 @@ class CoinsRepo { } } - Future getCoinAddress(String coinId) async { - final enabledCoins = await getEnabledCoinsMap(); - return _getCoinAddress(coinId, enabledCoins); - } - - Future _getCoinAddress( - String coinId, - Map walletCoins, - ) async { - final isLoggedIn = await _kdfSdk.auth.isSignedIn(); - final currentWallet = (await _kdfSdk.auth.currentUser)?.wallet; - final loggedIn = isLoggedIn && currentWallet != null; - if (!loggedIn) { + @Deprecated('Use SDK pubkeys.getPubkeys instead and let the user ' + 'select from the available options.') + Future getFirstPubkey(String coinId) async { + final asset = _kdfSdk.assets.findAssetsByTicker(coinId).single; + final pubkeys = await _kdfSdk.pubkeys.getPubkeys(asset); + if (pubkeys.keys.isEmpty) { return null; } - - final String accountKey = currentWallet.id; - if (_addressCache.containsKey(accountKey) && - _addressCache[accountKey]!.containsKey(coinId)) { - return _addressCache[accountKey]![coinId]; - } else { - try { - if (walletCoins[coinId] == null) { - await activateCoinsSync([getCoin(coinId)!]); - } - - // This function is also called within `getEnabledCoins`, so cannot use - // that function unless you enjoy recursive stackoverflow :) - final legacyEnabledCoins = await _getEnabledCoins(walletCoins.values); - final Coin? coin = legacyEnabledCoins - ?.firstWhereOrNull((enabledCoin) => enabledCoin.abbr == coinId); - - if (coin == null || coin.address == null) { - if (!_addressCache.containsKey(accountKey)) { - _addressCache[accountKey] = {}; - } - - // Cache this wallet's addresses - for (final walletCoin in walletCoins.values) { - if (walletCoin.address != null && - !_addressCache[accountKey]!.containsKey(walletCoin.abbr)) { - // Exit if the address already exists in a different account - // Address belongs to another account, this is a bug, - // gives outdated data - for (final entry in _addressCache.entries) { - if (entry.key != accountKey && - entry.value.containsValue(walletCoin.address)) { - return null; - } - } - - _addressCache[accountKey]![walletCoin.abbr] = walletCoin.address!; - } - } - - return _addressCache[accountKey]![coinId]; - } - } catch (e, s) { - log( - 'Failed to get coin address: $e', - isError: true, - path: 'coins_repo => _getCoinAddress', - trace: s, - ).ignore(); - } - } - return null; + return pubkeys.keys.first.address; } double? getUsdPriceByAmount(String amount, String coinAbbr) { @@ -562,51 +503,4 @@ class CoinsRepo { result: withdrawDetails, ); } - - /// This is needed to access the legacy `coin.address` field. The alternative - /// method is to use the [tryGetBalanceInfo] method, which also returns an - /// address field (although idk if it's equivalent at the time of writing) - Future?> _getEnabledCoins(Iterable knownCoins) async { - JsonMap response; - try { - response = await _mm2.call(GetEnabledCoinsReq()); - } catch (e) { - log( - 'Error getting enabled coins: $e', - path: 'api => getEnabledCoins => _call', - isError: true, - ).ignore(); - return null; - } - - dynamic resultJson; - try { - resultJson = response['result']; - } catch (e, s) { - log( - 'Error parsing of enabled coins response: $e', - path: 'api => getEnabledCoins => jsonDecode', - trace: s, - isError: true, - ).ignore(); - return null; - } - - final List list = []; - if (resultJson is List) { - for (final dynamic item in resultJson) { - final enabledCoinItem = item as Map? ?? {}; - final Coin? coin = knownCoins.firstWhereOrNull( - (Coin known) => known.abbr == enabledCoinItem['ticker'], - ); - - if (coin != null) { - coin.address = enabledCoinItem['address'] as String?; - list.add(coin); - } - } - } - - return list; - } } diff --git a/lib/bloc/fiat/fiat_onramp_form/fiat_form_bloc.dart b/lib/bloc/fiat/fiat_onramp_form/fiat_form_bloc.dart index 4bf44852a5..2126b24457 100644 --- a/lib/bloc/fiat/fiat_onramp_form/fiat_form_bloc.dart +++ b/lib/bloc/fiat/fiat_onramp_form/fiat_form_bloc.dart @@ -295,13 +295,13 @@ class FiatFormBloc extends Bloc { AccountInformationChanged event, Emitter emit, ) async { - final accountRerference = await _coinsRepository.getCoinAddress('KMD'); + final accountReference = await _coinsRepository.getFirstPubkey('KMD'); final address = await _coinsRepository - .getCoinAddress(state.selectedCoin.value!.getAbbr()); + .getFirstPubkey(state.selectedCoin.value!.getAbbr()); emit( state.copyWith( - accountReference: accountRerference, + accountReference: accountReference, coinReceiveAddress: address, ), ); @@ -321,7 +321,7 @@ class FiatFormBloc extends Bloc { Future _fetchAccountInfo(Emitter emit) async { final address = - await _coinsRepository.getCoinAddress(state.selectedCoin.value!.symbol); + await _coinsRepository.getFirstPubkey(state.selectedCoin.value!.symbol); emit( state.copyWith(accountReference: address, coinReceiveAddress: address), ); diff --git a/lib/mm2/mm2_api/rpc/get_enabled_coins_request.dart b/lib/mm2/mm2_api/rpc/get_enabled_coins_request.dart deleted file mode 100644 index 026d3243ae..0000000000 --- a/lib/mm2/mm2_api/rpc/get_enabled_coins_request.dart +++ /dev/null @@ -1,11 +0,0 @@ -class GetEnabledCoinsReq { - static const String method = 'get_enabled_coins'; - late String userpass; - - Map toJson() { - return { - 'method': method, - 'userpass': userpass, - }; - } -}