2020-01-04 19:31:52 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:http/http.dart';
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/exchange/exchange_pair.dart';
|
|
|
|
import 'package:cake_wallet/exchange/exchange_provider.dart';
|
|
|
|
import 'package:cake_wallet/exchange/limits.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/xmrto/xmrto_trade_request.dart';
|
|
|
|
import 'package:cake_wallet/exchange/trade_not_created_exeption.dart';
|
|
|
|
import 'package:cake_wallet/exchange/exchange_provider_description.dart';
|
|
|
|
import 'package:cake_wallet/exchange/trade_not_found_exeption.dart';
|
2020-12-03 21:22:56 +00:00
|
|
|
import 'package:cake_wallet/generated/i18n.dart';
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
class XMRTOExchangeProvider extends ExchangeProvider {
|
2020-01-08 12:26:34 +00:00
|
|
|
XMRTOExchangeProvider()
|
2020-11-10 14:58:40 +00:00
|
|
|
: _isAvailable = false,
|
|
|
|
super(pairList: [
|
2020-01-08 12:26:34 +00:00
|
|
|
ExchangePair(
|
|
|
|
from: CryptoCurrency.xmr, to: CryptoCurrency.btc, reverse: false)
|
|
|
|
]);
|
|
|
|
|
2020-01-04 19:31:52 +00:00
|
|
|
static const userAgent = 'CakeWallet/XMR iOS';
|
2020-09-25 19:55:41 +00:00
|
|
|
static const originalApiUri = 'https://xmr.to/api/v3/xmr2btc';
|
2020-11-10 14:58:40 +00:00
|
|
|
static const _orderParameterUriSuffix = '/order_parameter_query';
|
|
|
|
static const _orderStatusUriSuffix = '/order_status_query/';
|
|
|
|
static const _orderCreateUriSuffix = '/order_create/';
|
2020-12-15 18:41:06 +00:00
|
|
|
static const _headers = {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'User-Agent': userAgent
|
|
|
|
};
|
2020-01-04 19:31:52 +00:00
|
|
|
|
2020-11-10 14:58:40 +00:00
|
|
|
static Future<bool> _checkIsAvailable() async {
|
|
|
|
const url = originalApiUri + _orderParameterUriSuffix;
|
2022-10-12 17:09:57 +00:00
|
|
|
final uri = Uri.parse(url);
|
|
|
|
final response = await get(uri, headers: _headers);
|
2020-11-10 14:58:40 +00:00
|
|
|
return !(response.statusCode == 403);
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
@override
|
2020-01-04 19:31:52 +00:00
|
|
|
String get title => 'XMR.TO';
|
|
|
|
|
2020-11-10 14:58:40 +00:00
|
|
|
@override
|
|
|
|
bool get isAvailable => _isAvailable;
|
|
|
|
|
2022-09-01 14:12:38 +00:00
|
|
|
@override
|
|
|
|
bool get isEnabled => true;
|
|
|
|
|
2022-12-06 17:23:46 +00:00
|
|
|
@override
|
|
|
|
bool get supportsFixedRate => false;
|
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
@override
|
2020-01-04 19:31:52 +00:00
|
|
|
ExchangeProviderDescription get description =>
|
|
|
|
ExchangeProviderDescription.xmrto;
|
|
|
|
|
|
|
|
double _rate = 0;
|
2020-11-10 14:58:40 +00:00
|
|
|
bool _isAvailable;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> checkIsAvailable() async {
|
|
|
|
_isAvailable = await _checkIsAvailable();
|
|
|
|
return isAvailable;
|
|
|
|
}
|
2020-01-04 19:31:52 +00:00
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
@override
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<Limits> fetchLimits({
|
|
|
|
required CryptoCurrency from,
|
|
|
|
required CryptoCurrency to,
|
|
|
|
required bool isFixedRateMode}) async {
|
2020-11-10 14:58:40 +00:00
|
|
|
final url = originalApiUri + _orderParameterUriSuffix;
|
2022-10-12 17:09:57 +00:00
|
|
|
final uri = Uri.parse(url);
|
|
|
|
final response = await get(uri);
|
2020-04-13 16:05:27 +00:00
|
|
|
final correction = 0.001;
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
if (response.statusCode != 200) {
|
|
|
|
return Limits(min: 0, max: 0);
|
|
|
|
}
|
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
2020-09-25 19:55:41 +00:00
|
|
|
double min = double.parse(responseJSON['lower_limit'] as String);
|
|
|
|
double max = double.parse(responseJSON['upper_limit'] as String);
|
|
|
|
final price = double.parse(responseJSON['price'] as String);
|
2020-04-13 16:05:27 +00:00
|
|
|
|
|
|
|
if (price > 0) {
|
|
|
|
try {
|
2020-11-10 14:58:40 +00:00
|
|
|
min = min / price + correction;
|
2020-04-13 17:31:00 +00:00
|
|
|
min = _limitsFormat(min);
|
2020-11-10 14:58:40 +00:00
|
|
|
max = max / price - correction;
|
2020-04-13 17:31:00 +00:00
|
|
|
max = _limitsFormat(max);
|
2020-04-13 16:05:27 +00:00
|
|
|
} catch (e) {
|
|
|
|
min = 0;
|
|
|
|
max = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
min = 0;
|
|
|
|
max = 0;
|
|
|
|
}
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
return Limits(min: min, max: max);
|
|
|
|
}
|
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
@override
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<Trade> createTrade({required TradeRequest request, required bool isFixedRateMode}) async {
|
2020-01-04 19:31:52 +00:00
|
|
|
final _request = request as XMRTOTradeRequest;
|
2020-11-10 14:58:40 +00:00
|
|
|
final url = originalApiUri + _orderCreateUriSuffix;
|
2020-12-15 18:41:06 +00:00
|
|
|
final _amount =
|
|
|
|
_request.isBTCRequest ? _request.receiveAmount : _request.amount;
|
2020-12-03 21:22:56 +00:00
|
|
|
final _amountCurrency = _request.isBTCRequest
|
|
|
|
? _request.to.toString()
|
|
|
|
: _request.from.toString();
|
|
|
|
final pattern = '^([0-9]+([.\,][0-9]{0,8})?|[.\,][0-9]{1,8})\$';
|
|
|
|
final isValid = RegExp(pattern).hasMatch(_amount);
|
|
|
|
|
|
|
|
if (!isValid) {
|
|
|
|
throw TradeNotCreatedException(description,
|
|
|
|
description: S.current.xmr_to_error_description);
|
|
|
|
}
|
|
|
|
|
2020-01-04 19:31:52 +00:00
|
|
|
final body = {
|
2020-12-03 21:22:56 +00:00
|
|
|
'amount': _amount,
|
|
|
|
'amount_currency': _amountCurrency,
|
2022-10-12 17:09:57 +00:00
|
|
|
'btc_dest_address': _request.address};
|
|
|
|
final uri = Uri.parse(url);
|
2020-12-15 18:41:06 +00:00
|
|
|
final response =
|
2022-10-12 17:09:57 +00:00
|
|
|
await post(uri, headers: _headers, body: json.encode(body));
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
if (response.statusCode != 201) {
|
|
|
|
if (response.statusCode == 400) {
|
2020-01-08 12:26:34 +00:00
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final error = responseJSON['error_msg'] as String;
|
|
|
|
|
|
|
|
throw TradeNotCreatedException(description, description: error);
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw TradeNotCreatedException(description);
|
|
|
|
}
|
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final uuid = responseJSON["uuid"] as String;
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
return Trade(
|
|
|
|
id: uuid,
|
|
|
|
provider: description,
|
|
|
|
from: _request.from,
|
|
|
|
to: _request.to,
|
|
|
|
state: TradeState.created,
|
|
|
|
amount: _request.amount,
|
|
|
|
createdAt: DateTime.now());
|
|
|
|
}
|
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
@override
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<Trade> findTradeById({required String id}) async {
|
2020-11-10 14:58:40 +00:00
|
|
|
final url = originalApiUri + _orderStatusUriSuffix;
|
2022-10-12 17:09:57 +00:00
|
|
|
final uri = Uri.parse(url);
|
2020-01-04 19:31:52 +00:00
|
|
|
final body = {'uuid': id};
|
2020-12-15 18:41:06 +00:00
|
|
|
final response =
|
2022-10-12 17:09:57 +00:00
|
|
|
await post(uri, headers: _headers, body: json.encode(body));
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
if (response.statusCode != 200) {
|
|
|
|
if (response.statusCode == 400) {
|
2020-01-08 12:26:34 +00:00
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final error = responseJSON['error_msg'] as String;
|
|
|
|
|
2020-01-04 19:31:52 +00:00
|
|
|
throw TradeNotFoundException(id,
|
|
|
|
provider: description, description: error);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw TradeNotFoundException(id, provider: description);
|
|
|
|
}
|
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
2020-10-24 12:55:24 +00:00
|
|
|
final address = responseJSON['receiving_subaddress'] as String;
|
2020-01-08 12:26:34 +00:00
|
|
|
final paymentId = responseJSON['xmr_required_payment_id_short'] as String;
|
2020-10-24 12:55:24 +00:00
|
|
|
final amount = responseJSON['incoming_amount_total'].toString();
|
2020-01-08 12:26:34 +00:00
|
|
|
final stateRaw = responseJSON['state'] as String;
|
|
|
|
final expiredAtRaw = responseJSON['expires_at'] as String;
|
2020-01-04 19:31:52 +00:00
|
|
|
final expiredAt = DateTime.parse(expiredAtRaw).toLocal();
|
2020-01-08 12:26:34 +00:00
|
|
|
final outputTransaction = responseJSON['btc_transaction_id'] as String;
|
2020-01-04 19:31:52 +00:00
|
|
|
final state = TradeState.deserialize(raw: stateRaw);
|
|
|
|
|
|
|
|
return Trade(
|
|
|
|
id: id,
|
|
|
|
provider: description,
|
|
|
|
from: CryptoCurrency.xmr,
|
|
|
|
to: CryptoCurrency.btc,
|
|
|
|
inputAddress: address,
|
|
|
|
extraId: paymentId,
|
|
|
|
expiredAt: expiredAt,
|
|
|
|
amount: amount,
|
|
|
|
state: state,
|
|
|
|
outputTransaction: outputTransaction);
|
|
|
|
}
|
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
@override
|
2022-12-08 19:14:42 +00:00
|
|
|
Future<double> fetchRate(
|
2022-10-12 17:09:57 +00:00
|
|
|
{required CryptoCurrency from,
|
|
|
|
required CryptoCurrency to,
|
|
|
|
required double amount,
|
|
|
|
required bool isFixedRateMode,
|
|
|
|
required bool isReceiveAmount}) async {
|
2020-01-04 19:31:52 +00:00
|
|
|
if (from != CryptoCurrency.xmr && to != CryptoCurrency.btc) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
if (_rate == 0) {
|
2020-01-04 19:31:52 +00:00
|
|
|
_rate = await _fetchRates();
|
|
|
|
}
|
|
|
|
|
2020-09-25 19:55:41 +00:00
|
|
|
final double result = isReceiveAmount
|
2020-11-10 14:58:40 +00:00
|
|
|
? _rate == 0
|
|
|
|
? 0
|
|
|
|
: amount / _rate
|
2020-09-25 19:55:41 +00:00
|
|
|
: _rate * amount;
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
return double.parse(result.toStringAsFixed(12));
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<double> _fetchRates() async {
|
|
|
|
try {
|
2020-11-10 14:58:40 +00:00
|
|
|
final url = originalApiUri + _orderParameterUriSuffix;
|
2022-10-12 17:09:57 +00:00
|
|
|
final uri = Uri.parse(url);
|
|
|
|
final response = await get(uri, headers: _headers);
|
2020-01-08 12:26:34 +00:00
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
2020-09-25 19:55:41 +00:00
|
|
|
final price = double.parse(responseJSON['price'] as String);
|
2020-01-08 12:26:34 +00:00
|
|
|
|
2020-01-04 19:31:52 +00:00
|
|
|
return price;
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
}
|
2020-04-13 17:31:00 +00:00
|
|
|
|
|
|
|
double _limitsFormat(double limit) => double.parse(limit.toStringAsFixed(3));
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|