setExchangeRates method

Future<void> setExchangeRates()

Implementation

Future<void> setExchangeRates() async {
  final accountCode = state.accountCode!;
  final counterCode = state.counterCode!;
  final baseCode = state.baseCode!;
  final pipPrecision = state.pipPrecision;
  final tradingPairQuoteFuture = exchangeProvider?.rates(
    baseCode,
    counterCode,
  );

  // FIXME: workaround,
  // we can't properly set null values on the calculator state.
  if (pipPrecision == null || pipPrecision == intMaxValue) {
    final pairMetadata = await fetchPairMetadata();

    if (pairMetadata != null) {
      patchState(
        MatexBaseCoreState(pipPrecision: pairMetadata.pip.precision),
      );
    } else {
      patchState(MatexBaseCoreState(
        pipPrecision: MatexPairPipMetadata.defaultMetatada().precision,
      ));
    }
  }

  if (tradingPairQuoteFuture != null) {
    final tradingPairQuote = await tradingPairQuoteFuture;
    exchangeRateLastUpdateAt(tradingPairQuote.timestamp);
    tradingPairExchangeRate(tradingPairQuote.price);
    counterAccountCurrencyPairExchangeRate(0);
    baseListedSecond(false);

    if (accountCode == counterCode) {
      baseListedSecond(true);
    } else {
      final accountBaseQuote = await exchangeProvider!.rates(
        counterCode,
        accountCode,
      );

      counterAccountCurrencyPairExchangeRate(accountBaseQuote.price);
    }
  }
}