From b2c40c014af744ec7255f4b6d861c0a32e0db873 Mon Sep 17 00:00:00 2001 From: likho Date: Tue, 20 Dec 2022 14:54:51 +0200 Subject: [PATCH] WIP: ADD ETH --- .../coins/ethereum/ethereum_wallet.dart | 129 ++++++++++++++---- 1 file changed, 106 insertions(+), 23 deletions(-) diff --git a/lib/services/coins/ethereum/ethereum_wallet.dart b/lib/services/coins/ethereum/ethereum_wallet.dart index 513effe8d..679144860 100644 --- a/lib/services/coins/ethereum/ethereum_wallet.dart +++ b/lib/services/coins/ethereum/ethereum_wallet.dart @@ -12,15 +12,18 @@ import 'package:stackwallet/utilities/prefs.dart'; import 'package:string_to_hex/string_to_hex.dart'; import 'package:web3dart/web3dart.dart'; import 'package:web3dart/web3dart.dart' as Transaction; -// import 'package:string_to_hex/string_to_hex.dart'; -// import 'package:web3dart/credentials.dart'; -// import 'package:web3dart/web3dart.dart'; + import 'package:http/http.dart'; import 'package:stackwallet/hive/db.dart'; import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/services/coins/coin_service.dart'; +import 'package:stackwallet/services/event_bus/events/global/node_connection_status_changed_event.dart'; +import 'package:stackwallet/services/event_bus/events/global/refresh_percent_changed_event.dart'; +import 'package:stackwallet/services/event_bus/events/global/wallet_sync_status_changed_event.dart'; +import 'package:stackwallet/services/event_bus/global_event_bus.dart'; + const int MINIMUM_CONFIRMATIONS = 1; const int DUST_LIMIT = 294; @@ -55,8 +58,7 @@ class EthereumWallet extends CoinServiceAPI { late PriceAPI _priceAPI; final _prefs = Prefs.instance; final _client = Web3Client( - "https://mainnet.infura.io/v3/22677300bf774e49a458b73313ee56ba", - Client()); + "https://goerli.infura.io/v3/22677300bf774e49a458b73313ee56ba", Client()); late EthPrivateKey _credentials; @@ -108,15 +110,19 @@ class EthereumWallet extends CoinServiceAPI { @override Future confirmSend({required Map txData}) async { + print("CALLING CONFIRM SEND WITH $txData"); + final gasPrice = await _client.getGasPrice(); + print("GAS PRICE IS $gasPrice"); + + // final fee = "21,000" * (gasPrice! + 2 ); + final tx = Transaction.Transaction( + to: EthereumAddress.fromHex(txData['addresss'] as String), + gasPrice: gasPrice, + maxGas: 21000, + value: EtherAmount.fromUnitAndValue(EtherUnit.ether, 1)); final transaction = await _client.sendTransaction( _credentials, - Transaction( - to: EthereumAddress.fromHex( - '0xC914Bb2ba888e3367bcecEb5C2d99DF7C7423706'), - gasPrice: EtherAmount.inWei(BigInt.one), - maxGas: 100000, - value: EtherAmount.fromUnitAndValue(EtherUnit.ether, 1), - ), + tx, ); return transaction; @@ -129,9 +135,11 @@ class EthereumWallet extends CoinServiceAPI { } @override - Future estimateFeeFor(int satoshiAmount, int feeRate) { + Future estimateFeeFor(int satoshiAmount, int feeRate) async { + print("CALLING ESTIMATE FEE"); // TODO: implement estimateFeeFor - throw UnimplementedError(); + // throw UnimplementedError(); + return 1; } @override @@ -145,7 +153,6 @@ class EthereumWallet extends CoinServiceAPI { Future? _feeObject; Future _getFees() async { - // TODO: implement _getFees return FeeObject( numberOfBlocksFast: 10, numberOfBlocksAverage: 10, @@ -168,9 +175,10 @@ class EthereumWallet extends CoinServiceAPI { throw UnimplementedError(); } + bool _hasCalledExit = false; + @override - // TODO: implement hasCalledExit - bool get hasCalledExit => throw UnimplementedError(); + bool get hasCalledExit => _hasCalledExit; @override Future initializeExisting() async { @@ -241,6 +249,29 @@ class EthereumWallet extends CoinServiceAPI { @override Future> get mnemonic => _getMnemonicList(); + // Future get chainHeight async { + // try { + // final result = await _client.getSyncStatus(); + // print("HEIGHT IS $result"); + // return 1 as int; + // } catch (e, s) { + // Logging.instance.log("Exception caught in chainHeight: $e\n$s", + // level: LogLevel.Error); + // return -1; + // } + // } + // + // int get storedChainHeight { + // final storedHeight = DB.instance + // .get(boxName: walletId, key: "storedChainHeight") as int?; + // return storedHeight ?? 0; + // } + // + // Future updateStoredChainHeight({required int newHeight}) async { + // await DB.instance.put( + // boxName: walletId, key: "storedChainHeight", value: newHeight); + // } + Future> _getMnemonicList() async { final mnemonicString = await _secureStore.read(key: '${_walletId}_mnemonic'); @@ -255,13 +286,22 @@ class EthereumWallet extends CoinServiceAPI { // TODO: implement pendingBalance Future get pendingBalance => throw UnimplementedError(); + // Future transactionFee(int satoshiAmount) {} + @override Future> prepareSend( {required String address, required int satoshiAmount, - Map? args}) { - // TODO: implement prepareSend - throw UnimplementedError(); + Map? args}) async { + print("CALLING PREPARE SEND ${Decimal.fromInt(satoshiAmount)}"); + + Map txData = { + "fee": 0, + "addresss": address, + "recipientAmt": satoshiAmount, + }; + + return txData; } @override @@ -303,9 +343,50 @@ class EthereumWallet extends CoinServiceAPI { } @override - Future refresh() { - // TODO: implement refresh - throw UnimplementedError(); + Future refresh() async { + if (refreshMutex) { + Logging.instance.log("$walletId $walletName refreshMutex denied", + level: LogLevel.Info); + return; + } else { + refreshMutex = true; + } + + try { + GlobalEventBus.instance.fire( + WalletSyncStatusChangedEvent( + WalletSyncStatus.syncing, + walletId, + coin, + ), + ); + + GlobalEventBus.instance.fire(RefreshPercentChangedEvent(0.0, walletId)); + GlobalEventBus.instance.fire(RefreshPercentChangedEvent(0.1, walletId)); + + // final currentHeight = await chainHeight; + const storedHeight = 1; //await storedChainHeight; + + } catch (error, strace) { + refreshMutex = false; + GlobalEventBus.instance.fire( + NodeConnectionStatusChangedEvent( + NodeConnectionStatus.disconnected, + walletId, + coin, + ), + ); + GlobalEventBus.instance.fire( + WalletSyncStatusChangedEvent( + WalletSyncStatus.unableToSync, + walletId, + coin, + ), + ); + Logging.instance.log( + "Caught exception in refreshWalletData(): $error\n$strace", + level: LogLevel.Warning); + } } @override @@ -327,6 +408,8 @@ class EthereumWallet extends CoinServiceAPI { // TODO: Check difference between total and available balance for eth Future get totalBalance async { EtherAmount ethBalance = await _client.getBalance(_credentials.address); + print( + "BALANCE NOW IS ${ethBalance.getValueInUnit(EtherUnit.ether).toString()}"); return Decimal.parse(ethBalance.getValueInUnit(EtherUnit.ether).toString()); }