Skip to content

Commit

Permalink
remove reference to deprecated get_enabled_coins RPC
Browse files Browse the repository at this point in the history
fixes the 'my_address' is deprecated error messages
  • Loading branch information
takenagain committed Jan 20, 2025
1 parent c40e319 commit 4dbf30a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 129 deletions.
122 changes: 8 additions & 114 deletions lib/bloc/coins_bloc/coins_repo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -268,73 +267,15 @@ class CoinsRepo {
}
}

Future<String?> getCoinAddress(String coinId) async {
final enabledCoins = await getEnabledCoinsMap();
return _getCoinAddress(coinId, enabledCoins);
}

Future<String?> _getCoinAddress(
String coinId,
Map<String, Coin> 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<String?> 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) {
Expand Down Expand Up @@ -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<List<Coin>?> _getEnabledCoins(Iterable<Coin> 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<Coin> list = [];
if (resultJson is List) {
for (final dynamic item in resultJson) {
final enabledCoinItem = item as Map<String, dynamic>? ?? {};
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;
}
}
8 changes: 4 additions & 4 deletions lib/bloc/fiat/fiat_onramp_form/fiat_form_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,13 @@ class FiatFormBloc extends Bloc<FiatFormEvent, FiatFormState> {
AccountInformationChanged event,
Emitter<FiatFormState> 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,
),
);
Expand All @@ -321,7 +321,7 @@ class FiatFormBloc extends Bloc<FiatFormEvent, FiatFormState> {

Future<void> _fetchAccountInfo(Emitter<FiatFormState> 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),
);
Expand Down
11 changes: 0 additions & 11 deletions lib/mm2/mm2_api/rpc/get_enabled_coins_request.dart

This file was deleted.

0 comments on commit 4dbf30a

Please sign in to comment.