JaveedYara
JaveedYara

Reputation: 21

Which address is paying the gas fee? And, can the smart contract pay the gas fee by itself?

pragma solidity ^0.8.1;

contract SendMoney{
    uint public publicBalance;
    uint public lockedUntil;

    function receiveMoney() public payable{
        publicBalance += msg.value;
        lockedUntil = block.timestamp + 1;
    }

    function getBalance() public view returns(uint){
        return address(this).balance;
    }

    function withdrawMoney() public{
        if(lockedUntil < block.timestamp){
            address payable to  = payable(msg.sender);
            to.transfer(getBalance());
        }
    }

    function withdrawMoneyTo(address payable _to) public{
        if(lockedUntil < block.timestamp){
            _to.transfer(getBalance());
        }
    }
}

I have deployed a smart contract with some address lets say. I sent some ether to the smart contract with the method receive money. Now, when i press on withdrawMoney() function with some another address. Who will pay the gas fee? is it the address that has deployed the smart contract? or is it the smart contract itself?

Upvotes: 1

Views: 1434

Answers (1)

Petr Hejda
Petr Hejda

Reputation: 43521

The fee for the internal transaction to.transfer(getBalance()); is included in the total fee paid by the user executing the withdrawMoney() function.

It's currently not possible to split the fee to multiple payers (e.g. the user paying for the main transaction, and the contract paying for the internal transaction).

Upvotes: 2

Related Questions