cake_wallet/lib/src/domain/common/fetch_price.dart

38 lines
1.1 KiB
Dart
Raw Normal View History

2020-01-04 19:31:52 +00:00
import 'dart:convert';
import 'package:http/http.dart';
import 'package:cake_wallet/src/domain/common/crypto_currency.dart';
import 'package:cake_wallet/src/domain/common/fiat_currency.dart';
import 'package:cake_wallet/src/domain/common/currency_formatter.dart';
const fiatApiAuthority = 'fiat-api.cakewallet.com';
const fiatApiPath = '/v1/rates';
Future<double> fetchPriceFor({CryptoCurrency crypto, FiatCurrency fiat}) async {
double price = 0.0;
try {
final fiatStringified = fiat.toString();
final uri =
Uri.https(fiatApiAuthority, fiatApiPath, {'convert': fiatStringified});
final response = await get(uri.toString());
if (response.statusCode != 200) {
return 0.0;
}
2020-01-08 12:26:34 +00:00
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
2020-01-10 16:11:14 +00:00
final data = responseJSON['data'] as List<dynamic>;
2020-01-04 19:31:52 +00:00
for (final item in data) {
if (item['symbol'] == cryptoToString(crypto)) {
2020-01-08 12:26:34 +00:00
price = item['quote'][fiatStringified]['price'] as double;
2020-01-04 19:31:52 +00:00
break;
}
}
return price;
} catch (e) {
return price;
}
}