1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-12-03 13:48:15 +00:00
trezor-wallet/src/utils/fiatConverter.js

45 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-03-11 17:37:19 +00:00
import BigNumber from 'bignumber.js';
2019-03-12 11:55:21 +00:00
const toFiatCurrency = (amount, fiatCurrency, networkRates) => {
2019-03-11 16:53:28 +00:00
// calculate amount in local currency
if (!networkRates || !networkRates.rates || !amount) {
2019-03-12 11:55:21 +00:00
return '';
}
const rate = networkRates.rates[fiatCurrency];
if (!rate) {
return '';
}
2019-03-11 16:53:28 +00:00
2019-03-13 16:01:24 +00:00
let formattedAmount = amount;
if (typeof amount === 'string') {
formattedAmount = amount.replace(',', '.');
}
let localAmount = BigNumber(formattedAmount).times(rate);
2019-03-11 17:37:19 +00:00
localAmount = localAmount.isNaN() ? '' : localAmount.toFixed(2);
2019-03-11 16:53:28 +00:00
return localAmount;
};
2019-03-12 11:55:21 +00:00
const fromFiatCurrency = (localAmount, fiatCurrency, networkRates, decimals) => {
if (!networkRates || !networkRates.rates || !localAmount) {
2019-03-12 11:55:21 +00:00
return '';
}
const rate = networkRates.rates[fiatCurrency];
if (!rate) {
return '';
}
2019-03-11 16:53:28 +00:00
2019-03-13 16:01:24 +00:00
let formattedLocalAmount = localAmount;
if (typeof localAmount === 'string') {
formattedLocalAmount = localAmount.replace(',', '.');
}
let amount = BigNumber(formattedLocalAmount).div(rate);
2019-03-11 17:37:19 +00:00
amount = amount.isNaN() ? '' : amount.toFixed(decimals);
2019-03-11 16:53:28 +00:00
return amount;
};
export { toFiatCurrency, fromFiatCurrency };