CWA-169 | added limits check to createTrade() and format to amounts in exchange store
Oleksandr Sobol committed
Feb 7, 2020 at 00:47 UTC
fd964d68bf6dc0b30a525b2fd49014bfe768b7b0
1 file changed
+43
-11
lib/src/stores/exchange/exchange_store.dart
+43
-11
@@ -15,6 +15,8 @@ import 'package:cake_wallet/src/stores/wallet/wallet_store.dart';
15
import 'package:cake_wallet/src/stores/exchange/exchange_trade_state.dart';
16
import 'package:cake_wallet/src/stores/exchange/limits_state.dart';
17
import 'package:cake_wallet/generated/i18n.dart';
18
+import 'package:cake_wallet/src/domain/exchange/limits.dart';
19
+import 'package:intl/intl.dart';
20
21
part 'exchange_store.g.dart';
22
@@ -33,6 +35,7 @@ abstract class ExchangeStoreBase with Store {
35
receiveCurrency = initialReceiveCurrency;
36
limitsState = LimitsInitialState();
37
tradeState = ExchangeTradeStateInitial();
38
+ _cryptoNumberFormat = NumberFormat()..maximumFractionDigits = 12;
39
loadLimits();
40
}
41
@@ -74,6 +77,10 @@ abstract class ExchangeStoreBase with Store {
77
78
WalletStore walletStore;
79
80
+ Limits limits;
81
+
82
+ NumberFormat _cryptoNumberFormat;
83
+
84
@action
85
void changeProvider({ExchangeProvider provider}) {
86
this.provider = provider;
@@ -108,7 +115,7 @@ abstract class ExchangeStoreBase with Store {
115
provider
116
.calculateAmount(
117
from: depositCurrency, to: receiveCurrency, amount: _amount)
111
- .then((amount) => amount.toString())
118
+ .then((amount) => _cryptoNumberFormat.format(amount).toString().replaceAll(RegExp("\\,"), ""))
119
.then((amount) => depositAmount = amount);
120
}
121
@@ -125,7 +132,7 @@ abstract class ExchangeStoreBase with Store {
132
provider
133
.calculateAmount(
134
from: depositCurrency, to: receiveCurrency, amount: _amount)
128
- .then((amount) => amount.toString())
135
+ .then((amount) => _cryptoNumberFormat.format(amount).toString().replaceAll(RegExp("\\,"), ""))
136
.then((amount) => receiveAmount = amount);
137
}
138
@@ -134,7 +141,7 @@ abstract class ExchangeStoreBase with Store {
141
limitsState = LimitsIsLoading();
142
143
try {
137
- final limits = await provider.fetchLimits(
144
+ limits = await provider.fetchLimits(
145
from: depositCurrency, to: receiveCurrency);
146
limitsState = LimitsLoadedSuccessfully(limits: limits);
147
} catch (e) {
@@ -145,6 +152,8 @@ abstract class ExchangeStoreBase with Store {
152
@action
153
Future createTrade() async {
154
TradeRequest request;
155
+ String amount;
156
+ CryptoCurrency currency;
157
158
if (provider is XMRTOExchangeProvider) {
159
request = XMRTOTradeRequest(
@@ -153,6 +162,8 @@ abstract class ExchangeStoreBase with Store {
162
amount: receiveAmount,
163
address: receiveAddress,
164
refundAddress: depositAddress);
165
+ amount = receiveAmount;
166
+ currency = receiveCurrency;
167
}
168
169
if (provider is ChangeNowExchangeProvider) {
@@ -162,6 +173,8 @@ abstract class ExchangeStoreBase with Store {
173
amount: depositAmount,
174
refundAddress: depositAddress,
175
address: receiveAddress);
176
+ amount = depositAmount;
177
+ currency = depositCurrency;
178
}
179
180
if (provider is MorphTokenExchangeProvider) {
@@ -171,17 +184,36 @@ abstract class ExchangeStoreBase with Store {
184
amount: depositAmount,
185
refundAddress: depositAddress,
186
address: receiveAddress);
187
+ amount = depositAmount;
188
+ currency = depositCurrency;
189
}
190
176
- try {
177
- tradeState = TradeIsCreating();
178
- final trade = await provider.createTrade(request: request);
179
- trade.walletId = walletStore.id;
180
- await trades.add(trade);
181
- tradeState = TradeIsCreatedSuccessfully(trade: trade);
182
- } catch (e) {
183
- tradeState = TradeIsCreatedFailure(error: e.toString());
191
+ if (limitsState is LimitsLoadedSuccessfully) {
192
+ if (double.parse(amount) < limits.min) {
193
+ final error = "Trade for ${provider.description} is not created. "
194
+ "Amount is less then minimal: ${limits.min} ${currency.toString()}";
195
+ tradeState = TradeIsCreatedFailure(error: error);
196
+ } else if (limits.max != null && double.parse(amount) > limits.max) {
197
+ final error = "Trade for ${provider.description} is not created. "
198
+ "Amount is more then maximum: ${limits.max} ${currency.toString()}";
199
+ tradeState = TradeIsCreatedFailure(error: error);
200
+ } else {
201
+ try {
202
+ tradeState = TradeIsCreating();
203
+ final trade = await provider.createTrade(request: request);
204
+ trade.walletId = walletStore.id;
205
+ await trades.add(trade);
206
+ tradeState = TradeIsCreatedSuccessfully(trade: trade);
207
+ } catch (e) {
208
+ tradeState = TradeIsCreatedFailure(error: e.toString());
209
+ }
210
+ }
211
+ } else {
212
+ final error = "Trade for ${provider.description} is not created. "
213
+ "Limits loading failed";
214
+ tradeState = TradeIsCreatedFailure(error: error);
215
}
216
+
217
}
218
219
@action