Fix send and refactoring

This commit is contained in:
likho 2023-08-14 16:36:27 +02:00
parent d785a2ef83
commit a6d3941858

View file

@ -1,6 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'package:decimal/decimal.dart';
import 'package:http/http.dart'; import 'package:http/http.dart';
import 'package:isar/isar.dart'; import 'package:isar/isar.dart';
import 'package:stackwallet/db/isar/main_db.dart'; import 'package:stackwallet/db/isar/main_db.dart';
@ -137,18 +138,26 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
@override @override
Future<String> confirmSend({required Map<String, dynamic> txData}) async { Future<String> confirmSend({required Map<String, dynamic> txData}) async {
try { try {
final node = getCurrentNode().host + getCurrentNode().port.toString();
final int amountInMicroTez = final amount = txData["recipientAmt"] as Amount;
((int.parse((txData["recipientAmt"] as Amount).raw.toString()) * final amountInMicroTez =
1000000)) amount.decimal * Decimal.fromInt(1000000);
.round(); final microtezToInt = int.parse(amountInMicroTez.toString());
final int feeInMicroTez = int.parse(txData["fee"].toString()); final int feeInMicroTez = int.parse(txData["fee"].toString());
final String destinationAddress = txData["address"] as String; final String destinationAddress = txData["address"] as String;
final secretKey = Keystore.fromMnemonic((await mnemonicString)!).secretKey; final secretKey = Keystore.fromMnemonic((await mnemonicString)!).secretKey;
Logging.instance.log(secretKey, level: LogLevel.Info); Logging.instance.log(secretKey, level: LogLevel.Info);
final sourceKeyStore = Keystore.fromSecretKey(secretKey); final sourceKeyStore = Keystore.fromSecretKey(secretKey);
final client = TezartClient("${getCurrentNode().host}:${getCurrentNode().port}"); final client = TezartClient(getCurrentNode().host);
final operation = await client.transferOperation(source: sourceKeyStore, destination: destinationAddress, amount: amountInMicroTez, customFee: feeInMicroTez); //TODO - Update gas Limit
final operation = await client.transferOperation(
source: sourceKeyStore,
destination: destinationAddress,
amount: microtezToInt,
customFee: feeInMicroTez,
customGasLimit: 400
);
await operation.executeAndMonitor(); // This line gives an error await operation.executeAndMonitor(); // This line gives an error
return Future.value(""); return Future.value("");
} catch (e) { } catch (e) {
@ -189,7 +198,7 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
estimatedFee = feeRate; estimatedFee = feeRate;
} }
Logging.instance.log("estimatedFee:$estimatedFee", level: LogLevel.Info); Logging.instance.log("estimatedFee:$estimatedFee", level: LogLevel.Info);
return Amount(rawValue: BigInt.from(estimatedFee), fractionDigits: 6); return Amount(rawValue: BigInt.from(estimatedFee), fractionDigits: coin.decimals);
} }
@override @override
@ -347,8 +356,10 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
} }
Future<void> updateBalance() async { Future<void> updateBalance() async {
try {
var api = var api =
"${getCurrentNode().host}:${getCurrentNode().port}/chains/main/blocks/head/context/contracts/${await currentReceivingAddress}/balance"; "${getCurrentNode().host}/chains/main/blocks/head/context/contracts/${await currentReceivingAddress}/balance";
var theBalance = (await get(Uri.parse(api)).then((value) => value.body)) var theBalance = (await get(Uri.parse(api)).then((value) => value.body))
.substring(1, .substring(1,
(await get(Uri.parse(api)).then((value) => value.body)).length - 2); (await get(Uri.parse(api)).then((value) => value.body)).length - 2);
@ -356,24 +367,32 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
"Balance for ${await currentReceivingAddress}: $theBalance", "Balance for ${await currentReceivingAddress}: $theBalance",
level: LogLevel.Info); level: LogLevel.Info);
var balanceInAmount = Amount( var balanceInAmount = Amount(
rawValue: BigInt.parse(theBalance.toString()), fractionDigits: 6); rawValue: BigInt.parse(theBalance.toString()), fractionDigits: coin.decimals);
_balance = Balance( _balance = Balance(
total: balanceInAmount, total: balanceInAmount,
spendable: balanceInAmount, spendable: balanceInAmount,
blockedTotal: Amount(rawValue: BigInt.parse("0"), fractionDigits: 6), blockedTotal: Amount(rawValue: BigInt.parse("0"), fractionDigits: coin.decimals),
pendingSpendable: Amount(rawValue: BigInt.parse("0"), fractionDigits: 6), pendingSpendable: Amount(rawValue: BigInt.parse("0"), fractionDigits: coin.decimals),
); );
await updateCachedBalance(_balance!); await updateCachedBalance(_balance!);
} catch (e, s) {
Logging.instance.log("ERROR GETTING BALANCE ${e.toString()}", level: LogLevel.Error);
}
} }
Future<void> updateTransactions() async { Future<void> updateTransactions() async {
// TODO: Use node RPC instead of tzstats API // TODO: Use node RPC instead of tzstats API
var api = "https://api.tzstats.com/tables/op?address=${await currentReceivingAddress}"; var api = "https://api.tzstats.com/tables/op?address=${await currentReceivingAddress}";
var jsonResponse = jsonDecode(await get(Uri.parse(api)).then((value) => value.body)); var jsonResponse = jsonDecode(await get(Uri.parse(api)).then((value) => value.body));
List<Tuple2<Transaction, Address>> txs = []; List<Tuple2<Transaction, Address>> txs = [];
for (var tx in jsonResponse as List) { for (var tx in jsonResponse as List) {
if (tx[1] == "transaction") { if (tx[1] == "transaction") {
var txApi = "https://api.tzstats.com/explorer/op/${tx[2]}"; var txApi = "https://api.tzstats.com/explorer/op/${tx[0]}"; //Get transactions by Unique Id, this way we will only get txs
var txJsonResponse = jsonDecode(await get(Uri.parse(txApi)).then((value) => value.body)); var txJsonResponse = jsonDecode(await get(Uri.parse(txApi)).then((value) => value.body));
// Check if list is larger than 1 (if it is, it's a batch transaction) // Check if list is larger than 1 (if it is, it's a batch transaction)
if (!((txJsonResponse as List).length > 1)) { if (!((txJsonResponse as List).length > 1)) {
@ -394,7 +413,7 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
amount: (float.parse(opJson["volume"].toString()) * 1000000).toInt(), amount: (float.parse(opJson["volume"].toString()) * 1000000).toInt(),
amountString: Amount( amountString: Amount(
rawValue: BigInt.parse((float.parse(opJson["volume"].toString()) * 1000000).toInt().toString()), rawValue: BigInt.parse((float.parse(opJson["volume"].toString()) * 1000000).toInt().toString()),
fractionDigits: 6 fractionDigits: coin.decimals
).toJsonString(), ).toJsonString(),
fee: (float.parse(opJson["fee"].toString()) * 1000000).toInt(), fee: (float.parse(opJson["fee"].toString()) * 1000000).toInt(),
height: int.parse(opJson["height"].toString()), height: int.parse(opJson["height"].toString()),
@ -427,13 +446,18 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
} }
Future<void> updateChainHeight() async { Future<void> updateChainHeight() async {
try {
var api = var api =
"${getCurrentNode().host}:${getCurrentNode().port}/chains/main/blocks/head/header/shell"; "${getCurrentNode().host}/chains/main/blocks/head/header/shell";
var jsonParsedResponse = var jsonParsedResponse =
jsonDecode(await get(Uri.parse(api)).then((value) => value.body)); jsonDecode(await get(Uri.parse(api)).then((value) => value.body));
final int intHeight = int.parse(jsonParsedResponse["level"].toString()); final int intHeight = int.parse(jsonParsedResponse["level"].toString());
Logging.instance.log("Chain height: $intHeight", level: LogLevel.Info); Logging.instance.log("Chain height: $intHeight", level: LogLevel.Info);
await updateCachedChainHeight(intHeight); await updateCachedChainHeight(intHeight);
} catch (e, s) {
Logging.instance.log("GET CHAIN HEIGHT ERROR ${e.toString()}", level: LogLevel.Error);
}
} }
@override @override