mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-01-08 20:09:24 +00:00
fix min amount bug
This commit is contained in:
parent
53441e7c2a
commit
2a0d8f9692
1 changed files with 54 additions and 57 deletions
|
@ -9,7 +9,6 @@ import 'package:cake_wallet/exchange/trade_request.dart';
|
|||
import 'package:cake_wallet/exchange/trade_state.dart';
|
||||
import 'package:cake_wallet/exchange/utils/currency_pairs_utils.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:cw_core/amount_converter.dart';
|
||||
import 'package:cw_core/crypto_currency.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
@ -25,6 +24,7 @@ class ThorChainExchangeProvider extends ExchangeProvider {
|
|||
CryptoCurrency.btc,
|
||||
CryptoCurrency.eth,
|
||||
CryptoCurrency.ltc,
|
||||
CryptoCurrency.bch
|
||||
].contains(element))
|
||||
.toList())
|
||||
];
|
||||
|
@ -54,19 +54,31 @@ class ThorChainExchangeProvider extends ExchangeProvider {
|
|||
@override
|
||||
Future<bool> checkIsAvailable() async => true;
|
||||
|
||||
Future<Map<String, dynamic>> _getSwapQuote(Map<String, String> params) async {
|
||||
final uri = Uri.parse('$_baseURL$_quotePath${Uri(queryParameters: params)}');
|
||||
final response = await http.get(uri);
|
||||
@override
|
||||
Future<double> fetchRate(
|
||||
{required CryptoCurrency from,
|
||||
required CryptoCurrency to,
|
||||
required double amount,
|
||||
required bool isFixedRateMode,
|
||||
required bool isReceiveAmount}) async {
|
||||
try {
|
||||
if (amount == 0) return 0.0;
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('Unexpected HTTP status: ${response.statusCode}');
|
||||
final params = {
|
||||
'from_asset': _normalizeCurrency(from),
|
||||
'to_asset': _normalizeCurrency(to),
|
||||
'amount': _doubleToThorChainString(amount),
|
||||
};
|
||||
|
||||
final responseJSON = await _getSwapQuote(params);
|
||||
|
||||
final expectedAmountOut = responseJSON['expected_amount_out'] as String? ?? '0.0';
|
||||
|
||||
return _thorChainAmountToDouble(expectedAmountOut);
|
||||
} catch (e) {
|
||||
print(e.toString());
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
if (response.body.contains('error')) {
|
||||
throw Exception('Unexpected response: ${response.body}');
|
||||
}
|
||||
|
||||
return json.decode(response.body) as Map<String, dynamic>;
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -77,30 +89,30 @@ class ThorChainExchangeProvider extends ExchangeProvider {
|
|||
final params = {
|
||||
'from_asset': _normalizeCurrency(from),
|
||||
'to_asset': _normalizeCurrency(to),
|
||||
'amount': AmountConverter.amountToSmallestUnit(cryptoCurrency: from, amount: 1).toString(),
|
||||
'amount': _doubleToThorChainString(1),
|
||||
'affiliate': _affiliateName,
|
||||
'affiliate_bps': _affiliateBps
|
||||
};
|
||||
|
||||
final responseJSON = await _getSwapQuote(params);
|
||||
final minAmountIn = responseJSON['recommended_min_amount_in'] as String?;
|
||||
final formattedMinAmountIn = minAmountIn != null ? int.parse(minAmountIn) / 1e8 : 0.0;
|
||||
|
||||
return Limits(min: formattedMinAmountIn);
|
||||
return Limits(min: _thorChainAmountToDouble(minAmountIn));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Trade> createTrade({required TradeRequest request, required bool isFixedRateMode}) async {
|
||||
double amountBTC = double.parse(request.fromAmount);
|
||||
String formattedAmount = AmountConverter.amountToSmallestUnit(
|
||||
cryptoCurrency: request.fromCurrency, amount: amountBTC)
|
||||
.toString();
|
||||
String formattedToAddress = request.toAddress.startsWith('bitcoincash:')
|
||||
? request.toAddress.replaceFirst('bitcoincash:', '')
|
||||
: request.toAddress;
|
||||
|
||||
final formattedFromAmount = double.parse(request.fromAmount);
|
||||
|
||||
final params = {
|
||||
'from_asset': _normalizeCurrency(request.fromCurrency),
|
||||
'to_asset': _normalizeCurrency(request.toCurrency),
|
||||
'amount': formattedAmount,
|
||||
'destination': request.toAddress,
|
||||
'amount': _doubleToThorChainString(formattedFromAmount),
|
||||
'destination': formattedToAddress,
|
||||
'affiliate': _affiliateName,
|
||||
'affiliate_bps': _affiliateBps
|
||||
};
|
||||
|
@ -124,42 +136,6 @@ class ThorChainExchangeProvider extends ExchangeProvider {
|
|||
memo: memo);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<double> fetchRate(
|
||||
{required CryptoCurrency from,
|
||||
required CryptoCurrency to,
|
||||
required double amount,
|
||||
required bool isFixedRateMode,
|
||||
required bool isReceiveAmount}) async {
|
||||
try {
|
||||
if (amount == 0) return 0.0;
|
||||
|
||||
final params = {
|
||||
'from_asset': _normalizeCurrency(from),
|
||||
'to_asset': _normalizeCurrency(to),
|
||||
'amount':
|
||||
AmountConverter.amountToSmallestUnit(cryptoCurrency: from, amount: amount).toString(),
|
||||
};
|
||||
|
||||
final responseJSON = await _getSwapQuote(params);
|
||||
|
||||
final expectedAmountOutString = responseJSON['expected_amount_out'] as String? ?? '0';
|
||||
final expectedAmountOut = double.parse(expectedAmountOutString);
|
||||
double formattedAmountOut = 0.0;
|
||||
|
||||
if (to == CryptoCurrency.eth) {
|
||||
formattedAmountOut = expectedAmountOut / 1e9;
|
||||
} else {
|
||||
formattedAmountOut = AmountConverter.amountIntToDouble(to, expectedAmountOut.toInt());
|
||||
}
|
||||
|
||||
return formattedAmountOut;
|
||||
} catch (e) {
|
||||
print(e.toString());
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Trade> findTradeById({required String id}) async {
|
||||
final foundTrade = tradesStore.values.firstWhereOrNull((element) => element.id == id);
|
||||
if (foundTrade == null) {
|
||||
|
@ -168,6 +144,22 @@ class ThorChainExchangeProvider extends ExchangeProvider {
|
|||
return foundTrade;
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> _getSwapQuote(Map<String, String> params) async {
|
||||
final uri = Uri.parse('$_baseURL$_quotePath${Uri(queryParameters: params)}');
|
||||
|
||||
final response = await http.get(uri);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('Unexpected HTTP status: ${response.statusCode}');
|
||||
}
|
||||
|
||||
if (response.body.contains('error')) {
|
||||
throw Exception('Unexpected response: ${response.body}');
|
||||
}
|
||||
|
||||
return json.decode(response.body) as Map<String, dynamic>;
|
||||
}
|
||||
|
||||
String _normalizeCurrency(CryptoCurrency currency) {
|
||||
switch (currency) {
|
||||
case CryptoCurrency.btc:
|
||||
|
@ -183,6 +175,11 @@ class ThorChainExchangeProvider extends ExchangeProvider {
|
|||
}
|
||||
}
|
||||
|
||||
String _doubleToThorChainString(double amount) => (amount * 1e8).toInt().toString();
|
||||
|
||||
double _thorChainAmountToDouble(String? amount) =>
|
||||
amount == null ? 0.0 : double.parse(amount) / 1e8;
|
||||
|
||||
Future<int> getNextTradeCounter() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
int currentCounter = prefs.getInt(PreferencesKey.thorChainTradeCounter) ?? 0;
|
||||
|
|
Loading…
Reference in a new issue