2023-06-12 19:03:32 +00:00
|
|
|
import 'dart:async';
|
2023-06-21 11:29:28 +00:00
|
|
|
import 'dart:convert';
|
2023-06-12 19:03:32 +00:00
|
|
|
|
2023-08-14 14:36:27 +00:00
|
|
|
import 'package:decimal/decimal.dart';
|
2023-06-12 19:03:32 +00:00
|
|
|
import 'package:http/http.dart';
|
2023-06-21 11:29:28 +00:00
|
|
|
import 'package:isar/isar.dart';
|
2023-07-28 16:32:50 +00:00
|
|
|
import 'package:stackwallet/db/isar/main_db.dart';
|
2023-06-12 19:03:32 +00:00
|
|
|
import 'package:stackwallet/models/balance.dart';
|
|
|
|
import 'package:stackwallet/models/isar/models/blockchain_data/address.dart';
|
|
|
|
import 'package:stackwallet/models/isar/models/blockchain_data/transaction.dart';
|
|
|
|
import 'package:stackwallet/models/isar/models/blockchain_data/utxo.dart';
|
2023-07-28 16:32:50 +00:00
|
|
|
import 'package:stackwallet/models/node_model.dart';
|
2023-06-12 19:03:32 +00:00
|
|
|
import 'package:stackwallet/models/paymint/fee_object_model.dart';
|
|
|
|
import 'package:stackwallet/services/coins/coin_service.dart';
|
2023-08-14 22:53:44 +00:00
|
|
|
import 'package:stackwallet/services/event_bus/events/global/node_connection_status_changed_event.dart';
|
|
|
|
import 'package:stackwallet/services/event_bus/global_event_bus.dart';
|
2023-07-28 16:32:50 +00:00
|
|
|
import 'package:stackwallet/services/mixins/wallet_cache.dart';
|
|
|
|
import 'package:stackwallet/services/mixins/wallet_db.dart';
|
2023-06-12 19:03:32 +00:00
|
|
|
import 'package:stackwallet/services/node_service.dart';
|
2023-07-28 16:32:50 +00:00
|
|
|
import 'package:stackwallet/services/transaction_notification_tracker.dart';
|
2023-06-12 19:03:32 +00:00
|
|
|
import 'package:stackwallet/utilities/amount/amount.dart';
|
2023-08-14 22:53:44 +00:00
|
|
|
import 'package:stackwallet/utilities/constants.dart';
|
2023-06-12 19:03:32 +00:00
|
|
|
import 'package:stackwallet/utilities/default_nodes.dart';
|
2023-07-28 16:32:50 +00:00
|
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
2023-06-21 11:29:28 +00:00
|
|
|
import 'package:stackwallet/utilities/enums/fee_rate_type_enum.dart';
|
2023-07-28 16:32:50 +00:00
|
|
|
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
|
|
|
|
import 'package:stackwallet/utilities/logger.dart';
|
|
|
|
import 'package:stackwallet/utilities/prefs.dart';
|
2023-06-12 19:03:32 +00:00
|
|
|
import 'package:tezart/tezart.dart';
|
2023-06-21 11:29:28 +00:00
|
|
|
import 'package:tuple/tuple.dart';
|
2023-06-12 19:03:32 +00:00
|
|
|
|
|
|
|
const int MINIMUM_CONFIRMATIONS = 1;
|
2023-08-16 12:46:21 +00:00
|
|
|
const int _gasLimit = 10200;
|
2023-06-12 19:03:32 +00:00
|
|
|
|
|
|
|
class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|
|
|
TezosWallet({
|
|
|
|
required String walletId,
|
|
|
|
required String walletName,
|
|
|
|
required Coin coin,
|
|
|
|
required SecureStorageInterface secureStore,
|
|
|
|
required TransactionNotificationTracker tracker,
|
|
|
|
MainDB? mockableOverride,
|
|
|
|
}) {
|
|
|
|
txTracker = tracker;
|
|
|
|
_walletId = walletId;
|
|
|
|
_walletName = walletName;
|
|
|
|
_coin = coin;
|
|
|
|
_secureStore = secureStore;
|
|
|
|
initCache(walletId, coin);
|
|
|
|
initWalletDB(mockableOverride: mockableOverride);
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeModel? _xtzNode;
|
|
|
|
|
|
|
|
NodeModel getCurrentNode() {
|
2023-07-28 16:32:50 +00:00
|
|
|
return _xtzNode ??
|
|
|
|
NodeService(secureStorageInterface: _secureStore)
|
|
|
|
.getPrimaryNodeFor(coin: Coin.tezos) ??
|
|
|
|
DefaultNodes.getNodeFor(Coin.tezos);
|
2023-06-12 19:03:32 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 11:29:28 +00:00
|
|
|
Future<Keystore> getKeystore() async {
|
|
|
|
return Keystore.fromMnemonic((await mnemonicString).toString());
|
|
|
|
}
|
|
|
|
|
2023-06-12 19:03:32 +00:00
|
|
|
@override
|
|
|
|
String get walletId => _walletId;
|
|
|
|
late String _walletId;
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get walletName => _walletName;
|
|
|
|
late String _walletName;
|
|
|
|
|
|
|
|
@override
|
|
|
|
set walletName(String name) => _walletName = name;
|
|
|
|
|
|
|
|
@override
|
|
|
|
set isFavorite(bool markFavorite) {
|
|
|
|
_isFavorite = markFavorite;
|
|
|
|
updateCachedIsFavorite(markFavorite);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isFavorite => _isFavorite ??= getCachedIsFavorite();
|
|
|
|
bool? _isFavorite;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Coin get coin => _coin;
|
|
|
|
late Coin _coin;
|
|
|
|
|
|
|
|
late SecureStorageInterface _secureStore;
|
|
|
|
late final TransactionNotificationTracker txTracker;
|
|
|
|
final _prefs = Prefs.instance;
|
|
|
|
|
|
|
|
Timer? timer;
|
|
|
|
bool _shouldAutoSync = false;
|
2023-08-14 22:53:44 +00:00
|
|
|
Timer? _networkAliveTimer;
|
2023-06-12 19:03:32 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
bool get shouldAutoSync => _shouldAutoSync;
|
|
|
|
|
|
|
|
@override
|
|
|
|
set shouldAutoSync(bool shouldAutoSync) {
|
|
|
|
if (_shouldAutoSync != shouldAutoSync) {
|
|
|
|
_shouldAutoSync = shouldAutoSync;
|
|
|
|
if (!shouldAutoSync) {
|
|
|
|
timer?.cancel();
|
|
|
|
timer = null;
|
2023-08-14 22:53:44 +00:00
|
|
|
stopNetworkAlivePinging();
|
2023-06-12 19:03:32 +00:00
|
|
|
} else {
|
2023-08-14 22:53:44 +00:00
|
|
|
startNetworkAlivePinging();
|
2023-06-12 19:03:32 +00:00
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-14 22:53:44 +00:00
|
|
|
void startNetworkAlivePinging() {
|
|
|
|
// call once on start right away
|
|
|
|
_periodicPingCheck();
|
|
|
|
|
|
|
|
// then periodically check
|
|
|
|
_networkAliveTimer = Timer.periodic(
|
|
|
|
Constants.networkAliveTimerDuration,
|
|
|
|
(_) async {
|
|
|
|
_periodicPingCheck();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void stopNetworkAlivePinging() {
|
|
|
|
_networkAliveTimer?.cancel();
|
|
|
|
_networkAliveTimer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _periodicPingCheck() async {
|
|
|
|
bool hasNetwork = await testNetworkConnection();
|
|
|
|
|
|
|
|
if (_isConnected != hasNetwork) {
|
|
|
|
NodeConnectionStatus status = hasNetwork
|
|
|
|
? NodeConnectionStatus.connected
|
|
|
|
: NodeConnectionStatus.disconnected;
|
|
|
|
|
|
|
|
GlobalEventBus.instance.fire(
|
|
|
|
NodeConnectionStatusChangedEvent(
|
|
|
|
status,
|
|
|
|
walletId,
|
|
|
|
coin,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
_isConnected = hasNetwork;
|
|
|
|
if (hasNetwork) {
|
|
|
|
unawaited(refresh());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-12 19:03:32 +00:00
|
|
|
@override
|
|
|
|
Balance get balance => _balance ??= getCachedBalance();
|
|
|
|
Balance? _balance;
|
|
|
|
|
|
|
|
@override
|
2023-07-28 16:32:50 +00:00
|
|
|
Future<Map<String, dynamic>> prepareSend(
|
|
|
|
{required String address,
|
|
|
|
required Amount amount,
|
|
|
|
Map<String, dynamic>? args}) async {
|
2023-06-21 11:29:28 +00:00
|
|
|
try {
|
|
|
|
if (amount.decimals != coin.decimals) {
|
|
|
|
throw Exception("Amount decimals do not match coin decimals!");
|
|
|
|
}
|
2023-07-28 16:32:50 +00:00
|
|
|
var fee = int.parse((await estimateFeeFor(
|
|
|
|
amount, (args!["feeRate"] as FeeRateType).index))
|
|
|
|
.raw
|
|
|
|
.toString());
|
2023-06-21 11:29:28 +00:00
|
|
|
Map<String, dynamic> txData = {
|
|
|
|
"fee": fee,
|
|
|
|
"address": address,
|
|
|
|
"recipientAmt": amount,
|
|
|
|
};
|
|
|
|
return Future.value(txData);
|
|
|
|
} catch (e) {
|
|
|
|
return Future.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<String> confirmSend({required Map<String, dynamic> txData}) async {
|
|
|
|
try {
|
2023-08-14 14:36:27 +00:00
|
|
|
final amount = txData["recipientAmt"] as Amount;
|
2023-08-23 17:08:32 +00:00
|
|
|
final amountInMicroTez = amount.decimal * Decimal.fromInt(1000000);
|
2023-08-14 14:36:27 +00:00
|
|
|
final microtezToInt = int.parse(amountInMicroTez.toString());
|
|
|
|
|
2023-06-21 11:29:28 +00:00
|
|
|
final int feeInMicroTez = int.parse(txData["fee"].toString());
|
|
|
|
final String destinationAddress = txData["address"] as String;
|
2023-08-23 17:08:32 +00:00
|
|
|
final secretKey =
|
|
|
|
Keystore.fromMnemonic((await mnemonicString)!).secretKey;
|
2023-08-15 16:01:51 +00:00
|
|
|
|
2023-08-05 21:16:19 +00:00
|
|
|
Logging.instance.log(secretKey, level: LogLevel.Info);
|
|
|
|
final sourceKeyStore = Keystore.fromSecretKey(secretKey);
|
2023-08-14 14:36:27 +00:00
|
|
|
final client = TezartClient(getCurrentNode().host);
|
2023-08-15 14:49:28 +00:00
|
|
|
|
2023-08-15 16:01:51 +00:00
|
|
|
int? sendAmount = microtezToInt;
|
2023-08-16 12:46:21 +00:00
|
|
|
int gasLimit = _gasLimit;
|
|
|
|
int thisFee = feeInMicroTez;
|
|
|
|
|
2023-08-15 16:01:51 +00:00
|
|
|
if (balance.spendable == txData["recipientAmt"] as Amount) {
|
2023-08-16 12:46:21 +00:00
|
|
|
//Fee guides for emptying a tz account
|
|
|
|
// https://github.com/TezTech/eztz/blob/master/PROTO_004_FEES.md
|
2023-08-23 17:08:32 +00:00
|
|
|
thisFee = thisFee + 32;
|
|
|
|
sendAmount = microtezToInt - thisFee;
|
|
|
|
gasLimit = _gasLimit + 320;
|
2023-08-15 16:01:51 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 14:36:27 +00:00
|
|
|
final operation = await client.transferOperation(
|
|
|
|
source: sourceKeyStore,
|
|
|
|
destination: destinationAddress,
|
2023-08-15 16:01:51 +00:00
|
|
|
amount: sendAmount,
|
2023-08-14 14:36:27 +00:00
|
|
|
customFee: feeInMicroTez,
|
2023-08-23 17:08:32 +00:00
|
|
|
customGasLimit: gasLimit);
|
2023-08-16 12:46:21 +00:00
|
|
|
await operation.executeAndMonitor();
|
|
|
|
return operation.result.id as String;
|
2023-06-21 11:29:28 +00:00
|
|
|
} catch (e) {
|
|
|
|
Logging.instance.log(e.toString(), level: LogLevel.Error);
|
|
|
|
return Future.error(e);
|
|
|
|
}
|
2023-06-12 19:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<String> get currentReceivingAddress async {
|
|
|
|
var mneString = await mnemonicString;
|
|
|
|
if (mneString == null) {
|
|
|
|
throw Exception("No mnemonic found!");
|
|
|
|
}
|
2023-06-21 11:29:28 +00:00
|
|
|
return Future.value((Keystore.fromMnemonic(mneString)).address);
|
2023-06-12 19:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-08-05 21:16:19 +00:00
|
|
|
Future<Amount> estimateFeeFor(Amount amount, int feeRate) async {
|
2023-08-16 13:17:58 +00:00
|
|
|
var api = "https://api.tzstats.com/series/op?start_date=today&collapse=1d";
|
2023-08-05 21:16:19 +00:00
|
|
|
var response = jsonDecode((await get(Uri.parse(api))).body)[0];
|
|
|
|
double totalFees = response[4] as double;
|
|
|
|
int totalTxs = response[8] as int;
|
|
|
|
int feePerTx = (totalFees / totalTxs * 1000000).floor();
|
2023-08-16 13:17:58 +00:00
|
|
|
|
2023-08-14 22:53:44 +00:00
|
|
|
return Amount(
|
2023-08-16 13:17:58 +00:00
|
|
|
rawValue: BigInt.from(feePerTx),
|
|
|
|
fractionDigits: coin.decimals,
|
|
|
|
);
|
2023-06-12 19:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> exit() {
|
|
|
|
_hasCalledExit = true;
|
|
|
|
return Future.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-06-21 11:29:28 +00:00
|
|
|
Future<FeeObject> get fees async {
|
2023-08-05 21:16:19 +00:00
|
|
|
var api = "https://api.tzstats.com/series/op?start_date=today&collapse=10d";
|
|
|
|
var response = jsonDecode((await get(Uri.parse(api))).body);
|
|
|
|
double totalFees = response[0][4] as double;
|
|
|
|
int totalTxs = response[0][8] as int;
|
|
|
|
int feePerTx = (totalFees / totalTxs * 1000000).floor();
|
|
|
|
Logging.instance.log("feePerTx:$feePerTx", level: LogLevel.Info);
|
2023-08-16 13:17:58 +00:00
|
|
|
// TODO: fix numberOfBlocks - Since there is only one fee no need to set blocks
|
2023-06-21 11:29:28 +00:00
|
|
|
return FeeObject(
|
2023-08-16 13:17:58 +00:00
|
|
|
numberOfBlocksFast: 10,
|
2023-08-05 21:16:19 +00:00
|
|
|
numberOfBlocksAverage: 10,
|
2023-08-16 13:17:58 +00:00
|
|
|
numberOfBlocksSlow: 10,
|
|
|
|
fast: feePerTx,
|
2023-08-05 21:16:19 +00:00
|
|
|
medium: feePerTx,
|
2023-08-16 13:17:58 +00:00
|
|
|
slow: feePerTx,
|
2023-06-21 11:29:28 +00:00
|
|
|
);
|
|
|
|
}
|
2023-06-12 19:03:32 +00:00
|
|
|
|
|
|
|
@override
|
2023-07-28 16:32:50 +00:00
|
|
|
Future<void> fullRescan(
|
|
|
|
int maxUnusedAddressGap, int maxNumberOfIndexesToCheck) {
|
2023-06-21 11:29:28 +00:00
|
|
|
refresh();
|
|
|
|
return Future.value();
|
2023-06-12 19:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> generateNewAddress() {
|
|
|
|
// TODO: implement generateNewAddress
|
|
|
|
throw UnimplementedError();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get hasCalledExit => _hasCalledExit;
|
|
|
|
bool _hasCalledExit = false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> initializeExisting() async {
|
|
|
|
await _prefs.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-08-23 17:08:32 +00:00
|
|
|
Future<void> initializeNew(
|
|
|
|
({String mnemonicPassphrase, int wordCount})? data,
|
|
|
|
) async {
|
2023-08-05 21:16:19 +00:00
|
|
|
if ((await mnemonicString) != null || (await mnemonicPassphrase) != null) {
|
|
|
|
throw Exception(
|
|
|
|
"Attempted to overwrite mnemonic on generate new wallet!");
|
|
|
|
}
|
|
|
|
|
|
|
|
await _prefs.init();
|
|
|
|
|
2023-06-12 19:03:32 +00:00
|
|
|
var newKeystore = Keystore.random();
|
|
|
|
await _secureStore.write(
|
|
|
|
key: '${_walletId}_mnemonic',
|
|
|
|
value: newKeystore.mnemonic,
|
|
|
|
);
|
|
|
|
await _secureStore.write(
|
|
|
|
key: '${_walletId}_mnemonicPassphrase',
|
|
|
|
value: "",
|
|
|
|
);
|
|
|
|
|
|
|
|
final address = Address(
|
2023-07-28 16:32:50 +00:00
|
|
|
walletId: walletId,
|
|
|
|
value: newKeystore.address,
|
|
|
|
publicKey: [], // TODO: Add public key
|
|
|
|
derivationIndex: 0,
|
|
|
|
derivationPath: null,
|
|
|
|
type: AddressType.unknown,
|
|
|
|
subType: AddressSubType.unknown,
|
2023-06-12 19:03:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await db.putAddress(address);
|
|
|
|
|
|
|
|
await Future.wait([
|
|
|
|
updateCachedId(walletId),
|
|
|
|
updateCachedIsFavorite(false),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isConnected => _isConnected;
|
|
|
|
bool _isConnected = false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isRefreshing => refreshMutex;
|
|
|
|
bool refreshMutex = false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
// TODO: implement maxFee
|
|
|
|
Future<int> get maxFee => throw UnimplementedError();
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<List<String>> get mnemonic async {
|
|
|
|
final mnemonic = await mnemonicString;
|
|
|
|
final mnemonicPassphrase = await this.mnemonicPassphrase;
|
|
|
|
if (mnemonic == null) {
|
|
|
|
throw Exception("No mnemonic found!");
|
|
|
|
}
|
|
|
|
if (mnemonicPassphrase == null) {
|
|
|
|
throw Exception("No mnemonic passphrase found!");
|
|
|
|
}
|
|
|
|
return mnemonic.split(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-07-28 16:32:50 +00:00
|
|
|
Future<String?> get mnemonicPassphrase =>
|
|
|
|
_secureStore.read(key: '${_walletId}_mnemonicPassphrase');
|
2023-06-12 19:03:32 +00:00
|
|
|
|
|
|
|
@override
|
2023-07-28 16:32:50 +00:00
|
|
|
Future<String?> get mnemonicString =>
|
|
|
|
_secureStore.read(key: '${_walletId}_mnemonic');
|
2023-06-12 19:03:32 +00:00
|
|
|
|
|
|
|
@override
|
2023-07-28 16:32:50 +00:00
|
|
|
Future<void> recoverFromMnemonic(
|
|
|
|
{required String mnemonic,
|
|
|
|
String? mnemonicPassphrase,
|
|
|
|
required int maxUnusedAddressGap,
|
|
|
|
required int maxNumberOfIndexesToCheck,
|
|
|
|
required int height}) async {
|
2023-06-12 19:03:32 +00:00
|
|
|
if ((await mnemonicString) != null ||
|
|
|
|
(await this.mnemonicPassphrase) != null) {
|
|
|
|
throw Exception("Attempted to overwrite mnemonic on restore!");
|
|
|
|
}
|
|
|
|
await _secureStore.write(
|
|
|
|
key: '${_walletId}_mnemonic', value: mnemonic.trim());
|
|
|
|
await _secureStore.write(
|
|
|
|
key: '${_walletId}_mnemonicPassphrase',
|
|
|
|
value: mnemonicPassphrase ?? "",
|
|
|
|
);
|
|
|
|
|
|
|
|
final address = Address(
|
2023-07-28 16:32:50 +00:00
|
|
|
walletId: walletId,
|
|
|
|
value: Keystore.fromMnemonic(mnemonic).address,
|
|
|
|
publicKey: [], // TODO: Add public key
|
|
|
|
derivationIndex: 0,
|
|
|
|
derivationPath: null,
|
|
|
|
type: AddressType.unknown,
|
|
|
|
subType: AddressSubType.unknown,
|
2023-06-12 19:03:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await db.putAddress(address);
|
|
|
|
|
|
|
|
await Future.wait([
|
|
|
|
updateCachedId(walletId),
|
|
|
|
updateCachedIsFavorite(false),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> updateBalance() async {
|
2023-08-14 14:36:27 +00:00
|
|
|
try {
|
2023-08-16 12:46:21 +00:00
|
|
|
String balanceCall = "https://api.mainnet.tzkt.io/v1/accounts/"
|
|
|
|
"${await currentReceivingAddress}/balance";
|
2023-08-23 17:08:32 +00:00
|
|
|
var response = jsonDecode(
|
|
|
|
await get(Uri.parse(balanceCall)).then((value) => value.body));
|
2023-08-15 13:28:53 +00:00
|
|
|
Amount balanceInAmount = Amount(
|
2023-08-16 12:46:21 +00:00
|
|
|
rawValue: BigInt.parse(response.toString()),
|
2023-08-14 22:53:44 +00:00
|
|
|
fractionDigits: coin.decimals);
|
2023-08-14 14:36:27 +00:00
|
|
|
_balance = Balance(
|
|
|
|
total: balanceInAmount,
|
|
|
|
spendable: balanceInAmount,
|
2023-08-14 22:53:44 +00:00
|
|
|
blockedTotal:
|
|
|
|
Amount(rawValue: BigInt.parse("0"), fractionDigits: coin.decimals),
|
|
|
|
pendingSpendable:
|
|
|
|
Amount(rawValue: BigInt.parse("0"), fractionDigits: coin.decimals),
|
2023-08-14 14:36:27 +00:00
|
|
|
);
|
|
|
|
await updateCachedBalance(_balance!);
|
|
|
|
} catch (e, s) {
|
2023-08-14 22:53:44 +00:00
|
|
|
Logging.instance
|
|
|
|
.log("ERROR GETTING BALANCE ${e.toString()}", level: LogLevel.Error);
|
2023-08-14 14:36:27 +00:00
|
|
|
}
|
2023-06-12 19:03:32 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 11:29:28 +00:00
|
|
|
Future<void> updateTransactions() async {
|
2023-08-16 12:46:21 +00:00
|
|
|
String transactionsCall = "https://api.mainnet.tzkt.io/v1/accounts/"
|
|
|
|
"${await currentReceivingAddress}/operations";
|
2023-08-23 17:08:32 +00:00
|
|
|
var response = jsonDecode(
|
|
|
|
await get(Uri.parse(transactionsCall)).then((value) => value.body));
|
2023-06-21 11:29:28 +00:00
|
|
|
List<Tuple2<Transaction, Address>> txs = [];
|
2023-08-15 14:49:28 +00:00
|
|
|
for (var tx in response as List) {
|
|
|
|
if (tx["type"] == "transaction") {
|
|
|
|
TransactionType txType;
|
|
|
|
if (tx["sender"]["address"] == (await currentReceivingAddress)) {
|
|
|
|
txType = TransactionType.outgoing;
|
|
|
|
} else {
|
|
|
|
txType = TransactionType.incoming;
|
2023-07-28 19:16:04 +00:00
|
|
|
}
|
2023-08-15 14:49:28 +00:00
|
|
|
|
|
|
|
var theTx = Transaction(
|
|
|
|
walletId: walletId,
|
|
|
|
txid: tx["hash"].toString(),
|
|
|
|
timestamp: DateTime.parse(tx["timestamp"].toString())
|
2023-08-23 17:08:32 +00:00
|
|
|
.toUtc()
|
|
|
|
.millisecondsSinceEpoch ~/
|
2023-08-15 14:49:28 +00:00
|
|
|
1000,
|
|
|
|
type: txType,
|
|
|
|
subType: TransactionSubType.none,
|
|
|
|
amount: tx["amount"] as int,
|
|
|
|
amountString: Amount(
|
2023-08-23 17:08:32 +00:00
|
|
|
rawValue:
|
|
|
|
BigInt.parse((tx["amount"] as int).toInt().toString()),
|
|
|
|
fractionDigits: coin.decimals)
|
2023-08-15 14:49:28 +00:00
|
|
|
.toJsonString(),
|
|
|
|
fee: tx["bakerFee"] as int,
|
|
|
|
height: int.parse(tx["level"].toString()),
|
|
|
|
isCancelled: false,
|
|
|
|
isLelantus: false,
|
|
|
|
slateId: "",
|
|
|
|
otherData: "",
|
|
|
|
inputs: [],
|
|
|
|
outputs: [],
|
|
|
|
nonce: 0,
|
|
|
|
numberOfMessages: null,
|
|
|
|
);
|
|
|
|
var theAddress = Address(
|
|
|
|
walletId: walletId,
|
|
|
|
value: tx["target"]["address"].toString(),
|
|
|
|
publicKey: [], // TODO: Add public key
|
|
|
|
derivationIndex: 0,
|
|
|
|
derivationPath: null,
|
|
|
|
type: AddressType.unknown,
|
|
|
|
subType: AddressSubType.unknown,
|
|
|
|
);
|
|
|
|
txs.add(Tuple2(theTx, theAddress));
|
2023-06-21 11:29:28 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-28 19:16:04 +00:00
|
|
|
Logging.instance.log("Transactions: $txs", level: LogLevel.Info);
|
2023-07-28 10:39:07 +00:00
|
|
|
await db.addNewTransactionData(txs, walletId);
|
2023-06-21 11:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> updateChainHeight() async {
|
2023-08-14 14:36:27 +00:00
|
|
|
try {
|
2023-08-14 22:53:44 +00:00
|
|
|
var api = "${getCurrentNode().host}/chains/main/blocks/head/header/shell";
|
2023-08-14 14:36:27 +00:00
|
|
|
var jsonParsedResponse =
|
2023-08-14 22:53:44 +00:00
|
|
|
jsonDecode(await get(Uri.parse(api)).then((value) => value.body));
|
2023-08-14 14:36:27 +00:00
|
|
|
final int intHeight = int.parse(jsonParsedResponse["level"].toString());
|
|
|
|
Logging.instance.log("Chain height: $intHeight", level: LogLevel.Info);
|
|
|
|
await updateCachedChainHeight(intHeight);
|
|
|
|
} catch (e, s) {
|
2023-08-14 22:53:44 +00:00
|
|
|
Logging.instance
|
|
|
|
.log("GET CHAIN HEIGHT ERROR ${e.toString()}", level: LogLevel.Error);
|
2023-08-14 14:36:27 +00:00
|
|
|
}
|
2023-06-21 11:29:28 +00:00
|
|
|
}
|
|
|
|
|
2023-06-12 19:03:32 +00:00
|
|
|
@override
|
|
|
|
Future<void> refresh() {
|
2023-06-21 11:29:28 +00:00
|
|
|
updateChainHeight();
|
2023-06-12 19:03:32 +00:00
|
|
|
updateBalance();
|
2023-06-21 11:29:28 +00:00
|
|
|
updateTransactions();
|
2023-06-12 19:03:32 +00:00
|
|
|
return Future.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get storedChainHeight => getCachedChainHeight();
|
|
|
|
|
|
|
|
@override
|
2023-07-28 16:32:50 +00:00
|
|
|
Future<bool> testNetworkConnection() async {
|
2023-06-21 11:29:28 +00:00
|
|
|
try {
|
2023-07-28 16:32:50 +00:00
|
|
|
await get(Uri.parse(
|
|
|
|
"${getCurrentNode().host}:${getCurrentNode().port}/chains/main/blocks/head/header/shell"));
|
2023-06-21 11:29:28 +00:00
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-06-12 19:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-07-28 16:32:50 +00:00
|
|
|
Future<List<Transaction>> get transactions =>
|
|
|
|
db.getTransactions(walletId).findAll();
|
2023-06-12 19:03:32 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> updateNode(bool shouldRefresh) async {
|
2023-07-28 16:32:50 +00:00
|
|
|
_xtzNode = NodeService(secureStorageInterface: _secureStore)
|
|
|
|
.getPrimaryNodeFor(coin: coin) ??
|
|
|
|
DefaultNodes.getNodeFor(coin);
|
2023-06-12 19:03:32 +00:00
|
|
|
|
|
|
|
if (shouldRefresh) {
|
|
|
|
await refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-08-16 12:46:21 +00:00
|
|
|
Future<void> updateSentCachedTxData(Map<String, dynamic> txData) async {
|
|
|
|
final transaction = Transaction(
|
|
|
|
walletId: walletId,
|
|
|
|
txid: txData["txid"] as String,
|
|
|
|
timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000,
|
|
|
|
type: TransactionType.outgoing,
|
|
|
|
subType: TransactionSubType.none,
|
|
|
|
// precision may be lost here hence the following amountString
|
|
|
|
amount: (txData["recipientAmt"] as Amount).raw.toInt(),
|
|
|
|
amountString: (txData["recipientAmt"] as Amount).toJsonString(),
|
|
|
|
fee: txData["fee"] as int,
|
|
|
|
height: null,
|
|
|
|
isCancelled: false,
|
|
|
|
isLelantus: false,
|
|
|
|
otherData: null,
|
|
|
|
slateId: null,
|
|
|
|
nonce: null,
|
|
|
|
inputs: [],
|
|
|
|
outputs: [],
|
|
|
|
numberOfMessages: null,
|
|
|
|
);
|
|
|
|
|
|
|
|
final address = txData["address"] is String
|
2023-08-23 17:08:32 +00:00
|
|
|
? await db.getAddress(walletId, txData["address"] as String)
|
|
|
|
: null;
|
2023-08-16 12:46:21 +00:00
|
|
|
|
|
|
|
await db.addNewTransactionData(
|
2023-08-23 17:08:32 +00:00
|
|
|
[
|
2023-08-16 12:46:21 +00:00
|
|
|
Tuple2(transaction, address),
|
2023-08-23 17:08:32 +00:00
|
|
|
],
|
|
|
|
walletId,
|
2023-08-16 12:46:21 +00:00
|
|
|
);
|
2023-06-12 19:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
// TODO: implement utxos
|
|
|
|
Future<List<UTXO>> get utxos => throw UnimplementedError();
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool validateAddress(String address) {
|
|
|
|
return RegExp(r"^tz[1-9A-HJ-NP-Za-km-z]{34}$").hasMatch(address);
|
|
|
|
}
|
|
|
|
}
|