majestic bank limits and rates api calls

This commit is contained in:
julian 2023-02-01 18:24:41 -06:00
parent 773c5fad9c
commit e666928d63
4 changed files with 86 additions and 2 deletions

View file

@ -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 } }";
}
}

View file

@ -0,0 +1 @@
abstract class MBObject {}

View file

@ -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 }";
}
}

View file

@ -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<dynamic> getRates() async {
Future<ExchangeResponse<List<MBRate>>> 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<String, dynamic>.from(jsonObject as Map);
final List<MBRate> 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<ExchangeResponse<List<MBLimit>>> 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<String, dynamic>.from(jsonObject as Map)["limits"] as Map;
final List<MBLimit> 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<dynamic> calculateOrder() async {
final uri = _buildUri(
endpoint: "calculate",