You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Booleans are more expensive than uint256 or any type that takes up a full// word because each write operation emits an extra SLOAD to first read the// slot's contents, replace the bits taken up by the boolean, and then write// back. This is the compiler's defense against contract upgrades and// pointer aliasing, and it cannot be disabled.
function _getPastDelegate(addressaccount, uint256blockNumber) internalviewreturns (address) {
State variables should be cached in stack variables rather than re-reading them from storage
The instances below point to the second access of a state variable within a function.
Less obvious optimizations include having local storage variables of mappings within state variable mappings or mappings within state variable structs, having local storage variables of structs within mappings, or having local caches of state variable contracts/addresses.
Usage of uints/ints smaller than 32 bytes (256 bits) incurs overhead
When using elements that are smaller than 32 bytes, your contract’s gas usage may be higher. This is because the EVM operates on 32 bytes at a time. Therefore, if the element is smaller than that, the EVM must use more operations in order to reduce the size of the element from 32 bytes to the desired size.
Use custom errors rather than revert()/require() strings to save deployment gas
File: contracts/HolyPaladinToken.sol (Various lines throughout the file)
File: contracts/PaladinRewardReserve.sol (Various lines throughout the file)
Functions guaranteed to revert when called by normal users can be marked payable
If a function modifier such as onlyOwner is used, the function will revert if a normal user tries to pay the function. Marking the function as payable will lower the gas cost for legitimate callers because the compiler will not include checks for whether a payment was provided.
QA & gas optimizations changes are done in the PR: PaladinFinance/Paladin-Tokenomics#6
(some changes/tips were implemented, others are noted but won't be applied)
Refactor structs and binary searches into a common function
Use a more recent version of solidity
Use a solidity version of at least 0.8.10 to have external calls skip contract existence checks if the external call has a return value
Using
bool
s for storage incurs overheadhttps://github.com/OpenZeppelin/openzeppelin-contracts/blob/58f635312aa21f947cae5f8578638a85aa2519f5/contracts/security/ReentrancyGuard.sol#L23-L27
Using
> 0
costs more gas than!= 0
when used on auint
in arequire()
statementIt costs more gas to initialize variables to zero than to let the default of zero be applied
internal
functions only called once can be inlined to save gasinternal
functions not called by the contract should be removed to save deployment gasState variables should be cached in stack variables rather than re-reading them from storage
The instances below point to the second access of a state variable within a function.
Less obvious optimizations include having local storage variables of mappings within state variable mappings or mappings within state variable structs, having local storage variables of structs within mappings, or having local caches of state variable contracts/addresses.
safe224(currentTotalLocked),
safe224(currentTotalLocked),
safe224(currentTotalLocked),
safe224(currentTotalLocked),
_moveDelegates(delegates[from], delegates[to], amount);
currentDropPerSecond = endDropPerSecond;
Splitting
require()
statements that use&&
saves gasSee this issue for an example
Usage of
uints
/ints
smaller than 32 bytes (256 bits) incurs overheadhttps://docs.soliditylang.org/en/v0.8.11/internals/layout_in_storage.html
Use a larger size then downcast where needed
uint48 startTimestamp;
uint48 duration;
uint224 total;
uint32 fromBlock;
uint32 fromBlock;
uint224 votes;
uint32 fromBlock;
Using
private
rather thanpublic
for constants, saves gasIf needed, the value can be read from the verified contract source code
Duplicated
require()
/revert()
checks should be refactored to a modifier or functionMultiplication/division by two should use bit shifting
<x> * 2
is equivalent to<x> << 1
and<x> / 2
is the same as<x> >> 1
require()
orrevert()
statements that check input arguments should be at the top of the functionChecks that involve constants should come before checks that involve state variables
Use custom errors rather than
revert()
/require()
strings to save deployment gasFunctions guaranteed to revert when called by normal users can be marked
payable
If a function modifier such as
onlyOwner
is used, the function will revert if a normal user tries to pay the function. Marking the function aspayable
will lower the gas cost for legitimate callers because the compiler will not include checks for whether a payment was provided.Not using the named return variables when a function returns, wastes deployment gas
The text was updated successfully, but these errors were encountered: