2023-02-06 19:20:43 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
|
|
|
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';
|
2023-02-06 19:20:43 +00:00
|
|
|
import 'package:cake_wallet/exchange/trade_state.dart';
|
2023-10-25 20:58:25 +00:00
|
|
|
import 'package:cake_wallet/exchange/utils/currency_pairs_utils.dart';
|
2023-02-06 19:20:43 +00:00
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
|
|
|
import 'package:http/http.dart';
|
|
|
|
|
|
|
|
class TrocadorExchangeProvider extends ExchangeProvider {
|
2023-11-16 23:23:34 +00:00
|
|
|
TrocadorExchangeProvider({this.useTorOnly = false, this.providerStates = const {}})
|
|
|
|
: _lastUsedRateId = '', _provider = [],
|
2023-10-25 20:58:25 +00:00
|
|
|
super(pairList: supportedPairs(_notSupported));
|
2023-03-02 17:24:52 +00:00
|
|
|
|
2023-03-01 21:44:15 +00:00
|
|
|
bool useTorOnly;
|
2023-11-16 23:23:34 +00:00
|
|
|
final Map<String, bool> providerStates;
|
|
|
|
|
|
|
|
static const List<String> availableProviders = [
|
|
|
|
'Swapter',
|
|
|
|
'StealthEx',
|
|
|
|
'Simpleswap',
|
|
|
|
'Swapuz'
|
|
|
|
'ChangeNow',
|
|
|
|
'Changehero',
|
|
|
|
'FixedFloat',
|
|
|
|
'LetsExchange',
|
|
|
|
'Exolix',
|
|
|
|
'Godex',
|
|
|
|
'Exch',
|
|
|
|
'CoinCraddle'
|
|
|
|
];
|
2023-02-06 19:20:43 +00:00
|
|
|
|
|
|
|
static const List<CryptoCurrency> _notSupported = [
|
|
|
|
CryptoCurrency.stx,
|
|
|
|
CryptoCurrency.zaddr,
|
|
|
|
];
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
static const apiKey = secrets.trocadorApiKey;
|
2023-02-06 19:20:43 +00:00
|
|
|
static const onionApiAuthority = 'trocadorfyhlu27aefre5u7zri66gudtzdyelymftvr4yjwcxhfaqsid.onion';
|
|
|
|
static const clearNetAuthority = 'trocador.app';
|
2023-02-06 20:20:46 +00:00
|
|
|
static const markup = secrets.trocadorExchangeMarkup;
|
2023-02-06 19:20:43 +00:00
|
|
|
static const newRatePath = '/api/new_rate';
|
|
|
|
static const createTradePath = 'api/new_trade';
|
|
|
|
static const tradePath = 'api/trade';
|
2023-02-28 16:12:24 +00:00
|
|
|
static const coinPath = 'api/coin';
|
2023-10-25 20:58:25 +00:00
|
|
|
|
2023-02-06 19:20:43 +00:00
|
|
|
String _lastUsedRateId;
|
2023-11-16 23:23:34 +00:00
|
|
|
List<dynamic> _provider;
|
2023-02-06 19:20:43 +00:00
|
|
|
|
|
|
|
@override
|
2023-10-25 20:58:25 +00:00
|
|
|
String get title => 'Trocador';
|
2023-02-06 19:20:43 +00:00
|
|
|
|
|
|
|
@override
|
2023-10-25 20:58:25 +00:00
|
|
|
bool get isAvailable => true;
|
2023-02-06 19:20:43 +00:00
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
@override
|
|
|
|
bool get isEnabled => true;
|
2023-02-06 19:20:43 +00:00
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
@override
|
|
|
|
bool get supportsFixedRate => true;
|
2023-02-06 19:20:43 +00:00
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
@override
|
|
|
|
bool get supportsOnionAddress => true;
|
2023-02-06 19:20:43 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
ExchangeProviderDescription get description => ExchangeProviderDescription.trocador;
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
@override
|
|
|
|
Future<bool> checkIsAvailable() async => true;
|
|
|
|
|
2023-02-06 19:20:43 +00:00
|
|
|
@override
|
|
|
|
Future<Limits> fetchLimits(
|
|
|
|
{required CryptoCurrency from,
|
|
|
|
required CryptoCurrency to,
|
|
|
|
required bool isFixedRateMode}) async {
|
2023-10-25 20:58:25 +00:00
|
|
|
final params = {
|
2023-03-02 17:24:52 +00:00
|
|
|
'api_key': apiKey,
|
2023-08-24 21:30:53 +00:00
|
|
|
'ticker': _normalizeCurrency(from),
|
2023-03-02 17:24:52 +00:00
|
|
|
'name': from.name,
|
|
|
|
};
|
2023-02-28 16:12:24 +00:00
|
|
|
|
2023-03-02 17:24:52 +00:00
|
|
|
final uri = await _getUri(coinPath, params);
|
|
|
|
final response = await get(uri);
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
if (response.statusCode != 200)
|
2023-03-02 17:24:52 +00:00
|
|
|
throw Exception('Unexpected http status: ${response.statusCode}');
|
|
|
|
|
|
|
|
final responseJSON = json.decode(response.body) as List<dynamic>;
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
if (responseJSON.isEmpty) throw Exception('No data');
|
2023-03-02 17:24:52 +00:00
|
|
|
|
|
|
|
final coinJson = responseJSON.first as Map<String, dynamic>;
|
2023-02-28 16:12:24 +00:00
|
|
|
|
2023-02-06 19:20:43 +00:00
|
|
|
return Limits(
|
2023-02-28 16:12:24 +00:00
|
|
|
min: coinJson['minimum'] as double,
|
|
|
|
max: coinJson['maximum'] as double,
|
2023-02-06 19:20:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<double> fetchRate(
|
|
|
|
{required CryptoCurrency from,
|
|
|
|
required CryptoCurrency to,
|
|
|
|
required double amount,
|
|
|
|
required bool isFixedRateMode,
|
|
|
|
required bool isReceiveAmount}) async {
|
|
|
|
try {
|
2023-10-25 20:58:25 +00:00
|
|
|
if (amount == 0) return 0.0;
|
2023-02-06 19:20:43 +00:00
|
|
|
|
|
|
|
final params = <String, String>{
|
|
|
|
'api_key': apiKey,
|
2023-08-24 21:30:53 +00:00
|
|
|
'ticker_from': _normalizeCurrency(from),
|
|
|
|
'ticker_to': _normalizeCurrency(to),
|
2023-02-06 19:20:43 +00:00
|
|
|
'network_from': _networkFor(from),
|
|
|
|
'network_to': _networkFor(to),
|
2023-02-06 19:42:17 +00:00
|
|
|
if (!isFixedRateMode) 'amount_from': amount.toString(),
|
|
|
|
if (isFixedRateMode) 'amount_to': amount.toString(),
|
2023-02-06 19:20:43 +00:00
|
|
|
'payment': isFixedRateMode ? 'True' : 'False',
|
|
|
|
'min_kycrating': 'C',
|
2023-02-06 20:20:46 +00:00
|
|
|
'markup': markup,
|
2023-02-06 19:20:43 +00:00
|
|
|
};
|
|
|
|
|
2023-03-02 17:24:52 +00:00
|
|
|
final uri = await _getUri(newRatePath, params);
|
2023-02-06 19:20:43 +00:00
|
|
|
final response = await get(uri);
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final fromAmount = double.parse(responseJSON['amount_from'].toString());
|
|
|
|
final toAmount = double.parse(responseJSON['amount_to'].toString());
|
|
|
|
final rateId = responseJSON['trade_id'] as String? ?? '';
|
|
|
|
|
2023-11-16 23:23:34 +00:00
|
|
|
var quotes = responseJSON['quotes']['quotes'] as List;
|
|
|
|
_provider = quotes.map((quote) => quote['provider']).toList();
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
if (rateId.isNotEmpty) _lastUsedRateId = rateId;
|
2023-02-06 19:20:43 +00:00
|
|
|
|
|
|
|
return isReceiveAmount ? (amount / fromAmount) : (toAmount / amount);
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
@override
|
|
|
|
Future<Trade> createTrade({required TradeRequest request, required bool isFixedRateMode}) async {
|
2023-11-16 23:23:34 +00:00
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
final params = {
|
|
|
|
'api_key': apiKey,
|
|
|
|
'ticker_from': _normalizeCurrency(request.fromCurrency),
|
|
|
|
'ticker_to': _normalizeCurrency(request.toCurrency),
|
|
|
|
'network_from': _networkFor(request.fromCurrency),
|
|
|
|
'network_to': _networkFor(request.toCurrency),
|
|
|
|
'payment': isFixedRateMode ? 'True' : 'False',
|
|
|
|
'min_kycrating': 'C',
|
|
|
|
'markup': markup,
|
|
|
|
if (!isFixedRateMode) 'amount_from': request.fromAmount,
|
|
|
|
if (isFixedRateMode) 'amount_to': request.toAmount,
|
|
|
|
'address': request.toAddress,
|
|
|
|
'refund': request.refundAddress
|
|
|
|
};
|
|
|
|
|
|
|
|
if (isFixedRateMode) {
|
|
|
|
await fetchRate(
|
|
|
|
from: request.fromCurrency,
|
|
|
|
to: request.toCurrency,
|
|
|
|
amount: double.tryParse(request.toAmount) ?? 0,
|
|
|
|
isFixedRateMode: true,
|
|
|
|
isReceiveAmount: true,
|
|
|
|
);
|
|
|
|
params['id'] = _lastUsedRateId;
|
|
|
|
}
|
|
|
|
|
2023-11-16 23:23:34 +00:00
|
|
|
|
|
|
|
String firstAvailableProvider = '';
|
|
|
|
|
|
|
|
for (var provider in _provider) {
|
|
|
|
if (providerStates.containsKey(provider) && providerStates[provider] == true) {
|
|
|
|
firstAvailableProvider = provider as String;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (firstAvailableProvider.isEmpty) {
|
|
|
|
throw Exception('No available provider is enabled');
|
|
|
|
}
|
|
|
|
|
|
|
|
params['provider'] = firstAvailableProvider;
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
final uri = await _getUri(createTradePath, params);
|
|
|
|
final response = await get(uri);
|
|
|
|
|
|
|
|
if (response.statusCode == 400) {
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final error = responseJSON['error'] as String;
|
|
|
|
final message = responseJSON['message'] as String;
|
|
|
|
throw Exception('${error}\n$message');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.statusCode != 200)
|
|
|
|
throw Exception('Unexpected http status: ${response.statusCode}');
|
|
|
|
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final id = responseJSON['trade_id'] as String;
|
|
|
|
final inputAddress = responseJSON['address_provider'] as String;
|
|
|
|
final refundAddress = responseJSON['refund_address'] as String;
|
|
|
|
final status = responseJSON['status'] as String;
|
|
|
|
final payoutAddress = responseJSON['address_user'] as String;
|
|
|
|
final date = responseJSON['date'] as String;
|
|
|
|
final password = responseJSON['password'] as String;
|
|
|
|
final providerId = responseJSON['id_provider'] as String;
|
|
|
|
final providerName = responseJSON['provider'] as String;
|
|
|
|
|
|
|
|
return Trade(
|
|
|
|
id: id,
|
|
|
|
from: request.fromCurrency,
|
|
|
|
to: request.toCurrency,
|
|
|
|
provider: description,
|
|
|
|
inputAddress: inputAddress,
|
|
|
|
refundAddress: refundAddress,
|
|
|
|
state: TradeState.deserialize(raw: status),
|
|
|
|
password: password,
|
|
|
|
providerId: providerId,
|
|
|
|
providerName: providerName,
|
|
|
|
createdAt: DateTime.tryParse(date)?.toLocal(),
|
|
|
|
amount: responseJSON['amount_from']?.toString() ?? request.fromAmount,
|
|
|
|
payoutAddress: payoutAddress);
|
|
|
|
}
|
|
|
|
|
2023-02-06 19:20:43 +00:00
|
|
|
@override
|
|
|
|
Future<Trade> findTradeById({required String id}) async {
|
2023-03-02 17:24:52 +00:00
|
|
|
final uri = await _getUri(tradePath, {'api_key': apiKey, 'id': id});
|
2023-02-06 19:20:43 +00:00
|
|
|
return get(uri).then((response) {
|
2023-10-25 20:58:25 +00:00
|
|
|
if (response.statusCode != 200)
|
2023-02-06 19:20:43 +00:00
|
|
|
throw Exception('Unexpected http status: ${response.statusCode}');
|
|
|
|
|
|
|
|
final responseListJson = json.decode(response.body) as List;
|
|
|
|
final responseJSON = responseListJson.first;
|
|
|
|
final id = responseJSON['trade_id'] as String;
|
2023-03-10 22:21:25 +00:00
|
|
|
final payoutAddress = responseJSON['address_user'] as String;
|
2023-02-06 19:20:43 +00:00
|
|
|
final refundAddress = responseJSON['refund_address'] as String;
|
2023-03-10 22:21:25 +00:00
|
|
|
final inputAddress = responseJSON['address_provider'] as String;
|
2023-02-06 19:20:43 +00:00
|
|
|
final fromAmount = responseJSON['amount_from']?.toString() ?? '0';
|
2023-02-07 17:39:39 +00:00
|
|
|
final password = responseJSON['password'] as String;
|
|
|
|
final providerId = responseJSON['id_provider'] as String;
|
|
|
|
final providerName = responseJSON['provider'] as String;
|
2023-02-06 19:20:43 +00:00
|
|
|
|
|
|
|
return Trade(
|
|
|
|
id: id,
|
2023-10-25 20:58:25 +00:00
|
|
|
from: CryptoCurrency.fromString(responseJSON['ticker_from'] as String),
|
|
|
|
to: CryptoCurrency.fromString(responseJSON['ticker_to'] as String),
|
2023-02-06 19:20:43 +00:00
|
|
|
provider: description,
|
|
|
|
inputAddress: inputAddress,
|
|
|
|
refundAddress: refundAddress,
|
2023-10-25 20:58:25 +00:00
|
|
|
createdAt: DateTime.parse(responseJSON['date'] as String),
|
2023-02-06 19:20:43 +00:00
|
|
|
amount: fromAmount,
|
2023-10-25 20:58:25 +00:00
|
|
|
state: TradeState.deserialize(raw: responseJSON['status'] as String),
|
2023-02-06 19:20:43 +00:00
|
|
|
payoutAddress: payoutAddress,
|
2023-02-07 17:39:39 +00:00
|
|
|
password: password,
|
|
|
|
providerId: providerId,
|
|
|
|
providerName: providerName,
|
2023-02-06 19:20:43 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
String _networkFor(CryptoCurrency currency) {
|
2023-02-09 16:52:24 +00:00
|
|
|
switch (currency) {
|
|
|
|
case CryptoCurrency.eth:
|
|
|
|
return 'ERC20';
|
|
|
|
case CryptoCurrency.maticpoly:
|
|
|
|
return 'Mainnet';
|
|
|
|
case CryptoCurrency.usdcpoly:
|
2023-12-13 14:03:07 +00:00
|
|
|
case CryptoCurrency.usdtPoly:
|
|
|
|
case CryptoCurrency.usdcEPoly:
|
2023-02-09 16:52:24 +00:00
|
|
|
return 'MATIC';
|
|
|
|
case CryptoCurrency.zec:
|
|
|
|
return 'Mainnet';
|
|
|
|
default:
|
|
|
|
return currency.tag != null ? _normalizeTag(currency.tag!) : 'Mainnet';
|
|
|
|
}
|
2023-02-06 19:20:43 +00:00
|
|
|
}
|
|
|
|
|
2023-08-24 21:30:53 +00:00
|
|
|
String _normalizeCurrency(CryptoCurrency currency) {
|
|
|
|
switch (currency) {
|
|
|
|
case CryptoCurrency.zec:
|
|
|
|
return 'zec';
|
2023-12-13 14:03:07 +00:00
|
|
|
case CryptoCurrency.usdcEPoly:
|
|
|
|
return 'usdce';
|
2023-08-24 21:30:53 +00:00
|
|
|
default:
|
|
|
|
return currency.title.toLowerCase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 19:20:43 +00:00
|
|
|
String _normalizeTag(String tag) {
|
|
|
|
switch (tag) {
|
|
|
|
case 'ETH':
|
|
|
|
return 'ERC20';
|
2023-02-13 14:51:26 +00:00
|
|
|
case 'TRX':
|
|
|
|
return 'TRC20';
|
2023-04-20 18:55:44 +00:00
|
|
|
case 'LN':
|
|
|
|
return 'Lightning';
|
2023-02-06 19:20:43 +00:00
|
|
|
default:
|
|
|
|
return tag.toLowerCase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 19:13:19 +00:00
|
|
|
Future<Uri> _getUri(String path, Map<String, String> queryParams) async {
|
|
|
|
final uri = Uri.http(onionApiAuthority, path, queryParams);
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
if (useTorOnly) return uri;
|
2023-03-01 21:44:15 +00:00
|
|
|
|
2023-02-06 19:20:43 +00:00
|
|
|
try {
|
|
|
|
await get(uri);
|
2023-03-01 21:44:15 +00:00
|
|
|
|
2023-03-02 19:13:19 +00:00
|
|
|
return uri;
|
2023-02-06 19:20:43 +00:00
|
|
|
} catch (e) {
|
2023-03-02 19:13:19 +00:00
|
|
|
return Uri.https(clearNetAuthority, path, queryParams);
|
2023-02-06 19:20:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|