Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/pages/exchange_view/exchange_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import '../../models/isar/models/ethereum/eth_contract.dart';
import '../../pages_desktop_specific/desktop_exchange/exchange_steps/step_scaffold.dart';
import '../../providers/providers.dart';
import '../../services/exchange/change_now/change_now_exchange.dart';
import '../../services/exchange/cyphergoat/cyphergoat_exchange.dart';
import '../../services/exchange/exchange.dart';
import '../../services/exchange/exchange_data_loading_service.dart';
import '../../services/exchange/exchange_response.dart';
Expand Down Expand Up @@ -86,6 +87,7 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
TrocadorExchange.instance,
NanswapExchange.instance,
WizardSwapExchange.instance,
CypherGoatExchange.instance,
];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../models/exchange/aggregate_currency.dart';
import '../../../providers/providers.dart';
import '../../../services/exchange/change_now/change_now_exchange.dart';
import '../../../services/exchange/cyphergoat/cyphergoat_exchange.dart';
import '../../../services/exchange/exchange.dart';
import '../../../services/exchange/exolix/exolix_exchange.dart';
import '../../../services/exchange/nanswap/nanswap_exchange.dart';
Expand Down Expand Up @@ -103,6 +104,11 @@ class _ExchangeProviderOptionsState
sendCurrency: sendCurrency,
receiveCurrency: receivingCurrency,
);
final showCypherGoat = exchangeSupported(
exchangeName: CypherGoatExchange.exchangeName,
sendCurrency: sendCurrency,
receiveCurrency: receivingCurrency,
);

return RoundedWhiteContainer(
padding: isDesktop ? const EdgeInsets.all(0) : const EdgeInsets.all(12),
Expand All @@ -116,6 +122,7 @@ class _ExchangeProviderOptionsState
if (showTrocador) TrocadorExchange.instance,
if (showNanswap) NanswapExchange.instance,
if (showWizardSwap) WizardSwapExchange.instance,
if (showCypherGoat) CypherGoatExchange.instance,
],
fixedRate: widget.fixedRate,
reversed: widget.reversed,
Expand Down
224 changes: 224 additions & 0 deletions lib/services/exchange/cyphergoat/cyphergoat_api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
import 'dart:convert';

import '../../../app_config.dart';
import '../../../exceptions/exchange/exchange_exception.dart';
import '../../../external_api_keys.dart';
import '../../../networking/http.dart';
import '../../../utilities/logger.dart';
import '../../../utilities/prefs.dart';
import '../../tor_service.dart';
import '../exchange_response.dart';
import 'response_objects/cg_estimate.dart';
import 'response_objects/cg_transaction.dart';

const kCypherGoatSource = "stackwallet";

abstract class CypherGoatAPI {
static const String authority = "api.cyphergoat.com";

static const HTTP _client = HTTP();

static Uri _buildUri({
required String path,
Map<String, String>? params,
}) {
return Uri.https(authority, path, params);
}

static Future<dynamic> _makeGetRequest(Uri uri) async {
int code = -1;
try {
final headers = <String, String>{
"Content-Type": "application/json",
"Accept": "application/json",
};
if (kCypherGoatApiKey.isNotEmpty) {
headers["Authorization"] = "Bearer $kCypherGoatApiKey";
}

final response = await _client.get(
url: uri,
headers: headers,
proxyInfo: !AppConfig.hasFeature(AppFeature.tor)
? null
: Prefs.instance.useTor
? TorService.sharedInstance.getProxyInfo()
: null,
);

code = response.code;

final json = jsonDecode(response.body);

if (code != 200) {
final errMsg = (json is Map ? json["error"] : null) as String?;
throw Exception(errMsg ?? "HTTP $code: ${response.body}");
}

return json;
} catch (e, s) {
Logging.instance.e(
"CypherGoatAPI GET $uri HTTP:$code threw:",
error: e,
stackTrace: s,
);
rethrow;
}
}

/// GET /estimate
/// Returns all exchange provider estimates for the given pair and amount.
static Future<ExchangeResponse<({CgEstimatesResponse rates, double min})>>
getEstimate({
required String coin1,
required String network1,
required String coin2,
required String network2,
required String amount,
}) async {
final params = <String, String>{
"coin1": coin1.toLowerCase(),
"network1": network1.toLowerCase(),
"coin2": coin2.toLowerCase(),
"network2": network2.toLowerCase(),
"amount": amount,
"best": "false",
};

if (kCypherGoatApiKey.isNotEmpty) {
params["api_key"] = kCypherGoatApiKey;
}

final uri = _buildUri(path: "/estimate", params: params);

try {
final json = await _makeGetRequest(uri);
final map = Map<String, dynamic>.from(json as Map);

final ratesMap = map["rates"] as Map?;
if (ratesMap == null) {
throw Exception("Missing 'rates' in estimate response");
}

final rates = CgEstimatesResponse.fromMap(
Map<String, dynamic>.from(ratesMap),
);
final min = (map["min"] as num?)?.toDouble() ?? rates.min;

return ExchangeResponse(value: (rates: rates, min: min));
} catch (e, s) {
Logging.instance.e(
"CypherGoatAPI.getEstimate() exception:",
error: e,
stackTrace: s,
);
return ExchangeResponse(
exception: ExchangeException(
e.toString(),
ExchangeExceptionType.generic,
),
);
}
}

/// GET /swap
/// Creates a swap with the specified exchange partner.
static Future<ExchangeResponse<CgTransaction>> createSwap({
required String coin1,
required String network1,
required String coin2,
required String network2,
required String amount,
required String partner,
required String address,
String? estimateId,
}) async {
final params = <String, String>{
"coin1": coin1.toLowerCase(),
"network1": network1.toLowerCase(),
"coin2": coin2.toLowerCase(),
"network2": network2.toLowerCase(),
"amount": amount,
"partner": partner,
"address": address,
"source": kCypherGoatSource,
};

if (kCypherGoatAffiliate.isNotEmpty) {
params["affiliate"] = kCypherGoatAffiliate;
}
if (estimateId != null && estimateId.isNotEmpty) {
params["estimateid"] = estimateId;
}
if (kCypherGoatApiKey.isNotEmpty) {
params["api_key"] = kCypherGoatApiKey;
}

final uri = _buildUri(path: "/swap", params: params);

try {
final json = await _makeGetRequest(uri);
final map = Map<String, dynamic>.from(json as Map);

final txMap = map["transaction"] as Map?;
if (txMap == null) {
throw Exception("Missing 'transaction' in swap response");
}

return ExchangeResponse(
value: CgTransaction.fromMap(Map<String, dynamic>.from(txMap)),
);
} catch (e, s) {
Logging.instance.e(
"CypherGoatAPI.createSwap() exception:",
error: e,
stackTrace: s,
);
return ExchangeResponse(
exception: ExchangeException(
e.toString(),
ExchangeExceptionType.generic,
),
);
}
}

/// GET /transaction
/// Fetches transaction details by CGID.
static Future<ExchangeResponse<CgTransaction>> getTransaction({
required String cgid,
}) async {
final params = <String, String>{"id": cgid};
if (kCypherGoatApiKey.isNotEmpty) {
params["api_key"] = kCypherGoatApiKey;
}

final uri = _buildUri(path: "/transaction", params: params);

try {
final json = await _makeGetRequest(uri);
final map = Map<String, dynamic>.from(json as Map);

final txMap = map["transaction"] as Map?;
if (txMap == null) {
throw Exception("Missing 'transaction' in response");
}

return ExchangeResponse(
value: CgTransaction.fromMap(Map<String, dynamic>.from(txMap)),
);
} catch (e, s) {
Logging.instance.e(
"CypherGoatAPI.getTransaction($cgid) exception:",
error: e,
stackTrace: s,
);
return ExchangeResponse(
exception: ExchangeException(
e.toString(),
ExchangeExceptionType.generic,
),
);
}
}
}
Loading