cake_wallet/lib/exchange/provider/thorchain_exchange.provider.dart

204 lines
6.6 KiB
Dart
Raw Normal View History

2024-01-25 20:35:58 +00:00
import 'dart:convert';
2024-01-29 11:40:40 +00:00
import 'package:cake_wallet/entities/preferences_key.dart';
2024-01-25 20:35:58 +00:00
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';
import 'package:collection/collection.dart';
2024-01-25 20:35:58 +00:00
import 'package:cw_core/crypto_currency.dart';
2024-01-29 11:40:40 +00:00
import 'package:hive/hive.dart';
2024-01-25 20:35:58 +00:00
import 'package:http/http.dart' as http;
2024-01-29 11:40:40 +00:00
import 'package:shared_preferences/shared_preferences.dart';
2024-01-25 20:35:58 +00:00
class ThorChainExchangeProvider extends ExchangeProvider {
2024-01-29 11:40:40 +00:00
ThorChainExchangeProvider({required this.tradesStore})
: 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,
2024-02-02 11:03:00 +00:00
CryptoCurrency.bch
2024-01-28 13:02:59 +00:00
].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-02-04 11:01:01 +00:00
static const _txInfoPath = '/thorchain/tx/';
2024-01-28 13:02:59 +00:00
static const _affiliateName = 'cakewallet';
2024-02-04 11:01:01 +00:00
static const _affiliateBps = '0';
2024-01-25 20:35:58 +00:00
2024-01-29 11:40:40 +00:00
final Box<Trade> tradesStore;
2024-01-25 20:35:58 +00:00
@override
String get title => 'ThorChain';
@override
bool get isAvailable => true;
@override
bool get isEnabled => true;
@override
2024-01-30 11:55:54 +00:00
bool get supportsFixedRate => false;
2024-01-25 20:35:58 +00:00
@override
ExchangeProviderDescription get description => ExchangeProviderDescription.thorChain;
@override
Future<bool> checkIsAvailable() async => true;
2024-02-02 11:03:00 +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;
2024-01-28 13:02:59 +00:00
2024-02-02 11:03:00 +00:00
final params = {
'from_asset': _normalizeCurrency(from),
'to_asset': _normalizeCurrency(to),
'amount': _doubleToThorChainString(amount),
2024-02-04 11:01:01 +00:00
'affiliate': _affiliateName,
'affiliate_bps': _affiliateBps
2024-02-02 11:03:00 +00:00
};
2024-01-28 13:02:59 +00:00
2024-02-02 11:03:00 +00:00
final responseJSON = await _getSwapQuote(params);
2024-01-28 13:02:59 +00:00
2024-02-02 11:03:00 +00:00
final expectedAmountOut = responseJSON['expected_amount_out'] as String? ?? '0.0';
return _thorChainAmountToDouble(expectedAmountOut);
} catch (e) {
print(e.toString());
return 0.0;
}
2024-01-28 13:02:59 +00:00
}
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-02-02 11:03:00 +00:00
'amount': _doubleToThorChainString(1),
2024-01-30 13:09:46 +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-02-04 11:01:01 +00:00
final minAmountIn = responseJSON['recommended_min_amount_in'] as String? ?? '0.0';
2024-01-25 20:35:58 +00:00
2024-02-02 11:03:00 +00:00
return Limits(min: _thorChainAmountToDouble(minAmountIn));
2024-01-25 20:35:58 +00:00
}
@override
Future<Trade> createTrade({required TradeRequest request, required bool isFixedRateMode}) async {
2024-02-02 11:03:00 +00:00
String formattedToAddress = request.toAddress.startsWith('bitcoincash:')
? request.toAddress.replaceFirst('bitcoincash:', '')
: request.toAddress;
final formattedFromAmount = double.parse(request.fromAmount);
2024-01-25 20:35:58 +00:00
final params = {
'from_asset': _normalizeCurrency(request.fromCurrency),
'to_asset': _normalizeCurrency(request.toCurrency),
2024-02-02 11:03:00 +00:00
'amount': _doubleToThorChainString(formattedFromAmount),
'destination': formattedToAddress,
2024-01-28 13:02:59 +00:00
'affiliate': _affiliateName,
2024-01-29 11:40:40 +00:00
'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 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(
2024-02-04 11:01:01 +00:00
id: '',
2024-01-25 20:35:58 +00:00
from: request.fromCurrency,
to: request.toCurrency,
provider: description,
inputAddress: inputAddress,
createdAt: DateTime.now(),
amount: request.fromAmount,
2024-02-04 11:01:01 +00:00
state: TradeState.pending,
2024-01-28 13:02:59 +00:00
payoutAddress: request.toAddress,
memo: memo);
2024-01-25 20:35:58 +00:00
}
2024-02-04 11:01:01 +00:00
@override
2024-01-29 11:40:40 +00:00
Future<Trade> findTradeById({required String id}) async {
2024-02-04 11:01:01 +00:00
if (id.isEmpty) throw Exception('Trade id is empty');
final formattedId = id.startsWith('0x') ? id.substring(2) : id;
final uri = Uri.parse('$_baseURL$_txInfoPath$formattedId');
2024-02-04 11:01:01 +00:00
final response = await http.get(uri);
if (response.statusCode == 404) {
throw Exception('Trade not found for id: $id');
} else if (response.statusCode != 200) {
throw Exception('Unexpected HTTP status: ${response.statusCode}');
2024-01-29 11:40:40 +00:00
}
2024-02-04 11:01:01 +00:00
final responseJSON = json.decode(response.body);
final observedTx = responseJSON['observed_tx'];
if (observedTx == null) {
throw Exception('No observed transaction found for id: $id');
}
final tx = observedTx['tx'];
final String fromAddress = tx['from_address'] as String? ?? '';
final String toAddress = tx['to_address'] as String? ?? '';
final List<dynamic> coins = tx['coins'] as List<dynamic>;
final String? memo = tx['memo'] as String?;
final String toAsset = memo != null ? (memo.split(':')[1]).split('.')[0] : '';
final status = observedTx['status'] as String?;
final formattedStatus = status ?? 'pending';
return Trade(
id: id,
from: CryptoCurrency.fromString(tx['chain'] as String? ?? ''),
to: CryptoCurrency.fromString(toAsset),
provider: description,
inputAddress: fromAddress,
payoutAddress: toAddress,
amount: coins.first['amount'] as String? ?? '0.0',
state: TradeState.deserialize(raw: formattedStatus),
memo: memo,
);
2024-01-25 20:35:58 +00:00
}
2024-02-02 11:03:00 +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-02-04 11:01:01 +00:00
String _normalizeCurrency(CryptoCurrency currency) => '${currency.title}.${currency.title}';
2024-01-29 11:40:40 +00:00
2024-02-02 11:03:00 +00:00
String _doubleToThorChainString(double amount) => (amount * 1e8).toInt().toString();
2024-02-04 11:01:01 +00:00
double _thorChainAmountToDouble(String amount) => double.parse(amount) / 1e8;
2024-01-25 20:35:58 +00:00
}