Skip to content

Solidity: Fix premint amounts below 1 generating literals with leading zeros - #894

Open
RaphaelFakhri wants to merge 1 commit into
OpenZeppelin:masterfrom
RaphaelFakhri:fix-premint-leading-zero
Open

RaphaelFakhri wants to merge 1 commit into
OpenZeppelin:masterfrom
RaphaelFakhri:fix-premint-leading-zero

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

Fixes #893

calculatePremint strips leading zeros from the integer part and trailing zeros from the fractional part, but it does not strip leading zeros from the fractional part. When the premint is below 1 and the fractional part starts with a zero, the generated literal starts with a zero. Solidity rejects it.

For example, ERC20 with premint 0.05 generates the following line:

_mint(recipient, 05 * 10 ** (decimals() - 2));

solc 0.8.x fails with ParserError: Octal numbers not allowed. The same happens for 0.001, 0.01e1, and in Stablecoin, RWA, and Confidential ERC7984, which use the same calculation.

Changes

  • Strip leading zeros from the concatenated units in calculatePremint. 0.05 now generates _mint(recipient, 5 * 10 ** (decimals() - 2));, which compiles.
  • Add a test for calculatePremint with amounts below 1.
  • Add a patch changeset for @openzeppelin/wizard.

Testing

  • npx ava src/erc20.test.ts --match "calculatePremint*" in packages/core/solidity: fails on main (units: '05'), passes with the fix.
  • Generated contract for premint 0.05 compiles with solcjs after the fix.
  • yarn test in packages/core/confidential, yarn lint, and yarn format:check pass.

@RaphaelFakhri
RaphaelFakhri requested review from a team as code owners October 25, 2026 07:41
@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 925f935c-c18a-4ad5-83a3-188c20adad6c
Commits

Reviewing files that changed from the base of the PR and between b2c649b and f067ab1.

Files selected for processing: 3
  • .changeset/premint-leading-zeros.md
  • packages/core/solidity/src/erc20.test.ts
  • packages/core/solidity/src/erc20.ts

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

Walkthrough

The change removes leading zeros from premint units before exponent-derived zeros are appended. Tests cover 0.05 and 0.001e1. A patch changeset documents the fix.

Changes

Premint normalization

Layer / File(s) Summary
Normalize and test premint units
packages/core/solidity/src/erc20.ts, packages/core/solidity/src/erc20.test.ts, .changeset/premint-leading-zeros.md
calculatePremint removes leading zeros from the combined digits before appending exponent-derived zeros. Tests cover 0.05 and 0.001e1. A patch changeset describes the fix.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

Suggested reviewers: frangio, ericglau, ernestognw

Merge Risk: Minimal · up to f067a

The premint fix preserves supported numeric forms and addresses the invalid leading-zero literals without an identified merge-blocking risk.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (1 skipped: 1 … Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary fix: preventing invalid Solidity literals with leading zeros for premint amounts below 1.
Description check Passed The description directly explains the affected function, the Solidity compilation issue, the fix, the tests, and the changeset.
Linked Issues check Passed Issue #893 requires compilable literals for premint values below 1, including 0.05 and exponent notation. calculatePremint now applies stripLeadingZeros to the concatenated integer and fractiona…
Out of Scope Changes check Passed The changes are limited to the calculatePremint fix, focused regression tests, and a patch changeset for @openzeppelin/wizard. Each change supports issue #893.
Full details: Linked Issues check

Explanation

Issue #893 requires compilable literals for premint values below 1, including 0.05 and exponent notation. calculatePremint now applies stripLeadingZeros to the concatenated integer and fractional digits before adding exponent-derived zeros. The new tests verify 0.05 produces units: '5' and 0.001e1 produces units: '1', with the expected decimal exponent.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches: 💡 1
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@RaphaelFakhri

Copy link
Copy Markdown
Author

I confirm that I have read and hereby agree to the OpenZeppelin Contributor License Agreement

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Solidity: Premint amounts below 1 with a leading fractional zero generate code that does not compile

1 participant