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,
);
break;
case 48:
_changeExchangeProviderAvailability(
sharedPreferences,
providerName: "SwapTrade",
enabled: true,
);
break;
default:
break;
}

View file

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

View file

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

View file

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

View file

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

View file

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