From c20d2c6c6ff494fe244f52c4d61a440b57d19d29 Mon Sep 17 00:00:00 2001 From: Ana G <40721795+Bitcoinera@users.noreply.github.com> Date: Wed, 19 Feb 2020 21:44:11 +0100 Subject: [PATCH] emoney fixes for fiatvalue (#345) * emoney fixes for fiatvalue * refactor with coinreducer * speaking functions are your friends * refactor ugly nested code * lint --- lib/reducers/emoneyV0-reducers.js | 3 ++- lib/source/emoneyV0-source.js | 35 ++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/reducers/emoneyV0-reducers.js b/lib/reducers/emoneyV0-reducers.js index cbfe6a31a6..6ddbc9c545 100644 --- a/lib/reducers/emoneyV0-reducers.js +++ b/lib/reducers/emoneyV0-reducers.js @@ -14,7 +14,7 @@ async function totalBackedValueReducer(totalBackedValue) { // fiat currency const fiatValue = exchangeRates[token] ? BigNumber(totalBackedValue.amount) - .div(100000000) + .div(1000000) .times(exchangeRates[token][ticker]) .toNumber() : null @@ -120,5 +120,6 @@ async function expectedRewardsPerToken( module.exports = { ...terraV3Reducers, expectedRewardsPerToken, + fetchTokenExchangeRates, totalBackedValueReducer } diff --git a/lib/source/emoneyV0-source.js b/lib/source/emoneyV0-source.js index b31e7e2981..ac42dc9580 100644 --- a/lib/source/emoneyV0-source.js +++ b/lib/source/emoneyV0-source.js @@ -60,10 +60,7 @@ class EMoneyV0API extends TerraV3API { // To handle the NGM balance, first we convert to EUR value and then to the selected // fiat currency value if (denom === 'NGM') { - const eurValue = - BigNumber(balance.amount) - .div(100000000) - .toNumber() * 0.5 // 0.50€ is the price the NGM tokens will be first sold. Therefore, the official value until they reach an exchange + const eurValue = this.reducers.coinReducer(balance).amount * 0.5 // 0.50€ is the price the NGM tokens will be first sold. Therefore, the official value until they reach an exchange if (selectedFiatCurrency === `EUR`) { return parseFloat(eurValue) .toFixed(6) @@ -88,20 +85,20 @@ class EMoneyV0API extends TerraV3API { selectedFiatCurrency, denom ) - let totalFiatValue = 0 + let totalAsFiat = 0 // Here we check if the balance we are currently calculating the total fiat value from is // the same currency we want the fiat value to be displayed in. // In that case, we simply add it, don't need to convert it. if (denom !== selectedFiatCurrency) { - totalFiatValue = - BigNumber(balance.amount) - .div(100000000) - .toNumber() / rates[denom] + totalAsFiat = this.reducers.coinReducer(balance).amount / rates[denom] } else { - totalFiatValue = BigNumber(balance.amount) - .div(100000000) - .toNumber() + totalAsFiat = this.reducers.coinReducer(balance).amount } + // Now we do total value in the selected currency times the token fiat value + const totalFiatValue = await this.convertFiatValueToTokenFiatValue( + totalAsFiat, + denom + ) // Finally we get the proper currency sign to display const currencySign = this.getCurrencySign(selectedFiatCurrency) return parseFloat(totalFiatValue) @@ -119,6 +116,20 @@ class EMoneyV0API extends TerraV3API { .catch(error => console.error(error)) } + async convertFiatValueToTokenFiatValue(totalAsFiat, denom) { + const tokenExchangeRates = await this.reducers.fetchTokenExchangeRates() + const tokenExchangeRatesArray = Object.values(tokenExchangeRates) + const filterDenomFromExchangeRates = tokenExchangeRatesArray.filter( + rate => Object.keys(rate)[0] === denom + )[0] + const selectedFiatCurrencyExchangeRate = Object.values( + filterDenomFromExchangeRates + )[0] + return BigNumber(totalAsFiat) + .times(selectedFiatCurrencyExchangeRate) + .toNumber() + } + getCurrencySign(currency) { switch (currency) { case `EUR`: