trocador api tweaks and clean up

This commit is contained in:
julian 2023-04-28 14:26:51 -06:00
parent 9c414068d3
commit da9b679d3c
3 changed files with 33 additions and 26 deletions

View file

@ -1,5 +1,4 @@
import 'package:decimal/decimal.dart';
import 'package:stackwallet/services/exchange/trocador/response_objects/trocador_quote.dart';
class TrocadorTrade {
final String tradeId;
@ -23,7 +22,10 @@ class TrocadorTrade {
final String refundAddressMemo;
final String password;
final String idProvider;
final List<TrocadorQuote> quotes;
// dynamic is the devil but this could be anything... because json.
final dynamic quotes;
final bool payment;
TrocadorTrade({
@ -53,10 +55,6 @@ class TrocadorTrade {
});
factory TrocadorTrade.fromMap(Map<String, dynamic> map) {
final list =
List<Map<String, dynamic>>.from(map['quotes']['quotes'] as List);
final quotes = list.map((quote) => TrocadorQuote.fromMap(quote)).toList();
return TrocadorTrade(
tradeId: map['trade_id'] as String,
date: DateTime.parse(map['date'] as String),
@ -79,14 +77,13 @@ class TrocadorTrade {
refundAddressMemo: map['refund_address_memo'] as String,
password: map['password'] as String,
idProvider: map['id_provider'] as String,
quotes: quotes,
quotes: map['quotes'],
payment: map['payment'] as bool,
);
}
@override
String toString() {
final quotesString = quotes.map((quote) => quote.toString()).join(', ');
return 'TrocadorTrade( '
'tradeId: $tradeId, '
'date: $date, '
@ -109,7 +106,7 @@ class TrocadorTrade {
'refundAddressMemo: $refundAddressMemo, '
'password: $password, '
'idProvider: $idProvider, '
'quotes: [ $quotesString ], '
'quotes: $quotes, '
'payment: $payment '
')';
}

View file

@ -14,8 +14,8 @@ class TrocadorTradeNew {
final String provider;
final bool fixed;
final String status;
final String? addressProvider;
final String? addressProviderMemo;
final String addressProvider;
final String addressProviderMemo;
final String addressUser;
final String addressUserMemo;
final String refundAddress;

View file

@ -34,7 +34,7 @@ abstract class TrocadorAPI {
static Future<dynamic> _makeGetRequest(Uri uri) async {
int code = -1;
try {
print("URI: $uri");
// print("URI: $uri");
final response = await http.get(
uri,
headers: {'Content-Type': 'application/json'},
@ -42,8 +42,8 @@ abstract class TrocadorAPI {
code = response.statusCode;
print("CODE: $code");
print("BODY: ${response.body}");
// print("CODE: $code");
// print("BODY: ${response.body}");
final json = jsonDecode(response.body);
@ -113,7 +113,7 @@ abstract class TrocadorAPI {
try {
final json = await _makeGetRequest(uri);
final map = Map<String, dynamic>.from(json as Map);
final map = Map<String, dynamic>.from((json as List).first as Map);
return ExchangeResponse(value: TrocadorTrade.fromMap(map));
} catch (e, s) {
@ -128,7 +128,7 @@ abstract class TrocadorAPI {
}
/// get standard/floating rate
static Future<ExchangeResponse<dynamic>> getNewStandardRate({
static Future<ExchangeResponse<TrocadorRate>> getNewStandardRate({
required bool isOnion,
required String fromTicker,
required String fromNetwork,
@ -139,11 +139,12 @@ abstract class TrocadorAPI {
final params = {
"api_key": kTrocadorApiKey,
"ref": kTrocadorRefCode,
"ticker_from": fromTicker,
"ticker_from": fromTicker.toLowerCase(),
"network_from": fromNetwork,
"ticker_to": toTicker,
"ticker_to": toTicker.toLowerCase(),
"network_to": toNetwork,
"amount_from": fromAmount,
"payment": "false",
"min_kycrating": minKYCRating,
"markup": markup,
};
@ -152,7 +153,7 @@ abstract class TrocadorAPI {
}
/// get fixed rate/payment rate
static Future<ExchangeResponse<dynamic>> getNewPaymentRate({
static Future<ExchangeResponse<TrocadorRate>> getNewPaymentRate({
required bool isOnion,
required String fromTicker,
required String fromNetwork,
@ -163,11 +164,12 @@ abstract class TrocadorAPI {
final params = {
"api_key": kTrocadorApiKey,
"ref": kTrocadorRefCode,
"ticker_from": fromTicker,
"ticker_from": fromTicker.toLowerCase(),
"network_from": fromNetwork,
"ticker_to": toTicker,
"ticker_to": toTicker.toLowerCase(),
"network_to": toNetwork,
"amount_to": toAmount,
"payment": "true",
"min_kycrating": minKYCRating,
"markup": markup,
};
@ -221,10 +223,9 @@ abstract class TrocadorAPI {
final Map<String, String> params = {
"api_key": kTrocadorApiKey,
"ref": kTrocadorRefCode,
"id": rateId ?? "",
"ticker_from": fromTicker,
"ticker_from": fromTicker.toLowerCase(),
"network_from": fromNetwork,
"ticker_to": toTicker,
"ticker_to": toTicker.toLowerCase(),
"network_to": toNetwork,
"amount_from": fromAmount,
"address": receivingAddress,
@ -238,11 +239,16 @@ abstract class TrocadorAPI {
"markup": markup,
};
if (rateId != null) {
params["id"] = rateId;
}
return await _getNewTrade(isOnion: isOnion, params: params);
}
static Future<ExchangeResponse<TrocadorTradeNew>> createNewPaymentRateTrade({
required bool isOnion,
required String? rateId,
required String fromTicker,
required String fromNetwork,
required String toTicker,
@ -258,9 +264,9 @@ abstract class TrocadorAPI {
final params = {
"api_key": kTrocadorApiKey,
"ref": kTrocadorRefCode,
"ticker_from": fromTicker,
"ticker_from": fromTicker.toLowerCase(),
"network_from": fromNetwork,
"ticker_to": toTicker,
"ticker_to": toTicker.toLowerCase(),
"network_to": toNetwork,
"amount_to": toAmount,
"address": receivingAddress,
@ -274,6 +280,10 @@ abstract class TrocadorAPI {
"markup": markup,
};
if (rateId != null) {
params["id"] = rateId;
}
return await _getNewTrade(isOnion: isOnion, params: params);
}