CW-983-SwapTrade-enhancements (#2057)

* Update swaptrade_exchange_provider.dart

* fix network and trade state issues

* Update trade_filter_store.dart

* hopefully final changes :3

* re-enable swaptrade [skip ci]

* fix rate calculation issue

* Add refund address

---------

Co-authored-by: OmarHatem <omarh.ismail1@gmail.com>
This commit is contained in:
Serhii 2025-03-06 20:00:10 +02:00 committed by GitHub
parent 1aad8366c4
commit c2996ac303
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 55 additions and 21 deletions

View file

@ -401,6 +401,13 @@ Future<void> defaultSettingsMigration(
enabled: false, enabled: false,
); );
break; break;
case 48:
_changeExchangeProviderAvailability(
sharedPreferences,
providerName: "SwapTrade",
enabled: true,
);
break;
default: default:
break; break;
} }

View file

@ -26,9 +26,11 @@ class SwapTradeExchangeProvider extends ExchangeProvider {
CryptoCurrency.ltc, CryptoCurrency.ltc,
CryptoCurrency.ada, CryptoCurrency.ada,
CryptoCurrency.bch, CryptoCurrency.bch,
CryptoCurrency.usdt, CryptoCurrency.usdterc20,
CryptoCurrency.usdttrc20,
CryptoCurrency.bnb, CryptoCurrency.bnb,
CryptoCurrency.xmr, CryptoCurrency.xmr,
CryptoCurrency.zec,
].contains(element)) ].contains(element))
.toList()) .toList())
]; ];
@ -39,6 +41,7 @@ class SwapTradeExchangeProvider extends ExchangeProvider {
static const getRate = '/api/swap/get-rate'; static const getRate = '/api/swap/get-rate';
static const getCoins = '/api/swap/get-coins'; static const getCoins = '/api/swap/get-coins';
static const createOrder = '/api/swap/create-order'; static const createOrder = '/api/swap/create-order';
static const order = '/api/swap/order';
@override @override
String get title => 'SwapTrade'; String get title => 'SwapTrade';
@ -108,6 +111,7 @@ class SwapTradeExchangeProvider extends ExchangeProvider {
final body = <String, String>{ final body = <String, String>{
'coin_send': _normalizeCurrency(from), 'coin_send': _normalizeCurrency(from),
'coin_receive': _normalizeCurrency(to), 'coin_receive': _normalizeCurrency(to),
'amount': amount.toString(),
'ref': 'cake', 'ref': 'cake',
}; };
@ -120,7 +124,7 @@ class SwapTradeExchangeProvider extends ExchangeProvider {
final data = responseBody['data'] as Map<String, dynamic>; final data = responseBody['data'] as Map<String, dynamic>;
double rate = double.parse(data['price'].toString()); double rate = double.parse(data['price'].toString());
return rate; return rate > 0 ? isFixedRateMode ? amount / rate : rate / amount : 0.0;
} catch (e) { } catch (e) {
printV("error fetching rate: ${e.toString()}"); printV("error fetching rate: ${e.toString()}");
return 0.0; return 0.0;
@ -138,18 +142,16 @@ class SwapTradeExchangeProvider extends ExchangeProvider {
final params = <String, dynamic>{}; final params = <String, dynamic>{};
var body = <String, dynamic>{ var body = <String, dynamic>{
'coin_send': _normalizeCurrency(request.fromCurrency), 'coin_send': _normalizeCurrency(request.fromCurrency),
'coin_send_network': _networkFor(request.fromCurrency),
'coin_receive': _normalizeCurrency(request.toCurrency), 'coin_receive': _normalizeCurrency(request.toCurrency),
'coin_receive_network': _networkFor(request.toCurrency),
'amount_send': request.fromAmount, 'amount_send': request.fromAmount,
'recipient': request.toAddress, 'recipient': request.toAddress,
'ref': 'cake', 'ref': 'cake',
'markup': markup, 'markup': markup,
'refund_address': request.refundAddress,
}; };
String? fromNetwork = _networkFor(request.fromCurrency);
String? toNetwork = _networkFor(request.toCurrency);
if (fromNetwork != null) body['coin_send_network'] = fromNetwork;
if (toNetwork != null) body['coin_receive_network'] = toNetwork;
final uri = Uri.https(apiAuthority, createOrder, params); final uri = Uri.https(apiAuthority, createOrder, params);
final response = await post(uri, body: body, headers: headers); final response = await post(uri, body: body, headers: headers);
final responseBody = json.decode(response.body) as Map<String, dynamic>; final responseBody = json.decode(response.body) as Map<String, dynamic>;
@ -193,7 +195,7 @@ class SwapTradeExchangeProvider extends ExchangeProvider {
'order_id': id, 'order_id': id,
}; };
final uri = Uri.https(apiAuthority, createOrder, params); final uri = Uri.https(apiAuthority, order, params);
final response = await post(uri, body: body, headers: headers); final response = await post(uri, body: body, headers: headers);
final responseBody = json.decode(response.body) as Map<String, dynamic>; final responseBody = json.decode(response.body) as Map<String, dynamic>;
@ -211,10 +213,14 @@ class SwapTradeExchangeProvider extends ExchangeProvider {
final toCurrency = responseData['coin_receive'] as String; final toCurrency = responseData['coin_receive'] as String;
final to = CryptoCurrency.fromString(toCurrency); final to = CryptoCurrency.fromString(toCurrency);
final inputAddress = responseData['server_address'] as String; final inputAddress = responseData['server_address'] as String;
final payoutAddress = responseData['recipient'] as String;
final status = responseData['status'] as String; final status = responseData['status'] as String;
final state = TradeState.deserialize(raw: status); final state = TradeState.deserialize(raw: status);
final response_id = responseData['order_id'] as String; final response_id = responseData['order_id'] as String;
final expectedSendAmount = responseData['amount_send'] as String; final expectedSendAmount = responseData['amount_send'] as String;
final expectedReceiveAmount = responseData['amount_receive'] as String;
final memo = responseData['memo'] as String?;
final createdAt = responseData['created_at'] as String?;
return Trade( return Trade(
id: response_id, id: response_id,
@ -223,7 +229,11 @@ class SwapTradeExchangeProvider extends ExchangeProvider {
provider: description, provider: description,
inputAddress: inputAddress, inputAddress: inputAddress,
amount: expectedSendAmount, amount: expectedSendAmount,
payoutAddress: payoutAddress,
state: state, state: state,
receiveAmount: expectedReceiveAmount,
memo: memo,
createdAt: DateTime.tryParse(createdAt ?? ''),
); );
} catch (e) { } catch (e) {
printV("error getting trade: ${e.toString()}"); printV("error getting trade: ${e.toString()}");
@ -242,14 +252,14 @@ class SwapTradeExchangeProvider extends ExchangeProvider {
} }
} }
String? _networkFor(CryptoCurrency currency) { String _networkFor(CryptoCurrency currency) {
switch (currency) { final network = switch (currency) {
case CryptoCurrency.usdt: CryptoCurrency.eth => 'ETH',
return "USDT_ERC20"; CryptoCurrency.bnb => 'BNB_BSC',
case CryptoCurrency.bnb: CryptoCurrency.usdterc20 => 'USDT_ERC20',
return "BNB_BSC"; CryptoCurrency.usdttrc20 => 'TRX_USDT_S2UZ',
default: _ => '',
return null; };
} return network;
} }
} }

View file

@ -131,6 +131,8 @@ class TradeState extends EnumerableItem<String> with Serializable<String> {
case 'success': case 'success':
case 'done': case 'done':
return success; return success;
case 'expired':
return expired;
default: default:
throw Exception('Unexpected token: $raw in TradeState deserialize'); throw Exception('Unexpected token: $raw in TradeState deserialize');
} }

View file

@ -215,7 +215,7 @@ Future<void> initializeAppConfigs() async {
secureStorage: secureStorage, secureStorage: secureStorage,
anonpayInvoiceInfo: anonpayInvoiceInfo, anonpayInvoiceInfo: anonpayInvoiceInfo,
havenSeedStore: havenSeedStore, havenSeedStore: havenSeedStore,
initialMigrationVersion: 47, initialMigrationVersion: 48,
); );
} }

View file

@ -19,7 +19,8 @@ abstract class TradeFilterStoreBase with Store {
displayChainflip = true, displayChainflip = true,
displayThorChain = true, displayThorChain = true,
displayLetsExchange = true, displayLetsExchange = true,
displayStealthEx = true; displayStealthEx = true,
displaySwapTrade = true;
@observable @observable
bool displayXMRTO; bool displayXMRTO;
@ -54,6 +55,9 @@ abstract class TradeFilterStoreBase with Store {
@observable @observable
bool displayStealthEx; bool displayStealthEx;
@observable
bool displaySwapTrade;
@computed @computed
bool get displayAllTrades => bool get displayAllTrades =>
displayChangeNow && displayChangeNow &&
@ -64,7 +68,8 @@ abstract class TradeFilterStoreBase with Store {
displayChainflip && displayChainflip &&
displayThorChain && displayThorChain &&
displayLetsExchange && displayLetsExchange &&
displayStealthEx; displayStealthEx &&
displaySwapTrade;
@action @action
void toggleDisplayExchange(ExchangeProviderDescription provider) { void toggleDisplayExchange(ExchangeProviderDescription provider) {
@ -102,6 +107,8 @@ abstract class TradeFilterStoreBase with Store {
case ExchangeProviderDescription.stealthEx: case ExchangeProviderDescription.stealthEx:
displayStealthEx = !displayStealthEx; displayStealthEx = !displayStealthEx;
break; break;
case ExchangeProviderDescription.swapTrade:
displaySwapTrade = !displaySwapTrade;
case ExchangeProviderDescription.all: case ExchangeProviderDescription.all:
if (displayAllTrades) { if (displayAllTrades) {
displayChangeNow = false; displayChangeNow = false;
@ -115,6 +122,7 @@ abstract class TradeFilterStoreBase with Store {
displayThorChain = false; displayThorChain = false;
displayLetsExchange = false; displayLetsExchange = false;
displayStealthEx = false; displayStealthEx = false;
displaySwapTrade = false;
} else { } else {
displayChangeNow = true; displayChangeNow = true;
displaySideShift = true; displaySideShift = true;
@ -127,6 +135,7 @@ abstract class TradeFilterStoreBase with Store {
displayThorChain = true; displayThorChain = true;
displayLetsExchange = true; displayLetsExchange = true;
displayStealthEx = true; displayStealthEx = true;
displaySwapTrade = true;
} }
break; break;
} }
@ -158,7 +167,8 @@ abstract class TradeFilterStoreBase with Store {
item.trade.provider == ExchangeProviderDescription.thorChain) || item.trade.provider == ExchangeProviderDescription.thorChain) ||
(displayLetsExchange && (displayLetsExchange &&
item.trade.provider == ExchangeProviderDescription.letsExchange) || item.trade.provider == ExchangeProviderDescription.letsExchange) ||
(displayStealthEx && item.trade.provider == ExchangeProviderDescription.stealthEx)) (displayStealthEx && item.trade.provider == ExchangeProviderDescription.stealthEx) ||
(displaySwapTrade && item.trade.provider == ExchangeProviderDescription.swapTrade))
.toList() .toList()
: _trades; : _trades;
} }

View file

@ -152,6 +152,11 @@ abstract class DashboardViewModelBase with Store {
caption: ExchangeProviderDescription.stealthEx.title, caption: ExchangeProviderDescription.stealthEx.title,
onChanged: () => onChanged: () =>
tradeFilterStore.toggleDisplayExchange(ExchangeProviderDescription.stealthEx)), tradeFilterStore.toggleDisplayExchange(ExchangeProviderDescription.stealthEx)),
FilterItem(
value: () => tradeFilterStore.displaySwapTrade,
caption: ExchangeProviderDescription.swapTrade.title,
onChanged: () => tradeFilterStore
.toggleDisplayExchange(ExchangeProviderDescription.swapTrade)),
] ]
}, },
subname = '', subname = '',