2022-08-31 15:34:07 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'package:cake_wallet/exchange/exchange_pair.dart';
|
|
|
|
import 'package:cake_wallet/exchange/exchange_provider.dart';
|
|
|
|
import 'package:cake_wallet/exchange/exchange_provider_description.dart';
|
|
|
|
import 'package:cake_wallet/exchange/simpleswap/simpleswap_request.dart';
|
|
|
|
import 'package:cake_wallet/exchange/trade_not_created_exeption.dart';
|
|
|
|
import 'package:cake_wallet/exchange/trade_not_found_exeption.dart';
|
|
|
|
import 'package:cake_wallet/exchange/trade_state.dart';
|
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
|
|
|
import 'package:cake_wallet/exchange/trade_request.dart';
|
|
|
|
import 'package:cake_wallet/exchange/trade.dart';
|
|
|
|
import 'package:cake_wallet/exchange/limits.dart';
|
|
|
|
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
|
|
|
import 'package:http/http.dart';
|
|
|
|
|
|
|
|
class SimpleSwapExchangeProvider extends ExchangeProvider {
|
|
|
|
SimpleSwapExchangeProvider()
|
|
|
|
: super(
|
|
|
|
pairList: CryptoCurrency.all
|
2022-10-20 17:54:00 +00:00
|
|
|
.where((i) => i != CryptoCurrency.zaddr)
|
|
|
|
.map((i) => CryptoCurrency.all
|
|
|
|
.where((i) => i != CryptoCurrency.zaddr)
|
2022-12-06 13:40:40 +00:00
|
|
|
.map((k) => ExchangePair(from: i, to: k, reverse: true)))
|
2022-08-31 15:34:07 +00:00
|
|
|
.expand((i) => i)
|
|
|
|
.toList());
|
|
|
|
|
|
|
|
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';
|
|
|
|
static const apiKey = secrets.simpleSwapApiKey;
|
|
|
|
|
|
|
|
@override
|
|
|
|
ExchangeProviderDescription get description =>
|
|
|
|
ExchangeProviderDescription.simpleSwap;
|
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 {
|
|
|
|
if (amount == 0) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
final fromCurrency = _normalizeCryptoCurrency(from);
|
|
|
|
final toCurrency = _normalizeCryptoCurrency(to);
|
|
|
|
final params = <String, String>{
|
|
|
|
'api_key': apiKey,
|
|
|
|
'currency_from': fromCurrency,
|
|
|
|
'currency_to': toCurrency,
|
|
|
|
'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;
|
2022-08-31 15:34:07 +00:00
|
|
|
final data = json.decode(response.body) as String;
|
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
|
|
|
|
Future<bool> checkIsAvailable() async {
|
|
|
|
final uri = Uri.https(apiAuthority, getEstimatePath, <String, String>{'api_key': apiKey});
|
|
|
|
final response = await get(uri);
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
return !(response.statusCode == 403);
|
2022-08-31 15:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<Trade> createTrade({required TradeRequest request, required bool isFixedRateMode}) async {
|
2022-08-31 15:34:07 +00:00
|
|
|
final _request = request as SimpleSwapRequest;
|
2022-10-25 20:36:28 +00:00
|
|
|
final headers = {
|
2022-08-31 15:34:07 +00:00
|
|
|
'Content-Type': 'application/json'};
|
|
|
|
final params = <String, String>{
|
|
|
|
'api_key': apiKey,
|
|
|
|
};
|
|
|
|
final body = <String, dynamic>{
|
|
|
|
"currency_from": _normalizeCryptoCurrency(_request.from),
|
|
|
|
"currency_to": _normalizeCryptoCurrency(_request.to),
|
|
|
|
"amount": _request.amount,
|
2022-12-06 18:10:26 +00:00
|
|
|
"fixed": isFixedRateMode,
|
2022-08-31 15:34:07 +00:00
|
|
|
"user_refund_address": _request.refundAddress,
|
|
|
|
"address_to": _request.address
|
|
|
|
};
|
|
|
|
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;
|
|
|
|
final settleAddress = responseJSON['user_refund_address'] as String;
|
2022-10-25 20:36:28 +00:00
|
|
|
final extraId = responseJSON['extra_id_from'] as String?;
|
2022-08-31 15:34:07 +00:00
|
|
|
return Trade(
|
|
|
|
id: id,
|
|
|
|
provider: description,
|
|
|
|
from: _request.from,
|
|
|
|
to: _request.to,
|
|
|
|
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,
|
|
|
|
amount: _request.amount,
|
|
|
|
createdAt: DateTime.now(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<Limits> fetchLimits({
|
|
|
|
required CryptoCurrency from,
|
|
|
|
required CryptoCurrency to,
|
|
|
|
required bool isFixedRateMode}) async {
|
2022-08-31 15:34:07 +00:00
|
|
|
final fromCurrency = _normalizeCryptoCurrency(from);
|
|
|
|
final toCurrency = _normalizeCryptoCurrency(to);
|
|
|
|
final params = <String, dynamic>{
|
|
|
|
'api_key': apiKey,
|
|
|
|
'fixed': isFixedRateMode.toString(),
|
|
|
|
'currency_from': fromCurrency,
|
|
|
|
'currency_to': toCurrency,
|
|
|
|
};
|
|
|
|
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) {
|
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>;
|
2022-10-20 17:47:36 +00:00
|
|
|
final min = double.tryParse(responseJSON['min'] as String? ?? '');
|
|
|
|
final max = double.tryParse(responseJSON['max'] as String? ?? '');
|
2022-08-31 15:34:07 +00:00
|
|
|
|
|
|
|
return Limits(min: min, max: max);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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 from = CryptoCurrency.fromString(fromCurrency);
|
|
|
|
final toCurrency = responseJSON['currency_to'] as String;
|
|
|
|
final to = CryptoCurrency.fromString(toCurrency);
|
|
|
|
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;
|
|
|
|
final state = TradeState.deserialize(raw: status);
|
|
|
|
|
|
|
|
return Trade(
|
|
|
|
id: id,
|
|
|
|
from: from,
|
|
|
|
to: to,
|
2022-10-25 20:36:28 +00:00
|
|
|
extraId: extraId,
|
2022-08-31 15:34:07 +00:00
|
|
|
provider: description,
|
|
|
|
inputAddress: inputAddress,
|
|
|
|
amount: expectedSendAmount,
|
|
|
|
state: state,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get isAvailable => true;
|
|
|
|
|
2022-09-01 14:12:38 +00:00
|
|
|
@override
|
|
|
|
bool get isEnabled => true;
|
|
|
|
|
2022-12-06 17:23:46 +00:00
|
|
|
@override
|
|
|
|
bool get supportsFixedRate => false;
|
|
|
|
|
2022-08-31 15:34:07 +00:00
|
|
|
@override
|
|
|
|
String get title => 'SimpleSwap';
|
|
|
|
|
|
|
|
static String _normalizeCryptoCurrency(CryptoCurrency currency) {
|
|
|
|
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';
|
|
|
|
case CryptoCurrency.usdcsol:
|
|
|
|
return 'usdcspl';
|
2022-08-31 15:34:07 +00:00
|
|
|
default:
|
|
|
|
return currency.title.toLowerCase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|