This assignment focused on gas optimizing a smart contract named Distribute.sol
. The key objective was to enhance the efficiency of the distribute()
function within a tight timeframe of one hour, without altering the Solidity version or adding any extra functionalities to the contract.
- Task: Improve the gas cost of calling the
distribute()
function in theDistribute
contract. - Constraints:
- Do not change the Solidity version.
- Do not add extra functionality or behavior to the contract.
- Timeframe: 1 hour.
- Optimize
distribute()
: I decided to focus only on reducing the gas consumption of thedistribute()
function. - Unit Testing for Gas Usage: To save time, I only wrote two unit-tests per contract, my goal was to have enough to just determine the gas usage.
- Documentation: I spent the last time I had left to write this README.
- Immutable Variable: Changed
createTime
to an immutable variabledistributeTime
, added with2 weeks
set at contract deployment instead of insidedistribute()
. - Internal Visibility: Changed the state variables
distributeTime
andcontributors
to internal variables. - Custom Error: Replaced the
require
statement with a custom errorCode1
.
-
distribute()
Function:- Original: 90656 gas
- Optimized: 88321 gas
- OtmimizedContract: 81851 gas (Last task)
-
Deployment Gas Usage:
- Original: 602781 gas
- Optimized: 445416 gas
- OtmimizedContract: 254740 gas (Last task)
- Clone the Repository: Clone the git repository to your local machine.
- Install Dependencies: Run
yarn
to install required dependencies. - Testing: Execute unit tests with
yarn test
to verify functionality and gas usage. - Gas Reporting: The gas usage can be seen using the Hardhat gas reporter. Make sure to have it configured in your Hardhat setup.
- License: MIT
- Author: @dadogg80