Trick or Treat

First Flight #27
Beginner FriendlyFoundry
100 EXP
View results
Submission Details
Severity: low
Invalid

Incorrect userTreats Mapping Structure

Summary

The current implementation uses mapping(string => Treat) public treatList;, which maps treat names directly to Treat structs. This approach may lead to inefficient gas usage and limitations in treat management for individual users, particularly if each user is expected to have multiple treats.

The mapping(string => Treat) public treatList; is not optimal for managing user-specific treats, as it only allows a single treat per unique string key (treat name). This structure is not suitable if each user should have their own separate collection of treats. Additionally, using string as a mapping key is costly in terms of gas efficiency compared to more efficient keys like address.

mapping(string => Treat) public treatList;
string[] public treatNames;
function addTreat(string memory _name, uint256 _rate, string memory _metadataURI) public onlyOwner {
treatList[_name] = Treat(_name, _rate, _metadataURI);
treatNames.push(_name);
emit TreatAdded(_name, _rate, _metadataURI);
}

Impact

Inefficient Gas Usage: Using string as the key incurs higher gas costs due to the need for complex data handling.

  • Single Treat Limitation: The mapping structure restricts the contract to one treat per unique name, making it unsuitable for tracking multiple treats per user.

  • Lack of User-Specific Tracking: Without using an address key, the current setup cannot easily map treats to specific users, reducing flexibility and usability

Tools Used

Manual Review

Recommendations

  1. Use address => Treat[] mapping: Replace mapping(string => Treat) with mapping(address => Treat[]), which allows each user (represented by their address) to have an array of treats. This structure makes treat management more flexible and efficient for user-specific data.

  2. Efficient Data Retrieval: Use functions to add, retrieve, and manage treats per user within the new mapping structure. For example, create an addTreatForUser function to append treats to a user’s Treat[] array.

Updated Code Example

mapping(address => Treat[]) public userTreats;
function addTreatForUser(address _user, string memory _name, uint256 _cost, string memory _metadataURI) public {
userTreats[_user].push(Treat({
name: _name,
cost: _cost,
metadataURI: _metadataURI
}));
}
Updates

Appeal created

bube Lead Judge 8 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

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