cake_wallet/cw_zano/lib/api/wallet.dart

108 lines
3.6 KiB
Dart
Raw Normal View History

2023-10-02 14:17:35 +00:00
import 'dart:async';
2023-12-14 04:51:16 +00:00
import 'dart:convert';
2023-12-16 12:19:11 +00:00
2023-12-14 04:51:16 +00:00
import 'package:cw_core/crypto_currency.dart';
2023-12-16 12:19:11 +00:00
import 'package:cw_zano/api/calls.dart' as calls;
2023-12-14 04:51:16 +00:00
import 'package:cw_zano/api/model/get_wallet_info_result.dart';
import 'package:cw_zano/api/model/get_wallet_status_result.dart';
import 'package:cw_zano/api/model/zano_wallet_keys.dart';
import 'package:cw_zano/zano_balance.dart';
import 'package:cw_zano/zano_wallet.dart';
2023-10-02 14:17:35 +00:00
import 'package:flutter/foundation.dart';
2023-12-14 04:51:16 +00:00
import 'package:mobx/mobx.dart' as mobx;
2023-10-02 14:17:35 +00:00
2023-11-17 17:40:23 +00:00
int getCurrentHeight(int hWallet) {
2023-12-14 04:51:16 +00:00
final json = calls.getWalletStatus(hWallet);
final walletStatus = GetWalletStatusResult.fromJson(jsonDecode(json) as Map<String, dynamic>);
return walletStatus.currentWalletHeight;
2023-11-17 17:40:23 +00:00
}
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
int getNodeHeightSync(int hWallet) {
final json = calls.getWalletStatus(hWallet);
final walletStatus = GetWalletStatusResult.fromJson(jsonDecode(json) as Map<String, dynamic>);
return walletStatus.currentDaemonHeight;
}
2023-10-02 14:17:35 +00:00
class SyncListener {
SyncListener(this.onNewBlock, this.onNewTransaction)
: _cachedBlockchainHeight = 0,
_lastKnownBlockHeight = 0,
_initialSyncHeight = 0;
void Function(int, int, double) onNewBlock;
void Function() onNewTransaction;
Timer? _updateSyncInfoTimer;
int _cachedBlockchainHeight;
int _lastKnownBlockHeight;
int _initialSyncHeight;
2023-12-14 04:51:16 +00:00
Future<int> getNodeHeightOrUpdate(int hWallet, int baseHeight) async {
2023-10-02 14:17:35 +00:00
if (_cachedBlockchainHeight < baseHeight || _cachedBlockchainHeight == 0) {
2023-12-14 04:51:16 +00:00
_cachedBlockchainHeight = await compute<int, int>(getNodeHeightSync, hWallet);
2023-10-02 14:17:35 +00:00
}
return _cachedBlockchainHeight;
}
2023-12-14 04:51:16 +00:00
void start(ZanoWalletBase wallet, int hWallet) async {
2023-10-02 14:17:35 +00:00
_cachedBlockchainHeight = 0;
_lastKnownBlockHeight = 0;
_initialSyncHeight = 0;
2023-12-14 04:51:16 +00:00
_updateSyncInfoTimer ??= Timer.periodic(Duration(milliseconds: 1200), (_) async {
/**if (isNewTransactionExist()) {
2023-10-02 14:17:35 +00:00
onNewTransaction?.call();
2023-12-14 04:51:16 +00:00
}*/
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
var syncHeight = getCurrentHeight(hWallet);
2023-10-02 14:17:35 +00:00
2023-12-14 04:51:16 +00:00
final json = calls.getWalletInfo(hWallet);
final result = GetWalletInfoResult.fromJson(jsonDecode(json) as Map<String, dynamic>);
wallet.seed = result.wiExtended.seed;
wallet.keys = ZanoWalletKeys(
privateSpendKey: result.wiExtended.spendPrivateKey,
privateViewKey: result.wiExtended.viewPrivateKey,
publicSpendKey: result.wiExtended.spendPublicKey,
publicViewKey: result.wiExtended.viewPublicKey,
);
final balance = result.wi.balances.first;
wallet.assetId = balance.assetInfo.assetId;
wallet.balance = mobx.ObservableMap.of(
{CryptoCurrency.zano: ZanoBalance(total: balance.total, unlocked: balance.unlocked)});
2023-10-02 14:17:35 +00:00
if (_initialSyncHeight <= 0) {
_initialSyncHeight = syncHeight;
}
2023-12-14 04:51:16 +00:00
final bchHeight = await getNodeHeightOrUpdate(hWallet, syncHeight);
2023-10-02 14:17:35 +00:00
if (_lastKnownBlockHeight == syncHeight || syncHeight == null) {
return;
}
_lastKnownBlockHeight = syncHeight;
final track = bchHeight - _initialSyncHeight;
final diff = track - (bchHeight - syncHeight);
final ptc = diff <= 0 ? 0.0 : diff / track;
final left = bchHeight - syncHeight;
if (syncHeight < 0 || left < 0) {
return;
}
// 1. Actual new height; 2. Blocks left to finish; 3. Progress in percents;
onNewBlock?.call(syncHeight, left, ptc);
});
}
void stop() => _updateSyncInfoTimer?.cancel();
}
2023-12-14 04:51:16 +00:00
SyncListener setListeners(
void Function(int, int, double) onNewBlock, void Function() onNewTransaction) {
2023-10-02 14:17:35 +00:00
final listener = SyncListener(onNewBlock, onNewTransaction);
2023-11-17 17:40:23 +00:00
/**setListenerNative();*/
2023-10-02 14:17:35 +00:00
return listener;
}