From 73743932aec6db5b38d360578a60227988ffb7b8 Mon Sep 17 00:00:00 2001 From: OmarHatem Date: Tue, 6 Jun 2023 22:32:36 +0300 Subject: [PATCH] Add initial flow for transactions subscription --- cw_ethereum/lib/ethereum_client.dart | 48 +++++++++++++++++++ .../lib/ethereum_transaction_history.dart | 4 +- cw_ethereum/lib/ethereum_wallet.dart | 14 ++++-- 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/cw_ethereum/lib/ethereum_client.dart b/cw_ethereum/lib/ethereum_client.dart index 7390e6619..f36288ee6 100644 --- a/cw_ethereum/lib/ethereum_client.dart +++ b/cw_ethereum/lib/ethereum_client.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:math'; import 'package:cw_core/crypto_currency.dart'; @@ -20,6 +21,7 @@ class EthereumClient { Map get erc20Currencies => _erc20Currencies; Web3Client? _client; + StreamSubscription? subscription; bool connect(Node node) { try { @@ -31,6 +33,47 @@ class EthereumClient { } } + void setListeners(EthereumAddress userAddress, Function(FilterEvent) onNewTransaction) async { + // final String abi = await rootBundle.loadString("assets/abi_json/erc20_abi.json"); + // final contractAbi = ContractAbi.fromJson(abi, "ERC20"); + // + // final contract = DeployedContract( + // contractAbi, + // EthereumAddress.fromHex("0xf451659CF5688e31a31fC3316efbcC2339A490Fb"), + // ); + // + // final transferEvent = contract.event('Transfer'); + // // listen for the Transfer event when it's emitted by the contract above + // final subscription = _client! + // .events(FilterOptions.events(contract: contract, event: transferEvent)) + // .take(1) + // .listen((event) { + // final decoded = transferEvent.decodeResults(event.topics ?? [], event.data ?? ''); + // + // final from = decoded[0] as EthereumAddress; + // final to = decoded[1] as EthereumAddress; + // final value = decoded[2] as BigInt; + // + // print('$from sent $value MetaCoins to $to'); + // }); + + // final eventFilter = FilterOptions(address: userAddress); + // + // _client!.events(eventFilter).listen((event) { + // print("!!!!!!!!!!!!!!!!!!"); + // print('Address ${event.address} data ${event.data} tx hash ${event.transactionHash}!'); + // onNewTransaction(event); + // }); + + // final erc20 = Erc20(client: _client!, address: userAddress); + // + // subscription = erc20.transferEvents().take(1).listen((event) { + // print("!!!!!!!!!!!!!!!!!!"); + // print('${event.from} sent ${event.value} MetaCoins to ${event.to}!'); + // onNewTransaction(event); + // }); + } + Future getBalance(EthereumAddress address) async => await _client!.getBalance(address); @@ -186,6 +229,11 @@ I/flutter ( 4474): Gas Used: 53000 return erc20Balances; } + void stop() { + subscription?.cancel(); + _client?.dispose(); + } + // Future sendERC20Token( // EthereumAddress to, CryptoCurrency erc20Currency, BigInt amount) async { // if (_erc20Currencies[erc20Currency] == null) { diff --git a/cw_ethereum/lib/ethereum_transaction_history.dart b/cw_ethereum/lib/ethereum_transaction_history.dart index faf04db20..8a5825c73 100644 --- a/cw_ethereum/lib/ethereum_transaction_history.dart +++ b/cw_ethereum/lib/ethereum_transaction_history.dart @@ -15,7 +15,9 @@ abstract class EthereumTransactionHistoryBase } @override - Future save() async {} + Future save() async { + // TODO: implement + } @override void addOne(EthereumTransactionInfo transaction) => diff --git a/cw_ethereum/lib/ethereum_wallet.dart b/cw_ethereum/lib/ethereum_wallet.dart index 1a4b57e63..ae440bdcf 100644 --- a/cw_ethereum/lib/ethereum_wallet.dart +++ b/cw_ethereum/lib/ethereum_wallet.dart @@ -96,7 +96,9 @@ abstract class EthereumWalletBase } @override - void close() {} + void close() { + _client.stop(); + } @action @override @@ -110,6 +112,7 @@ abstract class EthereumWalletBase throw Exception("Ethereum Node connection failed"); } + _client.setListeners(_privateKey.address, _onNewTransaction); _updateBalance(); syncStatus = ConnectedSyncStatus(); @@ -199,9 +202,7 @@ abstract class EthereumWalletBase (timer) async => _priorityFees = await _client.getEstimatedGasForPriorities()); syncStatus = SyncedSyncStatus(); - } catch (e, stacktrace) { - print(stacktrace); - print(e.toString()); + } catch (e) { syncStatus = FailedSyncStatus(); } } @@ -270,4 +271,9 @@ abstract class EthereumWalletBase Future? updateBalance() => null; List get erc20Currencies => _client.erc20Currencies.keys.toList(); + + void _onNewTransaction(FilterEvent event) { + _updateBalance(); + // TODO: Add in transaction history + } }