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>

Serhii committed Mar 6, 2025 at 20:00 UTC c2996ac303ee34de675c70e4612081760186ffa1
6 files changed +55 -21
lib/entities/default_settings_migration.dart
+7
@@ -401,6 +401,13 @@ Future<void> defaultSettingsMigration(
401 enabled: false,
402 );
403 break;
404 + case 48:
405 + _changeExchangeProviderAvailability(
406 + sharedPreferences,
407 + providerName: "SwapTrade",
408 + enabled: true,
409 + );
410 + break;
411 default:
412 break;
413 }
lib/exchange/provider/swaptrade_exchange_provider.dart
+27 -17
@@ -26,9 +26,11 @@ class SwapTradeExchangeProvider extends ExchangeProvider {
26 CryptoCurrency.ltc,
27 CryptoCurrency.ada,
28 CryptoCurrency.bch,
29 - CryptoCurrency.usdt,
29 + CryptoCurrency.usdterc20,
30 + CryptoCurrency.usdttrc20,
31 CryptoCurrency.bnb,
32 CryptoCurrency.xmr,
33 + CryptoCurrency.zec,
34 ].contains(element))
35 .toList())
36 ];
@@ -39,6 +41,7 @@ class SwapTradeExchangeProvider extends ExchangeProvider {
41 static const getRate = '/api/swap/get-rate';
42 static const getCoins = '/api/swap/get-coins';
43 static const createOrder = '/api/swap/create-order';
44 + static const order = '/api/swap/order';
45
46 @override
47 String get title => 'SwapTrade';
@@ -108,6 +111,7 @@ class SwapTradeExchangeProvider extends ExchangeProvider {
111 final body = <String, String>{
112 'coin_send': _normalizeCurrency(from),
113 'coin_receive': _normalizeCurrency(to),
114 + 'amount': amount.toString(),
115 'ref': 'cake',
116 };
117
@@ -120,7 +124,7 @@ class SwapTradeExchangeProvider extends ExchangeProvider {
124
125 final data = responseBody['data'] as Map<String, dynamic>;
126 double rate = double.parse(data['price'].toString());
123 - return rate;
127 + return rate > 0 ? isFixedRateMode ? amount / rate : rate / amount : 0.0;
128 } catch (e) {
129 printV("error fetching rate: ${e.toString()}");
130 return 0.0;
@@ -138,18 +142,16 @@ class SwapTradeExchangeProvider extends ExchangeProvider {
142 final params = <String, dynamic>{};
143 var body = <String, dynamic>{
144 'coin_send': _normalizeCurrency(request.fromCurrency),
145 + 'coin_send_network': _networkFor(request.fromCurrency),
146 'coin_receive': _normalizeCurrency(request.toCurrency),
147 + 'coin_receive_network': _networkFor(request.toCurrency),
148 'amount_send': request.fromAmount,
149 'recipient': request.toAddress,
150 'ref': 'cake',
151 'markup': markup,
152 + 'refund_address': request.refundAddress,
153 };
154
148 - String? fromNetwork = _networkFor(request.fromCurrency);
149 - String? toNetwork = _networkFor(request.toCurrency);
150 - if (fromNetwork != null) body['coin_send_network'] = fromNetwork;
151 - if (toNetwork != null) body['coin_receive_network'] = toNetwork;
152 -
155 final uri = Uri.https(apiAuthority, createOrder, params);
156 final response = await post(uri, body: body, headers: headers);
157 final responseBody = json.decode(response.body) as Map<String, dynamic>;
@@ -193,7 +195,7 @@ class SwapTradeExchangeProvider extends ExchangeProvider {
195 'order_id': id,
196 };
197
196 - final uri = Uri.https(apiAuthority, createOrder, params);
198 + final uri = Uri.https(apiAuthority, order, params);
199 final response = await post(uri, body: body, headers: headers);
200 final responseBody = json.decode(response.body) as Map<String, dynamic>;
201
@@ -211,10 +213,14 @@ class SwapTradeExchangeProvider extends ExchangeProvider {
213 final toCurrency = responseData['coin_receive'] as String;
214 final to = CryptoCurrency.fromString(toCurrency);
215 final inputAddress = responseData['server_address'] as String;
216 + final payoutAddress = responseData['recipient'] as String;
217 final status = responseData['status'] as String;
218 final state = TradeState.deserialize(raw: status);
219 final response_id = responseData['order_id'] as String;
220 final expectedSendAmount = responseData['amount_send'] as String;
221 + final expectedReceiveAmount = responseData['amount_receive'] as String;
222 + final memo = responseData['memo'] as String?;
223 + final createdAt = responseData['created_at'] as String?;
224
225 return Trade(
226 id: response_id,
@@ -223,7 +229,11 @@ class SwapTradeExchangeProvider extends ExchangeProvider {
229 provider: description,
230 inputAddress: inputAddress,
231 amount: expectedSendAmount,
232 + payoutAddress: payoutAddress,
233 state: state,
234 + receiveAmount: expectedReceiveAmount,
235 + memo: memo,
236 + createdAt: DateTime.tryParse(createdAt ?? ''),
237 );
238 } catch (e) {
239 printV("error getting trade: ${e.toString()}");
@@ -242,14 +252,14 @@ class SwapTradeExchangeProvider extends ExchangeProvider {
252 }
253 }
254
245 - String? _networkFor(CryptoCurrency currency) {
246 - switch (currency) {
247 - case CryptoCurrency.usdt:
248 - return "USDT_ERC20";
249 - case CryptoCurrency.bnb:
250 - return "BNB_BSC";
251 - default:
252 - return null;
253 - }
255 + String _networkFor(CryptoCurrency currency) {
256 + final network = switch (currency) {
257 + CryptoCurrency.eth => 'ETH',
258 + CryptoCurrency.bnb => 'BNB_BSC',
259 + CryptoCurrency.usdterc20 => 'USDT_ERC20',
260 + CryptoCurrency.usdttrc20 => 'TRX_USDT_S2UZ',
261 + _ => '',
262 + };
263 + return network;
264 }
265 }
lib/exchange/trade_state.dart
+2
@@ -131,6 +131,8 @@ class TradeState extends EnumerableItem<String> with Serializable<String> {
131 case 'success':
132 case 'done':
133 return success;
134 + case 'expired':
135 + return expired;
136 default:
137 throw Exception('Unexpected token: $raw in TradeState deserialize');
138 }
lib/main.dart
+1 -1
@@ -215,7 +215,7 @@ Future<void> initializeAppConfigs() async {
215 secureStorage: secureStorage,
216 anonpayInvoiceInfo: anonpayInvoiceInfo,
217 havenSeedStore: havenSeedStore,
218 - initialMigrationVersion: 47,
218 + initialMigrationVersion: 48,
219 );
220 }
221
lib/store/dashboard/trade_filter_store.dart
+13 -3
@@ -19,7 +19,8 @@ abstract class TradeFilterStoreBase with Store {
19 displayChainflip = true,
20 displayThorChain = true,
21 displayLetsExchange = true,
22 - displayStealthEx = true;
22 + displayStealthEx = true,
23 + displaySwapTrade = true;
24
25 @observable
26 bool displayXMRTO;
@@ -54,6 +55,9 @@ abstract class TradeFilterStoreBase with Store {
55 @observable
56 bool displayStealthEx;
57
58 + @observable
59 + bool displaySwapTrade;
60 +
61 @computed
62 bool get displayAllTrades =>
63 displayChangeNow &&
@@ -64,7 +68,8 @@ abstract class TradeFilterStoreBase with Store {
68 displayChainflip &&
69 displayThorChain &&
70 displayLetsExchange &&
67 - displayStealthEx;
71 + displayStealthEx &&
72 + displaySwapTrade;
73
74 @action
75 void toggleDisplayExchange(ExchangeProviderDescription provider) {
@@ -102,6 +107,8 @@ abstract class TradeFilterStoreBase with Store {
107 case ExchangeProviderDescription.stealthEx:
108 displayStealthEx = !displayStealthEx;
109 break;
110 + case ExchangeProviderDescription.swapTrade:
111 + displaySwapTrade = !displaySwapTrade;
112 case ExchangeProviderDescription.all:
113 if (displayAllTrades) {
114 displayChangeNow = false;
@@ -115,6 +122,7 @@ abstract class TradeFilterStoreBase with Store {
122 displayThorChain = false;
123 displayLetsExchange = false;
124 displayStealthEx = false;
125 + displaySwapTrade = false;
126 } else {
127 displayChangeNow = true;
128 displaySideShift = true;
@@ -127,6 +135,7 @@ abstract class TradeFilterStoreBase with Store {
135 displayThorChain = true;
136 displayLetsExchange = true;
137 displayStealthEx = true;
138 + displaySwapTrade = true;
139 }
140 break;
141 }
@@ -158,7 +167,8 @@ abstract class TradeFilterStoreBase with Store {
167 item.trade.provider == ExchangeProviderDescription.thorChain) ||
168 (displayLetsExchange &&
169 item.trade.provider == ExchangeProviderDescription.letsExchange) ||
161 - (displayStealthEx && item.trade.provider == ExchangeProviderDescription.stealthEx))
170 + (displayStealthEx && item.trade.provider == ExchangeProviderDescription.stealthEx) ||
171 + (displaySwapTrade && item.trade.provider == ExchangeProviderDescription.swapTrade))
172 .toList()
173 : _trades;
174 }
lib/view_model/dashboard/dashboard_view_model.dart
+5
@@ -152,6 +152,11 @@ abstract class DashboardViewModelBase with Store {
152 caption: ExchangeProviderDescription.stealthEx.title,
153 onChanged: () =>
154 tradeFilterStore.toggleDisplayExchange(ExchangeProviderDescription.stealthEx)),
155 + FilterItem(
156 + value: () => tradeFilterStore.displaySwapTrade,
157 + caption: ExchangeProviderDescription.swapTrade.title,
158 + onChanged: () => tradeFilterStore
159 + .toggleDisplayExchange(ExchangeProviderDescription.swapTrade)),
160 ]
161 },
162 subname = '',