├── .github ├── FUNDING.yml └── workflows │ └── build.yml ├── contracts └── SaveGas.sol ├── README.md └── LICENSE /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | 2 | name: Build 3 | 4 | on: 5 | push: 6 | branches: [ master ] 7 | pull_request: 8 | branches: [ master ] 9 | 10 | jobs: 11 | test: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v2 15 | - name: Set up Ruby 16 | uses: ruby/setup-ruby@v1 17 | with: 18 | ruby-version: 2.6 19 | - name: Install awesome_bot 20 | run: gem install awesome_bot 21 | - name: Check URLs 22 | run: awesome_bot README.md --allow-redirect 23 | -------------------------------------------------------------------------------- /contracts/SaveGas.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.8.1; 2 | 3 | // Source: https://marduc812.com/2021/04/08/how-to-save-gas-in-your-ethereum-smart-contracts/ 4 | contract SaveGas { 5 | uint8 resultA = 0; 6 | uint256 resultB = 0; 7 | 8 | function UseUint() external returns (uint256) { 9 | uint256 selectedRange = 50; 10 | for (uint256 i = 0; i < selectedRange; i++) { 11 | resultB += 1; 12 | } 13 | return resultB; 14 | } 15 | 16 | function UseUInt8() external returns (uint8) { 17 | uint8 selectedRange = 50; 18 | for (uint8 i = 0; i < selectedRange; i++) { 19 | resultA += 1; 20 | } 21 | return resultA; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
28 | 29 | ### Resources 👇 30 | 31 | I. [Research Papers](https://github.com/iskanderandrews/awesome-soliidty-gas-optimization#i-research-papers) 32 | - [Ethereum EVM Research Papers](https://github.com/iskanderandrews/awesome-solidity-gas-optimization/blob/master/README.md#ethereum-evm-research-papers) 33 | 34 | II. [Medium / Articles](https://github.com/iskanderandrews/awesome-soliidty-gas-optimization#medium--articles) 35 | 36 | III. [Q&A / StackOverflow](https://github.com/iskanderandrews/awesome-soliidty-gas-optimization#qa--stackoverflow) 37 | 38 | IV. [Video resources / YouTube](https://github.com/iskanderandrews/awesome-soliidty-gas-optimization#video-resources--youtube) 39 | 40 | V. [Smart-Contract](https://github.com/iskanderandrews/awesome-soliidty-gas-optimization#smart-contract) 41 | 42 | ### I. Research Papers 43 | 44 | - MultiCall: A Transaction-batching Interpreter for Ethereum - 2021 45 | https://dl.acm.org/doi/pdf/10.1145/3457337.3457839 46 | 47 | - IEEE: Static Profiling and Optimization of Ethereum Smart Contracts Using Resource Analysis - 2021 48 | https://ieeexplore.ieee.org/abstract/document/9348894 49 | 50 | - Arxiv: Under-Optimized Smart Contracts Devour Your Money - 2020 51 | https://arxiv.org/pdf/1703.03994.pdf 52 | 53 | - IEEE: Design Patterns for Gas Optimization in Ethereum - 2020 54 | https://ieeexplore.ieee.org/abstract/document/9050163 55 | 56 | - GASOL: Gas Analysis and Optimization for Ethereum Smart Contracts - 2020 57 | http://costa.fdi.ucm.es/papers/costa/AlbertCGRR20.pdf 58 | 59 | - Optimization of solidity smart contracts - 2020 60 | https://repositum.tuwien.at/handle/20.500.12708/1428 61 | 62 | - A Gas-Efficient Superlight Bitcoin Client in Solidity - 2020 63 | https://dl.acm.org/doi/abs/10.1145/3419614.3423255 64 | 65 | - Arxiv: Profiling Gas Leaks in Solidity Smart Contracts - 2020 66 | https://arxiv.org/abs/2008.05449 67 | 68 | - ABCDE—agile block chain DApp engineering - 2020 69 | https://www.sciencedirect.com/science/article/pii/S2096720920300026 70 | 71 | - IEEE: Transformation of Contract Descriptions in a Domain Specific Language to Solidity Assembly - 2020 72 | https://ieeexplore.ieee.org/abstract/document/9325490 73 | 74 | - Developing Cost-Effective Blockchain-Powered Applications: A Case Study of the Gas Usage of Smart Contract Transactions in the Ethereum Blockchain Platform - 2020 75 | http://www.cse.yorku.ca/~zmjiang/publications/tosem2020_zarir.pdf 76 | 77 | - Cost Minimization of Solidity Smart Contracts on Blockchain Systems - 2020 78 | https://www.koreascience.or.kr/article/JAKO202019550426714.page 79 | 80 | - IEEE: GasChecker: Scalable Analysis for Discovering Gas-Inefficient Smart Contracts - 2020 81 | https://ieeexplore.ieee.org/abstract/document/9026761 82 | 83 | - IEEE: Research on Smart Contract Optimization Method on Blockchain - 2019 84 | https://ieeexplore.ieee.org/abstract/document/8832276 85 | 86 | - Springer: Synthesis of Super-Optimized Smart Contracts Using Max-SMT - 2020 87 | https://link.springer.com/chapter/10.1007/978-3-030-53288-8_10 88 | 89 | - Characterizing Efficiency Optimizations in Solidity Smart Contracts - 2020 90 | https://dsg.tuwien.ac.at/team/sschulte/paper/bc2020.pdf 91 | 92 | - A Quantitative Analysis of the Ethereum Fee Market: How Storing Gas Can Result in More Predictable Prices. 93 | https://wwz.unibas.ch/fileadmin/user_upload/wwz/00_Professuren/Schaer_DLTFintech/Lehre/Teaching_Page/Nadler_MasterThesis_2020-1_copy.pdf 94 | 95 | - Springer: Studying gas exceptions in blockchain-based cloud applications - 2020 96 | https://journalofcloudcomputing.springeropen.com/articles/10.1186/s13677-020-00176-9 97 | 98 | - IEEE: Characterizing Efficiency Optimizations in Solidity Smart Contracts - 2019 99 | https://ieeexplore.ieee.org/abstract/document/9284779 100 | 101 | - Springer: Computing Exact Worst-Case Gas Consumption for Smart Contracts - 2018 102 | https://link.springer.com/chapter/10.1007/978-3-030-03427-6_33 103 | 104 | - Gas Cost Analysis for Ethereum Smart Contracts - 2018 105 | https://www.research-collection.ethz.ch/bitstream/handle/20.500.11850/312914/1/TMeineOrdnerMaster-ArbeitenHS18Signer_Christopher.pdf 106 | 107 | - Springer: Optimized Execution of Business Processes on Blockchain - 2017 108 | https://link.springer.com/chapter/10.1007/978-3-319-65000-5_8 109 | 110 | - Saving Deployment Costs of Smart Contracts by Eliminating Gaswasteful Patterns - 2017 111 | http://article.nadiapub.com/IJGDC/vol10_no12/6.pdf 112 | 113 | #### Ethereum EVM Research Papers 114 | - EVM Opcode Gas Costs 115 | https://github.com/djrtwo/evm-opcode-gas-costs 116 | 117 | 118 | ### II. Medium / Articles 119 | 120 | - Basics of Smart Contract Gas Optimization with Solidity - 2022 121 | https://eip2535diamonds.substack.com/p/smart-contract-gas-optimization-with 122 | 123 | - How EIP2535 Diamonds Reduces Gas Costs for Smart Contract Systems - 2022 124 | (Thanks to @mudgen, https://github.com/iskdrews/awesome-solidity-gas-optimization/issues/1) 125 | https://eip2535diamonds.substack.com/p/how-eip2535-diamonds-reduces-gas 126 | 127 | - How to Save Gas in your Ethereum Smart Contracts - 2021 128 | https://marduc812.com/2021/04/08/how-to-save-gas-in-your-ethereum-smart-contracts/ 129 | 130 | - Solidity Quick Tip: Efficiently Swap Two Variables - 2021 131 | https://blog.oliverjumpertz.dev/solidity-quick-tip-efficiently-swap-two-variables 132 | 133 | - How to Write Smart Contracts That Optimize Gas Spent on Ethereum - Medium 134 | https://betterprogramming.pub/how-to-write-smart-contracts-that-optimize-gas-spent-on-ethereum-30b5e9c5db85 135 | 136 | - Optimizing your Solidity contract’s gas usage - Medium 137 | https://medium.com/coinmonks/optimizing-your-solidity-contracts-gas-usage-9d65334db6c7 138 | 139 | - Solidity gas optimization tips - Article 140 | https://mudit.blog/solidity-gas-optimization-tips/ 141 | 142 | - Storage vs. Memory vs. Stack in Solidity & Ethereum 143 | https://dlt-repo.net/storage-vs-memory-vs-stack-in-solidity-ethereum/ 144 | 145 | - 10 solidity code patterns that optimize the gas utilization of Ethereum smart contracts - 2019 146 | https://blog.krybot.com/a?ID=01600-56f06856-e060-4787-93a3-0ffb088cd88f 147 | 148 | - How to reduce gas cost in Solidity - 2019 149 | https://medium.com/layerx/how-to-reduce-gas-cost-in-solidity-f2e5321e0395 150 | 151 | - Gas Optimization in Solidity Part I: Variables - 2019 https://medium.com/coinmonks/gas-optimization-in-solidity-part-i-variables-9d5775e43dde 152 | 153 | - How to optimize gas cost in a Solidity smart contract? 6 tips - 2019 154 | https://eattheblocks.com/how-to-optimize-gas-cost-in-a-solidity-smart-contract-6-tips/ 155 | 156 | - Solidity tips and tricks to save gas and reduce bytecode size - 2019 157 | https://blog.polymath.network/solidity-tips-and-tricks-to-save-gas-and-reduce-bytecode-size-c44580b218e6 158 | 159 | - Save Gas with Packing in Solidity - 2019 160 | https://www.maxgrok.com/posts/one-hundred-and-nineth-post 161 | 162 | - The million dollar question (RU) - 2018 163 | https://habr.com/ru/company/raiffeisenbank/blog/354122/ 164 | 165 | - Solidity gas optimization - small int types and structs - 2018 166 | https://danielszego.blogspot.com/2018/01/solidity-gas-optimization-small-int.html 167 | 168 | - 8 Ways of Reducing the Gas Consumption of your Smart Contracts - 2018 169 | https://medium.com/coinmonks/8-ways-of-reducing-the-gas-consumption-of-your-smart-contracts-9a506b339c0a 170 | 171 | - How to Reduce Solidity Gas Costs Full Guide - 2022 172 | https://moralis.io/how-to-reduce-solidity-gas-costs-full-guide/ 173 | 174 | - A Collection of Gas Optimisation Tricks - 2022 175 | https://forum.openzeppelin.com/t/a-collection-of-gas-optimisation-tricks/19966 176 | 177 | - Solidity Gas Optimizations Cheat Sheet - 2022 178 | https://0xmacro.com/blog/solidity-gas-optimizations-cheat-sheet/ 179 | 180 | ### III. Q&A / StackOverflow 181 | 182 | - How to write an optimized (gas-cost) smart contract? - StackOverflow 183 | https://ethereum.stackexchange.com/questions/28813/how-to-write-an-optimized-gas-cost-smart-contract 184 | 185 | - Why does uint8 cost more gas than uint256? - StackOverflow 186 | https://ethereum.stackexchange.com/questions/3067/why-does-uint8-cost-more-gas-than-uint256 187 | 188 | - Use string type or bytes32? - StackOverflow 189 | https://ethereum.stackexchange.com/questions/11556/use-string-type-or-bytes32 190 | 191 | - Integer types; createPerson function alternatives; Gas; Optimization; View v Pure functions; Non-iterable nature of mappings - 2020 192 | https://forum.ivanontech.com/t/integer-types-createperson-function-alternatives-gas-optimization-view-v-pure-functions-non-iterable-nature-of-mappings-by-jon-m/12169 193 | 194 | - What’s the best way to optimize the bytecode size of a contract? - 2019 195 | https://forum.openzeppelin.com/t/whats-the-best-way-to-optimize-the-bytecode-size-of-a-contract/981 196 | 197 | - Gas optimization for my smart contract? - 2017 198 | https://ethereum.stackexchange.com/questions/13472/gas-optimization-for-my-smart-contract 199 | 200 | ### IV. Video resources / YouTube 201 | 202 | - EVM Basics — Macro Hackathons - 2022 203 | https://www.youtube.com/watch?v=LNU4JLo_KAA 204 | 205 | - Gas Golf | Solidity 0.8 - 2022 206 | https://www.youtube.com/watch?v=4r20M9Fr8lY 207 | 208 | - EVM Bytecode ABI Gas and Gas Price - 2021 209 | https://www.youtube.com/watch?v=HcOWNxL3Iy0 210 | 211 | - Simple Trick to Save Gas in Your Smart Contract - 2021 212 | https://www.youtube.com/watch?v=l2OxNQcb8e8 213 | 214 | - Storage, Memory and Calldata | Solidity 0.8 215 | https://www.youtube.com/watch?v=wOCIhzAuhgs 216 | 217 | - Simple Storage | Solidity 0.8 218 | https://www.youtube.com/watch?v=YNu4wH2DXoU 219 | 220 | - EVM: From Solidity to byte code, memory and storage - 2020 221 | https://www.youtube.com/watch?v=RxL_1AfV7N4 222 | 223 | - Three tips for optimizing gas - 2019 224 | https://blockbites.io/bites/bite2.html 225 | 226 | - Learn Solidity (0.5) - Gas and Gas Price - 2019 227 | https://www.youtube.com/watch?v=oTS9uxU6cAM 228 | 229 | - DAPPCON 2018: Solidity Dapp Optimization Gonçalo Sá (ConsenSys Diligence) - 2018 230 | https://www.youtube.com/watch?v=qwBkeJ84d2g&t=68s 231 | 232 | - Less Gas, More Fun: Optimising Smart Contracts through Yul by Christian Reitwiessner (Devcon4) - 2018 233 | https://www.youtube.com/watch?v=VH4MgZDyZJU 234 | 235 | - Gas optimization in solidity: 10tips by Julien Klepatch (EatTheBlocks) - 2019 236 | https://www.youtube.com/watch?v=PYilP2bjtwc 237 | 238 | ### V. Smart-Contracts: 239 | - Playpen: is a set of modern, gas-optimized staking pool contracts - 2022 240 | https://github.com/ZeframLou/playpen 241 | 242 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------