stack_wallet/test/services/change_now/change_now_test.dart

833 lines
31 KiB
Dart
Raw Normal View History

import 'dart:convert';
import 'package:decimal/decimal.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:stackwallet/exceptions/exchange/exchange_exception.dart';
import 'package:stackwallet/models/exchange/change_now/exchange_transaction.dart';
2022-08-30 20:26:43 +00:00
import 'package:stackwallet/models/exchange/change_now/exchange_transaction_status.dart';
import 'package:stackwallet/models/exchange/response_objects/estimate.dart';
import 'package:stackwallet/models/isar/exchange_cache/pair.dart';
2023-09-08 16:04:56 +00:00
import 'package:stackwallet/networking/http.dart';
2022-10-02 20:53:53 +00:00
import 'package:stackwallet/services/exchange/change_now/change_now_api.dart';
import 'change_now_sample_data.dart';
import 'change_now_test.mocks.dart';
2023-09-08 16:04:56 +00:00
@GenerateMocks([HTTP])
void main() {
group("getAvailableCurrencies", () {
test("getAvailableCurrencies succeeds without options", () async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse("https://api.ChangeNow.io/v1/currencies"),
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async =>
Response(utf8.encode(jsonEncode(availableCurrenciesJSON)), 200));
final result = await instance.getAvailableCurrencies();
expect(result.exception, null);
expect(result.value == null, false);
expect(result.value!.length, 538);
});
test("getAvailableCurrencies succeeds with active option", () async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse("https://api.ChangeNow.io/v1/currencies?active=true"),
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async => Response(
utf8.encode(jsonEncode(availableCurrenciesJSONActive)), 200));
final result = await instance.getAvailableCurrencies(active: true);
expect(result.exception, null);
expect(result.value == null, false);
expect(result.value!.length, 531);
});
test("getAvailableCurrencies succeeds with fixedRate option", () async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse("https://api.ChangeNow.io/v1/currencies?fixedRate=true"),
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async => Response(
utf8.encode(jsonEncode(availableCurrenciesJSONFixedRate)), 200));
final result = await instance.getAvailableCurrencies(fixedRate: true);
expect(result.exception, null);
expect(result.value == null, false);
expect(result.value!.length, 410);
});
test("getAvailableCurrencies succeeds with fixedRate and active options",
() async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/currencies?fixedRate=true&active=true"),
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async => Response(
utf8.encode(jsonEncode(availableCurrenciesJSONActiveFixedRate)),
200));
final result =
await instance.getAvailableCurrencies(active: true, fixedRate: true);
expect(result.exception, null);
expect(result.value == null, false);
expect(result.value!.length, 410);
});
test(
"getAvailableCurrencies fails with ChangeNowExceptionType.serializeResponseError",
() async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse("https://api.ChangeNow.io/v1/currencies"),
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async => Response(
utf8.encode('{"some unexpected": "but valid json data"}'), 200));
final result = await instance.getAvailableCurrencies();
2022-10-02 20:53:53 +00:00
expect(
result.exception!.type, ExchangeExceptionType.serializeResponseError);
expect(result.value == null, true);
});
test("getAvailableCurrencies fails for any other reason", () async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse("https://api.ChangeNow.io/v1/currencies"),
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async => Response(utf8.encode(""), 400));
final result = await instance.getAvailableCurrencies();
2023-02-21 22:40:32 +00:00
expect(
result.exception!.type, ExchangeExceptionType.serializeResponseError);
expect(result.value == null, true);
});
});
group("getPairedCurrencies", () {
test("getPairedCurrencies succeeds without fixedRate option", () async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse("https://api.ChangeNow.io/v1/currencies-to/XMR"),
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async =>
Response(utf8.encode(jsonEncode(getPairedCurrenciesJSON)), 200));
final result = await instance.getPairedCurrencies(ticker: "XMR");
expect(result.exception, null);
expect(result.value == null, false);
expect(result.value!.length, 537);
});
test("getPairedCurrencies succeeds with fixedRate option", () async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/currencies-to/XMR?fixedRate=true"),
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async => Response(
utf8.encode(jsonEncode(getPairedCurrenciesJSONFixedRate)), 200));
final result =
await instance.getPairedCurrencies(ticker: "XMR", fixedRate: true);
expect(result.exception, null);
expect(result.value == null, false);
expect(result.value!.length, 410);
});
test(
2022-08-30 20:26:43 +00:00
"getPairedCurrencies fails with ChangeNowExceptionType.serializeResponseError A",
() async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse("https://api.ChangeNow.io/v1/currencies-to/XMR"),
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async => Response(
utf8.encode('[{"some unexpected": "but valid json data"}]'), 200));
final result = await instance.getPairedCurrencies(ticker: "XMR");
2022-10-02 20:53:53 +00:00
expect(
result.exception!.type, ExchangeExceptionType.serializeResponseError);
expect(result.value == null, true);
});
test("getPairedCurrencies fails for any other reason", () async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse("https://api.ChangeNow.io/v1/currencies"),
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async => Response(utf8.encode(""), 400));
final result =
await instance.getPairedCurrencies(ticker: "XMR", fixedRate: true);
2022-10-02 20:53:53 +00:00
expect(result.exception!.type, ExchangeExceptionType.generic);
expect(result.value == null, true);
});
});
group("getMinimalExchangeAmount", () {
test("getMinimalExchangeAmount succeeds", () async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/min-amount/xmr_btc?api_key=testAPIKEY"),
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async =>
Response(utf8.encode('{"minAmount": 42}'), 200));
final result = await instance.getMinimalExchangeAmount(
fromTicker: "xmr",
toTicker: "btc",
apiKey: "testAPIKEY",
);
expect(result.exception, null);
expect(result.value == null, false);
expect(result.value, Decimal.fromInt(42));
});
test(
"getMinimalExchangeAmount fails with ChangeNowExceptionType.serializeResponseError",
() async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/min-amount/xmr_btc?api_key=testAPIKEY"),
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async =>
Response(utf8.encode('{"error": 42}'), 200));
final result = await instance.getMinimalExchangeAmount(
fromTicker: "xmr",
toTicker: "btc",
apiKey: "testAPIKEY",
);
2022-10-02 20:53:53 +00:00
expect(
result.exception!.type, ExchangeExceptionType.serializeResponseError);
expect(result.value == null, true);
});
test("getMinimalExchangeAmount fails for any other reason", () async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/min-amount/xmr_btc?api_key=testAPIKEY"),
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async => Response(utf8.encode(''), 400));
final result = await instance.getMinimalExchangeAmount(
fromTicker: "xmr",
toTicker: "btc",
apiKey: "testAPIKEY",
);
2023-02-21 22:40:32 +00:00
expect(
result.exception!.type, ExchangeExceptionType.serializeResponseError);
expect(result.value == null, true);
});
});
2022-08-30 20:26:43 +00:00
group("getEstimatedExchangeAmount", () {
test("getEstimatedExchangeAmount succeeds", () async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
2022-08-30 20:26:43 +00:00
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/exchange-amount/42/xmr_btc?api_key=testAPIKEY"),
2022-08-30 20:26:43 +00:00
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
2022-08-30 20:26:43 +00:00
)).thenAnswer((realInvocation) async => Response(
utf8.encode(
'{"estimatedAmount": 58.4142873, "transactionSpeedForecast": "10-60", "warningMessage": null}'),
2022-08-30 20:26:43 +00:00
200));
final result = await instance.getEstimatedExchangeAmount(
2022-08-30 20:26:43 +00:00
fromTicker: "xmr",
toTicker: "btc",
fromAmount: Decimal.fromInt(42),
apiKey: "testAPIKEY",
);
expect(result.exception, null);
expect(result.value == null, false);
expect(result.value, isA<Estimate>());
2022-08-30 20:26:43 +00:00
});
test(
"getEstimatedExchangeAmount fails with ChangeNowExceptionType.serializeResponseError",
() async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
2022-08-30 20:26:43 +00:00
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/exchange-amount/42/xmr_btc?api_key=testAPIKEY"),
2022-08-30 20:26:43 +00:00
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async =>
Response(utf8.encode('{"error": 42}'), 200));
2022-08-30 20:26:43 +00:00
final result = await instance.getEstimatedExchangeAmount(
2022-08-30 20:26:43 +00:00
fromTicker: "xmr",
toTicker: "btc",
fromAmount: Decimal.fromInt(42),
apiKey: "testAPIKEY",
);
2022-10-02 20:53:53 +00:00
expect(
result.exception!.type, ExchangeExceptionType.serializeResponseError);
2022-08-30 20:26:43 +00:00
expect(result.value == null, true);
});
test("getEstimatedExchangeAmount fails for any other reason", () async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
2022-08-30 20:26:43 +00:00
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/exchange-amount/42/xmr_btc?api_key=testAPIKEY"),
2022-08-30 20:26:43 +00:00
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async => Response(utf8.encode(''), 400));
2022-08-30 20:26:43 +00:00
final result = await instance.getEstimatedExchangeAmount(
2022-08-30 20:26:43 +00:00
fromTicker: "xmr",
toTicker: "btc",
fromAmount: Decimal.fromInt(42),
apiKey: "testAPIKEY",
);
2022-10-02 20:53:53 +00:00
expect(result.exception!.type, ExchangeExceptionType.generic);
2022-08-30 20:26:43 +00:00
expect(result.value == null, true);
});
});
2022-09-08 17:10:53 +00:00
// group("getEstimatedFixedRateExchangeAmount", () {
// test("getEstimatedFixedRateExchangeAmount succeeds", () async {
2023-09-08 16:04:56 +00:00
// final client = MockHTTP();
2022-09-08 17:10:53 +00:00
// ChangeNow.instance.client = client;
//
2023-09-08 16:04:56 +00:00
// when(client.get(url:
2022-09-08 17:10:53 +00:00
// Uri.parse(
// "https://api.ChangeNow.io/v1/exchange-amount/fixed-rate/10/xmr_btc?api_key=testAPIKEY&useRateId=true"),
// headers: {'Content-Type': 'application/json'},
// proxyInfo: null,
2022-09-08 17:10:53 +00:00
// )).thenAnswer((realInvocation) async =>
// Response(utf8.encode(jsonEncode(estFixedRateExchangeAmountJSON )), 200));
2022-09-08 17:10:53 +00:00
//
// final result =
// await ChangeNow.instance.getEstimatedFixedRateExchangeAmount(
// fromTicker: "xmr",
// toTicker: "btc",
// fromAmount: Decimal.fromInt(10),
// apiKey: "testAPIKEY",
// );
//
// expect(result.exception, null);
// expect(result.value == null, false);
// expect(result.value.toString(),
// 'EstimatedExchangeAmount: {estimatedAmount: 0.07271053, transactionSpeedForecast: 10-60, warningMessage: null, rateId: 1t2W5KBPqhycSJVYpaNZzYWLfMr0kSFe, networkFee: 0.00002408}');
// });
//
// test(
// "getEstimatedFixedRateExchangeAmount fails with ChangeNowExceptionType.serializeResponseError",
// () async {
2023-09-08 16:04:56 +00:00
// final client = MockHTTP();
2022-09-08 17:10:53 +00:00
// ChangeNow.instance.client = client;
//
2023-09-08 16:04:56 +00:00
// when(client.get(url:
2022-09-08 17:10:53 +00:00
// Uri.parse(
// "https://api.ChangeNow.io/v1/exchange-amount/fixed-rate/10/xmr_btc?api_key=testAPIKEY&useRateId=true"),
// headers: {'Content-Type': 'application/json'},
// proxyInfo: null,
2022-09-08 17:10:53 +00:00
// )).thenAnswer((realInvocation) async => Response('{"error": 42}', 200));
//
// final result =
// await ChangeNow.instance.getEstimatedFixedRateExchangeAmount(
// fromTicker: "xmr",
// toTicker: "btc",
// fromAmount: Decimal.fromInt(10),
// apiKey: "testAPIKEY",
// );
//
// expect(result.exception!.type,
// ChangeNowExceptionType.serializeResponseError);
// expect(result.value == null, true);
// });
//
// test("getEstimatedFixedRateExchangeAmount fails for any other reason",
// () async {
2023-09-08 16:04:56 +00:00
// final client = MockHTTP();
2022-09-08 17:10:53 +00:00
// ChangeNow.instance.client = client;
//
2023-09-08 16:04:56 +00:00
// when(client.get(url:
2022-09-08 17:10:53 +00:00
// Uri.parse(
// "https://api.ChangeNow.io/v1/exchange-amount/fixed-rate/10/xmr_btc?api_key=testAPIKEY&useRateId=true"),
// headers: {'Content-Type': 'application/json'},
// proxyInfo: null,
2022-09-08 17:10:53 +00:00
// )).thenAnswer((realInvocation) async => Response('', 400));
//
// final result =
// await ChangeNow.instance.getEstimatedFixedRateExchangeAmount(
// fromTicker: "xmr",
// toTicker: "btc",
// fromAmount: Decimal.fromInt(10),
// apiKey: "testAPIKEY",
// );
//
// expect(result.exception!.type, ChangeNowExceptionType.generic);
// expect(result.value == null, true);
// });
// });
group("getAvailableFixedRateMarkets", () {
test("getAvailableFixedRateMarkets succeeds", () async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/market-info/fixed-rate/testAPIKEY"),
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async =>
Response(utf8.encode(jsonEncode(fixedRateMarketsJSON)), 200));
final result = await instance.getAvailableFixedRateMarkets(
apiKey: "testAPIKEY",
);
expect(result.exception, null);
expect(result.value == null, false);
expect(result.value!.length, 237);
});
test(
"getAvailableFixedRateMarkets fails with ChangeNowExceptionType.serializeResponseError",
() async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/market-info/fixed-rate/testAPIKEY"),
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async =>
Response(utf8.encode('{"error": 42}'), 200));
final result = await instance.getAvailableFixedRateMarkets(
apiKey: "testAPIKEY",
);
2022-10-02 20:53:53 +00:00
expect(
result.exception!.type, ExchangeExceptionType.serializeResponseError);
expect(result.value == null, true);
});
test("getAvailableFixedRateMarkets fails for any other reason", () async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/market-info/fixed-rate/testAPIKEY"),
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async => Response(utf8.encode(''), 400));
final result = await instance.getAvailableFixedRateMarkets(
apiKey: "testAPIKEY",
);
2023-02-21 22:40:32 +00:00
expect(
result.exception!.type, ExchangeExceptionType.serializeResponseError);
expect(result.value == null, true);
});
});
group("createStandardExchangeTransaction", () {
test("createStandardExchangeTransaction succeeds", () async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
when(client.post(
2023-09-08 16:04:56 +00:00
url: Uri.parse("https://api.ChangeNow.io/v1/transactions/testAPIKEY"),
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
body:
'{"from":"xmr","to":"btc","address":"bc1qu58svs9983e2vuyqh7gq7ratf8k5qehz5k0cn5","amount":"0.3","flow":"standard","extraId":"","userId":"","contactEmail":"","refundAddress":"888tNkZrPN6JsEgekjMnABU4TBzc2Dt29EPAvkRxbANsAnjyPbb3iQ1YBRk1UXcdRsiKc9dhwMVgN5S9cQUiyoogDavup3H","refundExtraId":""}',
encoding: null,
)).thenAnswer((realInvocation) async => Response(
utf8.encode(jsonEncode(createStandardTransactionResponse)), 200));
final result = await instance.createStandardExchangeTransaction(
fromTicker: "xmr",
toTicker: "btc",
receivingAddress: "bc1qu58svs9983e2vuyqh7gq7ratf8k5qehz5k0cn5",
amount: Decimal.parse("0.3"),
refundAddress:
"888tNkZrPN6JsEgekjMnABU4TBzc2Dt29EPAvkRxbANsAnjyPbb3iQ1YBRk1UXcdRsiKc9dhwMVgN5S9cQUiyoogDavup3H",
apiKey: "testAPIKEY",
);
expect(result.exception, null);
expect(result.value == null, false);
expect(result.value, isA<ExchangeTransaction>());
});
test(
"createStandardExchangeTransaction fails with ChangeNowExceptionType.serializeResponseError",
() async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
when(client.post(
2023-09-08 16:04:56 +00:00
url: Uri.parse("https://api.ChangeNow.io/v1/transactions/testAPIKEY"),
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
body:
'{"from":"xmr","to":"btc","address":"bc1qu58svs9983e2vuyqh7gq7ratf8k5qehz5k0cn5","amount":"0.3","flow":"standard","extraId":"","userId":"","contactEmail":"","refundAddress":"888tNkZrPN6JsEgekjMnABU4TBzc2Dt29EPAvkRxbANsAnjyPbb3iQ1YBRk1UXcdRsiKc9dhwMVgN5S9cQUiyoogDavup3H","refundExtraId":""}',
encoding: null,
)).thenAnswer((realInvocation) async =>
Response(utf8.encode('{"error": 42}'), 200));
final result = await instance.createStandardExchangeTransaction(
fromTicker: "xmr",
toTicker: "btc",
receivingAddress: "bc1qu58svs9983e2vuyqh7gq7ratf8k5qehz5k0cn5",
amount: Decimal.parse("0.3"),
refundAddress:
"888tNkZrPN6JsEgekjMnABU4TBzc2Dt29EPAvkRxbANsAnjyPbb3iQ1YBRk1UXcdRsiKc9dhwMVgN5S9cQUiyoogDavup3H",
apiKey: "testAPIKEY",
);
2022-10-02 20:53:53 +00:00
expect(
result.exception!.type, ExchangeExceptionType.serializeResponseError);
expect(result.value == null, true);
});
test("createStandardExchangeTransaction fails for any other reason",
() async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
when(client.post(
2023-09-08 16:04:56 +00:00
url: Uri.parse("https://api.ChangeNow.io/v1/transactions/testAPIKEY"),
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
body:
'{"from":"xmr","to":"btc","address":"bc1qu58svs9983e2vuyqh7gq7ratf8k5qehz5k0cn5","amount":"0.3","flow":"standard","extraId":"","userId":"","contactEmail":"","refundAddress":"888tNkZrPN6JsEgekjMnABU4TBzc2Dt29EPAvkRxbANsAnjyPbb3iQ1YBRk1UXcdRsiKc9dhwMVgN5S9cQUiyoogDavup3H","refundExtraId":""}',
encoding: null,
)).thenAnswer((realInvocation) async => Response(utf8.encode(''), 400));
final result = await instance.createStandardExchangeTransaction(
fromTicker: "xmr",
toTicker: "btc",
receivingAddress: "bc1qu58svs9983e2vuyqh7gq7ratf8k5qehz5k0cn5",
amount: Decimal.parse("0.3"),
refundAddress:
"888tNkZrPN6JsEgekjMnABU4TBzc2Dt29EPAvkRxbANsAnjyPbb3iQ1YBRk1UXcdRsiKc9dhwMVgN5S9cQUiyoogDavup3H",
apiKey: "testAPIKEY",
);
2022-10-02 20:53:53 +00:00
expect(result.exception!.type, ExchangeExceptionType.generic);
expect(result.value == null, true);
});
});
2022-08-30 20:26:43 +00:00
group("createFixedRateExchangeTransaction", () {
test("createFixedRateExchangeTransaction succeeds", () async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
2022-08-30 20:26:43 +00:00
when(client.post(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/transactions/fixed-rate/testAPIKEY"),
2022-08-30 20:26:43 +00:00
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
2022-08-30 20:26:43 +00:00
body:
'{"from":"btc","to":"eth","address":"0x57f31ad4b64095347F87eDB1675566DAfF5EC886","flow":"fixed-rate","extraId":"","userId":"","contactEmail":"","refundAddress":"","refundExtraId":"","rateId":"","amount":"0.3"}',
2022-08-30 20:26:43 +00:00
encoding: null,
)).thenAnswer((realInvocation) async => Response(
utf8.encode(
'{"payinAddress": "33eFX2jfeWbXMSmRe9ewUUTrmSVSxZi5cj", "payoutAddress":'
' "0x57f31ad4b64095347F87eDB1675566DAfF5EC886","payoutExtraId": "",'
' "fromCurrency": "btc", "toCurrency": "eth", "refundAddress": "",'
'"refundExtraId": "","validUntil": "2019-09-09T14:01:04.921Z","id":'
' "a5c73e2603f40d","amount": 62.9737711}'),
2022-08-30 20:26:43 +00:00
200));
final result = await instance.createFixedRateExchangeTransaction(
2022-08-30 20:26:43 +00:00
fromTicker: "btc",
toTicker: "eth",
receivingAddress: "0x57f31ad4b64095347F87eDB1675566DAfF5EC886",
amount: Decimal.parse("0.3"),
refundAddress: "",
apiKey: "testAPIKEY",
rateId: '',
2022-10-04 00:59:49 +00:00
reversed: false,
2022-08-30 20:26:43 +00:00
);
expect(result.exception, null);
expect(result.value == null, false);
expect(result.value, isA<ExchangeTransaction>());
});
test(
"createFixedRateExchangeTransaction fails with ChangeNowExceptionType.serializeResponseError",
() async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
2022-08-30 20:26:43 +00:00
when(client.post(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/transactions/fixed-rate/testAPIKEY"),
2022-08-30 20:26:43 +00:00
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
2022-08-30 20:26:43 +00:00
body:
'{"from":"btc","to":"eth","address":"0x57f31ad4b64095347F87eDB1675566DAfF5EC886","amount":"0.3","flow":"fixed-rate","extraId":"","userId":"","contactEmail":"","refundAddress":"","refundExtraId":"","rateId":""}',
encoding: null,
)).thenAnswer((realInvocation) async => Response(
utf8.encode('{"id": "a5c73e2603f40d","amount": 62.9737711}'), 200));
2022-08-30 20:26:43 +00:00
final result = await instance.createFixedRateExchangeTransaction(
2022-08-30 20:26:43 +00:00
fromTicker: "btc",
toTicker: "eth",
receivingAddress: "0x57f31ad4b64095347F87eDB1675566DAfF5EC886",
amount: Decimal.parse("0.3"),
refundAddress: "",
apiKey: "testAPIKEY",
rateId: '',
2022-10-04 00:59:49 +00:00
reversed: false,
2022-08-30 20:26:43 +00:00
);
expect(result.exception!.type, ExchangeExceptionType.generic);
2022-08-30 20:26:43 +00:00
expect(result.value == null, true);
});
test("createFixedRateExchangeTransaction fails for any other reason",
() async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
2022-08-30 20:26:43 +00:00
when(client.post(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/transactions/fixed-rate/testAPIKEY"),
2022-08-30 20:26:43 +00:00
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
2022-08-30 20:26:43 +00:00
body:
'{"from": "btc","to": "eth","address": "0x57f31ad4b64095347F87eDB1675566DAfF5EC886", "amount": "1.12345","extraId": "", "userId": "","contactEmail": "","refundAddress": "", "refundExtraId": "", "rateId": "" }',
encoding: null,
)).thenAnswer((realInvocation) async => Response(utf8.encode(''), 400));
2022-08-30 20:26:43 +00:00
final result = await instance.createFixedRateExchangeTransaction(
2022-08-30 20:26:43 +00:00
fromTicker: "xmr",
toTicker: "btc",
receivingAddress: "bc1qu58svs9983e2vuyqh7gq7ratf8k5qehz5k0cn5",
amount: Decimal.parse("0.3"),
refundAddress:
"888tNkZrPN6JsEgekjMnABU4TBzc2Dt29EPAvkRxbANsAnjyPbb3iQ1YBRk1UXcdRsiKc9dhwMVgN5S9cQUiyoogDavup3H",
apiKey: "testAPIKEY",
rateId: '',
2022-10-04 00:59:49 +00:00
reversed: false,
2022-08-30 20:26:43 +00:00
);
2022-10-02 20:53:53 +00:00
expect(result.exception!.type, ExchangeExceptionType.generic);
2022-08-30 20:26:43 +00:00
expect(result.value == null, true);
});
});
group("getTransactionStatus", () {
test("getTransactionStatus succeeds", () async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
2022-08-30 20:26:43 +00:00
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/transactions/47F87eDB1675566DAfF5EC886/testAPIKEY"),
2022-08-30 20:26:43 +00:00
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
2022-08-30 20:26:43 +00:00
)).thenAnswer((realInvocation) async => Response(
utf8.encode(
'{"status": "waiting", "payinAddress": "32Ge2ci26rj1sRGw2NjiQa9L7Xvxtgzhrj", '
'"payoutAddress": "0x57f31ad4b64095347F87eDB1675566DAfF5EC886", '
'"fromCurrency": "btc", "toCurrency": "eth", "id": "50727663e5d9a4", '
'"updatedAt": "2019-08-22T14:47:49.943Z", "expectedSendAmount": 1, '
'"expectedReceiveAmount": 52.31667, "createdAt": "2019-08-22T14:47:49.943Z",'
' "isPartner": false}'),
2022-08-30 20:26:43 +00:00
200));
final result = await instance.getTransactionStatus(
2022-08-30 20:26:43 +00:00
id: "47F87eDB1675566DAfF5EC886",
apiKey: "testAPIKEY",
);
expect(result.exception, null);
expect(result.value == null, false);
expect(result.value, isA<ExchangeTransactionStatus>());
});
test(
"getTransactionStatus fails with ChangeNowExceptionType.serializeResponseError",
() async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
2022-08-30 20:26:43 +00:00
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/transactions/47F87eDB1675566DAfF5EC886/testAPIKEY"),
2022-08-30 20:26:43 +00:00
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async =>
Response(utf8.encode('{"error": 42}'), 200));
2022-08-30 20:26:43 +00:00
final result = await instance.getTransactionStatus(
2022-08-30 20:26:43 +00:00
id: "47F87eDB1675566DAfF5EC886",
apiKey: "testAPIKEY",
);
2022-10-02 20:53:53 +00:00
expect(
result.exception!.type, ExchangeExceptionType.serializeResponseError);
2022-08-30 20:26:43 +00:00
expect(result.value == null, true);
});
test("getTransactionStatus fails for any other reason", () async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
2022-08-30 20:26:43 +00:00
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/transactions/47F87eDB1675566DAfF5EC886/testAPIKEY"),
2022-08-30 20:26:43 +00:00
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async => Response(utf8.encode(''), 400));
2022-08-30 20:26:43 +00:00
final result = await instance.getTransactionStatus(
2022-08-30 20:26:43 +00:00
id: "47F87eDB1675566DAfF5EC886",
apiKey: "testAPIKEY",
);
2023-02-21 22:40:32 +00:00
expect(
result.exception!.type, ExchangeExceptionType.serializeResponseError);
2022-08-30 20:26:43 +00:00
expect(result.value == null, true);
});
});
group("getAvailableFloatingRatePairs", () {
test("getAvailableFloatingRatePairs succeeds", () async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
2022-08-30 20:26:43 +00:00
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/market-info/available-pairs?includePartners=false"),
2022-08-30 20:26:43 +00:00
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async => Response(
utf8.encode('["btc_xmr","btc_firo","btc_doge","eth_ltc"]'), 200));
2022-08-30 20:26:43 +00:00
final result = await instance.getAvailableFloatingRatePairs();
2022-08-30 20:26:43 +00:00
expect(result.exception, null);
expect(result.value == null, false);
2022-10-03 15:41:53 +00:00
expect(result.value, isA<List<Pair>>());
2022-08-30 20:26:43 +00:00
});
test(
"getAvailableFloatingRatePairs fails with ChangeNowExceptionType.serializeResponseError",
() async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
2022-08-30 20:26:43 +00:00
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/market-info/available-pairs?includePartners=false"),
2022-08-30 20:26:43 +00:00
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async =>
Response(utf8.encode('{"error": 42}'), 200));
2022-08-30 20:26:43 +00:00
final result = await instance.getAvailableFloatingRatePairs();
2022-08-30 20:26:43 +00:00
2022-10-02 20:53:53 +00:00
expect(
result.exception!.type, ExchangeExceptionType.serializeResponseError);
2022-08-30 20:26:43 +00:00
expect(result.value == null, true);
});
test("getAvailableFloatingRatePairs fails for any other reason", () async {
2023-09-08 16:04:56 +00:00
final client = MockHTTP();
final instance = ChangeNowAPI(http: client);
2022-08-30 20:26:43 +00:00
when(client.get(
2023-09-08 16:04:56 +00:00
url: Uri.parse(
2022-08-31 18:50:02 +00:00
"https://api.ChangeNow.io/v1/market-info/available-pairs?includePartners=false"),
2022-08-30 20:26:43 +00:00
headers: {'Content-Type': 'application/json'},
proxyInfo: null,
)).thenAnswer((realInvocation) async => Response(utf8.encode(''), 400));
2022-08-30 20:26:43 +00:00
final result = await instance.getAvailableFloatingRatePairs();
2022-08-30 20:26:43 +00:00
2023-02-21 22:40:32 +00:00
expect(
result.exception!.type, ExchangeExceptionType.serializeResponseError);
2022-08-30 20:26:43 +00:00
expect(result.value == null, true);
});
});
}