Merge pull request #646 from detherminal/add-xtz

implement fees and sending?
This commit is contained in:
Likho 2023-08-07 16:56:25 +02:00 committed by GitHub
commit 491fe2d402
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -144,8 +144,13 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
.round(); .round();
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 String sourceAddress = await currentReceivingAddress; final secretKey = Keystore.fromMnemonic((await mnemonicString)!).secretKey;
return Future.value(""); // TODO: return tx hash Logging.instance.log(secretKey, level: LogLevel.Info);
final sourceKeyStore = Keystore.fromSecretKey(secretKey);
final client = TezartClient("${getCurrentNode().host}:${getCurrentNode().port}");
final operation = await client.transferOperation(source: sourceKeyStore, destination: destinationAddress, amount: amountInMicroTez, customFee: feeInMicroTez);
await operation.executeAndMonitor(); // This line gives an error
return Future.value("");
} catch (e) { } catch (e) {
Logging.instance.log(e.toString(), level: LogLevel.Error); Logging.instance.log(e.toString(), level: LogLevel.Error);
return Future.error(e); return Future.error(e);
@ -162,12 +167,29 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
} }
@override @override
Future<Amount> estimateFeeFor(Amount amount, int feeRate) { Future<Amount> estimateFeeFor(Amount amount, int feeRate) async {
return Future.value( // TODO: Check if this is correct
Amount( var api = "https://api.tzstats.com/series/op?start_date=today&collapse=10d";
rawValue: BigInt.parse(100000.toString()), var response = jsonDecode((await get(Uri.parse(api))).body)[0];
fractionDigits: coin.decimals), double totalFees = response[4] as double;
); int totalTxs = response[8] as int;
int feePerTx = (totalFees / totalTxs * 1000000).floor();
int estimatedFee = 0;
Logging.instance.log("feePerTx:$feePerTx", level: LogLevel.Info);
Logging.instance.log("feeRate:$feeRate", level: LogLevel.Info);
switch (feeRate) {
case 0:
estimatedFee = feePerTx * 2;
case 1:
estimatedFee = feePerTx;
case 2:
case 3:
estimatedFee = (feePerTx / 2).floor();
default:
estimatedFee = feeRate;
}
Logging.instance.log("estimatedFee:$estimatedFee", level: LogLevel.Info);
return Amount(rawValue: BigInt.from(estimatedFee), fractionDigits: 6);
} }
@override @override
@ -178,14 +200,21 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
@override @override
Future<FeeObject> get fees async { Future<FeeObject> get fees async {
// TODO: Change this to get fees from node and fix numberOfBlocks // TODO: Check if this is correct
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);
// TODO: fix numberOfBlocks
return FeeObject( return FeeObject(
numberOfBlocksFast: 1, numberOfBlocksFast: 3,
numberOfBlocksAverage: 1, numberOfBlocksAverage: 10,
numberOfBlocksSlow: 1, numberOfBlocksSlow: 30,
fast: 1000000, fast: (feePerTx * 2),
medium: 100000, medium: feePerTx,
slow: 10000, slow: (feePerTx / 2).floor(),
); );
} }
@ -213,6 +242,13 @@ class TezosWallet extends CoinServiceAPI with WalletCache, WalletDB {
@override @override
Future<void> initializeNew() async { Future<void> initializeNew() 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(); var newKeystore = Keystore.random();
await _secureStore.write( await _secureStore.write(
key: '${_walletId}_mnemonic', key: '${_walletId}_mnemonic',