Really use euro as base currency when querying exchange rates, so we can use the configuration-free ECB provider

This should fixes issue #154.
This commit is contained in:
Jan Böhmer 2022-12-11 15:33:50 +01:00
parent 2b2fc6f1e6
commit 7235cb72b4
4 changed files with 29 additions and 7 deletions

View file

@ -1,4 +1,8 @@
florianv_swap:
cache:
ttl: 3600 # 1 hour
providers:
european_central_bank: ~ # European Central Bank (only works for EUR base currency)
fixer: # Fixer.io (needs an API key)

View file

@ -22,6 +22,7 @@ namespace App\Services;
use App\Entity\PriceInformations\Currency;
use Brick\Math\BigDecimal;
use Brick\Math\RoundingMode;
use Swap\Swap;
class ExchangeRateUpdater
@ -40,8 +41,15 @@ class ExchangeRateUpdater
*/
public function update(Currency $currency): Currency
{
$rate = $this->swap->latest($currency->getIsoCode().'/'.$this->base_currency);
$currency->setExchangeRate(BigDecimal::of($rate->getValue()));
//Currency pairs are always in the format "BASE/QUOTE"
$rate = $this->swap->latest($this->base_currency.'/'.$currency->getIsoCode());
//The rate says how many quote units are worth one base unit
//So we need to invert it to get the exchange rate
$rate_bd = BigDecimal::of($rate->getValue());
$rate_inverted = BigDecimal::one()->dividedBy($rate_bd, Currency::PRICE_SCALE, RoundingMode::HALF_UP);
$currency->setExchangeRate($rate_inverted);
return $currency;
}

View file

@ -18,15 +18,19 @@
{% endif %}
{{ form_row(form.exchange_rate) }}
{% if entity.inverseExchangeRate %}
<p class="form-text text-muted offset-3 col-9">
{{ '1'|format_currency(default_currency) }} = {{ entity.inverseExchangeRate.tofloat | format_currency(entity.isoCode, {fraction_digit: 5}) }}<br>
{{ '1'|format_currency(entity.isoCode) }} = {{ entity.exchangeRate.tofloat | format_currency(default_currency, {fraction_digit: 5}) }}
</p>
{% endif %}
{% if form.update_exchange_rate is defined %}
{{ form_row(form.update_exchange_rate, {attr: {class: 'btn-info'}}) }}
{% endif %}
{% if entity.inverseExchangeRate %}
<span class="form-text text-muted offset-3 col-9">
{{ '1'|format_currency(default_currency) }} = {{ entity.inverseExchangeRate.tofloat | format_currency(entity.isoCode, {fraction_digit: 5}) }}
</span>
{% endif %}
{% endblock %}
{% block edit_title %}

View file

@ -9941,5 +9941,11 @@ Element 3</target>
<target>Unable to retrieve the exchange rate. Check your exchange rate provider configuration.</target>
</segment>
</unit>
<unit id="E_M7mZ5" name="currency.edit.exchange_rate_updated.success">
<segment>
<source>currency.edit.exchange_rate_updated.success</source>
<target>Retrieved the exchange rate successfully.</target>
</segment>
</unit>
</file>
</xliff>