[utils] `toWei` accepts fractional wei and returns wrong values
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 88/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- typescript
- Domain
- blockchain
Research direction
Start with packages/utils/src/unitsConversion.ts and compare the behavior with the original pull request linked in the issue. Add regression coverage for fractional wei values such as 0.5, 1.5, and 0.9, then run the relevant utils tests and confirm fractional wei is rejected while whole wei remains unchanged.
Written by the indexing model from the issue text.
Description
Carried over from https://github.com/MetaMask/utils/pull/303, which was open when @metamask/utils moved into core.
toWei silently accepts fractional wei and returns the wrong number. Confirmed against main today:
import { toWei } from '@metamask/utils';
toWei('0.5', 'wei'); // 5n
toWei('1.5', 'wei'); // 6n
toWei('0.9', 'wei'); // 9n
toWei('1', 'wei'); // 1n
Wei is the base unit, so anything with a fractional part should be rejected rather than scaled.
The cause is in packages/utils/src/unitsConversion.ts:
const unitLengths = Object.fromEntries(
Object.entries(unitMap).map(([key, value]) => [key, value.length - 1 || 1]),
) as Record<EthereumUnit, number>;
Wei's raw value is '1', so value.length - 1 is 0, which is the correct precision for the base unit. The || 1 fallback then turns that 0 into 1, so the fraction gets treated as one decimal place of a larger unit.
The original PR has the fix plus tests.
- Dominant language
- TypeScript
- Stars
- 413
- Forks
- 308
- Avg merge
- 1d 4h
- Merged PRs (30d)
- 253
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from MetaMask/core
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
category-documentation github-migration-triaged team-wallet-framework wf-documentation
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Eynzof/Hermes-CN-Desktop#610 ·
-
bug clawsweeper:linked-pr-open clawsweeper:needs-live-repro clawsweeper:no-new-fix-pr impact:message-loss issue-rating: 🐚 platinum hermit P2 regression
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
calcite-components needs triage refactor
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Esri/calcite-design-system#15203 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
fullcalendar/fullcalendar#8106 ·