NFTBridge
60,000 USDC
View results
Submission Details
Severity: low
Invalid

Use of Literals Instead of Constants for Code Maintainability and Reducing Bugs

Summary

Literal values are repeatedly used across multiple files in the codebase, which could lead to inconsistencies and difficulties in maintenance. Defining these literals as constant variables will improve code readability and maintainability.

Instances

  1. File: src/Escrow.sol

    • Lines: 86, 107

      // Line 86
      require(amount >= 100, "Minimum amount required.");
      // Line 107
      uint256 fee = (amount * 2) / 100;
  2. File: src/Protocol.sol

    • Lines: 177, 228

      // Line 177
      uint256 constant RATE = 2500;
      // Line 228
      uint256 threshold = 5000;
  3. File: src/sn/Cairo.sol

    • Lines: 128, 149, 239, 451

      // Line 128
      buf[offset] = uint128(val & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
      // Line 149
      buf[offset + 1] = uint128(val >> 128);
      // Line 239
      uint256 constant MAX_UINT128 = 2**128 - 1;
      // Line 451
      require(val <= MAX_UINT128, "Value exceeds maximum.");

Impact on Project:

  • Maintainability: Using constants instead of literals reduces the risk of errors during code updates and improves the readability of the code.

  • Consistency: Defining constants ensures consistent usage across the codebase, minimizing discrepancies and potential bugs.

Recommendations

  1. Consistent Usage: Replace all repeated literals with constant variables across the codebase.

  2. Code Review: Perform a thorough code review to identify any additional instances where literals could be replaced with constants.

Suggested Code Refactoring

  1. In src/Protocol.sol:

    // Define constant values
    uint256 constant BASE_LENGTH = 7;
    // Replace literals with constants
    uint256 len = BASE_LENGTH;
  2. In src/sn/Cairo.sol:

    // Define constants
    uint256 constant LOW_PART = 128;
    uint256 constant HIGH_PART = 128;
    // Refactor the serialization function
    buf[offset] = uint128(val);
    buf[offset + 1] = uint128(val >> LOW_PART);
Updates

Lead Judging Commences

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

Informational / Gas

Please, do not suppose impacts, think about the real impact of the bug and check the CodeHawks documentation to confirm: https://docs.codehawks.com/hawks-auditors/how-to-determine-a-finding-validity A PoC always helps to understand the real impact possible.

Support

FAQs

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