Skip to content

Use currency codes as keys in ExchangeRateManager - #3040

Open
sohailshk wants to merge 1 commit into
lballabio:masterfrom
sohailshk:fix/exchangerate-manager-currency-codes
Open

sohailshk wants to merge 1 commit into
lballabio:masterfrom
sohailshk:fix/exchangerate-manager-currency-codes

Conversation

@sohailshk

Copy link
Copy Markdown
Contributor

ExchangeRateManager stores and looks up rates with a key built from the numeric codes of the two currencies (min*1000 + max). This breaks in two cases:

  • CNY and CNH have the same numeric code (156), so their rates end up in the same list. After adding USD/CNY at 7.1012 and USD/CNH at 7.1236, lookup(USD, CNY) returns the USD/CNH rate, and converting 100,000 USD with it gives 712,360 CNH instead of 710,120 CNY. With only USD/CNY stored, lookup(USD, CNH) returns the USD/CNY rate instead of failing.
  • The cryptocurrencies have numeric codes from 10000 upwards, so hashes(), which decodes the key with % 1000 and / 1000, never recognizes a rate involving them. As a result, a derived rate can't start from them: with BTC/USD and EUR/USD stored, lookup(EUR, BTC) works but lookup(BTC, EUR) fails with "no conversion available from BTC to EUR".

The key is now the pair of currency codes, in alphabetical order, and the smart lookup also uses the codes to avoid cycles. Only private members change.

The two new test cases in ExchangeRateTests fail on master as described above and pass with the change; the other exchange-rate and money tests and the full test suite pass.

Rates were stored and looked up by a key built from the numeric codes
of the two currencies. CNY and CNH share the numeric code 156, so their
rates were stored together and a lookup for one could return the latest
rate added for the other. Cryptocurrencies have numeric codes above 999,
which broke the check used by the smart lookup, so a derived rate
starting from them (e.g., BTC to EUR through USD) could not be found.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant