Skip to content

Commit 5d5ba48

Browse files
authored
fix: Remove lingering decimal in return of gweiDecimalToWeiDecimal (#5839)
## Explanation <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> This PR aims to fix minor issue on `gweiDecimalToWeiDecimal` utility. If given value is more than 9 decimal places then generated WEI value will have decimal part which we don't expect / want in WEI value. ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> Fixes: MetaMask/MetaMask-planning#4973 ## Changelog <!-- THIS SECTION IS NO LONGER NEEDED. The process for updating changelogs has changed. Please consult the "Updating changelogs" section of the Contributing doc for more. --> ## Checklist - [X] I've updated the test suite for new or updated code as appropriate - [X] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [X] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs), highlighting breaking changes as necessary - [X] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes
1 parent 1f8c7cc commit 5d5ba48

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

packages/transaction-controller/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
- Fix `userFeeLevel` as `dappSuggested` initially when dApp suggested gas values for legacy transactions ([#5821](https://github.com/MetaMask/core/pull/5821))
1717
- Fix `addTransaction` function to correctly identify a transaction as a `simpleSend` type when the recipient is a smart account ([#5822](https://github.com/MetaMask/core/pull/5822))
18+
- Fix gas fee randomisation with many decimal places ([#5839](https://github.com/MetaMask/core/pull/5839))
1819

1920
## [56.1.0]
2021

packages/transaction-controller/src/utils/gas-fees.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,4 +580,37 @@ describe('gweiDecimalToWeiDecimal', () => {
580580
expect(gweiDecimalToWeiDecimal('1000000')).toBe('1000000000000000');
581581
expect(gweiDecimalToWeiDecimal(1000000)).toBe('1000000000000000');
582582
});
583+
584+
it('handles values with many decimal places', () => {
585+
expect(gweiDecimalToWeiDecimal('1.123456789123')).toBe('1123456789');
586+
expect(gweiDecimalToWeiDecimal(1.123456789123)).toBe('1123456789');
587+
});
588+
589+
it('handles small decimal values', () => {
590+
expect(gweiDecimalToWeiDecimal('0.000000001')).toBe('1');
591+
expect(gweiDecimalToWeiDecimal(0.000000001)).toBe('1');
592+
expect(gweiDecimalToWeiDecimal('0.00000001')).toBe('10');
593+
});
594+
595+
it('handles string values with leading zeros', () => {
596+
expect(gweiDecimalToWeiDecimal('00.1')).toBe('100000000');
597+
expect(gweiDecimalToWeiDecimal('01.5')).toBe('1500000000');
598+
});
599+
600+
it('handles string values with trailing zeros', () => {
601+
expect(gweiDecimalToWeiDecimal('1.500')).toBe('1500000000');
602+
expect(gweiDecimalToWeiDecimal('123.450000')).toBe('123450000000');
603+
});
604+
605+
it('handles extremely small values', () => {
606+
expect(gweiDecimalToWeiDecimal('0.000000000001')).toBe('0');
607+
expect(gweiDecimalToWeiDecimal(0.000000000001)).toBe('0');
608+
});
609+
610+
it('handles scientific notation inputs', () => {
611+
expect(gweiDecimalToWeiDecimal('1e-9')).toBe('1');
612+
expect(gweiDecimalToWeiDecimal(1e-9)).toBe('1');
613+
expect(gweiDecimalToWeiDecimal('1e9')).toBe('1000000000000000000');
614+
expect(gweiDecimalToWeiDecimal(1e9)).toBe('1000000000000000000');
615+
});
583616
});

packages/transaction-controller/src/utils/gas-fees.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,9 @@ export function gweiDecimalToWeiHex(value: string) {
126126
* // Returns "1500000000"
127127
*/
128128
export function gweiDecimalToWeiDecimal(gweiDecimal: string | number): string {
129-
const gwei =
130-
typeof gweiDecimal === 'string' ? gweiDecimal : String(gweiDecimal);
129+
const weiValue = Number(gweiDecimal) * 1e9;
131130

132-
const weiDecimal = Number(gwei) * 1e9;
133-
134-
return weiDecimal.toString();
131+
return weiValue.toString().split('.')[0];
135132
}
136133

137134
/**

0 commit comments

Comments
 (0)