fix: (ba)nano sync/refresh bugs and address optimizations

This commit is contained in:
julian 2023-06-07 13:44:45 -06:00
parent 40d53f08c2
commit 9046379d18
2 changed files with 59 additions and 18 deletions

View file

@ -174,7 +174,7 @@ class BananoWallet extends CoinServiceAPI
Future<String> confirmSend({required Map<String, dynamic> txData}) async { Future<String> confirmSend({required Map<String, dynamic> txData}) async {
try { try {
// our address: // our address:
final String publicAddress = await getAddressFromMnemonic(); final String publicAddress = await currentReceivingAddress;
// first get the account balance: // first get the account balance:
final balanceBody = jsonEncode({ final balanceBody = jsonEncode({
@ -276,8 +276,18 @@ class BananoWallet extends CoinServiceAPI
} }
} }
Future<Address?> get _currentReceivingAddress => db
.getAddresses(walletId)
.filter()
.typeEqualTo(AddressType.nano)
.and()
.subTypeEqualTo(AddressSubType.receiving)
.sortByDerivationIndexDesc()
.findFirst();
@override @override
Future<String> get currentReceivingAddress => getAddressFromMnemonic(); Future<String> get currentReceivingAddress async =>
(await _currentReceivingAddress)?.value ?? await getAddressFromMnemonic();
@override @override
Future<Amount> estimateFeeFor(Amount amount, int feeRate) { Future<Amount> estimateFeeFor(Amount amount, int feeRate) {
@ -298,7 +308,7 @@ class BananoWallet extends CoinServiceAPI
Future<void> updateBalance() async { Future<void> updateBalance() async {
final body = jsonEncode({ final body = jsonEncode({
"action": "account_balance", "action": "account_balance",
"account": await getAddressFromMnemonic(), "account": await currentReceivingAddress,
}); });
final headers = { final headers = {
"Content-Type": "application/json", "Content-Type": "application/json",
@ -333,7 +343,7 @@ class BananoWallet extends CoinServiceAPI
}; };
// our address: // our address:
final String publicAddress = await getAddressFromMnemonic(); final String publicAddress = await currentReceivingAddress;
// first check if the account is open: // first check if the account is open:
// get the account info (we need the frontier and representative): // get the account info (we need the frontier and representative):
@ -452,7 +462,7 @@ class BananoWallet extends CoinServiceAPI
body: jsonEncode({ body: jsonEncode({
"action": "receivable", "action": "receivable",
"source": "true", "source": "true",
"account": await getAddressFromMnemonic(), "account": await currentReceivingAddress,
"count": "-1", "count": "-1",
})); }));
@ -474,7 +484,8 @@ class BananoWallet extends CoinServiceAPI
Future<void> updateTransactions() async { Future<void> updateTransactions() async {
await confirmAllReceivable(); await confirmAllReceivable();
final String publicAddress = await getAddressFromMnemonic(); final receivingAddress = (await _currentReceivingAddress)!;
final String publicAddress = receivingAddress.value;
final response = await http.post(Uri.parse(getCurrentNode().host), final response = await http.post(Uri.parse(getCurrentNode().host),
headers: {"Content-Type": "application/json"}, headers: {"Content-Type": "application/json"},
body: jsonEncode({ body: jsonEncode({
@ -483,7 +494,8 @@ class BananoWallet extends CoinServiceAPI
"count": "-1", "count": "-1",
})); }));
final data = await jsonDecode(response.body); final data = await jsonDecode(response.body);
final transactions = data["history"] as List<dynamic>; final transactions =
data["history"] is List ? data["history"] as List<dynamic> : [];
if (transactions.isEmpty) { if (transactions.isEmpty) {
return; return;
} else { } else {
@ -734,7 +746,11 @@ class BananoWallet extends CoinServiceAPI
coin, coin,
), ),
); );
} catch (e) { } catch (e, s) {
Logging.instance.log(
"Failed to refresh banano wallet \'$walletName\': $e\n$s",
level: LogLevel.Warning,
);
GlobalEventBus.instance.fire( GlobalEventBus.instance.fire(
WalletSyncStatusChangedEvent( WalletSyncStatusChangedEvent(
WalletSyncStatus.unableToSync, WalletSyncStatus.unableToSync,
@ -757,8 +773,18 @@ class BananoWallet extends CoinServiceAPI
@override @override
Future<bool> testNetworkConnection() async { Future<bool> testNetworkConnection() async {
final uri = Uri.parse("${getCurrentNode().host}?action=version"); final uri = Uri.parse(getCurrentNode().host);
final response = await http.get(uri);
final response = await http.post(
uri,
headers: {"Content-Type": "application/json"},
body: jsonEncode(
{
"action": "version",
},
),
);
return response.statusCode == 200; return response.statusCode == 200;
} }
@ -836,7 +862,7 @@ class BananoWallet extends CoinServiceAPI
} }
Future<void> updateChainHeight() async { Future<void> updateChainHeight() async {
final String publicAddress = await getAddressFromMnemonic(); final String publicAddress = await currentReceivingAddress;
// first get the account balance: // first get the account balance:
final infoBody = jsonEncode({ final infoBody = jsonEncode({
"action": "account_info", "action": "account_info",
@ -852,7 +878,9 @@ class BananoWallet extends CoinServiceAPI
); );
final infoData = jsonDecode(infoResponse.body); final infoData = jsonDecode(infoResponse.body);
final int height = int.parse(infoData["confirmation_height"].toString()); final int? height = int.tryParse(
await updateCachedChainHeight(height); infoData["confirmation_height"].toString(),
);
await updateCachedChainHeight(height ?? 0);
} }
} }

View file

@ -504,7 +504,8 @@ class NanoWallet extends CoinServiceAPI
"count": "-1", "count": "-1",
})); }));
final data = await jsonDecode(response.body); final data = await jsonDecode(response.body);
final transactions = data["history"] as List<dynamic>; final transactions =
data["history"] is List ? data["history"] as List<dynamic> : [];
if (transactions.isEmpty) { if (transactions.isEmpty) {
return; return;
} else { } else {
@ -776,8 +777,18 @@ class NanoWallet extends CoinServiceAPI
@override @override
Future<bool> testNetworkConnection() async { Future<bool> testNetworkConnection() async {
final uri = Uri.parse("${getCurrentNode().host}?action=version"); final uri = Uri.parse(getCurrentNode().host);
final response = await http.get(uri);
final response = await http.post(
uri,
headers: {"Content-Type": "application/json"},
body: jsonEncode(
{
"action": "version",
},
),
);
return response.statusCode == 200; return response.statusCode == 200;
} }
@ -871,7 +882,9 @@ class NanoWallet extends CoinServiceAPI
); );
final infoData = jsonDecode(infoResponse.body); final infoData = jsonDecode(infoResponse.body);
final int height = int.parse(infoData["confirmation_height"].toString()); final int? height = int.tryParse(
await updateCachedChainHeight(height); infoData["confirmation_height"].toString(),
);
await updateCachedChainHeight(height ?? 0);
} }
} }