From 0f86b6aad46c53cf79a8aa6e7b8f4ea3f4ee5427 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?BT3GL=20/ba=C9=AAt=C9=A1=C9=9D=C9=AB/?= <1130416+bt3gl@users.noreply.github.com> Date: Sat, 14 May 2022 13:46:52 -0700 Subject: [PATCH] Add tricks to save gas --- Solidity-Expert/Tricks-to-save-gas/README.md | 80 ++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/Solidity-Expert/Tricks-to-save-gas/README.md b/Solidity-Expert/Tricks-to-save-gas/README.md index 56c65b6..f5d5ce1 100644 --- a/Solidity-Expert/Tricks-to-save-gas/README.md +++ b/Solidity-Expert/Tricks-to-save-gas/README.md @@ -2,3 +2,83 @@
+ +- *In DeFi, any gas saving is an edge. Below are some notes on tricks we use.* +- Note that in Solidity, the maximum size of a contract is restricted to 24 KB by [EIP 170](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-170.md). + +--- + +## Function names + +- In Ninja we brute force hashes of function names to find those that starts `0000`, so this can save around 50 gas +- TBA: show the code + +--- + +## Pack variables + +The below code is an example of poor code and will consume 3 storage slot: + +``` +uint8 numberOne; +uint256 bigNumber; +uint8 numberTwo; +``` + +A much more efficient way to do this in solidity will be: + +``` +uint8 numberOne; +uint8 numberTwo; +uint256 bigNumber; +``` + +--- + +### Mappings are cheaper than Arrays + +- An array is not stored sequentially in memory but as a mapping. +- You can pack Arrays but not Mappings. +- It’s cheaper to use arrays if you are using smaller elements like `uint8` which can be packed together. +- You can’t get the length of a mapping or parse through all its elements, so depending on your use case, you might be forced to use an Array even though it might cost you more gas. + +--- + +## **Use bytes32 rather than string/bytes** + +- If you can fit your data in 32 bytes, then you should use bytes32 datatype rather than bytes or strings as it is much cheaper in solidity. +- Any fixed size variable in solidity is cheaper than variable size. + +--- + +## Use external function modifier + +- For all the public functions, the input parameters are copied to memory automatically, and it costs gas. +- If your function is only called externally, then you should explicitly mark it as external. +- External function’s parameters are not copied into memory but are read from `calldata` directly. +- By the way, calling internal functions is cheaper. + +--- + +## Delete variables that you don’t need + +- If you don’t need a variable anymore, you should delete it using the delete keyword provided by solidity or by setting it to its default value. + +--- + +## **No need to initialize variables with default values** + +- If a variable is not set/initialized, it is assumed to have the default value (0, false, 0x0 etc depending on the data type). If you explicitly initialize it with its default value, you are just wasting gas. + +``` +uint256 hello = 0; //bad, expensive +uint256 world; //good, cheap +``` + +--- + +## **Make use of single line swaps** + +``` +(hello, world) = (world, hello) +```