cake_wallet/lib/exchange/xmrto/xmrto_exchange_provider.dart

199 lines
6.4 KiB
Dart
Raw Normal View History

2020-01-04 19:31:52 +00:00
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/entities/crypto_currency.dart';
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-01-04 19:31:52 +00:00
class XMRTOExchangeProvider extends ExchangeProvider {
2020-01-08 12:26:34 +00:00
XMRTOExchangeProvider()
: super(pairList: [
ExchangePair(
from: CryptoCurrency.xmr, to: CryptoCurrency.btc, reverse: false)
]);
2020-01-04 19:31:52 +00:00
static const userAgent = 'CakeWallet/XMR iOS';
static const originalApiUri = 'https://xmr.to/api/v2/xmr2btc';
static const proxyApiUri = 'https://xmrproxy.net/api/v2/xmr2btc';
static const _orderParameterUriSufix = '/order_parameter_query';
static const _orderStatusUriSufix = '/order_status_query/';
static const _orderCreateUriSufix = '/order_create/';
static String _apiUri = '';
static Future<String> getApiUri() async {
if (_apiUri != null && _apiUri.isNotEmpty) {
return _apiUri;
}
const url = originalApiUri + _orderParameterUriSufix;
final response =
await get(url, headers: {'Content-Type': 'application/json'});
_apiUri = response.statusCode == 403 ? proxyApiUri : originalApiUri;
return _apiUri;
}
2020-01-08 12:26:34 +00:00
@override
2020-01-04 19:31:52 +00:00
String get title => 'XMR.TO';
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-01-08 12:26:34 +00:00
@override
2020-01-04 19:31:52 +00:00
Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to}) async {
final url = await getApiUri() + _orderParameterUriSufix;
final response = await get(url);
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>;
double min = responseJSON['lower_limit'] as double;
double max = responseJSON['upper_limit'] as double;
final price = responseJSON['price'] as double;
if (price > 0) {
try {
min = min/price + correction;
min = _limitsFormat(min);
max = max/price - correction;
max = _limitsFormat(max);
} 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
2020-01-04 19:31:52 +00:00
Future<Trade> createTrade({TradeRequest request}) async {
final _request = request as XMRTOTradeRequest;
final url = await getApiUri() + _orderCreateUriSufix;
final body = {
'xmr_amount': _request.amount,
2020-01-04 19:31:52 +00:00
'btc_dest_address': _request.address
};
final response = await post(url,
headers: {'Content-Type': 'application/json'}, body: json.encode(body));
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
2020-01-04 19:31:52 +00:00
Future<Trade> findTradeById({@required String id}) async {
const headers = {
'Content-Type': 'application/json',
'User-Agent': userAgent
};
final url = await getApiUri() + _orderStatusUriSufix;
final body = {'uuid': id};
final response = await post(url, headers: headers, body: json.encode(body));
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>;
final address = responseJSON['xmr_receiving_integrated_address'] as String;
final paymentId = responseJSON['xmr_required_payment_id_short'] as String;
2020-01-04 19:31:52 +00:00
final amount = responseJSON['xmr_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
2020-01-04 19:31:52 +00:00
Future<double> calculateAmount(
{CryptoCurrency from, CryptoCurrency to, double amount}) async {
if (from != CryptoCurrency.xmr && to != CryptoCurrency.btc) {
return 0;
}
if (_rate == null || _rate == 0) {
_rate = await _fetchRates();
}
final double result = _rate * amount;
return double.parse(result.toStringAsFixed(12));
}
Future<double> _fetchRates() async {
try {
final url = await getApiUri() + _orderParameterUriSufix;
final response =
await get(url, headers: {'Content-Type': 'application/json'});
2020-01-08 12:26:34 +00:00
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final price = responseJSON['price'] as double;
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;
}
}
double _limitsFormat(double limit) => double.parse(limit.toStringAsFixed(3));
2020-01-04 19:31:52 +00:00
}