CWA-169 | added limits check to createTrade() and format to amounts in exchange store

This commit is contained in:
Oleksandr Sobol 2020-02-07 00:47:21 +02:00
parent 348d1080c9
commit fd964d68bf

View file

@ -15,6 +15,8 @@ import 'package:cake_wallet/src/stores/wallet/wallet_store.dart';
import 'package:cake_wallet/src/stores/exchange/exchange_trade_state.dart'; import 'package:cake_wallet/src/stores/exchange/exchange_trade_state.dart';
import 'package:cake_wallet/src/stores/exchange/limits_state.dart'; import 'package:cake_wallet/src/stores/exchange/limits_state.dart';
import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/src/domain/exchange/limits.dart';
import 'package:intl/intl.dart';
part 'exchange_store.g.dart'; part 'exchange_store.g.dart';
@ -33,6 +35,7 @@ abstract class ExchangeStoreBase with Store {
receiveCurrency = initialReceiveCurrency; receiveCurrency = initialReceiveCurrency;
limitsState = LimitsInitialState(); limitsState = LimitsInitialState();
tradeState = ExchangeTradeStateInitial(); tradeState = ExchangeTradeStateInitial();
_cryptoNumberFormat = NumberFormat()..maximumFractionDigits = 12;
loadLimits(); loadLimits();
} }
@ -74,6 +77,10 @@ abstract class ExchangeStoreBase with Store {
WalletStore walletStore; WalletStore walletStore;
Limits limits;
NumberFormat _cryptoNumberFormat;
@action @action
void changeProvider({ExchangeProvider provider}) { void changeProvider({ExchangeProvider provider}) {
this.provider = provider; this.provider = provider;
@ -108,7 +115,7 @@ abstract class ExchangeStoreBase with Store {
provider provider
.calculateAmount( .calculateAmount(
from: depositCurrency, to: receiveCurrency, amount: _amount) from: depositCurrency, to: receiveCurrency, amount: _amount)
.then((amount) => amount.toString()) .then((amount) => _cryptoNumberFormat.format(amount).toString().replaceAll(RegExp("\\,"), ""))
.then((amount) => depositAmount = amount); .then((amount) => depositAmount = amount);
} }
@ -125,7 +132,7 @@ abstract class ExchangeStoreBase with Store {
provider provider
.calculateAmount( .calculateAmount(
from: depositCurrency, to: receiveCurrency, amount: _amount) from: depositCurrency, to: receiveCurrency, amount: _amount)
.then((amount) => amount.toString()) .then((amount) => _cryptoNumberFormat.format(amount).toString().replaceAll(RegExp("\\,"), ""))
.then((amount) => receiveAmount = amount); .then((amount) => receiveAmount = amount);
} }
@ -134,7 +141,7 @@ abstract class ExchangeStoreBase with Store {
limitsState = LimitsIsLoading(); limitsState = LimitsIsLoading();
try { try {
final limits = await provider.fetchLimits( limits = await provider.fetchLimits(
from: depositCurrency, to: receiveCurrency); from: depositCurrency, to: receiveCurrency);
limitsState = LimitsLoadedSuccessfully(limits: limits); limitsState = LimitsLoadedSuccessfully(limits: limits);
} catch (e) { } catch (e) {
@ -145,6 +152,8 @@ abstract class ExchangeStoreBase with Store {
@action @action
Future createTrade() async { Future createTrade() async {
TradeRequest request; TradeRequest request;
String amount;
CryptoCurrency currency;
if (provider is XMRTOExchangeProvider) { if (provider is XMRTOExchangeProvider) {
request = XMRTOTradeRequest( request = XMRTOTradeRequest(
@ -153,6 +162,8 @@ abstract class ExchangeStoreBase with Store {
amount: receiveAmount, amount: receiveAmount,
address: receiveAddress, address: receiveAddress,
refundAddress: depositAddress); refundAddress: depositAddress);
amount = receiveAmount;
currency = receiveCurrency;
} }
if (provider is ChangeNowExchangeProvider) { if (provider is ChangeNowExchangeProvider) {
@ -162,6 +173,8 @@ abstract class ExchangeStoreBase with Store {
amount: depositAmount, amount: depositAmount,
refundAddress: depositAddress, refundAddress: depositAddress,
address: receiveAddress); address: receiveAddress);
amount = depositAmount;
currency = depositCurrency;
} }
if (provider is MorphTokenExchangeProvider) { if (provider is MorphTokenExchangeProvider) {
@ -171,17 +184,36 @@ abstract class ExchangeStoreBase with Store {
amount: depositAmount, amount: depositAmount,
refundAddress: depositAddress, refundAddress: depositAddress,
address: receiveAddress); address: receiveAddress);
amount = depositAmount;
currency = depositCurrency;
} }
try { if (limitsState is LimitsLoadedSuccessfully) {
tradeState = TradeIsCreating(); if (double.parse(amount) < limits.min) {
final trade = await provider.createTrade(request: request); final error = "Trade for ${provider.description} is not created. "
trade.walletId = walletStore.id; "Amount is less then minimal: ${limits.min} ${currency.toString()}";
await trades.add(trade); tradeState = TradeIsCreatedFailure(error: error);
tradeState = TradeIsCreatedSuccessfully(trade: trade); } else if (limits.max != null && double.parse(amount) > limits.max) {
} catch (e) { final error = "Trade for ${provider.description} is not created. "
tradeState = TradeIsCreatedFailure(error: e.toString()); "Amount is more then maximum: ${limits.max} ${currency.toString()}";
tradeState = TradeIsCreatedFailure(error: error);
} else {
try {
tradeState = TradeIsCreating();
final trade = await provider.createTrade(request: request);
trade.walletId = walletStore.id;
await trades.add(trade);
tradeState = TradeIsCreatedSuccessfully(trade: trade);
} catch (e) {
tradeState = TradeIsCreatedFailure(error: e.toString());
}
}
} else {
final error = "Trade for ${provider.description} is not created. "
"Limits loading failed";
tradeState = TradeIsCreatedFailure(error: error);
} }
} }
@action @action