One gas rule
Storage is the expensive part. Keep less data on-chain, write it fewer times, and avoid functions that may loop over growing storage.
What gas pays for
A contract is code plus data at an address. The data is state. It stays between transactions, so the EVM charges more when you initialize or change it.
Memory is temporary for one call. Calldata is the input sent to an external function. Use these when you do not need to save something permanently.
Design cheaper state
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done
Cheaper registry
This pattern stores only what the contract needs for checks. The name is emitted for the app and block explorer, but it is not stored forever.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.37;
contract ProjectRegistry {
address public immutable organizer;
mapping(address => bool) public submitted;
event ProjectSubmitted(address indexed builder, string repoUrl);
constructor(address _organizer) {
organizer = _organizer;
}
function submitProject(string calldata repoUrl) external {
require(!submitted[msg.sender], "Already submitted");
submitted[msg.sender] = true;
emit ProjectSubmitted(msg.sender, repoUrl);
}
}Use constants well
If a value never changes, do not store it like normal state. Solidity has constant and immutable variables, and both have much lower gas costs than regular state variables.
Use constant for compile-time values. Use immutable when the value is known during deployment, like an organizer address passed to the constructor.
Loops can brick features
A loop over a growing storage array can become too expensive. If it grows beyond what fits in a block, that function can stop being usable.
Do not write payoutAllStudents or verifyEverySubmission if the list can grow during a hackathon. Let users claim one by one, or process small batches with a clear limit.
Review with AI
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 4 done
Do this now
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done