diff --git a/lib/bloc/withdraw_form/withdraw_form_bloc.dart b/lib/bloc/withdraw_form/withdraw_form_bloc.dart index d9c3c4002c..c99d68c61d 100644 --- a/lib/bloc/withdraw_form/withdraw_form_bloc.dart +++ b/lib/bloc/withdraw_form/withdraw_form_bloc.dart @@ -6,7 +6,6 @@ import 'package:web_dex/bloc/withdraw_form/withdraw_form_bloc.dart'; import 'package:web_dex/generated/codegen_loader.g.dart'; import 'package:web_dex/mm2/mm2_api/rpc/base.dart'; import 'package:web_dex/mm2/mm2_api/mm2_api.dart'; -import 'package:web_dex/mm2/mm2_api/rpc/send_raw_transaction/send_raw_transaction_request.dart'; import 'package:web_dex/model/text_error.dart'; import 'package:web_dex/model/wallet.dart'; import 'package:web_dex/services/fd_monitor_service.dart'; @@ -23,7 +22,6 @@ import 'package:decimal/decimal.dart'; class WithdrawFormBloc extends Bloc { final KomodoDefiSdk _sdk; final WalletType? _walletType; - final Mm2Api _mm2Api; WithdrawFormBloc({ required Asset asset, @@ -31,7 +29,6 @@ class WithdrawFormBloc extends Bloc { required Mm2Api mm2Api, WalletType? walletType, }) : _sdk = sdk, - _mm2Api = mm2Api, _walletType = walletType, super( WithdrawFormState( @@ -485,27 +482,24 @@ class WithdrawFormBloc extends Bloc { throw Exception('Missing withdrawal preview'); } - final response = await _mm2Api.sendRawTransaction( - SendRawTransactionRequest( - coin: preview.coin, - txHex: preview.txHex, - ), - ); - - if (response.txHash == null) { - throw Exception(response.error?.message ?? 'Broadcast failed'); + // Execute the previewed withdrawal (transaction already signed during preview) + WithdrawalResult? result; + await for (final progress in _sdk.withdrawals.executeWithdrawal( + preview, + state.asset.id.id, + )) { + if (progress.status == WithdrawalStatus.complete) { + result = progress.withdrawalResult; + break; + } else if (progress.status == WithdrawalStatus.error) { + throw Exception(progress.errorMessage ?? 'Broadcast failed'); + } + // Continue for in-progress states } - final result = WithdrawalResult( - txHash: response.txHash!, - balanceChanges: preview.balanceChanges, - coin: preview.coin, - toAddress: preview.to.first, - fee: preview.fee, - kmdRewardsEligible: - preview.kmdRewards != null && - Decimal.parse(preview.kmdRewards!.amount) > Decimal.zero, - ); + if (result == null) { + throw Exception('Withdrawal completed without result'); + } emit( state.copyWith( diff --git a/lib/blocs/kmd_rewards_bloc.dart b/lib/blocs/kmd_rewards_bloc.dart index ec31b5d469..b0b8b2b2c0 100644 --- a/lib/blocs/kmd_rewards_bloc.dart +++ b/lib/blocs/kmd_rewards_bloc.dart @@ -41,6 +41,13 @@ class KmdRewardsBloc implements BlocBase { ); } + if (withdrawDetails.txHex == null) { + _claimInProgress = false; + return BlocResponse( + error: TextError(error: LocaleKeys.somethingWrong.tr()), + ); + } + final tx = await _mm2Api.sendRawTransaction(SendRawTransactionRequest( coin: 'KMD', txHex: withdrawDetails.txHex, diff --git a/lib/mm2/mm2_api/rpc/send_raw_transaction/send_raw_transaction_request.dart b/lib/mm2/mm2_api/rpc/send_raw_transaction/send_raw_transaction_request.dart index d3e2573753..d32d9f474a 100644 --- a/lib/mm2/mm2_api/rpc/send_raw_transaction/send_raw_transaction_request.dart +++ b/lib/mm2/mm2_api/rpc/send_raw_transaction/send_raw_transaction_request.dart @@ -3,13 +3,18 @@ import 'package:web_dex/mm2/mm2_api/rpc/base.dart'; class SendRawTransactionRequest implements BaseRequest { SendRawTransactionRequest({ required this.coin, - required this.txHex, - }); + this.txHex, + this.txJson, + }) : assert( + txHex != null || txJson != null, + 'Either txHex or txJson must be provided', + ); factory SendRawTransactionRequest.fromJson(Map json) { return SendRawTransactionRequest( coin: json['coin'], txHex: json['tx_hex'], + txJson: json['tx_json'], ); } @@ -17,7 +22,8 @@ class SendRawTransactionRequest implements BaseRequest { final String method = 'send_raw_transaction'; String coin; - String txHex; + String? txHex; + Map? txJson; @override late String userpass; @@ -27,7 +33,8 @@ class SendRawTransactionRequest implements BaseRequest { return { 'method': method, 'coin': coin, - 'tx_hex': txHex, + if (txHex != null) 'tx_hex': txHex, + if (txJson != null) 'tx_json': txJson, 'userpass': userpass, }; } diff --git a/lib/model/withdraw_details/withdraw_details.dart b/lib/model/withdraw_details/withdraw_details.dart index 313732a28d..3f34cb10da 100644 --- a/lib/model/withdraw_details/withdraw_details.dart +++ b/lib/model/withdraw_details/withdraw_details.dart @@ -2,7 +2,8 @@ import 'package:web_dex/model/withdraw_details/fee_details.dart'; class WithdrawDetails { WithdrawDetails({ - required this.txHex, + this.txHex, + this.txJson, required this.txHash, required this.from, required this.to, @@ -15,7 +16,11 @@ class WithdrawDetails { required this.feeDetails, required this.coin, required this.internalId, - }); + }) : assert( + txHex != null || txJson != null, + 'Either txHex or txJson must be provided', + ); + factory WithdrawDetails.fromJson(Map json) { final String totalAmount = json['total_amount'].toString(); final String spentByMe = json['spent_by_me'].toString(); @@ -24,6 +29,7 @@ class WithdrawDetails { return WithdrawDetails( txHex: json['tx_hex'], + txJson: json['tx_json'], txHash: json['tx_hash'], from: List.from(json['from']), to: List.from(json['to']), @@ -55,7 +61,8 @@ class WithdrawDetails { internalId: '', ); - final String txHex; + final String? txHex; + final Map? txJson; final String txHash; final List from; final List to; @@ -83,6 +90,7 @@ class WithdrawDetails { static WithdrawDetails fromTrezorJson(Map json) { return WithdrawDetails( txHex: json['tx_hex'], + txJson: json['tx_json'], txHash: json['tx_hash'], totalAmount: json['total_amount'].toString(), coin: json['coin'], diff --git a/sdk b/sdk index ac31382745..91dd9b309b 160000 --- a/sdk +++ b/sdk @@ -1 +1 @@ -Subproject commit ac3138274501b20391f5a4e37a8ef7dbd860c059 +Subproject commit 91dd9b309ba4094d3e4fff2ea4bcaed670eb08af