Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove usage of bn.js v4 #12043

Draft
wants to merge 57 commits into
base: deps-ethereumjs-util-7
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
915357d
fix(deps): unpin ethereumjs-abi@0.6,x ethereumjs-util@6.x
legobeat Oct 23, 2024
64e9f80
fix: add package @types/bn.js
legobeat Oct 23, 2024
82d9427
fix: import BN.js directly instead of through ethereumjs-util
legobeat Oct 23, 2024
3d55e25
fix: validateTokenAmount fix
legobeat Oct 23, 2024
cd7a42e
lint:fix
legobeat Oct 23, 2024
5745eab
chore: remove redundant depcheckrc entry
legobeat Oct 28, 2024
8b41954
chore: rename BN to BN4
legobeat Oct 28, 2024
28ec999
chore(devDeps): detox@20.27.2->^20.27.5 (#12039)
legobeat Oct 28, 2024
f993c51
fix(deps): ethereumjs-util@^6.2.1->^7.1.5
legobeat Oct 21, 2024
71dd460
chore: testfix
legobeat Oct 23, 2024
68d5431
chore: remove usage of bn.js v4
legobeat Oct 28, 2024
fca7026
fix: toBN handle 0x-prefixed strings
legobeat Oct 28, 2024
b1e0cf9
fix: keep treating NaN as 0
legobeat Oct 28, 2024
5dff7dc
deps: number-to-bn -> @metamask/number-to-bn
legobeat Oct 28, 2024
4887e4a
hexToBN backwards-compatibility
legobeat Oct 28, 2024
341884c
toBN backwards-compat: falsey means 0
legobeat Oct 28, 2024
c0bbd6f
fix: treat string "NaN" as 0
legobeat Oct 28, 2024
6a86289
fix: default decimals=0 to prevent NaN calculation
legobeat Oct 28, 2024
f2dc7e4
fix(test): bn.js does not support scientific notation input
legobeat Oct 28, 2024
8561783
fix: toGwei, renderToGwei
legobeat Oct 28, 2024
b31de2d
wip: simplify fromTokenMinimalUnit
legobeat Oct 28, 2024
a240931
chore: update expected results for incorrectly rounding cases
legobeat Oct 28, 2024
6cc0955
fixup
legobeat Oct 28, 2024
6b53e86
use bignum.js for weiToFiatNumber
legobeat Oct 28, 2024
b2dd6bf
fixup
legobeat Oct 28, 2024
24c82e6
fix
legobeat Oct 28, 2024
523eba2
fix: toGwei use BigNumber, not BN
legobeat Oct 28, 2024
db0ac07
fix weiToFiat
legobeat Oct 28, 2024
96b490c
fix weiToFiat
legobeat Oct 28, 2024
f5c4787
fix weiToFiat
legobeat Oct 28, 2024
d7acee3
fix weiToFiatNumber
legobeat Oct 28, 2024
6b4ce3e
fix weiToFiatNumber
legobeat Oct 28, 2024
85cb7d6
fix: update test case that was asserting incorrect behavior
legobeat Oct 28, 2024
d0b5098
chore: fix incorrectly typed controller messages in test
legobeat Oct 28, 2024
89ab6bd
fix(ramps): use single internal representation of amount instead of 3
legobeat Oct 28, 2024
4bb3be6
fix test
legobeat Oct 28, 2024
e9fe643
fix(useBalance): remove balanceBN
legobeat Oct 28, 2024
ef2e7a9
buildquote: add memoizations
legobeat Oct 29, 2024
42d0736
chore: update test snapshot
legobeat Oct 29, 2024
511f945
useBalance refactor, test fix
legobeat Oct 29, 2024
098cd0f
buildquote: add memoizations
legobeat Oct 29, 2024
2db98c3
chore: clean up BuildQuote balance checks
legobeat Oct 29, 2024
9a7c754
chore: ramps test reordering
legobeat Oct 29, 2024
62494c2
fix: buildquote cleanup
legobeat Oct 29, 2024
2524583
fix: weiToFiat validation fix
legobeat Oct 29, 2024
f308077
fix: BuildQuote quickAmounts fix
legobeat Oct 29, 2024
e91a686
fix: simplify toTokenMinimalUnit
legobeat Oct 29, 2024
87fb6a2
fixup toTokenMinimalUnit
legobeat Oct 29, 2024
f3f6be4
fix: toTokenMinimalUnit
legobeat Oct 29, 2024
b4a1d4d
wip: introduce balanceMinimalUnit
legobeat Oct 29, 2024
34e7d2c
fix Ramps useBalance
legobeat Oct 29, 2024
f047310
fix: toTokenMinimalUnit, fromTokenMiimalUnitSTring default decimals=0
legobeat Oct 29, 2024
2623b57
fix: Ramps BuildQuote fix
legobeat Oct 29, 2024
49d73d8
chore: test, jsdoc fix
legobeat Oct 29, 2024
242eb08
wrap toTokenMinimalUnit value
legobeat Oct 29, 2024
e9cbdce
chore: BuildQuote test fix
legobeat Oct 29, 2024
2096f86
lint:fix
legobeat Oct 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/components/Nav/Main/RootRPCMethodsUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
getIsSwapApproveOrSwapTransaction,
isApprovalTransaction,
} from '../../../util/transactions';
import { BN } from 'ethereumjs-util';
import BN from 'bn.js';
import Logger from '../../../util/Logger';
import TransactionTypes from '../../../core/TransactionTypes';
import { swapsUtils } from '@metamask/swaps-controller';
Expand Down
4 changes: 2 additions & 2 deletions app/components/UI/ConfirmAddAsset/ConfirmAddAsset.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import renderWithProvider, {
import useBalance from '../Ramp/hooks/useBalance';
import { toTokenMinimalUnit } from '../../../util/number';
import { fireEvent } from '@testing-library/react-native';
import { BN } from 'ethereumjs-util';
import { RootState } from '../../../reducers';
import { mockNetworkState } from '../../../util/test/network';
import { CHAIN_IDS } from '@metamask/transaction-controller';
Expand Down Expand Up @@ -48,7 +47,8 @@ jest.mock('../../../util/navigation/navUtils', () => ({

const mockUseBalanceInitialValue: Partial<ReturnType<typeof useBalance>> = {
balanceFiat: '$27.02',
balanceBN: toTokenMinimalUnit('5.36385', 18) as BN,
balance: '5.36385',
balanceMinimalUnit: toTokenMinimalUnit('5.36385', 18).toString(),
};

const mockUseBalanceValues: Partial<ReturnType<typeof useBalance>> = {
Expand Down
61 changes: 30 additions & 31 deletions app/components/UI/Ramp/Views/BuildQuote/BuildQuote.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { Limits, Payment } from '@consensys/on-ramp-sdk';
import { act, fireEvent, screen } from '@testing-library/react-native';
import { BN } from 'ethereumjs-util';
import BN from 'bn.js';
import { renderScreen } from '../../../../../util/test/renderWithProvider';
import BuildQuote from './BuildQuote';
import useRegions from '../../hooks/useRegions';
Expand Down Expand Up @@ -155,7 +155,7 @@ jest.mock('../../hooks/usePaymentMethods', () =>
);

const MAX_LIMIT = 4;
const VALID_AMOUNT = 3;
const VALID_AMOUNT = '3';
const MIN_LIMIT = 2;
const mockUseLimitsInitialValues: Partial<ReturnType<typeof useLimits>> = {
limits: {
Expand Down Expand Up @@ -191,7 +191,8 @@ jest.mock('../../../../hooks/useAddressBalance/useAddressBalance', () =>

const mockUseBalanceInitialValue: Partial<ReturnType<typeof useBalance>> = {
balanceFiat: '$27.02',
balanceBN: toTokenMinimalUnit('5.36385', 18) as BN,
balance: '5.36385',
balanceMinimalUnit: '5363850000000000000',
};

let mockUseBalanceValues: Partial<ReturnType<typeof useBalance>> = {
Expand Down Expand Up @@ -239,10 +240,10 @@ let mockUseParamsValues: {
const mockUseGasPriceEstimationInitialValue: ReturnType<
typeof useGasPriceEstimation
> = {
estimatedGasFee: toTokenMinimalUnit(
estimatedGasFee: new BN(toTokenMinimalUnit(
'0.01',
mockUseRampSDKInitialValues.selectedAsset?.decimals || 18,
) as BN,
)),
};

let mockUseGasPriceEstimationValue: ReturnType<typeof useGasPriceEstimation> =
Expand Down Expand Up @@ -666,10 +667,12 @@ describe('BuildQuote View', () => {
maxAmount: 10,
} as Limits;

mockUseBalanceValues.balanceBN = toTokenMinimalUnit(
'5',
mockUseRampSDKValues.selectedAsset?.decimals || 18,
) as BN;
mockUseBalanceValues = {
...mockUseBalanceInitialValue,
balance: '5',
balanceMinimalUnit: (5 * Math.pow(10, mockUseRampSDKValues.selectedAsset?.decimals || 18)).toString(10),
};

render(BuildQuote);
const initialAmount = '0';
const overBalanceAmout = '6';
Expand All @@ -682,14 +685,15 @@ describe('BuildQuote View', () => {
});

it('updates the amount input with quick amount buttons', async () => {
render(BuildQuote);
const initialAmount = '0';
mockUseBalanceValues = {
...mockUseBalanceInitialValue,
balance: '1',
balanceMinimalUnit: (1 * Math.pow(10, mockUseRampSDKValues.selectedAsset?.decimals || 18)).toString(10),
};

mockUseBalanceValues.balanceBN = toTokenMinimalUnit(
'1',
mockUseRampSDKValues.selectedAsset?.decimals || 18,
) as BN;
const symbol = mockUseRampSDKValues.selectedAsset?.symbol;
render(BuildQuote);
fireEvent.press(getByRoleButton(`${initialAmount} ${symbol}`));
fireEvent.press(getByRoleButton('25%'));
expect(getByRoleButton(`0.25 ${symbol}`)).toBeTruthy();
Expand All @@ -715,27 +719,23 @@ describe('BuildQuote View', () => {

mockUseBalanceValues = {
balance: '1',
balanceMinimalUnit: (1 * Math.pow(10, mockUseRampSDKValues.selectedAsset?.decimals || 18)).toString(10),
balanceFiat: '$1.00',
balanceBN: toTokenMinimalUnit(
'1',
mockUseRampSDKValues.selectedAsset?.decimals || 18,
) as BN,
};
mockUseGasPriceEstimationValue = {
estimatedGasFee: toTokenMinimalUnit(
estimatedGasFee: new BN(toTokenMinimalUnit(
'0.27',
mockUseRampSDKValues.selectedAsset?.decimals || 18,
) as BN,
)),
};
const symbol = mockUseRampSDKValues.selectedAsset?.symbol;
fireEvent.press(getByRoleButton(`${initialAmount} ${symbol}`));
fireEvent.press(getByRoleButton(quickAmount));
expect(getByRoleButton(`0.73 ${symbol}`)).toBeTruthy();
});

it('updates the amount input up to the percentage considering gas', async () => {
render(BuildQuote);
const initialAmount = '0';
it('updates the amount input up to the percentage considering gas', async () => {
mockUseRampSDKValues = {
...mockUseRampSDKInitialValues,
isBuy: false,
Expand All @@ -745,21 +745,20 @@ describe('BuildQuote View', () => {
address: NATIVE_ADDRESS,
},
};

mockUseBalanceValues = {
balance: '1',
balanceMinimalUnit: (1 * Math.pow(10, mockUseRampSDKValues.selectedAsset?.decimals || 18)).toString(10),
balanceFiat: '$1.00',
balanceBN: toTokenMinimalUnit(
'1',
mockUseRampSDKValues.selectedAsset?.decimals || 18,
) as BN,
};
mockUseGasPriceEstimationValue = {
estimatedGasFee: toTokenMinimalUnit(
estimatedGasFee: new BN(toTokenMinimalUnit(
'0.27',
mockUseRampSDKValues.selectedAsset?.decimals || 18,
) as BN,
)),
};

render(BuildQuote);

const symbol = mockUseRampSDKValues.selectedAsset?.symbol;
fireEvent.press(getByRoleButton(`${initialAmount} ${symbol}`));
fireEvent.press(getByRoleButton('75%'));
Expand Down Expand Up @@ -799,7 +798,7 @@ describe('BuildQuote View', () => {
});

expect(mockTrackEvent).toHaveBeenCalledWith('ONRAMP_QUOTES_REQUESTED', {
amount: VALID_AMOUNT,
amount: parseInt(VALID_AMOUNT),
currency_source: mockUseFiatCurrenciesValues?.currentFiatCurrency?.symbol,
currency_destination: mockUseRampSDKValues?.selectedAsset?.symbol,
payment_method_id: mockUsePaymentMethodsValues.currentPaymentMethod?.id,
Expand Down Expand Up @@ -834,7 +833,7 @@ describe('BuildQuote View', () => {
});

expect(mockTrackEvent).toHaveBeenCalledWith('OFFRAMP_QUOTES_REQUESTED', {
amount: VALID_AMOUNT,
amount: parseInt(VALID_AMOUNT),
currency_source: mockUseRampSDKValues?.selectedAsset?.symbol,
currency_destination:
mockUseFiatCurrenciesValues?.currentFiatCurrency?.symbol,
Expand Down
Loading
Loading