mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-02-03 03:36:39 +00:00
fixes + auto receive incoming txs
This commit is contained in:
parent
92d4a6f6d6
commit
f8a2e4dd7c
3 changed files with 41 additions and 17 deletions
|
@ -31,11 +31,8 @@ class NanoClient {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setListeners(EthereumAddress userAddress, Function(FilterEvent) onNewTransaction) async {}
|
|
||||||
|
|
||||||
Future<NanoBalance> getBalance(String address) async {
|
Future<NanoBalance> getBalance(String address) async {
|
||||||
// this is the preferred rpc call but the test node isn't returning this one:
|
|
||||||
final response = await http.post(
|
final response = await http.post(
|
||||||
_node!.uri,
|
_node!.uri,
|
||||||
headers: {"Content-Type": "application/json"},
|
headers: {"Content-Type": "application/json"},
|
||||||
|
@ -319,16 +316,29 @@ class NanoClient {
|
||||||
headers: {"Content-Type": "application/json"},
|
headers: {"Content-Type": "application/json"},
|
||||||
body: jsonEncode({
|
body: jsonEncode({
|
||||||
"action": "receivable",
|
"action": "receivable",
|
||||||
"source": "true",
|
|
||||||
"account": destinationAddress,
|
"account": destinationAddress,
|
||||||
"count": "-1",
|
"count": "-1",
|
||||||
|
"source": true,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
final receivableData = await jsonDecode(receivableResponse.body);
|
final receivableData = await jsonDecode(receivableResponse.body);
|
||||||
if (receivableData["blocks"] == "") {
|
if (receivableData["blocks"] == "" || receivableData["blocks"] == null) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
final blocks = receivableData["blocks"] as Map<String, dynamic>;
|
|
||||||
|
dynamic blocks;
|
||||||
|
if (receivableData["blocks"] is List<dynamic>) {
|
||||||
|
var listBlocks = receivableData["blocks"] as List<dynamic>;
|
||||||
|
if (listBlocks.isEmpty) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
blocks = {for (var block in listBlocks) block['hash']: block};
|
||||||
|
} else {
|
||||||
|
blocks = receivableData["blocks"] as Map<String, dynamic>;
|
||||||
|
}
|
||||||
|
|
||||||
|
blocks = blocks as Map<String, dynamic>;
|
||||||
|
|
||||||
// confirm all receivable blocks:
|
// confirm all receivable blocks:
|
||||||
for (final blockHash in blocks.keys) {
|
for (final blockHash in blocks.keys) {
|
||||||
final block = blocks[blockHash];
|
final block = blocks[blockHash];
|
||||||
|
|
|
@ -48,7 +48,7 @@ class NanoTransactionInfo extends TransactionInfo {
|
||||||
return NanoTransactionInfo(
|
return NanoTransactionInfo(
|
||||||
id: data['id'] as String,
|
id: data['id'] as String,
|
||||||
height: data['height'] as int,
|
height: data['height'] as int,
|
||||||
amountRaw: data['amountRaw'] as BigInt,
|
amountRaw: BigInt.parse(data['amountRaw'] as String),
|
||||||
direction: parseTransactionDirectionFromInt(data['direction'] as int),
|
direction: parseTransactionDirectionFromInt(data['direction'] as int),
|
||||||
date: DateTime.fromMillisecondsSinceEpoch(data['date'] as int),
|
date: DateTime.fromMillisecondsSinceEpoch(data['date'] as int),
|
||||||
confirmed: data['confirmed'] as bool,
|
confirmed: data['confirmed'] as bool,
|
||||||
|
@ -60,7 +60,7 @@ class NanoTransactionInfo extends TransactionInfo {
|
||||||
Map<String, dynamic> toJson() => {
|
Map<String, dynamic> toJson() => {
|
||||||
'id': id,
|
'id': id,
|
||||||
'height': height,
|
'height': height,
|
||||||
'amountRaw': amountRaw,
|
'amountRaw': amountRaw.toString(),
|
||||||
'direction': direction.index,
|
'direction': direction.index,
|
||||||
'date': date.millisecondsSinceEpoch,
|
'date': date.millisecondsSinceEpoch,
|
||||||
'confirmed': confirmed,
|
'confirmed': confirmed,
|
||||||
|
|
|
@ -62,6 +62,7 @@ abstract class NanoWalletBase
|
||||||
late final String _privateKey;
|
late final String _privateKey;
|
||||||
late final String _publicAddress;
|
late final String _publicAddress;
|
||||||
late final String _seedKey;
|
late final String _seedKey;
|
||||||
|
Timer? _receiveTimer;
|
||||||
|
|
||||||
late NanoClient _client;
|
late NanoClient _client;
|
||||||
bool _isTransactionUpdating;
|
bool _isTransactionUpdating;
|
||||||
|
@ -114,13 +115,17 @@ abstract class NanoWalletBase
|
||||||
syncStatus = ConnectingSyncStatus();
|
syncStatus = ConnectingSyncStatus();
|
||||||
final isConnected = _client.connect(node);
|
final isConnected = _client.connect(node);
|
||||||
if (!isConnected) {
|
if (!isConnected) {
|
||||||
throw Exception("Ethereum Node connection failed");
|
throw Exception("Nano Node connection failed");
|
||||||
}
|
}
|
||||||
// _client.setListeners(_privateKey.address, _onNewTransaction);
|
|
||||||
await _updateBalance();
|
try {
|
||||||
await _receiveAll();
|
await _updateBalance();
|
||||||
|
await _receiveAll();
|
||||||
|
} catch (e) {}
|
||||||
|
|
||||||
syncStatus = ConnectedSyncStatus();
|
syncStatus = ConnectedSyncStatus();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
print(e);
|
||||||
syncStatus = FailedSyncStatus();
|
syncStatus = FailedSyncStatus();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -186,6 +191,7 @@ abstract class NanoWalletBase
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _receiveAll() async {
|
Future<void> _receiveAll() async {
|
||||||
|
await _updateBalance();
|
||||||
int blocksReceived = await this._client.confirmAllReceivable(
|
int blocksReceived = await this._client.confirmAllReceivable(
|
||||||
destinationAddress: _publicAddress,
|
destinationAddress: _publicAddress,
|
||||||
privateKey: _privateKey,
|
privateKey: _privateKey,
|
||||||
|
@ -269,14 +275,22 @@ abstract class NanoWalletBase
|
||||||
syncStatus = AttemptingSyncStatus();
|
syncStatus = AttemptingSyncStatus();
|
||||||
await _updateBalance();
|
await _updateBalance();
|
||||||
await updateTransactions();
|
await updateTransactions();
|
||||||
Timer.periodic(
|
|
||||||
const Duration(minutes: 1),
|
_receiveTimer?.cancel();
|
||||||
(timer) async => await _receiveAll(),
|
_receiveTimer = Timer.periodic(const Duration(seconds: 15), (timer) async {
|
||||||
);
|
// get our balance:
|
||||||
|
await _updateBalance();
|
||||||
|
// if we have anything to receive, process it:
|
||||||
|
if (balance[currency]!.receivableBalance > BigInt.zero) {
|
||||||
|
await _receiveAll();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
syncStatus = SyncedSyncStatus();
|
syncStatus = SyncedSyncStatus();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
print(e);
|
||||||
syncStatus = FailedSyncStatus();
|
syncStatus = FailedSyncStatus();
|
||||||
|
rethrow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue