2022-08-31 15:34:07 +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/provider/exchange_provider.dart';
|
2022-08-31 15:34:07 +00:00
|
|
|
import 'package:cake_wallet/exchange/exchange_provider_description.dart';
|
2023-10-25 20:58:25 +00:00
|
|
|
import 'package:cake_wallet/exchange/limits.dart';
|
|
|
|
import 'package:cake_wallet/exchange/trade.dart';
|
|
|
|
import 'package:cake_wallet/exchange/trade_not_created_exception.dart';
|
|
|
|
import 'package:cake_wallet/exchange/trade_not_found_exception.dart';
|
|
|
|
import 'package:cake_wallet/exchange/trade_request.dart';
|
2022-08-31 15:34:07 +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-04-14 04:39:08 +00:00
|
|
|
import 'package:cake_wallet/utils/device_info.dart';
|
2022-08-31 15:34:07 +00:00
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
|
|
|
import 'package:http/http.dart';
|
|
|
|
|
|
|
|
class SimpleSwapExchangeProvider extends ExchangeProvider {
|
2023-10-25 20:58:25 +00:00
|
|
|
SimpleSwapExchangeProvider() : super(pairList: supportedPairs(_notSupported));
|
|
|
|
|
|
|
|
static const List<CryptoCurrency> _notSupported = [
|
|
|
|
CryptoCurrency.zaddr,
|
|
|
|
CryptoCurrency.xhv,
|
|
|
|
];
|
2022-08-31 15:34:07 +00:00
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
static final apiKey =
|
|
|
|
DeviceInfo.instance.isMobile ? secrets.simpleSwapApiKey : secrets.simpleSwapApiKeyDesktop;
|
2022-08-31 15:34:07 +00:00
|
|
|
static const apiAuthority = 'api.simpleswap.io';
|
|
|
|
static const getEstimatePath = '/v1/get_estimated';
|
|
|
|
static const rangePath = '/v1/get_ranges';
|
|
|
|
static const getExchangePath = '/v1/get_exchange';
|
|
|
|
static const createExchangePath = '/v1/create_exchange';
|
|
|
|
|
|
|
|
@override
|
2023-10-25 20:58:25 +00:00
|
|
|
String get title => 'SimpleSwap';
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isAvailable => true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isEnabled => true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get supportsFixedRate => false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
ExchangeProviderDescription get description => ExchangeProviderDescription.simpleSwap;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> checkIsAvailable() async {
|
|
|
|
final uri = Uri.https(apiAuthority, getEstimatePath, <String, String>{'api_key': apiKey});
|
|
|
|
final response = await get(uri);
|
|
|
|
|
|
|
|
return !(response.statusCode == 403);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Limits> fetchLimits(
|
|
|
|
{required CryptoCurrency from,
|
|
|
|
required CryptoCurrency to,
|
|
|
|
required bool isFixedRateMode}) async {
|
|
|
|
final params = <String, dynamic>{
|
|
|
|
'api_key': apiKey,
|
|
|
|
'fixed': isFixedRateMode.toString(),
|
|
|
|
'currency_from': _normalizeCurrency(from),
|
|
|
|
'currency_to': _normalizeCurrency(to),
|
|
|
|
};
|
|
|
|
final uri = Uri.https(apiAuthority, rangePath, params);
|
|
|
|
|
|
|
|
final response = await get(uri);
|
|
|
|
|
|
|
|
if (response.statusCode == 500) {
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final error = responseJSON['message'] as String;
|
|
|
|
|
|
|
|
throw Exception('$error');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.statusCode != 200) {
|
|
|
|
throw Exception('Unexpected http status: ${response.statusCode}');
|
|
|
|
}
|
|
|
|
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final min = double.tryParse(responseJSON['min'] as String? ?? '');
|
|
|
|
final max = double.tryParse(responseJSON['max'] as String? ?? '');
|
|
|
|
|
|
|
|
return Limits(min: min, max: max);
|
|
|
|
}
|
2022-10-25 20:36:28 +00:00
|
|
|
|
2022-08-31 15:34:07 +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 {
|
2022-08-31 15:34:07 +00:00
|
|
|
try {
|
2023-10-25 20:58:25 +00:00
|
|
|
if (amount == 0) return 0.0;
|
|
|
|
|
|
|
|
final params = {
|
2022-08-31 15:34:07 +00:00
|
|
|
'api_key': apiKey,
|
2023-10-25 20:58:25 +00:00
|
|
|
'currency_from': _normalizeCurrency(from),
|
|
|
|
'currency_to': _normalizeCurrency(to),
|
2022-08-31 15:34:07 +00:00
|
|
|
'amount': amount.toString(),
|
2022-12-06 18:10:26 +00:00
|
|
|
'fixed': isFixedRateMode.toString()
|
2022-08-31 15:34:07 +00:00
|
|
|
};
|
|
|
|
final uri = Uri.https(apiAuthority, getEstimatePath, params);
|
|
|
|
final response = await get(uri);
|
|
|
|
|
2022-12-06 13:40:40 +00:00
|
|
|
if (response.body == "null") return 0.00;
|
2023-10-25 20:58:25 +00:00
|
|
|
|
2022-08-31 15:34:07 +00:00
|
|
|
final data = json.decode(response.body) as String;
|
2023-10-25 20:58:25 +00:00
|
|
|
|
2022-12-07 19:09:15 +00:00
|
|
|
return double.parse(data) / amount;
|
2022-08-31 15:34:07 +00:00
|
|
|
} catch (_) {
|
|
|
|
return 0.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-25 20:58:25 +00:00
|
|
|
final headers = {'Content-Type': 'application/json'};
|
|
|
|
final params = {'api_key': apiKey};
|
2022-08-31 15:34:07 +00:00
|
|
|
final body = <String, dynamic>{
|
2023-10-25 20:58:25 +00:00
|
|
|
"currency_from": _normalizeCurrency(request.fromCurrency),
|
|
|
|
"currency_to": _normalizeCurrency(request.toCurrency),
|
|
|
|
"amount": request.fromAmount,
|
2022-12-06 18:10:26 +00:00
|
|
|
"fixed": isFixedRateMode,
|
2024-11-05 09:33:24 +00:00
|
|
|
"user_refund_address": _normalizeAddress(request.refundAddress),
|
|
|
|
"address_to": _normalizeAddress(request.toAddress)
|
2022-08-31 15:34:07 +00:00
|
|
|
};
|
|
|
|
final uri = Uri.https(apiAuthority, createExchangePath, params);
|
|
|
|
|
|
|
|
final response = await post(uri, headers: headers, body: json.encode(body));
|
|
|
|
|
2022-10-20 17:23:36 +00:00
|
|
|
if (response.statusCode != 200 && response.statusCode != 201) {
|
2022-08-31 15:34:07 +00:00
|
|
|
if (response.statusCode == 400) {
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final error = responseJSON['message'] as String;
|
|
|
|
|
|
|
|
throw TradeNotCreatedException(description, description: error);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw TradeNotCreatedException(description);
|
|
|
|
}
|
|
|
|
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final id = responseJSON['id'] as String;
|
|
|
|
final inputAddress = responseJSON['address_from'] as String;
|
2023-01-16 15:27:49 +00:00
|
|
|
final payoutAddress = responseJSON['address_to'] as String;
|
2022-08-31 15:34:07 +00:00
|
|
|
final settleAddress = responseJSON['user_refund_address'] as String;
|
2022-10-25 20:36:28 +00:00
|
|
|
final extraId = responseJSON['extra_id_from'] as String?;
|
2024-07-23 00:20:55 +00:00
|
|
|
final receiveAmount = responseJSON['amount_to'] as String?;
|
2023-10-25 20:58:25 +00:00
|
|
|
|
2022-08-31 15:34:07 +00:00
|
|
|
return Trade(
|
|
|
|
id: id,
|
|
|
|
provider: description,
|
2023-10-25 20:58:25 +00:00
|
|
|
from: request.fromCurrency,
|
|
|
|
to: request.toCurrency,
|
2022-08-31 15:34:07 +00:00
|
|
|
inputAddress: inputAddress,
|
|
|
|
refundAddress: settleAddress,
|
2022-10-25 20:36:28 +00:00
|
|
|
extraId: extraId,
|
2022-08-31 15:34:07 +00:00
|
|
|
state: TradeState.created,
|
2023-10-25 20:58:25 +00:00
|
|
|
amount: request.fromAmount,
|
2024-07-23 00:20:55 +00:00
|
|
|
receiveAmount: receiveAmount ?? request.toAmount,
|
2023-01-16 15:27:49 +00:00
|
|
|
payoutAddress: payoutAddress,
|
2022-08-31 15:34:07 +00:00
|
|
|
createdAt: DateTime.now(),
|
2024-03-29 18:51:34 +00:00
|
|
|
isSendAll: isSendAll,
|
2022-08-31 15:34:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<Trade> findTradeById({required String id}) async {
|
2022-08-31 15:34:07 +00:00
|
|
|
final params = {'api_key': apiKey, 'id': id};
|
|
|
|
final uri = Uri.https(apiAuthority, getExchangePath, params);
|
|
|
|
final response = await get(uri);
|
|
|
|
|
|
|
|
if (response.statusCode == 404) {
|
|
|
|
throw TradeNotFoundException(id, provider: description);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.statusCode == 400) {
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final error = responseJSON['message'] as String;
|
|
|
|
|
|
|
|
throw TradeNotFoundException(id, provider: description, description: error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.statusCode != 200) {
|
2022-10-12 17:09:57 +00:00
|
|
|
throw Exception('Unexpected http status: ${response.statusCode}');
|
2022-08-31 15:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final fromCurrency = responseJSON['currency_from'] as String;
|
|
|
|
final toCurrency = responseJSON['currency_to'] as String;
|
|
|
|
final inputAddress = responseJSON['address_from'] as String;
|
|
|
|
final expectedSendAmount = responseJSON['expected_amount'].toString();
|
2022-10-25 20:36:28 +00:00
|
|
|
final extraId = responseJSON['extra_id_from'] as String?;
|
2022-08-31 15:34:07 +00:00
|
|
|
final status = responseJSON['status'] as String;
|
2023-01-16 15:27:49 +00:00
|
|
|
final payoutAddress = responseJSON['address_to'] as String;
|
2022-08-31 15:34:07 +00:00
|
|
|
|
|
|
|
return Trade(
|
|
|
|
id: id,
|
2023-10-25 20:58:25 +00:00
|
|
|
from: CryptoCurrency.fromString(fromCurrency),
|
|
|
|
to: CryptoCurrency.fromString(toCurrency),
|
2022-10-25 20:36:28 +00:00
|
|
|
extraId: extraId,
|
2022-08-31 15:34:07 +00:00
|
|
|
provider: description,
|
|
|
|
inputAddress: inputAddress,
|
|
|
|
amount: expectedSendAmount,
|
2023-10-25 20:58:25 +00:00
|
|
|
state: TradeState.deserialize(raw: status),
|
2023-01-16 15:27:49 +00:00
|
|
|
payoutAddress: payoutAddress,
|
2022-08-31 15:34:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
static String _normalizeCurrency(CryptoCurrency currency) {
|
2022-08-31 15:34:07 +00:00
|
|
|
switch (currency) {
|
|
|
|
case CryptoCurrency.zaddr:
|
|
|
|
return 'zec';
|
|
|
|
case CryptoCurrency.zec:
|
|
|
|
return 'zec';
|
|
|
|
case CryptoCurrency.bnb:
|
2022-10-12 17:09:57 +00:00
|
|
|
return currency.tag!.toLowerCase();
|
2022-08-31 15:34:07 +00:00
|
|
|
case CryptoCurrency.usdterc20:
|
2022-10-20 17:23:36 +00:00
|
|
|
return 'usdterc20';
|
|
|
|
case CryptoCurrency.usdttrc20:
|
|
|
|
return 'usdttrc20';
|
|
|
|
case CryptoCurrency.usdcpoly:
|
|
|
|
return 'usdcpoly';
|
2023-12-13 14:03:07 +00:00
|
|
|
case CryptoCurrency.usdtPoly:
|
|
|
|
return 'usdtpoly';
|
|
|
|
case CryptoCurrency.usdcEPoly:
|
|
|
|
return 'usdcepoly';
|
2022-10-20 17:23:36 +00:00
|
|
|
case CryptoCurrency.usdcsol:
|
|
|
|
return 'usdcspl';
|
2023-01-30 20:05:17 +00:00
|
|
|
case CryptoCurrency.matic:
|
2024-09-23 14:10:40 +00:00
|
|
|
return 'pol';
|
2023-01-30 20:05:17 +00:00
|
|
|
case CryptoCurrency.maticpoly:
|
|
|
|
return 'matic';
|
2022-08-31 15:34:07 +00:00
|
|
|
default:
|
|
|
|
return currency.title.toLowerCase();
|
|
|
|
}
|
|
|
|
}
|
2024-11-05 09:33:24 +00:00
|
|
|
|
|
|
|
String _normalizeAddress(String address) =>
|
|
|
|
address.startsWith('bitcoincash:') ? address.replaceFirst('bitcoincash:', '') : address;
|
2022-08-31 15:34:07 +00:00
|
|
|
}
|