2022-04-13 13:28:21 +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-04-13 13:28:21 +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-04-13 13:28:21 +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';
|
2022-04-13 13:28:21 +00:00
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
|
|
|
import 'package:http/http.dart';
|
|
|
|
|
|
|
|
class SideShiftExchangeProvider extends ExchangeProvider {
|
2023-10-25 20:58:25 +00:00
|
|
|
SideShiftExchangeProvider() : super(pairList: supportedPairs(_notSupported));
|
2022-04-13 13:28:21 +00:00
|
|
|
|
2022-10-20 17:23:36 +00:00
|
|
|
static const List<CryptoCurrency> _notSupported = [
|
|
|
|
CryptoCurrency.xhv,
|
|
|
|
CryptoCurrency.dcr,
|
|
|
|
CryptoCurrency.kmd,
|
|
|
|
CryptoCurrency.oxt,
|
|
|
|
CryptoCurrency.pivx,
|
|
|
|
CryptoCurrency.rune,
|
|
|
|
CryptoCurrency.rvn,
|
|
|
|
CryptoCurrency.scrt,
|
|
|
|
CryptoCurrency.stx,
|
2022-11-30 19:18:12 +00:00
|
|
|
CryptoCurrency.bttc,
|
2023-06-09 11:46:00 +00:00
|
|
|
CryptoCurrency.usdt,
|
|
|
|
CryptoCurrency.eos,
|
2022-10-20 17:23:36 +00:00
|
|
|
];
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
static const affiliateId = secrets.sideShiftAffiliateId;
|
|
|
|
static const apiBaseUrl = 'https://sideshift.ai/api';
|
|
|
|
static const rangePath = '/v2/pair';
|
|
|
|
static const orderPath = '/v2/shifts';
|
|
|
|
static const quotePath = '/v2/quotes';
|
|
|
|
static const permissionPath = '/v2/permissions';
|
2022-10-20 17:23:36 +00:00
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
@override
|
|
|
|
String get title => 'SideShift';
|
2022-10-20 17:23:36 +00:00
|
|
|
|
2022-04-13 13:28:21 +00:00
|
|
|
@override
|
2023-10-25 20:58:25 +00:00
|
|
|
bool get isAvailable => true;
|
2022-04-13 13:28:21 +00:00
|
|
|
|
|
|
|
@override
|
2023-10-25 20:58:25 +00:00
|
|
|
bool get isEnabled => true;
|
2023-06-09 11:46:00 +00:00
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
@override
|
|
|
|
bool get supportsFixedRate => true;
|
2023-06-09 11:46:00 +00:00
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
@override
|
|
|
|
ExchangeProviderDescription get description => ExchangeProviderDescription.sideShift;
|
2023-06-09 11:46:00 +00:00
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
@override
|
|
|
|
Future<bool> checkIsAvailable() async {
|
|
|
|
const url = apiBaseUrl + permissionPath;
|
|
|
|
final uri = Uri.parse(url);
|
|
|
|
final response = await get(uri);
|
|
|
|
|
|
|
|
if (response.statusCode == 500) {
|
2022-04-13 13:28:21 +00:00
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
2023-10-25 20:58:25 +00:00
|
|
|
final error = responseJSON['error']['message'] as String;
|
2022-04-13 13:28:21 +00:00
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
throw Exception('$error');
|
2022-04-13 13:28:21 +00:00
|
|
|
}
|
2023-10-25 20:58:25 +00:00
|
|
|
|
|
|
|
if (response.statusCode != 200) return false;
|
|
|
|
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
return responseJSON['createShift'] as bool;
|
2022-04-13 13:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-10-25 20:58:25 +00:00
|
|
|
Future<Limits> fetchLimits(
|
|
|
|
{required CryptoCurrency from,
|
|
|
|
required CryptoCurrency to,
|
|
|
|
required bool isFixedRateMode}) async {
|
|
|
|
final fromCurrency = isFixedRateMode ? to : from;
|
|
|
|
final toCurrency = isFixedRateMode ? from : to;
|
|
|
|
|
|
|
|
final fromNetwork = _networkFor(fromCurrency);
|
|
|
|
final toNetwork = _networkFor(toCurrency);
|
|
|
|
|
|
|
|
final url =
|
|
|
|
"$apiBaseUrl$rangePath/${fromCurrency.title.toLowerCase()}-$fromNetwork/${toCurrency.title.toLowerCase()}-$toNetwork";
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
final uri = Uri.parse(url);
|
|
|
|
final response = await get(uri);
|
2022-04-13 13:28:21 +00:00
|
|
|
|
|
|
|
if (response.statusCode == 500) {
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final error = responseJSON['error']['message'] as String;
|
|
|
|
|
|
|
|
throw Exception('$error');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.statusCode != 200) {
|
2023-10-25 20:58:25 +00:00
|
|
|
throw Exception('Unexpected http status: ${response.statusCode}');
|
2022-04-13 13:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
2023-10-25 20:58:25 +00:00
|
|
|
final min = double.tryParse(responseJSON['min'] as String? ?? '');
|
|
|
|
final max = double.tryParse(responseJSON['max'] as String? ?? '');
|
|
|
|
|
|
|
|
if (isFixedRateMode) {
|
|
|
|
final currentRate = double.parse(responseJSON['rate'] as String);
|
|
|
|
return Limits(
|
|
|
|
min: min != null ? (min * currentRate) : null,
|
|
|
|
max: max != null ? (max * currentRate) : null,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Limits(min: min, max: max);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
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 fromCurrency = from.title.toLowerCase();
|
|
|
|
final toCurrency = to.title.toLowerCase();
|
|
|
|
final depositNetwork = _networkFor(from);
|
|
|
|
final settleNetwork = _networkFor(to);
|
|
|
|
|
|
|
|
final url =
|
|
|
|
"$apiBaseUrl$rangePath/$fromCurrency-$depositNetwork/$toCurrency-$settleNetwork?amount=$amount";
|
|
|
|
|
|
|
|
final uri = Uri.parse(url);
|
|
|
|
final response = await get(uri);
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
|
|
|
|
return double.parse(responseJSON['rate'] as String);
|
|
|
|
} catch (_) {
|
|
|
|
return 0.00;
|
|
|
|
}
|
2022-04-13 13:28:21 +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-06-09 11:46:00 +00:00
|
|
|
String url = '';
|
2022-04-13 13:28:21 +00:00
|
|
|
final body = {
|
|
|
|
'affiliateId': affiliateId,
|
2023-10-25 20:58:25 +00:00
|
|
|
'settleAddress': request.toAddress,
|
|
|
|
'refundAddress': request.refundAddress,
|
2022-04-13 13:28:21 +00:00
|
|
|
};
|
2023-06-09 11:46:00 +00:00
|
|
|
|
|
|
|
if (isFixedRateMode) {
|
2023-10-25 20:58:25 +00:00
|
|
|
final quoteId = await _createQuote(request);
|
2023-06-09 11:46:00 +00:00
|
|
|
body['quoteId'] = quoteId;
|
|
|
|
|
|
|
|
url = apiBaseUrl + orderPath + '/fixed';
|
|
|
|
} else {
|
|
|
|
url = apiBaseUrl + orderPath + '/variable';
|
2023-12-13 14:03:07 +00:00
|
|
|
body["depositCoin"] = _normalizeCurrency(request.fromCurrency);
|
|
|
|
body["settleCoin"] = _normalizeCurrency(request.toCurrency);
|
2023-10-25 20:58:25 +00:00
|
|
|
body["settleNetwork"] = _networkFor(request.toCurrency);
|
|
|
|
body["depositNetwork"] = _networkFor(request.fromCurrency);
|
2023-06-09 11:46:00 +00:00
|
|
|
}
|
|
|
|
final headers = {'Content-Type': 'application/json'};
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
final uri = Uri.parse(url);
|
|
|
|
final response = await post(uri, headers: headers, body: json.encode(body));
|
2022-04-13 13:28:21 +00:00
|
|
|
|
|
|
|
if (response.statusCode != 201) {
|
|
|
|
if (response.statusCode == 400) {
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final error = responseJSON['error']['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;
|
2023-06-09 11:46:00 +00:00
|
|
|
final inputAddress = responseJSON['depositAddress'] as String;
|
|
|
|
final settleAddress = responseJSON['settleAddress'] as String;
|
|
|
|
final depositAmount = responseJSON['depositAmount'] as String?;
|
2022-04-13 13:28:21 +00:00
|
|
|
|
|
|
|
return Trade(
|
|
|
|
id: id,
|
|
|
|
provider: description,
|
2023-10-25 20:58:25 +00:00
|
|
|
from: request.fromCurrency,
|
|
|
|
to: request.toCurrency,
|
2022-04-13 13:28:21 +00:00
|
|
|
inputAddress: inputAddress,
|
|
|
|
refundAddress: settleAddress,
|
|
|
|
state: TradeState.created,
|
2023-10-25 20:58:25 +00:00
|
|
|
amount: depositAmount ?? request.fromAmount,
|
2023-01-16 15:27:49 +00:00
|
|
|
payoutAddress: settleAddress,
|
2022-04-13 13:28:21 +00:00
|
|
|
createdAt: DateTime.now(),
|
2024-03-29 18:51:34 +00:00
|
|
|
isSendAll: isSendAll,
|
2022-04-13 13:28:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<Trade> findTradeById({required String id}) async {
|
2022-04-13 13:28:21 +00:00
|
|
|
final url = apiBaseUrl + orderPath + '/' + id;
|
2022-10-12 17:09:57 +00:00
|
|
|
final uri = Uri.parse(url);
|
|
|
|
final response = await get(uri);
|
2022-04-13 13:28:21 +00:00
|
|
|
|
|
|
|
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['error']['message'] as String;
|
|
|
|
|
2023-06-09 11:46:00 +00:00
|
|
|
throw TradeNotFoundException(id, provider: description, description: error);
|
2022-04-13 13:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (response.statusCode != 200) {
|
2022-10-12 17:09:57 +00:00
|
|
|
throw Exception('Unexpected http status: ${response.statusCode}');
|
2022-04-13 13:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
2023-06-09 11:46:00 +00:00
|
|
|
final fromCurrency = responseJSON['depositCoin'] as String;
|
|
|
|
final toCurrency = responseJSON['settleCoin'] as String;
|
|
|
|
final inputAddress = responseJSON['depositAddress'] as String;
|
|
|
|
final expectedSendAmount = responseJSON['depositAmount'] as String?;
|
|
|
|
final status = responseJSON['status'] as String?;
|
|
|
|
final settleAddress = responseJSON['settleAddress'] as String;
|
|
|
|
final isVariable = (responseJSON['type'] as String) == 'variable';
|
|
|
|
final expiredAtRaw = responseJSON['expiresAt'] as String;
|
|
|
|
final expiredAt = isVariable ? null : DateTime.tryParse(expiredAtRaw)?.toLocal();
|
2022-04-13 13:28:21 +00:00
|
|
|
|
|
|
|
return Trade(
|
2023-06-09 11:46:00 +00:00
|
|
|
id: id,
|
2023-10-25 20:58:25 +00:00
|
|
|
from: CryptoCurrency.fromString(fromCurrency),
|
|
|
|
to: CryptoCurrency.fromString(toCurrency),
|
2023-06-09 11:46:00 +00:00
|
|
|
provider: description,
|
|
|
|
inputAddress: inputAddress,
|
|
|
|
amount: expectedSendAmount ?? '',
|
2023-10-25 20:58:25 +00:00
|
|
|
state: TradeState.deserialize(raw: status ?? 'created'),
|
2023-06-09 11:46:00 +00:00
|
|
|
expiredAt: expiredAt,
|
|
|
|
payoutAddress: settleAddress);
|
2022-04-13 13:28:21 +00:00
|
|
|
}
|
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
Future<String> _createQuote(TradeRequest request) async {
|
|
|
|
final url = apiBaseUrl + quotePath;
|
|
|
|
final headers = {'Content-Type': 'application/json'};
|
|
|
|
final body = {
|
2023-12-13 14:03:07 +00:00
|
|
|
'depositCoin': _normalizeCurrency(request.fromCurrency),
|
|
|
|
'settleCoin': _normalizeCurrency(request.toCurrency),
|
2023-10-25 20:58:25 +00:00
|
|
|
'affiliateId': affiliateId,
|
|
|
|
'settleAmount': request.toAmount,
|
|
|
|
'settleNetwork': _networkFor(request.toCurrency),
|
|
|
|
'depositNetwork': _networkFor(request.fromCurrency),
|
|
|
|
};
|
|
|
|
final uri = Uri.parse(url);
|
|
|
|
final response = await post(uri, headers: headers, body: json.encode(body));
|
2022-04-13 13:28:21 +00:00
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
if (response.statusCode != 201) {
|
|
|
|
if (response.statusCode == 400) {
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final error = responseJSON['error']['message'] as String;
|
2022-09-01 14:12:38 +00:00
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
throw TradeNotCreatedException(description, description: error);
|
|
|
|
}
|
2022-12-06 17:23:46 +00:00
|
|
|
|
2023-10-25 20:58:25 +00:00
|
|
|
throw TradeNotCreatedException(description);
|
|
|
|
}
|
|
|
|
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
|
|
|
|
return responseJSON['id'] as String;
|
|
|
|
}
|
2022-04-13 13:28:21 +00:00
|
|
|
|
2023-12-13 14:03:07 +00:00
|
|
|
String _normalizeCurrency(CryptoCurrency currency) {
|
|
|
|
switch (currency) {
|
|
|
|
case CryptoCurrency.usdcEPoly:
|
|
|
|
return 'usdc';
|
|
|
|
default:
|
|
|
|
return currency.title.toLowerCase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-09 11:46:00 +00:00
|
|
|
String _networkFor(CryptoCurrency currency) =>
|
|
|
|
currency.tag != null ? _normalizeTag(currency.tag!) : 'mainnet';
|
|
|
|
|
|
|
|
String _normalizeTag(String tag) {
|
|
|
|
switch (tag) {
|
|
|
|
case 'ETH':
|
|
|
|
return 'ethereum';
|
|
|
|
case 'TRX':
|
|
|
|
return 'tron';
|
|
|
|
case 'LN':
|
|
|
|
return 'lightning';
|
|
|
|
case 'POLY':
|
2022-10-20 17:23:36 +00:00
|
|
|
return 'polygon';
|
2023-06-09 11:46:00 +00:00
|
|
|
case 'ZEC':
|
|
|
|
return 'zcash';
|
|
|
|
case 'AVAXC':
|
|
|
|
return 'avax';
|
2022-04-13 13:28:21 +00:00
|
|
|
default:
|
2023-06-09 11:46:00 +00:00
|
|
|
return tag.toLowerCase();
|
2022-04-13 13:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|