Skip to content

Commit

Permalink
adding 10 solady safeTransferEth
Browse files Browse the repository at this point in the history
  • Loading branch information
devdacian committed Jan 23, 2025
1 parent 98fbbb2 commit d37a7d0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function resetId(uint256 id) public {

### #9 Use `msg.sender` Instead Of `owner()`: EFFECTIVE 0.84% CHEAPER ###
When `msg.sender` is guaranteed to be `owner()` such as inside `onlyOwner` functions, it is cheaper to use `msg.sender`:
```solidity
```diff
function sendETHToOwner() external virtual onlyOwner {
uint256 ethBal = address(this).balance;

Expand All @@ -149,4 +149,20 @@ function sendETHToOwner() external virtual onlyOwner {
}
```

### #10 Use Solady `SafeTransferLib::safeTransferETH` Instead Of Solidity `call()` : EFFECTIVE 0.35% CHEAPER ###
When sending ETH, it is cheaper to use Solady's `safeTransferETH` function:
```diff
+import {SafeTransferLib} from "@solady/utils/SafeTransferLib.sol";

function sendETHToOwner() external virtual onlyOwner {
uint256 ethBal = address(this).balance;

if(ethBal > 0) {
- (bool sent, ) = msg.sender.call{value: ethBal}("");
- if(!sent) revert EthTransferFailed();
+ SafeTransferLib.safeTransferETH(msg.sender, ethBal);
}
}
```


18 changes: 18 additions & 0 deletions src/10-solady-safeTransferEth/SoladySafeTransferEth.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import {OwnerVault} from "../OwnerVault.sol";

import {SafeTransferLib} from "@solady/utils/SafeTransferLib.sol";

contract SoladySafeTransferEth is OwnerVault {
function sendETHToOwner() external override onlyOwner {
uint256 ethBal = address(this).balance;

if(ethBal > 0) {
SafeTransferLib.safeTransferETH(msg.sender, ethBal);
}
}
}


28 changes: 28 additions & 0 deletions test/10-solady-safeTransferEth/SoladySafeTransferEthTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import {OwnerVaultTest} from "../OwnerVaultTest.sol";
import {SoladySafeTransferEth} from "../../src/10-solady-safeTransferEth/SoladySafeTransferEth.sol";

// Optimizer ON, 10000 runs
// ========================
// Pre : 14420 gas
// forge test --optimizer-runs 10000 --match-contract MsgSenderNotOwnerTest --match-test test_sendETHToOwner -vvv
// Post : 14368 gas (0.36% cheaper)
// forge test --optimizer-runs 10000 --match-contract SoladySafeTransferEthTest --match-test test_sendETHToOwner -vvv
//
// Optimizer OFF
// =============
// Pre : 14456 gas
// forge test --match-contract MsgSenderNotOwnerTest --match-test test_sendETHToOwner -vvv
// Post : 14404 gas (0.36% cheaper)
// forge test --match-contract SoladySafeTransferEthTest --match-test test_sendETHToOwner -vvv
//
// Conclusion
// ==========
// Using Solady SafeTransferEth cheaper than standard Solidity using `call`
contract SoladySafeTransferEthTest is OwnerVaultTest {
function _createContract() internal override {
ownerVault = new SoladySafeTransferEth();
}
}

0 comments on commit d37a7d0

Please sign in to comment.