Skip to content

Commit 968c36a

Browse files
Merge pull request #1209 from cypherstack/ada-updates
Ada updates
2 parents f996eb0 + cbb3fc3 commit 968c36a

File tree

2 files changed

+92
-61
lines changed

2 files changed

+92
-61
lines changed

lib/db/isar/main_db.dart

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,10 @@ class MainDB {
9292
Future<void> updateWalletInfo(WalletInfo walletInfo) async {
9393
try {
9494
await isar.writeTxn(() async {
95-
final info =
96-
await isar.walletInfo
97-
.where()
98-
.walletIdEqualTo(walletInfo.walletId)
99-
.findFirst();
95+
final info = await isar.walletInfo
96+
.where()
97+
.walletIdEqualTo(walletInfo.walletId)
98+
.findFirst();
10099
if (info == null) {
101100
throw Exception(
102101
"updateWalletInfo() called with new WalletInfo."
@@ -189,6 +188,7 @@ class MainDB {
189188
}
190189

191190
Future<List<int>> putAddresses(List<Address> addresses) async {
191+
if (addresses.isEmpty) return [];
192192
try {
193193
return await isar.writeTxn(() async {
194194
return await isar.addresses.putAll(addresses);
@@ -199,6 +199,7 @@ class MainDB {
199199
}
200200

201201
Future<List<int>> updateOrPutAddresses(List<Address> addresses) async {
202+
if (addresses.isEmpty) return [];
202203
try {
203204
final List<int> ids = [];
204205
await isar.writeTxn(() async {
@@ -269,6 +270,7 @@ class MainDB {
269270
}
270271

271272
Future<List<int>> putTransactions(List<Transaction> transactions) async {
273+
if (transactions.isEmpty) return [];
272274
try {
273275
return await isar.writeTxn(() async {
274276
return await isar.transactions.putAll(transactions);
@@ -306,9 +308,11 @@ class MainDB {
306308
await isar.utxos.put(utxo);
307309
});
308310

309-
Future<void> putUTXOs(List<UTXO> utxos) => isar.writeTxn(() async {
310-
await isar.utxos.putAll(utxos);
311-
});
311+
Future<void> putUTXOs(List<UTXO> utxos) => utxos.isEmpty
312+
? Future.value()
313+
: isar.writeTxn(() async {
314+
await isar.utxos.putAll(utxos);
315+
});
312316

313317
Future<bool> updateUTXOs(String walletId, List<UTXO> utxos) async {
314318
bool newUTXO = false;
@@ -317,11 +321,10 @@ class MainDB {
317321
final set = utxos.toSet();
318322
for (final utxo in utxos) {
319323
// check if utxo exists in db and update accordingly
320-
final storedUtxo =
321-
await isar.utxos
322-
.where()
323-
.txidWalletIdVoutEqualTo(utxo.txid, utxo.walletId, utxo.vout)
324-
.findFirst();
324+
final storedUtxo = await isar.utxos
325+
.where()
326+
.txidWalletIdVoutEqualTo(utxo.txid, utxo.walletId, utxo.vout)
327+
.findFirst();
325328

326329
if (storedUtxo != null) {
327330
// update
@@ -441,8 +444,10 @@ class MainDB {
441444
//
442445
Future<void> deleteWalletBlockchainData(String walletId) async {
443446
final transactionCount = await getTransactions(walletId).count();
444-
final transactionCountV2 =
445-
await isar.transactionV2s.where().walletIdEqualTo(walletId).count();
447+
final transactionCountV2 = await isar.transactionV2s
448+
.where()
449+
.walletIdEqualTo(walletId)
450+
.count();
446451
final addressCount = await getAddresses(walletId).count();
447452
final utxoCount = await getUTXOs(walletId).count();
448453
// final lelantusCoinCount =
@@ -453,41 +458,37 @@ class MainDB {
453458

454459
// transactions
455460
for (int i = 0; i < transactionCount; i += paginateLimit) {
456-
final txnIds =
457-
await getTransactions(
458-
walletId,
459-
).offset(i).limit(paginateLimit).idProperty().findAll();
461+
final txnIds = await getTransactions(
462+
walletId,
463+
).offset(i).limit(paginateLimit).idProperty().findAll();
460464
await isar.transactions.deleteAll(txnIds);
461465
}
462466

463467
// transactions V2
464468
for (int i = 0; i < transactionCountV2; i += paginateLimit) {
465-
final txnIds =
466-
await isar.transactionV2s
467-
.where()
468-
.walletIdEqualTo(walletId)
469-
.offset(i)
470-
.limit(paginateLimit)
471-
.idProperty()
472-
.findAll();
469+
final txnIds = await isar.transactionV2s
470+
.where()
471+
.walletIdEqualTo(walletId)
472+
.offset(i)
473+
.limit(paginateLimit)
474+
.idProperty()
475+
.findAll();
473476
await isar.transactionV2s.deleteAll(txnIds);
474477
}
475478

476479
// addresses
477480
for (int i = 0; i < addressCount; i += paginateLimit) {
478-
final addressIds =
479-
await getAddresses(
480-
walletId,
481-
).offset(i).limit(paginateLimit).idProperty().findAll();
481+
final addressIds = await getAddresses(
482+
walletId,
483+
).offset(i).limit(paginateLimit).idProperty().findAll();
482484
await isar.addresses.deleteAll(addressIds);
483485
}
484486

485487
// utxos
486488
for (int i = 0; i < utxoCount; i += paginateLimit) {
487-
final utxoIds =
488-
await getUTXOs(
489-
walletId,
490-
).offset(i).limit(paginateLimit).idProperty().findAll();
489+
final utxoIds = await getUTXOs(
490+
walletId,
491+
).offset(i).limit(paginateLimit).idProperty().findAll();
491492
await isar.utxos.deleteAll(utxoIds);
492493
}
493494

@@ -504,10 +505,9 @@ class MainDB {
504505
await isar.writeTxn(() async {
505506
const paginateLimit = 50;
506507
for (int i = 0; i < addressLabelCount; i += paginateLimit) {
507-
final labelIds =
508-
await getAddressLabels(
509-
walletId,
510-
).offset(i).limit(paginateLimit).idProperty().findAll();
508+
final labelIds = await getAddressLabels(
509+
walletId,
510+
).offset(i).limit(paginateLimit).idProperty().findAll();
511511
await isar.addressLabels.deleteAll(labelIds);
512512
}
513513
});
@@ -518,10 +518,9 @@ class MainDB {
518518
await isar.writeTxn(() async {
519519
const paginateLimit = 50;
520520
for (int i = 0; i < noteCount; i += paginateLimit) {
521-
final labelIds =
522-
await getTransactionNotes(
523-
walletId,
524-
).offset(i).limit(paginateLimit).idProperty().findAll();
521+
final labelIds = await getTransactionNotes(
522+
walletId,
523+
).offset(i).limit(paginateLimit).idProperty().findAll();
525524
await isar.transactionNotes.deleteAll(labelIds);
526525
}
527526
});
@@ -531,6 +530,7 @@ class MainDB {
531530
List<Tuple2<Transaction, Address?>> transactionsData,
532531
String walletId,
533532
) async {
533+
if (transactionsData.isEmpty) return;
534534
try {
535535
await isar.writeTxn(() async {
536536
for (final data in transactionsData) {
@@ -570,15 +570,15 @@ class MainDB {
570570
Future<List<int>> updateOrPutTransactionV2s(
571571
List<TransactionV2> transactions,
572572
) async {
573+
if (transactions.isEmpty) return [];
573574
try {
574575
final List<int> ids = [];
575576
await isar.writeTxn(() async {
576577
for (final tx in transactions) {
577-
final storedTx =
578-
await isar.transactionV2s
579-
.where()
580-
.txidWalletIdEqualTo(tx.txid, tx.walletId)
581-
.findFirst();
578+
final storedTx = await isar.transactionV2s
579+
.where()
580+
.txidWalletIdEqualTo(tx.txid, tx.walletId)
581+
.findFirst();
582582

583583
Id id;
584584
if (storedTx == null) {

lib/wallets/wallet/impl/cardano_wallet.dart

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:blockchain_utils/bip/cardano/mnemonic/cardano_icarus_seed_genera
88
import 'package:blockchain_utils/bip/cardano/shelley/cardano_shelley.dart';
99
import 'package:isar_community/isar.dart';
1010
import 'package:on_chain/ada/ada.dart';
11+
import 'package:on_chain/ada/src/provider/exception/blockfrost_api_error.dart';
1112
import 'package:socks5_proxy/socks.dart';
1213
import 'package:tuple/tuple.dart';
1314

@@ -418,19 +419,45 @@ class CardanoWallet extends Bip39Wallet<Cardano> {
418419
});
419420
}
420421

422+
Future<bool> _checkAddressIsFound() async {
423+
try {
424+
await blockfrostProvider!.request(
425+
BlockfrostRequestSpecificAddress(
426+
ADAAddress.fromAddress((await getCurrentReceivingAddress())!.value),
427+
),
428+
);
429+
return true;
430+
} on BlockfrostError catch (e, s) {
431+
if (e.statusCode == 404 && e.error == "Not found") {
432+
Logging.instance.i(
433+
"Ada address not seen on network yet",
434+
error: e,
435+
stackTrace: s,
436+
);
437+
return false;
438+
} else {
439+
rethrow;
440+
}
441+
}
442+
}
443+
421444
@override
422445
Future<void> updateBalance() async {
423446
try {
424447
await updateProvider();
425448

426-
final addressUtxos = await blockfrostProvider!.request(
427-
BlockfrostRequestAddressUTXOsOfAGivenAsset(
428-
address: ADAAddress.fromAddress(
429-
(await getCurrentReceivingAddress())!.value,
430-
),
431-
asset: "lovelace",
432-
),
433-
);
449+
final addressExists = await _checkAddressIsFound();
450+
451+
final addressUtxos = addressExists
452+
? await blockfrostProvider!.request(
453+
BlockfrostRequestAddressUTXOsOfAGivenAsset(
454+
address: ADAAddress.fromAddress(
455+
(await getCurrentReceivingAddress())!.value,
456+
),
457+
asset: "lovelace",
458+
),
459+
)
460+
: <ADAAccountUTXOResponse>[];
434461

435462
BigInt totalBalanceInLovelace = BigInt.parse("0");
436463
for (final utxo in addressUtxos) {
@@ -498,13 +525,17 @@ class CardanoWallet extends Bip39Wallet<Cardano> {
498525
try {
499526
await updateProvider();
500527

528+
final addressExists = await _checkAddressIsFound();
529+
501530
final currentAddr = (await getCurrentReceivingAddress())!.value;
502531

503-
final txsList = await blockfrostProvider!.request(
504-
BlockfrostRequestAddressTransactions(
505-
ADAAddress.fromAddress(currentAddr),
506-
),
507-
);
532+
final txsList = addressExists
533+
? await blockfrostProvider!.request(
534+
BlockfrostRequestAddressTransactions(
535+
ADAAddress.fromAddress(currentAddr),
536+
),
537+
)
538+
: <ADATransactionSummaryInfoResponse>[];
508539

509540
final parsedTxsList = List<Tuple2<isar.Transaction, Address>>.empty(
510541
growable: true,

0 commit comments

Comments
 (0)