From e666928d63294613779bf85dfc7d54e8e4851f92 Mon Sep 17 00:00:00 2001 From: julian Date: Wed, 1 Feb 2023 18:24:41 -0600 Subject: [PATCH] majestic bank limits and rates api calls --- .../exchange/majestic_bank/mb_limit.dart | 19 +++++++ .../exchange/majestic_bank/mb_object.dart | 1 + .../exchange/majestic_bank/mb_rate.dart | 15 ++++++ .../majestic_bank/majestic_bank_api.dart | 53 ++++++++++++++++++- 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 lib/models/exchange/majestic_bank/mb_limit.dart create mode 100644 lib/models/exchange/majestic_bank/mb_object.dart create mode 100644 lib/models/exchange/majestic_bank/mb_rate.dart diff --git a/lib/models/exchange/majestic_bank/mb_limit.dart b/lib/models/exchange/majestic_bank/mb_limit.dart new file mode 100644 index 000000000..baa002d56 --- /dev/null +++ b/lib/models/exchange/majestic_bank/mb_limit.dart @@ -0,0 +1,19 @@ +import 'package:decimal/decimal.dart'; +import 'package:stackwallet/models/exchange/majestic_bank/mb_object.dart'; + +class MBLimit extends MBObject { + MBLimit({ + required this.currency, + required this.min, + required this.max, + }); + + final String currency; + final Decimal min; + final Decimal max; + + @override + String toString() { + return "MBLimit: { $currency: { min: $min, max: $max } }"; + } +} diff --git a/lib/models/exchange/majestic_bank/mb_object.dart b/lib/models/exchange/majestic_bank/mb_object.dart new file mode 100644 index 000000000..e3810131c --- /dev/null +++ b/lib/models/exchange/majestic_bank/mb_object.dart @@ -0,0 +1 @@ +abstract class MBObject {} diff --git a/lib/models/exchange/majestic_bank/mb_rate.dart b/lib/models/exchange/majestic_bank/mb_rate.dart new file mode 100644 index 000000000..60d71cdf0 --- /dev/null +++ b/lib/models/exchange/majestic_bank/mb_rate.dart @@ -0,0 +1,15 @@ +import 'package:decimal/decimal.dart'; +import 'package:stackwallet/models/exchange/majestic_bank/mb_object.dart'; + +class MBRate extends MBObject { + MBRate({required this.fromCurrency, required this.toCurrency, required this.rate,}); + + final String fromCurrency; + final String toCurrency; + final Decimal rate; + + @override + String toString() { + return "MBRate: { $fromCurrency-$toCurrency: $rate }"; + } +} diff --git a/lib/services/exchange/majestic_bank/majestic_bank_api.dart b/lib/services/exchange/majestic_bank/majestic_bank_api.dart index 58333b807..00d4e2e20 100644 --- a/lib/services/exchange/majestic_bank/majestic_bank_api.dart +++ b/lib/services/exchange/majestic_bank/majestic_bank_api.dart @@ -1,6 +1,9 @@ import 'dart:convert'; +import 'package:decimal/decimal.dart'; import 'package:http/http.dart' as http; +import 'package:stackwallet/models/exchange/majestic_bank/mb_limit.dart'; +import 'package:stackwallet/models/exchange/majestic_bank/mb_rate.dart'; import 'package:stackwallet/models/exchange/response_objects/trade.dart'; import 'package:stackwallet/services/exchange/exchange_response.dart'; import 'package:stackwallet/services/exchange/majestic_bank/majestic_bank_exchange.dart'; @@ -54,7 +57,7 @@ class MajesticBankAPI { } } - Future getRates() async { + Future>> getRates() async { final uri = _buildUri( endpoint: "rates", ); @@ -62,7 +65,20 @@ class MajesticBankAPI { try { final jsonObject = await _makeGetRequest(uri); - return getPrettyJSONString(jsonObject); + final map = Map.from(jsonObject as Map); + final List rates = []; + for (final key in map.keys) { + final currencies = key.split("-"); + if (currencies.length == 2) { + final rate = MBRate( + fromCurrency: currencies.first, + toCurrency: currencies.last, + rate: Decimal.parse(map[key].toString()), + ); + rates.add(rate); + } + } + return ExchangeResponse(value: rates); } catch (e, s) { Logging.instance.log("getRates exception: $e\n$s", level: LogLevel.Error); return ExchangeResponse( @@ -74,6 +90,39 @@ class MajesticBankAPI { } } + Future>> getLimits() async { + final uri = _buildUri( + endpoint: + "rates", // limits are included in the rates call for some reason??? + ); + + try { + final jsonObject = await _makeGetRequest(uri); + + final map = Map.from(jsonObject as Map)["limits"] as Map; + final List limits = []; + for (final key in map.keys) { + final limit = MBLimit( + currency: key as String, + min: Decimal.parse(map[key]["min"].toString()), + max: Decimal.parse(map[key]["max"].toString()), + ); + limits.add(limit); + } + + return ExchangeResponse(value: limits); + } catch (e, s) { + Logging.instance + .log("getLimits exception: $e\n$s", level: LogLevel.Error); + return ExchangeResponse( + exception: ExchangeException( + e.toString(), + ExchangeExceptionType.generic, + ), + ); + } + } + Future calculateOrder() async { final uri = _buildUri( endpoint: "calculate",