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
|
2019-03-12 12:55:13 +00:00
|
|
|
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(',', '.');
|
|
|
|
}
|
2019-03-12 12:55:13 +00:00
|
|
|
|
|
|
|
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) => {
|
2019-03-12 12:55:13 +00:00
|
|
|
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(',', '.');
|
|
|
|
}
|
2019-03-12 12:55:13 +00:00
|
|
|
|
|
|
|
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 };
|