Puppy Raffle

AI First Flight #1
Beginner FriendlyFoundrySolidityNFT
EXP
View results
Submission Details
Impact: low
Likelihood: high
Invalid

Gas: Redundant mappings `rarityToUri` and `rarityToName`

Description

  • Mappings are best used for dynamic data associations, not statically known associations.

  • The contract stores constant URIs and Names in mappings, which requires expensive SSTORE operations in the constructor and expensive SLOAD operations in tokenURI().

// Root cause in the codebase with @> marks to highlight the relevant section
@> mapping(uint256 => string) public rarityToUri;
@> mapping(uint256 => string) public rarityToName;
constructor(...) ERC721("Puppy Raffle", "PR") {
// ...
@> rarityToUri[COMMON_RARITY] = commonImageUri;
@> rarityToName[COMMON_RARITY] = COMMON;
// ...
}

Risk

Likelihood:

  • Constant, affects every deployment and metadata read.

  • tokenURI() performs multiple unnecessary state lookups.

Impact:

  • Very high deployment cost due to writing strings to mapped storage slots.

  • Increased gas costs when calling tokenURI().

Proof of Concept

Not applicable - Gas Optimization

Recommended Mitigation

Remove both mappings and replace them with dynamic returning inside tokenURI().

- mapping(uint256 => string) public rarityToUri;
- mapping(uint256 => string) public rarityToName;
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
require(_exists(tokenId), "PuppyRaffle: URI query for nonexistent token");
uint256 rarity = tokenIdToRarity[tokenId];
+ string memory imageURI;
+ string memory rareName;
+
+ if (rarity == COMMON_RARITY) {
+ imageURI = commonImageUri;
+ rareName = COMMON;
+ } else if (rarity == RARE_RARITY) {
+ imageURI = rareImageUri;
+ rareName = RARE;
+ } else {
+ imageURI = legendaryImageUri;
+ rareName = LEGENDARY;
+ }
- string memory imageURI = rarityToUri[rarity];
- string memory rareName = rarityToName[rarity];
// ...
}
Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge about 2 hours ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

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

Give us feedback!