From e3d040b9b817c324f29fed3938e9370356fae0ff Mon Sep 17 00:00:00 2001 From: dethe <76167420+detherminal@users.noreply.github.com> Date: Sun, 1 Sep 2024 15:12:09 +0300 Subject: [PATCH] refactor: enhance ada txs --- lib/wallets/wallet/impl/cardano_wallet.dart | 54 ++++++++++--------- pubspec.lock | 4 +- scripts/app_config/templates/pubspec.template | 2 +- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/lib/wallets/wallet/impl/cardano_wallet.dart b/lib/wallets/wallet/impl/cardano_wallet.dart index 7b82edd08..31d645415 100644 --- a/lib/wallets/wallet/impl/cardano_wallet.dart +++ b/lib/wallets/wallet/impl/cardano_wallet.dart @@ -353,10 +353,11 @@ class CardanoWallet extends Bip39Wallet { await updateProvider(); final addressUtxos = await blockfrostProvider!.request( - BlockfrostRequestAddressUTXOs( - ADAAddress.fromAddress( + BlockfrostRequestAddressUTXOsOfAGivenAsset( + address: ADAAddress.fromAddress( (await getCurrentReceivingAddress())!.value, ), + asset: "lovelace", ), ); @@ -402,7 +403,7 @@ class CardanoWallet extends Bip39Wallet { await info.updateCachedChainHeight( newHeight: latestBlock.height == null ? 0 : latestBlock.height!, - isar: mainDB.isar); + isar: mainDB.isar,); } catch (e, s) { Logging.instance.log( "Error updating transactions in cardano_wallet.dart: $e\n$s", @@ -443,55 +444,56 @@ class CardanoWallet extends Bip39Wallet { ); var txType = isar.TransactionType.unknown; - var txOutputIndex = -1; - for (final input in utxoInfo.inputs) { if (input.address == currentAddr) { txType = isar.TransactionType.outgoing; } } + if (txType == isar.TransactionType.outgoing) { + var isSelfTx = true; + for (final output in utxoInfo.outputs) { + if (output.address != currentAddr) { + isSelfTx = false; + } + } + if (isSelfTx) { + txType = isar.TransactionType.sentToSelf; + } + } + if (txType == isar.TransactionType.unknown) { for (final output in utxoInfo.outputs) { if (output.address == currentAddr) { txType = isar.TransactionType.incoming; - txOutputIndex = output.outputIndex; } } } var receiverAddr = "Unknown?"; + var amount = 0; if (txType == isar.TransactionType.incoming) { receiverAddr = currentAddr; + for (final output in utxoInfo.outputs) { + if (output.address == currentAddr) { + amount += int.parse(output.amount.first.quantity); + } + } } else if (txType == isar.TransactionType.outgoing) { for (final output in utxoInfo.outputs) { if (output.address != currentAddr) { receiverAddr = output.address; - txOutputIndex = output.outputIndex; + amount += int.parse(output.amount.first.quantity); } } + } else if (txType == isar.TransactionType.sentToSelf) { + receiverAddr = currentAddr; + for (final output in utxoInfo.outputs) { + amount += int.parse(output.amount.first.quantity); + } } - var amount = 0; - - // Temporary solution until https://github.com/mrtnetwork/On_chain/issues/9 is solved. - // Use the library when the mentioned issue is solved. - final currentNode = getCurrentNode(); - final response = await _httpClient.get( - url: Uri.parse( - "${currentNode.host}:${currentNode.port}/api/v0/txs/${tx.txHash}/utxos",), - proxyInfo: Prefs.instance.useTor - ? TorService.sharedInstance.getProxyInfo() - : null, - ); - final json = jsonDecode(response.body); - if (txOutputIndex != -1) { - amount = int.parse(json["outputs"][txOutputIndex]["amount"][0] - ["quantity"]! as String,); - } - // Temporary solution part end. - final transaction = isar.Transaction( walletId: walletId, txid: txInfo.hash, diff --git a/pubspec.lock b/pubspec.lock index daa4609c0..682c9a65c 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1289,10 +1289,10 @@ packages: dependency: "direct main" description: name: on_chain - sha256: "53a17cc19c722e3cdef7a34931e7cd07c490443a92bf538f08cc755b854dfb59" + sha256: "4040c187be82f6f6414110139f6e70fe304f23f63111c3ee60438c539b1a1dce" url: "https://pub.dev" source: hosted - version: "3.9.0" + version: "4.0.1" package_config: dependency: transitive description: diff --git a/scripts/app_config/templates/pubspec.template b/scripts/app_config/templates/pubspec.template index e28d27667..b443f9f97 100644 --- a/scripts/app_config/templates/pubspec.template +++ b/scripts/app_config/templates/pubspec.template @@ -199,7 +199,7 @@ dependencies: camera_platform_interface: ^2.8.0 camera_macos: ^0.0.8 blockchain_utils: ^3.3.0 - on_chain: ^3.7.0 + on_chain: ^4.0.1 dev_dependencies: flutter_test: