Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 16 additions & 22 deletions lib/bloc/withdraw_form/withdraw_form_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -23,15 +22,13 @@ import 'package:decimal/decimal.dart';
class WithdrawFormBloc extends Bloc<WithdrawFormEvent, WithdrawFormState> {
final KomodoDefiSdk _sdk;
final WalletType? _walletType;
final Mm2Api _mm2Api;

WithdrawFormBloc({
required Asset asset,
required KomodoDefiSdk sdk,
required Mm2Api mm2Api,
WalletType? walletType,
}) : _sdk = sdk,
_mm2Api = mm2Api,
_walletType = walletType,
super(
WithdrawFormState(
Expand Down Expand Up @@ -485,27 +482,24 @@ class WithdrawFormBloc extends Bloc<WithdrawFormEvent, WithdrawFormState> {
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(
Expand Down
7 changes: 7 additions & 0 deletions lib/blocs/kmd_rewards_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@ 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<String, dynamic> json) {
return SendRawTransactionRequest(
coin: json['coin'],
txHex: json['tx_hex'],
txJson: json['tx_json'],
);
}

@override
final String method = 'send_raw_transaction';

String coin;
String txHex;
String? txHex;
Map<String, dynamic>? txJson;

@override
late String userpass;
Expand All @@ -27,7 +33,8 @@ class SendRawTransactionRequest implements BaseRequest {
return <String, dynamic>{
'method': method,
'coin': coin,
'tx_hex': txHex,
if (txHex != null) 'tx_hex': txHex,
if (txJson != null) 'tx_json': txJson,
'userpass': userpass,
};
}
Expand Down
14 changes: 11 additions & 3 deletions lib/model/withdraw_details/withdraw_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<String, dynamic> json) {
final String totalAmount = json['total_amount'].toString();
final String spentByMe = json['spent_by_me'].toString();
Expand All @@ -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']),
Expand Down Expand Up @@ -55,7 +61,8 @@ class WithdrawDetails {
internalId: '',
);

final String txHex;
final String? txHex;
final Map<String, dynamic>? txJson;
final String txHash;
final List<String> from;
final List<String> to;
Expand Down Expand Up @@ -83,6 +90,7 @@ class WithdrawDetails {
static WithdrawDetails fromTrezorJson(Map<String, dynamic> json) {
return WithdrawDetails(
txHex: json['tx_hex'],
txJson: json['tx_json'],
txHash: json['tx_hash'],
totalAmount: json['total_amount'].toString(),
coin: json['coin'],
Expand Down
2 changes: 1 addition & 1 deletion sdk
Submodule sdk updated from ac3138 to 91dd9b
Loading