Skip to content

Commit 7a5c58b

Browse files
committed
fiat: fix change rate. Handle failure updates
1 parent 6b631a5 commit 7a5c58b

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/at/fiat.cc

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,31 +86,33 @@ void Fiat::_update()
8686
}
8787
}
8888

89-
// rate returns the exchange rate of the fiat pair.
90-
// eg. pair(eur,usd) -> amount of usd to buy/sell eur
89+
// rate returns the exchange rate of the fiat pair
9190
double Fiat::rate(const currency_pair_t &pair)
9291
{
93-
_update();
92+
try {
93+
_update();
94+
}
95+
catch (...) {
96+
// if here _eur_to_currency_rate was aready filled by
97+
// the constructor, hence let's use the old values
98+
}
99+
94100
std::string base, quote;
95101
base = pair.first;
96102
quote = pair.second;
97103

98104
toupper(base);
99105
toupper(quote);
100106

101-
// First convert eveything to EUR quotation, than change from
102-
// base to quote
103107
if (base == "EUR") {
104-
return _eur_to_currency_rate[quote];
108+
return 1. / _eur_to_currency_rate[quote];
105109
}
106110

107111
if (quote == "EUR") {
108-
return 1. / _eur_to_currency_rate[base];
112+
return _eur_to_currency_rate[base];
109113
}
110114

111-
// (1 / number of base for eur) * (number of quote for eur)
112-
// number of quote for base
113-
return _eur_to_currency_rate[quote] / _eur_to_currency_rate[base];
115+
return _eur_to_currency_rate[base] / _eur_to_currency_rate[quote];
114116
}
115117

116118
} // namespace at

0 commit comments

Comments
 (0)