mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2024-11-17 01:37:40 +00:00
CWA-169 | added limits check to createTrade() and format to amounts in exchange store
This commit is contained in:
parent
348d1080c9
commit
fd964d68bf
1 changed files with 43 additions and 11 deletions
|
@ -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,8 +184,20 @@ abstract class ExchangeStoreBase with Store {
|
||||||
amount: depositAmount,
|
amount: depositAmount,
|
||||||
refundAddress: depositAddress,
|
refundAddress: depositAddress,
|
||||||
address: receiveAddress);
|
address: receiveAddress);
|
||||||
|
amount = depositAmount;
|
||||||
|
currency = depositCurrency;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (limitsState is LimitsLoadedSuccessfully) {
|
||||||
|
if (double.parse(amount) < limits.min) {
|
||||||
|
final error = "Trade for ${provider.description} is not created. "
|
||||||
|
"Amount is less then minimal: ${limits.min} ${currency.toString()}";
|
||||||
|
tradeState = TradeIsCreatedFailure(error: error);
|
||||||
|
} else if (limits.max != null && double.parse(amount) > limits.max) {
|
||||||
|
final error = "Trade for ${provider.description} is not created. "
|
||||||
|
"Amount is more then maximum: ${limits.max} ${currency.toString()}";
|
||||||
|
tradeState = TradeIsCreatedFailure(error: error);
|
||||||
|
} else {
|
||||||
try {
|
try {
|
||||||
tradeState = TradeIsCreating();
|
tradeState = TradeIsCreating();
|
||||||
final trade = await provider.createTrade(request: request);
|
final trade = await provider.createTrade(request: request);
|
||||||
|
@ -183,6 +208,13 @@ abstract class ExchangeStoreBase with Store {
|
||||||
tradeState = TradeIsCreatedFailure(error: e.toString());
|
tradeState = TradeIsCreatedFailure(error: e.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
final error = "Trade for ${provider.description} is not created. "
|
||||||
|
"Limits loading failed";
|
||||||
|
tradeState = TradeIsCreatedFailure(error: error);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
void reset() {
|
void reset() {
|
||||||
|
|
Loading…
Reference in a new issue