stack_wallet/lib/services/exchange/majestic_bank/majestic_bank_api.dart

393 lines
11 KiB
Dart
Raw Normal View History

2023-05-26 21:21:16 +00:00
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
2023-01-29 17:21:35 +00:00
import 'dart:convert';
import 'package:decimal/decimal.dart';
import '../../../exceptions/exchange/exchange_exception.dart';
import '../../../exceptions/exchange/majestic_bank/mb_exception.dart';
import '../../../exceptions/exchange/pair_unavailable_exception.dart';
import '../../../models/exchange/majestic_bank/mb_limit.dart';
import '../../../models/exchange/majestic_bank/mb_order.dart';
import '../../../models/exchange/majestic_bank/mb_order_calculation.dart';
import '../../../models/exchange/majestic_bank/mb_order_status.dart';
import '../../../models/exchange/majestic_bank/mb_rate.dart';
import '../../../networking/http.dart';
import '../exchange_response.dart';
import '../../tor_service.dart';
import '../../../utilities/logger.dart';
import '../../../utilities/prefs.dart';
2023-01-29 17:21:35 +00:00
class MajesticBankAPI {
static const String scheme = "https";
static const String authority = "majesticbank.sc";
static const String version = "v1";
static const kMajesticBankRefCode = "rjWugM";
2023-01-29 17:21:35 +00:00
MajesticBankAPI._();
static final MajesticBankAPI _instance = MajesticBankAPI._();
static MajesticBankAPI get instance => _instance;
2023-09-11 20:20:40 +00:00
HTTP client = HTTP();
2023-01-29 17:21:35 +00:00
Uri _buildUri({required String endpoint, Map<String, String>? params}) {
return Uri.https(authority, "/api/$version/$endpoint", params);
}
Future<dynamic> _makeGetRequest(Uri uri) async {
2023-09-11 20:20:40 +00:00
// final client = this.client ?? http.Client();
2023-01-29 17:21:35 +00:00
int code = -1;
try {
final response = await client.get(
2023-09-11 20:20:40 +00:00
url: uri,
2023-09-15 19:51:20 +00:00
proxyInfo: Prefs.instance.useTor
? TorService.sharedInstance.getProxyInfo()
: null,
2023-01-29 17:21:35 +00:00
);
2023-09-11 20:20:40 +00:00
code = response.code;
2023-01-29 17:21:35 +00:00
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<ExchangeResponse<List<MBRate>>> getRates() async {
2023-01-29 17:21:35 +00:00
final uri = _buildUri(
endpoint: "rates",
);
try {
final jsonObject = await _makeGetRequest(uri);
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);
2023-01-29 17:21:35 +00:00
} catch (e, s) {
Logging.instance.log("getRates exception: $e\n$s", level: LogLevel.Error);
return ExchangeResponse(
exception: ExchangeException(
e.toString(),
ExchangeExceptionType.generic,
),
);
}
}
Future<ExchangeResponse<MBLimit>> getLimit({
required String fromCurrency,
}) async {
final uri = _buildUri(
endpoint: "limits",
params: {
"from_currency": fromCurrency,
},
);
try {
final jsonObject = await _makeGetRequest(uri);
final map = Map<String, dynamic>.from(jsonObject as Map);
final limit = MBLimit(
currency: fromCurrency,
min: Decimal.parse(map["min"].toString()),
max: Decimal.parse(map["max"].toString()),
);
return ExchangeResponse(value: limit);
} catch (e, s) {
Logging.instance
.log("getLimits exception: $e\n$s", level: LogLevel.Error);
return ExchangeResponse(
exception: ExchangeException(
e.toString(),
ExchangeExceptionType.generic,
),
);
}
}
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,
),
);
}
}
/// If [reversed] then the amount is the expected receive_amount, otherwise
/// the amount is assumed to be the from_amount.
Future<ExchangeResponse<MBOrderCalculation>> calculateOrder({
required String amount,
required bool reversed,
required String fromCurrency,
required String receiveCurrency,
}) async {
final params = {
"from_currency": fromCurrency,
"receive_currency": receiveCurrency,
};
if (reversed) {
params["receive_amount"] = amount;
} else {
params["from_amount"] = amount;
}
2023-02-04 15:16:05 +00:00
final uri = _buildUri(
endpoint: "calculate",
params: params,
);
2023-01-29 17:21:35 +00:00
try {
final jsonObject = await _makeGetRequest(uri);
2023-02-04 15:16:05 +00:00
final map = Map<String, dynamic>.from(jsonObject as Map);
if (map["error"] != null) {
final errorMessage = map["extra"] as String?;
if (errorMessage != null &&
errorMessage.startsWith("Bad") &&
errorMessage.endsWith("currency symbol")) {
return ExchangeResponse(
exception: PairUnavailableException(
errorMessage,
ExchangeExceptionType.generic,
),
);
} else {
return ExchangeResponse(
exception: ExchangeException(
errorMessage ?? "Error: ${map["error"]}",
ExchangeExceptionType.generic,
),
);
}
}
2023-02-04 15:16:05 +00:00
final result = MBOrderCalculation(
fromCurrency: map["from_currency"] as String,
fromAmount: Decimal.parse(map["from_amount"].toString()),
receiveCurrency: map["receive_currency"] as String,
receiveAmount: Decimal.parse(map["receive_amount"].toString()),
);
2023-01-29 17:21:35 +00:00
2023-02-04 15:16:05 +00:00
return ExchangeResponse(value: result);
2023-01-29 17:21:35 +00:00
} catch (e, s) {
2023-02-05 23:50:40 +00:00
Logging.instance.log(
2024-05-27 23:56:22 +00:00
"calculateOrder $fromCurrency-$receiveCurrency exception: $e\n$s",
level: LogLevel.Error,
);
2023-01-29 17:21:35 +00:00
return ExchangeResponse(
exception: ExchangeException(
e.toString(),
ExchangeExceptionType.generic,
),
);
}
}
2023-02-04 15:16:05 +00:00
Future<ExchangeResponse<MBOrder>> createOrder({
required String fromAmount,
2023-01-29 17:21:35 +00:00
required String fromCurrency,
required String receiveCurrency,
required String receiveAddress,
}) async {
final params = {
2023-02-04 15:16:05 +00:00
"from_amount": fromAmount,
2023-01-29 17:21:35 +00:00
"from_currency": fromCurrency,
"receive_currency": receiveCurrency,
"receive_address": receiveAddress,
2023-02-06 21:34:17 +00:00
"referral_code": kMajesticBankRefCode,
2023-01-29 17:21:35 +00:00
};
2023-02-04 15:16:05 +00:00
final uri = _buildUri(endpoint: "exchange", params: params);
2023-01-29 17:21:35 +00:00
try {
final now = DateTime.now();
final jsonObject = await _makeGetRequest(uri);
final json = Map<String, dynamic>.from(jsonObject as Map);
2023-02-04 15:16:05 +00:00
final order = MBOrder(
orderId: json["trx"] as String,
fromCurrency: json["from_currency"] as String,
fromAmount: Decimal.parse(json["from_amount"].toString()),
receiveCurrency: json["receive_currency"] as String,
receiveAmount: Decimal.parse(json["receive_amount"].toString()),
address: json["address"] as String,
orderType: MBOrderType.floating,
expiration: json["expiration"] as int,
createdAt: now,
2023-01-29 17:21:35 +00:00
);
2023-02-04 15:16:05 +00:00
return ExchangeResponse(value: order);
2023-01-29 17:21:35 +00:00
} catch (e, s) {
Logging.instance
.log("createOrder exception: $e\n$s", level: LogLevel.Error);
return ExchangeResponse(
exception: ExchangeException(
e.toString(),
ExchangeExceptionType.generic,
),
);
}
}
2023-02-04 15:16:05 +00:00
/// Fixed rate for 10 minutes, useful for payments.
/// If [reversed] then the amount is the expected receive_amount, otherwise
/// the amount is assumed to be the from_amount.
Future<ExchangeResponse<MBOrder>> createFixedRateOrder({
2023-01-29 17:21:35 +00:00
required String amount,
required String fromCurrency,
required String receiveCurrency,
required String receiveAddress,
required bool reversed,
}) async {
final params = {
"from_currency": fromCurrency,
"receive_currency": receiveCurrency,
"receive_address": receiveAddress,
2023-02-06 21:34:17 +00:00
"referral_code": kMajesticBankRefCode,
2023-01-29 17:21:35 +00:00
};
if (reversed) {
params["receive_amount"] = amount;
} else {
params["from_amount"] = amount;
}
final uri = _buildUri(endpoint: "pay", params: params);
try {
final now = DateTime.now();
final jsonObject = await _makeGetRequest(uri);
final json = Map<String, dynamic>.from(jsonObject as Map);
2023-02-04 15:16:05 +00:00
final order = MBOrder(
orderId: json["trx"] as String,
fromCurrency: json["from_currency"] as String,
fromAmount: Decimal.parse(json["from_amount"].toString()),
receiveCurrency: json["receive_currency"] as String,
receiveAmount: Decimal.parse(json["receive_amount"].toString()),
address: json["address"] as String,
orderType: MBOrderType.fixed,
expiration: json["expiration"] as int,
createdAt: now,
2023-01-29 17:21:35 +00:00
);
2023-02-04 15:16:05 +00:00
return ExchangeResponse(value: order);
2023-01-29 17:21:35 +00:00
} catch (e, s) {
Logging.instance
.log("createFixedRateOrder exception: $e\n$s", level: LogLevel.Error);
return ExchangeResponse(
exception: ExchangeException(
e.toString(),
ExchangeExceptionType.generic,
),
);
}
}
Future<ExchangeResponse<MBOrderStatus>> trackOrder({
required String orderId,
}) async {
2024-05-27 23:56:22 +00:00
final uri = _buildUri(
endpoint: "track",
params: {
"trx": orderId,
},
);
2023-01-29 17:21:35 +00:00
try {
final jsonObject = await _makeGetRequest(uri);
2023-02-04 15:16:05 +00:00
final json = Map<String, dynamic>.from(jsonObject as Map);
if (json.length == 2) {
return ExchangeResponse(
exception: MBException(
json["status"] as String,
ExchangeExceptionType.orderNotFound,
),
);
}
2023-02-04 15:16:05 +00:00
final status = MBOrderStatus(
orderId: json["trx"] as String,
status: json["status"] as String,
fromCurrency: json["from_currency"] as String,
fromAmount: Decimal.parse(json["from_amount"].toString()),
receiveCurrency: json["receive_currency"] as String,
receiveAmount: Decimal.parse(json["receive_amount"].toString()),
address: json["address"] as String,
received: Decimal.parse(json["received"].toString()),
confirmed: Decimal.parse(json["confirmed"].toString()),
);
2023-01-29 17:21:35 +00:00
2023-02-04 15:16:05 +00:00
return ExchangeResponse(value: status);
2023-01-29 17:21:35 +00:00
} catch (e, s) {
2023-03-03 20:46:53 +00:00
Logging.instance.log(
"trackOrder exception when trying to parse $json: $e\n$s",
level: LogLevel.Error,
);
2023-01-29 17:21:35 +00:00
return ExchangeResponse(
exception: ExchangeException(
e.toString(),
ExchangeExceptionType.generic,
),
);
}
}
}