Skip to content

Commit

Permalink
chore: Revert "fix: swaps quote nan to bnjs (#9848)" (#9863)
Browse files Browse the repository at this point in the history
This reverts commit d8987c8.

<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

This PR reverts #9848 in favor of
#9855.

## **Related issues**

Fixes:

## **Manual testing steps**

n/a

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->
n/a

### **Before**

n/a
<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
  • Loading branch information
infiniteflower authored Jun 5, 2024
1 parent d8987c8 commit 3fcb780
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 28 deletions.
11 changes: 1 addition & 10 deletions app/components/UI/Swaps/QuotesView.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,6 @@ function SwapsQuotesView({
const ethAmountBN = isSwapsNativeAsset(sourceToken)
? sourceBN
: new BigNumber(0);

// accounts[selectedAddress].balance might be undefined
// BigNumber(undefined) returns "NaN", which causes missingEthBalance to be "NaN" downstream
const ethBalanceBN = new BigNumber(accounts[selectedAddress].balance);
const gasBN = toWei(selectedQuoteValue?.maxEthFee || '0');
const hasEnoughEthBalance = ethBalanceBN.gte(ethAmountBN.plus(gasBN));
Expand Down Expand Up @@ -1677,12 +1674,6 @@ function SwapsQuotesView({
hasEnoughTokenBalance &&
hasEnoughEthBalance;

const shouldShowNeedMoreAlert =
(!isSwapsNativeAsset(sourceToken) && !hasEnoughTokenBalance) ||
(isSwapsNativeAsset(sourceToken) &&
!hasEnoughEthBalance &&
missingEthBalance?.isNaN() === false);

return (
<ScreenView
contentContainerStyle={styles.screen}
Expand All @@ -1691,7 +1682,7 @@ function SwapsQuotesView({
scrollEnabled={!isSwiping}
>
<View style={styles.topBar}>
{shouldShowNeedMoreAlert && (
{(!hasEnoughTokenBalance || !hasEnoughEthBalance) && (
<View style={styles.alertBar}>
<Alert small type={AlertType.Info}>
{`${strings('swaps.you_need')} `}
Expand Down
23 changes: 9 additions & 14 deletions app/util/number/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,22 +312,17 @@ export function fiatNumberToTokenMinimalUnit(
export function renderFromWei(value, decimalsToShow = 5) {
let renderWei = '0';
// avoid undefined
// This function can throw an error if value is "NaN", so wrap in try/catch
try {
if (value) {
const wei = fromWei(value);
const weiNumber = parseFloat(wei);
if (weiNumber < 0.00001 && weiNumber > 0) {
renderWei = '< 0.00001';
} else {
const base = Math.pow(10, decimalsToShow);
renderWei = (Math.round(weiNumber * base) / base).toString();
}
if (value) {
const wei = fromWei(value);
const weiNumber = parseFloat(wei);
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;
} catch (e) {
return renderWei;
}
return renderWei;
}

/**
Expand Down
4 changes: 0 additions & 4 deletions app/util/number/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,6 @@ describe('Number utils :: renderFromWei', () => {
expect(renderFromWei('133700000000000000')).toEqual('0.1337');
expect(renderFromWei('1337')).toEqual('< 0.00001');
expect(renderFromWei('0')).toEqual('0');

// https://github.com/MetaMask/metamask-mobile/issues/9672
// fromWei will throw error if "NaN" is passed
expect(renderFromWei('NaN')).toEqual('0');
});

it('renderFromWei using BN number', () => {
Expand Down

0 comments on commit 3fcb780

Please sign in to comment.