Suggestion: A few minor gas optimizations for the example contracts

Abierto
#46 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
48/100
Tipo de issue
Refactorización
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
solidity
Área
blockchain

Línea de trabajo

Start with VRFConsumerV2.sol and KeepersCounter.sol, focusing on the s_owner declaration, constructor, onlyOwner checks, and performUpkeep. Apply the two requested gas-oriented changes while preserving behavior, then verify that ownership checks and upkeep reverts still work as expected.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Hi team,

First of all, thanks for this excellent Foundry starter kit! It's a great resource for the community.

While looking through the example contracts (VRFConsumerV2.sol and KeepersCounter.sol), I noticed a couple of small gas optimization opportunities that could be nice additions to make the templates even more efficient.

1. Make s_owner immutable in VRFConsumerV2.sol

The s_owner state variable is set in the constructor and never changed afterwards. Marking it as immutable would save significant gas on every read by replacing an expensive SLOAD opcode with a cheap PUSH of the value directly from the contract's bytecode.

Suggestion:

- address public s_owner;
+ address public immutable s_owner;

Approximate Gas Savings: This typically saves ~2000 gas each time the onlyOwner modifier is checked, as it avoids a storage read.

2. Use Custom Errors in KeepersCounter.sol

The require statement in performUpkeep uses a string message ("Time interval not met"). Since Solidity 0.8.4, using custom errors is more gas-efficient both at deployment and during runtime reverts.

Suggestion:

// At the contract level, above the constructor
+ error UpkeepNotNeeded();

// In the performUpkeep() function
- require(upkeepNeeded, "Time interval not met");
+ if (!upkeepNeeded) {
+     revert UpkeepNotNeeded();
+ }

Approximate Gas Savings: This provides two benefits:

  1. Deployment: A small saving by not storing the 23-byte revert string in the contract bytecode.
  2. Runtime (on revert): A more significant saving of ~250-400 gas each time the transaction reverts. This is because a custom error avoids the memory allocation and copying operations required for a revert string.

These are just small suggestions to make the starter kit even better. Thanks again for the great work!

Lenguaje dominante
Solidity
Estrellas
364
Forks
107
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de smartcontractkit/foundry-starter-kit

Todos los issues de smartcontractkit/foundry-starter-kit

Issues similares

Más issues de Blockchain

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.