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

View file

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

View file

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