working tezos refactor

This commit is contained in:
julian 2023-11-21 13:44:21 -06:00
parent de43baaaa0
commit f524bc1d87
5 changed files with 162 additions and 846 deletions
lib
services/coins/tezos
wallets
pubspec.lockpubspec.yaml

View file

@ -1,732 +0,0 @@
// import 'dart:async';
//
// import 'package:decimal/decimal.dart';
// import 'package:isar/isar.dart';
// import 'package:stackwallet/db/isar/main_db.dart';
// 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';
// import 'package:stackwallet/models/paymint/fee_object_model.dart';
// import 'package:stackwallet/services/coins/coin_service.dart';
// import 'package:stackwallet/services/event_bus/events/global/node_connection_status_changed_event.dart';
// import 'package:stackwallet/services/event_bus/events/global/updated_in_background_event.dart';
// import 'package:stackwallet/services/event_bus/events/global/wallet_sync_status_changed_event.dart';
// import 'package:stackwallet/services/event_bus/global_event_bus.dart';
// import 'package:stackwallet/services/mixins/wallet_cache.dart';
// import 'package:stackwallet/services/mixins/wallet_db.dart';
// import 'package:stackwallet/services/node_service.dart';
// import 'package:stackwallet/services/transaction_notification_tracker.dart';
// import 'package:stackwallet/utilities/amount/amount.dart';
// import 'package:stackwallet/utilities/constants.dart';
// import 'package:stackwallet/utilities/default_nodes.dart';
// import 'package:stackwallet/utilities/enums/coin_enum.dart';
// import 'package:stackwallet/utilities/enums/fee_rate_type_enum.dart';
// import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
// import 'package:stackwallet/utilities/logger.dart';
// import 'package:stackwallet/utilities/prefs.dart';
// import 'package:stackwallet/wallets/api/tezos/tezos_api.dart';
// import 'package:stackwallet/wallets/api/tezos/tezos_rpc_api.dart';
// import 'package:stackwallet/wallets/api/tezos/tezos_transaction.dart';
// import 'package:tezart/tezart.dart';
// import 'package:tuple/tuple.dart';
//
// const int MINIMUM_CONFIRMATIONS = 1;
// const int _gasLimit = 10200;
//
// 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() {
// // return _xtzNode ??
// // NodeService(secureStorageInterface: _secureStore)
// // .getPrimaryNodeFor(coin: Coin.tezos) ??
// // DefaultNodes.getNodeFor(Coin.tezos);
// // }
// //
// // Future<Keystore> getKeystore() async {
// // return Keystore.fromMnemonic((await mnemonicString).toString());
// // }
// //
// // @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;
// // Timer? _networkAliveTimer;
// //
// // @override
// // bool get shouldAutoSync => _shouldAutoSync;
// //
// // @override
// // set shouldAutoSync(bool shouldAutoSync) {
// // if (_shouldAutoSync != shouldAutoSync) {
// // _shouldAutoSync = shouldAutoSync;
// // if (!shouldAutoSync) {
// // timer?.cancel();
// // timer = null;
// // stopNetworkAlivePinging();
// // } else {
// // startNetworkAlivePinging();
// // refresh();
// // }
// // }
// // }
// //
// // 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());
// // }
// // }
// // }
// //
// // @override
// // Balance get balance => _balance ??= getCachedBalance();
// // Balance? _balance;
//
// @override
// Future<Map<String, dynamic>> prepareSend(
// {required String address,
// required Amount amount,
// Map<String, dynamic>? args}) async {
// try {
// if (amount.decimals != coin.decimals) {
// throw Exception("Amount decimals do not match coin decimals!");
// }
// var fee = int.parse((await estimateFeeFor(
// amount, (args!["feeRate"] as FeeRateType).index))
// .raw
// .toString());
// 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 {
// final amount = txData["recipientAmt"] as Amount;
// final amountInMicroTez = amount.decimal * Decimal.fromInt(1000000);
// final microtezToInt = int.parse(amountInMicroTez.toString());
//
// final int feeInMicroTez = int.parse(txData["fee"].toString());
// final String destinationAddress = txData["address"] as String;
// final secretKey =
// Keystore.fromMnemonic((await mnemonicString)!).secretKey;
//
// Logging.instance.log(secretKey, level: LogLevel.Info);
// final sourceKeyStore = Keystore.fromSecretKey(secretKey);
// final client = TezartClient(getCurrentNode().host);
//
// int? sendAmount = microtezToInt;
// int gasLimit = _gasLimit;
// int thisFee = feeInMicroTez;
//
// if (balance.spendable == txData["recipientAmt"] as Amount) {
// //Fee guides for emptying a tz account
// // https://github.com/TezTech/eztz/blob/master/PROTO_004_FEES.md
// thisFee = thisFee + 32;
// sendAmount = microtezToInt - thisFee;
// gasLimit = _gasLimit + 320;
// }
//
// final operation = await client.transferOperation(
// source: sourceKeyStore,
// destination: destinationAddress,
// amount: sendAmount,
// customFee: feeInMicroTez,
// customGasLimit: gasLimit);
// await operation.executeAndMonitor();
// return operation.result.id as String;
// } catch (e) {
// Logging.instance.log(e.toString(), level: LogLevel.Error);
// return Future.error(e);
// }
// }
//
// @override
// Future<String> get currentReceivingAddress async {
// var mneString = await mnemonicString;
// if (mneString == null) {
// throw Exception("No mnemonic found!");
// }
// return Future.value((Keystore.fromMnemonic(mneString)).address);
// }
//
// @override
// Future<Amount> estimateFeeFor(Amount amount, int feeRate) async {
// int? feePerTx = await TezosAPI.getFeeEstimationFromLastDays(1);
// feePerTx ??= 0;
// return Amount(
// rawValue: BigInt.from(feePerTx),
// fractionDigits: coin.decimals,
// );
// }
//
// // @override
// // Future<void> exit() {
// // _hasCalledExit = true;
// // return Future.value();
// // }
//
// @override
// Future<FeeObject> get fees async {
// int? feePerTx = await TezosAPI.getFeeEstimationFromLastDays(1);
// feePerTx ??= 0;
// Logging.instance.log("feePerTx:$feePerTx", level: LogLevel.Info);
// return FeeObject(
// numberOfBlocksFast: 10,
// numberOfBlocksAverage: 10,
// numberOfBlocksSlow: 10,
// fast: feePerTx,
// medium: feePerTx,
// slow: feePerTx,
// );
// }
//
// // @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
// // Future<void> initializeNew(
// // ({String mnemonicPassphrase, int wordCount})? data,
// // ) async {
// // if ((await mnemonicString) != null || (await mnemonicPassphrase) != null) {
// // throw Exception(
// // "Attempted to overwrite mnemonic on generate new wallet!");
// // }
// //
// // await _prefs.init();
// //
// // var newKeystore = Keystore.random();
// // await _secureStore.write(
// // key: '${_walletId}_mnemonic',
// // value: newKeystore.mnemonic,
// // );
// // await _secureStore.write(
// // key: '${_walletId}_mnemonicPassphrase',
// // value: "",
// // );
// //
// // final address = Address(
// // walletId: walletId,
// // value: newKeystore.address,
// // publicKey: [],
// // derivationIndex: 0,
// // derivationPath: null,
// // type: AddressType.unknown,
// // subType: AddressSubType.receiving,
// // );
// //
// // 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
// // Future<String?> get mnemonicPassphrase =>
// // _secureStore.read(key: '${_walletId}_mnemonicPassphrase');
// //
// // @override
// // Future<String?> get mnemonicString =>
// // _secureStore.read(key: '${_walletId}_mnemonic');
// //
// // Future<void> _recoverWalletFromSeedPhrase({
// // required String mnemonic,
// // required String mnemonicPassphrase,
// // bool isRescan = false,
// // }) async {
// // final keystore = Keystore.fromMnemonic(
// // mnemonic,
// // password: mnemonicPassphrase,
// // );
// //
// // final address = Address(
// // walletId: walletId,
// // value: keystore.address,
// // publicKey: [],
// // derivationIndex: 0,
// // derivationPath: null,
// // type: AddressType.unknown,
// // subType: AddressSubType.receiving,
// // );
// //
// // if (isRescan) {
// // await db.updateOrPutAddresses([address]);
// // } else {
// // await db.putAddress(address);
// // }
// // }
// //
// // bool longMutex = false;
// // @override
// // Future<void> fullRescan(
// // int maxUnusedAddressGap,
// // int maxNumberOfIndexesToCheck,
// // ) async {
// // try {
// // Logging.instance.log("Starting full rescan!", level: LogLevel.Info);
// // longMutex = true;
// // GlobalEventBus.instance.fire(
// // WalletSyncStatusChangedEvent(
// // WalletSyncStatus.syncing,
// // walletId,
// // coin,
// // ),
// // );
// //
// // final _mnemonic = await mnemonicString;
// // final _mnemonicPassphrase = await mnemonicPassphrase;
// //
// // await db.deleteWalletBlockchainData(walletId);
// //
// // await _recoverWalletFromSeedPhrase(
// // mnemonic: _mnemonic!,
// // mnemonicPassphrase: _mnemonicPassphrase!,
// // isRescan: true,
// // );
// //
// // await refresh();
// // Logging.instance.log("Full rescan complete!", level: LogLevel.Info);
// // GlobalEventBus.instance.fire(
// // WalletSyncStatusChangedEvent(
// // WalletSyncStatus.synced,
// // walletId,
// // coin,
// // ),
// // );
// // } catch (e, s) {
// // GlobalEventBus.instance.fire(
// // WalletSyncStatusChangedEvent(
// // WalletSyncStatus.unableToSync,
// // walletId,
// // coin,
// // ),
// // );
// //
// // Logging.instance.log(
// // "Exception rethrown from fullRescan(): $e\n$s",
// // level: LogLevel.Error,
// // );
// // rethrow;
// // } finally {
// // longMutex = false;
// // }
// // }
// //
// // @override
// // Future<void> recoverFromMnemonic({
// // required String mnemonic,
// // String? mnemonicPassphrase,
// // required int maxUnusedAddressGap,
// // required int maxNumberOfIndexesToCheck,
// // required int height,
// // }) async {
// // longMutex = true;
// // try {
// // 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 ?? "",
// // );
// //
// // await _recoverWalletFromSeedPhrase(
// // mnemonic: mnemonic,
// // mnemonicPassphrase: mnemonicPassphrase ?? "",
// // isRescan: false,
// // );
// //
// // await Future.wait([
// // updateCachedId(walletId),
// // updateCachedIsFavorite(false),
// // ]);
// //
// // await refresh();
// // } catch (e, s) {
// // Logging.instance.log(
// // "Exception rethrown from recoverFromMnemonic(): $e\n$s",
// // level: LogLevel.Error);
// //
// // rethrow;
// // } finally {
// // longMutex = false;
// // }
// // }
// //
// // Future<void> updateBalance() async {
// // try {
// // NodeModel currentNode = getCurrentNode();
// // BigInt? balance = await TezosRpcAPI.getBalance(
// // nodeInfo: (host: currentNode.host, port: currentNode.port),
// // address: await currentReceivingAddress);
// // if (balance == null) {
// // return;
// // }
// // Logging.instance.log(
// // "Balance for ${await currentReceivingAddress}: $balance",
// // level: LogLevel.Info);
// // Amount balanceInAmount =
// // Amount(rawValue: balance, fractionDigits: coin.decimals);
// // _balance = Balance(
// // total: balanceInAmount,
// // spendable: balanceInAmount,
// // blockedTotal:
// // Amount(rawValue: BigInt.parse("0"), fractionDigits: coin.decimals),
// // pendingSpendable:
// // Amount(rawValue: BigInt.parse("0"), fractionDigits: coin.decimals),
// // );
// // await updateCachedBalance(_balance!);
// // } catch (e, s) {
// // Logging.instance.log(
// // "Error getting balance in tezos_wallet.dart: ${e.toString()}",
// // level: LogLevel.Error);
// // }
// // }
// //
// // Future<void> updateTransactions() async {
// // List<TezosTransaction>? txs =
// // await TezosAPI.getTransactions(await currentReceivingAddress);
// // Logging.instance.log("Transactions: $txs", level: LogLevel.Info);
// // if (txs == null) {
// // return;
// // } else if (txs.isEmpty) {
// // return;
// // }
// // List<Tuple2<Transaction, Address>> transactions = [];
// // for (var theTx in txs) {
// // var txType = TransactionType.unknown;
// // var selfAddress = await currentReceivingAddress;
// // if (selfAddress == theTx.senderAddress) {
// // txType = TransactionType.outgoing;
// // } else if (selfAddress == theTx.receiverAddress) {
// // txType = TransactionType.incoming;
// // } else if (selfAddress == theTx.receiverAddress &&
// // selfAddress == theTx.senderAddress) {
// // txType = TransactionType.sentToSelf;
// // }
// // var transaction = Transaction(
// // walletId: walletId,
// // txid: theTx.hash,
// // timestamp: theTx.timestamp,
// // type: txType,
// // subType: TransactionSubType.none,
// // amount: theTx.amountInMicroTez,
// // amountString: Amount(
// // rawValue: BigInt.parse(theTx.amountInMicroTez.toString()),
// // fractionDigits: coin.decimals,
// // ).toJsonString(),
// // fee: theTx.feeInMicroTez,
// // height: theTx.height,
// // isCancelled: false,
// // isLelantus: false,
// // slateId: "",
// // otherData: "",
// // inputs: [],
// // outputs: [],
// // nonce: 0,
// // numberOfMessages: null,
// // );
// // final AddressSubType subType;
// // switch (txType) {
// // case TransactionType.incoming:
// // case TransactionType.sentToSelf:
// // subType = AddressSubType.receiving;
// // break;
// // case TransactionType.outgoing:
// // case TransactionType.unknown:
// // subType = AddressSubType.unknown;
// // break;
// // }
// // final theAddress = Address(
// // walletId: walletId,
// // value: theTx.receiverAddress,
// // publicKey: [],
// // derivationIndex: 0,
// // derivationPath: null,
// // type: AddressType.unknown,
// // subType: subType,
// // );
// // transactions.add(Tuple2(transaction, theAddress));
// // }
// // await db.addNewTransactionData(transactions, walletId);
// // }
// //
// // Future<void> updateChainHeight() async {
// // try {
// // NodeModel currentNode = getCurrentNode();
// // int? intHeight = await TezosRpcAPI.getChainHeight(
// // nodeInfo: (host: currentNode.host, port: currentNode.port));
// // if (intHeight == null) {
// // return;
// // }
// // Logging.instance
// // .log("Chain height for tezos: $intHeight", level: LogLevel.Info);
// // await updateCachedChainHeight(intHeight);
// // } catch (e, s) {
// // Logging.instance.log(
// // "Error occured in tezos_wallet.dart while getting chain height for tezos: ${e.toString()}",
// // level: LogLevel.Error);
// // }
// // }
// //
// // @override
// // Future<void> refresh() async {
// // if (refreshMutex) {
// // Logging.instance.log(
// // "$walletId $walletName refreshMutex denied",
// // level: LogLevel.Info,
// // );
// // return;
// // } else {
// // refreshMutex = true;
// // }
// //
// // try {
// // GlobalEventBus.instance.fire(
// // WalletSyncStatusChangedEvent(
// // WalletSyncStatus.syncing,
// // walletId,
// // coin,
// // ),
// // );
// //
// // await updateChainHeight();
// // await updateBalance();
// // await updateTransactions();
// // GlobalEventBus.instance.fire(
// // WalletSyncStatusChangedEvent(
// // WalletSyncStatus.synced,
// // walletId,
// // coin,
// // ),
// // );
// //
// // if (shouldAutoSync) {
// // timer ??= Timer.periodic(const Duration(seconds: 30), (timer) async {
// // Logging.instance.log(
// // "Periodic refresh check for $walletId $walletName in object instance: $hashCode",
// // level: LogLevel.Info);
// //
// // await refresh();
// // GlobalEventBus.instance.fire(
// // UpdatedInBackgroundEvent(
// // "New data found in $walletId $walletName in background!",
// // walletId,
// // ),
// // );
// // });
// // }
// // } catch (e, s) {
// // Logging.instance.log(
// // "Failed to refresh tezos wallet $walletId: '$walletName': $e\n$s",
// // level: LogLevel.Warning,
// // );
// // GlobalEventBus.instance.fire(
// // WalletSyncStatusChangedEvent(
// // WalletSyncStatus.unableToSync,
// // walletId,
// // coin,
// // ),
// // );
// // }
// //
// // refreshMutex = false;
// // }
// //
// // @override
// // int get storedChainHeight => getCachedChainHeight();
//
// // @override
// // Future<bool> testNetworkConnection() async {
// // NodeModel currentNode = getCurrentNode();
// // return await TezosRpcAPI.testNetworkConnection(
// // nodeInfo: (host: currentNode.host, port: currentNode.port));
// // }
//
// // @override
// // Future<List<Transaction>> get transactions =>
// // db.getTransactions(walletId).findAll();
//
// // @override
// // Future<void> updateNode(bool shouldRefresh) async {
// // _xtzNode = NodeService(secureStorageInterface: _secureStore)
// // .getPrimaryNodeFor(coin: coin) ??
// // DefaultNodes.getNodeFor(coin);
// //
// // if (shouldRefresh) {
// // await refresh();
// // }
// // }
//
// @override
// 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
// ? await db.getAddress(walletId, txData["address"] as String)
// : null;
//
// await db.addNewTransactionData(
// [
// Tuple2(transaction, address),
// ],
// walletId,
// );
// }
//
// // @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);
// // }
// }

View file

@ -2,7 +2,6 @@ import 'dart:convert';
import 'package:stackwallet/networking/http.dart';
import 'package:stackwallet/services/tor_service.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/prefs.dart';
import 'package:stackwallet/wallets/api/tezos/tezos_transaction.dart';
@ -11,7 +10,29 @@ abstract final class TezosAPI {
static final HTTP _client = HTTP();
static const String _baseURL = 'https://api.tzkt.io';
static Future<List<TezosTransaction>?> getTransactions(String address) async {
static Future<int> getCounter(String address) async {
try {
final uriString = "$_baseURL/v1/accounts/$address/counter";
final response = await _client.get(
url: Uri.parse(uriString),
headers: {'Content-Type': 'application/json'},
proxyInfo: Prefs.instance.useTor
? TorService.sharedInstance.getProxyInfo()
: null,
);
final result = jsonDecode(response.body);
return result as int;
} catch (e, s) {
Logging.instance.log(
"Error occurred in TezosAPI while getting counter for $address: $e\n$s",
level: LogLevel.Error,
);
rethrow;
}
}
static Future<List<TezosTransaction>> getTransactions(String address) async {
try {
final transactionsCall =
"$_baseURL/v1/accounts/$address/operations?type=transaction";
@ -60,36 +81,10 @@ abstract final class TezosAPI {
return txs;
} catch (e, s) {
Logging.instance.log(
"Error occurred in tezos_api.dart while getting transactions for $address: $e\n$s",
"Error occurred in TezosAPI while getting transactions for $address: $e\n$s",
level: LogLevel.Error,
);
rethrow;
}
return null;
}
static Future<int?> getFeeEstimationFromLastDays(int days) async {
try {
var api = "$_baseURL/series/op?start_date=today&collapse=$days";
final response = await _client.get(
url: Uri.parse(api),
headers: {'Content-Type': 'application/json'},
proxyInfo: Prefs.instance.useTor
? TorService.sharedInstance.getProxyInfo()
: null,
);
final result = jsonDecode(response.body);
double totalFees = result[0][4] as double;
int totalTxs = result[0][8] as int;
return ((totalFees / totalTxs * Coin.tezos.decimals).floor());
} catch (e) {
Logging.instance.log(
"Error occurred in tezos_api.dart while getting fee estimation for tezos: $e",
level: LogLevel.Error,
);
}
return null;
}
}

View file

@ -12,7 +12,6 @@ import 'package:stackwallet/utilities/extensions/impl/string.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/wallets/api/tezos/tezos_api.dart';
import 'package:stackwallet/wallets/api/tezos/tezos_rpc_api.dart';
import 'package:stackwallet/wallets/api/tezos/tezos_transaction.dart';
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';
@ -20,7 +19,12 @@ import 'package:stackwallet/wallets/wallet/intermediate/bip39_wallet.dart';
import 'package:tezart/tezart.dart' as tezart;
import 'package:tuple/tuple.dart';
const int GAS_LIMIT = 10200;
// const kDefaultTransactionStorageLimit = 496;
const kDefaultTransactionGasLimit = 10600;
// const kDefaultKeyRevealFee = 1270;
// const kDefaultKeyRevealStorageLimit = 0;
// const kDefaultKeyRevealGasLimit = 1100;
class TezosWallet extends Bip39Wallet {
TezosWallet(CryptoCurrencyNetwork network) : super(Tezos(network));
@ -46,6 +50,54 @@ class TezosWallet extends Bip39Wallet {
);
}
Future<tezart.OperationsList> _buildSendTransaction({
required Amount amount,
required String address,
int? customGasLimit,
Amount? customFee,
}) async {
try {
final sourceKeyStore = await _getKeyStore();
final tezartClient = tezart.TezartClient(
(_xtzNode ?? getCurrentNode()).host,
);
final opList = await tezartClient.transferOperation(
source: sourceKeyStore,
destination: address,
amount: amount.raw.toInt(),
customGasLimit: customGasLimit,
customFee: customFee?.raw.toInt(),
);
final counter = (await TezosAPI.getCounter(
(await getCurrentReceivingAddress())!.value,
)) +
1;
for (final op in opList.operations) {
if (op is tezart.RevealOperation) {
// op.storageLimit = kDefaultKeyRevealStorageLimit;
// op.gasLimit = kDefaultKeyRevealGasLimit;
// op.fee = kDefaultKeyRevealFee;
op.counter = counter;
} else if (op is tezart.TransactionOperation) {
op.counter = counter + 1;
// op.storageLimit = kDefaultTransactionStorageLimit;
// op.gasLimit = kDefaultTransactionGasLimit;
}
}
return opList;
} catch (e, s) {
Logging.instance.log(
"Error in estimateFeeFor() in tezos_wallet.dart: $e\n$s}",
level: LogLevel.Error,
);
rethrow;
}
}
// ===========================================================================
@override
@ -82,35 +134,28 @@ class TezosWallet extends Bip39Wallet {
final bool isSendAll = sendAmount == info.cachedBalance.spendable;
final sourceKeyStore = await _getKeyStore();
final tezartClient = tezart.TezartClient(
(_xtzNode ?? getCurrentNode()).host,
);
Amount fee = await estimateFeeFor(sendAmount, -1);
final opList = await tezartClient.transferOperation(
source: sourceKeyStore,
destination: txData.recipients!.first.address,
amount: sendAmount.raw.toInt(),
);
await opList.computeFees();
final fee = Amount(
rawValue: opList.operations
.map(
(e) => BigInt.from(e.fee),
)
.fold(
BigInt.zero,
(p, e) => p + e,
),
fractionDigits: cryptoCurrency.fractionDigits,
);
int? customGasLimit;
if (isSendAll) {
//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(fee.raw.toInt() + 32),
fractionDigits: cryptoCurrency.fractionDigits,
);
sendAmount = sendAmount - fee;
}
final opList = await _buildSendTransaction(
amount: sendAmount,
address: txData.recipients!.first.address,
customFee: fee,
customGasLimit: customGasLimit,
);
return txData.copyWith(
recipients: [
(
@ -125,63 +170,59 @@ class TezosWallet extends Bip39Wallet {
@override
Future<TxData> confirmSend({required TxData txData}) async {
// TODO: implement confirmSend
throw UnimplementedError();
await txData.tezosOperationsList!.executeAndMonitor();
return txData.copyWith(
txid: txData.tezosOperationsList!.result.id,
);
}
@override
Future<Amount> estimateFeeFor(Amount amount, int feeRate) async {
throw UnimplementedError();
// final ADDRESS_REPLACEME = (await getCurrentReceivingAddress())!.value;
//
// try {
// final sourceKeyStore = await _getKeyStore();
// final tezartClient = tezart.TezartClient(
// (_xtzNode ?? getCurrentNode()).host,
// );
//
// final opList = await tezartClient.transferOperation(
// source: sourceKeyStore,
// destination: ADDRESS_REPLACEME,
// amount: amount.raw.toInt(),
// );
//
// await opList.run();
// await opList.estimate();
//
// final fee = Amount(
// rawValue: opList.operations
// .map(
// (e) => BigInt.from(e.fee),
// )
// .fold(
// BigInt.zero,
// (p, e) => p + e,
// ),
// fractionDigits: cryptoCurrency.fractionDigits,
// );
//
// return fee;
// } catch (e, s) {
// Logging.instance.log(
// "Error in estimateFeeFor() in tezos_wallet.dart: $e\n$s}",
// level: LogLevel.Error,
// );
// rethrow;
// }
if (amount.raw == BigInt.zero) {
amount = Amount(
rawValue: BigInt.one,
fractionDigits: cryptoCurrency.fractionDigits,
);
}
final myAddressForSimulation = (await getCurrentReceivingAddress())!.value;
try {
final opList = await _buildSendTransaction(
amount: amount,
address: myAddressForSimulation,
);
await opList.computeLimits();
await opList.computeFees();
await opList.simulate();
final fee = Amount(
rawValue: opList.operations
.map(
(e) => BigInt.from(e.fee),
)
.fold(
BigInt.zero,
(p, e) => p + e,
),
fractionDigits: cryptoCurrency.fractionDigits,
);
return fee;
} catch (e, s) {
Logging.instance.log(
"Error in estimateFeeFor() in tezos_wallet.dart: $e\n$s}",
level: LogLevel.Error,
);
rethrow;
}
}
/// Not really used (yet)
@override
Future<FeeObject> get fees async {
final feePerTx = (await estimateFeeFor(
Amount(
rawValue: BigInt.one,
fractionDigits: cryptoCurrency.fractionDigits,
),
42))
.raw
.toInt();
Logging.instance.log("feePerTx:$feePerTx", level: LogLevel.Info);
const feePerTx = 1;
return FeeObject(
numberOfBlocksFast: 10,
numberOfBlocksAverage: 10,
@ -303,10 +344,9 @@ class TezosWallet extends Bip39Wallet {
// TODO: optimize updateTransactions
final myAddress = (await getCurrentReceivingAddress())!;
List<TezosTransaction>? txs =
await TezosAPI.getTransactions(myAddress.value);
Logging.instance.log("Transactions: $txs", level: LogLevel.Info);
if (txs == null || txs.isEmpty) {
final txs = await TezosAPI.getTransactions(myAddress.value);
if (txs.isEmpty) {
return;
}
@ -334,7 +374,7 @@ class TezosWallet extends Bip39Wallet {
subType: TransactionSubType.none,
amount: theTx.amountInMicroTez,
amountString: Amount(
rawValue: BigInt.parse(theTx.amountInMicroTez.toString()),
rawValue: BigInt.from(theTx.amountInMicroTez),
fractionDigits: cryptoCurrency.fractionDigits,
).toJsonString(),
fee: theTx.feeInMicroTez,

View file

@ -142,6 +142,14 @@ packages:
url: "https://github.com/cypherstack/bitcoindart.git"
source: git
version: "3.0.1"
blockchain_signer:
dependency: transitive
description:
name: blockchain_signer
sha256: aa62c62df1fec11dbce7516444715ae492862ebdf3108b8b464a1909827963cd
url: "https://pub.dev"
source: hosted
version: "0.1.0"
boolean_selector:
dependency: transitive
description:
@ -1649,10 +1657,11 @@ packages:
tezart:
dependency: "direct main"
description:
name: tezart
sha256: "35d526f2e6ca250c64461ebfb4fa9f64b6599fab8c4242c8e89ae27d4ac2e15a"
url: "https://pub.dev"
source: hosted
path: "."
ref: "8a7070f533e63dd150edae99476f6853bfb25913"
resolved-ref: "8a7070f533e63dd150edae99476f6853bfb25913"
url: "https://github.com/cypherstack/tezart.git"
source: git
version: "2.0.5"
time:
dependency: transitive

View file

@ -154,7 +154,11 @@ dependencies:
url: https://github.com/cypherstack/socks_socket.git
ref: master
bip340: ^0.2.0
tezart: ^2.0.5
# tezart: ^2.0.5
tezart:
git:
url: https://github.com/cypherstack/tezart.git
ref: 8a7070f533e63dd150edae99476f6853bfb25913
socks5_proxy: ^1.0.3+dev.3
coinlib_flutter: ^1.0.0
convert: ^3.1.1