Fixed rate for changenow.

This commit is contained in:
M 2022-01-26 17:44:15 +02:00
parent 860e904816
commit 02de3104eb
5 changed files with 195 additions and 140 deletions

View file

@ -16,7 +16,8 @@ import 'package:cake_wallet/exchange/trade_not_created_exeption.dart';
class ChangeNowExchangeProvider extends ExchangeProvider { class ChangeNowExchangeProvider extends ExchangeProvider {
ChangeNowExchangeProvider() ChangeNowExchangeProvider()
: super( : _lastUsedRateId = '',
super(
pairList: CryptoCurrency.all pairList: CryptoCurrency.all
.map((i) => CryptoCurrency.all .map((i) => CryptoCurrency.all
.map((k) => ExchangePair(from: i, to: k, reverse: true)) .map((k) => ExchangePair(from: i, to: k, reverse: true))
@ -24,13 +25,13 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
.expand((i) => i) .expand((i) => i)
.toList()); .toList());
static const apiUri = 'https://changenow.io/api/v1';
static const apiKey = secrets.changeNowApiKey; static const apiKey = secrets.changeNowApiKey;
static const _exchangeAmountUriSufix = '/exchange-amount/'; static const apiAuthority = 'api.changenow.io';
static const _transactionsUriSufix = '/transactions/'; static const createTradePath = '/v2/exchange';
static const _minAmountUriSufix = '/min-amount/'; static const findTradeByIdPath = '/v2/exchange/by-id';
static const _marketInfoUriSufix = '/market-info/'; static const estimatedAmountPath = '/v2/exchange/estimated-amount';
static const _fixedRateUriSufix = 'fixed-rate/'; static const rangePath = '/v2/exchange/range';
static const apiHeaderKey = 'x-changenow-api-key';
@override @override
String get title => 'ChangeNOW'; String get title => 'ChangeNOW';
@ -45,68 +46,74 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
@override @override
Future<bool> checkIsAvailable() async => true; Future<bool> checkIsAvailable() async => true;
String _lastUsedRateId;
static String getFlow(bool isFixedRate) => isFixedRate ? 'fixed-rate' : 'standard';
@override @override
Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to, Future<Limits> fetchLimits({CryptoCurrency from, CryptoCurrency to,
bool isFixedRateMode}) async { bool isFixedRateMode}) async {
final fromTitle = defineCurrencyTitle(from); final headers = {apiHeaderKey: apiKey};
final toTitle = defineCurrencyTitle(to); final normalizedFrom = normalizeCryptoCurrency(from);
final symbol = fromTitle + '_' + toTitle; final normalizedTo = normalizeCryptoCurrency(to);
final url = isFixedRateMode final flow = getFlow(isFixedRateMode);
? apiUri + _marketInfoUriSufix + _fixedRateUriSufix + apiKey final params = <String, String>{
: apiUri + _minAmountUriSufix + symbol; 'fromCurrency': normalizedFrom,
final response = await get(url); 'toCurrency': normalizedTo,
'flow': flow};
final uri = Uri.https(apiAuthority, rangePath, params);
final response = await get(uri, headers: headers);
if (isFixedRateMode) { if (response.statusCode == 400) {
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 == fromTitle) && (elemTo == toTitle)) {
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 responseJSON = json.decode(response.body) as Map<String, dynamic>;
final min = responseJSON['minAmount'] as double; final error = responseJSON['error'] as String;
final message = responseJSON['message'] as String;
return Limits(min: min, max: null); throw Exception('${error}\n$message');
} }
if (response.statusCode != 200) {
return null;
}
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
return Limits(
min: responseJSON['minAmount'] as double,
max: responseJSON['maxAmount'] as double);
} }
@override @override
Future<Trade> createTrade({TradeRequest request, bool isFixedRateMode}) async { Future<Trade> createTrade({TradeRequest request, bool isFixedRateMode}) async {
final url = isFixedRateMode
? apiUri + _transactionsUriSufix + _fixedRateUriSufix + apiKey
: apiUri + _transactionsUriSufix + apiKey;
final _request = request as ChangeNowRequest; final _request = request as ChangeNowRequest;
final fromTitle = defineCurrencyTitle(_request.from); final headers = {
final toTitle = defineCurrencyTitle(_request.to); apiHeaderKey: apiKey,
final body = { 'Content-Type': 'application/json'};
'from': fromTitle, final flow = getFlow(isFixedRateMode);
'to': toTitle, final body = <String, String>{
'fromCurrency': normalizeCryptoCurrency(_request.from),
'toCurrency': normalizeCryptoCurrency(_request.to),
'fromAmount': _request.fromAmount,
'toAmount': _request.toAmount,
'address': _request.address, 'address': _request.address,
'amount': _request.amount, 'flow': flow,
'refundAddress': _request.refundAddress 'refundAddress': _request.refundAddress
}; };
final response = await post(url, if (isFixedRateMode) {
headers: {'Content-Type': 'application/json'}, body: json.encode(body)); body['rateId'] = _lastUsedRateId;
if (response.statusCode != 200) {
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 uri = Uri.https(apiAuthority, createTradePath);
final response = await post(uri, headers: headers, body: json.encode(body));
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');
}
if (response.statusCode != 200) {
return null;
} }
final responseJSON = json.decode(response.body) as Map<String, dynamic>; final responseJSON = json.decode(response.body) as Map<String, dynamic>;
@ -124,16 +131,21 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
refundAddress: refundAddress, refundAddress: refundAddress,
extraId: extraId, extraId: extraId,
createdAt: DateTime.now(), createdAt: DateTime.now(),
amount: _request.amount, amount: _request.fromAmount,
state: TradeState.created); state: TradeState.created);
} }
@override @override
Future<Trade> findTradeById({@required String id}) async { Future<Trade> findTradeById({@required String id}) async {
final url = apiUri + _transactionsUriSufix + id + '/' + apiKey; final headers = {apiHeaderKey: apiKey};
final response = await get(url); final params = <String, String>{'id': id};
final uri = Uri.https(apiAuthority,findTradeByIdPath, params);
final response = await get(uri, headers: headers);
if (response.statusCode == 404) {
throw TradeNotFoundException(id, provider: description);
}
if (response.statusCode != 200) {
if (response.statusCode == 400) { if (response.statusCode == 400) {
final responseJSON = json.decode(response.body) as Map<String, dynamic>; final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final error = responseJSON['message'] as String; final error = responseJSON['message'] as String;
@ -142,7 +154,8 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
provider: description, description: error); provider: description, description: error);
} }
throw TradeNotFoundException(id, provider: description); if (response.statusCode != 200) {
return null;
} }
final responseJSON = json.decode(response.body) as Map<String, dynamic>; final responseJSON = json.decode(response.body) as Map<String, dynamic>;
@ -151,7 +164,7 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
final toCurrency = responseJSON['toCurrency'] as String; final toCurrency = responseJSON['toCurrency'] as String;
final to = CryptoCurrency.fromString(toCurrency); final to = CryptoCurrency.fromString(toCurrency);
final inputAddress = responseJSON['payinAddress'] as String; final inputAddress = responseJSON['payinAddress'] as String;
final expectedSendAmount = responseJSON['expectedSendAmount'].toString(); final expectedSendAmount = responseJSON['expectedAmountFrom'].toString();
final status = responseJSON['status'] as String; final status = responseJSON['status'] as String;
final state = TradeState.deserialize(raw: status); final state = TradeState.deserialize(raw: status);
final extraId = responseJSON['payinExtraId'] as String; final extraId = responseJSON['payinExtraId'] as String;
@ -181,68 +194,46 @@ class ChangeNowExchangeProvider extends ExchangeProvider {
double amount, double amount,
bool isFixedRateMode, bool isFixedRateMode,
bool isReceiveAmount}) async { bool isReceiveAmount}) async {
if (isReceiveAmount && isFixedRateMode) { try {
final url = apiUri + _marketInfoUriSufix + _fixedRateUriSufix + apiKey; if (amount == 0) {
final response = await get(url); return 0.0;
final responseJSON = json.decode(response.body) as List<dynamic>;
final fromTitle = defineCurrencyTitle(from);
final toTitle = defineCurrencyTitle(to);
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 == toTitle) && (elemTo == fromTitle)) {
rate = elem["rate"] as double;
fee = elem["minerFee"] as double;
break;
}
} }
final estimatedAmount = (amount == 0.0)||(rate == 0.0) ? 0.0 final headers = {apiHeaderKey: apiKey};
: (amount + fee)/rate; final isReverse = isReceiveAmount;
final type = isReverse ? 'reverse' : 'direct';
final flow = getFlow(isFixedRateMode);
final params = <String, String>{
'fromCurrency': isReverse ? normalizeCryptoCurrency(to) : normalizeCryptoCurrency(from),
'toCurrency': isReverse ? normalizeCryptoCurrency(from) : normalizeCryptoCurrency(to) ,
'type': type,
'flow': flow};
return estimatedAmount; if (isReverse) {
params['toAmount'] = amount.toString();
} else { } else {
final url = defineUrlForCalculatingAmount(from, to, amount, isFixedRateMode); params['fromAmount'] = amount.toString();
final response = await get(url); }
final uri = Uri.https(apiAuthority, estimatedAmountPath, params);
final response = await get(uri, headers: headers);
final responseJSON = json.decode(response.body) as Map<String, dynamic>; final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final estimatedAmount = responseJSON['estimatedAmount'] as double; final fromAmount = double.parse(responseJSON['fromAmount'].toString());
final toAmount = double.parse(responseJSON['toAmount'].toString());
final rateId = responseJSON['rateId'] as String ?? '';
return estimatedAmount; if (rateId.isNotEmpty) {
_lastUsedRateId = rateId;
}
return isReverse ? fromAmount : toAmount;
} catch(e) {
print(e.toString());
return 0.0;
} }
} }
static String defineUrlForCalculatingAmount( static String normalizeCryptoCurrency(CryptoCurrency currency) {
CryptoCurrency from,
CryptoCurrency to,
double amount,
bool isFixedRateMode) {
final fromTitle = defineCurrencyTitle(from);
final toTitle = defineCurrencyTitle(to);
return isFixedRateMode
? apiUri +
_exchangeAmountUriSufix +
_fixedRateUriSufix +
amount.toString() +
'/' +
fromTitle +
'_' +
toTitle +
'?api_key=' + apiKey
: apiUri +
_exchangeAmountUriSufix +
amount.toString() +
'/' +
fromTitle +
'_' +
toTitle;
}
static String defineCurrencyTitle(CryptoCurrency currency) {
const bnbTitle = 'bnbmainnet'; const bnbTitle = 'bnbmainnet';
final currencyTitle = currency == CryptoCurrency.bnb final currencyTitle = currency == CryptoCurrency.bnb
? bnbTitle : currency.title.toLowerCase(); ? bnbTitle : currency.title.toLowerCase();

View file

@ -7,12 +7,16 @@ class ChangeNowRequest extends TradeRequest {
{@required this.from, {@required this.from,
@required this.to, @required this.to,
@required this.address, @required this.address,
@required this.amount, @required this.fromAmount,
@required this.refundAddress}); @required this.toAmount,
@required this.refundAddress,
@required this.isReverse});
CryptoCurrency from; CryptoCurrency from;
CryptoCurrency to; CryptoCurrency to;
String address; String address;
String amount; String fromAmount;
String toAmount;
String refundAddress; String refundAddress;
bool isReverse;
} }

View file

@ -1,5 +1,6 @@
import 'dart:ui'; import 'dart:ui';
import 'package:cake_wallet/entities/parsed_address.dart'; import 'package:cake_wallet/entities/parsed_address.dart';
import 'package:cake_wallet/utils/debounce.dart';
import 'package:cw_core/sync_status.dart'; import 'package:cw_core/sync_status.dart';
import 'package:cw_core/wallet_type.dart'; import 'package:cw_core/wallet_type.dart';
import 'package:cake_wallet/entities/parse_address_from_domain.dart'; import 'package:cake_wallet/entities/parse_address_from_domain.dart';
@ -44,6 +45,8 @@ class ExchangePage extends BasePage {
final _depositAddressFocus = FocusNode(); final _depositAddressFocus = FocusNode();
final _receiveAmountFocus = FocusNode(); final _receiveAmountFocus = FocusNode();
final _receiveAddressFocus = FocusNode(); final _receiveAddressFocus = FocusNode();
final _receiveAmountDebounce = Debounce(Duration(milliseconds: 500));
final _depositAmountDebounce = Debounce(Duration(milliseconds: 500));
var _isReactionsSet = false; var _isReactionsSet = false;
@override @override
@ -99,6 +102,7 @@ class ExchangePage extends BasePage {
.addPostFrameCallback((_) => _setReactions(context, exchangeViewModel)); .addPostFrameCallback((_) => _setReactions(context, exchangeViewModel));
return KeyboardActions( return KeyboardActions(
disableScroll: true,
config: KeyboardActionsConfig( config: KeyboardActionsConfig(
keyboardActionsPlatform: KeyboardActionsPlatform.IOS, keyboardActionsPlatform: KeyboardActionsPlatform.IOS,
keyboardBarColor: keyboardBarColor:
@ -113,7 +117,6 @@ class ExchangePage extends BasePage {
toolbarButtons: [(_) => KeyboardDoneButton()]) toolbarButtons: [(_) => KeyboardDoneButton()])
]), ]),
child: Container( child: Container(
height: 1,
color: Theme.of(context).backgroundColor, color: Theme.of(context).backgroundColor,
child: Form( child: Form(
key: _formKey, key: _formKey,
@ -314,6 +317,21 @@ class ExchangePage extends BasePage {
], ],
), ),
), ),
Padding(
padding: EdgeInsets.only(top: 12, left: 24),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
StandardCheckbox(
key: checkBoxKey,
value: exchangeViewModel.isFixedRateMode,
caption: S.of(context).fixed_rate,
onChanged: (value) =>
exchangeViewModel.isFixedRateMode = value,
),
],
)
),
Padding( Padding(
padding: EdgeInsets.only(top: 30, left: 24, bottom: 24), padding: EdgeInsets.only(top: 30, left: 24, bottom: 24),
child: Row( child: Row(
@ -548,7 +566,9 @@ class ExchangePage extends BasePage {
final max = limitsState.limits.max != null final max = limitsState.limits.max != null
? limitsState.limits.max.toString() ? limitsState.limits.max.toString()
: null; : null;
final key = depositKey; final key = exchangeViewModel.isFixedRateMode
? receiveKey
: depositKey;
key.currentState.changeLimits(min: min, max: max); key.currentState.changeLimits(min: min, max: max);
} }
@ -656,8 +676,13 @@ class ExchangePage extends BasePage {
max = '...'; max = '...';
} }
if (exchangeViewModel.isFixedRateMode) {
depositKey.currentState.changeLimits(min: null, max: null);
receiveKey.currentState.changeLimits(min: min, max: max);
} else {
depositKey.currentState.changeLimits(min: min, max: max); depositKey.currentState.changeLimits(min: min, max: max);
receiveKey.currentState.changeLimits(min: null, max: null); receiveKey.currentState.changeLimits(min: null, max: null);
}
}); });
depositAddressController.addListener( depositAddressController.addListener(
@ -665,9 +690,11 @@ class ExchangePage extends BasePage {
depositAmountController.addListener(() { depositAmountController.addListener(() {
if (depositAmountController.text != exchangeViewModel.depositAmount) { if (depositAmountController.text != exchangeViewModel.depositAmount) {
_depositAmountDebounce.run(() {
exchangeViewModel.changeDepositAmount( exchangeViewModel.changeDepositAmount(
amount: depositAmountController.text); amount: depositAmountController.text);
exchangeViewModel.isReceiveAmountEntered = false; exchangeViewModel.isReceiveAmountEntered = false;
});
} }
}); });
@ -676,9 +703,11 @@ class ExchangePage extends BasePage {
receiveAmountController.addListener(() { receiveAmountController.addListener(() {
if (receiveAmountController.text != exchangeViewModel.receiveAmount) { if (receiveAmountController.text != exchangeViewModel.receiveAmount) {
_receiveAmountDebounce.run(() {
exchangeViewModel.changeReceiveAmount( exchangeViewModel.changeReceiveAmount(
amount: receiveAmountController.text); amount: receiveAmountController.text);
exchangeViewModel.isReceiveAmountEntered = true; exchangeViewModel.isReceiveAmountEntered = true;
});
} }
}); });

14
lib/utils/debounce.dart Normal file
View file

@ -0,0 +1,14 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
class Debounce {
Debounce(this.duration);
final Duration duration;
Timer _timer;
void run(VoidCallback action) {
_timer?.cancel();
_timer = Timer(duration, action);
}
}

View file

@ -57,10 +57,14 @@ abstract class ExchangeViewModelBase with Store {
receiveCurrencies = CryptoCurrency.all receiveCurrencies = CryptoCurrency.all
.where((cryptoCurrency) => !excludeCurrencies.contains(cryptoCurrency)) .where((cryptoCurrency) => !excludeCurrencies.contains(cryptoCurrency))
.toList(); .toList();
_defineIsReceiveAmountEditable(); isReverse = false;
isFixedRateMode = false; isFixedRateMode = false;
isReceiveAmountEntered = false; isReceiveAmountEntered = false;
_defineIsReceiveAmountEditable();
loadLimits(); loadLimits();
reaction(
(_) => isFixedRateMode,
(Object _) => _defineIsReceiveAmountEditable());
} }
final WalletBase wallet; final WalletBase wallet;
@ -129,6 +133,8 @@ abstract class ExchangeViewModelBase with Store {
Limits limits; Limits limits;
bool isReverse;
NumberFormat _cryptoNumberFormat; NumberFormat _cryptoNumberFormat;
SettingsStore _settingsStore; SettingsStore _settingsStore;
@ -164,6 +170,7 @@ abstract class ExchangeViewModelBase with Store {
@action @action
void changeReceiveAmount({String amount}) { void changeReceiveAmount({String amount}) {
receiveAmount = amount; receiveAmount = amount;
isReverse = true;
if (amount == null || amount.isEmpty) { if (amount == null || amount.isEmpty) {
depositAmount = ''; depositAmount = '';
@ -190,6 +197,7 @@ abstract class ExchangeViewModelBase with Store {
@action @action
void changeDepositAmount({String amount}) { void changeDepositAmount({String amount}) {
depositAmount = amount; depositAmount = amount;
isReverse = false;
if (amount == null || amount.isEmpty) { if (amount == null || amount.isEmpty) {
depositAmount = ''; depositAmount = '';
@ -217,9 +225,15 @@ abstract class ExchangeViewModelBase with Store {
limitsState = LimitsIsLoading(); limitsState = LimitsIsLoading();
try { try {
final from = isFixedRateMode
? receiveCurrency
: depositCurrency;
final to = isFixedRateMode
? depositCurrency
: receiveCurrency;
limits = await provider.fetchLimits( limits = await provider.fetchLimits(
from: depositCurrency, from: from,
to: receiveCurrency, to: to,
isFixedRateMode: isFixedRateMode); isFixedRateMode: isFixedRateMode);
limitsState = LimitsLoadedSuccessfully(limits: limits); limitsState = LimitsLoadedSuccessfully(limits: limits);
} catch (e) { } catch (e) {
@ -250,10 +264,12 @@ abstract class ExchangeViewModelBase with Store {
request = ChangeNowRequest( request = ChangeNowRequest(
from: depositCurrency, from: depositCurrency,
to: receiveCurrency, to: receiveCurrency,
amount: depositAmount?.replaceAll(',', '.'), fromAmount: depositAmount?.replaceAll(',', '.'),
toAmount: receiveAmount?.replaceAll(',', '.'),
refundAddress: depositAddress, refundAddress: depositAddress,
address: receiveAddress); address: receiveAddress,
amount = depositAmount; isReverse: isReverse);
amount = isReverse ? receiveAmount : depositAmount;
currency = depositCurrency; currency = depositCurrency;
} }
@ -422,6 +438,7 @@ abstract class ExchangeViewModelBase with Store {
} else { } else {
isReceiveAmountEditable = false; isReceiveAmountEditable = false;
}*/ }*/
isReceiveAmountEditable = false; //isReceiveAmountEditable = false;
isReceiveAmountEditable = (isFixedRateMode ?? false) && provider is ChangeNowExchangeProvider;
} }
} }