2023-10-12 00:20:19 +00:00
|
|
|
import 'dart:convert';
|
2023-10-25 20:58:25 +00:00
|
|
|
|
2023-10-12 00:20:19 +00:00
|
|
|
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
2023-10-25 20:58:25 +00:00
|
|
|
import 'package:cake_wallet/exchange/exchange_provider_description.dart';
|
2023-10-12 00:20:19 +00:00
|
|
|
import 'package:cake_wallet/exchange/limits.dart';
|
2023-10-25 20:58:25 +00:00
|
|
|
import 'package:cake_wallet/exchange/provider/exchange_provider.dart';
|
2023-10-12 00:20:19 +00:00
|
|
|
import 'package:cake_wallet/exchange/trade.dart';
|
2023-10-25 20:58:25 +00:00
|
|
|
import 'package:cake_wallet/exchange/trade_not_found_exception.dart';
|
2023-10-12 00:20:19 +00:00
|
|
|
import 'package:cake_wallet/exchange/trade_request.dart';
|
|
|
|
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';
|
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
|
|
|
import 'package:http/http.dart';
|
2023-10-12 00:20:19 +00:00
|
|
|
|
|
|
|
class ExolixExchangeProvider extends ExchangeProvider {
|
2023-10-25 20:58:25 +00:00
|
|
|
ExolixExchangeProvider() : super(pairList: supportedPairs(_notSupported));
|
2023-10-12 00:20:19 +00:00
|
|
|
|
|
|
|
static final apiKey = secrets.exolixApiKey;
|
|
|
|
static const apiBaseUrl = 'exolix.com';
|
|
|
|
static const transactionsPath = '/api/v2/transactions';
|
|
|
|
static const ratePath = '/api/v2/rate';
|
|
|
|
|
|
|
|
static const List<CryptoCurrency> _notSupported = [
|
|
|
|
CryptoCurrency.usdt,
|
|
|
|
CryptoCurrency.xhv,
|
|
|
|
CryptoCurrency.btt,
|
|
|
|
CryptoCurrency.firo,
|
|
|
|
CryptoCurrency.zaddr,
|
|
|
|
CryptoCurrency.xvg,
|
|
|
|
CryptoCurrency.kmd,
|
|
|
|
CryptoCurrency.paxg,
|
|
|
|
CryptoCurrency.rune,
|
|
|
|
CryptoCurrency.scrt,
|
|
|
|
CryptoCurrency.btcln,
|
|
|
|
CryptoCurrency.cro,
|
|
|
|
CryptoCurrency.ftm,
|
|
|
|
CryptoCurrency.frax,
|
|
|
|
CryptoCurrency.gusd,
|
|
|
|
CryptoCurrency.gtc,
|
|
|
|
CryptoCurrency.weth,
|
|
|
|
];
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get title => 'Exolix';
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isAvailable => true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isEnabled => true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get supportsFixedRate => true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
ExchangeProviderDescription get description => ExchangeProviderDescription.exolix;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> checkIsAvailable() async => true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Limits> fetchLimits(
|
|
|
|
{required CryptoCurrency from,
|
|
|
|
required CryptoCurrency to,
|
|
|
|
required bool isFixedRateMode}) async {
|
|
|
|
final params = <String, String>{
|
2023-10-25 20:58:25 +00:00
|
|
|
'rateType': _getRateType(isFixedRateMode),
|
2023-10-12 00:20:19 +00:00
|
|
|
'amount': '1',
|
2024-03-18 15:45:11 +00:00
|
|
|
'apiToken': apiKey,
|
2023-10-12 00:20:19 +00:00
|
|
|
};
|
|
|
|
if (isFixedRateMode) {
|
|
|
|
params['coinFrom'] = _normalizeCurrency(to);
|
|
|
|
params['coinTo'] = _normalizeCurrency(from);
|
|
|
|
params['networkFrom'] = _networkFor(to);
|
|
|
|
params['networkTo'] = _networkFor(from);
|
|
|
|
} else {
|
|
|
|
params['coinFrom'] = _normalizeCurrency(from);
|
|
|
|
params['coinTo'] = _normalizeCurrency(to);
|
|
|
|
params['networkFrom'] = _networkFor(from);
|
|
|
|
params['networkTo'] = _networkFor(to);
|
|
|
|
}
|
|
|
|
final uri = Uri.https(apiBaseUrl, ratePath, params);
|
|
|
|
final response = await get(uri);
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
if (response.statusCode != 200)
|
2023-10-12 00:20:19 +00:00
|
|
|
throw Exception('Unexpected http status: ${response.statusCode}');
|
|
|
|
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
return Limits(min: responseJSON['minAmount'] as double?);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-10-25 20:58:25 +00:00
|
|
|
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 = {
|
|
|
|
'coinFrom': _normalizeCurrency(from),
|
|
|
|
'coinTo': _normalizeCurrency(to),
|
|
|
|
'networkFrom': _networkFor(from),
|
|
|
|
'networkTo': _networkFor(to),
|
|
|
|
'rateType': _getRateType(isFixedRateMode),
|
|
|
|
'apiToken': apiKey,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (isReceiveAmount)
|
|
|
|
params['withdrawalAmount'] = amount.toString();
|
|
|
|
else
|
|
|
|
params['amount'] = amount.toString();
|
|
|
|
|
|
|
|
final uri = Uri.https(apiBaseUrl, ratePath, params);
|
|
|
|
final response = await get(uri);
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
|
|
|
|
if (response.statusCode != 200) {
|
|
|
|
final message = responseJSON['message'] as String?;
|
|
|
|
throw Exception(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return responseJSON['rate'] as double;
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
}
|
2023-10-12 00:20:19 +00:00
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
@override
|
2024-03-29 18:51:34 +00:00
|
|
|
Future<Trade> createTrade({
|
|
|
|
required TradeRequest request,
|
|
|
|
required bool isFixedRateMode,
|
|
|
|
required bool isSendAll,
|
|
|
|
}) async {
|
2023-10-12 00:20:19 +00:00
|
|
|
final headers = {'Content-Type': 'application/json'};
|
2023-10-25 20:58:25 +00:00
|
|
|
final body = {
|
|
|
|
'coinFrom': _normalizeCurrency(request.fromCurrency),
|
|
|
|
'coinTo': _normalizeCurrency(request.toCurrency),
|
|
|
|
'networkFrom': _networkFor(request.fromCurrency),
|
|
|
|
'networkTo': _networkFor(request.toCurrency),
|
2024-11-05 09:33:24 +00:00
|
|
|
'withdrawalAddress': _normalizeAddress(request.toAddress),
|
|
|
|
'refundAddress': _normalizeAddress(request.refundAddress),
|
2023-10-25 20:58:25 +00:00
|
|
|
'rateType': _getRateType(isFixedRateMode),
|
2023-10-12 00:20:19 +00:00
|
|
|
'apiToken': apiKey,
|
|
|
|
};
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
if (isFixedRateMode)
|
|
|
|
body['withdrawalAmount'] = request.toAmount;
|
|
|
|
else
|
|
|
|
body['amount'] = request.fromAmount;
|
2023-10-12 00:20:19 +00:00
|
|
|
|
|
|
|
final uri = Uri.https(apiBaseUrl, transactionsPath);
|
|
|
|
final response = await post(uri, headers: headers, body: json.encode(body));
|
|
|
|
|
|
|
|
if (response.statusCode == 400) {
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final errors = responseJSON['errors'] as Map<String, String>;
|
|
|
|
final errorMessage = errors.values.join(', ');
|
|
|
|
throw Exception(errorMessage);
|
|
|
|
}
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
if (response.statusCode != 200 && response.statusCode != 201)
|
2023-10-12 00:20:19 +00:00
|
|
|
throw Exception('Unexpected http status: ${response.statusCode}');
|
|
|
|
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final id = responseJSON['id'] as String;
|
|
|
|
final inputAddress = responseJSON['depositAddress'] as String;
|
|
|
|
final refundAddress = responseJSON['refundAddress'] as String?;
|
|
|
|
final extraId = responseJSON['depositExtraId'] as String?;
|
|
|
|
final payoutAddress = responseJSON['withdrawalAddress'] as String;
|
|
|
|
final amount = responseJSON['amount'].toString();
|
2024-07-23 00:20:55 +00:00
|
|
|
final receiveAmount = responseJSON['amountTo']?.toString();
|
2023-10-12 00:20:19 +00:00
|
|
|
|
|
|
|
return Trade(
|
2024-07-23 00:20:55 +00:00
|
|
|
id: id,
|
|
|
|
from: request.fromCurrency,
|
|
|
|
to: request.toCurrency,
|
|
|
|
provider: description,
|
|
|
|
inputAddress: inputAddress,
|
|
|
|
refundAddress: refundAddress,
|
|
|
|
extraId: extraId,
|
|
|
|
createdAt: DateTime.now(),
|
|
|
|
amount: amount,
|
|
|
|
receiveAmount:receiveAmount ?? request.toAmount,
|
|
|
|
state: TradeState.created,
|
|
|
|
payoutAddress: payoutAddress,
|
|
|
|
isSendAll: isSendAll,
|
|
|
|
);
|
2023-10-12 00:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Trade> findTradeById({required String id}) async {
|
2023-10-25 20:58:25 +00:00
|
|
|
final findTradeByIdPath = '$transactionsPath/$id';
|
2023-10-12 00:20:19 +00:00
|
|
|
final uri = Uri.https(apiBaseUrl, findTradeByIdPath);
|
|
|
|
final response = await get(uri);
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
if (response.statusCode == 404) throw TradeNotFoundException(id, provider: description);
|
2023-10-12 00:20:19 +00:00
|
|
|
|
|
|
|
if (response.statusCode == 400) {
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final errors = responseJSON['errors'] as Map<String, String>;
|
|
|
|
final errorMessage = errors.values.join(', ');
|
|
|
|
|
|
|
|
throw TradeNotFoundException(id, provider: description, description: errorMessage);
|
|
|
|
}
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
if (response.statusCode != 200)
|
2023-10-12 00:20:19 +00:00
|
|
|
throw Exception('Unexpected http status: ${response.statusCode}');
|
|
|
|
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final coinFrom = responseJSON['coinFrom']['coinCode'] as String;
|
|
|
|
final coinTo = responseJSON['coinTo']['coinCode'] as String;
|
|
|
|
final inputAddress = responseJSON['depositAddress'] as String;
|
|
|
|
final amount = responseJSON['amount'].toString();
|
|
|
|
final status = responseJSON['status'] as String;
|
|
|
|
final extraId = responseJSON['depositExtraId'] as String?;
|
|
|
|
final outputTransaction = responseJSON['hashOut']['hash'] as String?;
|
|
|
|
final payoutAddress = responseJSON['withdrawalAddress'] as String;
|
|
|
|
|
|
|
|
return Trade(
|
|
|
|
id: id,
|
2023-10-25 20:58:25 +00:00
|
|
|
from: CryptoCurrency.fromString(coinFrom),
|
|
|
|
to: CryptoCurrency.fromString(coinTo),
|
2023-10-12 00:20:19 +00:00
|
|
|
provider: description,
|
|
|
|
inputAddress: inputAddress,
|
|
|
|
amount: amount,
|
2023-10-25 20:58:25 +00:00
|
|
|
state: TradeState.deserialize(raw: _prepareStatus(status)),
|
2023-10-12 00:20:19 +00:00
|
|
|
extraId: extraId,
|
|
|
|
outputTransaction: outputTransaction,
|
|
|
|
payoutAddress: payoutAddress);
|
|
|
|
}
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
String _getRateType(bool isFixedRate) => isFixedRate ? 'fixed' : 'float';
|
2023-10-12 00:20:19 +00:00
|
|
|
|
|
|
|
String _prepareStatus(String status) {
|
|
|
|
switch (status) {
|
|
|
|
case 'deleted':
|
|
|
|
case 'error':
|
|
|
|
return 'overdue';
|
|
|
|
default:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String _networkFor(CryptoCurrency currency) {
|
|
|
|
switch (currency) {
|
|
|
|
case CryptoCurrency.arb:
|
|
|
|
return 'ARBITRUM';
|
|
|
|
default:
|
|
|
|
return currency.tag != null ? _normalizeTag(currency.tag!) : currency.title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String _normalizeCurrency(CryptoCurrency currency) {
|
|
|
|
switch (currency) {
|
|
|
|
case CryptoCurrency.nano:
|
|
|
|
return 'XNO';
|
|
|
|
case CryptoCurrency.bttc:
|
|
|
|
return 'BTT';
|
|
|
|
case CryptoCurrency.zec:
|
|
|
|
return 'ZEC';
|
|
|
|
default:
|
|
|
|
return currency.title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String _normalizeTag(String tag) {
|
|
|
|
switch (tag) {
|
|
|
|
case 'POLY':
|
|
|
|
return 'Polygon';
|
|
|
|
default:
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
}
|
2024-11-05 09:33:24 +00:00
|
|
|
|
|
|
|
String _normalizeAddress(String address) =>
|
|
|
|
address.startsWith('bitcoincash:') ? address.replaceFirst('bitcoincash:', '') : address;
|
2023-10-12 00:20:19 +00:00
|
|
|
}
|