CW-424-ChangeNOW-add-payload-details (#979)
* add payload details * remove unused import * fix payload
Serhii committed
Jul 8, 2023 at 05:30 UTC
9afed201c6f65f21ca52bd69a4e6130220627642
5 files changed
+95
-46
lib/exchange/changenow/changenow_exchange_provider.dart
+52
-43
@@ -1,6 +1,10 @@
1
import 'dart:convert';
2
+import 'dart:io';
3
import 'package:cake_wallet/exchange/trade_not_found_exeption.dart';
4
+import 'package:cake_wallet/store/settings_store.dart';
5
import 'package:cake_wallet/utils/device_info.dart';
6
+import 'package:cake_wallet/utils/distribution_info.dart';
7
+import 'package:cake_wallet/wallet_type_utils.dart';
8
import 'package:http/http.dart';
9
import 'package:cake_wallet/.secrets.g.dart' as secrets;
10
import 'package:cw_core/crypto_currency.dart';
@@ -14,7 +18,7 @@ import 'package:cake_wallet/exchange/changenow/changenow_request.dart';
18
import 'package:cake_wallet/exchange/exchange_provider_description.dart';
19
20
class ChangeNowExchangeProvider extends ExchangeProvider {
17
- ChangeNowExchangeProvider()
21
+ ChangeNowExchangeProvider({required this.settingsStore})
22
: _lastUsedRateId = '',
23
super(
24
pairList: CryptoCurrency.all
@@ -25,7 +29,8 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
29
.expand((i) => i)
30
.toList());
31
28
- static final apiKey = DeviceInfo.instance.isMobile ? secrets.changeNowApiKey : secrets.changeNowApiKeyDesktop;
32
+ static final apiKey =
33
+ DeviceInfo.instance.isMobile ? secrets.changeNowApiKey : secrets.changeNowApiKeyDesktop;
34
static const apiAuthority = 'api.changenow.io';
35
static const createTradePath = '/v2/exchange';
36
static const findTradeByIdPath = '/v2/exchange/by-id';
@@ -46,21 +51,22 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
51
bool get supportsFixedRate => true;
52
53
@override
49
- ExchangeProviderDescription get description =>
50
- ExchangeProviderDescription.changeNow;
54
+ ExchangeProviderDescription get description => ExchangeProviderDescription.changeNow;
55
56
@override
57
Future<bool> checkIsAvailable() async => true;
58
59
+ final SettingsStore settingsStore;
60
+
61
String _lastUsedRateId;
62
63
static String getFlow(bool isFixedRate) => isFixedRate ? 'fixed-rate' : 'standard';
64
65
@override
60
- Future<Limits> fetchLimits({
61
- required CryptoCurrency from,
62
- required CryptoCurrency to,
63
- required bool isFixedRateMode}) async {
66
+ Future<Limits> fetchLimits(
67
+ {required CryptoCurrency from,
68
+ required CryptoCurrency to,
69
+ required bool isFixedRateMode}) async {
70
final headers = {apiHeaderKey: apiKey};
71
final normalizedFrom = normalizeCryptoCurrency(from);
72
final normalizedTo = normalizeCryptoCurrency(to);
@@ -70,10 +76,11 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
76
'toCurrency': normalizedTo,
77
'fromNetwork': networkFor(from),
78
'toNetwork': networkFor(to),
73
- 'flow': flow};
79
+ 'flow': flow
80
+ };
81
final uri = Uri.https(apiAuthority, rangePath, params);
82
final response = await get(uri, headers: headers);
76
-
83
+
84
if (response.statusCode == 400) {
85
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
86
final error = responseJSON['error'] as String;
@@ -87,19 +94,24 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
94
95
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
96
return Limits(
90
- min: responseJSON['minAmount'] as double?,
91
- max: responseJSON['maxAmount'] as double?);
97
+ min: responseJSON['minAmount'] as double?, max: responseJSON['maxAmount'] as double?);
98
}
99
100
@override
101
Future<Trade> createTrade({required TradeRequest request, required bool isFixedRateMode}) async {
102
final _request = request as ChangeNowRequest;
97
- final headers = {
98
- apiHeaderKey: apiKey,
99
- 'Content-Type': 'application/json'};
103
+ final distributionPath = await DistributionInfo.instance.getDistributionPath();
104
+ final formattedAppVersion = int.tryParse(settingsStore.appVersion.replaceAll('.', '')) ?? 0;
105
+ final payload = {
106
+ 'app': isMoneroOnly ? 'monerocom' : 'cakewallet',
107
+ 'device': Platform.operatingSystem,
108
+ 'distribution': distributionPath,
109
+ 'version': formattedAppVersion
110
+ };
111
+ final headers = {apiHeaderKey: apiKey, 'Content-Type': 'application/json'};
112
final flow = getFlow(isFixedRateMode);
113
final type = isFixedRateMode ? 'reverse' : 'direct';
102
- final body = <String, String>{
114
+ final body = <String, dynamic>{
115
'fromCurrency': normalizeCryptoCurrency(_request.from),
116
'toCurrency': normalizeCryptoCurrency(_request.to),
117
'fromNetwork': networkFor(_request.from),
@@ -109,7 +121,8 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
121
'address': _request.address,
122
'flow': flow,
123
'type': type,
112
- 'refundAddress': _request.refundAddress
124
+ 'refundAddress': _request.refundAddress,
125
+ 'payload': payload,
126
};
127
128
if (isFixedRateMode) {
@@ -164,7 +177,7 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
177
Future<Trade> findTradeById({required String id}) async {
178
final headers = {apiHeaderKey: apiKey};
179
final params = <String, String>{'id': id};
167
- final uri = Uri.https(apiAuthority,findTradeByIdPath, params);
180
+ final uri = Uri.https(apiAuthority, findTradeByIdPath, params);
181
final response = await get(uri, headers: headers);
182
183
if (response.statusCode == 404) {
@@ -175,8 +188,7 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
188
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
189
final error = responseJSON['message'] as String;
190
178
- throw TradeNotFoundException(id,
179
- provider: description, description: error);
191
+ throw TradeNotFoundException(id, provider: description, description: error);
192
}
193
194
if (response.statusCode != 200) {
@@ -234,19 +246,20 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
246
'fromNetwork': networkFor(from),
247
'toNetwork': networkFor(to),
248
'type': type,
237
- 'flow': flow};
249
+ 'flow': flow
250
+ };
251
252
if (isReverse) {
253
params['toAmount'] = amount.toString();
254
} else {
255
params['fromAmount'] = amount.toString();
256
}
244
-
257
+
258
final uri = Uri.https(apiAuthority, estimatedAmountPath, params);
259
final response = await get(uri, headers: headers);
260
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
261
final fromAmount = double.parse(responseJSON['fromAmount'].toString());
249
- final toAmount = double.parse(responseJSON['toAmount'].toString());
262
+ final toAmount = double.parse(responseJSON['toAmount'].toString());
263
final rateId = responseJSON['rateId'] as String? ?? '';
264
265
if (rateId.isNotEmpty) {
@@ -254,35 +267,31 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
267
}
268
269
return isReverse ? (amount / fromAmount) : (toAmount / amount);
257
- } catch(e) {
270
+ } catch (e) {
271
print(e.toString());
272
return 0.0;
273
}
274
}
262
-
275
+
276
String networkFor(CryptoCurrency currency) {
277
switch (currency) {
278
case CryptoCurrency.usdt:
279
return CryptoCurrency.btc.title.toLowerCase();
280
default:
268
- return currency.tag != null
269
- ? currency.tag!.toLowerCase()
270
- : currency.title.toLowerCase();
271
- }
281
+ return currency.tag != null ? currency.tag!.toLowerCase() : currency.title.toLowerCase();
282
}
273
-
283
}
275
-
276
- String normalizeCryptoCurrency(CryptoCurrency currency) {
277
- switch(currency) {
278
- case CryptoCurrency.zec:
279
- return 'zec';
280
- case CryptoCurrency.usdcpoly:
281
- return 'usdcmatic';
282
- case CryptoCurrency.maticpoly:
283
- return 'maticmainnet';
284
- default:
285
- return currency.title.toLowerCase();
286
- }
287
-
284
+}
285
+
286
+String normalizeCryptoCurrency(CryptoCurrency currency) {
287
+ switch (currency) {
288
+ case CryptoCurrency.zec:
289
+ return 'zec';
290
+ case CryptoCurrency.usdcpoly:
291
+ return 'usdcmatic';
292
+ case CryptoCurrency.maticpoly:
293
+ return 'maticmainnet';
294
+ default:
295
+ return currency.title.toLowerCase();
296
}
297
+}
lib/utils/distribution_info.dart
new
+39
@@ -0,0 +1,39 @@
1
+import 'dart:io';
2
+import 'package:package_info/package_info.dart';
3
+
4
+enum DistributionType { googleplay, github, appstore, fdroid }
5
+
6
+class DistributionInfo {
7
+ DistributionInfo._();
8
+
9
+ static DistributionInfo get instance => DistributionInfo._();
10
+
11
+ Future<String> getDistributionPath() async {
12
+ final isPlayStore = await isInstalledFromPlayStore();
13
+ final distributionPath = _getDistributionPath(isPlayStore);
14
+
15
+ return distributionPath.name;
16
+ }
17
+
18
+ DistributionType _getDistributionPath(bool isPlayStore) {
19
+ if (isPlayStore) {
20
+ return DistributionType.googleplay;
21
+ } else if (Platform.isAndroid) {
22
+ return DistributionType.github;
23
+ } else if (Platform.isIOS) {
24
+ return DistributionType.appstore;
25
+ } else {
26
+ return DistributionType.github;
27
+ }
28
+ }
29
+
30
+ Future<bool> isInstalledFromPlayStore() async {
31
+ try {
32
+ final packageInfo = await PackageInfo.fromPlatform();
33
+ return packageInfo.packageName == 'com.android.vending';
34
+ } catch (e) {
35
+ print('Error: $e');
36
+ return false;
37
+ }
38
+ }
39
+}
lib/view_model/exchange/exchange_trade_view_model.dart
+2
-1
@@ -36,7 +36,8 @@ abstract class ExchangeTradeViewModelBase with Store {
36
_provider = XMRTOExchangeProvider();
37
break;
38
case ExchangeProviderDescription.changeNow:
39
- _provider = ChangeNowExchangeProvider();
39
+ _provider =
40
+ ChangeNowExchangeProvider(settingsStore: sendViewModel.balanceViewModel.settingsStore);
41
break;
42
case ExchangeProviderDescription.morphToken:
43
_provider = MorphTokenExchangeProvider(trades: trades);
lib/view_model/exchange/exchange_view_model.dart
+1
-1
@@ -130,7 +130,7 @@ abstract class ExchangeViewModelBase with Store {
130
final SharedPreferences sharedPreferences;
131
132
List<ExchangeProvider> get _allProviders => [
133
- ChangeNowExchangeProvider(),
133
+ ChangeNowExchangeProvider(settingsStore: _settingsStore),
134
SideShiftExchangeProvider(),
135
SimpleSwapExchangeProvider(),
136
TrocadorExchangeProvider(useTorOnly: _useTorOnly),
lib/view_model/trade_details_view_model.dart
+1
-1
@@ -40,7 +40,7 @@ abstract class TradeDetailsViewModelBase with Store {
40
_provider = XMRTOExchangeProvider();
41
break;
42
case ExchangeProviderDescription.changeNow:
43
- _provider = ChangeNowExchangeProvider();
43
+ _provider = ChangeNowExchangeProvider(settingsStore: settingsStore);
44
break;
45
case ExchangeProviderDescription.morphToken:
46
_provider = MorphTokenExchangeProvider(trades: trades);