Skip to content

Commit

Permalink
Merge pull request #191 from leapdao/fix/currency-calculations
Browse files Browse the repository at this point in the history
Fix currency calculation timing
  • Loading branch information
TimDaub authored Jul 8, 2019
2 parents d91d421 + b841796 commit f6d1780
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 34 deletions.
93 changes: 68 additions & 25 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ export default class App extends Component {
ethprice: 0.00,
hasUpdateOnce: false,
possibleNewPrivateKey: '',
exchangeRate: {
USD: 0.00
}
// NOTE: USD in exchangeRate is undefined, such that any result using this
// number becomes NaN intentionally until it's defined.
exchangeRate: {}
};
this.alertTimeout = null;

Expand All @@ -156,18 +156,30 @@ export default class App extends Component {
this.setPossibleNewPrivateKey = this.setPossibleNewPrivateKey.bind(this)
}

currencyDisplay = (amount)=>{
let { exchangeRate } = this.state
let locale = localStorage.getItem('i18nextLng')
let balance = Math.floor(amount * 100) / 100

let symbol = localStorage.getItem('currency') || Object.keys(exchangeRate)[0];
let rate = Object.values(exchangeRate)[0];
return new Intl.NumberFormat(locale, { style: 'currency', currency: symbol, maximumFractionDigits: 2 }).format(this.convertExchangeRate(rate, balance))
// NOTE: This function is for _displaying_ a currency value to a user. It
// adds a currency unit to the beginning or end of the number!
currencyDisplay = amount => {
// NOTE: For some reason, this function seems to take very long.
const { exchangeRate } = this.state
const locale = localStorage.getItem('i18nextLng')
const symbol = localStorage.getItem('currency') || Object.keys(exchangeRate)[0];
const convertedAmount = this.convertExchangeRate(amount);

return new Intl.NumberFormat(locale, {
style: 'currency',
currency: symbol,
maximumFractionDigits: 2
}).format(convertedAmount)
}

convertExchangeRate = (rate, amount)=>{
return (parseFloat(amount) * rate)
// NOTE: This function is for telling a computer the converted value between
// two currencies. Please do not use this function to display values to
// users but use `currencyDisplay`.
convertExchangeRate = amount => {
const { exchangeRate } = this.state
const rate = Object.values(exchangeRate)[0];

return amount * rate;
}

parseAndCleanPath(path){
Expand Down Expand Up @@ -292,6 +304,9 @@ export default class App extends Component {
}
interval = setInterval(this.poll,1500)
intervalLong = setInterval(this.longPoll,45000)
// NOTE: We query once before starting the interval to define the value
// for the UI, as it needs to be readily available for the user.
this.queryExchangeWithNativeCurrency(CONFIG.CURRENCY.EXCHANGE_RATE_QUERY);
exchangeRatesQueryTimer = setInterval(this.queryExchangeWithNativeCurrency, CONFIG.CURRENCY.EXCHANGE_RATE_QUERY)
setTimeout(this.longPoll,150)

Expand Down Expand Up @@ -359,13 +374,22 @@ export default class App extends Component {

queryExchangeWithNativeCurrency() {
let currency = localStorage.getItem('currency') || CONFIG.CURRENCY.DEFAULT_CURRENCY
fetch(`https://min-api.cryptocompare.com/data/price?fsym=DAI&tsyms=${currency}`)
.then(response => response.json())
.then(response => {
this.setState({
'exchangeRate': response
if (currency === "USD") {
// NOTE: 1 DAI === 1 USD. Veritas in numeris! :)
this.setState({
exchangeRate: {
USD: 1
}
});
} else {
fetch(`https://min-api.cryptocompare.com/data/price?fsym=DAI&tsyms=${currency}`)
.then(response => response.json())
.then(response => {
this.setState({
'exchangeRate': response
})
})
})
}
}

setPossibleNewPrivateKey(value){
Expand Down Expand Up @@ -797,12 +821,16 @@ export default class App extends Component {
/>
)

let selected = "xDai"
let extraTokens = ""

let defaultBalanceDisplay = (
<div>
<Balance icon={pdai} selected={false} text={"PDAI"} amount={this.state.xdaiBalance} address={account} currencyDisplay={this.currencyDisplay} />
<Balance
icon={pdai}
text={"PDAI"}
amount={this.state.xdaiBalance}
address={account}
currencyDisplay={this.currencyDisplay} />
</div>
)

Expand Down Expand Up @@ -908,11 +936,26 @@ export default class App extends Component {
<Card>
{extraTokens}

<Balance icon={pdai} selected={selected} text={"PDAI"} amount={this.state.xdaiBalance} address={account} currencyDisplay={this.currencyDisplay}/>
<Balance
icon={pdai}
text={"PDAI"}
amount={this.state.xdaiBalance}
address={account}
currencyDisplay={this.currencyDisplay}/>

<Balance icon={dai} selected={selected} text={"DAI"} amount={this.state.daiBalance} address={account} currencyDisplay={this.currencyDisplay}/>
<Balance
icon={dai}
text={"DAI"}
amount={this.state.daiBalance}
address={account}
currencyDisplay={this.currencyDisplay}/>

<Balance icon={eth} selected={selected} text={"ETH"} amount={parseFloat(this.state.ethBalance) * parseFloat(this.state.ethprice)} address={account} currencyDisplay={this.currencyDisplay}/>
<Balance
icon={eth}
text={"ETH"}
amount={parseFloat(this.state.ethBalance) * parseFloat(this.state.ethprice)}
address={account}
currencyDisplay={this.currencyDisplay}/>

{/* eslint-disable-next-line jsx-a11y/accessible-emoji */}
<Warning>💀 This product is currently in early alpha. Use at your own risk! 💀</Warning>
Expand Down Expand Up @@ -957,7 +1000,6 @@ export default class App extends Component {
<div>
<Balance
icon={eth}
selected={selected}
text={"ETH"}
amount={parseFloat(this.state.ethBalance) * parseFloat(this.state.ethprice)}
currencyDisplay={this.currencyDisplay}
Expand Down Expand Up @@ -1059,6 +1101,7 @@ export default class App extends Component {
changeView={this.changeView}
setReceipt={this.setReceipt}
changeAlert={this.changeAlert}
convertExchangeRate={this.convertExchangeRate}
currencyDisplay={this.currencyDisplay}
/>
</Card>
Expand Down
14 changes: 7 additions & 7 deletions src/components/Balance.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import React from 'react';
import { Flex, Text, Image } from "rimble-ui";

export default ({icon, text, selected, amount, currencyDisplay}) => {
export default ({icon, text, amount, currencyDisplay}) => {

let opacity = 1
if(text === selected){
opacity=0.95
}

if(isNaN(amount) || typeof amount === "undefined"){
amount = 0.00
if(isNaN(amount)){
opacity = 0.25
}

Expand All @@ -23,7 +19,11 @@ export default ({icon, text, selected, amount, currencyDisplay}) => {
</Flex>

<Text fontSize={4}>
{currencyDisplay(amount)}

{/* NOTE: Sometimes the exchangeRate to fiat wasn't loaded yet and hence
* amount can become NaN. In this case, we simply pass 0 to
* currencyDisplay.*/}
{isNaN(amount) ? currencyDisplay(0) : currencyDisplay(amount)}
</Text>
</Flex>
)
Expand Down
4 changes: 2 additions & 2 deletions src/components/SendToAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ export default class SendToAddress extends React.Component {

send = async () => {
let { toAddress, amount } = this.state;
let {currencyDisplay} = this.props
let { convertExchangeRate, currencyDisplay } = this.props

amount = currencyDisplay(amount)
amount = convertExchangeRate(amount);
console.log("CONVERTED TO DOLLAR AMOUNT",amount)

if(this.state.canSend){
Expand Down

0 comments on commit f6d1780

Please sign in to comment.