Generic Fixes: Support Errors and others (#1394)

* fix: Crypto amout formatting when calculating fiat amount * fix: Issue with some token symbols coming up with a dollar sign * feat: Split transactions to display on history screen token byh token * fix: Remove restriction on balance length * fix: error when a particular token is not available * fix: Remove token transactions when a token is deleted * fix: Revert previous change * make added spl tokens enabled by default fix issue when entering invalid contract address --------- Co-authored-by: OmarHatem <omarh.ismail1@gmail.com>

Adegoke David committed Apr 25, 2024 at 02:14 UTC fff77519d9cd889b5a17705a1488611640378e33
10 files changed +54 -40
cw_core/lib/crypto_currency.dart
+1 -1
@@ -10,7 +10,7 @@ class CryptoCurrency extends EnumerableItem<int> with Serializable<int> implemen
10 this.fullName,
11 this.iconPath,
12 this.tag,
13 - this.enabled = false,
13 + this.enabled = true,
14 })
15 : super(title: title, raw: raw);
16
cw_evm/lib/evm_chain_client.dart
+5 -2
@@ -234,14 +234,17 @@ abstract class EVMChainClient {
234
235 final decodedResponse = jsonDecode(response.body)[0] as Map<String, dynamic>;
236
237 +
238 + final symbol = (decodedResponse['symbol'] ?? '') as String;
239 + String filteredSymbol = symbol.replaceFirst(RegExp('^\\\$'), '');
240 +
241 final name = decodedResponse['name'] ?? '';
238 - final symbol = decodedResponse['symbol'] ?? '';
242 final decimal = decodedResponse['decimals'] ?? '0';
243 final iconPath = decodedResponse['logo'] ?? '';
244
245 return Erc20Token(
246 name: name,
244 - symbol: symbol,
247 + symbol: filteredSymbol,
248 contractAddress: contractAddress,
249 decimal: int.tryParse(decimal) ?? 0,
250 iconPath: iconPath,
cw_evm/lib/evm_chain_wallet.dart
+6
@@ -468,9 +468,15 @@ abstract class EVMChainWalletBase
468 await token.delete();
469
470 balance.remove(token);
471 + await _removeTokenTransactionsInHistory(token);
472 _updateBalance();
473 }
474
475 + Future<void> _removeTokenTransactionsInHistory(Erc20Token token) async {
476 + transactionHistory.transactions.removeWhere((key, value) => value.tokenSymbol == token.title);
477 + await transactionHistory.save();
478 + }
479 +
480 Future<Erc20Token?> getErc20Token(String contractAddress, String chainName) async =>
481 await _client.getErc20Token(contractAddress, chainName);
482
cw_solana/lib/solana_transaction_info.dart
+1 -4
@@ -34,10 +34,7 @@ class SolanaTransactionInfo extends TransactionInfo {
34 @override
35 String amountFormatted() {
36 String stringBalance = solAmount.toString();
37 -
38 - if (stringBalance.toString().length >= 6) {
39 - stringBalance = stringBalance.substring(0, 6);
40 - }
37 +
38 return '$stringBalance $tokenSymbol';
39 }
40
cw_solana/lib/solana_wallet.dart
+26 -26
@@ -273,32 +273,12 @@ abstract class SolanaWalletBase
273
274 final transactions = await _client.fetchTransactions(address);
275
276 - final Map<String, SolanaTransactionInfo> result = {};
277 -
278 - for (var transactionModel in transactions) {
279 - result[transactionModel.id] = SolanaTransactionInfo(
280 - id: transactionModel.id,
281 - to: transactionModel.to,
282 - from: transactionModel.from,
283 - blockTime: transactionModel.blockTime,
284 - direction: transactionModel.isOutgoingTx
285 - ? TransactionDirection.outgoing
286 - : TransactionDirection.incoming,
287 - solAmount: transactionModel.amount,
288 - isPending: false,
289 - txFee: transactionModel.fee,
290 - tokenSymbol: transactionModel.tokenSymbol,
291 - );
292 - }
293 -
294 - transactionHistory.addMany(result);
295 -
296 - await transactionHistory.save();
276 + await _addTransactionsToTransactionHistory(transactions);
277 }
278
279 /// Fetches the SPL Tokens transactions linked to the token account Public Key
280 Future<void> _updateSPLTokenTransactions() async {
301 - List<SolanaTransactionModel> splTokenTransactions = [];
281 + // List<SolanaTransactionModel> splTokenTransactions = [];
282
283 // Make a copy of keys to avoid concurrent modification
284 var tokenKeys = List<CryptoCurrency>.from(balance.keys);
@@ -312,13 +292,20 @@ abstract class SolanaWalletBase
292 _walletKeyPair!,
293 );
294
315 - splTokenTransactions.addAll(tokenTxs);
295 + // splTokenTransactions.addAll(tokenTxs);
296 + await _addTransactionsToTransactionHistory(tokenTxs);
297 }
298 }
299
300 + // await _addTransactionsToTransactionHistory(splTokenTransactions);
301 + }
302 +
303 + Future<void> _addTransactionsToTransactionHistory(
304 + List<SolanaTransactionModel> transactions,
305 + ) async {
306 final Map<String, SolanaTransactionInfo> result = {};
307
321 - for (var transactionModel in splTokenTransactions) {
308 + for (var transactionModel in transactions) {
309 result[transactionModel.id] = SolanaTransactionInfo(
310 id: transactionModel.id,
311 to: transactionModel.to,
@@ -460,12 +447,23 @@ abstract class SolanaWalletBase
447 await token.delete();
448
449 balance.remove(token);
450 + await _removeTokenTransactionsInHistory(token);
451 _updateBalance();
452 }
453
454 + Future<void> _removeTokenTransactionsInHistory(SPLToken token) async {
455 + transactionHistory.transactions.removeWhere((key, value) => value.tokenSymbol == token.title);
456 + await transactionHistory.save();
457 + }
458 +
459 Future<SPLToken?> getSPLToken(String mintAddress) async {
460 // Convert SPL token mint address to public key
468 - final mintPublicKey = Ed25519HDPublicKey.fromBase58(mintAddress);
461 + final Ed25519HDPublicKey mintPublicKey;
462 + try {
463 + mintPublicKey = Ed25519HDPublicKey.fromBase58(mintAddress);
464 + } catch (_) {
465 + return null;
466 + }
467
468 // Fetch token's metadata account
469 try {
@@ -480,10 +478,12 @@ abstract class SolanaWalletBase
478 iconPath = await _client.getIconImageFromTokenUri(token.uri);
479 } catch (_) {}
480
481 + String filteredTokenSymbol = token.symbol.replaceFirst(RegExp('^\\\$'), '');
482 +
483 return SPLToken.fromMetadata(
484 name: token.name,
485 mint: token.mint,
486 - symbol: token.symbol,
486 + symbol: filteredTokenSymbol,
487 mintAddress: mintAddress,
488 iconPath: iconPath,
489 );
cw_solana/lib/spl_token.dart
+2 -2
@@ -19,7 +19,7 @@ class SPLToken extends CryptoCurrency with HiveObjectMixin {
19 @HiveField(3)
20 final int decimal;
21
22 - @HiveField(4, defaultValue: false)
22 + @HiveField(4, defaultValue: true)
23 bool _enabled;
24
25 @HiveField(5)
@@ -39,7 +39,7 @@ class SPLToken extends CryptoCurrency with HiveObjectMixin {
39 required this.mint,
40 this.iconPath,
41 this.tag = 'SOL',
42 - bool enabled = false,
42 + bool enabled = true,
43 }) : _enabled = enabled,
44 super(
45 name: mint.toLowerCase(),
lib/entities/calculate_fiat_amount.dart
+2
@@ -3,6 +3,8 @@ String calculateFiatAmount({double? price, String? cryptoAmount}) {
3 return '0.00';
4 }
5
6 + cryptoAmount = cryptoAmount.replaceAll(',', '.');
7 +
8 final _amount = double.parse(cryptoAmount);
9 final _result = price * _amount;
10 final result = _result < 0 ? _result * -1 : _result;
lib/ethereum/cw_ethereum.dart
+4 -2
@@ -142,8 +142,10 @@ class CWEthereum extends Ethereum {
142 }
143
144 wallet as EthereumWallet;
145 - return wallet.erc20Currencies
146 - .firstWhere((element) => transaction.tokenSymbol == element.symbol);
145 +
146 + return wallet.erc20Currencies.firstWhere(
147 + (element) => transaction.tokenSymbol == element.symbol,
148 + );
149 }
150
151 @override
lib/polygon/cw_polygon.dart
+3 -1
@@ -140,8 +140,10 @@ class CWPolygon extends Polygon {
140 }
141
142 wallet as PolygonWallet;
143 +
144 return wallet.erc20Currencies.firstWhere(
144 - (element) => transaction.tokenSymbol.toLowerCase() == element.symbol.toLowerCase());
145 + (element) => transaction.tokenSymbol.toLowerCase() == element.symbol.toLowerCase(),
146 + );
147 }
148
149 @override
lib/solana/cw_solana.dart
+4 -2
@@ -110,8 +110,10 @@ class CWSolana extends Solana {
110 }
111
112 wallet as SolanaWallet;
113 - return wallet.splTokenCurrencies
114 - .firstWhere((element) => transaction.tokenSymbol == element.symbol);
113 +
114 + return wallet.splTokenCurrencies.firstWhere(
115 + (element) => transaction.tokenSymbol == element.symbol,
116 + );
117 }
118
119 @override