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:flutter/foundation.dart';
|
|
|
|
import 'package:http/http.dart';
|
|
|
|
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/entities/crypto_currency.dart';
|
|
|
|
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';
|
|
|
|
import 'package:cake_wallet/exchange/trade_not_created_exeption.dart';
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
class ChangeNowExchangeProvider extends ExchangeProvider {
|
2020-01-08 12:26:34 +00:00
|
|
|
ChangeNowExchangeProvider()
|
|
|
|
: super(
|
|
|
|
pairList: CryptoCurrency.all
|
2020-11-10 14:58:40 +00:00
|
|
|
.map((i) => CryptoCurrency.all
|
|
|
|
.map((k) => ExchangePair(from: i, to: k, reverse: true))
|
|
|
|
.where((c) => c != null))
|
2020-01-08 12:26:34 +00:00
|
|
|
.expand((i) => i)
|
|
|
|
.toList());
|
|
|
|
|
2020-01-04 19:31:52 +00:00
|
|
|
static const apiUri = 'https://changenow.io/api/v1';
|
|
|
|
static const apiKey = secrets.change_now_api_key;
|
|
|
|
static const _exchangeAmountUriSufix = '/exchange-amount/';
|
|
|
|
static const _transactionsUriSufix = '/transactions/';
|
|
|
|
static const _minAmountUriSufix = '/min-amount/';
|
2021-02-18 19:08:52 +00:00
|
|
|
static const _marketInfoUriSufix = '/market-info/';
|
|
|
|
static const _fixedRateUriSufix = 'fixed-rate/';
|
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;
|
|
|
|
|
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;
|
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
@override
|
2021-02-18 19:08:52 +00:00
|
|
|
Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to,
|
|
|
|
bool isFixedRateMode}) async {
|
2020-01-04 19:31:52 +00:00
|
|
|
final symbol = from.toString() + '_' + to.toString();
|
2021-02-18 19:08:52 +00:00
|
|
|
final url = isFixedRateMode
|
|
|
|
? apiUri + _marketInfoUriSufix + _fixedRateUriSufix + apiKey
|
|
|
|
: apiUri + _minAmountUriSufix + symbol;
|
2020-01-04 19:31:52 +00:00
|
|
|
final response = await get(url);
|
|
|
|
|
2021-02-18 19:08:52 +00:00
|
|
|
if (isFixedRateMode) {
|
|
|
|
final responseJSON = json.decode(response.body) as List<dynamic>;
|
|
|
|
|
|
|
|
for (var elem in responseJSON) {
|
|
|
|
final elemFrom = elem["from"] as String;
|
|
|
|
final elemTo = elem["to"] as String;
|
|
|
|
|
|
|
|
if ((elemFrom == from.toString().toLowerCase()) &&
|
|
|
|
(elemTo == to.toString().toLowerCase())) {
|
|
|
|
final min = elem["min"] as double;
|
|
|
|
final max = elem["max"] as double;
|
|
|
|
|
|
|
|
return Limits(min: min, max: max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Limits(min: 0, max: 0);
|
|
|
|
} else {
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final min = responseJSON['minAmount'] as double;
|
|
|
|
|
|
|
|
return Limits(min: min, max: null);
|
|
|
|
}
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
@override
|
2021-02-18 19:08:52 +00:00
|
|
|
Future<Trade> createTrade({TradeRequest request, bool isFixedRateMode}) async {
|
|
|
|
final url = isFixedRateMode
|
|
|
|
? apiUri + _transactionsUriSufix + _fixedRateUriSufix + apiKey
|
|
|
|
: apiUri + _transactionsUriSufix + apiKey;
|
2020-01-04 19:31:52 +00:00
|
|
|
final _request = request as ChangeNowRequest;
|
|
|
|
final body = {
|
|
|
|
'from': _request.from.toString(),
|
|
|
|
'to': _request.to.toString(),
|
|
|
|
'address': _request.address,
|
|
|
|
'amount': _request.amount,
|
|
|
|
'refundAddress': _request.refundAddress
|
|
|
|
};
|
|
|
|
|
|
|
|
final response = await post(url,
|
|
|
|
headers: {'Content-Type': 'application/json'}, body: json.encode(body));
|
|
|
|
|
|
|
|
if (response.statusCode != 200) {
|
|
|
|
if (response.statusCode == 400) {
|
2020-01-08 12:26:34 +00:00
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final error = responseJSON['message'] as String;
|
|
|
|
|
2020-01-04 19:31:52 +00:00
|
|
|
throw TradeNotCreatedException(description, description: error);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw TradeNotCreatedException(description);
|
|
|
|
}
|
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final id = responseJSON['id'] as String;
|
|
|
|
final inputAddress = responseJSON['payinAddress'] as String;
|
|
|
|
final refundAddress = responseJSON['refundAddress'] as String;
|
|
|
|
final extraId = responseJSON['payinExtraId'] 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(),
|
|
|
|
amount: _request.amount,
|
|
|
|
state: TradeState.created);
|
|
|
|
}
|
|
|
|
|
2020-01-08 12:26:34 +00:00
|
|
|
@override
|
2020-01-04 19:31:52 +00:00
|
|
|
Future<Trade> findTradeById({@required String id}) async {
|
|
|
|
final url = apiUri + _transactionsUriSufix + id + '/' + apiKey;
|
|
|
|
final response = await get(url);
|
|
|
|
|
|
|
|
if (response.statusCode != 200) {
|
|
|
|
if (response.statusCode == 400) {
|
2020-01-08 12:26:34 +00:00
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final error = responseJSON['message'] as String;
|
|
|
|
|
2020-01-04 19:31:52 +00:00
|
|
|
throw TradeNotFoundException(id,
|
|
|
|
provider: description, description: error);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw TradeNotFoundException(id, provider: description);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
final expectedSendAmount = responseJSON['expectedSendAmount'].toString();
|
|
|
|
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;
|
|
|
|
final expiredAt = expiredAtRaw != null
|
|
|
|
? DateTime.parse(expiredAtRaw).toLocal()
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if (expiredAt != null) {
|
|
|
|
return Trade(
|
|
|
|
id: id,
|
|
|
|
from: from,
|
|
|
|
to: to,
|
|
|
|
provider: description,
|
|
|
|
inputAddress: inputAddress,
|
|
|
|
amount: expectedSendAmount,
|
|
|
|
state: state,
|
|
|
|
extraId: extraId,
|
|
|
|
expiredAt: expiredAt,
|
|
|
|
outputTransaction: outputTransaction);
|
|
|
|
} else {
|
|
|
|
return Trade(
|
|
|
|
id: id,
|
|
|
|
from: from,
|
|
|
|
to: to,
|
|
|
|
provider: description,
|
|
|
|
inputAddress: inputAddress,
|
|
|
|
amount: expectedSendAmount,
|
|
|
|
state: state,
|
|
|
|
extraId: extraId,
|
|
|
|
outputTransaction: outputTransaction);
|
|
|
|
}
|
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
|
|
|
Future<double> calculateAmount(
|
2020-11-10 14:58:40 +00:00
|
|
|
{CryptoCurrency from,
|
|
|
|
CryptoCurrency to,
|
|
|
|
double amount,
|
2021-02-18 19:08:52 +00:00
|
|
|
bool isFixedRateMode,
|
2020-11-10 14:58:40 +00:00
|
|
|
bool isReceiveAmount}) async {
|
2021-02-19 08:37:30 +00:00
|
|
|
if (isReceiveAmount && isFixedRateMode) {
|
|
|
|
final url = apiUri + _marketInfoUriSufix + _fixedRateUriSufix + apiKey;
|
|
|
|
final response = await get(url);
|
|
|
|
final responseJSON = json.decode(response.body) as List<dynamic>;
|
|
|
|
var rate = 0.0;
|
|
|
|
var fee = 0.0;
|
|
|
|
|
|
|
|
for (var elem in responseJSON) {
|
|
|
|
final elemFrom = elem["from"] as String;
|
|
|
|
final elemTo = elem["to"] as String;
|
|
|
|
|
|
|
|
if ((elemFrom == to.toString().toLowerCase()) &&
|
|
|
|
(elemTo == from.toString().toLowerCase())) {
|
|
|
|
rate = elem["rate"] as double;
|
|
|
|
fee = elem["minerFee"] as double;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final estimatedAmount = (amount == 0.0)||(rate == 0.0) ? 0.0
|
|
|
|
: (amount + fee)/rate;
|
|
|
|
|
|
|
|
return estimatedAmount;
|
|
|
|
} else {
|
|
|
|
final url = isFixedRateMode
|
|
|
|
? apiUri +
|
2021-02-18 19:08:52 +00:00
|
|
|
_exchangeAmountUriSufix +
|
|
|
|
_fixedRateUriSufix +
|
|
|
|
amount.toString() +
|
|
|
|
'/' +
|
|
|
|
from.toString() +
|
|
|
|
'_' +
|
|
|
|
to.toString() +
|
|
|
|
'?api_key=' + apiKey
|
2021-02-19 08:37:30 +00:00
|
|
|
: apiUri +
|
2021-02-18 19:08:52 +00:00
|
|
|
_exchangeAmountUriSufix +
|
|
|
|
amount.toString() +
|
|
|
|
'/' +
|
|
|
|
from.toString() +
|
|
|
|
'_' +
|
|
|
|
to.toString();
|
2021-02-19 08:37:30 +00:00
|
|
|
final response = await get(url);
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final estimatedAmount = responseJSON['estimatedAmount'] as double;
|
2020-01-04 19:31:52 +00:00
|
|
|
|
2021-02-19 08:37:30 +00:00
|
|
|
return estimatedAmount;
|
|
|
|
}
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|
|
|
|
}
|