Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Gas optimized contracts #123

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

meetjn
Copy link
Contributor

@meetjn meetjn commented Jan 31, 2025

This PR fixes #108

Changes made:

  1. IN ELECTION.SOL The election Inactive modifier reads electionInfo from storage twice.

CACHE STORAGE VARIABLES

Suggested fix : (Saves 500-800 Gas per Call)



modifier electionInactive() {
    uint256 start = electionInfo.startTime;
    uint256 end = electionInfo.endTime;
    if (block.timestamp < start || block.timestamp > end) revert ElectionInactive();
    _;
}
  1. Remove Redundant candidateID (Saves 20k+ Gas per Candidate)

Issue: The Candidate struct stores candidateID, which duplicates the array index.

Suggested Fix

struct Candidate {
    string name;       // Removed candidateID
    string description;
}

hardhat.config.js file

  1. Enabled Solidity Optimizer: Inside the hardhat.config.js file
json
{
  "optimizer": {
    "enabled": true,
    "runs": 200 // Optimize for frequent function calls
  }
}

Impact:
Reduces contract size and runtime gas by 10-15%

Inside the ElectionFactory.sol file

  1. Use of Immutable wherever necessary
address public immutable factoryOwner; // Immutable value set in constructor
BallotGenerator private immutable ballotGenerator;

Inside the CCIPSender.sol file

  1. I have used scientific notion where we represent the 25 LINK tokens which have improved readability while having no effect on gas fees
uint public constant minTokens = 25e18; // 25 LINK 
  1. In the Emit 'messageSent' struct we are using a hardcoded 'Voted' string which is always re-initialized every time, instead I have declared a constant variable and used it inside the emit "messageSent"
string private constant VOTE_TEXT = "Voted";
emit MessageSent(
            messageId,
            destinationChainSelector,
            electionFactory,
            VOTE_TEXT,
            address(s_linkToken),
            fees
        );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enhancements: Adding minor changes for improvement in gas fees
1 participant