Clean up and fixes

This commit is contained in:
likho 2023-01-13 16:36:50 +02:00
parent 78a64690ed
commit 5a1569b3fa

View file

@ -5,7 +5,6 @@ import 'package:bip39/bip39.dart' as bip39;
import 'package:decimal/decimal.dart'; import 'package:decimal/decimal.dart';
import 'package:devicelocale/devicelocale.dart'; import 'package:devicelocale/devicelocale.dart';
import 'package:ethereum_addresses/ethereum_addresses.dart'; import 'package:ethereum_addresses/ethereum_addresses.dart';
import 'package:flutter/foundation.dart';
import 'package:stackwallet/models/node_model.dart'; import 'package:stackwallet/models/node_model.dart';
import 'package:stackwallet/models/paymint/fee_object_model.dart'; import 'package:stackwallet/models/paymint/fee_object_model.dart';
import 'package:stackwallet/models/paymint/transactions_model.dart'; import 'package:stackwallet/models/paymint/transactions_model.dart';
@ -37,16 +36,11 @@ import 'package:stackwallet/services/event_bus/global_event_bus.dart';
import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/services/notifications_api.dart'; import 'package:stackwallet/services/notifications_api.dart';
import 'package:stackwallet/services/event_bus/events/global/updated_in_background_event.dart'; import 'package:stackwallet/services/event_bus/events/global/updated_in_background_event.dart';
import 'package:stackwallet/services/node_service.dart'; import 'package:stackwallet/services/node_service.dart';
import 'package:stackwallet/utilities/default_nodes.dart'; import 'package:stackwallet/utilities/default_nodes.dart';
const int MINIMUM_CONFIRMATIONS = 5; const int MINIMUM_CONFIRMATIONS = 5;
const int DUST_LIMIT = 294;
const String GENESIS_HASH_MAINNET = const String GENESIS_HASH_MAINNET =
"0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa"; "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa";
@ -91,6 +85,8 @@ class GasTracker {
class EthereumWallet extends CoinServiceAPI { class EthereumWallet extends CoinServiceAPI {
NodeModel? _ethNode; NodeModel? _ethNode;
final _gasLimit = 21000; final _gasLimit = 21000;
final _blockExplorer = "https://eth-goerli.blockscout.com/api?";
final _gasTrackerUrl = "https://beaconcha.in/api/v1/execution/gasnow";
@override @override
set isFavorite(bool markFavorite) { set isFavorite(bool markFavorite) {
@ -120,10 +116,16 @@ class EthereumWallet extends CoinServiceAPI {
final _prefs = Prefs.instance; final _prefs = Prefs.instance;
bool longMutex = false; bool longMutex = false;
final _client = Web3Client( Future<NodeModel> getCurrentNode() async {
"https://goerli.infura.io/v3/22677300bf774e49a458b73313ee56ba", Client()); return NodeService(secureStorageInterface: _secureStore)
.getPrimaryNodeFor(coin: coin) ??
DefaultNodes.getNodeFor(coin);
}
final _blockExplorer = "https://eth-goerli.blockscout.com/api?"; Future<Web3Client> getEthClient() async {
final node = await getCurrentNode();
return Web3Client(node.host, Client());
}
late EthPrivateKey _credentials; late EthPrivateKey _credentials;
@ -186,7 +188,8 @@ class EthereumWallet extends CoinServiceAPI {
@override @override
Future<Decimal> get availableBalance async { Future<Decimal> get availableBalance async {
EtherAmount ethBalance = await _client.getBalance(_credentials.address); Web3Client client = await getEthClient();
EtherAmount ethBalance = await client.getBalance(_credentials.address);
return Decimal.parse(ethBalance.getValueInUnit(EtherUnit.ether).toString()); return Decimal.parse(ethBalance.getValueInUnit(EtherUnit.ether).toString());
} }
@ -199,7 +202,8 @@ class EthereumWallet extends CoinServiceAPI {
@override @override
Future<String> confirmSend({required Map<String, dynamic> txData}) async { Future<String> confirmSend({required Map<String, dynamic> txData}) async {
final int chainId = await _client.getNetworkId(); Web3Client client = await getEthClient();
final int chainId = await client.getNetworkId();
final amount = txData['recipientAmt']; final amount = txData['recipientAmt'];
final decimalAmount = final decimalAmount =
Format.satoshisToAmount(amount as int, coin: Coin.ethereum); Format.satoshisToAmount(amount as int, coin: Coin.ethereum);
@ -212,7 +216,7 @@ class EthereumWallet extends CoinServiceAPI {
maxGas: _gasLimit, maxGas: _gasLimit,
value: EtherAmount.inWei(bigIntAmount)); value: EtherAmount.inWei(bigIntAmount));
final transaction = final transaction =
await _client.sendTransaction(_credentials, tx, chainId: chainId); await client.sendTransaction(_credentials, tx, chainId: chainId);
return transaction; return transaction;
} }
@ -268,8 +272,7 @@ class EthereumWallet extends CoinServiceAPI {
} }
Future<GasTracker> getGasOracle() async { Future<GasTracker> getGasOracle() async {
final response = final response = await get(Uri.parse(_gasTrackerUrl));
await get(Uri.parse("https://beaconcha.in/api/v1/execution/gasnow"));
if (response.statusCode == 200) { if (response.statusCode == 200) {
return GasTracker.fromJson( return GasTracker.fromJson(
@ -377,8 +380,9 @@ class EthereumWallet extends CoinServiceAPI {
Future<List<String>> get mnemonic => _getMnemonicList(); Future<List<String>> get mnemonic => _getMnemonicList();
Future<int> get chainHeight async { Future<int> get chainHeight async {
Web3Client client = await getEthClient();
try { try {
final result = await _client.getBlockNumber(); final result = await client.getBlockNumber();
return result; return result;
} catch (e, s) { } catch (e, s) {
@ -499,6 +503,7 @@ class EthereumWallet extends CoinServiceAPI {
} }
Future<bool> refreshIfThereIsNewData() async { Future<bool> refreshIfThereIsNewData() async {
Web3Client client = await getEthClient();
if (longMutex) return false; if (longMutex) return false;
if (_hasCalledExit) return false; if (_hasCalledExit) return false;
final currentChainHeight = await chainHeight; final currentChainHeight = await chainHeight;
@ -514,7 +519,7 @@ class EthereumWallet extends CoinServiceAPI {
} }
for (String txid in txnsToCheck) { for (String txid in txnsToCheck) {
final txn = await _client.getTransactionByHash(txid); final txn = await client.getTransactionByHash(txid);
final int txBlockNumber = txn.blockNumber.blockNum; final int txBlockNumber = txn.blockNumber.blockNum;
final int txConfirmations = currentChainHeight - txBlockNumber; final int txConfirmations = currentChainHeight - txBlockNumber;
@ -524,22 +529,23 @@ class EthereumWallet extends CoinServiceAPI {
break; break;
} }
} }
// if (!needsRefresh) { if (!needsRefresh) {
// var allOwnAddresses = await _fetchAllOwnAddresses(); var allOwnAddresses = await _fetchAllOwnAddresses();
// List<Map<String, dynamic>> allTxs = AddressTransaction addressTransactions =
// await _fetchHistory(allOwnAddresses); await fetchAddressTransactions(allOwnAddresses.elementAt(0));
// final txData = await transactionData; final txData = await transactionData;
// for (Map<String, dynamic> transaction in allTxs) { if (addressTransactions.message == "OK") {
// if (txData.findTransaction(transaction['tx_hash'] as String) == final allTxs = addressTransactions.result;
// null) { allTxs.forEach((element) {
// Logging.instance.log( if (txData.findTransaction(element["hash"] as String) == null) {
// " txid not found in address history already ${transaction['tx_hash']}", Logging.instance.log(
// level: LogLevel.Info); " txid not found in address history already ${element["hash"]}",
// needsRefresh = true; level: LogLevel.Info);
// break; needsRefresh = true;
// } }
// } });
// } }
}
return needsRefresh; return needsRefresh;
} catch (e, s) { } catch (e, s) {
Logging.instance.log( Logging.instance.log(
@ -644,8 +650,6 @@ class EthereumWallet extends CoinServiceAPI {
refreshMutex = true; refreshMutex = true;
} }
// final blockNumber = await _client.getBlockNumber();
try { try {
GlobalEventBus.instance.fire( GlobalEventBus.instance.fire(
WalletSyncStatusChangedEvent( WalletSyncStatusChangedEvent(
@ -709,22 +713,19 @@ class EthereumWallet extends CoinServiceAPI {
), ),
); );
// if (shouldAutoSync) { if (shouldAutoSync) {
// timer ??= Timer.periodic(const Duration(seconds: 30), (timer) async { timer ??= Timer.periodic(const Duration(seconds: 30), (timer) async {
// Logging.instance.log( Logging.instance.log(
// "Periodic refresh check for $walletId $walletName in object instance: $hashCode", "Periodic refresh check for $walletId $walletName in object instance: $hashCode",
// level: LogLevel.Info); level: LogLevel.Info);
// // chain height check currently broken if (await refreshIfThereIsNewData()) {
// // if ((await chainHeight) != (await storedChainHeight)) { await refresh();
// if (await refreshIfThereIsNewData()) { GlobalEventBus.instance.fire(UpdatedInBackgroundEvent(
// await refresh(); "New data found in $walletId $walletName in background!",
// GlobalEventBus.instance.fire(UpdatedInBackgroundEvent( walletId));
// "New data found in $walletId $walletName in background!", }
// walletId)); });
// } }
// // }
// });
// }
} catch (error, strace) { } catch (error, strace) {
refreshMutex = false; refreshMutex = false;
GlobalEventBus.instance.fire( GlobalEventBus.instance.fire(
@ -758,8 +759,9 @@ class EthereumWallet extends CoinServiceAPI {
@override @override
Future<bool> testNetworkConnection() async { Future<bool> testNetworkConnection() async {
Web3Client client = await getEthClient();
try { try {
final result = await _client.isListeningForNetwork(); final result = await client.isListeningForNetwork();
return result; return result;
} catch (_) { } catch (_) {
return false; return false;
@ -780,7 +782,8 @@ class EthereumWallet extends CoinServiceAPI {
@override @override
Future<Decimal> get totalBalance async { Future<Decimal> get totalBalance async {
EtherAmount ethBalance = await _client.getBalance(_credentials.address); Web3Client client = await getEthClient();
EtherAmount ethBalance = await client.getBalance(_credentials.address);
return Decimal.parse(ethBalance.getValueInUnit(EtherUnit.ether).toString()); return Decimal.parse(ethBalance.getValueInUnit(EtherUnit.ether).toString());
} }
@ -887,7 +890,6 @@ class EthereumWallet extends CoinServiceAPI {
final allTxs = txs.result; final allTxs = txs.result;
allTxs.forEach((element) { allTxs.forEach((element) {
Map<String, dynamic> midSortedTx = {}; Map<String, dynamic> midSortedTx = {};
// create final tx map // create final tx map
midSortedTx["txid"] = element["hash"]; midSortedTx["txid"] = element["hash"];
int confirmations = int.parse(element['confirmations'].toString()); int confirmations = int.parse(element['confirmations'].toString());