Cw 220 fix grey exchange screen (#641)

* Fix nullability issue for ChangeNow rateId

* Handle concurrent modification error from Splay Tree
This commit is contained in:
Omar Hatem 2022-11-22 03:52:03 +02:00 committed by GitHub
parent 3d836d5dce
commit bcf09b048c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,7 +7,6 @@ import 'package:cake_wallet/exchange/sideshift/sideshift_exchange_provider.dart'
import 'package:cake_wallet/exchange/sideshift/sideshift_request.dart'; import 'package:cake_wallet/exchange/sideshift/sideshift_request.dart';
import 'package:cake_wallet/exchange/simpleswap/simpleswap_exchange_provider.dart'; import 'package:cake_wallet/exchange/simpleswap/simpleswap_exchange_provider.dart';
import 'package:cake_wallet/view_model/settings/settings_view_model.dart'; import 'package:cake_wallet/view_model/settings/settings_view_model.dart';
import 'package:cw_core/transaction_priority.dart';
import 'package:cake_wallet/exchange/simpleswap/simpleswap_request.dart'; import 'package:cake_wallet/exchange/simpleswap/simpleswap_request.dart';
import 'package:cw_core/wallet_base.dart'; import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/crypto_currency.dart'; import 'package:cw_core/crypto_currency.dart';
@ -375,96 +374,105 @@ abstract class ExchangeViewModelBase with Store {
TradeRequest? request; TradeRequest? request;
String amount = ''; String amount = '';
for (var provider in _sortedAvailableProviders.values) { try {
if (!(await provider.checkIsAvailable())) { for (var provider in _sortedAvailableProviders.values) {
continue; if (!(await provider.checkIsAvailable())) {
} continue;
}
if (provider is SideShiftExchangeProvider) { if (provider is SideShiftExchangeProvider) {
request = SideShiftRequest( request = SideShiftRequest(
depositMethod: depositCurrency, depositMethod: depositCurrency,
settleMethod: receiveCurrency, settleMethod: receiveCurrency,
depositAmount: depositAmount.replaceAll(',', '.'), depositAmount: depositAmount.replaceAll(',', '.'),
settleAddress: receiveAddress, settleAddress: receiveAddress,
refundAddress: depositAddress, refundAddress: depositAddress,
); );
amount = depositAmount; amount = depositAmount;
} }
if (provider is SimpleSwapExchangeProvider) { if (provider is SimpleSwapExchangeProvider) {
request = SimpleSwapRequest( request = SimpleSwapRequest(
from: depositCurrency,
to: receiveCurrency,
amount: depositAmount.replaceAll(',', '.'),
address: receiveAddress,
refundAddress: depositAddress,
);
amount = depositAmount;
}
if (provider is XMRTOExchangeProvider) {
request = XMRTOTradeRequest(
from: depositCurrency, from: depositCurrency,
to: receiveCurrency, to: receiveCurrency,
amount: depositAmount.replaceAll(',', '.'), amount: depositAmount.replaceAll(',', '.'),
receiveAmount: receiveAmount.replaceAll(',', '.'),
address: receiveAddress, address: receiveAddress,
refundAddress: depositAddress, refundAddress: depositAddress,
isBTCRequest: isReceiveAmountEntered); );
amount = depositAmount; amount = depositAmount;
} }
if (provider is ChangeNowExchangeProvider) { if (provider is XMRTOExchangeProvider) {
request = ChangeNowRequest( request = XMRTOTradeRequest(
from: depositCurrency, from: depositCurrency,
to: receiveCurrency, to: receiveCurrency,
fromAmount: depositAmount.replaceAll(',', '.'), amount: depositAmount.replaceAll(',', '.'),
toAmount: receiveAmount.replaceAll(',', '.'), receiveAmount: receiveAmount.replaceAll(',', '.'),
refundAddress: depositAddress, address: receiveAddress,
address: receiveAddress, refundAddress: depositAddress,
isReverse: isReverse); isBTCRequest: isReceiveAmountEntered);
amount = isReverse ? receiveAmount : depositAmount; amount = depositAmount;
} }
if (provider is MorphTokenExchangeProvider) { if (provider is ChangeNowExchangeProvider) {
request = MorphTokenRequest( request = ChangeNowRequest(
from: depositCurrency, from: depositCurrency,
to: receiveCurrency, to: receiveCurrency,
amount: depositAmount.replaceAll(',', '.'), fromAmount: depositAmount.replaceAll(',', '.'),
refundAddress: depositAddress, toAmount: receiveAmount.replaceAll(',', '.'),
address: receiveAddress); refundAddress: depositAddress,
amount = depositAmount; address: receiveAddress,
} isReverse: isReverse);
amount = isReverse ? receiveAmount : depositAmount;
}
amount = amount.replaceAll(',', '.'); if (provider is MorphTokenExchangeProvider) {
request = MorphTokenRequest(
from: depositCurrency,
to: receiveCurrency,
amount: depositAmount.replaceAll(',', '.'),
refundAddress: depositAddress,
address: receiveAddress);
amount = depositAmount;
}
if (limitsState is LimitsLoadedSuccessfully) { amount = amount.replaceAll(',', '.');
if (double.parse(amount) < limits.min!) {
continue; if (limitsState is LimitsLoadedSuccessfully) {
} else if (limits.max != null && double.parse(amount) > limits.max!) { if (double.parse(amount) < limits.min!) {
continue;
} else {
try {
tradeState = TradeIsCreating();
final trade = await provider.createTrade(
request: request!, isFixedRateMode: isFixedRateMode);
trade.walletId = wallet.id;
tradesStore.setTrade(trade);
await trades.add(trade);
tradeState = TradeIsCreatedSuccessfully(trade: trade);
/// return after the first successful trade
return;
} catch (e) {
continue; continue;
} else if (limits.max != null && double.parse(amount) > limits.max!) {
continue;
} else {
try {
tradeState = TradeIsCreating();
final trade = await provider.createTrade(
request: request!, isFixedRateMode: isFixedRateMode);
trade.walletId = wallet.id;
tradesStore.setTrade(trade);
await trades.add(trade);
tradeState = TradeIsCreatedSuccessfully(trade: trade);
/// return after the first successful trade
return;
} catch (e) {
continue;
}
} }
} }
} }
}
/// if the code reached here then none of the providers succeeded /// if the code reached here then none of the providers succeeded
tradeState = TradeIsCreatedFailure( tradeState = TradeIsCreatedFailure(
title: S.current.trade_not_created, title: S.current.trade_not_created,
error: S.current.none_of_selected_providers_can_exchange); error: S.current.none_of_selected_providers_can_exchange);
} on ConcurrentModificationError {
/// if create trade happened at the exact same time of the scheduled rate update
/// then delay the create trade a bit and try again
///
/// this is because the limitation of the SplayTreeMap that
/// you can't modify it while iterating through it
Future.delayed(Duration(milliseconds: 500), createTrade);
}
} }
@action @action