currentPrice method

  1. @override
Future<Result<Price, String>> currentPrice({
  1. String from = 'ada',
  2. String to = 'usd',
})
override

retrieve current ratio of to currency to from currency. example: priceService(from:'BTC', to:'USD') -> 55234.654

Implementation

@override
Future<Result<Price, String>> currentPrice(
    {String from = 'ada', String to = 'usd'}) async {
  final fromId = await _toId(from);
  if (fromId == null) {
    return Err("can't convert symbol($from) to ID");
  }
  final CoinGeckoResult<List<PricedCoin>> list =
      await coingecko.simplePrice(ids: [fromId], vs_currencies: [to]);
  if (list.isError) {
    return Err(list.errorMessage);
  } else if (list.data.isEmpty || list.data.first.data[to] == null) {
    return Err("no data");
  } else {
    PricedCoin pricedCoin = list.data.first;
    final timestamp = DateTime.now()
        .millisecondsSinceEpoch; // pricedCoin.lastUpdatedAtTimeStamp.millisecondsSinceEpoch;
    Map<String, double> pair = pricedCoin.data;
    return Ok(Price(
        fromTicker: from,
        toTicker: to,
        timestamp: timestamp,
        value: pair[to]!));
  }
}