/// <summary> /// Performs an indirect conversion between two currencies /// </summary> /// <param name="sourceCurrencyCode">The source currency</param> /// <param name="targetCurrencyCode">The target currency</param> /// <param name="amount">The conversion amount</param> /// <param name="date">The conversion date</param> /// <returns>A <see cref="FXConversion"/> with the result of the conversion</returns> private async Task <ServiceResponse <FXConversion> > ConvertIndirect(string sourceCurrencyCode, string targetCurrencyCode, decimal amount, string date) { var response = new ServiceResponse <FXConversion>(); var sourceToBaseRecord = await _fxServiceBackend.GetFXData(sourceCurrencyCode, _fxServiceBackend.BaseCurrencyCode, date); if (sourceToBaseRecord == null) { return(response.Fail(FXErrorCode.RecordNotFoundForDate, $"No exchange rate was found for source currency '{sourceCurrencyCode}' and target currency '{_fxServiceBackend.BaseCurrencyCode}' on date '{date}'.")); } // Convert from source to base var sourceToBaseValue = amount * sourceToBaseRecord.ExchangeRate; var baseToTargetRecord = await _fxServiceBackend.GetFXData(_fxServiceBackend.BaseCurrencyCode, targetCurrencyCode, date); if (baseToTargetRecord == null) { return(response.Fail(FXErrorCode.RecordNotFoundForDate, $"No exchange rate was found for source currency '{_fxServiceBackend.BaseCurrencyCode}' and target currency '{targetCurrencyCode}' on date '{date}'.")); } // Convert from base to target var baseToTargetValue = sourceToBaseValue * baseToTargetRecord.ExchangeRate; // Derive the exchange rate decimal exchangeRate = Math.Round(baseToTargetValue / amount, 4); var conversion = new FXConversion { SourceCurrencyCode = sourceToBaseRecord.SourceCurrencyCode, TargetCurrencyCode = baseToTargetRecord.TargetCurrencyCode, Amount = amount, ExchangeRate = exchangeRate, Value = amount * exchangeRate, // we do not use baseToTargetValue here because we rounded the exchange rate after deriving baseToTargetValue Date = baseToTargetRecord.Date, Direct = false }; return(response.Succeed(conversion)); }
/// <summary> /// Performs a direct conversion between two currencies /// </summary> /// <param name="sourceCurrencyCode">The source currency</param> /// <param name="targetCurrencyCode">The target currency</param> /// <param name="amount">The conversion amount</param> /// <param name="date">The conversion date</param> /// <returns>A <see cref="FXConversion"/> with the result of the conversion</returns> private async Task <ServiceResponse <FXConversion> > ConvertDirect(string sourceCurrencyCode, string targetCurrencyCode, decimal amount, string date) { var response = new ServiceResponse <FXConversion>(); var fxRecord = await _fxServiceBackend.GetFXData(sourceCurrencyCode, targetCurrencyCode, date); if (fxRecord == null) { return(response.Fail(FXErrorCode.RecordNotFoundForDate, $"No exchange rate was found for source currency '{sourceCurrencyCode}' and target currency '{targetCurrencyCode}' on date '{date}'.")); } var conversion = new FXConversion { SourceCurrencyCode = fxRecord.SourceCurrencyCode, TargetCurrencyCode = fxRecord.TargetCurrencyCode, Amount = amount, ExchangeRate = fxRecord.ExchangeRate, Value = amount * fxRecord.ExchangeRate, Date = fxRecord.Date, Direct = true }; return(response.Succeed(conversion)); }