Transferring Value
selfbalance()
- exists in Yul but not Solidity, essentiallyaddress(this).balance
The Yul contract will use less gas upon deployment, as well as when calling the withdraw function
// Solidity version
contract WithdrawV1 {
constructor() payable {}
address public constant owner = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
function withdraw() external {
payable(owner).transfer(address(this).balance);
(bool s, ) = payable(owner).call{value: address(this).balance}("");
require(s);
}
}
// Yul version
contract WithdrawV2 {
constructor() payable {}
address public constant owner = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
function withdraw() external {
assembly {
let s := call(gas(), owner, selfbalance(), 0, 0, 0, 0)
if iszero(s) {
revert(0, 0)
}
}
}
}
Last updated