WIP: XLM fixes and refactor

This commit is contained in:
likho 2023-08-22 18:33:24 +02:00
parent 27e33f4dea
commit 2732b2fe71
3 changed files with 54 additions and 43 deletions

View file

@ -27,6 +27,7 @@ import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/test_epic_box_connection.dart';
import 'package:stackwallet/utilities/test_monero_node_connection.dart';
import 'package:stackwallet/utilities/test_stellar_node_connection.dart';
import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/util.dart';
import 'package:stackwallet/widgets/background.dart';
@ -194,10 +195,14 @@ class _AddEditNodeViewState extends ConsumerState<AddEditNodeView> {
// await client.getSyncStatus();
} catch (_) {}
case Coin.nano:
case Coin.banano:
case Coin.stellar:
case Coin.stellarTestnet:
try {
testPassed = await testStellarNodeConnection(formData.host!);
} catch(_) {}
case Coin.nano:
case Coin.banano:
throw UnimplementedError();
//TODO: check network/node
}

View file

@ -1,5 +1,7 @@
import 'dart:async';
import 'dart:convert';
import 'package:decimal/decimal.dart';
import 'package:http/http.dart' as http;
import 'package:isar/isar.dart';
import 'package:stackwallet/db/isar/main_db.dart';
@ -37,6 +39,7 @@ class StellarWallet extends CoinServiceAPI with WalletCache, WalletDB {
late StellarSDK stellarSdk;
late Network stellarNetwork;
StellarWallet({
required String walletId,
required String walletName,
@ -53,20 +56,28 @@ class StellarWallet extends CoinServiceAPI with WalletCache, WalletDB {
initCache(walletId, coin);
initWalletDB(mockableOverride: mockableOverride);
if (coin.name == "stellarTestnet") {
if (coin.isTestNet) {
stellarSdk = StellarSDK.TESTNET;
stellarNetwork = Network.TESTNET;
} else {
stellarSdk = StellarSDK.PUBLIC;
stellarNetwork = Network.PUBLIC;
}
_updateNode();
}
void _updateNode() {
_xlmNode = NodeService(secureStorageInterface: _secureStore)
.getPrimaryNodeFor(coin: coin) ??
DefaultNodes.getNodeFor(coin);
stellarSdk = StellarSDK("${_xlmNode!.host}:${_xlmNode!.port}");
}
late final TransactionNotificationTracker txTracker;
late SecureStorageInterface _secureStore;
// final StellarSDK stellarSdk = StellarSDK.PUBLIC;
@override
bool get isFavorite => _isFavorite ??= getCachedIsFavorite();
bool? _isFavorite;
@ -232,32 +243,14 @@ class StellarWallet extends CoinServiceAPI with WalletCache, WalletDB {
(await _currentReceivingAddress)?.value ?? await getAddressSW();
Future<int> getBaseFee() async {
// final nodeURI = Uri.parse("${getCurrentNode().host}:${getCurrentNode().port}");
final nodeURI = Uri.parse(getCurrentNode().host);
final httpClient = http.Client();
FeeStatsResponse fsp =
await FeeStatsRequestBuilder(httpClient, nodeURI).execute();
return int.parse(fsp.lastLedgerBaseFee);
var fees = await stellarSdk.feeStats.execute();
return int.parse(fees.lastLedgerBaseFee);
}
@override
Future<Amount> estimateFeeFor(Amount amount, int feeRate) async {
var baseFee = await getBaseFee();
int fee = 100;
switch (feeRate) {
case 0:
fee = baseFee * 10;
case 1:
case 2:
fee = baseFee * 50;
case 3:
fee = baseFee * 100;
case 4:
fee = baseFee * 200;
default:
fee = baseFee * 50;
}
return Amount(rawValue: BigInt.from(fee), fractionDigits: coin.decimals);
return Amount(rawValue: BigInt.from(baseFee), fractionDigits: coin.decimals);
}
@override
@ -285,20 +278,13 @@ class StellarWallet extends CoinServiceAPI with WalletCache, WalletDB {
@override
Future<FeeObject> get fees async {
// final nodeURI = Uri.parse("${getCurrentNode().host}:${getCurrentNode().port}");
final nodeURI = Uri.parse(getCurrentNode().host);
final httpClient = http.Client();
FeeStatsResponse fsp =
await FeeStatsRequestBuilder(httpClient, nodeURI).execute();
return FeeObject(
numberOfBlocksFast: 0,
numberOfBlocksAverage: 0,
numberOfBlocksSlow: 0,
fast: int.parse(fsp.lastLedgerBaseFee) * 100,
medium: int.parse(fsp.lastLedgerBaseFee) * 50,
slow: int.parse(fsp.lastLedgerBaseFee) * 10);
numberOfBlocksFast: 10,
numberOfBlocksAverage: 10,
numberOfBlocksSlow: 10,
fast: 1,
medium: 1,
slow: 1);
}
@override
@ -399,6 +385,7 @@ class StellarWallet extends CoinServiceAPI with WalletCache, WalletDB {
{required String address,
required Amount amount,
Map<String, dynamic>? args}) async {
try {
final feeRate = args?["feeRate"];
var fee = 1000;
@ -750,10 +737,7 @@ class StellarWallet extends CoinServiceAPI with WalletCache, WalletDB {
@override
Future<void> updateNode(bool shouldRefresh) async {
_xlmNode = NodeService(secureStorageInterface: _secureStore)
.getPrimaryNodeFor(coin: coin) ??
DefaultNodes.getNodeFor(coin);
_updateNode();
if (shouldRefresh) {
unawaited(refresh());
}

View file

@ -0,0 +1,22 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
Future<bool> testStellarNodeConnection(String host) async {
final client = http.Client();
Uri uri = Uri.parse(host);
final response = await client.get(
uri,
headers: {'Content-Type': 'application/json'},
).timeout(const Duration(milliseconds: 2000),
onTimeout: () async => http.Response('Error', 408));
final json = jsonDecode(response.body);
if (response.statusCode == 200 && json["horizon_version"] != null) {
return true;
} else {
return false;
}
}