Skip to content

Commit

Permalink
Bugfix: currency code & values with less than 5 precision decimals (#615
Browse files Browse the repository at this point in the history
)

* remove usd dollar symbol

* render low values

* avoid crashing app when hot reloading

* avoid rendering when no value

* rollback fix

* tests

* comment
  • Loading branch information
estebanmino authored Apr 18, 2019
1 parent 06741f2 commit c37bac0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
2 changes: 1 addition & 1 deletion app/components/UI/AccountOverview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class AccountOverview extends Component {
currentCurrency
} = this.props;

const fiatBalance = `$${renderFiat(Engine.getTotalFiatAccountBalance(), currentCurrency)}`;
const fiatBalance = `${renderFiat(Engine.getTotalFiatAccountBalance(), currentCurrency)}`;

if (!address) return null;
const { accountLabelEditable, accountLabel } = this.state;
Expand Down
2 changes: 1 addition & 1 deletion app/components/UI/DrawerView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ class DrawerView extends Component {
</Text>
<Icon name="caret-down" size={24} style={styles.caretDown} />
</View>
<Text style={styles.accountBalance}>${fiatBalanceStr}</Text>
<Text style={styles.accountBalance}>{fiatBalanceStr}</Text>
<Text style={styles.accountAddress}>{renderShortAddress(account.address)}</Text>
{this.isCurrentAccountImported() && (
<View style={styles.importedWrapper}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ exports[`TransactionReviewInformation should render correctly 1`] = `
}
}
>
0 ETH
&lt; 0.00001 ETH
</Text>
</View>
</View>
Expand Down
26 changes: 20 additions & 6 deletions app/util/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,19 @@ export function toTokenMinimalUnit(tokenValue, decimals) {
* @param {Number|String|BN} tokenValue - Token value to convert
* @param {Number} decimals - Token decimals to convert
* @param {Number} decimalsToShow - Decimals to 5
* @returns {Number} - Number of token minimal unit, in render format
* @returns {String} - Number of token minimal unit, in render format
* If value is less than 5 precision decimals will show '< 0.00001'
*/
export function renderFromTokenMinimalUnit(tokenValue, decimals, decimalsToShow = 5) {
const minimalUnit = fromTokenMinimalUnit(tokenValue, decimals);
const base = Math.pow(10, decimalsToShow);
const renderMinimalUnit = Math.round(parseFloat(minimalUnit) * base) / base;
const minimalUnitNumber = parseFloat(minimalUnit);
let renderMinimalUnit;
if (minimalUnitNumber < 0.00001 && minimalUnitNumber > 0) {
renderMinimalUnit = '< 0.00001';
} else {
const base = Math.pow(10, decimalsToShow);
renderMinimalUnit = (Math.round(minimalUnitNumber * base) / base).toString();
}
return renderMinimalUnit;
}

Expand All @@ -123,12 +130,19 @@ export function renderFromTokenMinimalUnit(tokenValue, decimals, decimalsToShow
*
* @param {Number|String|BN} value - Wei to convert
* @param {Number} decimalsToShow - Decimals to 5
* @returns {Number} - Number of token minimal unit, in render format
* @returns {String} - Number of token minimal unit, in render format
* If value is less than 5 precision decimals will show '< 0.00001'
*/
export function renderFromWei(value, decimalsToShow = 5) {
const wei = fromWei(value);
const base = Math.pow(10, decimalsToShow);
const renderWei = Math.round(parseFloat(wei) * base) / base;
const weiNumber = parseFloat(wei);
let renderWei;
if (weiNumber < 0.00001 && weiNumber > 0) {
renderWei = '< 0.00001';
} else {
const base = Math.pow(10, decimalsToShow);
renderWei = (Math.round(weiNumber * base) / base).toString();
}
return renderWei;
}

Expand Down
36 changes: 21 additions & 15 deletions app/util/number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,38 +85,44 @@ describe('Number utils :: toTokenMinimalUnit', () => {

describe('Number utils :: renderFromTokenMinimalUnit', () => {
it('renderFromTokenMinimalUnit using number', () => {
expect(renderFromTokenMinimalUnit(1337, 6)).toEqual(0.00134);
expect(renderFromTokenMinimalUnit(1337, 0)).toEqual(1337);
expect(renderFromTokenMinimalUnit(1337, 10)).toEqual(0);
expect(renderFromTokenMinimalUnit(1337, 6)).toEqual('0.00134');
expect(renderFromTokenMinimalUnit(1337, 0)).toEqual('1337');
expect(renderFromTokenMinimalUnit(1337, 10)).toEqual('< 0.00001');
expect(renderFromTokenMinimalUnit(0, 10)).toEqual('0');
});

it('renderFromTokenMinimalUnit using string', () => {
expect(renderFromTokenMinimalUnit('1337', 6)).toEqual(0.00134);
expect(renderFromTokenMinimalUnit('1337', 0)).toEqual(1337);
expect(renderFromTokenMinimalUnit('1337', 10)).toEqual(0);
expect(renderFromTokenMinimalUnit('1337', 6)).toEqual('0.00134');
expect(renderFromTokenMinimalUnit('1337', 0)).toEqual('1337');
expect(renderFromTokenMinimalUnit('1337', 10)).toEqual('< 0.00001');
expect(renderFromTokenMinimalUnit('0', 10)).toEqual('0');
});

it('renderFromTokenMinimalUnit using BN number', () => {
expect(renderFromTokenMinimalUnit(new BN('1337'), 0)).toEqual(1337);
expect(renderFromTokenMinimalUnit(new BN('1337'), 6)).toEqual(0.00134);
expect(renderFromTokenMinimalUnit(new BN('1337'), 10)).toEqual(0);
expect(renderFromTokenMinimalUnit(new BN('1337'), 0)).toEqual('1337');
expect(renderFromTokenMinimalUnit(new BN('1337'), 6)).toEqual('0.00134');
expect(renderFromTokenMinimalUnit(new BN('1337'), 10)).toEqual('< 0.00001');
expect(renderFromTokenMinimalUnit(new BN('0'), 10)).toEqual('0');
});
});

describe('Number utils :: renderFromWei', () => {
it('renderFromWei using number', () => {
expect(renderFromWei(133700000000000000)).toEqual(0.1337);
expect(renderFromWei(1337)).toEqual(0);
expect(renderFromWei(133700000000000000)).toEqual('0.1337');
expect(renderFromWei(1337)).toEqual('< 0.00001');
expect(renderFromWei(0)).toEqual('0');
});

it('renderFromWei using string', () => {
expect(renderFromWei('133700000000000000')).toEqual(0.1337);
expect(renderFromWei('1337')).toEqual(0);
expect(renderFromWei('133700000000000000')).toEqual('0.1337');
expect(renderFromWei('1337')).toEqual('< 0.00001');
expect(renderFromWei('0')).toEqual('0');
});

it('renderFromWei using BN number', () => {
expect(renderFromWei(new BN('133700000000000000'))).toEqual(0.1337);
expect(renderFromWei(new BN('1337'))).toEqual(0);
expect(renderFromWei(new BN('133700000000000000'))).toEqual('0.1337');
expect(renderFromWei(new BN('1337'))).toEqual('< 0.00001');
expect(renderFromWei(new BN('0'))).toEqual('0');
});
});

Expand Down

0 comments on commit c37bac0

Please sign in to comment.