stack_wallet/lib/wallets/wallet/impl/tezos_wallet.dart

534 lines
15 KiB
Dart
Raw Normal View History

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';
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
class TezosWallet extends Bip39Wallet {
TezosWallet(CryptoCurrencyNetwork network) : super(Tezos(network));
2023-11-20 19:55:22 +00:00
NodeModel? _xtzNode;
Future<tezart.Keystore> _getKeyStore() async {
final mnemonic = await getMnemonic();
final passphrase = await getMnemonicPassphrase();
return tezart.Keystore.fromMnemonic(mnemonic, password: passphrase);
}
Future<Address> _getAddressFromMnemonic() async {
final keyStore = await _getKeyStore();
return Address(
walletId: walletId,
value: keyStore.address,
publicKey: keyStore.publicKey.toUint8ListFromBase58CheckEncoded,
derivationIndex: 0,
derivationPath: null,
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-22 18:30:09 +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 {
final _address = await getCurrentReceivingAddress();
if (_address == null) {
final address = await _getAddressFromMnemonic();
await mainDB.updateOrPutAddresses([address]);
}
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");
}
final account = await TezosAPI.getAccount(
(await getCurrentReceivingAddress())!.value,
);
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,
)
],
// 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(
"Error in prepareSend() in tezos_wallet.dart: $e\n$s}",
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(
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(
" Error in _estimate in tezos_wallet.dart: $e\n$s}",
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,
);
}
final account = await TezosAPI.getAccount(
(await getCurrentReceivingAddress())!.value,
);
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-22 18:30:09 +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);
}
final address = await _getAddressFromMnemonic();
await mainDB.updateOrPutAddresses([address]);
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(
"Error getting balance in tezos_wallet.dart: $e\n$s}",
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"
" chain height for tezos: $e\n$s}",
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 {
// TODO: optimize updateTransactions
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,
type: AddressType.unknown,
subType: AddressSubType.unknown,
);
break;
}
transactions.add(Tuple2(transaction, theAddress));
}
await mainDB.addNewTransactionData(transactions, walletId);
2023-11-20 16:37:28 +00:00
}
@override
2023-11-20 19:55:22 +00:00
Future<void> updateUTXOs() async {
// do nothing. Not used in tezos
2023-11-20 16:37:28 +00:00
}
}