From 3f2f592593efe306f85fb0e81ce07aa1ea90b7b6 Mon Sep 17 00:00:00 2001 From: Dominik Toton <166132265+dtscalac@users.noreply.github.com> Date: Mon, 2 Sep 2024 14:21:13 +0200 Subject: [PATCH] fix: catalyst cardano null utxos (#746) * fix: handle null utxos * fix: more error logs --- .../example/lib/sign_and_submit_rbac_tx.dart | 4 +++ .../example/lib/sign_and_submit_tx.dart | 4 +++ .../src/interop/catalyst_cardano_interop.dart | 2 +- .../catalyst_cardano_wallet_proxy.dart | 35 ++++++++++--------- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/catalyst_voices_packages/catalyst_cardano/catalyst_cardano/example/lib/sign_and_submit_rbac_tx.dart b/catalyst_voices_packages/catalyst_cardano/catalyst_cardano/example/lib/sign_and_submit_rbac_tx.dart index e4ca17c1db1..ccd16f05746 100644 --- a/catalyst_voices_packages/catalyst_cardano/catalyst_cardano/example/lib/sign_and_submit_rbac_tx.dart +++ b/catalyst_voices_packages/catalyst_cardano/catalyst_cardano/example/lib/sign_and_submit_rbac_tx.dart @@ -80,6 +80,10 @@ Future _signAndSubmitRbacTx({ ), ); + if (utxos.isEmpty) { + throw Exception('Insufficient balance, please top up your wallet'); + } + final x509Envelope = await _buildMetadataEnvelope( utxos: utxos, ); diff --git a/catalyst_voices_packages/catalyst_cardano/catalyst_cardano/example/lib/sign_and_submit_tx.dart b/catalyst_voices_packages/catalyst_cardano/catalyst_cardano/example/lib/sign_and_submit_tx.dart index 22bdc078912..a51e7ea4b33 100644 --- a/catalyst_voices_packages/catalyst_cardano/catalyst_cardano/example/lib/sign_and_submit_tx.dart +++ b/catalyst_voices_packages/catalyst_cardano/catalyst_cardano/example/lib/sign_and_submit_tx.dart @@ -14,6 +14,10 @@ Future _signAndSubmitTx({ ), ); + if (utxos.isEmpty) { + throw Exception('Insufficient balance, please top up your wallet'); + } + final unsignedTx = _buildUnsignedTx( utxos: utxos, changeAddress: changeAddress, diff --git a/catalyst_voices_packages/catalyst_cardano/catalyst_cardano_web/lib/src/interop/catalyst_cardano_interop.dart b/catalyst_voices_packages/catalyst_cardano/catalyst_cardano_web/lib/src/interop/catalyst_cardano_interop.dart index 835fceff1d9..43fbaafc34b 100644 --- a/catalyst_voices_packages/catalyst_cardano/catalyst_cardano_web/lib/src/interop/catalyst_cardano_interop.dart +++ b/catalyst_voices_packages/catalyst_cardano/catalyst_cardano_web/lib/src/interop/catalyst_cardano_interop.dart @@ -73,7 +73,7 @@ extension type JSCardanoWalletApi(JSObject _) implements JSObject { ]); /// See [CardanoWalletApi.getUtxos]. - external JSPromise> getUtxos([ + external JSPromise>? getUtxos([ JSAny? amount, JSAny? paginate, ]); diff --git a/catalyst_voices_packages/catalyst_cardano/catalyst_cardano_web/lib/src/interop/catalyst_cardano_wallet_proxy.dart b/catalyst_voices_packages/catalyst_cardano/catalyst_cardano_web/lib/src/interop/catalyst_cardano_wallet_proxy.dart index ce14d5775c2..573ba46361b 100644 --- a/catalyst_voices_packages/catalyst_cardano/catalyst_cardano_web/lib/src/interop/catalyst_cardano_wallet_proxy.dart +++ b/catalyst_voices_packages/catalyst_cardano/catalyst_cardano_web/lib/src/interop/catalyst_cardano_wallet_proxy.dart @@ -176,23 +176,24 @@ class JSCardanoWalletApiProxy implements CardanoWalletApi { Paginate? paginate, }) async { try { - return await _delegate - .getUtxos( - amount != null - ? hex.encode(cbor.encode(amount.toCbor())).toJS - : makeUndefined(), - paginate != null ? JSPaginate.fromDart(paginate) : makeUndefined(), - ) - .toDart - .then( - (array) => array.toDart - .map( - (item) => TransactionUnspentOutput.fromCbor( - cbor.decode(hex.decode(item.toDart)), - ), - ) - .toList(), - ); + final utxos = _delegate.getUtxos( + amount != null + ? hex.encode(cbor.encode(amount.toCbor())).toJS + : makeUndefined(), + paginate != null ? JSPaginate.fromDart(paginate) : makeUndefined(), + ); + + if (utxos == null) return []; + + return await utxos.toDart.then( + (array) => array.toDart + .map( + (item) => TransactionUnspentOutput.fromCbor( + cbor.decode(hex.decode(item.toDart)), + ), + ) + .toList(), + ); } catch (ex) { throw _mapApiException(ex) ?? _mapPaginateException(ex) ??