Beginner FriendlyFoundryDeFi
100 EXP
View results
Submission Details
Severity: low
Invalid

Precision issue in server-side points calculation

Summary

A precision issue was identified in the steak points calculation system, potentially leading to inaccuracies in user point balances.

Vulnerability Details

The current implementation uses JavaScript's native number type for calculations, which can lead to precision loss when dealing with large numbers or fractional values. This is particularly problematic when calculating steak points from Ether amounts.

- points: +ethers.formatEther(amount) * PRECISION,
+ points: new Decimal(ethers.formatEther(amount)).mul(PRECISION);

For example, consider a user staking 0.512345678912345678 ETH:

cast send ... --value 0.512345678912345678ether

The difference in points awarded would be:

- 512.3456789123456 // Points awarded using JS number (precision loss)
+ 512.345678912345678 // Points awarded using Decimal (full precision maintained)

This difference may seem small for a single transaction, but it can accumulate to significant discrepancies over multiple transactions or with larger amounts.

Impact

Users may receive incorrect steak point allocations, potentially leading to unfair distribution of rewards or benefits in the system.

Tools Used

Manual code review

Recommendations

• Use a high-precision decimal library (e.g., decimal.js) for calculations:

+ import Decimal from 'decimal.js';
+ Decimal.set({ precision: 50 })

• Update the MongoDB schema to use a Decimal (Decimal128 under the hood) type for storing points:

- type: Number,
+ type: mongoose.Schema.Types.Decimal,

• Update the package.json to include the new dependency:

+ "decimal.js": "^10.4.3",
Updates

Lead Judging Commences

inallhonesty Lead Judge 10 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
Assigned finding tags:

Precision

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.