Test this blog by myself
pragma solidity ^0.8.7;
contract Test1 {
function one() public{}
}
contract Test2 {
function one() public payable{}
}
contract Test3 {
function one() external{}
}
contract Test4 {
function one() external payable{}
}
contract Test5 {
error NoPayable(uint256 amounts, address user);
function one() external payable{
if(msg.value>0) {
revert NoPayable({amounts: msg.value, user: msg.sender});
}
}
}
contract Test6 {
function one() external payable{
require((msg.value <= 0),"ERROR");
}
}
contract Test7 {
event NoPayable(uint256 indexed amounts, address indexed user);
function one() external payable{
if(msg.value>0) {
emit NoPayable(msg.value,msg.sender);
}
}
}
Public and External don’t matter
Payable is cheaper than non-payable.
Contract | Gas(deploy) | Gas(executed) |
---|---|---|
1 | 88522 | 24364 |
2 | 85555 | 24337 |
3 | 88522 | 24364 |
4 | 85555 | 24337 |
5 | 132344 | 24366 |
6 | 130481 | 24366 |
7 | 104663 | 24366 |
https://coinsbench.com/advanced-gas-optimizations-tips-for-solidity-85c47f413dc5