Skip to content

Commit

Permalink
emoney fixes for fiatvalue (#345)
Browse files Browse the repository at this point in the history
* emoney fixes for fiatvalue

* refactor with coinreducer

* speaking functions are your friends

* refactor ugly nested code

* lint
  • Loading branch information
Bitcoinera authored Feb 19, 2020
1 parent 3f4195f commit c20d2c6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
3 changes: 2 additions & 1 deletion lib/reducers/emoneyV0-reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -120,5 +120,6 @@ async function expectedRewardsPerToken(
module.exports = {
...terraV3Reducers,
expectedRewardsPerToken,
fetchTokenExchangeRates,
totalBackedValueReducer
}
35 changes: 23 additions & 12 deletions lib/source/emoneyV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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`:
Expand Down

0 comments on commit c20d2c6

Please sign in to comment.