A sequence of smart contracts to practice gas optimization. These are used as practice assignments for RareSkills.io and the Udemy Gas Optimization Course

Overview

RareSkills Gas Puzzles

Puzzles that are ready for you

  • Distribute (hard)
  • Array Sum (easy)
  • Escrow
  • EscrowV2
  • Mint
  • Presale
  • Require (easy)
  • Staking

Contributors

DO NOT COMMIT SOLUTIONS, BE SURE TO PUT ANSWERS IN contracts/contracts_optimized to ensure they fall into the .gitignore

Players

Your goal is to optimize the contracts such that they reach the target efficiency.

Rules

  • you may not change the optimizer level
  • you may not change the solidity version
  • you may refactor functionality as long as you don't break the business logic
  • you may make reasonable assumptions about what variable sizes are necessary to get things done
  • you may remove unnecessary or redunant logic (some have been intentionally added)
  • because making functions payable is a controversial optimization, you do not need to make functions payable to reach the gas target unless the function needs to be payable to fulfill its business logic

Testing

As mentioned above, optimized contracts should be created in their own sub-folder to prevent commiting them publically.

The file structure should look similar to this:

- GasPuzzles
  |_
    contracts
    |_
      contracts_optimized
    | |_
    |   ArraySum.sol
    |   Distribute.sol
    |   ..
    |   ...
    |
    ArraySum.sol
    Distribute.sol
    ..
    ...

Within the contracts that are optimized be sure to follow the following naming convention to ensure tests run smoothly:

contract OptimizedArraySum {

...

contract OptimizedDistribute {

...

All tests can be run with: npx hardhat test

Specific tests can be run with: npx hardhat test ./test/arraySum.js

Comments
  • Fix/distribute contract balance setup

    Fix/distribute contract balance setup

    The tests are failing because the contract instance is always with balance=0 means that the transfer is sending zero values

    Not sure if this was intentional and part of the puzzle, if it's the case let me know and I will delete the MR

    opened by uranium93 4
  • Trouble with Distribute challenge

    Trouble with Distribute challenge

    I am having some trouble trying to optimize the Distribute contract. I believe that I need to do something about the transfer functions. I tried putting the 4 transfers in a for loop and also defining a local memory array that copies the contributors storage array, so that it does not get cold access for each element. But nothing of that worked. Could you give some tip that can help me solve this challenge? Thank you

    opened by EWCunha 3
  • New puzzle/sorting

    New puzzle/sorting

    This is a purposed new Gas Puzzle Challenge.

    There are few fairly obvious techniques to get towards the gas target. However, some research will likely be required to get the last 561 gas shaved off. The starting gas consumption is 29_488.

    The test cases for this challenge are certainly not exhaustive, and leave room for further golfing. Perhaps if this challenge gets some traction there could be a follow up to handle non-sequential values or have the function remove duplicates. Screen Shot 2022-11-15 at 1 53 23 PM

    opened by burke-md 1
  • Specific Test Typo Rectified

    Specific Test Typo Rectified

    If we run a specific test with npx hardhat test test/arraySum, it will give the error:

    Error: Cannot find module '/workspaces/gas-puzzles/test/arraySum'
    

    It has to be ArraySum instead of arraySum.

    opened by remedcu 1
  • Business logic unit tests

    Business logic unit tests

    There should be unit tests to ensure people aren't hardcoding solutions. These should pass if the logic is adhered to and not measure the gas cost. This is to let people know they didn't break things as they update the contract.

    opened by jeffreyscholz 1
  • Testing/nonpayable/i3

    Testing/nonpayable/i3

    This PR is dirrectly associated with issue #3 (tests to ensure functions remain non-payable).

    I've established a seperate file for each gas puzzle and added notes in the readme to help users run either all tests or tests associated with one puzzle at a time. A test has been written to ensure the main function in each puzzle remains non-payable (there are supporting functions that could also be restricted depending on your intended outcome). Also there are two contracts that have payable functions (Mint, Require), these contracts have files created with place holder tests.

    At this time running tests without a file at contracts/contracts_optimized/<puzzle_name>.sol with a contract named Optimized<Contract_name> will cause the tests to throw an error. However, in the short term, this will help keep answers out of the commit history. Perhaps after all development has been completed, blank contracts with the propper naming convention can be included for users.

    Screenshot from 2022-09-29 18-55-07

    opened by burke-md 0
  • Gitcoin - Create a new gas puzzle!

    Gitcoin - Create a new gas puzzle!

    Contribute a new gas puzzle to the RareSkills.io repo. To qualify, your gas puzzle must:

    • Follow the same pattern as the ones in this repo
    • Inefficient code must be realistic and clean. The point is to teach developers how to make reasonable code great, not how to fix ugly code.
    • The gas target must require using a non-obvious trick or two, but should not require excessive and highly advanced optimization, unless explicitly marked as such. (See ERC165 for an example)
    • The problem should be realistic. Puzzles that are clearly useless like "discover if the calldata is a palindrome" won't be accepted.
    • However, solutions don't have to be safe or realistic. The point is to drill knowledge of the EVM.

    For this bounty, only one solution will be accepted.

    opened by RareSkills 1
Owner
RareSkills
An elite bootcamp where developers master solidity and Ethereum development. Join as a student or recruit web3 developers at RareSkills.io
RareSkills
Lecture by lecture commits of the https://www.udemy.com/build-blockchain-full-stack/ course

Commit-by-commit breakdown of "Build a Blockchain & Cryptocurrency | Full-Stack Edition" This is a commit-by-commit breakdown of "Build a Blockchain &

David Katz 9 Dec 12, 2022
this is a single-page web application. we built a book website where the user can add , remove and display books. we used modules to implement these functionalities. also, we used the Date class to display the date and time.

Awsome Books In this Project, we have built A Books websites. Built With ?? HTML CSS javascript Git & Github Live Demo Here you can find the live Demo

Nedjwa Bouraiou 10 Aug 3, 2022
We are students of group named "Special-Team" of GоIT academy. We graduated JavaScript course and for consolidate in practice 📌 knowledges received on this course, we together 🤝 developed graduation project

Проект сайту "Filmoteka" Привіт! ?? Ми студенти групи під назвою "Special-Team" академії GоIT ?? ?? Ми закінчили курс JavaScript і для того, щоб закрі

Oksana Banshchykova 12 Jan 3, 2023
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Ethereum smart contract gas cost waste pattern detection and patching tool

Ethereum smart contract gas cost waste pattern detection and patching tool

ibelab 4 Mar 23, 2022
All five assignments and the final group project is done in class CSCI5410 (Serverless Data Processing) Fall 2021 of MACS at Dalhousie University.

Dalhousie University | Fall 2021 | CSCI5410 | SDP (Serverless Data Processing) All five assignments and the final group project is done in class CSCI5

Dhrumil Shah 1 Dec 26, 2021
This Repository consist of Daily learning JS, Assignments, coding challenge, projects, references, tutorial

?? A Tour of JavaScript This Repository consist of Daily learning, Assignments, coding challenge, projects, references, tutorial. ??‍?? ??‍?? alert(

null 23 Sep 7, 2022
Homework Assignments for Visualization for Data Science, Fall 2022, University of Utah

Homeworks for Utah's Vis for Data Science Course In subfolders in this directory you will find the homeworks for CS 6630 / CS 5630 / DS 4630 – Visuali

Datavis Course 19 Nov 14, 2022
Full-stack-todo-rust-course - we are building this out now in prep for the real course

full-stack-todo-rust-course wip - we are building this out now in prep for the real course Plan Come up with the requirements Create user stories Desi

Brooks Builds 89 Jan 2, 2023
Course material for a ~10 hours introductionary course for Julia. Topics: Introduction, Parallel Programming, Data Science

Development We use Franklin.jl to generate the lecture material. To do so, simply activate the environment, use Franklin and run the local server: act

GregorE 3 Dec 15, 2022
Vantage is a web optimization tool designed for NEXTjs apps.

VANTAGE Vantage is a web optimization tool designed for NEXTjs apps. Uses Google lighthouse under the hood to determine key web vital scores and impro

OSLabs Beta 107 Dec 20, 2022
Ethereum Smart Contracts for locking your Ether and ERC20 tokens based on time and price conditions

SmartHold - a simple way to lock and hold your ETH or ERC20 in a smart contract This is a BETA software that has not been audited for security. USE AT

Paweł Urbanek 22 May 5, 2022
Yet another library for generating NFT artwork, uploading NFT assets and metadata to IPFS, deploying NFT smart contracts, and minting NFT collections

eznft Yet another library for generating NFT artwork, uploading NFT assets and metadata to IPFS, deploying NFT smart contracts, and minting NFT collec

null 3 Sep 21, 2022
17ᵗʰ Project developed as assessment practice during Driven's Full Stack Develpment course

Linkr | Back end WIP Full Stack Development Project Browse Nodejs code» - Front end code» Built With Study Playlist In this section I included all You

Nivaldo Farias 2 Aug 23, 2022
17ᵗʰ Project developed as assessment practice during Driven's Full Stack Develpment course

Linkr | Front end WIP Full Stack Development Project Browse JSX code» - Back end code» Built With Study Playlist In this section I included all Youtub

Nivaldo Farias 3 Jun 20, 2022
♦ Crowd funding project using Smart Contracts on the Ethereum. Created with Next.js and Tailwind CSS.

Crowdcoin ♦ Crowd funding project using Smart Contracts on the Ethereum. Created with Next.js and Tailwind CSS. Project from "Ethereum and Solidity: T

Luiz Fernando Veríssimo 2 Dec 14, 2022
Run CosmWasm smart contracts in Node.js and web browsers

Run CosmWasm smart contracts in Node.js and web browsers

Terran One 31 Nov 25, 2022