2020-01-04 19:31:52 +00:00
|
|
|
import 'dart:convert';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/exchange/trade_not_found_exeption.dart';
|
2020-01-04 19:31:52 +00:00
|
|
|
import 'package:http/http.dart';
|
|
|
|
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/exchange/exchange_pair.dart';
|
|
|
|
import 'package:cake_wallet/exchange/exchange_provider.dart';
|
|
|
|
import 'package:cake_wallet/exchange/limits.dart';
|
|
|
|
import 'package:cake_wallet/exchange/trade.dart';
|
|
|
|
import 'package:cake_wallet/exchange/trade_request.dart';
|
|
|
|
import 'package:cake_wallet/exchange/trade_state.dart';
|
|
|
|
import 'package:cake_wallet/exchange/changenow/changenow_request.dart';
|
|
|
|
import 'package:cake_wallet/exchange/exchange_provider_description.dart';
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
class ChangeNowExchangeProvider extends ExchangeProvider {
|
2020-01-08 12:26:34 +00:00
|
|
|
ChangeNowExchangeProvider()
|
2022-01-26 15:44:15 +00:00
|
|
|
: _lastUsedRateId = '',
|
|
|
|
super(
|
2020-01-08 12:26:34 +00:00
|
|
|
pairList: CryptoCurrency.all
|
2022-09-01 18:46:14 +00:00
|
|
|
.where((i) => i != CryptoCurrency.xhv)
|
2020-11-10 14:58:40 +00:00
|
|
|
.map((i) => CryptoCurrency.all
|
2022-09-01 18:46:14 +00:00
|
|
|
.where((i) => i != CryptoCurrency.xhv)
|
2022-12-06 17:23:46 +00:00
|
|
|
.map((k) => ExchangePair(from: i, to: k, reverse: true)))
|
2020-01-08 12:26:34 +00:00
|
|
|
.expand((i) => i)
|
|
|
|
.toList());
|
|
|
|
|
2021-03-16 13:20:46 +00:00
|
|
|
static const apiKey = secrets.changeNowApiKey;
|
2022-01-26 15:44:15 +00:00
|
|
|
static const apiAuthority = 'api.changenow.io';
|
|
|
|
static const createTradePath = '/v2/exchange';
|
|
|
|
static const findTradeByIdPath = '/v2/exchange/by-id';
|
|
|
|
static const estimatedAmountPath = '/v2/exchange/estimated-amount';
|
|
|
|
static const rangePath = '/v2/exchange/range';
|
|
|
|
static const apiHeaderKey = 'x-changenow-api-key';
|
2020-01-04 19:31:52 +00:00
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
@override
|
2020-01-04 19:31:52 +00:00
|
|
|
String get title => 'ChangeNOW';
|
2020-01-08 12:26:34 +00:00
|
|
|
|
2020-11-10 14:58:40 +00:00
|
|
|
@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 => true;
|
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
@override
|
2020-01-04 19:31:52 +00:00
|
|
|
ExchangeProviderDescription get description =>
|
|
|
|
ExchangeProviderDescription.changeNow;
|
|
|
|
|
2020-11-10 14:58:40 +00:00
|
|
|
@override
|
|
|
|
Future<bool> checkIsAvailable() async => true;
|
|
|
|
|
2022-01-26 15:44:15 +00:00
|
|
|
String _lastUsedRateId;
|
|
|
|
|
|
|
|
static String getFlow(bool isFixedRate) => isFixedRate ? 'fixed-rate' : 'standard';
|
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
@override
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<Limits> fetchLimits({
|
|
|
|
required CryptoCurrency from,
|
|
|
|
required CryptoCurrency to,
|
|
|
|
required bool isFixedRateMode}) async {
|
2022-01-26 15:44:15 +00:00
|
|
|
final headers = {apiHeaderKey: apiKey};
|
|
|
|
final normalizedFrom = normalizeCryptoCurrency(from);
|
|
|
|
final normalizedTo = normalizeCryptoCurrency(to);
|
|
|
|
final flow = getFlow(isFixedRateMode);
|
|
|
|
final params = <String, String>{
|
|
|
|
'fromCurrency': normalizedFrom,
|
|
|
|
'toCurrency': normalizedTo,
|
2022-02-08 08:57:02 +00:00
|
|
|
'fromNetwork': networkFor(from),
|
|
|
|
'toNetwork': networkFor(to),
|
2022-01-26 15:44:15 +00:00
|
|
|
'flow': flow};
|
|
|
|
final uri = Uri.https(apiAuthority, rangePath, params);
|
|
|
|
final response = await get(uri, headers: headers);
|
|
|
|
|
|
|
|
if (response.statusCode == 400) {
|
2021-02-18 19:08:52 +00:00
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
2022-01-26 15:44:15 +00:00
|
|
|
final error = responseJSON['error'] as String;
|
|
|
|
final message = responseJSON['message'] as String;
|
|
|
|
throw Exception('${error}\n$message');
|
|
|
|
}
|
2021-02-18 19:08:52 +00:00
|
|
|
|
2022-01-26 15:44:15 +00:00
|
|
|
if (response.statusCode != 200) {
|
2022-10-12 17:09:57 +00:00
|
|
|
throw Exception('Unexpected http status: ${response.statusCode}');
|
2021-02-18 19:08:52 +00:00
|
|
|
}
|
2022-01-26 15:44:15 +00:00
|
|
|
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
return Limits(
|
2022-10-19 22:20:11 +00:00
|
|
|
min: responseJSON['minAmount'] as double?,
|
|
|
|
max: responseJSON['maxAmount'] as double?);
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
@override
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<Trade> createTrade({required TradeRequest request, required bool isFixedRateMode}) async {
|
2020-01-04 19:31:52 +00:00
|
|
|
final _request = request as ChangeNowRequest;
|
2022-01-26 15:44:15 +00:00
|
|
|
final headers = {
|
|
|
|
apiHeaderKey: apiKey,
|
|
|
|
'Content-Type': 'application/json'};
|
|
|
|
final flow = getFlow(isFixedRateMode);
|
2022-12-07 23:21:45 +00:00
|
|
|
final type = isFixedRateMode ? 'reverse' : 'direct';
|
2022-01-26 15:44:15 +00:00
|
|
|
final body = <String, String>{
|
add new coins to exchange (#499)
* add new coins
APE, AVAXC, BTT, BTTBSC, DOGE, FIRO, USDTTRC20, HBAR, SC, SOL, USDC, USDCSOL, ZEN, XVG
* remove UST icon
* remove xhv from excludeDepositCurrencies
* remove xhv from excludeReceiveCurrencies
2022-09-07 12:10:21 +00:00
|
|
|
'fromCurrency': normalizeCryptoCurrency(_request.from),
|
2022-02-08 08:57:02 +00:00
|
|
|
'toCurrency': normalizeCryptoCurrency(_request.to),
|
|
|
|
'fromNetwork': networkFor(_request.from),
|
|
|
|
'toNetwork': networkFor(_request.to),
|
2022-12-07 23:21:45 +00:00
|
|
|
if (!isFixedRateMode) 'fromAmount': _request.fromAmount,
|
|
|
|
if (isFixedRateMode) 'toAmount': _request.toAmount,
|
2020-01-04 19:31:52 +00:00
|
|
|
'address': _request.address,
|
2022-01-26 15:44:15 +00:00
|
|
|
'flow': flow,
|
2022-12-07 23:21:45 +00:00
|
|
|
'type': type,
|
2020-01-04 19:31:52 +00:00
|
|
|
'refundAddress': _request.refundAddress
|
|
|
|
};
|
|
|
|
|
2022-01-26 15:44:15 +00:00
|
|
|
if (isFixedRateMode) {
|
2022-12-06 17:23:46 +00:00
|
|
|
// since we schedule to calculate the rate every 5 seconds we need to ensure that
|
|
|
|
// we have the latest rate id with the given inputs before creating the trade
|
2022-12-08 19:14:42 +00:00
|
|
|
await fetchRate(
|
2022-12-07 19:09:15 +00:00
|
|
|
from: _request.from,
|
|
|
|
to: _request.to,
|
2022-12-06 17:23:46 +00:00
|
|
|
amount: double.tryParse(_request.toAmount) ?? 0,
|
|
|
|
isFixedRateMode: true,
|
|
|
|
isReceiveAmount: true,
|
|
|
|
);
|
2022-01-26 15:44:15 +00:00
|
|
|
body['rateId'] = _lastUsedRateId;
|
|
|
|
}
|
2020-01-08 12:26:34 +00:00
|
|
|
|
2022-01-26 15:44:15 +00:00
|
|
|
final uri = Uri.https(apiAuthority, createTradePath);
|
|
|
|
final response = await post(uri, headers: headers, body: json.encode(body));
|
2022-12-01 16:09:21 +00:00
|
|
|
|
2022-01-26 15:44:15 +00:00
|
|
|
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');
|
|
|
|
}
|
2020-01-04 19:31:52 +00:00
|
|
|
|
2022-01-26 15:44:15 +00:00
|
|
|
if (response.statusCode != 200) {
|
2022-10-12 17:09:57 +00:00
|
|
|
throw Exception('Unexpected http status: ${response.statusCode}');
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final id = responseJSON['id'] as String;
|
2022-12-01 16:21:19 +00:00
|
|
|
final inputAddress = responseJSON['payinAddress'] as String;
|
|
|
|
final refundAddress = responseJSON['refundAddress'] as String;
|
2022-12-01 16:09:21 +00:00
|
|
|
final extraId = responseJSON['payinExtraId'] as String?;
|
2023-01-16 15:27:49 +00:00
|
|
|
final payoutAddress = responseJSON['payoutAddress'] as String;
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
return Trade(
|
2020-01-08 12:26:34 +00:00
|
|
|
id: id,
|
2020-01-04 19:31:52 +00:00
|
|
|
from: _request.from,
|
|
|
|
to: _request.to,
|
|
|
|
provider: description,
|
2020-01-08 12:26:34 +00:00
|
|
|
inputAddress: inputAddress,
|
|
|
|
refundAddress: refundAddress,
|
|
|
|
extraId: extraId,
|
2020-01-04 19:31:52 +00:00
|
|
|
createdAt: DateTime.now(),
|
2022-12-07 23:21:45 +00:00
|
|
|
amount: responseJSON['fromAmount']?.toString() ?? _request.fromAmount,
|
2023-01-16 15:27:49 +00:00
|
|
|
state: TradeState.created,
|
|
|
|
payoutAddress: payoutAddress);
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
@override
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<Trade> findTradeById({required String id}) async {
|
2022-01-26 15:44:15 +00:00
|
|
|
final headers = {apiHeaderKey: apiKey};
|
|
|
|
final params = <String, String>{'id': id};
|
|
|
|
final uri = Uri.https(apiAuthority,findTradeByIdPath, params);
|
|
|
|
final response = await get(uri, headers: headers);
|
2020-01-04 19:31:52 +00:00
|
|
|
|
2022-01-26 15:44:15 +00:00
|
|
|
if (response.statusCode == 404) {
|
|
|
|
throw TradeNotFoundException(id, provider: description);
|
|
|
|
}
|
2020-01-08 12:26:34 +00:00
|
|
|
|
2022-01-26 15:44:15 +00:00
|
|
|
if (response.statusCode == 400) {
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final error = responseJSON['message'] as String;
|
2020-01-04 19:31:52 +00:00
|
|
|
|
2022-01-26 15:44:15 +00:00
|
|
|
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}');
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final fromCurrency = responseJSON['fromCurrency'] as String;
|
|
|
|
final from = CryptoCurrency.fromString(fromCurrency);
|
|
|
|
final toCurrency = responseJSON['toCurrency'] as String;
|
|
|
|
final to = CryptoCurrency.fromString(toCurrency);
|
|
|
|
final inputAddress = responseJSON['payinAddress'] as String;
|
2022-01-26 15:44:15 +00:00
|
|
|
final expectedSendAmount = responseJSON['expectedAmountFrom'].toString();
|
2020-01-08 12:26:34 +00:00
|
|
|
final status = responseJSON['status'] as String;
|
|
|
|
final state = TradeState.deserialize(raw: status);
|
|
|
|
final extraId = responseJSON['payinExtraId'] as String;
|
|
|
|
final outputTransaction = responseJSON['payoutHash'] as String;
|
2021-02-22 16:04:37 +00:00
|
|
|
final expiredAtRaw = responseJSON['validUntil'] as String;
|
2023-01-16 15:27:49 +00:00
|
|
|
final payoutAddress = responseJSON['payoutAddress'] as String;
|
2022-12-06 17:23:46 +00:00
|
|
|
final expiredAt = DateTime.tryParse(expiredAtRaw)?.toLocal();
|
2021-02-22 16:04:37 +00:00
|
|
|
|
2021-03-01 18:16:28 +00:00
|
|
|
return Trade(
|
|
|
|
id: id,
|
|
|
|
from: from,
|
|
|
|
to: to,
|
|
|
|
provider: description,
|
|
|
|
inputAddress: inputAddress,
|
|
|
|
amount: expectedSendAmount,
|
|
|
|
state: state,
|
|
|
|
extraId: extraId,
|
|
|
|
expiredAt: expiredAt,
|
2023-01-16 15:27:49 +00:00
|
|
|
outputTransaction: outputTransaction,
|
|
|
|
payoutAddress: payoutAddress);
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 12:26:34 +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-01-26 15:44:15 +00:00
|
|
|
try {
|
|
|
|
if (amount == 0) {
|
|
|
|
return 0.0;
|
2021-02-19 08:37:30 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 15:44:15 +00:00
|
|
|
final headers = {apiHeaderKey: apiKey};
|
|
|
|
final isReverse = isReceiveAmount;
|
|
|
|
final type = isReverse ? 'reverse' : 'direct';
|
|
|
|
final flow = getFlow(isFixedRateMode);
|
|
|
|
final params = <String, String>{
|
2022-12-07 19:09:15 +00:00
|
|
|
'fromCurrency': normalizeCryptoCurrency(from),
|
|
|
|
'toCurrency': normalizeCryptoCurrency(to),
|
|
|
|
'fromNetwork': networkFor(from),
|
|
|
|
'toNetwork': networkFor(to),
|
2022-01-26 15:44:15 +00:00
|
|
|
'type': type,
|
|
|
|
'flow': flow};
|
|
|
|
|
|
|
|
if (isReverse) {
|
|
|
|
params['toAmount'] = amount.toString();
|
|
|
|
} else {
|
|
|
|
params['fromAmount'] = amount.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
final uri = Uri.https(apiAuthority, estimatedAmountPath, params);
|
|
|
|
final response = await get(uri, headers: headers);
|
2021-02-19 08:37:30 +00:00
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
2022-01-26 15:44:15 +00:00
|
|
|
final fromAmount = double.parse(responseJSON['fromAmount'].toString());
|
|
|
|
final toAmount = double.parse(responseJSON['toAmount'].toString());
|
2022-11-08 20:57:33 +00:00
|
|
|
final rateId = responseJSON['rateId'] as String? ?? '';
|
2020-01-04 19:31:52 +00:00
|
|
|
|
2022-01-26 15:44:15 +00:00
|
|
|
if (rateId.isNotEmpty) {
|
|
|
|
_lastUsedRateId = rateId;
|
|
|
|
}
|
2021-03-01 18:16:28 +00:00
|
|
|
|
2022-12-07 19:09:15 +00:00
|
|
|
return isReverse ? (amount / fromAmount) : (toAmount / amount);
|
2022-01-26 15:44:15 +00:00
|
|
|
} catch(e) {
|
|
|
|
print(e.toString());
|
|
|
|
return 0.0;
|
|
|
|
}
|
2021-04-30 09:46:15 +00:00
|
|
|
}
|
2022-02-08 08:57:02 +00:00
|
|
|
|
|
|
|
String networkFor(CryptoCurrency currency) {
|
|
|
|
switch (currency) {
|
|
|
|
case CryptoCurrency.usdt:
|
|
|
|
return CryptoCurrency.btc.title.toLowerCase();
|
|
|
|
default:
|
2022-07-11 15:20:16 +00:00
|
|
|
return currency.tag != null
|
2022-10-12 17:09:57 +00:00
|
|
|
? currency.tag!.toLowerCase()
|
2022-07-11 15:20:16 +00:00
|
|
|
: currency.title.toLowerCase();
|
|
|
|
}
|
2022-02-08 08:57:02 +00:00
|
|
|
}
|
2023-02-06 19:20:43 +00:00
|
|
|
|
2022-02-08 08:57:02 +00:00
|
|
|
}
|
2021-04-30 09:46:15 +00:00
|
|
|
|
2022-07-11 15:20:16 +00:00
|
|
|
String normalizeCryptoCurrency(CryptoCurrency currency) {
|
|
|
|
switch(currency) {
|
|
|
|
case CryptoCurrency.zec:
|
|
|
|
return 'zec';
|
2022-02-08 08:57:02 +00:00
|
|
|
default:
|
|
|
|
return currency.title.toLowerCase();
|
|
|
|
}
|
|
|
|
|
2021-03-01 18:16:28 +00:00
|
|
|
}
|