mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2024-11-17 09:47:37 +00:00
majestic bank dart api impl
This commit is contained in:
parent
e2ee38bc83
commit
a3b5ba5b04
3 changed files with 142 additions and 73 deletions
43
lib/models/exchange/majestic_bank/mb_order.dart
Normal file
43
lib/models/exchange/majestic_bank/mb_order.dart
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
import 'package:decimal/decimal.dart';
|
||||||
|
import 'package:stackwallet/models/exchange/majestic_bank/mb_object.dart';
|
||||||
|
|
||||||
|
enum MBOrderType {
|
||||||
|
fixed,
|
||||||
|
floating,
|
||||||
|
}
|
||||||
|
|
||||||
|
class MBOrder extends MBObject {
|
||||||
|
MBOrder({
|
||||||
|
required this.orderId,
|
||||||
|
required this.fromCurrency,
|
||||||
|
required this.fromAmount,
|
||||||
|
required this.receiveCurrency,
|
||||||
|
required this.receiveAmount,
|
||||||
|
required this.address,
|
||||||
|
required this.orderType,
|
||||||
|
required this.expiration,
|
||||||
|
required this.createdAt,
|
||||||
|
});
|
||||||
|
|
||||||
|
final String orderId;
|
||||||
|
final String fromCurrency;
|
||||||
|
final Decimal fromAmount;
|
||||||
|
final String receiveCurrency;
|
||||||
|
final String address;
|
||||||
|
final Decimal receiveAmount;
|
||||||
|
final MBOrderType orderType;
|
||||||
|
|
||||||
|
/// minutes
|
||||||
|
final int expiration;
|
||||||
|
|
||||||
|
final DateTime createdAt;
|
||||||
|
|
||||||
|
bool isExpired() =>
|
||||||
|
(DateTime.now().difference(createdAt) >= Duration(minutes: expiration));
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() {
|
||||||
|
// todo: full toString
|
||||||
|
return orderId;
|
||||||
|
}
|
||||||
|
}
|
32
lib/models/exchange/majestic_bank/mb_order_status.dart
Normal file
32
lib/models/exchange/majestic_bank/mb_order_status.dart
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
import 'package:decimal/decimal.dart';
|
||||||
|
import 'package:stackwallet/models/exchange/majestic_bank/mb_object.dart';
|
||||||
|
|
||||||
|
class MBOrderStatus extends MBObject {
|
||||||
|
MBOrderStatus({
|
||||||
|
required this.orderId,
|
||||||
|
required this.status,
|
||||||
|
required this.fromCurrency,
|
||||||
|
required this.fromAmount,
|
||||||
|
required this.receiveCurrency,
|
||||||
|
required this.receiveAmount,
|
||||||
|
required this.address,
|
||||||
|
required this.received,
|
||||||
|
required this.confirmed,
|
||||||
|
});
|
||||||
|
|
||||||
|
final String orderId;
|
||||||
|
final String status;
|
||||||
|
final String fromCurrency;
|
||||||
|
final Decimal fromAmount;
|
||||||
|
final String receiveCurrency;
|
||||||
|
final Decimal receiveAmount;
|
||||||
|
final String address;
|
||||||
|
final Decimal received;
|
||||||
|
final Decimal confirmed;
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() {
|
||||||
|
// todo: full toString
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,19 +3,18 @@ import 'dart:convert';
|
||||||
import 'package:decimal/decimal.dart';
|
import 'package:decimal/decimal.dart';
|
||||||
import 'package:http/http.dart' as http;
|
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_limit.dart';
|
||||||
|
import 'package:stackwallet/models/exchange/majestic_bank/mb_order.dart';
|
||||||
import 'package:stackwallet/models/exchange/majestic_bank/mb_order_calculation.dart';
|
import 'package:stackwallet/models/exchange/majestic_bank/mb_order_calculation.dart';
|
||||||
|
import 'package:stackwallet/models/exchange/majestic_bank/mb_order_status.dart';
|
||||||
import 'package:stackwallet/models/exchange/majestic_bank/mb_rate.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/exchange_response.dart';
|
||||||
import 'package:stackwallet/services/exchange/majestic_bank/majestic_bank_exchange.dart';
|
|
||||||
import 'package:stackwallet/utilities/logger.dart';
|
import 'package:stackwallet/utilities/logger.dart';
|
||||||
import 'package:uuid/uuid.dart';
|
|
||||||
|
|
||||||
class MajesticBankAPI {
|
class MajesticBankAPI {
|
||||||
static const String scheme = "https";
|
static const String scheme = "https";
|
||||||
static const String authority = "majesticbank.sc";
|
static const String authority = "majesticbank.sc";
|
||||||
static const String version = "v1";
|
static const String version = "v1";
|
||||||
static const String refCode = "";
|
static const String refCode = "fixme";
|
||||||
|
|
||||||
MajesticBankAPI._();
|
MajesticBankAPI._();
|
||||||
|
|
||||||
|
@ -30,11 +29,6 @@ class MajesticBankAPI {
|
||||||
return Uri.https(authority, "/api/$version/$endpoint", params);
|
return Uri.https(authority, "/api/$version/$endpoint", params);
|
||||||
}
|
}
|
||||||
|
|
||||||
String getPrettyJSONString(jsonObject) {
|
|
||||||
var encoder = const JsonEncoder.withIndent(" ");
|
|
||||||
return encoder.convert(jsonObject);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<dynamic> _makeGetRequest(Uri uri) async {
|
Future<dynamic> _makeGetRequest(Uri uri) async {
|
||||||
final client = this.client ?? http.Client();
|
final client = this.client ?? http.Client();
|
||||||
int code = -1;
|
int code = -1;
|
||||||
|
@ -132,10 +126,6 @@ class MajesticBankAPI {
|
||||||
required String fromCurrency,
|
required String fromCurrency,
|
||||||
required String receiveCurrency,
|
required String receiveCurrency,
|
||||||
}) async {
|
}) async {
|
||||||
final uri = _buildUri(
|
|
||||||
endpoint: "calculate",
|
|
||||||
);
|
|
||||||
|
|
||||||
final params = {
|
final params = {
|
||||||
"from_currency": fromCurrency,
|
"from_currency": fromCurrency,
|
||||||
"receive_currency": receiveCurrency,
|
"receive_currency": receiveCurrency,
|
||||||
|
@ -147,11 +137,22 @@ class MajesticBankAPI {
|
||||||
params["from_amount"] = amount;
|
params["from_amount"] = amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final uri = _buildUri(
|
||||||
|
endpoint: "calculate",
|
||||||
|
params: params,
|
||||||
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final jsonObject = await _makeGetRequest(uri);
|
final jsonObject = await _makeGetRequest(uri);
|
||||||
|
final map = Map<String, dynamic>.from(jsonObject as Map);
|
||||||
|
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()),
|
||||||
|
);
|
||||||
|
|
||||||
// return getPrettyJSONString(jsonObject);
|
return ExchangeResponse(value: result);
|
||||||
return ExchangeResponse();
|
|
||||||
} catch (e, s) {
|
} catch (e, s) {
|
||||||
Logging.instance
|
Logging.instance
|
||||||
.log("calculateOrder exception: $e\n$s", level: LogLevel.Error);
|
.log("calculateOrder exception: $e\n$s", level: LogLevel.Error);
|
||||||
|
@ -164,52 +165,40 @@ class MajesticBankAPI {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<ExchangeResponse<Trade>> createOrder({
|
Future<ExchangeResponse<MBOrder>> createOrder({
|
||||||
required String amount,
|
required String fromAmount,
|
||||||
required String fromCurrency,
|
required String fromCurrency,
|
||||||
required String receiveCurrency,
|
required String receiveCurrency,
|
||||||
required String receiveAddress,
|
required String receiveAddress,
|
||||||
}) async {
|
}) async {
|
||||||
final params = {
|
final params = {
|
||||||
"from_amount": amount,
|
"from_amount": fromAmount,
|
||||||
"from_currency": fromCurrency,
|
"from_currency": fromCurrency,
|
||||||
"receive_currency": receiveCurrency,
|
"receive_currency": receiveCurrency,
|
||||||
"receive_address": receiveAddress,
|
"receive_address": receiveAddress,
|
||||||
"referral_code": refCode,
|
"referral_code": refCode,
|
||||||
};
|
};
|
||||||
|
|
||||||
final uri = _buildUri(endpoint: "create", params: params);
|
final uri = _buildUri(endpoint: "exchange", params: params);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final now = DateTime.now();
|
final now = DateTime.now();
|
||||||
final jsonObject = await _makeGetRequest(uri);
|
final jsonObject = await _makeGetRequest(uri);
|
||||||
final json = Map<String, dynamic>.from(jsonObject as Map);
|
final json = Map<String, dynamic>.from(jsonObject as Map);
|
||||||
|
|
||||||
final trade = Trade(
|
final order = MBOrder(
|
||||||
uuid: const Uuid().v1(),
|
orderId: json["trx"] as String,
|
||||||
tradeId: json["trx"] as String,
|
fromCurrency: json["from_currency"] as String,
|
||||||
rateType: "floating-rate",
|
fromAmount: Decimal.parse(json["from_amount"].toString()),
|
||||||
direction: "direct",
|
receiveCurrency: json["receive_currency"] as String,
|
||||||
timestamp: now,
|
receiveAmount: Decimal.parse(json["receive_amount"].toString()),
|
||||||
updatedAt: now,
|
address: json["address"] as String,
|
||||||
payInCurrency: json["from_currency"] as String,
|
orderType: MBOrderType.floating,
|
||||||
payInAmount: json["from_amount"] as String,
|
expiration: json["expiration"] as int,
|
||||||
payInAddress: json["address"] as String,
|
createdAt: now,
|
||||||
payInNetwork: "",
|
|
||||||
payInExtraId: "",
|
|
||||||
payInTxid: "",
|
|
||||||
payOutCurrency: json["receive_currency"] as String,
|
|
||||||
payOutAmount: json["receive_amount"] as String,
|
|
||||||
payOutAddress: json["receive_address"] as String,
|
|
||||||
payOutNetwork: "",
|
|
||||||
payOutExtraId: "",
|
|
||||||
payOutTxid: "",
|
|
||||||
refundAddress: "",
|
|
||||||
refundExtraId: "",
|
|
||||||
status: "Waiting",
|
|
||||||
exchangeName: MajesticBankExchange.exchangeName,
|
|
||||||
);
|
);
|
||||||
return ExchangeResponse(value: trade);
|
|
||||||
|
return ExchangeResponse(value: order);
|
||||||
} catch (e, s) {
|
} catch (e, s) {
|
||||||
Logging.instance
|
Logging.instance
|
||||||
.log("createOrder exception: $e\n$s", level: LogLevel.Error);
|
.log("createOrder exception: $e\n$s", level: LogLevel.Error);
|
||||||
|
@ -222,7 +211,10 @@ class MajesticBankAPI {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<ExchangeResponse<Trade>> createFixedRateOrder({
|
/// 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({
|
||||||
required String amount,
|
required String amount,
|
||||||
required String fromCurrency,
|
required String fromCurrency,
|
||||||
required String receiveCurrency,
|
required String receiveCurrency,
|
||||||
|
@ -249,31 +241,19 @@ class MajesticBankAPI {
|
||||||
final jsonObject = await _makeGetRequest(uri);
|
final jsonObject = await _makeGetRequest(uri);
|
||||||
final json = Map<String, dynamic>.from(jsonObject as Map);
|
final json = Map<String, dynamic>.from(jsonObject as Map);
|
||||||
|
|
||||||
final trade = Trade(
|
final order = MBOrder(
|
||||||
uuid: const Uuid().v1(),
|
orderId: json["trx"] as String,
|
||||||
tradeId: json["trx"] as String,
|
fromCurrency: json["from_currency"] as String,
|
||||||
rateType: "fixed-rate",
|
fromAmount: Decimal.parse(json["from_amount"].toString()),
|
||||||
direction: reversed ? "reversed" : "direct",
|
receiveCurrency: json["receive_currency"] as String,
|
||||||
timestamp: now,
|
receiveAmount: Decimal.parse(json["receive_amount"].toString()),
|
||||||
updatedAt: now,
|
address: json["address"] as String,
|
||||||
payInCurrency: json["from_currency"] as String,
|
orderType: MBOrderType.fixed,
|
||||||
payInAmount: json["from_amount"] as String,
|
expiration: json["expiration"] as int,
|
||||||
payInAddress: json["address"] as String,
|
createdAt: now,
|
||||||
payInNetwork: "",
|
|
||||||
payInExtraId: "",
|
|
||||||
payInTxid: "",
|
|
||||||
payOutCurrency: json["receive_currency"] as String,
|
|
||||||
payOutAmount: json["receive_amount"] as String,
|
|
||||||
payOutAddress: json["receive_address"] as String,
|
|
||||||
payOutNetwork: "",
|
|
||||||
payOutExtraId: "",
|
|
||||||
payOutTxid: "",
|
|
||||||
refundAddress: "",
|
|
||||||
refundExtraId: "",
|
|
||||||
status: "Waiting",
|
|
||||||
exchangeName: MajesticBankExchange.exchangeName,
|
|
||||||
);
|
);
|
||||||
return ExchangeResponse(value: trade);
|
|
||||||
|
return ExchangeResponse(value: order);
|
||||||
} catch (e, s) {
|
} catch (e, s) {
|
||||||
Logging.instance
|
Logging.instance
|
||||||
.log("createFixedRateOrder exception: $e\n$s", level: LogLevel.Error);
|
.log("createFixedRateOrder exception: $e\n$s", level: LogLevel.Error);
|
||||||
|
@ -286,15 +266,29 @@ class MajesticBankAPI {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<dynamic> trackOrder({required String orderId}) async {
|
Future<ExchangeResponse<MBOrderStatus>> trackOrder(
|
||||||
final uri = _buildUri(
|
{required String orderId}) async {
|
||||||
endpoint: "track",
|
final uri = _buildUri(endpoint: "track", params: {
|
||||||
);
|
"trx": orderId,
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final jsonObject = await _makeGetRequest(uri);
|
final jsonObject = await _makeGetRequest(uri);
|
||||||
|
final json = Map<String, dynamic>.from(jsonObject as Map);
|
||||||
|
|
||||||
return getPrettyJSONString(jsonObject);
|
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()),
|
||||||
|
);
|
||||||
|
|
||||||
|
return ExchangeResponse(value: status);
|
||||||
} catch (e, s) {
|
} catch (e, s) {
|
||||||
Logging.instance
|
Logging.instance
|
||||||
.log("createOrder exception: $e\n$s", level: LogLevel.Error);
|
.log("createOrder exception: $e\n$s", level: LogLevel.Error);
|
||||||
|
|
Loading…
Reference in a new issue