Merge pull request #31 from cake-tech/CWA-169-add-morph-exchange-to-the-app

Cwa 169 add morph exchange to the app
This commit is contained in:
M
2020-02-12 18:20:56 +02:00
39 changed files with 553 additions and 24 deletions
@@ -11,6 +11,7 @@ abstract class TradeFilterStoreBase with Store {
TradeFilterStoreBase(
{this.displayXMRTO = true,
this.displayChangeNow = true,
this.displayMorphToken = true,
this.walletStore});
@observable
@@ -19,6 +20,9 @@ abstract class TradeFilterStoreBase with Store {
@observable
bool displayChangeNow;
@observable
bool displayMorphToken;
WalletStore walletStore;
@action
@@ -30,13 +34,16 @@ abstract class TradeFilterStoreBase with Store {
case ExchangeProviderDescription.xmrto:
displayXMRTO = !displayXMRTO;
break;
case ExchangeProviderDescription.morphToken:
displayMorphToken = !displayMorphToken;
break;
}
}
List<TradeListItem> filtered({List<TradeListItem> trades}) {
final _trades =
trades.where((item) => item.trade.walletId == walletStore.id).toList();
final needToFilter = !displayChangeNow || !displayXMRTO;
final needToFilter = !displayChangeNow || !displayXMRTO || !displayMorphToken;
return needToFilter
? trades
@@ -45,7 +52,10 @@ abstract class TradeFilterStoreBase with Store {
item.trade.provider == ExchangeProviderDescription.xmrto) ||
(displayChangeNow &&
item.trade.provider ==
ExchangeProviderDescription.changeNow))
ExchangeProviderDescription.changeNow) ||
(displayMorphToken &&
item.trade.provider ==
ExchangeProviderDescription.morphToken))
.toList()
: _trades;
}
+52 -11
View File
@@ -8,11 +8,15 @@ import 'package:cake_wallet/src/domain/exchange/exchange_provider.dart';
import 'package:cake_wallet/src/domain/exchange/trade_request.dart';
import 'package:cake_wallet/src/domain/exchange/xmrto/xmrto_exchange_provider.dart';
import 'package:cake_wallet/src/domain/exchange/xmrto/xmrto_trade_request.dart';
import 'package:cake_wallet/src/domain/exchange/morphtoken/morphtoken_exchange_provider.dart';
import 'package:cake_wallet/src/domain/exchange/morphtoken/morphtoken_request.dart';
import 'package:cake_wallet/src/domain/exchange/trade.dart';
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/limits_state.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';
@@ -31,6 +35,7 @@ abstract class ExchangeStoreBase with Store {
receiveCurrency = initialReceiveCurrency;
limitsState = LimitsInitialState();
tradeState = ExchangeTradeStateInitial();
_cryptoNumberFormat = NumberFormat()..maximumFractionDigits = 12;
loadLimits();
}
@@ -72,11 +77,16 @@ abstract class ExchangeStoreBase with Store {
WalletStore walletStore;
Limits limits;
NumberFormat _cryptoNumberFormat;
@action
void changeProvider({ExchangeProvider provider}) {
this.provider = provider;
depositAmount = '';
receiveAmount = '';
loadLimits();
}
@action
@@ -105,7 +115,7 @@ abstract class ExchangeStoreBase with Store {
provider
.calculateAmount(
from: depositCurrency, to: receiveCurrency, amount: _amount)
.then((amount) => amount.toString())
.then((amount) => _cryptoNumberFormat.format(amount).toString().replaceAll(RegExp("\\,"), ""))
.then((amount) => depositAmount = amount);
}
@@ -122,7 +132,7 @@ abstract class ExchangeStoreBase with Store {
provider
.calculateAmount(
from: depositCurrency, to: receiveCurrency, amount: _amount)
.then((amount) => amount.toString())
.then((amount) => _cryptoNumberFormat.format(amount).toString().replaceAll(RegExp("\\,"), ""))
.then((amount) => receiveAmount = amount);
}
@@ -131,7 +141,7 @@ abstract class ExchangeStoreBase with Store {
limitsState = LimitsIsLoading();
try {
final limits = await provider.fetchLimits(
limits = await provider.fetchLimits(
from: depositCurrency, to: receiveCurrency);
limitsState = LimitsLoadedSuccessfully(limits: limits);
} catch (e) {
@@ -142,6 +152,8 @@ abstract class ExchangeStoreBase with Store {
@action
Future createTrade() async {
TradeRequest request;
String amount;
CryptoCurrency currency;
if (provider is XMRTOExchangeProvider) {
request = XMRTOTradeRequest(
@@ -150,6 +162,8 @@ abstract class ExchangeStoreBase with Store {
amount: receiveAmount,
address: receiveAddress,
refundAddress: depositAddress);
amount = receiveAmount;
currency = receiveCurrency;
}
if (provider is ChangeNowExchangeProvider) {
@@ -159,17 +173,43 @@ abstract class ExchangeStoreBase with Store {
amount: depositAmount,
refundAddress: depositAddress,
address: receiveAddress);
amount = depositAmount;
currency = depositCurrency;
}
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());
if (provider is MorphTokenExchangeProvider) {
request = MorphTokenRequest(
from: depositCurrency,
to: receiveCurrency,
amount: depositAmount,
refundAddress: depositAddress,
address: receiveAddress);
amount = depositAmount;
currency = depositCurrency;
}
if (limitsState is LimitsLoadedSuccessfully && amount != null) {
if (double.parse(amount) < limits.min) {
tradeState = TradeIsCreatedFailure(error: S.current.error_text_minimal_limit("${provider.description}",
"${limits.min}", currency.toString()));
} else if (limits.max != null && double.parse(amount) > limits.max) {
tradeState = TradeIsCreatedFailure(error: S.current.error_text_maximum_limit("${provider.description}",
"${limits.max}", currency.toString()));
} 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 {
tradeState = TradeIsCreatedFailure(error: S.current.error_text_limits_loading_failed("${provider.description}"));
}
}
@action
@@ -181,6 +221,7 @@ abstract class ExchangeStoreBase with Store {
provider = XMRTOExchangeProvider();
depositCurrency = CryptoCurrency.xmr;
receiveCurrency = CryptoCurrency.btc;
loadLimits();
}
List<ExchangeProvider> providersForCurrentPair() {
@@ -6,7 +6,9 @@ import 'package:cake_wallet/src/domain/exchange/exchange_provider.dart';
import 'package:cake_wallet/src/domain/exchange/changenow/changenow_exchange_provider.dart';
import 'package:cake_wallet/src/domain/exchange/exchange_provider_description.dart';
import 'package:cake_wallet/src/domain/exchange/xmrto/xmrto_exchange_provider.dart';
import 'package:cake_wallet/src/domain/exchange/morphtoken/morphtoken_exchange_provider.dart';
import 'package:cake_wallet/src/stores/wallet/wallet_store.dart';
import 'package:hive/hive.dart';
part 'exchange_trade_store.g.dart';
@@ -14,7 +16,7 @@ class ExchangeTradeStore = ExchangeTradeStoreBase with _$ExchangeTradeStore;
abstract class ExchangeTradeStoreBase with Store {
ExchangeTradeStoreBase(
{@required this.trade, @required WalletStore walletStore}) {
{@required this.trade, @required WalletStore walletStore, @required this.trades}) {
isSendable = trade.from == walletStore.type ||
trade.provider == ExchangeProviderDescription.xmrto;
@@ -25,6 +27,9 @@ abstract class ExchangeTradeStoreBase with Store {
case ExchangeProviderDescription.changeNow:
_provider = ChangeNowExchangeProvider();
break;
case ExchangeProviderDescription.morphToken:
_provider = MorphTokenExchangeProvider(trades: trades);
break;
}
_updateTrade();
@@ -37,6 +42,8 @@ abstract class ExchangeTradeStoreBase with Store {
@observable
bool isSendable;
Box<Trade> trades;
ExchangeProvider _provider;
Timer _timer;