2024-01-25 20:35:58 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:cake_wallet/exchange/exchange_provider_description.dart';
|
|
|
|
import 'package:cake_wallet/exchange/limits.dart';
|
|
|
|
import 'package:cake_wallet/exchange/provider/exchange_provider.dart';
|
|
|
|
import 'package:cake_wallet/exchange/trade.dart';
|
|
|
|
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';
|
2024-01-28 13:02:59 +00:00
|
|
|
import 'package:cw_core/amount_converter.dart';
|
2024-01-25 20:35:58 +00:00
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
|
|
|
|
class ThorChainExchangeProvider extends ExchangeProvider {
|
2024-01-28 13:02:59 +00:00
|
|
|
ThorChainExchangeProvider() : super(pairList: supportedPairs(_notSupported));
|
2024-01-25 20:35:58 +00:00
|
|
|
|
|
|
|
static final List<CryptoCurrency> _notSupported = [
|
|
|
|
...(CryptoCurrency.all
|
2024-01-28 13:02:59 +00:00
|
|
|
.where((element) => ![
|
|
|
|
CryptoCurrency.btc,
|
|
|
|
CryptoCurrency.eth,
|
|
|
|
CryptoCurrency.ltc,
|
|
|
|
CryptoCurrency.bch
|
|
|
|
].contains(element))
|
2024-01-25 20:35:58 +00:00
|
|
|
.toList())
|
|
|
|
];
|
|
|
|
|
|
|
|
static const _baseURL = 'https://thornode.ninerealms.com';
|
|
|
|
static const _quotePath = '/thorchain/quote/swap';
|
2024-01-28 13:02:59 +00:00
|
|
|
static const _affiliateName = 'cakewallet';
|
|
|
|
static const _affiliateBps = '0';
|
2024-01-25 20:35:58 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
String get title => 'ThorChain';
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isAvailable => true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isEnabled => true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get supportsFixedRate => true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
ExchangeProviderDescription get description => ExchangeProviderDescription.thorChain;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> checkIsAvailable() async => true;
|
|
|
|
|
2024-01-28 13:02:59 +00:00
|
|
|
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>;
|
|
|
|
}
|
|
|
|
|
2024-01-25 20:35:58 +00:00
|
|
|
@override
|
|
|
|
Future<Limits> fetchLimits(
|
|
|
|
{required CryptoCurrency from,
|
|
|
|
required CryptoCurrency to,
|
|
|
|
required bool isFixedRateMode}) async {
|
|
|
|
final params = {
|
|
|
|
'from_asset': _normalizeCurrency(from),
|
|
|
|
'to_asset': _normalizeCurrency(to),
|
2024-01-28 13:02:59 +00:00
|
|
|
'amount': AmountConverter.amountToSmallestUnit(cryptoCurrency: from, amount: 1).toString(),
|
|
|
|
'affiliate': _affiliateName,
|
|
|
|
'affiliate_bps': _affiliateBps,
|
2024-01-25 20:35:58 +00:00
|
|
|
};
|
|
|
|
|
2024-01-28 13:02:59 +00:00
|
|
|
final responseJSON = await _getSwapQuote(params);
|
2024-01-25 20:35:58 +00:00
|
|
|
final minAmountIn = responseJSON['recommended_min_amount_in'] as String?;
|
2024-01-28 13:02:59 +00:00
|
|
|
final formattedMinAmountIn = minAmountIn != null ? int.parse(minAmountIn) / 1e8 : 0.0;
|
2024-01-25 20:35:58 +00:00
|
|
|
|
|
|
|
return Limits(min: formattedMinAmountIn);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Trade> createTrade({required TradeRequest request, required bool isFixedRateMode}) async {
|
|
|
|
double amountBTC = double.parse(request.fromAmount);
|
2024-01-28 13:02:59 +00:00
|
|
|
String formattedAmount = AmountConverter.amountToSmallestUnit(
|
|
|
|
cryptoCurrency: request.fromCurrency, amount: amountBTC)
|
|
|
|
.toString();
|
2024-01-25 20:35:58 +00:00
|
|
|
|
|
|
|
final params = {
|
|
|
|
'from_asset': _normalizeCurrency(request.fromCurrency),
|
|
|
|
'to_asset': _normalizeCurrency(request.toCurrency),
|
|
|
|
'amount': formattedAmount,
|
|
|
|
'destination': request.toAddress,
|
2024-01-28 13:02:59 +00:00
|
|
|
'affiliate': _affiliateName,
|
|
|
|
'affiliate_bps': _affiliateBps};
|
2024-01-25 20:35:58 +00:00
|
|
|
|
2024-01-28 13:02:59 +00:00
|
|
|
final responseJSON = await _getSwapQuote(params);
|
2024-01-25 20:35:58 +00:00
|
|
|
|
2024-01-28 13:02:59 +00:00
|
|
|
print('createTrade _ responseJSON________: $responseJSON');
|
2024-01-25 20:35:58 +00:00
|
|
|
final inputAddress = responseJSON['inbound_address'] as String?;
|
2024-01-28 13:02:59 +00:00
|
|
|
final memo = responseJSON['memo'] as String?;
|
2024-01-25 20:35:58 +00:00
|
|
|
|
|
|
|
return Trade(
|
|
|
|
id: 'id',
|
|
|
|
from: request.fromCurrency,
|
|
|
|
to: request.toCurrency,
|
|
|
|
provider: description,
|
|
|
|
inputAddress: inputAddress,
|
|
|
|
refundAddress: 'refundAddress',
|
|
|
|
extraId: 'extraId',
|
|
|
|
createdAt: DateTime.now(),
|
|
|
|
amount: request.fromAmount,
|
|
|
|
state: TradeState.created,
|
2024-01-28 13:02:59 +00:00
|
|
|
payoutAddress: request.toAddress,
|
|
|
|
memo: memo);
|
2024-01-25 20:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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),
|
2024-01-28 13:02:59 +00:00
|
|
|
'amount':
|
|
|
|
AmountConverter.amountToSmallestUnit(cryptoCurrency: from, amount: amount).toString(),
|
2024-01-25 20:35:58 +00:00
|
|
|
};
|
|
|
|
|
2024-01-28 13:02:59 +00:00
|
|
|
final responseJSON = await _getSwapQuote(params);
|
|
|
|
print(responseJSON);
|
2024-01-25 20:35:58 +00:00
|
|
|
|
|
|
|
final expectedAmountOutString = responseJSON['expected_amount_out'] as String? ?? '0';
|
|
|
|
final expectedAmountOut = double.parse(expectedAmountOutString);
|
2024-01-28 13:02:59 +00:00
|
|
|
double formattedAmountOut = 0.0;
|
2024-01-25 20:35:58 +00:00
|
|
|
|
2024-01-28 13:02:59 +00:00
|
|
|
if (to == CryptoCurrency.eth) {
|
|
|
|
formattedAmountOut = expectedAmountOut / 1e9;
|
|
|
|
} else {
|
|
|
|
formattedAmountOut = AmountConverter.amountIntToDouble(to, expectedAmountOut.toInt());
|
|
|
|
}
|
2024-01-25 20:35:58 +00:00
|
|
|
|
|
|
|
return formattedAmountOut;
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Trade> findTradeById({required String id}) {
|
|
|
|
throw UnimplementedError('findTradeById');
|
|
|
|
}
|
|
|
|
|
|
|
|
String _normalizeCurrency(CryptoCurrency currency) {
|
|
|
|
switch (currency) {
|
|
|
|
case CryptoCurrency.btc:
|
|
|
|
return 'BTC.BTC';
|
|
|
|
case CryptoCurrency.eth:
|
|
|
|
return 'ETH.ETH';
|
2024-01-28 13:02:59 +00:00
|
|
|
case CryptoCurrency.ltc:
|
|
|
|
return 'LTC.LTC';
|
|
|
|
case CryptoCurrency.bch:
|
|
|
|
return 'BCH.BCH';
|
2024-01-25 20:35:58 +00:00
|
|
|
default:
|
|
|
|
return currency.title.toLowerCase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|