Skip to content

Fix: Remove redundant division in FEE_UI scaling logic#93

Open
Rav1Chauhan wants to merge 1 commit intoDjedAlliance:mainfrom
Rav1Chauhan:fix/ui-fee-scaling
Open

Fix: Remove redundant division in FEE_UI scaling logic#93
Rav1Chauhan wants to merge 1 commit intoDjedAlliance:mainfrom
Rav1Chauhan:fix/ui-fee-scaling

Conversation

@Rav1Chauhan
Copy link

@Rav1Chauhan Rav1Chauhan commented Feb 25, 2026

Addressed Issues:

Fixes #66

Fix: Correct UI fee scaling logic
Problem

FEE_UI_UNSCALED divided FEE_UI by 100 before applying decimalUnscaling.
If FEE_UI already represents a decimal percentage (e.g., 0.01 for 1%), this resulted in a 100× smaller fee being sent to the contract.

Solution

Removed the redundant / 100 and passed FEE_UI directly to decimalUnscaling.

Result

The UI fee is now correctly scaled according to SCALING_DECIMALS.

Screenshots/Recordings:

Not applicable.

This change affects internal fee scaling logic in:

djed-sdk/src/djed/tradeUtils.js

No UI components were modified.

Additional Notes:

Problem

FEE_UI_UNSCALED was calculated as:

decimalUnscaling((FEE_UI / 100).toString(), SCALING_DECIMALS)

If FEE_UI already represents a decimal percentage (e.g., 0.01 for 1%), dividing by 100 again reduces the effective fee by 100×.

Example:

FEE_UI = 0.01 (intended 1%)

(FEE_UI / 100) = 0.0001

Resulting contract-side fee = 0.01% instead of 1%

This leads to incorrect fee scaling at the protocol level.

Removed the redundant / 100 and passed FEE_UI directly into decimalUnscaling:

export const FEE_UI_UNSCALED = decimalUnscaling(
FEE_UI.toString(),
SCALING_DECIMALS
);
Result

UI fee is now correctly scaled according to SCALING_DECIMALS

Prevents undercharging by 100×

Aligns SDK behavior with protocol expectations

No breaking API changes

Checklist

  • [x ] My PR addresses a single issue, fixes a single bug or makes a single improvement.
  • [ x] My code follows the project's code style and conventions.
  • If applicable, I have made corresponding changes or additions to the documentation.
  • If applicable, I have made corresponding changes or additions to tests.
  • [ x] My changes generate no new warnings or errors.
  • [ x] I have joined the Stability Nexus's Discord server and I will share a link to this PR with the project maintainers there.
  • [ x] I have read the Contribution Guidelines.
  • [ x] Once I submit my PR, CodeRabbit AI will automatically review it and I will address CodeRabbit's comments.

⚠️ AI Notice - Important!

We encourage contributors to use AI tools responsibly when creating Pull Requests. While AI can be a valuable aid, it is essential to ensure that your contributions meet the task requirements, build successfully, include relevant tests, and pass all linters. Submissions that do not meet these standards may be closed without warning to maintain the quality and integrity of the project. Please take the time to understand the changes you are proposing and their impact.

Summary by CodeRabbit

  • Bug Fixes
    • Corrected a fee calculation issue in trading that was applying incorrect scaling to transaction fees. Users will now see accurate fee amounts reflected in their trades.

@coderabbitai
Copy link

coderabbitai bot commented Feb 25, 2026

📝 Walkthrough

Walkthrough

Modified the FEE_UI_UNSCALED constant initialization in tradeUtils.js by removing the explicit division by 100, allowing the decimalUnscaling function to handle the proper scaling based on the protocol's decimal configuration.

Changes

Cohort / File(s) Summary
Fee Calculation Fix
djed-sdk/src/djed/tradeUtils.js
Removed the redundant division by 100 from FEE_UI_UNSCALED initialization. The constant now directly passes FEE_UI to decimalUnscaling() instead of (FEE_UI / 100), correcting the double-division bug that caused a 100x fee discrepancy.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Poem

🐰 A fee that was fractured by a hundred too much,
Now scales with precision—a delicate touch!
No more double-dividing what shouldn't divide,
The math hops along on the correct side! 🌟

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: removing a redundant division operation in the FEE_UI scaling logic that was causing a 100× discrepancy.
Linked Issues check ✅ Passed The PR directly addresses issue #66 by removing the redundant division by 100 in FEE_UI_UNSCALED calculation, matching the requirement to fix the fee-scaling logic so FEE_UI is passed directly to decimalUnscaling.
Out of Scope Changes check ✅ Passed The change is narrowly scoped to the single line identified in issue #66 (line 17 of tradeUtils.js) with no additional or unrelated modifications to other files or functionality.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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 and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
djed-sdk/src/djed/tradeUtils.js (1)

16-19: Add a regression test for the 1% UI-fee path.

Given this bug was subtle and high impact, please add a unit test that asserts Line 17 unscales FEE_UI = 0.01 correctly for SCALING_DECIMALS and that calculateTxFees(...).f_ui reflects 1% (not 0.01%).

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@djed-sdk/src/djed/tradeUtils.js` around lines 16 - 19, Add a regression unit
test that verifies FEE_UI_UNSCALED is computed correctly from FEE_UI = 0.01
using decimalUnscaling with SCALING_DECIMALS and that calculateTxFees(...).f_ui
returns 1% (0.01 as a fraction) not 0.0001; specifically, import FEE_UI,
SCALING_DECIMALS, FEE_UI_UNSCALED and decimalUnscaling, assert
decimalUnscaling(FEE_UI.toString(), SCALING_DECIMALS) equals FEE_UI_UNSCALED,
then call calculateTxFees with a representative amount and assert the returned
.f_ui equals the expected 0.01 (or equivalent fraction) to prevent the 0.01%
regression.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@djed-sdk/src/djed/tradeUtils.js`:
- Around line 16-19: Add a regression unit test that verifies FEE_UI_UNSCALED is
computed correctly from FEE_UI = 0.01 using decimalUnscaling with
SCALING_DECIMALS and that calculateTxFees(...).f_ui returns 1% (0.01 as a
fraction) not 0.0001; specifically, import FEE_UI, SCALING_DECIMALS,
FEE_UI_UNSCALED and decimalUnscaling, assert decimalUnscaling(FEE_UI.toString(),
SCALING_DECIMALS) equals FEE_UI_UNSCALED, then call calculateTxFees with a
representative amount and assert the returned .f_ui equals the expected 0.01 (or
equivalent fraction) to prevent the 0.01% regression.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 509e7d3 and 194e394.

📒 Files selected for processing (1)
  • djed-sdk/src/djed/tradeUtils.js

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.

[BUG] UI Fee Calculation Error (100x discrepancy)

1 participant