diff --git a/lib/exchange/provider/thorchain_exchange.provider.dart b/lib/exchange/provider/thorchain_exchange.provider.dart index 29e64b18f..baf37c098 100644 --- a/lib/exchange/provider/thorchain_exchange.provider.dart +++ b/lib/exchange/provider/thorchain_exchange.provider.dart @@ -1,6 +1,5 @@ import 'dart:convert'; -import 'package:cake_wallet/entities/preferences_key.dart'; import 'package:cake_wallet/exchange/exchange_provider_description.dart'; import 'package:cake_wallet/exchange/limits.dart'; import 'package:cake_wallet/exchange/provider/exchange_provider.dart'; @@ -8,11 +7,9 @@ import 'package:cake_wallet/exchange/trade.dart'; import 'package:cake_wallet/exchange/trade_request.dart'; import 'package:cake_wallet/exchange/trade_state.dart'; import 'package:cake_wallet/exchange/utils/currency_pairs_utils.dart'; -import 'package:collection/collection.dart'; import 'package:cw_core/crypto_currency.dart'; import 'package:hive/hive.dart'; import 'package:http/http.dart' as http; -import 'package:shared_preferences/shared_preferences.dart'; class ThorChainExchangeProvider extends ExchangeProvider { ThorChainExchangeProvider({required this.tradesStore}) @@ -29,9 +26,11 @@ class ThorChainExchangeProvider extends ExchangeProvider { .toList()) ]; + static final isRefundAddressSupported = [CryptoCurrency.eth]; + static const _baseURL = 'https://thornode.ninerealms.com'; static const _quotePath = '/thorchain/quote/swap'; - static const _txInfoPath = '/thorchain/tx/'; + static const _txInfoPath = '/thorchain/tx/status/'; static const _affiliateName = 'cakewallet'; static const _affiliateBps = '0'; @@ -117,7 +116,9 @@ class ThorChainExchangeProvider extends ExchangeProvider { 'amount': _doubleToThorChainString(formattedFromAmount), 'destination': formattedToAddress, 'affiliate': _affiliateName, - 'affiliate_bps': _affiliateBps + 'affiliate_bps': _affiliateBps, + 'refund_address': + isRefundAddressSupported.contains(request.fromCurrency) ? request.refundAddress : '', }; final responseJSON = await _getSwapQuote(params); @@ -133,7 +134,7 @@ class ThorChainExchangeProvider extends ExchangeProvider { inputAddress: inputAddress, createdAt: DateTime.now(), amount: request.fromAmount, - state: TradeState.pending, + state: TradeState.notFound, payoutAddress: request.toAddress, memo: memo); } @@ -146,25 +147,30 @@ class ThorChainExchangeProvider extends ExchangeProvider { final response = await http.get(uri); if (response.statusCode == 404) { - throw Exception('Trade not found for id: $id'); + throw Exception('Trade not found for id: $formattedId'); } else if (response.statusCode != 200) { throw Exception('Unexpected HTTP status: ${response.statusCode}'); } final responseJSON = json.decode(response.body); - final observedTx = responseJSON['observed_tx']; - if (observedTx == null) { - throw Exception('No observed transaction found for id: $id'); + final Map stagesJson = responseJSON['stages'] as Map; + + final inboundObservedStarted = stagesJson['inbound_observed']?['started'] as bool? ?? true; + if (!inboundObservedStarted) { + throw Exception('Trade has not started for id: $formattedId'); } - final tx = observedTx['tx']; + final currentState = _updateStateBasedOnStages(stagesJson) ?? TradeState.notFound; + + final tx = responseJSON['tx']; final String fromAddress = tx['from_address'] as String? ?? ''; final String toAddress = tx['to_address'] as String? ?? ''; final List coins = tx['coins'] as List; final String? memo = tx['memo'] as String?; final String toAsset = memo != null ? (memo.split(':')[1]).split('.')[0] : ''; - final status = observedTx['status'] as String?; - final formattedStatus = status ?? 'pending'; + + final plannedOutTxs = responseJSON['planned_out_txs'] as List?; + final isRefund = plannedOutTxs?.any((tx) => tx['refund'] == true) ?? false; return Trade( id: id, @@ -174,8 +180,9 @@ class ThorChainExchangeProvider extends ExchangeProvider { inputAddress: fromAddress, payoutAddress: toAddress, amount: coins.first['amount'] as String? ?? '0.0', - state: TradeState.deserialize(raw: formattedStatus), + state: currentState, memo: memo, + isRefund: isRefund, ); } @@ -200,4 +207,26 @@ class ThorChainExchangeProvider extends ExchangeProvider { String _doubleToThorChainString(double amount) => (amount * 1e8).toInt().toString(); double _thorChainAmountToDouble(String amount) => double.parse(amount) / 1e8; + + TradeState? _updateStateBasedOnStages(Map stages) { + TradeState? currentState; + + if (stages['inbound_observed']['completed'] as bool? ?? false) { + currentState = TradeState.confirmation; + } + if (stages['inbound_confirmation_counted']['completed'] as bool? ?? false) { + currentState = TradeState.confirmed; + } + if (stages['inbound_finalised']['completed'] as bool? ?? false) { + currentState = TradeState.processing; + } + if (stages['swap_finalised']['completed'] as bool? ?? false) { + currentState = TradeState.traded; + } + if (stages['outbound_signed']['completed'] as bool? ?? false) { + currentState = TradeState.success; + } + + return currentState; + } } diff --git a/lib/exchange/trade.dart b/lib/exchange/trade.dart index 8a7a88e07..4bfb335b8 100644 --- a/lib/exchange/trade.dart +++ b/lib/exchange/trade.dart @@ -29,7 +29,8 @@ class Trade extends HiveObject { this.providerName, this.fromWalletAddress, this.memo, - this.txId + this.txId, + this.isRefund, }) { if (provider != null) providerRaw = provider.raw; @@ -113,6 +114,9 @@ class Trade extends HiveObject { @HiveField(19) String? txId; + @HiveField(20) + bool? isRefund; + static Trade fromMap(Map map) { return Trade( id: map['id'] as String, @@ -125,7 +129,8 @@ class Trade extends HiveObject { walletId: map['wallet_id'] as String, fromWalletAddress: map['from_wallet_address'] as String?, memo: map['memo'] as String?, - txId: map['tx_id'] as String? + txId: map['tx_id'] as String?, + isRefund: map['isRefund'] as bool? ); } @@ -140,7 +145,8 @@ class Trade extends HiveObject { 'wallet_id': walletId, 'from_wallet_address': fromWalletAddress, 'memo': memo, - 'tx_id': txId + 'tx_id': txId, + 'isRefund': isRefund }; } diff --git a/lib/exchange/trade_state.dart b/lib/exchange/trade_state.dart index 313e62b42..2c58a96f4 100644 --- a/lib/exchange/trade_state.dart +++ b/lib/exchange/trade_state.dart @@ -41,6 +41,8 @@ class TradeState extends EnumerableItem with Serializable { static const success = TradeState(raw: 'success', title: 'Success'); static TradeState deserialize({required String raw}) { switch (raw) { + case 'NOT_FOUND': + return notFound; case 'pending': return pending; case 'confirming': diff --git a/lib/view_model/trade_details_view_model.dart b/lib/view_model/trade_details_view_model.dart index ba12037f1..1da322778 100644 --- a/lib/view_model/trade_details_view_model.dart +++ b/lib/view_model/trade_details_view_model.dart @@ -153,6 +153,11 @@ abstract class TradeDetailsViewModelBase with Store { title: S.current.track, value: trackUrl, onTap: () => _launchUrl(trackUrl))); } + if (trade.isRefund == true) { + items.add(StandartListItem( + title: 'Refund', value: trade.refundAddress ?? '')); + } + if (trade.provider == ExchangeProviderDescription.trocador) { items.add(StandartListItem( title: '${trade.providerName} ${S.current.id.toUpperCase()}',