mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-01-24 03:26:38 +00:00
add new status endpoint and refund address for eth
This commit is contained in:
parent
8228959ed0
commit
66e639f02b
4 changed files with 59 additions and 17 deletions
|
@ -1,6 +1,5 @@
|
||||||
import 'dart:convert';
|
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/exchange_provider_description.dart';
|
||||||
import 'package:cake_wallet/exchange/limits.dart';
|
import 'package:cake_wallet/exchange/limits.dart';
|
||||||
import 'package:cake_wallet/exchange/provider/exchange_provider.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_request.dart';
|
||||||
import 'package:cake_wallet/exchange/trade_state.dart';
|
import 'package:cake_wallet/exchange/trade_state.dart';
|
||||||
import 'package:cake_wallet/exchange/utils/currency_pairs_utils.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:cw_core/crypto_currency.dart';
|
||||||
import 'package:hive/hive.dart';
|
import 'package:hive/hive.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
|
||||||
|
|
||||||
class ThorChainExchangeProvider extends ExchangeProvider {
|
class ThorChainExchangeProvider extends ExchangeProvider {
|
||||||
ThorChainExchangeProvider({required this.tradesStore})
|
ThorChainExchangeProvider({required this.tradesStore})
|
||||||
|
@ -29,9 +26,11 @@ class ThorChainExchangeProvider extends ExchangeProvider {
|
||||||
.toList())
|
.toList())
|
||||||
];
|
];
|
||||||
|
|
||||||
|
static final isRefundAddressSupported = [CryptoCurrency.eth];
|
||||||
|
|
||||||
static const _baseURL = 'https://thornode.ninerealms.com';
|
static const _baseURL = 'https://thornode.ninerealms.com';
|
||||||
static const _quotePath = '/thorchain/quote/swap';
|
static const _quotePath = '/thorchain/quote/swap';
|
||||||
static const _txInfoPath = '/thorchain/tx/';
|
static const _txInfoPath = '/thorchain/tx/status/';
|
||||||
static const _affiliateName = 'cakewallet';
|
static const _affiliateName = 'cakewallet';
|
||||||
static const _affiliateBps = '0';
|
static const _affiliateBps = '0';
|
||||||
|
|
||||||
|
@ -117,7 +116,9 @@ class ThorChainExchangeProvider extends ExchangeProvider {
|
||||||
'amount': _doubleToThorChainString(formattedFromAmount),
|
'amount': _doubleToThorChainString(formattedFromAmount),
|
||||||
'destination': formattedToAddress,
|
'destination': formattedToAddress,
|
||||||
'affiliate': _affiliateName,
|
'affiliate': _affiliateName,
|
||||||
'affiliate_bps': _affiliateBps
|
'affiliate_bps': _affiliateBps,
|
||||||
|
'refund_address':
|
||||||
|
isRefundAddressSupported.contains(request.fromCurrency) ? request.refundAddress : '',
|
||||||
};
|
};
|
||||||
|
|
||||||
final responseJSON = await _getSwapQuote(params);
|
final responseJSON = await _getSwapQuote(params);
|
||||||
|
@ -133,7 +134,7 @@ class ThorChainExchangeProvider extends ExchangeProvider {
|
||||||
inputAddress: inputAddress,
|
inputAddress: inputAddress,
|
||||||
createdAt: DateTime.now(),
|
createdAt: DateTime.now(),
|
||||||
amount: request.fromAmount,
|
amount: request.fromAmount,
|
||||||
state: TradeState.pending,
|
state: TradeState.notFound,
|
||||||
payoutAddress: request.toAddress,
|
payoutAddress: request.toAddress,
|
||||||
memo: memo);
|
memo: memo);
|
||||||
}
|
}
|
||||||
|
@ -146,25 +147,30 @@ class ThorChainExchangeProvider extends ExchangeProvider {
|
||||||
final response = await http.get(uri);
|
final response = await http.get(uri);
|
||||||
|
|
||||||
if (response.statusCode == 404) {
|
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) {
|
} else if (response.statusCode != 200) {
|
||||||
throw Exception('Unexpected HTTP status: ${response.statusCode}');
|
throw Exception('Unexpected HTTP status: ${response.statusCode}');
|
||||||
}
|
}
|
||||||
|
|
||||||
final responseJSON = json.decode(response.body);
|
final responseJSON = json.decode(response.body);
|
||||||
final observedTx = responseJSON['observed_tx'];
|
final Map<String, dynamic> stagesJson = responseJSON['stages'] as Map<String, dynamic>;
|
||||||
if (observedTx == null) {
|
|
||||||
throw Exception('No observed transaction found for id: $id');
|
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 fromAddress = tx['from_address'] as String? ?? '';
|
||||||
final String toAddress = tx['to_address'] as String? ?? '';
|
final String toAddress = tx['to_address'] as String? ?? '';
|
||||||
final List<dynamic> coins = tx['coins'] as List<dynamic>;
|
final List<dynamic> coins = tx['coins'] as List<dynamic>;
|
||||||
final String? memo = tx['memo'] as String?;
|
final String? memo = tx['memo'] as String?;
|
||||||
final String toAsset = memo != null ? (memo.split(':')[1]).split('.')[0] : '';
|
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<dynamic>?;
|
||||||
|
final isRefund = plannedOutTxs?.any((tx) => tx['refund'] == true) ?? false;
|
||||||
|
|
||||||
return Trade(
|
return Trade(
|
||||||
id: id,
|
id: id,
|
||||||
|
@ -174,8 +180,9 @@ class ThorChainExchangeProvider extends ExchangeProvider {
|
||||||
inputAddress: fromAddress,
|
inputAddress: fromAddress,
|
||||||
payoutAddress: toAddress,
|
payoutAddress: toAddress,
|
||||||
amount: coins.first['amount'] as String? ?? '0.0',
|
amount: coins.first['amount'] as String? ?? '0.0',
|
||||||
state: TradeState.deserialize(raw: formattedStatus),
|
state: currentState,
|
||||||
memo: memo,
|
memo: memo,
|
||||||
|
isRefund: isRefund,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,4 +207,26 @@ class ThorChainExchangeProvider extends ExchangeProvider {
|
||||||
String _doubleToThorChainString(double amount) => (amount * 1e8).toInt().toString();
|
String _doubleToThorChainString(double amount) => (amount * 1e8).toInt().toString();
|
||||||
|
|
||||||
double _thorChainAmountToDouble(String amount) => double.parse(amount) / 1e8;
|
double _thorChainAmountToDouble(String amount) => double.parse(amount) / 1e8;
|
||||||
|
|
||||||
|
TradeState? _updateStateBasedOnStages(Map<String, dynamic> 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,8 @@ class Trade extends HiveObject {
|
||||||
this.providerName,
|
this.providerName,
|
||||||
this.fromWalletAddress,
|
this.fromWalletAddress,
|
||||||
this.memo,
|
this.memo,
|
||||||
this.txId
|
this.txId,
|
||||||
|
this.isRefund,
|
||||||
}) {
|
}) {
|
||||||
if (provider != null) providerRaw = provider.raw;
|
if (provider != null) providerRaw = provider.raw;
|
||||||
|
|
||||||
|
@ -113,6 +114,9 @@ class Trade extends HiveObject {
|
||||||
@HiveField(19)
|
@HiveField(19)
|
||||||
String? txId;
|
String? txId;
|
||||||
|
|
||||||
|
@HiveField(20)
|
||||||
|
bool? isRefund;
|
||||||
|
|
||||||
static Trade fromMap(Map<String, Object?> map) {
|
static Trade fromMap(Map<String, Object?> map) {
|
||||||
return Trade(
|
return Trade(
|
||||||
id: map['id'] as String,
|
id: map['id'] as String,
|
||||||
|
@ -125,7 +129,8 @@ class Trade extends HiveObject {
|
||||||
walletId: map['wallet_id'] as String,
|
walletId: map['wallet_id'] as String,
|
||||||
fromWalletAddress: map['from_wallet_address'] as String?,
|
fromWalletAddress: map['from_wallet_address'] as String?,
|
||||||
memo: map['memo'] 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,
|
'wallet_id': walletId,
|
||||||
'from_wallet_address': fromWalletAddress,
|
'from_wallet_address': fromWalletAddress,
|
||||||
'memo': memo,
|
'memo': memo,
|
||||||
'tx_id': txId
|
'tx_id': txId,
|
||||||
|
'isRefund': isRefund
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,6 +41,8 @@ class TradeState extends EnumerableItem<String> with Serializable<String> {
|
||||||
static const success = TradeState(raw: 'success', title: 'Success');
|
static const success = TradeState(raw: 'success', title: 'Success');
|
||||||
static TradeState deserialize({required String raw}) {
|
static TradeState deserialize({required String raw}) {
|
||||||
switch (raw) {
|
switch (raw) {
|
||||||
|
case 'NOT_FOUND':
|
||||||
|
return notFound;
|
||||||
case 'pending':
|
case 'pending':
|
||||||
return pending;
|
return pending;
|
||||||
case 'confirming':
|
case 'confirming':
|
||||||
|
|
|
@ -153,6 +153,11 @@ abstract class TradeDetailsViewModelBase with Store {
|
||||||
title: S.current.track, value: trackUrl, onTap: () => _launchUrl(trackUrl)));
|
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) {
|
if (trade.provider == ExchangeProviderDescription.trocador) {
|
||||||
items.add(StandartListItem(
|
items.add(StandartListItem(
|
||||||
title: '${trade.providerName} ${S.current.id.toUpperCase()}',
|
title: '${trade.providerName} ${S.current.id.toUpperCase()}',
|
||||||
|
|
Loading…
Reference in a new issue