mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2024-12-25 04:59:33 +00:00
Add initial flow for transactions subscription
This commit is contained in:
parent
cd206d730a
commit
73743932ae
3 changed files with 61 additions and 5 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
import 'dart:async';
|
||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:cw_core/crypto_currency.dart';
|
import 'package:cw_core/crypto_currency.dart';
|
||||||
|
@ -20,6 +21,7 @@ class EthereumClient {
|
||||||
Map<CryptoCurrency, String> get erc20Currencies => _erc20Currencies;
|
Map<CryptoCurrency, String> get erc20Currencies => _erc20Currencies;
|
||||||
|
|
||||||
Web3Client? _client;
|
Web3Client? _client;
|
||||||
|
StreamSubscription<Transfer>? subscription;
|
||||||
|
|
||||||
bool connect(Node node) {
|
bool connect(Node node) {
|
||||||
try {
|
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<EtherAmount> getBalance(EthereumAddress address) async =>
|
Future<EtherAmount> getBalance(EthereumAddress address) async =>
|
||||||
await _client!.getBalance(address);
|
await _client!.getBalance(address);
|
||||||
|
|
||||||
|
@ -186,6 +229,11 @@ I/flutter ( 4474): Gas Used: 53000
|
||||||
return erc20Balances;
|
return erc20Balances;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void stop() {
|
||||||
|
subscription?.cancel();
|
||||||
|
_client?.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
// Future<bool> sendERC20Token(
|
// Future<bool> sendERC20Token(
|
||||||
// EthereumAddress to, CryptoCurrency erc20Currency, BigInt amount) async {
|
// EthereumAddress to, CryptoCurrency erc20Currency, BigInt amount) async {
|
||||||
// if (_erc20Currencies[erc20Currency] == null) {
|
// if (_erc20Currencies[erc20Currency] == null) {
|
||||||
|
|
|
@ -15,7 +15,9 @@ abstract class EthereumTransactionHistoryBase
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> save() async {}
|
Future<void> save() async {
|
||||||
|
// TODO: implement
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void addOne(EthereumTransactionInfo transaction) =>
|
void addOne(EthereumTransactionInfo transaction) =>
|
||||||
|
|
|
@ -96,7 +96,9 @@ abstract class EthereumWalletBase
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void close() {}
|
void close() {
|
||||||
|
_client.stop();
|
||||||
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
@override
|
@override
|
||||||
|
@ -110,6 +112,7 @@ abstract class EthereumWalletBase
|
||||||
throw Exception("Ethereum Node connection failed");
|
throw Exception("Ethereum Node connection failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_client.setListeners(_privateKey.address, _onNewTransaction);
|
||||||
_updateBalance();
|
_updateBalance();
|
||||||
|
|
||||||
syncStatus = ConnectedSyncStatus();
|
syncStatus = ConnectedSyncStatus();
|
||||||
|
@ -199,9 +202,7 @@ abstract class EthereumWalletBase
|
||||||
(timer) async => _priorityFees = await _client.getEstimatedGasForPriorities());
|
(timer) async => _priorityFees = await _client.getEstimatedGasForPriorities());
|
||||||
|
|
||||||
syncStatus = SyncedSyncStatus();
|
syncStatus = SyncedSyncStatus();
|
||||||
} catch (e, stacktrace) {
|
} catch (e) {
|
||||||
print(stacktrace);
|
|
||||||
print(e.toString());
|
|
||||||
syncStatus = FailedSyncStatus();
|
syncStatus = FailedSyncStatus();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -270,4 +271,9 @@ abstract class EthereumWalletBase
|
||||||
Future<void>? updateBalance() => null;
|
Future<void>? updateBalance() => null;
|
||||||
|
|
||||||
List<CryptoCurrency> get erc20Currencies => _client.erc20Currencies.keys.toList();
|
List<CryptoCurrency> get erc20Currencies => _client.erc20Currencies.keys.toList();
|
||||||
|
|
||||||
|
void _onNewTransaction(FilterEvent event) {
|
||||||
|
_updateBalance();
|
||||||
|
// TODO: Add in transaction history
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue