Enhance loading time for erc20 tokens transactions

This commit is contained in:
OmarHatem 2023-07-21 18:52:10 +03:00
parent 659c702d48
commit 5376b9e663
3 changed files with 30 additions and 21 deletions

View file

@ -1,4 +1,3 @@
import 'package:flutter/foundation.dart';
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
import 'package:bitcoin_flutter/src/payments/index.dart' show PaymentData;
import 'package:cw_bitcoin/address_from_output.dart';
@ -217,9 +216,9 @@ class ElectrumTransactionInfo extends TransactionInfo {
height: info.height,
amount: info.amount,
fee: info.fee,
direction: direction ?? info.direction,
date: date ?? info.date,
isPending: isPending ?? info.isPending,
direction: direction,
date: date,
isPending: isPending,
confirmations: info.confirmations);
}

View file

@ -16,12 +16,13 @@ import 'package:cw_ethereum/ethereum_transaction_priority.dart';
import 'package:cw_ethereum/.secrets.g.dart' as secrets;
class EthereumClient {
final _httpClient = Client();
Web3Client? _client;
StreamSubscription<Transfer>? subscription;
bool connect(Node node) {
try {
_client = Web3Client(node.uri.toString(), Client());
_client = Web3Client(node.uri.toString(), _httpClient);
return true;
} catch (e) {
@ -242,25 +243,28 @@ I/flutter ( 4474): Gas Used: 53000
Future<List<EthereumTransactionModel>> fetchTransactions(String address,
{String? contractAddress}) async {
final client = Client();
try {
final response = await _httpClient.get(Uri.https("api.etherscan.io", "/api", {
"module": "account",
"action": contractAddress != null ? "tokentx" : "txlist",
if (contractAddress != null) "contractaddress": contractAddress,
"address": address,
"apikey": secrets.etherScanApiKey,
}));
final response = await client.get(Uri.https("api.etherscan.io", "/api", {
"module": "account",
"action": contractAddress != null ? "tokentx" : "txlist",
if (contractAddress != null) "contractaddress": contractAddress,
"address": address,
"apikey": secrets.etherScanApiKey,
}));
final _jsonResponse = json.decode(response.body) as Map<String, dynamic>;
final _jsonResponse = json.decode(response.body) as Map<String, dynamic>;
if (response.statusCode >= 200 && response.statusCode < 300 && _jsonResponse['status'] != 0) {
return (_jsonResponse['result'] as List)
.map((e) => EthereumTransactionModel.fromJson(e as Map<String, dynamic>))
.toList();
}
if (response.statusCode >= 200 && response.statusCode < 300 && _jsonResponse['status'] != 0) {
return (_jsonResponse['result'] as List)
.map((e) => EthereumTransactionModel.fromJson(e as Map<String, dynamic>))
.toList();
return [];
} catch (e) {
print(e);
return [];
}
return [];
}
// Future<int> _getDecimalPlacesForContract(DeployedContract contract) async {

View file

@ -19,6 +19,7 @@ import 'package:cw_ethereum/ethereum_exceptions.dart';
import 'package:cw_ethereum/ethereum_transaction_credentials.dart';
import 'package:cw_ethereum/ethereum_transaction_history.dart';
import 'package:cw_ethereum/ethereum_transaction_info.dart';
import 'package:cw_ethereum/ethereum_transaction_model.dart';
import 'package:cw_ethereum/ethereum_transaction_priority.dart';
import 'package:cw_ethereum/ethereum_wallet_addresses.dart';
import 'package:cw_ethereum/file.dart';
@ -200,15 +201,20 @@ abstract class EthereumWalletBase
final address = _privateKey.address.hex;
final transactions = await _client.fetchTransactions(address);
final List<Future<List<EthereumTransactionModel>>> erc20TokensTransactions = [];
for (var token in balance.keys) {
if (token is Erc20Token) {
transactions.addAll(await _client.fetchTransactions(
erc20TokensTransactions.add(_client.fetchTransactions(
address,
contractAddress: token.contractAddress,
));
}
}
final tokensTransaction = await Future.wait(erc20TokensTransactions);
transactions.addAll(tokensTransaction.expand((element) => element));
final Map<String, EthereumTransactionInfo> result = {};
for (var transactionModel in transactions) {