0

I have looked at previous answers for this title but they don't seem to help me.
My contract contract is deployed and verified on the Sepolia test net using Remix.
This is the failed transaction
The receiver contract.
My owner wallet, 0xb04d6a4949fa623629e0ED6bd4Ecb78A8C847693, contains: 0.611 SepoliaETH.
Sender contract code:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract Brave {
    // Balance variable to store received Ether
    uint256 private balance;
    address private owner;

    constructor() {
        owner = msg.sender;
        balance = 0;
    }

    modifier onlyOwner() {
        require(owner == msg.sender, "Only owner can perform this action");
        _;
    }

    function getBalance() public view onlyOwner returns (uint256) {
        return balance;
    }

    // Function to receive Ether
    receive() external payable {
        // Increment the balance with the received Ether amount
        balance += msg.value;
    }

    // Function to send Ether to a given address
    function sendEther(
        address payable _recipient,
        uint256 _amount
    ) public onlyOwner {
        require(balance >= _amount, "Insufficient balance");
        _recipient.transfer(_amount);
        balance -= _amount;
    }
}

Any idea how I can fix this?

1 Answer 1

2

The reason is the usage of transfer instead of call.

In the brave contract you are using _recipient.transfer(_amount);

That means this call will only carry 2300 gas for the execution, which only works when there are no additional operations inside the same execution.

But in this case, the recipient is a contract and it contains a receive function, which updates the state variable.

receive() external payable {
    // Increment the balance with the received Ether amount
    balance += msg.value;
}

Which requires more gas than just 2300.

So transfer wouldn't work in this case.

Whenever there is additional work to do along with sending ethers, you should use call instead of transfer.

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.