OrderBook

First Flight #43
Beginner FriendlySolidity
100 EXP
Submission Details
Impact: low
Likelihood: low
Invalid

Struct Packing (Gas Optimization)

Author Revealed upon completion

Root + Impact

Description

  • Normal behavior:

The Order struct stores all relevant data for each order, including the order ID, seller, token address, amount, price, deadline, and active status. This struct is stored in a mapping, and each order occupies a storage slot.

  • Issue:

The bool isActive field is not optimally packed within the struct, leading to slightly higher gas costs for storage operations. Solidity stores each variable in a 32-byte slot, and packing smaller variables together can reduce storage usage and gas costs. In this case, the bool could be packed with other smaller variables or moved to the end of the struct for better efficiency.

struct Order {
uint256 id;
address seller;
address tokenToSell;
uint256 amountToSell;
uint256 priceInUSDC;
uint256 deadlineTimestamp;
bool isActive; // @> Placed after uint256, not optimally packed
}

Risk

Likelihood:

  • Every order creation and update incurs slightly higher gas costs due to suboptimal storage layout.

  • This occurs for every user interaction with orders, especially in high-volume protocols.

Impact:

  • Increased gas costs for users, making the protocol less competitive.

  • Less efficient use of storage, which can add up over time.

Proof of Concept

// This PoC compares gas usage before and after optimizing struct packing.
// Deploy the contract as-is, then deploy a version with `bool isActive` at the end of the struct.
// Measure gas usage for order creation and updates in both versions.

Recommended Mitigation

Move bool isActive to the end of the struct and group address fields together for optimal packing. Consider using smaller types for fields where possible.
Explanation: Optimizing struct packing reduces storage costs and gas usage, making the protocol more efficient and user-friendly.
Updates

Lead Judging Commences

yeahchibyke Lead Judge about 6 hours ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

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