Closed sharkdp closed 1 year ago
How exactly does numbat web version get the conversion rates? Do you manually update them regularly on a module file? Is there a GitHub CI that fetches it from somewhere and saves them to a module file?
Yeah, currencies are a bit special :smile:
In the CLI version, we fetch currency exchange rates from the European Central Bank (https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml). We do this asynchronousy, so as not to slow down the startup of numbat
. Once the request comes back, we parse the XML respone here:
and then update the exchange rate cache here:
Once that is done, we load the units::currencies
module asynchronously (while the Numbat CLI is already running):
The units::currencies
module defines units like so:
Here, the exchange_rate_USD() -> Scalar
function is a so-called foreign function that is not implemented in Numbat code, but in Rust:
For the web version, we do this in a similar way, except that we fetch that file from JavaScript:
and then pass it into the Rust code (in order to fill the cache, and store the proper values for functions like exchange_rate_USD()
). Here, we do not fetch the file from the ECB directly, because that results in a cross-origin (CORS) error. Instead, we fetch https://numbat.dev/ecb-exchange-rates.php, which is basically just a cached proxy of that ECB XML file:
How exactly does numbat web version get the conversion rates? Do you manually update them regularly on a module file? Is there a GitHub CI that fetches it from somewhere and saves them to a module file?
No, there is no need to do that fortunately. Exchange rates are always up to date (ECB updates them once per day and we fetch them at least once per day).
Note that this approach has some downsides, but I have some ideas on how to improve it. For more details, see https://github.com/sharkdp/numbat/issues/107
Just to clarify, suppose I have installed the .deb installer. Does it update the exchange rates automatically, or does it retain the values from when this version was compiled?
Does it update the exchange rates automatically
it does.
The way we do this for the CLI is not straightforward:
https://github.com/sharkdp/numbat/blob/c666cd7e94e4e962c9485a6997a989faf195634e/numbat-cli/src/main.rs#L163-L178
We can hopefully do something similar for the WASM version