-
Notifications
You must be signed in to change notification settings - Fork 15
/
auto_receive_tx_worker.dart
141 lines (133 loc) · 4.63 KB
/
auto_receive_tx_worker.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import 'dart:async';
import 'dart:collection';
import 'package:json_rpc_2/json_rpc_2.dart';
import 'package:logging/logging.dart';
import 'package:zenon_syrius_wallet_flutter/blocs/auto_unlock_htlc_worker.dart';
import 'package:zenon_syrius_wallet_flutter/blocs/blocs.dart';
import 'package:zenon_syrius_wallet_flutter/main.dart';
import 'package:zenon_syrius_wallet_flutter/model/model.dart';
import 'package:zenon_syrius_wallet_flutter/utils/account_block_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/address_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/constants.dart';
import 'package:znn_sdk_dart/znn_sdk_dart.dart';
class AutoReceiveTxWorker extends BaseBloc<WalletNotification> {
static AutoReceiveTxWorker? _instance;
Queue<Hash> pool = Queue<Hash>();
bool running = false;
static AutoReceiveTxWorker getInstance() {
_instance ??= AutoReceiveTxWorker();
return _instance!;
}
Future<void> autoReceiveTransactionHash(Hash currentHash) async {
if (!running) {
running = true;
try {
Address toAddress =
(await zenon!.ledger.getAccountBlockByHash(currentHash))!
.toAddress;
AccountBlockTemplate transactionParams = AccountBlockTemplate.receive(
currentHash,
);
AccountBlockTemplate response =
await AccountBlockUtils.createAccountBlock(
transactionParams,
'receive transaction',
address: toAddress,
waitForRequiredPlasma: true,
);
_sendSuccessNotification(response, toAddress.toString());
} on RpcException catch (e, stackTrace) {
_sendErrorNotification(e.toString());
Logger('AutoReceiveTxWorker')
.log(Level.WARNING, 'autoReceive', e, stackTrace);
}
running = false;
}
}
Future<void> autoReceive() async {
if (!sharedPrefsService!.get(
kAutoReceiveKey,
defaultValue: kAutoReceiveDefaultValue,
)) {
pool.clear();
return;
}
// Make sure that AutoUnlockHtlcWorker is not running since it should be
// given priority to send transactions.
if (pool.isNotEmpty && !running && !sl<AutoUnlockHtlcWorker>().running) {
running = true;
Hash currentHash = pool.first;
try {
Address toAddress =
(await zenon!.ledger.getAccountBlockByHash(currentHash))!
.toAddress;
AccountBlockTemplate transactionParams = AccountBlockTemplate.receive(
currentHash,
);
AccountBlockTemplate response =
await AccountBlockUtils.createAccountBlock(
transactionParams,
'receive transaction',
address: toAddress,
waitForRequiredPlasma: true,
);
_sendSuccessNotification(response, toAddress.toString());
if (pool.isNotEmpty) {
pool.removeFirst();
}
running = false;
autoReceive();
} on RpcException catch (e, stackTrace) {
_sendErrorNotification(e.toString());
Logger('AutoReceiveTxWorker')
.log(Level.WARNING, 'autoReceive', e, stackTrace);
if (e.message.compareTo('account-block from-block already received') ==
0) {
if (pool.isNotEmpty) {
pool.removeFirst();
}
} else {
_sendErrorNotification(e.toString());
}
} catch (e, stackTrace) {
Logger('AutoReceiveTxWorker')
.log(Level.WARNING, 'autoReceive', e, stackTrace);
_sendErrorNotification(e.toString());
}
running = false;
}
return;
}
Future<void> addHash(Hash hash) async {
zenon!.stats.syncInfo().then((syncInfo) {
if (!pool.contains(hash) &&
(syncInfo.state == SyncState.syncDone ||
(syncInfo.targetHeight > 0 &&
syncInfo.currentHeight > 0 &&
(syncInfo.targetHeight - syncInfo.currentHeight) < 3))) {
pool.add(hash);
}
});
}
void _sendErrorNotification(String errorText) {
addEvent(
WalletNotification(
title: 'Receive transaction failed',
timestamp: DateTime.now().millisecondsSinceEpoch,
details: 'Failed to receive the transaction: $errorText',
type: NotificationType.error,
),
);
}
void _sendSuccessNotification(AccountBlockTemplate block, String toAddress) {
addEvent(
WalletNotification(
title:
'Transaction received on ${ZenonAddressUtils.getLabel(toAddress)}',
timestamp: DateTime.now().millisecondsSinceEpoch,
details: 'Transaction hash: ${block.hash}',
type: NotificationType.paymentReceived,
),
);
}
}