add basic simplex api and data loading services

need to update the data loading service to call /quote and provide lists of supported coins and fiats
This commit is contained in:
sneurlax 2023-01-11 17:04:03 -06:00
parent 9d6ec18057
commit 7c58a3a47e
2 changed files with 699 additions and 0 deletions

View file

@ -0,0 +1,201 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:stackwallet/providers/providers.dart';
import 'package:stackwallet/services/buy/simplex/simplex_api.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/logger.dart';
class BuyDataLoadingService {
Future<void> loadAll(WidgetRef ref, {Coin? coin}) async {
try {
await Future.wait([
_loadFixedRateMarkets(ref, coin: coin),
_loadChangeNowStandardCurrencies(ref, coin: coin),
loadSimpleswapFixedRateCurrencies(ref),
loadSimpleswapFloatingRateCurrencies(ref),
]);
} catch (e, s) {
Logging.instance.log("BuyDataLoadingService.loadAll failed: $e\n$s",
level: LogLevel.Error);
}
}
Future<void> _loadFixedRateMarkets(WidgetRef ref, {Coin? coin}) async {
if (ref.read(changeNowFixedInitialLoadStatusStateProvider.state).state ==
ChangeNowLoadStatus.loading) {
// already in progress so just
return;
}
ref.read(changeNowFixedInitialLoadStatusStateProvider.state).state =
ChangeNowLoadStatus.loading;
final response3 =
await ChangeNowAPI.instance.getAvailableFixedRateMarkets();
if (response3.value != null) {
ref
.read(availableChangeNowCurrenciesProvider)
.updateMarkets(response3.value!);
if (ref.read(buyFormStateProvider).market == null) {
String fromTicker = "btc";
String toTicker = "xmr";
if (coin != null) {
fromTicker = coin.ticker.toLowerCase();
}
final matchingMarkets = response3.value!
.where((e) => e.to == toTicker && e.from == fromTicker);
if (matchingMarkets.isNotEmpty) {
await ref
.read(buyFormStateProvider)
.updateMarket(matchingMarkets.first, true);
}
}
} else {
Logging.instance.log(
"Failed to load changeNOW fixed rate markets: ${response3.exception?.errorMessage}",
level: LogLevel.Error);
ref.read(changeNowFixedInitialLoadStatusStateProvider.state).state =
ChangeNowLoadStatus.failed;
return;
}
ref.read(changeNowFixedInitialLoadStatusStateProvider.state).state =
ChangeNowLoadStatus.success;
}
Future<void> _loadChangeNowStandardCurrencies(
WidgetRef ref, {
Coin? coin,
}) async {
if (ref
.read(changeNowEstimatedInitialLoadStatusStateProvider.state)
.state ==
ChangeNowLoadStatus.loading) {
// already in progress so just
return;
}
ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state =
ChangeNowLoadStatus.loading;
final response = await ChangeNowAPI.instance.getAvailableCurrencies();
final response2 =
await ChangeNowAPI.instance.getAvailableFloatingRatePairs();
if (response.value != null) {
ref
.read(availableChangeNowCurrenciesProvider)
.updateCurrencies(response.value!);
if (response2.value != null) {
ref
.read(availableChangeNowCurrenciesProvider)
.updateFloatingPairs(response2.value!);
String fromTicker = "btc";
String toTicker = "xmr";
if (coin != null) {
fromTicker = coin.ticker.toLowerCase();
}
if (response.value!.length > 1) {
if (ref.read(buyFormStateProvider).from == null) {
if (response.value!
.where((e) => e.ticker == fromTicker)
.isNotEmpty) {
await ref.read(buyFormStateProvider).updateFrom(
response.value!.firstWhere((e) => e.ticker == fromTicker),
false);
}
}
if (ref.read(buyFormStateProvider).to == null) {
if (response.value!.where((e) => e.ticker == toTicker).isNotEmpty) {
await ref.read(buyFormStateProvider).updateTo(
response.value!.firstWhere((e) => e.ticker == toTicker),
false);
}
}
}
} else {
Logging.instance.log(
"Failed to load changeNOW available floating rate pairs: ${response2.exception?.errorMessage}",
level: LogLevel.Error);
ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state =
ChangeNowLoadStatus.failed;
return;
}
} else {
Logging.instance.log(
"Failed to load changeNOW currencies: ${response.exception?.errorMessage}",
level: LogLevel.Error);
await Future<void>.delayed(const Duration(seconds: 3));
ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state =
ChangeNowLoadStatus.failed;
return;
}
ref.read(changeNowEstimatedInitialLoadStatusStateProvider.state).state =
ChangeNowLoadStatus.success;
}
Future<void> loadSimpleswapFloatingRateCurrencies(WidgetRef ref) async {
final buy = SimpleSwapBuy();
final responseCurrencies = await buy.getAllCurrencies(false);
if (responseCurrencies.value != null) {
ref
.read(availableSimpleswapCurrenciesProvider)
.updateFloatingCurrencies(responseCurrencies.value!);
final responsePairs = await buy.getAllPairs(false);
if (responsePairs.value != null) {
ref
.read(availableSimpleswapCurrenciesProvider)
.updateFloatingPairs(responsePairs.value!);
} else {
Logging.instance.log(
"loadSimpleswapFloatingRateCurrencies: $responsePairs",
level: LogLevel.Warning,
);
}
} else {
Logging.instance.log(
"loadSimpleswapFloatingRateCurrencies: $responseCurrencies",
level: LogLevel.Warning,
);
}
}
Future<void> loadSimpleswapFixedRateCurrencies(WidgetRef ref) async {
final buy = SimpleSwapBuy();
final responseCurrencies = await buy.getAllCurrencies(true);
if (responseCurrencies.value != null) {
ref
.read(availableSimpleswapCurrenciesProvider)
.updateFixedCurrencies(responseCurrencies.value!);
final responsePairs = await buy.getAllPairs(true);
if (responsePairs.value != null) {
ref
.read(availableSimpleswapCurrenciesProvider)
.updateFixedPairs(responsePairs.value!);
} else {
Logging.instance.log(
"loadSimpleswapFixedRateCurrencies: $responsePairs",
level: LogLevel.Warning,
);
}
} else {
Logging.instance.log(
"loadSimpleswapFixedRateCurrencies: $responseCurrencies",
level: LogLevel.Warning,
);
}
}
}

View file

@ -0,0 +1,498 @@
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
// import 'package:stackwallet/models/exchange/response_objects/fixed_rate_market.dart';
// import 'package:stackwallet/models/exchange/response_objects/pair.dart';
// import 'package:stackwallet/models/exchange/response_objects/range.dart';
// import 'package:stackwallet/models/exchange/response_objects/trade.dart';
import 'package:stackwallet/models/buy/response_objects/fiat.dart';
// import 'package:stackwallet/models/buy/response_objects/crypto.dart';
import 'package:stackwallet/services/buy/buy_response.dart';
import 'package:stackwallet/utilities/logger.dart';
class SimplexAPI {
static const String scheme = "https";
static const String authority = "sandbox-api.stackwallet.com";
SimplexAPI._();
static final SimplexAPI _instance = SimplexAPI._();
static SimplexAPI get instance => _instance;
/// set this to override using standard http client. Useful for testing
http.Client? client;
Uri _buildUri(String path, Map<String, String>? params) {
return Uri.https(authority, path, params);
}
Future<dynamic> _makeGetRequest(Uri uri) async {
final client = this.client ?? http.Client();
int code = -1;
try {
final response = await client.get(
uri,
);
code = response.statusCode;
final parsed = jsonDecode(response.body);
return parsed;
} catch (e, s) {
Logging.instance.log(
"_makeRequest($uri) HTTP:$code threw: $e\n$s",
level: LogLevel.Error,
);
rethrow;
}
}
Future<dynamic> _makePostRequest(
Uri uri,
Map<String, dynamic> body,
) async {
final client = this.client ?? http.Client();
try {
final response = await client.post(
uri,
headers: {'Content-Type': 'application/json'},
body: jsonEncode(body),
);
if (response.statusCode == 200) {
final parsed = jsonDecode(response.body);
return parsed;
}
throw Exception("response: ${response.body}");
} catch (e, s) {
Logging.instance
.log("_makeRequest($uri) threw: $e\n$s", level: LogLevel.Error);
rethrow;
}
}
// Future<ExchangeResponse<Trade>> createNewExchange({
// required bool isFixedRate,
// required String currencyFrom,
// required String currencyTo,
// required String addressTo,
// required String userRefundAddress,
// required String userRefundExtraId,
// required String amount,
// String? extraIdTo,
// String? apiKey,
// }) async {
// Map<String, dynamic> body = {
// "fixed": isFixedRate,
// "currency_from": currencyFrom,
// "currency_to": currencyTo,
// "addressTo": addressTo,
// "userRefundAddress": userRefundAddress,
// "userRefundExtraId": userRefundExtraId,
// "amount": double.parse(amount),
// "extraIdTo": extraIdTo,
// };
//
// final uri =
// _buildUri("/create_exchange", {"api_key": apiKey ?? kSimplexApiKey});
//
// try {
// final jsonObject = await _makePostRequest(uri, body);
//
// final json = Map<String, dynamic>.from(jsonObject as Map);
// final trade = Trade(
// uuid: const Uuid().v1(),
// tradeId: json["id"] as String,
// rateType: json["type"] as String,
// direction: "direct",
// timestamp: DateTime.parse(json["timestamp"] as String),
// updatedAt: DateTime.parse(json["updated_at"] as String),
// payInCurrency: json["currency_from"] as String,
// payInAmount: json["amount_from"] as String,
// payInAddress: json["address_from"] as String,
// payInNetwork: "",
// payInExtraId: json["extra_id_from"] as String? ?? "",
// payInTxid: json["tx_from"] as String? ?? "",
// payOutCurrency: json["currency_to"] as String,
// payOutAmount: json["amount_to"] as String,
// payOutAddress: json["address_to"] as String,
// payOutNetwork: "",
// payOutExtraId: json["extra_id_to"] as String? ?? "",
// payOutTxid: json["tx_to"] as String? ?? "",
// refundAddress: json["user_refund_address"] as String,
// refundExtraId: json["user_refund_extra_id"] as String,
// status: json["status"] as String,
// exchangeName: SimplexExchange.exchangeName,
// );
// return ExchangeResponse(value: trade, exception: null);
// } catch (e, s) {
// Logging.instance.log("getAvailableCurrencies exception: $e\n$s",
// level: LogLevel.Error);
// return ExchangeResponse(
// exception: ExchangeException(
// e.toString(),
// ExchangeExceptionType.generic,
// ),
// value: null,
// );
// }
// }
Future<BuyResponse<List<Fiat>>> getAllCurrencies({
String? apiKey,
required bool fixedRate,
}) async {
final uri = _buildUri("/quote", {});
try {
final jsonArray = await _makeGetRequest(uri);
return await compute(_parseAvailableCurrenciesJson, jsonArray as List);
} catch (e, s) {
Logging.instance.log("getAvailableCurrencies exception: $e\n$s",
level: LogLevel.Error);
return BuyResponse(
exception: BuyException(
e.toString(),
BuyExceptionType.generic,
),
);
}
}
BuyResponse<List<Fiat>> _parseAvailableCurrenciesJson(
List<dynamic> jsonArray) {
try {
List<Fiat> currencies = [];
for (final json in jsonArray) {
try {
currencies.add(Fiat.fromJson(Map<String, dynamic>.from(json as Map)));
} catch (_) {
return BuyResponse(
exception: BuyException("Failed to serialize $json",
BuyExceptionType.serializeResponseError));
}
}
return BuyResponse(value: currencies);
} catch (e, s) {
Logging.instance.log("_parseAvailableCurrenciesJson exception: $e\n$s",
level: LogLevel.Error);
return BuyResponse(
exception: BuyException(
e.toString(),
BuyExceptionType.generic,
),
);
}
}
// Future<ExchangeResponse<SPCurrency>> getCurrency({
// required String symbol,
// String? apiKey,
// }) async {
// final uri = _buildUri(
// "/get_currency",
// {
// "api_key": apiKey ?? kSimplexApiKey,
// "symbol": symbol,
// },
// );
//
// try {
// final jsonObject = await _makeGetRequest(uri);
//
// return ExchangeResponse(
// value: SPCurrency.fromJson(
// Map<String, dynamic>.from(jsonObject as Map)));
// } catch (e, s) {
// Logging.instance
// .log("getCurrency exception: $e\n$s", level: LogLevel.Error);
// return ExchangeResponse(
// exception: ExchangeException(
// e.toString(),
// ExchangeExceptionType.generic,
// ),
// );
// }
// }
//
// /// returns a map where the key currency symbol is a valid pair with any of
// /// the symbols in its value list
// Future<ExchangeResponse<List<Pair>>> getAllPairs({
// required bool isFixedRate,
// String? apiKey,
// }) async {
// final uri = _buildUri(
// "/get_all_pairs",
// {
// "api_key": apiKey ?? kSimplexApiKey,
// "fixed": isFixedRate.toString(),
// },
// );
//
// try {
// final jsonObject = await _makeGetRequest(uri);
// final result = await compute(
// _parseAvailablePairsJson,
// Tuple2(jsonObject as Map, isFixedRate),
// );
// return result;
// } catch (e, s) {
// Logging.instance
// .log("getAllPairs exception: $e\n$s", level: LogLevel.Error);
// return ExchangeResponse(
// exception: ExchangeException(
// e.toString(),
// ExchangeExceptionType.generic,
// ),
// );
// }
// }
//
// ExchangeResponse<List<Pair>> _parseAvailablePairsJson(
// Tuple2<Map<dynamic, dynamic>, bool> args,
// ) {
// try {
// List<Pair> pairs = [];
//
// for (final entry in args.item1.entries) {
// try {
// final from = entry.key as String;
// for (final to in entry.value as List) {
// pairs.add(
// Pair(
// from: from,
// fromNetwork: "",
// to: to as String,
// toNetwork: "",
// fixedRate: args.item2,
// floatingRate: !args.item2,
// ),
// );
// }
// } catch (_) {
// return ExchangeResponse(
// exception: ExchangeException("Failed to serialize $json",
// ExchangeExceptionType.serializeResponseError));
// }
// }
//
// return ExchangeResponse(value: pairs);
// } catch (e, s) {
// Logging.instance.log("_parseAvailableCurrenciesJson exception: $e\n$s",
// level: LogLevel.Error);
// return ExchangeResponse(
// exception: ExchangeException(
// e.toString(),
// ExchangeExceptionType.generic,
// ),
// );
// }
// }
//
// /// returns the estimated amount as a string
// Future<ExchangeResponse<String>> getEstimated({
// required bool isFixedRate,
// required String currencyFrom,
// required String currencyTo,
// required String amount,
// String? apiKey,
// }) async {
// final uri = _buildUri(
// "/get_estimated",
// {
// "api_key": apiKey ?? kSimplexApiKey,
// "fixed": isFixedRate.toString(),
// "currency_from": currencyFrom,
// "currency_to": currencyTo,
// "amount": amount,
// },
// );
//
// try {
// final jsonObject = await _makeGetRequest(uri);
//
// return ExchangeResponse(value: jsonObject as String);
// } catch (e, s) {
// Logging.instance
// .log("getEstimated exception: $e\n$s", level: LogLevel.Error);
// return ExchangeResponse(
// exception: ExchangeException(
// e.toString(),
// ExchangeExceptionType.generic,
// ),
// );
// }
// }
//
// /// returns the exchange for the given id
// Future<ExchangeResponse<Trade>> getExchange({
// required String exchangeId,
// String? apiKey,
// Trade? oldTrade,
// }) async {
// final uri = _buildUri(
// "/get_exchange",
// {
// "api_key": apiKey ?? kSimplexApiKey,
// "id": exchangeId,
// },
// );
//
// try {
// final jsonObject = await _makeGetRequest(uri);
//
// final json = Map<String, dynamic>.from(jsonObject as Map);
// final ts = DateTime.parse(json["timestamp"] as String);
// final trade = Trade(
// uuid: oldTrade?.uuid ?? const Uuid().v1(),
// tradeId: json["id"] as String,
// rateType: json["type"] as String,
// direction: "direct",
// timestamp: ts,
// updatedAt: DateTime.tryParse(json["updated_at"] as String? ?? "") ?? ts,
// payInCurrency: json["currency_from"] as String,
// payInAmount: json["amount_from"] as String,
// payInAddress: json["address_from"] as String,
// payInNetwork: "",
// payInExtraId: json["extra_id_from"] as String? ?? "",
// payInTxid: json["tx_from"] as String? ?? "",
// payOutCurrency: json["currency_to"] as String,
// payOutAmount: json["amount_to"] as String,
// payOutAddress: json["address_to"] as String,
// payOutNetwork: "",
// payOutExtraId: json["extra_id_to"] as String? ?? "",
// payOutTxid: json["tx_to"] as String? ?? "",
// refundAddress: json["user_refund_address"] as String,
// refundExtraId: json["user_refund_extra_id"] as String,
// status: json["status"] as String,
// exchangeName: SimplexExchange.exchangeName,
// );
//
// return ExchangeResponse(value: trade);
// } catch (e, s) {
// Logging.instance
// .log("getExchange exception: $e\n$s", level: LogLevel.Error);
// return ExchangeResponse(
// exception: ExchangeException(
// e.toString(),
// ExchangeExceptionType.generic,
// ),
// );
// }
// }
//
// /// returns the minimal exchange amount
// Future<ExchangeResponse<Range>> getRange({
// required bool isFixedRate,
// required String currencyFrom,
// required String currencyTo,
// String? apiKey,
// }) async {
// final uri = _buildUri(
// "/get_ranges",
// {
// "api_key": apiKey ?? kSimplexApiKey,
// "fixed": isFixedRate.toString(),
// "currency_from": currencyFrom,
// "currency_to": currencyTo,
// },
// );
//
// try {
// final jsonObject = await _makeGetRequest(uri);
//
// final json = Map<String, dynamic>.from(jsonObject as Map);
// return ExchangeResponse(
// value: Range(
// max: Decimal.tryParse(json["max"] as String? ?? ""),
// min: Decimal.tryParse(json["min"] as String? ?? ""),
// ),
// );
// } catch (e, s) {
// Logging.instance.log("getRange exception: $e\n$s", level: LogLevel.Error);
// return ExchangeResponse(
// exception: ExchangeException(
// e.toString(),
// ExchangeExceptionType.generic,
// ),
// );
// }
// }
//
// Future<ExchangeResponse<List<FixedRateMarket>>> getFixedRateMarketInfo({
// String? apiKey,
// }) async {
// final uri = _buildUri(
// "/get_market_info",
// null,
// // {
// // "api_key": apiKey ?? kSimplexApiKey,
// // "fixed": isFixedRate.toString(),
// // "currency_from": currencyFrom,
// // "currency_to": currencyTo,
// // },
// );
//
// try {
// final jsonArray = await _makeGetRequest(uri);
//
// try {
// final result = await compute(
// _parseFixedRateMarketsJson,
// jsonArray as List,
// );
// return result;
// } catch (e, s) {
// Logging.instance.log("getAvailableFixedRateMarkets exception: $e\n$s",
// level: LogLevel.Error);
// return ExchangeResponse(
// exception: ExchangeException(
// "Error: $jsonArray",
// ExchangeExceptionType.serializeResponseError,
// ),
// );
// }
// } catch (e, s) {
// Logging.instance.log("getAvailableFixedRateMarkets exception: $e\n$s",
// level: LogLevel.Error);
// return ExchangeResponse(
// exception: ExchangeException(
// e.toString(),
// ExchangeExceptionType.generic,
// ),
// );
// }
// }
//
// ExchangeResponse<List<FixedRateMarket>> _parseFixedRateMarketsJson(
// List<dynamic> jsonArray) {
// try {
// final List<FixedRateMarket> markets = [];
// for (final json in jsonArray) {
// try {
// final map = Map<String, dynamic>.from(json as Map);
// markets.add(FixedRateMarket(
// from: map["currency_from"] as String,
// to: map["currency_to"] as String,
// min: Decimal.parse(map["min"] as String),
// max: Decimal.parse(map["max"] as String),
// rate: Decimal.parse(map["rate"] as String),
// minerFee: null,
// ));
// } catch (_) {
// return ExchangeResponse(
// exception: ExchangeException("Failed to serialize $json",
// ExchangeExceptionType.serializeResponseError));
// }
// }
// return ExchangeResponse(value: markets);
// } catch (_) {
// rethrow;
// }
// }
}