0

Consider the contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Test {
    address payable public owner;

    constructor() {
        owner = payable(msg.sender);
    }

    function deposit() public payable {}

    function withdraw() public {
        require(msg.sender == owner, "Owner issue");

        owner.transfer(address(this).balance);
    }
}

Withdrawing from this contract costs around 34,900 gas.

I found out that if I use payable(msg.sender) instead of the owner, it costs less gas, around 34,700.

payable(msg.sender).transfer(address(this).balance);

I am not completely understanding why is this happening. Won't sending ETH to an already payable address will cost less than casting an address to payable then sending? Any answer would be helpful.

1 Answer 1

1

owner is read from storage, which means there's a SLOAD opcode, which costs 100 gas.

msg.sender is read using CALLER opcode, which costs 2 gas.

This explains around 100 gas difference. I'm not sure where the other 100 comes from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.