-
Notifications
You must be signed in to change notification settings - Fork 48
Skipper-go Solidity Contract Gas Optimization #23
base: main
Are you sure you want to change the base?
Conversation
| uint256 fee; | ||
| } | ||
|
|
||
| // --------------------- Errors -------------------- // |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use custom errors to save gas
| address fromToken, | ||
| uint256 fromAmount, | ||
| DexHop[] memory route | ||
| DexHop[] calldata route |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
route is not mutated in the function body so using calldata would save some gas.
| // Loop through the route and execute the trades | ||
| for (uint256 i = 0; i < route.length; i++) { | ||
| DexHop memory hop = route[i]; | ||
| uint256 length = route.length; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Caching array length to avoid repeatedly loading from the state in each iteration.
| (bool success, ) = owner().call{value: balance}(""); | ||
| if (!success) revert WithdrawNativeBalanceFailed(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.transfer has a hardcoded 2300 gas limit which prevents smart contracts from doing things like sending ether to a gnosis safe due to out of gas. .call instead forwards all gas and is now the recommended way to send ether from a contract. It's also reentrancy safe here since the function is only accessible by the owner.
A few gas optimizations that I nitpicked while checking out the codebase: