fixes + auto receive incoming txs

This commit is contained in:
fosse 2023-08-01 13:48:22 -04:00
parent 92d4a6f6d6
commit f8a2e4dd7c
3 changed files with 41 additions and 17 deletions

View file

@ -31,11 +31,8 @@ class NanoClient {
return false;
}
}
void setListeners(EthereumAddress userAddress, Function(FilterEvent) onNewTransaction) 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(
_node!.uri,
headers: {"Content-Type": "application/json"},
@ -319,16 +316,29 @@ class NanoClient {
headers: {"Content-Type": "application/json"},
body: jsonEncode({
"action": "receivable",
"source": "true",
"account": destinationAddress,
"count": "-1",
"source": true,
}));
final receivableData = await jsonDecode(receivableResponse.body);
if (receivableData["blocks"] == "") {
if (receivableData["blocks"] == "" || receivableData["blocks"] == null) {
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:
for (final blockHash in blocks.keys) {
final block = blocks[blockHash];

View file

@ -48,7 +48,7 @@ class NanoTransactionInfo extends TransactionInfo {
return NanoTransactionInfo(
id: data['id'] as String,
height: data['height'] as int,
amountRaw: data['amountRaw'] as BigInt,
amountRaw: BigInt.parse(data['amountRaw'] as String),
direction: parseTransactionDirectionFromInt(data['direction'] as int),
date: DateTime.fromMillisecondsSinceEpoch(data['date'] as int),
confirmed: data['confirmed'] as bool,
@ -60,7 +60,7 @@ class NanoTransactionInfo extends TransactionInfo {
Map<String, dynamic> toJson() => {
'id': id,
'height': height,
'amountRaw': amountRaw,
'amountRaw': amountRaw.toString(),
'direction': direction.index,
'date': date.millisecondsSinceEpoch,
'confirmed': confirmed,

View file

@ -62,6 +62,7 @@ abstract class NanoWalletBase
late final String _privateKey;
late final String _publicAddress;
late final String _seedKey;
Timer? _receiveTimer;
late NanoClient _client;
bool _isTransactionUpdating;
@ -114,13 +115,17 @@ abstract class NanoWalletBase
syncStatus = ConnectingSyncStatus();
final isConnected = _client.connect(node);
if (!isConnected) {
throw Exception("Ethereum Node connection failed");
throw Exception("Nano Node connection failed");
}
// _client.setListeners(_privateKey.address, _onNewTransaction);
await _updateBalance();
await _receiveAll();
try {
await _updateBalance();
await _receiveAll();
} catch (e) {}
syncStatus = ConnectedSyncStatus();
} catch (e) {
print(e);
syncStatus = FailedSyncStatus();
}
}
@ -186,6 +191,7 @@ abstract class NanoWalletBase
}
Future<void> _receiveAll() async {
await _updateBalance();
int blocksReceived = await this._client.confirmAllReceivable(
destinationAddress: _publicAddress,
privateKey: _privateKey,
@ -269,14 +275,22 @@ abstract class NanoWalletBase
syncStatus = AttemptingSyncStatus();
await _updateBalance();
await updateTransactions();
Timer.periodic(
const Duration(minutes: 1),
(timer) async => await _receiveAll(),
);
_receiveTimer?.cancel();
_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();
} catch (e) {
print(e);
syncStatus = FailedSyncStatus();
rethrow;
}
}