2023-11-20 16:37:28 +00:00
|
|
|
import 'package:isar/isar.dart';
|
2023-11-20 19:55:22 +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/node_model.dart';
|
2023-11-20 16:37:28 +00:00
|
|
|
import 'package:stackwallet/models/paymint/fee_object_model.dart';
|
2023-11-20 19:55:22 +00:00
|
|
|
import 'package:stackwallet/services/node_service.dart';
|
2023-11-20 16:37:28 +00:00
|
|
|
import 'package:stackwallet/utilities/amount/amount.dart';
|
2023-11-20 19:55:22 +00:00
|
|
|
import 'package:stackwallet/utilities/default_nodes.dart';
|
|
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
|
|
|
import 'package:stackwallet/utilities/extensions/impl/string.dart';
|
|
|
|
import 'package:stackwallet/utilities/logger.dart';
|
2023-11-22 18:30:09 +00:00
|
|
|
import 'package:stackwallet/wallets/api/tezos/tezos_account.dart';
|
2023-11-20 19:55:22 +00:00
|
|
|
import 'package:stackwallet/wallets/api/tezos/tezos_api.dart';
|
|
|
|
import 'package:stackwallet/wallets/api/tezos/tezos_rpc_api.dart';
|
2023-11-20 16:37:28 +00:00
|
|
|
import 'package:stackwallet/wallets/crypto_currency/coins/tezos.dart';
|
|
|
|
import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart';
|
2023-11-23 18:32:08 +00:00
|
|
|
import 'package:stackwallet/wallets/isar/models/wallet_info.dart';
|
2023-11-20 16:37:28 +00:00
|
|
|
import 'package:stackwallet/wallets/models/tx_data.dart';
|
|
|
|
import 'package:stackwallet/wallets/wallet/intermediate/bip39_wallet.dart';
|
2023-11-20 19:55:22 +00:00
|
|
|
import 'package:tezart/tezart.dart' as tezart;
|
|
|
|
import 'package:tuple/tuple.dart';
|
|
|
|
|
2023-11-21 19:44:21 +00:00
|
|
|
// const kDefaultTransactionStorageLimit = 496;
|
2023-11-22 18:30:09 +00:00
|
|
|
// const kDefaultTransactionGasLimit = 10600;
|
|
|
|
//
|
2023-11-21 19:44:21 +00:00
|
|
|
// const kDefaultKeyRevealFee = 1270;
|
|
|
|
// const kDefaultKeyRevealStorageLimit = 0;
|
|
|
|
// const kDefaultKeyRevealGasLimit = 1100;
|
2023-11-20 16:37:28 +00:00
|
|
|
|
2023-11-23 18:32:08 +00:00
|
|
|
class TezosWallet extends Bip39Wallet<Tezos> {
|
2023-11-20 16:37:28 +00:00
|
|
|
TezosWallet(CryptoCurrencyNetwork network) : super(Tezos(network));
|
|
|
|
|
2023-11-20 19:55:22 +00:00
|
|
|
NodeModel? _xtzNode;
|
|
|
|
|
2023-11-23 18:32:08 +00:00
|
|
|
String get derivationPath =>
|
|
|
|
info.otherData[WalletInfoKeys.tezosDerivationPath] as String? ?? "";
|
|
|
|
|
|
|
|
Future<DerivationPath> _scanPossiblePaths({
|
|
|
|
required String mnemonic,
|
|
|
|
String passphrase = "",
|
|
|
|
}) async {
|
|
|
|
try {
|
|
|
|
for (final path in Tezos.possibleDerivationPaths) {
|
|
|
|
final ks = await _getKeyStore(path: path.value);
|
|
|
|
|
|
|
|
// TODO: some kind of better check to see if the address has been used
|
|
|
|
|
|
|
|
final hasHistory =
|
|
|
|
(await TezosAPI.getTransactions(ks.address)).isNotEmpty;
|
|
|
|
|
|
|
|
if (hasHistory) {
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Tezos.standardDerivationPath;
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"Error in _scanPossiblePaths() in tezos_wallet.dart: $e\n$s",
|
|
|
|
level: LogLevel.Error,
|
|
|
|
);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<tezart.Keystore> _getKeyStore({String? path}) async {
|
2023-11-20 19:55:22 +00:00
|
|
|
final mnemonic = await getMnemonic();
|
|
|
|
final passphrase = await getMnemonicPassphrase();
|
2023-11-23 18:32:08 +00:00
|
|
|
|
|
|
|
return Tezos.mnemonicToKeyStore(
|
|
|
|
mnemonic: mnemonic,
|
|
|
|
mnemonicPassphrase: passphrase,
|
|
|
|
derivationPath: path ?? derivationPath,
|
|
|
|
);
|
2023-11-20 19:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Address> _getAddressFromMnemonic() async {
|
|
|
|
final keyStore = await _getKeyStore();
|
|
|
|
return Address(
|
|
|
|
walletId: walletId,
|
|
|
|
value: keyStore.address,
|
|
|
|
publicKey: keyStore.publicKey.toUint8ListFromBase58CheckEncoded,
|
|
|
|
derivationIndex: 0,
|
2023-11-23 18:32:08 +00:00
|
|
|
derivationPath: DerivationPath()..value = derivationPath,
|
2023-11-20 19:55:22 +00:00
|
|
|
type: info.coin.primaryAddressType,
|
|
|
|
subType: AddressSubType.receiving,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-21 19:44:21 +00:00
|
|
|
Future<tezart.OperationsList> _buildSendTransaction({
|
|
|
|
required Amount amount,
|
|
|
|
required String address,
|
2023-11-22 18:30:09 +00:00
|
|
|
required int counter,
|
|
|
|
// required bool reveal,
|
|
|
|
// int? customGasLimit,
|
|
|
|
// Amount? customFee,
|
|
|
|
// Amount? customRevealFee,
|
2023-11-21 19:44:21 +00:00
|
|
|
}) async {
|
|
|
|
try {
|
|
|
|
final sourceKeyStore = await _getKeyStore();
|
2023-11-22 18:30:09 +00:00
|
|
|
final server = (_xtzNode ?? getCurrentNode()).host;
|
|
|
|
// if (kDebugMode) {
|
|
|
|
// print("SERVER: $server");
|
|
|
|
// print("COUNTER: $counter");
|
|
|
|
// print("customFee: $customFee");
|
|
|
|
// }
|
2023-11-21 19:44:21 +00:00
|
|
|
final tezartClient = tezart.TezartClient(
|
2023-11-22 18:30:09 +00:00
|
|
|
server,
|
2023-11-21 19:44:21 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
final opList = await tezartClient.transferOperation(
|
|
|
|
source: sourceKeyStore,
|
|
|
|
destination: address,
|
|
|
|
amount: amount.raw.toInt(),
|
2023-11-22 18:30:09 +00:00
|
|
|
// customFee: customFee?.raw.toInt(),
|
|
|
|
// customGasLimit: customGasLimit,
|
|
|
|
// reveal: false,
|
2023-11-21 19:44:21 +00:00
|
|
|
);
|
|
|
|
|
2023-11-22 18:30:09 +00:00
|
|
|
// if (reveal) {
|
|
|
|
// opList.prependOperation(
|
|
|
|
// tezart.RevealOperation(
|
|
|
|
// customGasLimit: customGasLimit,
|
|
|
|
// customFee: customRevealFee?.raw.toInt(),
|
|
|
|
// ),
|
|
|
|
// );
|
|
|
|
// }
|
2023-11-21 19:44:21 +00:00
|
|
|
|
|
|
|
for (final op in opList.operations) {
|
2023-11-22 18:30:09 +00:00
|
|
|
op.counter = counter;
|
|
|
|
counter++;
|
2023-11-21 19:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return opList;
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
2023-11-23 18:32:08 +00:00
|
|
|
"Error in _buildSendTransaction() in tezos_wallet.dart: $e\n$s",
|
2023-11-21 19:44:21 +00:00
|
|
|
level: LogLevel.Error,
|
|
|
|
);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 19:55:22 +00:00
|
|
|
// ===========================================================================
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> init() async {
|
2024-01-11 23:26:21 +00:00
|
|
|
try {
|
|
|
|
final _address = await getCurrentReceivingAddress();
|
|
|
|
if (_address == null) {
|
|
|
|
final address = await _getAddressFromMnemonic();
|
|
|
|
await mainDB.updateOrPutAddresses([address]);
|
|
|
|
}
|
|
|
|
} catch (_) {
|
|
|
|
// do nothing, still allow user into wallet
|
2023-11-20 19:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await super.init();
|
|
|
|
}
|
|
|
|
|
2023-11-20 16:37:28 +00:00
|
|
|
@override
|
|
|
|
FilterOperation? get changeAddressFilterOperation =>
|
2023-11-20 19:55:22 +00:00
|
|
|
throw UnimplementedError("Not used for $runtimeType");
|
2023-11-20 16:37:28 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
FilterOperation? get receivingAddressFilterOperation =>
|
2023-11-20 19:55:22 +00:00
|
|
|
FilterGroup.and(standardReceivingAddressFilters);
|
2023-11-20 16:37:28 +00:00
|
|
|
|
|
|
|
@override
|
2023-11-20 19:55:22 +00:00
|
|
|
Future<TxData> prepareSend({required TxData txData}) async {
|
2023-11-22 18:30:09 +00:00
|
|
|
try {
|
|
|
|
if (txData.recipients == null || txData.recipients!.length != 1) {
|
|
|
|
throw Exception("$runtimeType prepareSend requires 1 recipient");
|
|
|
|
}
|
2023-11-20 19:55:22 +00:00
|
|
|
|
2023-11-22 18:30:09 +00:00
|
|
|
Amount sendAmount = txData.amount!;
|
2023-11-20 19:55:22 +00:00
|
|
|
|
2023-11-22 18:30:09 +00:00
|
|
|
if (sendAmount > info.cachedBalance.spendable) {
|
|
|
|
throw Exception("Insufficient available balance");
|
|
|
|
}
|
2023-11-23 18:32:08 +00:00
|
|
|
|
|
|
|
final myAddress = (await getCurrentReceivingAddress())!;
|
2023-11-22 18:30:09 +00:00
|
|
|
final account = await TezosAPI.getAccount(
|
2023-11-23 18:32:08 +00:00
|
|
|
myAddress.value,
|
2023-11-22 18:30:09 +00:00
|
|
|
);
|
2023-11-20 19:55:22 +00:00
|
|
|
|
2023-11-22 18:30:09 +00:00
|
|
|
// final bool isSendAll = sendAmount == info.cachedBalance.spendable;
|
|
|
|
//
|
|
|
|
// int? customGasLimit;
|
|
|
|
// Amount? fee;
|
|
|
|
// Amount? revealFee;
|
|
|
|
//
|
|
|
|
// if (isSendAll) {
|
|
|
|
// final fees = await _estimate(
|
|
|
|
// account,
|
|
|
|
// txData.recipients!.first.address,
|
|
|
|
// );
|
|
|
|
// //Fee guides for emptying a tz account
|
|
|
|
// // https://github.com/TezTech/eztz/blob/master/PROTO_004_FEES.md
|
|
|
|
// // customGasLimit = kDefaultTransactionGasLimit + 320;
|
|
|
|
// fee = Amount(
|
|
|
|
// rawValue: BigInt.from(fees.transfer + 32),
|
|
|
|
// fractionDigits: cryptoCurrency.fractionDigits,
|
|
|
|
// );
|
|
|
|
//
|
|
|
|
// BigInt rawAmount = sendAmount.raw - fee.raw;
|
|
|
|
//
|
|
|
|
// if (!account.revealed) {
|
|
|
|
// revealFee = Amount(
|
|
|
|
// rawValue: BigInt.from(fees.reveal + 32),
|
|
|
|
// fractionDigits: cryptoCurrency.fractionDigits,
|
|
|
|
// );
|
|
|
|
//
|
|
|
|
// rawAmount = rawAmount - revealFee.raw;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// sendAmount = Amount(
|
|
|
|
// rawValue: rawAmount,
|
|
|
|
// fractionDigits: cryptoCurrency.fractionDigits,
|
|
|
|
// );
|
|
|
|
// }
|
2023-11-20 19:55:22 +00:00
|
|
|
|
2023-11-22 18:30:09 +00:00
|
|
|
final opList = await _buildSendTransaction(
|
|
|
|
amount: sendAmount,
|
|
|
|
address: txData.recipients!.first.address,
|
|
|
|
counter: account.counter + 1,
|
|
|
|
// reveal: !account.revealed,
|
|
|
|
// customFee: isSendAll ? fee : null,
|
|
|
|
// customRevealFee: isSendAll ? revealFee : null,
|
|
|
|
// customGasLimit: customGasLimit,
|
|
|
|
);
|
2023-11-20 19:55:22 +00:00
|
|
|
|
2023-11-22 18:30:09 +00:00
|
|
|
await opList.computeLimits();
|
|
|
|
await opList.computeFees();
|
|
|
|
await opList.simulate();
|
2023-11-20 19:55:22 +00:00
|
|
|
|
2023-11-22 18:30:09 +00:00
|
|
|
return txData.copyWith(
|
|
|
|
recipients: [
|
|
|
|
(
|
|
|
|
amount: sendAmount,
|
|
|
|
address: txData.recipients!.first.address,
|
2024-01-05 19:45:42 +00:00
|
|
|
isChange: txData.recipients!.first.isChange,
|
2023-11-22 18:30:09 +00:00
|
|
|
)
|
|
|
|
],
|
|
|
|
// fee: fee,
|
|
|
|
fee: Amount(
|
|
|
|
rawValue: opList.operations
|
|
|
|
.map(
|
|
|
|
(e) => BigInt.from(e.fee),
|
|
|
|
)
|
|
|
|
.fold(
|
|
|
|
BigInt.zero,
|
|
|
|
(p, e) => p + e,
|
|
|
|
),
|
|
|
|
fractionDigits: cryptoCurrency.fractionDigits,
|
|
|
|
),
|
|
|
|
tezosOperationsList: opList,
|
|
|
|
);
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
2023-11-23 18:32:08 +00:00
|
|
|
"Error in prepareSend() in tezos_wallet.dart: $e\n$s",
|
2023-11-22 18:30:09 +00:00
|
|
|
level: LogLevel.Error,
|
2023-11-21 19:44:21 +00:00
|
|
|
);
|
2023-11-20 19:55:22 +00:00
|
|
|
|
2023-11-22 18:30:09 +00:00
|
|
|
if (e
|
|
|
|
.toString()
|
|
|
|
.contains("(_operationResult['errors']): Must not be null")) {
|
|
|
|
throw Exception("Probably insufficient balance");
|
|
|
|
} else if (e.toString().contains(
|
|
|
|
"The simulation of the operation: \"transaction\" failed with error(s) :"
|
|
|
|
" contract.balance_too_low, tez.subtraction_underflow.",
|
|
|
|
)) {
|
|
|
|
throw Exception("Insufficient balance to pay fees");
|
|
|
|
}
|
2023-11-21 19:44:21 +00:00
|
|
|
|
2023-11-22 18:30:09 +00:00
|
|
|
rethrow;
|
|
|
|
}
|
2023-11-20 19:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<TxData> confirmSend({required TxData txData}) async {
|
2023-11-22 18:30:09 +00:00
|
|
|
await txData.tezosOperationsList!.inject();
|
|
|
|
await txData.tezosOperationsList!.monitor();
|
2023-11-21 19:44:21 +00:00
|
|
|
return txData.copyWith(
|
|
|
|
txid: txData.tezosOperationsList!.result.id,
|
|
|
|
);
|
2023-11-20 16:37:28 +00:00
|
|
|
}
|
|
|
|
|
2023-11-22 18:30:09 +00:00
|
|
|
int _estCount = 0;
|
2023-11-21 19:44:21 +00:00
|
|
|
|
2023-11-22 18:30:09 +00:00
|
|
|
Future<({int reveal, int transfer})> _estimate(
|
2023-11-23 18:32:08 +00:00
|
|
|
TezosAccount account,
|
|
|
|
String recipientAddress,
|
|
|
|
) async {
|
2023-11-21 19:44:21 +00:00
|
|
|
try {
|
|
|
|
final opList = await _buildSendTransaction(
|
2023-11-22 18:30:09 +00:00
|
|
|
amount: Amount(
|
|
|
|
rawValue: BigInt.one,
|
|
|
|
fractionDigits: cryptoCurrency.fractionDigits,
|
|
|
|
),
|
|
|
|
address: recipientAddress,
|
|
|
|
counter: account.counter + 1,
|
|
|
|
// reveal: !account.revealed,
|
2023-11-21 19:44:21 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await opList.computeLimits();
|
|
|
|
await opList.computeFees();
|
|
|
|
await opList.simulate();
|
|
|
|
|
2023-11-22 18:30:09 +00:00
|
|
|
int reveal = 0;
|
|
|
|
int transfer = 0;
|
|
|
|
|
|
|
|
for (final op in opList.operations) {
|
|
|
|
if (op is tezart.TransactionOperation) {
|
|
|
|
transfer += op.fee;
|
|
|
|
} else if (op is tezart.RevealOperation) {
|
|
|
|
reveal += op.fee;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (reveal: reveal, transfer: transfer);
|
|
|
|
} catch (e, s) {
|
|
|
|
if (_estCount > 3) {
|
|
|
|
_estCount = 0;
|
|
|
|
Logging.instance.log(
|
2023-11-23 18:32:08 +00:00
|
|
|
" Error in _estimate in tezos_wallet.dart: $e\n$s",
|
2023-11-22 18:30:09 +00:00
|
|
|
level: LogLevel.Error,
|
|
|
|
);
|
|
|
|
rethrow;
|
|
|
|
} else {
|
|
|
|
_estCount++;
|
|
|
|
Logging.instance.log(
|
|
|
|
"_estimate() retry _estCount=$_estCount",
|
|
|
|
level: LogLevel.Warning,
|
|
|
|
);
|
|
|
|
return await _estimate(
|
|
|
|
account,
|
|
|
|
recipientAddress,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Amount> estimateFeeFor(
|
|
|
|
Amount amount,
|
|
|
|
int feeRate, {
|
|
|
|
String recipientAddress = "tz1MXvDCyXSqBqXPNDcsdmVZKfoxL9FTHmp2",
|
|
|
|
}) async {
|
|
|
|
if (info.cachedBalance.spendable.raw == BigInt.zero) {
|
|
|
|
return Amount(
|
|
|
|
rawValue: BigInt.zero,
|
|
|
|
fractionDigits: cryptoCurrency.fractionDigits,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-23 18:32:08 +00:00
|
|
|
final myAddress = (await getCurrentReceivingAddress())!;
|
2023-11-22 18:30:09 +00:00
|
|
|
final account = await TezosAPI.getAccount(
|
2023-11-23 18:32:08 +00:00
|
|
|
myAddress.value,
|
2023-11-22 18:30:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
final fees = await _estimate(account, recipientAddress);
|
|
|
|
|
2023-11-21 19:44:21 +00:00
|
|
|
final fee = Amount(
|
2023-11-22 18:30:09 +00:00
|
|
|
rawValue: BigInt.from(fees.reveal + fees.transfer),
|
2023-11-21 19:44:21 +00:00
|
|
|
fractionDigits: cryptoCurrency.fractionDigits,
|
|
|
|
);
|
|
|
|
|
|
|
|
return fee;
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
2023-11-23 18:32:08 +00:00
|
|
|
" Error in estimateFeeFor() in tezos_wallet.dart: $e\n$s",
|
2023-11-21 19:44:21 +00:00
|
|
|
level: LogLevel.Error,
|
|
|
|
);
|
|
|
|
rethrow;
|
|
|
|
}
|
2023-11-20 16:37:28 +00:00
|
|
|
}
|
|
|
|
|
2023-11-21 19:44:21 +00:00
|
|
|
/// Not really used (yet)
|
2023-11-20 16:37:28 +00:00
|
|
|
@override
|
2023-11-20 19:55:22 +00:00
|
|
|
Future<FeeObject> get fees async {
|
2023-11-21 19:44:21 +00:00
|
|
|
const feePerTx = 1;
|
2023-11-20 19:55:22 +00:00
|
|
|
return FeeObject(
|
|
|
|
numberOfBlocksFast: 10,
|
|
|
|
numberOfBlocksAverage: 10,
|
|
|
|
numberOfBlocksSlow: 10,
|
|
|
|
fast: feePerTx,
|
|
|
|
medium: feePerTx,
|
|
|
|
slow: feePerTx,
|
|
|
|
);
|
|
|
|
}
|
2023-11-20 16:37:28 +00:00
|
|
|
|
|
|
|
@override
|
2023-11-20 19:55:22 +00:00
|
|
|
Future<bool> pingCheck() async {
|
|
|
|
final currentNode = getCurrentNode();
|
|
|
|
return await TezosRpcAPI.testNetworkConnection(
|
|
|
|
nodeInfo: (
|
|
|
|
host: currentNode.host,
|
|
|
|
port: currentNode.port,
|
|
|
|
),
|
|
|
|
);
|
2023-11-20 16:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-11-20 19:55:22 +00:00
|
|
|
Future<void> recover({required bool isRescan}) async {
|
|
|
|
await refreshMutex.protect(() async {
|
|
|
|
if (isRescan) {
|
|
|
|
await mainDB.deleteWalletBlockchainData(walletId);
|
2023-11-23 18:32:08 +00:00
|
|
|
} else {
|
|
|
|
final derivationPath = await _scanPossiblePaths(
|
|
|
|
mnemonic: await getMnemonic(),
|
|
|
|
passphrase: await getMnemonicPassphrase(),
|
|
|
|
);
|
|
|
|
|
|
|
|
await info.updateOtherData(
|
|
|
|
newEntries: {
|
|
|
|
WalletInfoKeys.tezosDerivationPath: derivationPath.value,
|
|
|
|
},
|
|
|
|
isar: mainDB.isar,
|
|
|
|
);
|
2023-11-20 19:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final address = await _getAddressFromMnemonic();
|
|
|
|
|
|
|
|
await mainDB.updateOrPutAddresses([address]);
|
|
|
|
|
2023-11-23 18:32:08 +00:00
|
|
|
// ensure we only have a single address
|
|
|
|
await mainDB.isar.writeTxn(() async {
|
|
|
|
await mainDB.isar.addresses
|
|
|
|
.where()
|
|
|
|
.walletIdEqualTo(walletId)
|
|
|
|
.filter()
|
|
|
|
.not()
|
|
|
|
.derivationPath((q) => q.valueEqualTo(derivationPath))
|
|
|
|
.deleteAll();
|
|
|
|
});
|
|
|
|
|
|
|
|
if (info.cachedReceivingAddress != address.value) {
|
|
|
|
await info.updateReceivingAddress(
|
|
|
|
newAddress: address.value,
|
|
|
|
isar: mainDB.isar,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-20 19:55:22 +00:00
|
|
|
await Future.wait([
|
|
|
|
updateBalance(),
|
|
|
|
updateTransactions(),
|
|
|
|
updateChainHeight(),
|
|
|
|
]);
|
|
|
|
});
|
2023-11-20 16:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-11-20 19:55:22 +00:00
|
|
|
Future<void> updateBalance() async {
|
|
|
|
try {
|
|
|
|
final currentNode = _xtzNode ?? getCurrentNode();
|
|
|
|
final balance = await TezosRpcAPI.getBalance(
|
|
|
|
nodeInfo: (host: currentNode.host, port: currentNode.port),
|
|
|
|
address: (await getCurrentReceivingAddress())!.value,
|
|
|
|
);
|
|
|
|
|
|
|
|
final balanceInAmount = Amount(
|
|
|
|
rawValue: balance!,
|
|
|
|
fractionDigits: cryptoCurrency.fractionDigits,
|
|
|
|
);
|
|
|
|
final newBalance = Balance(
|
|
|
|
total: balanceInAmount,
|
|
|
|
spendable: balanceInAmount,
|
|
|
|
blockedTotal: Amount(
|
|
|
|
rawValue: BigInt.zero,
|
|
|
|
fractionDigits: cryptoCurrency.fractionDigits,
|
|
|
|
),
|
|
|
|
pendingSpendable: Amount(
|
|
|
|
rawValue: BigInt.zero,
|
|
|
|
fractionDigits: cryptoCurrency.fractionDigits,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
await info.updateBalance(newBalance: newBalance, isar: mainDB.isar);
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
2023-11-23 18:32:08 +00:00
|
|
|
"Error getting balance in tezos_wallet.dart: $e\n$s",
|
2023-11-20 19:55:22 +00:00
|
|
|
level: LogLevel.Error,
|
|
|
|
);
|
|
|
|
}
|
2023-11-20 16:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-11-20 19:55:22 +00:00
|
|
|
Future<void> updateChainHeight() async {
|
|
|
|
try {
|
|
|
|
final currentNode = _xtzNode ?? getCurrentNode();
|
|
|
|
final height = await TezosRpcAPI.getChainHeight(
|
|
|
|
nodeInfo: (
|
|
|
|
host: currentNode.host,
|
|
|
|
port: currentNode.port,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
await info.updateCachedChainHeight(
|
|
|
|
newHeight: height!,
|
|
|
|
isar: mainDB.isar,
|
|
|
|
);
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"Error occurred in tezos_wallet.dart while getting"
|
2023-11-23 18:32:08 +00:00
|
|
|
" chain height for tezos: $e\n$s",
|
2023-11-20 19:55:22 +00:00
|
|
|
level: LogLevel.Error,
|
|
|
|
);
|
|
|
|
}
|
2023-11-20 16:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-11-20 19:55:22 +00:00
|
|
|
Future<void> updateNode() async {
|
|
|
|
_xtzNode = NodeService(secureStorageInterface: secureStorageInterface)
|
|
|
|
.getPrimaryNodeFor(coin: info.coin) ??
|
|
|
|
DefaultNodes.getNodeFor(info.coin);
|
|
|
|
|
|
|
|
await refresh();
|
2023-11-20 16:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-11-20 19:55:22 +00:00
|
|
|
NodeModel getCurrentNode() {
|
|
|
|
return _xtzNode ??
|
|
|
|
NodeService(secureStorageInterface: secureStorageInterface)
|
|
|
|
.getPrimaryNodeFor(coin: info.coin) ??
|
|
|
|
DefaultNodes.getNodeFor(info.coin);
|
2023-11-20 16:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-11-20 19:55:22 +00:00
|
|
|
Future<void> updateTransactions() async {
|
2024-01-11 23:26:21 +00:00
|
|
|
// TODO: optimize updateTransactions and use V2
|
2023-11-20 19:55:22 +00:00
|
|
|
|
|
|
|
final myAddress = (await getCurrentReceivingAddress())!;
|
2023-11-21 19:44:21 +00:00
|
|
|
final txs = await TezosAPI.getTransactions(myAddress.value);
|
|
|
|
|
|
|
|
if (txs.isEmpty) {
|
2023-11-20 19:55:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Tuple2<Transaction, Address>> transactions = [];
|
|
|
|
for (final theTx in txs) {
|
|
|
|
final TransactionType txType;
|
|
|
|
|
|
|
|
if (myAddress.value == theTx.senderAddress) {
|
|
|
|
txType = TransactionType.outgoing;
|
|
|
|
} else if (myAddress.value == theTx.receiverAddress) {
|
|
|
|
if (myAddress.value == theTx.senderAddress) {
|
|
|
|
txType = TransactionType.sentToSelf;
|
|
|
|
} else {
|
|
|
|
txType = TransactionType.incoming;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
txType = TransactionType.unknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
var transaction = Transaction(
|
|
|
|
walletId: walletId,
|
|
|
|
txid: theTx.hash,
|
|
|
|
timestamp: theTx.timestamp,
|
|
|
|
type: txType,
|
|
|
|
subType: TransactionSubType.none,
|
|
|
|
amount: theTx.amountInMicroTez,
|
|
|
|
amountString: Amount(
|
2023-11-21 19:44:21 +00:00
|
|
|
rawValue: BigInt.from(theTx.amountInMicroTez),
|
2023-11-20 19:55:22 +00:00
|
|
|
fractionDigits: cryptoCurrency.fractionDigits,
|
|
|
|
).toJsonString(),
|
|
|
|
fee: theTx.feeInMicroTez,
|
|
|
|
height: theTx.height,
|
|
|
|
isCancelled: false,
|
|
|
|
isLelantus: false,
|
|
|
|
slateId: "",
|
|
|
|
otherData: "",
|
|
|
|
inputs: [],
|
|
|
|
outputs: [],
|
|
|
|
nonce: 0,
|
|
|
|
numberOfMessages: null,
|
|
|
|
);
|
|
|
|
|
|
|
|
final Address theAddress;
|
|
|
|
switch (txType) {
|
|
|
|
case TransactionType.incoming:
|
|
|
|
case TransactionType.sentToSelf:
|
|
|
|
theAddress = myAddress;
|
|
|
|
break;
|
|
|
|
case TransactionType.outgoing:
|
|
|
|
case TransactionType.unknown:
|
|
|
|
theAddress = Address(
|
|
|
|
walletId: walletId,
|
|
|
|
value: theTx.receiverAddress,
|
|
|
|
publicKey: [],
|
|
|
|
derivationIndex: 0,
|
|
|
|
derivationPath: null,
|
2024-01-11 23:26:21 +00:00
|
|
|
type: AddressType.tezos,
|
2023-11-20 19:55:22 +00:00
|
|
|
subType: AddressSubType.unknown,
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
transactions.add(Tuple2(transaction, theAddress));
|
|
|
|
}
|
|
|
|
await mainDB.addNewTransactionData(transactions, walletId);
|
2023-11-20 16:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2024-01-05 00:37:46 +00:00
|
|
|
Future<bool> updateUTXOs() async {
|
2023-11-20 19:55:22 +00:00
|
|
|
// do nothing. Not used in tezos
|
2024-01-05 00:37:46 +00:00
|
|
|
return false;
|
2023-11-20 16:37:28 +00:00
|
|
|
}
|
|
|
|
}
|